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"
34 * backref_node, mapping_node and tree_block start with this
37 struct rb_node rb_node
;
42 * present a tree block in the backref cache
45 struct rb_node rb_node
;
47 /* objectid tree block owner */
49 /* list of upper level blocks reference this block */
50 struct list_head upper
;
51 /* list of child blocks in the cache */
52 struct list_head lower
;
53 /* NULL if this node is not tree root */
54 struct btrfs_root
*root
;
55 /* extent buffer got by COW the block */
56 struct extent_buffer
*eb
;
57 /* level of tree block */
59 /* 1 if the block is root of old snapshot */
60 unsigned int old_root
:1;
61 /* 1 if no child blocks in the cache */
62 unsigned int lowest
:1;
63 /* is the extent buffer locked */
64 unsigned int locked
:1;
65 /* has the block been processed */
66 unsigned int processed
:1;
67 /* have backrefs of this block been checked */
68 unsigned int checked
:1;
72 * present a block pointer in the backref cache
75 struct list_head list
[2];
76 struct backref_node
*node
[2];
83 struct backref_cache
{
84 /* red black tree of all backref nodes in the cache */
85 struct rb_root rb_root
;
86 /* list of backref nodes with no child block in the cache */
87 struct list_head pending
[BTRFS_MAX_LEVEL
];
92 * map address of tree root to tree
95 struct rb_node rb_node
;
100 struct mapping_tree
{
101 struct rb_root rb_root
;
106 * present a tree block to process
109 struct rb_node rb_node
;
111 struct btrfs_key key
;
112 unsigned int level
:8;
113 unsigned int key_ready
:1;
117 #define INODEVEC_SIZE 16
120 struct list_head list
;
121 struct inode
*inode
[INODEVEC_SIZE
];
125 #define MAX_EXTENTS 128
127 struct file_extent_cluster
{
130 u64 boundary
[MAX_EXTENTS
];
134 struct reloc_control
{
135 /* block group to relocate */
136 struct btrfs_block_group_cache
*block_group
;
138 struct btrfs_root
*extent_root
;
139 /* inode for moving data */
140 struct inode
*data_inode
;
141 struct btrfs_workers workers
;
142 /* tree blocks have been processed */
143 struct extent_io_tree processed_blocks
;
144 /* map start of tree root to corresponding reloc tree */
145 struct mapping_tree reloc_root_tree
;
146 /* list of reloc trees */
147 struct list_head reloc_roots
;
152 int create_reloc_root
;
153 unsigned int found_file_extent
:1;
154 unsigned int found_old_snapshot
:1;
157 /* stages of data relocation */
158 #define MOVE_DATA_EXTENTS 0
159 #define UPDATE_DATA_PTRS 1
162 * merge reloc tree to corresponding fs tree in worker threads
165 struct btrfs_work work
;
166 struct reloc_control
*rc
;
167 struct btrfs_root
*root
;
168 struct completion
*done
;
169 atomic_t
*num_pending
;
172 static void mapping_tree_init(struct mapping_tree
*tree
)
174 tree
->rb_root
= RB_ROOT
;
175 spin_lock_init(&tree
->lock
);
178 static void backref_cache_init(struct backref_cache
*cache
)
181 cache
->rb_root
= RB_ROOT
;
182 for (i
= 0; i
< BTRFS_MAX_LEVEL
; i
++)
183 INIT_LIST_HEAD(&cache
->pending
[i
]);
184 spin_lock_init(&cache
->lock
);
187 static void backref_node_init(struct backref_node
*node
)
189 memset(node
, 0, sizeof(*node
));
190 INIT_LIST_HEAD(&node
->upper
);
191 INIT_LIST_HEAD(&node
->lower
);
192 RB_CLEAR_NODE(&node
->rb_node
);
195 static struct rb_node
*tree_insert(struct rb_root
*root
, u64 bytenr
,
196 struct rb_node
*node
)
198 struct rb_node
**p
= &root
->rb_node
;
199 struct rb_node
*parent
= NULL
;
200 struct tree_entry
*entry
;
204 entry
= rb_entry(parent
, struct tree_entry
, rb_node
);
206 if (bytenr
< entry
->bytenr
)
208 else if (bytenr
> entry
->bytenr
)
214 rb_link_node(node
, parent
, p
);
215 rb_insert_color(node
, root
);
219 static struct rb_node
*tree_search(struct rb_root
*root
, u64 bytenr
)
221 struct rb_node
*n
= root
->rb_node
;
222 struct tree_entry
*entry
;
225 entry
= rb_entry(n
, struct tree_entry
, rb_node
);
227 if (bytenr
< entry
->bytenr
)
229 else if (bytenr
> entry
->bytenr
)
238 * walk up backref nodes until reach node presents tree root
240 static struct backref_node
*walk_up_backref(struct backref_node
*node
,
241 struct backref_edge
*edges
[],
244 struct backref_edge
*edge
;
247 while (!list_empty(&node
->upper
)) {
248 edge
= list_entry(node
->upper
.next
,
249 struct backref_edge
, list
[LOWER
]);
251 node
= edge
->node
[UPPER
];
258 * walk down backref nodes to find start of next reference path
260 static struct backref_node
*walk_down_backref(struct backref_edge
*edges
[],
263 struct backref_edge
*edge
;
264 struct backref_node
*lower
;
268 edge
= edges
[idx
- 1];
269 lower
= edge
->node
[LOWER
];
270 if (list_is_last(&edge
->list
[LOWER
], &lower
->upper
)) {
274 edge
= list_entry(edge
->list
[LOWER
].next
,
275 struct backref_edge
, list
[LOWER
]);
276 edges
[idx
- 1] = edge
;
278 return edge
->node
[UPPER
];
284 static void drop_node_buffer(struct backref_node
*node
)
288 btrfs_tree_unlock(node
->eb
);
291 free_extent_buffer(node
->eb
);
296 static void drop_backref_node(struct backref_cache
*tree
,
297 struct backref_node
*node
)
299 BUG_ON(!node
->lowest
);
300 BUG_ON(!list_empty(&node
->upper
));
302 drop_node_buffer(node
);
303 list_del(&node
->lower
);
305 rb_erase(&node
->rb_node
, &tree
->rb_root
);
310 * remove a backref node from the backref cache
312 static void remove_backref_node(struct backref_cache
*cache
,
313 struct backref_node
*node
)
315 struct backref_node
*upper
;
316 struct backref_edge
*edge
;
321 BUG_ON(!node
->lowest
);
322 while (!list_empty(&node
->upper
)) {
323 edge
= list_entry(node
->upper
.next
, struct backref_edge
,
325 upper
= edge
->node
[UPPER
];
326 list_del(&edge
->list
[LOWER
]);
327 list_del(&edge
->list
[UPPER
]);
330 * add the node to pending list if no other
331 * child block cached.
333 if (list_empty(&upper
->lower
)) {
334 list_add_tail(&upper
->lower
,
335 &cache
->pending
[upper
->level
]);
339 drop_backref_node(cache
, node
);
343 * find reloc tree by address of tree root
345 static struct btrfs_root
*find_reloc_root(struct reloc_control
*rc
,
348 struct rb_node
*rb_node
;
349 struct mapping_node
*node
;
350 struct btrfs_root
*root
= NULL
;
352 spin_lock(&rc
->reloc_root_tree
.lock
);
353 rb_node
= tree_search(&rc
->reloc_root_tree
.rb_root
, bytenr
);
355 node
= rb_entry(rb_node
, struct mapping_node
, rb_node
);
356 root
= (struct btrfs_root
*)node
->data
;
358 spin_unlock(&rc
->reloc_root_tree
.lock
);
362 static int is_cowonly_root(u64 root_objectid
)
364 if (root_objectid
== BTRFS_ROOT_TREE_OBJECTID
||
365 root_objectid
== BTRFS_EXTENT_TREE_OBJECTID
||
366 root_objectid
== BTRFS_CHUNK_TREE_OBJECTID
||
367 root_objectid
== BTRFS_DEV_TREE_OBJECTID
||
368 root_objectid
== BTRFS_TREE_LOG_OBJECTID
||
369 root_objectid
== BTRFS_CSUM_TREE_OBJECTID
)
374 static struct btrfs_root
*read_fs_root(struct btrfs_fs_info
*fs_info
,
377 struct btrfs_key key
;
379 key
.objectid
= root_objectid
;
380 key
.type
= BTRFS_ROOT_ITEM_KEY
;
381 if (is_cowonly_root(root_objectid
))
384 key
.offset
= (u64
)-1;
386 return btrfs_read_fs_root_no_name(fs_info
, &key
);
389 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
390 static noinline_for_stack
391 struct btrfs_root
*find_tree_root(struct reloc_control
*rc
,
392 struct extent_buffer
*leaf
,
393 struct btrfs_extent_ref_v0
*ref0
)
395 struct btrfs_root
*root
;
396 u64 root_objectid
= btrfs_ref_root_v0(leaf
, ref0
);
397 u64 generation
= btrfs_ref_generation_v0(leaf
, ref0
);
399 BUG_ON(root_objectid
== BTRFS_TREE_RELOC_OBJECTID
);
401 root
= read_fs_root(rc
->extent_root
->fs_info
, root_objectid
);
402 BUG_ON(IS_ERR(root
));
404 if (root
->ref_cows
&&
405 generation
!= btrfs_root_generation(&root
->root_item
))
412 static noinline_for_stack
413 int find_inline_backref(struct extent_buffer
*leaf
, int slot
,
414 unsigned long *ptr
, unsigned long *end
)
416 struct btrfs_extent_item
*ei
;
417 struct btrfs_tree_block_info
*bi
;
420 item_size
= btrfs_item_size_nr(leaf
, slot
);
421 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
422 if (item_size
< sizeof(*ei
)) {
423 WARN_ON(item_size
!= sizeof(struct btrfs_extent_item_v0
));
427 ei
= btrfs_item_ptr(leaf
, slot
, struct btrfs_extent_item
);
428 WARN_ON(!(btrfs_extent_flags(leaf
, ei
) &
429 BTRFS_EXTENT_FLAG_TREE_BLOCK
));
431 if (item_size
<= sizeof(*ei
) + sizeof(*bi
)) {
432 WARN_ON(item_size
< sizeof(*ei
) + sizeof(*bi
));
436 bi
= (struct btrfs_tree_block_info
*)(ei
+ 1);
437 *ptr
= (unsigned long)(bi
+ 1);
438 *end
= (unsigned long)ei
+ item_size
;
443 * build backref tree for a given tree block. root of the backref tree
444 * corresponds the tree block, leaves of the backref tree correspond
445 * roots of b-trees that reference the tree block.
447 * the basic idea of this function is check backrefs of a given block
448 * to find upper level blocks that refernece the block, and then check
449 * bakcrefs of these upper level blocks recursively. the recursion stop
450 * when tree root is reached or backrefs for the block is cached.
452 * NOTE: if we find backrefs for a block are cached, we know backrefs
453 * for all upper level blocks that directly/indirectly reference the
454 * block are also cached.
456 static struct backref_node
*build_backref_tree(struct reloc_control
*rc
,
457 struct backref_cache
*cache
,
458 struct btrfs_key
*node_key
,
459 int level
, u64 bytenr
)
461 struct btrfs_path
*path1
;
462 struct btrfs_path
*path2
;
463 struct extent_buffer
*eb
;
464 struct btrfs_root
*root
;
465 struct backref_node
*cur
;
466 struct backref_node
*upper
;
467 struct backref_node
*lower
;
468 struct backref_node
*node
= NULL
;
469 struct backref_node
*exist
= NULL
;
470 struct backref_edge
*edge
;
471 struct rb_node
*rb_node
;
472 struct btrfs_key key
;
479 path1
= btrfs_alloc_path();
480 path2
= btrfs_alloc_path();
481 if (!path1
|| !path2
) {
486 node
= kmalloc(sizeof(*node
), GFP_NOFS
);
492 backref_node_init(node
);
493 node
->bytenr
= bytenr
;
501 key
.objectid
= cur
->bytenr
;
502 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
503 key
.offset
= (u64
)-1;
505 path1
->search_commit_root
= 1;
506 path1
->skip_locking
= 1;
507 ret
= btrfs_search_slot(NULL
, rc
->extent_root
, &key
, path1
,
513 BUG_ON(!ret
|| !path1
->slots
[0]);
517 WARN_ON(cur
->checked
);
518 if (!list_empty(&cur
->upper
)) {
520 * the backref was added previously when processsing
521 * backref of type BTRFS_TREE_BLOCK_REF_KEY
523 BUG_ON(!list_is_singular(&cur
->upper
));
524 edge
= list_entry(cur
->upper
.next
, struct backref_edge
,
526 BUG_ON(!list_empty(&edge
->list
[UPPER
]));
527 exist
= edge
->node
[UPPER
];
529 * add the upper level block to pending list if we need
533 list_add_tail(&edge
->list
[UPPER
], &list
);
540 eb
= path1
->nodes
[0];
543 if (path1
->slots
[0] >= btrfs_header_nritems(eb
)) {
544 ret
= btrfs_next_leaf(rc
->extent_root
, path1
);
551 eb
= path1
->nodes
[0];
554 btrfs_item_key_to_cpu(eb
, &key
, path1
->slots
[0]);
555 if (key
.objectid
!= cur
->bytenr
) {
560 if (key
.type
== BTRFS_EXTENT_ITEM_KEY
) {
561 ret
= find_inline_backref(eb
, path1
->slots
[0],
569 /* update key for inline back ref */
570 struct btrfs_extent_inline_ref
*iref
;
571 iref
= (struct btrfs_extent_inline_ref
*)ptr
;
572 key
.type
= btrfs_extent_inline_ref_type(eb
, iref
);
573 key
.offset
= btrfs_extent_inline_ref_offset(eb
, iref
);
574 WARN_ON(key
.type
!= BTRFS_TREE_BLOCK_REF_KEY
&&
575 key
.type
!= BTRFS_SHARED_BLOCK_REF_KEY
);
579 ((key
.type
== BTRFS_TREE_BLOCK_REF_KEY
&&
580 exist
->owner
== key
.offset
) ||
581 (key
.type
== BTRFS_SHARED_BLOCK_REF_KEY
&&
582 exist
->bytenr
== key
.offset
))) {
587 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
588 if (key
.type
== BTRFS_SHARED_BLOCK_REF_KEY
||
589 key
.type
== BTRFS_EXTENT_REF_V0_KEY
) {
590 if (key
.objectid
== key
.offset
&&
591 key
.type
== BTRFS_EXTENT_REF_V0_KEY
) {
592 struct btrfs_extent_ref_v0
*ref0
;
593 ref0
= btrfs_item_ptr(eb
, path1
->slots
[0],
594 struct btrfs_extent_ref_v0
);
595 root
= find_tree_root(rc
, eb
, ref0
);
603 BUG_ON(key
.type
== BTRFS_EXTENT_REF_V0_KEY
);
604 if (key
.type
== BTRFS_SHARED_BLOCK_REF_KEY
) {
606 if (key
.objectid
== key
.offset
) {
608 * only root blocks of reloc trees use
609 * backref of this type.
611 root
= find_reloc_root(rc
, cur
->bytenr
);
617 edge
= kzalloc(sizeof(*edge
), GFP_NOFS
);
622 rb_node
= tree_search(&cache
->rb_root
, key
.offset
);
624 upper
= kmalloc(sizeof(*upper
), GFP_NOFS
);
630 backref_node_init(upper
);
631 upper
->bytenr
= key
.offset
;
633 upper
->level
= cur
->level
+ 1;
635 * backrefs for the upper level block isn't
636 * cached, add the block to pending list
638 list_add_tail(&edge
->list
[UPPER
], &list
);
640 upper
= rb_entry(rb_node
, struct backref_node
,
642 INIT_LIST_HEAD(&edge
->list
[UPPER
]);
644 list_add(&edge
->list
[LOWER
], &cur
->upper
);
645 edge
->node
[UPPER
] = upper
;
646 edge
->node
[LOWER
] = cur
;
649 } else if (key
.type
!= BTRFS_TREE_BLOCK_REF_KEY
) {
653 /* key.type == BTRFS_TREE_BLOCK_REF_KEY */
654 root
= read_fs_root(rc
->extent_root
->fs_info
, key
.offset
);
660 if (btrfs_root_level(&root
->root_item
) == cur
->level
) {
662 BUG_ON(btrfs_root_bytenr(&root
->root_item
) !=
668 level
= cur
->level
+ 1;
671 * searching the tree to find upper level blocks
672 * reference the block.
674 path2
->search_commit_root
= 1;
675 path2
->skip_locking
= 1;
676 path2
->lowest_level
= level
;
677 ret
= btrfs_search_slot(NULL
, root
, node_key
, path2
, 0, 0);
678 path2
->lowest_level
= 0;
683 if (ret
> 0 && path2
->slots
[level
] > 0)
684 path2
->slots
[level
]--;
686 eb
= path2
->nodes
[level
];
687 WARN_ON(btrfs_node_blockptr(eb
, path2
->slots
[level
]) !=
691 for (; level
< BTRFS_MAX_LEVEL
; level
++) {
692 if (!path2
->nodes
[level
]) {
693 BUG_ON(btrfs_root_bytenr(&root
->root_item
) !=
699 edge
= kzalloc(sizeof(*edge
), GFP_NOFS
);
705 eb
= path2
->nodes
[level
];
706 rb_node
= tree_search(&cache
->rb_root
, eb
->start
);
708 upper
= kmalloc(sizeof(*upper
), GFP_NOFS
);
714 backref_node_init(upper
);
715 upper
->bytenr
= eb
->start
;
716 upper
->owner
= btrfs_header_owner(eb
);
717 upper
->level
= lower
->level
+ 1;
720 * if we know the block isn't shared
721 * we can void checking its backrefs.
723 if (btrfs_block_can_be_shared(root
, eb
))
729 * add the block to pending list if we
730 * need check its backrefs. only block
731 * at 'cur->level + 1' is added to the
732 * tail of pending list. this guarantees
733 * we check backrefs from lower level
734 * blocks to upper level blocks.
736 if (!upper
->checked
&&
737 level
== cur
->level
+ 1) {
738 list_add_tail(&edge
->list
[UPPER
],
741 INIT_LIST_HEAD(&edge
->list
[UPPER
]);
743 upper
= rb_entry(rb_node
, struct backref_node
,
745 BUG_ON(!upper
->checked
);
746 INIT_LIST_HEAD(&edge
->list
[UPPER
]);
748 list_add_tail(&edge
->list
[LOWER
], &lower
->upper
);
749 edge
->node
[UPPER
] = upper
;
750 edge
->node
[LOWER
] = lower
;
757 btrfs_release_path(root
, path2
);
760 ptr
+= btrfs_extent_inline_ref_size(key
.type
);
770 btrfs_release_path(rc
->extent_root
, path1
);
775 /* the pending list isn't empty, take the first block to process */
776 if (!list_empty(&list
)) {
777 edge
= list_entry(list
.next
, struct backref_edge
, list
[UPPER
]);
778 list_del_init(&edge
->list
[UPPER
]);
779 cur
= edge
->node
[UPPER
];
784 * everything goes well, connect backref nodes and insert backref nodes
787 BUG_ON(!node
->checked
);
788 rb_node
= tree_insert(&cache
->rb_root
, node
->bytenr
, &node
->rb_node
);
791 list_for_each_entry(edge
, &node
->upper
, list
[LOWER
])
792 list_add_tail(&edge
->list
[UPPER
], &list
);
794 while (!list_empty(&list
)) {
795 edge
= list_entry(list
.next
, struct backref_edge
, list
[UPPER
]);
796 list_del_init(&edge
->list
[UPPER
]);
797 upper
= edge
->node
[UPPER
];
799 if (!RB_EMPTY_NODE(&upper
->rb_node
)) {
801 list_del_init(&upper
->lower
);
805 list_add_tail(&edge
->list
[UPPER
], &upper
->lower
);
809 BUG_ON(!upper
->checked
);
810 rb_node
= tree_insert(&cache
->rb_root
, upper
->bytenr
,
814 list_add_tail(&edge
->list
[UPPER
], &upper
->lower
);
816 list_for_each_entry(edge
, &upper
->upper
, list
[LOWER
])
817 list_add_tail(&edge
->list
[UPPER
], &list
);
820 btrfs_free_path(path1
);
821 btrfs_free_path(path2
);
823 INIT_LIST_HEAD(&list
);
826 if (RB_EMPTY_NODE(&upper
->rb_node
)) {
827 list_splice_tail(&upper
->upper
, &list
);
831 if (list_empty(&list
))
834 edge
= list_entry(list
.next
, struct backref_edge
,
836 upper
= edge
->node
[UPPER
];
845 * helper to add 'address of tree root -> reloc tree' mapping
847 static int __add_reloc_root(struct btrfs_root
*root
)
849 struct rb_node
*rb_node
;
850 struct mapping_node
*node
;
851 struct reloc_control
*rc
= root
->fs_info
->reloc_ctl
;
853 node
= kmalloc(sizeof(*node
), GFP_NOFS
);
856 node
->bytenr
= root
->node
->start
;
859 spin_lock(&rc
->reloc_root_tree
.lock
);
860 rb_node
= tree_insert(&rc
->reloc_root_tree
.rb_root
,
861 node
->bytenr
, &node
->rb_node
);
862 spin_unlock(&rc
->reloc_root_tree
.lock
);
865 list_add_tail(&root
->root_list
, &rc
->reloc_roots
);
870 * helper to update/delete the 'address of tree root -> reloc tree'
873 static int __update_reloc_root(struct btrfs_root
*root
, int del
)
875 struct rb_node
*rb_node
;
876 struct mapping_node
*node
= NULL
;
877 struct reloc_control
*rc
= root
->fs_info
->reloc_ctl
;
879 spin_lock(&rc
->reloc_root_tree
.lock
);
880 rb_node
= tree_search(&rc
->reloc_root_tree
.rb_root
,
881 root
->commit_root
->start
);
883 node
= rb_entry(rb_node
, struct mapping_node
, rb_node
);
884 rb_erase(&node
->rb_node
, &rc
->reloc_root_tree
.rb_root
);
886 spin_unlock(&rc
->reloc_root_tree
.lock
);
888 BUG_ON((struct btrfs_root
*)node
->data
!= root
);
891 spin_lock(&rc
->reloc_root_tree
.lock
);
892 node
->bytenr
= root
->node
->start
;
893 rb_node
= tree_insert(&rc
->reloc_root_tree
.rb_root
,
894 node
->bytenr
, &node
->rb_node
);
895 spin_unlock(&rc
->reloc_root_tree
.lock
);
898 list_del_init(&root
->root_list
);
905 * create reloc tree for a given fs tree. reloc tree is just a
906 * snapshot of the fs tree with special root objectid.
908 int btrfs_init_reloc_root(struct btrfs_trans_handle
*trans
,
909 struct btrfs_root
*root
)
911 struct btrfs_root
*reloc_root
;
912 struct extent_buffer
*eb
;
913 struct btrfs_root_item
*root_item
;
914 struct btrfs_key root_key
;
917 if (root
->reloc_root
) {
918 reloc_root
= root
->reloc_root
;
919 reloc_root
->last_trans
= trans
->transid
;
923 if (!root
->fs_info
->reloc_ctl
||
924 !root
->fs_info
->reloc_ctl
->create_reloc_root
||
925 root
->root_key
.objectid
== BTRFS_TREE_RELOC_OBJECTID
)
928 root_item
= kmalloc(sizeof(*root_item
), GFP_NOFS
);
931 root_key
.objectid
= BTRFS_TREE_RELOC_OBJECTID
;
932 root_key
.type
= BTRFS_ROOT_ITEM_KEY
;
933 root_key
.offset
= root
->root_key
.objectid
;
935 ret
= btrfs_copy_root(trans
, root
, root
->commit_root
, &eb
,
936 BTRFS_TREE_RELOC_OBJECTID
);
939 btrfs_set_root_last_snapshot(&root
->root_item
, trans
->transid
- 1);
940 memcpy(root_item
, &root
->root_item
, sizeof(*root_item
));
941 btrfs_set_root_refs(root_item
, 1);
942 btrfs_set_root_bytenr(root_item
, eb
->start
);
943 btrfs_set_root_level(root_item
, btrfs_header_level(eb
));
944 btrfs_set_root_generation(root_item
, trans
->transid
);
945 memset(&root_item
->drop_progress
, 0, sizeof(struct btrfs_disk_key
));
946 root_item
->drop_level
= 0;
948 btrfs_tree_unlock(eb
);
949 free_extent_buffer(eb
);
951 ret
= btrfs_insert_root(trans
, root
->fs_info
->tree_root
,
952 &root_key
, root_item
);
956 reloc_root
= btrfs_read_fs_root_no_radix(root
->fs_info
->tree_root
,
958 BUG_ON(IS_ERR(reloc_root
));
959 reloc_root
->last_trans
= trans
->transid
;
961 __add_reloc_root(reloc_root
);
962 root
->reloc_root
= reloc_root
;
967 * update root item of reloc tree
969 int btrfs_update_reloc_root(struct btrfs_trans_handle
*trans
,
970 struct btrfs_root
*root
)
972 struct btrfs_root
*reloc_root
;
973 struct btrfs_root_item
*root_item
;
977 if (!root
->reloc_root
)
980 reloc_root
= root
->reloc_root
;
981 root_item
= &reloc_root
->root_item
;
983 if (btrfs_root_refs(root_item
) == 0) {
984 root
->reloc_root
= NULL
;
988 __update_reloc_root(reloc_root
, del
);
990 if (reloc_root
->commit_root
!= reloc_root
->node
) {
991 btrfs_set_root_node(root_item
, reloc_root
->node
);
992 free_extent_buffer(reloc_root
->commit_root
);
993 reloc_root
->commit_root
= btrfs_root_node(reloc_root
);
996 ret
= btrfs_update_root(trans
, root
->fs_info
->tree_root
,
997 &reloc_root
->root_key
, root_item
);
1003 * helper to find first cached inode with inode number >= objectid
1006 static struct inode
*find_next_inode(struct btrfs_root
*root
, u64 objectid
)
1008 struct rb_node
*node
;
1009 struct rb_node
*prev
;
1010 struct btrfs_inode
*entry
;
1011 struct inode
*inode
;
1013 spin_lock(&root
->inode_lock
);
1015 node
= root
->inode_tree
.rb_node
;
1019 entry
= rb_entry(node
, struct btrfs_inode
, rb_node
);
1021 if (objectid
< entry
->vfs_inode
.i_ino
)
1022 node
= node
->rb_left
;
1023 else if (objectid
> entry
->vfs_inode
.i_ino
)
1024 node
= node
->rb_right
;
1030 entry
= rb_entry(prev
, struct btrfs_inode
, rb_node
);
1031 if (objectid
<= entry
->vfs_inode
.i_ino
) {
1035 prev
= rb_next(prev
);
1039 entry
= rb_entry(node
, struct btrfs_inode
, rb_node
);
1040 inode
= igrab(&entry
->vfs_inode
);
1042 spin_unlock(&root
->inode_lock
);
1046 objectid
= entry
->vfs_inode
.i_ino
+ 1;
1047 if (cond_resched_lock(&root
->inode_lock
))
1050 node
= rb_next(node
);
1052 spin_unlock(&root
->inode_lock
);
1056 static int in_block_group(u64 bytenr
,
1057 struct btrfs_block_group_cache
*block_group
)
1059 if (bytenr
>= block_group
->key
.objectid
&&
1060 bytenr
< block_group
->key
.objectid
+ block_group
->key
.offset
)
1066 * get new location of data
1068 static int get_new_location(struct inode
*reloc_inode
, u64
*new_bytenr
,
1069 u64 bytenr
, u64 num_bytes
)
1071 struct btrfs_root
*root
= BTRFS_I(reloc_inode
)->root
;
1072 struct btrfs_path
*path
;
1073 struct btrfs_file_extent_item
*fi
;
1074 struct extent_buffer
*leaf
;
1077 path
= btrfs_alloc_path();
1081 bytenr
-= BTRFS_I(reloc_inode
)->index_cnt
;
1082 ret
= btrfs_lookup_file_extent(NULL
, root
, path
, reloc_inode
->i_ino
,
1091 leaf
= path
->nodes
[0];
1092 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
1093 struct btrfs_file_extent_item
);
1095 BUG_ON(btrfs_file_extent_offset(leaf
, fi
) ||
1096 btrfs_file_extent_compression(leaf
, fi
) ||
1097 btrfs_file_extent_encryption(leaf
, fi
) ||
1098 btrfs_file_extent_other_encoding(leaf
, fi
));
1100 if (num_bytes
!= btrfs_file_extent_disk_num_bytes(leaf
, fi
)) {
1106 *new_bytenr
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
1109 btrfs_free_path(path
);
1114 * update file extent items in the tree leaf to point to
1115 * the new locations.
1117 static int replace_file_extents(struct btrfs_trans_handle
*trans
,
1118 struct reloc_control
*rc
,
1119 struct btrfs_root
*root
,
1120 struct extent_buffer
*leaf
,
1121 struct list_head
*inode_list
)
1123 struct btrfs_key key
;
1124 struct btrfs_file_extent_item
*fi
;
1125 struct inode
*inode
= NULL
;
1126 struct inodevec
*ivec
= NULL
;
1138 if (rc
->stage
!= UPDATE_DATA_PTRS
)
1141 /* reloc trees always use full backref */
1142 if (root
->root_key
.objectid
== BTRFS_TREE_RELOC_OBJECTID
)
1143 parent
= leaf
->start
;
1147 nritems
= btrfs_header_nritems(leaf
);
1148 for (i
= 0; i
< nritems
; i
++) {
1150 btrfs_item_key_to_cpu(leaf
, &key
, i
);
1151 if (key
.type
!= BTRFS_EXTENT_DATA_KEY
)
1153 fi
= btrfs_item_ptr(leaf
, i
, struct btrfs_file_extent_item
);
1154 if (btrfs_file_extent_type(leaf
, fi
) ==
1155 BTRFS_FILE_EXTENT_INLINE
)
1157 bytenr
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
1158 num_bytes
= btrfs_file_extent_disk_num_bytes(leaf
, fi
);
1161 if (!in_block_group(bytenr
, rc
->block_group
))
1165 * if we are modifying block in fs tree, wait for readpage
1166 * to complete and drop the extent cache
1168 if (root
->root_key
.objectid
!= BTRFS_TREE_RELOC_OBJECTID
) {
1169 if (!ivec
|| ivec
->nr
== INODEVEC_SIZE
) {
1170 ivec
= kmalloc(sizeof(*ivec
), GFP_NOFS
);
1173 list_add_tail(&ivec
->list
, inode_list
);
1176 inode
= find_next_inode(root
, key
.objectid
);
1178 ivec
->inode
[ivec
->nr
++] = inode
;
1180 } else if (inode
&& inode
->i_ino
< key
.objectid
) {
1181 inode
= find_next_inode(root
, key
.objectid
);
1183 ivec
->inode
[ivec
->nr
++] = inode
;
1185 if (inode
&& inode
->i_ino
== key
.objectid
) {
1187 btrfs_file_extent_num_bytes(leaf
, fi
);
1188 WARN_ON(!IS_ALIGNED(key
.offset
,
1190 WARN_ON(!IS_ALIGNED(end
, root
->sectorsize
));
1192 ret
= try_lock_extent(&BTRFS_I(inode
)->io_tree
,
1198 btrfs_drop_extent_cache(inode
, key
.offset
, end
,
1200 unlock_extent(&BTRFS_I(inode
)->io_tree
,
1201 key
.offset
, end
, GFP_NOFS
);
1205 ret
= get_new_location(rc
->data_inode
, &new_bytenr
,
1211 btrfs_set_file_extent_disk_bytenr(leaf
, fi
, new_bytenr
);
1214 key
.offset
-= btrfs_file_extent_offset(leaf
, fi
);
1215 ret
= btrfs_inc_extent_ref(trans
, root
, new_bytenr
,
1217 btrfs_header_owner(leaf
),
1218 key
.objectid
, key
.offset
);
1221 ret
= btrfs_free_extent(trans
, root
, bytenr
, num_bytes
,
1222 parent
, btrfs_header_owner(leaf
),
1223 key
.objectid
, key
.offset
);
1227 btrfs_mark_buffer_dirty(leaf
);
1231 static noinline_for_stack
1232 int memcmp_node_keys(struct extent_buffer
*eb
, int slot
,
1233 struct btrfs_path
*path
, int level
)
1235 struct btrfs_disk_key key1
;
1236 struct btrfs_disk_key key2
;
1237 btrfs_node_key(eb
, &key1
, slot
);
1238 btrfs_node_key(path
->nodes
[level
], &key2
, path
->slots
[level
]);
1239 return memcmp(&key1
, &key2
, sizeof(key1
));
1243 * try to replace tree blocks in fs tree with the new blocks
1244 * in reloc tree. tree blocks haven't been modified since the
1245 * reloc tree was create can be replaced.
1247 * if a block was replaced, level of the block + 1 is returned.
1248 * if no block got replaced, 0 is returned. if there are other
1249 * errors, a negative error number is returned.
1251 static int replace_path(struct btrfs_trans_handle
*trans
,
1252 struct btrfs_root
*dest
, struct btrfs_root
*src
,
1253 struct btrfs_path
*path
, struct btrfs_key
*next_key
,
1254 struct extent_buffer
**leaf
,
1255 int lowest_level
, int max_level
)
1257 struct extent_buffer
*eb
;
1258 struct extent_buffer
*parent
;
1259 struct btrfs_key key
;
1270 BUG_ON(src
->root_key
.objectid
!= BTRFS_TREE_RELOC_OBJECTID
);
1271 BUG_ON(dest
->root_key
.objectid
== BTRFS_TREE_RELOC_OBJECTID
);
1272 BUG_ON(lowest_level
> 1 && leaf
);
1274 last_snapshot
= btrfs_root_last_snapshot(&src
->root_item
);
1276 slot
= path
->slots
[lowest_level
];
1277 btrfs_node_key_to_cpu(path
->nodes
[lowest_level
], &key
, slot
);
1279 eb
= btrfs_lock_root_node(dest
);
1280 btrfs_set_lock_blocking(eb
);
1281 level
= btrfs_header_level(eb
);
1283 if (level
< lowest_level
) {
1284 btrfs_tree_unlock(eb
);
1285 free_extent_buffer(eb
);
1289 ret
= btrfs_cow_block(trans
, dest
, eb
, NULL
, 0, &eb
);
1291 btrfs_set_lock_blocking(eb
);
1294 next_key
->objectid
= (u64
)-1;
1295 next_key
->type
= (u8
)-1;
1296 next_key
->offset
= (u64
)-1;
1301 level
= btrfs_header_level(parent
);
1302 BUG_ON(level
< lowest_level
);
1304 ret
= btrfs_bin_search(parent
, &key
, level
, &slot
);
1305 if (ret
&& slot
> 0)
1308 if (next_key
&& slot
+ 1 < btrfs_header_nritems(parent
))
1309 btrfs_node_key_to_cpu(parent
, next_key
, slot
+ 1);
1311 old_bytenr
= btrfs_node_blockptr(parent
, slot
);
1312 blocksize
= btrfs_level_size(dest
, level
- 1);
1313 old_ptr_gen
= btrfs_node_ptr_generation(parent
, slot
);
1315 if (level
<= max_level
) {
1316 eb
= path
->nodes
[level
];
1317 new_bytenr
= btrfs_node_blockptr(eb
,
1318 path
->slots
[level
]);
1319 new_ptr_gen
= btrfs_node_ptr_generation(eb
,
1320 path
->slots
[level
]);
1326 if (new_bytenr
> 0 && new_bytenr
== old_bytenr
) {
1332 if (new_bytenr
== 0 || old_ptr_gen
> last_snapshot
||
1333 memcmp_node_keys(parent
, slot
, path
, level
)) {
1334 if (level
<= lowest_level
&& !leaf
) {
1339 eb
= read_tree_block(dest
, old_bytenr
, blocksize
,
1341 btrfs_tree_lock(eb
);
1342 ret
= btrfs_cow_block(trans
, dest
, eb
, parent
,
1345 btrfs_set_lock_blocking(eb
);
1347 if (level
<= lowest_level
) {
1353 btrfs_tree_unlock(parent
);
1354 free_extent_buffer(parent
);
1360 btrfs_node_key_to_cpu(path
->nodes
[level
], &key
,
1361 path
->slots
[level
]);
1362 btrfs_release_path(src
, path
);
1364 path
->lowest_level
= level
;
1365 ret
= btrfs_search_slot(trans
, src
, &key
, path
, 0, 1);
1366 path
->lowest_level
= 0;
1370 * swap blocks in fs tree and reloc tree.
1372 btrfs_set_node_blockptr(parent
, slot
, new_bytenr
);
1373 btrfs_set_node_ptr_generation(parent
, slot
, new_ptr_gen
);
1374 btrfs_mark_buffer_dirty(parent
);
1376 btrfs_set_node_blockptr(path
->nodes
[level
],
1377 path
->slots
[level
], old_bytenr
);
1378 btrfs_set_node_ptr_generation(path
->nodes
[level
],
1379 path
->slots
[level
], old_ptr_gen
);
1380 btrfs_mark_buffer_dirty(path
->nodes
[level
]);
1382 ret
= btrfs_inc_extent_ref(trans
, src
, old_bytenr
, blocksize
,
1383 path
->nodes
[level
]->start
,
1384 src
->root_key
.objectid
, level
- 1, 0);
1386 ret
= btrfs_inc_extent_ref(trans
, dest
, new_bytenr
, blocksize
,
1387 0, dest
->root_key
.objectid
, level
- 1,
1391 ret
= btrfs_free_extent(trans
, src
, new_bytenr
, blocksize
,
1392 path
->nodes
[level
]->start
,
1393 src
->root_key
.objectid
, level
- 1, 0);
1396 ret
= btrfs_free_extent(trans
, dest
, old_bytenr
, blocksize
,
1397 0, dest
->root_key
.objectid
, level
- 1,
1401 btrfs_unlock_up_safe(path
, 0);
1406 btrfs_tree_unlock(parent
);
1407 free_extent_buffer(parent
);
1412 * helper to find next relocated block in reloc tree
1414 static noinline_for_stack
1415 int walk_up_reloc_tree(struct btrfs_root
*root
, struct btrfs_path
*path
,
1418 struct extent_buffer
*eb
;
1423 last_snapshot
= btrfs_root_last_snapshot(&root
->root_item
);
1425 for (i
= 0; i
< *level
; i
++) {
1426 free_extent_buffer(path
->nodes
[i
]);
1427 path
->nodes
[i
] = NULL
;
1430 for (i
= *level
; i
< BTRFS_MAX_LEVEL
&& path
->nodes
[i
]; i
++) {
1431 eb
= path
->nodes
[i
];
1432 nritems
= btrfs_header_nritems(eb
);
1433 while (path
->slots
[i
] + 1 < nritems
) {
1435 if (btrfs_node_ptr_generation(eb
, path
->slots
[i
]) <=
1442 free_extent_buffer(path
->nodes
[i
]);
1443 path
->nodes
[i
] = NULL
;
1449 * walk down reloc tree to find relocated block of lowest level
1451 static noinline_for_stack
1452 int walk_down_reloc_tree(struct btrfs_root
*root
, struct btrfs_path
*path
,
1455 struct extent_buffer
*eb
= NULL
;
1463 last_snapshot
= btrfs_root_last_snapshot(&root
->root_item
);
1465 for (i
= *level
; i
> 0; i
--) {
1466 eb
= path
->nodes
[i
];
1467 nritems
= btrfs_header_nritems(eb
);
1468 while (path
->slots
[i
] < nritems
) {
1469 ptr_gen
= btrfs_node_ptr_generation(eb
, path
->slots
[i
]);
1470 if (ptr_gen
> last_snapshot
)
1474 if (path
->slots
[i
] >= nritems
) {
1485 bytenr
= btrfs_node_blockptr(eb
, path
->slots
[i
]);
1486 blocksize
= btrfs_level_size(root
, i
- 1);
1487 eb
= read_tree_block(root
, bytenr
, blocksize
, ptr_gen
);
1488 BUG_ON(btrfs_header_level(eb
) != i
- 1);
1489 path
->nodes
[i
- 1] = eb
;
1490 path
->slots
[i
- 1] = 0;
1496 * invalidate extent cache for file extents whose key in range of
1497 * [min_key, max_key)
1499 static int invalidate_extent_cache(struct btrfs_root
*root
,
1500 struct btrfs_key
*min_key
,
1501 struct btrfs_key
*max_key
)
1503 struct inode
*inode
= NULL
;
1507 objectid
= min_key
->objectid
;
1512 if (objectid
> max_key
->objectid
)
1515 inode
= find_next_inode(root
, objectid
);
1519 if (inode
->i_ino
> max_key
->objectid
) {
1524 objectid
= inode
->i_ino
+ 1;
1525 if (!S_ISREG(inode
->i_mode
))
1528 if (unlikely(min_key
->objectid
== inode
->i_ino
)) {
1529 if (min_key
->type
> BTRFS_EXTENT_DATA_KEY
)
1531 if (min_key
->type
< BTRFS_EXTENT_DATA_KEY
)
1534 start
= min_key
->offset
;
1535 WARN_ON(!IS_ALIGNED(start
, root
->sectorsize
));
1541 if (unlikely(max_key
->objectid
== inode
->i_ino
)) {
1542 if (max_key
->type
< BTRFS_EXTENT_DATA_KEY
)
1544 if (max_key
->type
> BTRFS_EXTENT_DATA_KEY
) {
1547 if (max_key
->offset
== 0)
1549 end
= max_key
->offset
;
1550 WARN_ON(!IS_ALIGNED(end
, root
->sectorsize
));
1557 /* the lock_extent waits for readpage to complete */
1558 lock_extent(&BTRFS_I(inode
)->io_tree
, start
, end
, GFP_NOFS
);
1559 btrfs_drop_extent_cache(inode
, start
, end
, 1);
1560 unlock_extent(&BTRFS_I(inode
)->io_tree
, start
, end
, GFP_NOFS
);
1565 static void put_inodes(struct list_head
*list
)
1567 struct inodevec
*ivec
;
1568 while (!list_empty(list
)) {
1569 ivec
= list_entry(list
->next
, struct inodevec
, list
);
1570 list_del(&ivec
->list
);
1571 while (ivec
->nr
> 0) {
1573 iput(ivec
->inode
[ivec
->nr
]);
1579 static int find_next_key(struct btrfs_path
*path
, int level
,
1580 struct btrfs_key
*key
)
1583 while (level
< BTRFS_MAX_LEVEL
) {
1584 if (!path
->nodes
[level
])
1586 if (path
->slots
[level
] + 1 <
1587 btrfs_header_nritems(path
->nodes
[level
])) {
1588 btrfs_node_key_to_cpu(path
->nodes
[level
], key
,
1589 path
->slots
[level
] + 1);
1598 * merge the relocated tree blocks in reloc tree with corresponding
1601 static noinline_for_stack
int merge_reloc_root(struct reloc_control
*rc
,
1602 struct btrfs_root
*root
)
1604 LIST_HEAD(inode_list
);
1605 struct btrfs_key key
;
1606 struct btrfs_key next_key
;
1607 struct btrfs_trans_handle
*trans
;
1608 struct btrfs_root
*reloc_root
;
1609 struct btrfs_root_item
*root_item
;
1610 struct btrfs_path
*path
;
1611 struct extent_buffer
*leaf
= NULL
;
1619 path
= btrfs_alloc_path();
1623 reloc_root
= root
->reloc_root
;
1624 root_item
= &reloc_root
->root_item
;
1626 if (btrfs_disk_key_objectid(&root_item
->drop_progress
) == 0) {
1627 level
= btrfs_root_level(root_item
);
1628 extent_buffer_get(reloc_root
->node
);
1629 path
->nodes
[level
] = reloc_root
->node
;
1630 path
->slots
[level
] = 0;
1632 btrfs_disk_key_to_cpu(&key
, &root_item
->drop_progress
);
1634 level
= root_item
->drop_level
;
1636 path
->lowest_level
= level
;
1637 ret
= btrfs_search_slot(NULL
, reloc_root
, &key
, path
, 0, 0);
1638 path
->lowest_level
= 0;
1640 btrfs_free_path(path
);
1644 btrfs_node_key_to_cpu(path
->nodes
[level
], &next_key
,
1645 path
->slots
[level
]);
1646 WARN_ON(memcmp(&key
, &next_key
, sizeof(key
)));
1648 btrfs_unlock_up_safe(path
, 0);
1651 if (level
== 0 && rc
->stage
== UPDATE_DATA_PTRS
) {
1652 trans
= btrfs_start_transaction(root
, 1);
1654 leaf
= path
->nodes
[0];
1655 btrfs_item_key_to_cpu(leaf
, &key
, 0);
1656 btrfs_release_path(reloc_root
, path
);
1658 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 1);
1664 leaf
= path
->nodes
[0];
1665 btrfs_unlock_up_safe(path
, 1);
1666 ret
= replace_file_extents(trans
, rc
, root
, leaf
,
1673 memset(&next_key
, 0, sizeof(next_key
));
1678 trans
= btrfs_start_transaction(root
, 1);
1681 ret
= walk_down_reloc_tree(reloc_root
, path
, &level
);
1689 if (!find_next_key(path
, level
, &key
) &&
1690 btrfs_comp_cpu_keys(&next_key
, &key
) >= 0) {
1692 } else if (level
== 1 && rc
->stage
== UPDATE_DATA_PTRS
) {
1693 ret
= replace_path(trans
, root
, reloc_root
,
1694 path
, &next_key
, &leaf
,
1697 ret
= replace_path(trans
, root
, reloc_root
,
1698 path
, &next_key
, NULL
,
1708 btrfs_node_key_to_cpu(path
->nodes
[level
], &key
,
1709 path
->slots
[level
]);
1713 * no block got replaced, try replacing file extents
1715 btrfs_item_key_to_cpu(leaf
, &key
, 0);
1716 ret
= replace_file_extents(trans
, rc
, root
, leaf
,
1718 btrfs_tree_unlock(leaf
);
1719 free_extent_buffer(leaf
);
1723 ret
= walk_up_reloc_tree(reloc_root
, path
, &level
);
1729 * save the merging progress in the drop_progress.
1730 * this is OK since root refs == 1 in this case.
1732 btrfs_node_key(path
->nodes
[level
], &root_item
->drop_progress
,
1733 path
->slots
[level
]);
1734 root_item
->drop_level
= level
;
1736 nr
= trans
->blocks_used
;
1737 btrfs_end_transaction(trans
, root
);
1739 btrfs_btree_balance_dirty(root
, nr
);
1742 * put inodes outside transaction, otherwise we may deadlock.
1744 put_inodes(&inode_list
);
1746 if (replaced
&& rc
->stage
== UPDATE_DATA_PTRS
)
1747 invalidate_extent_cache(root
, &key
, &next_key
);
1751 * handle the case only one block in the fs tree need to be
1752 * relocated and the block is tree root.
1754 leaf
= btrfs_lock_root_node(root
);
1755 ret
= btrfs_cow_block(trans
, root
, leaf
, NULL
, 0, &leaf
);
1756 btrfs_tree_unlock(leaf
);
1757 free_extent_buffer(leaf
);
1761 btrfs_free_path(path
);
1764 memset(&root_item
->drop_progress
, 0,
1765 sizeof(root_item
->drop_progress
));
1766 root_item
->drop_level
= 0;
1767 btrfs_set_root_refs(root_item
, 0);
1770 nr
= trans
->blocks_used
;
1771 btrfs_end_transaction(trans
, root
);
1773 btrfs_btree_balance_dirty(root
, nr
);
1775 put_inodes(&inode_list
);
1777 if (replaced
&& rc
->stage
== UPDATE_DATA_PTRS
)
1778 invalidate_extent_cache(root
, &key
, &next_key
);
1784 * callback for the work threads.
1785 * this function merges reloc tree with corresponding fs tree,
1786 * and then drops the reloc tree.
1788 static void merge_func(struct btrfs_work
*work
)
1790 struct btrfs_trans_handle
*trans
;
1791 struct btrfs_root
*root
;
1792 struct btrfs_root
*reloc_root
;
1793 struct async_merge
*async
;
1795 async
= container_of(work
, struct async_merge
, work
);
1796 reloc_root
= async
->root
;
1798 if (btrfs_root_refs(&reloc_root
->root_item
) > 0) {
1799 root
= read_fs_root(reloc_root
->fs_info
,
1800 reloc_root
->root_key
.offset
);
1801 BUG_ON(IS_ERR(root
));
1802 BUG_ON(root
->reloc_root
!= reloc_root
);
1804 merge_reloc_root(async
->rc
, root
);
1806 trans
= btrfs_start_transaction(root
, 1);
1807 btrfs_update_reloc_root(trans
, root
);
1808 btrfs_end_transaction(trans
, root
);
1811 btrfs_drop_snapshot(reloc_root
, 0);
1813 if (atomic_dec_and_test(async
->num_pending
))
1814 complete(async
->done
);
1819 static int merge_reloc_roots(struct reloc_control
*rc
)
1821 struct async_merge
*async
;
1822 struct btrfs_root
*root
;
1823 struct completion done
;
1824 atomic_t num_pending
;
1826 init_completion(&done
);
1827 atomic_set(&num_pending
, 1);
1829 while (!list_empty(&rc
->reloc_roots
)) {
1830 root
= list_entry(rc
->reloc_roots
.next
,
1831 struct btrfs_root
, root_list
);
1832 list_del_init(&root
->root_list
);
1834 async
= kmalloc(sizeof(*async
), GFP_NOFS
);
1836 async
->work
.func
= merge_func
;
1837 async
->work
.flags
= 0;
1840 async
->done
= &done
;
1841 async
->num_pending
= &num_pending
;
1842 atomic_inc(&num_pending
);
1843 btrfs_queue_worker(&rc
->workers
, &async
->work
);
1846 if (!atomic_dec_and_test(&num_pending
))
1847 wait_for_completion(&done
);
1849 BUG_ON(!RB_EMPTY_ROOT(&rc
->reloc_root_tree
.rb_root
));
1853 static void free_block_list(struct rb_root
*blocks
)
1855 struct tree_block
*block
;
1856 struct rb_node
*rb_node
;
1857 while ((rb_node
= rb_first(blocks
))) {
1858 block
= rb_entry(rb_node
, struct tree_block
, rb_node
);
1859 rb_erase(rb_node
, blocks
);
1864 static int record_reloc_root_in_trans(struct btrfs_trans_handle
*trans
,
1865 struct btrfs_root
*reloc_root
)
1867 struct btrfs_root
*root
;
1869 if (reloc_root
->last_trans
== trans
->transid
)
1872 root
= read_fs_root(reloc_root
->fs_info
, reloc_root
->root_key
.offset
);
1873 BUG_ON(IS_ERR(root
));
1874 BUG_ON(root
->reloc_root
!= reloc_root
);
1876 return btrfs_record_root_in_trans(trans
, root
);
1880 * select one tree from trees that references the block.
1881 * for blocks in refernce counted trees, we preper reloc tree.
1882 * if no reloc tree found and reloc_only is true, NULL is returned.
1884 static struct btrfs_root
*__select_one_root(struct btrfs_trans_handle
*trans
,
1885 struct backref_node
*node
,
1886 struct backref_edge
*edges
[],
1887 int *nr
, int reloc_only
)
1889 struct backref_node
*next
;
1890 struct btrfs_root
*root
;
1898 next
= walk_up_backref(next
, edges
, &index
);
1901 BUG_ON(!node
->old_root
);
1905 /* no other choice for non-refernce counted tree */
1906 if (!root
->ref_cows
) {
1911 if (root
->root_key
.objectid
== BTRFS_TREE_RELOC_OBJECTID
) {
1912 record_reloc_root_in_trans(trans
, root
);
1917 btrfs_record_root_in_trans(trans
, root
);
1921 if (reloc_only
|| next
!= node
) {
1922 if (!root
->reloc_root
)
1923 btrfs_record_root_in_trans(trans
, root
);
1924 root
= root
->reloc_root
;
1926 * if the reloc tree was created in current
1927 * transation, there is no node in backref tree
1928 * corresponds to the root of the reloc tree.
1930 if (btrfs_root_last_snapshot(&root
->root_item
) ==
1936 next
= walk_down_backref(edges
, &index
);
1937 if (!next
|| next
->level
<= node
->level
)
1941 if (!root
&& !loop
&& !reloc_only
) {
1954 static noinline_for_stack
1955 struct btrfs_root
*select_one_root(struct btrfs_trans_handle
*trans
,
1956 struct backref_node
*node
)
1958 struct backref_edge
*edges
[BTRFS_MAX_LEVEL
- 1];
1960 return __select_one_root(trans
, node
, edges
, &nr
, 0);
1963 static noinline_for_stack
1964 struct btrfs_root
*select_reloc_root(struct btrfs_trans_handle
*trans
,
1965 struct backref_node
*node
,
1966 struct backref_edge
*edges
[], int *nr
)
1968 return __select_one_root(trans
, node
, edges
, nr
, 1);
1971 static void grab_path_buffers(struct btrfs_path
*path
,
1972 struct backref_node
*node
,
1973 struct backref_edge
*edges
[], int nr
)
1977 drop_node_buffer(node
);
1978 node
->eb
= path
->nodes
[node
->level
];
1980 if (path
->locks
[node
->level
])
1982 path
->nodes
[node
->level
] = NULL
;
1983 path
->locks
[node
->level
] = 0;
1988 edges
[i
]->blockptr
= node
->eb
->start
;
1989 node
= edges
[i
]->node
[UPPER
];
1995 * relocate a block tree, and then update pointers in upper level
1996 * blocks that reference the block to point to the new location.
1998 * if called by link_to_upper, the block has already been relocated.
1999 * in that case this function just updates pointers.
2001 static int do_relocation(struct btrfs_trans_handle
*trans
,
2002 struct backref_node
*node
,
2003 struct btrfs_key
*key
,
2004 struct btrfs_path
*path
, int lowest
)
2006 struct backref_node
*upper
;
2007 struct backref_edge
*edge
;
2008 struct backref_edge
*edges
[BTRFS_MAX_LEVEL
- 1];
2009 struct btrfs_root
*root
;
2010 struct extent_buffer
*eb
;
2019 BUG_ON(lowest
&& node
->eb
);
2021 path
->lowest_level
= node
->level
+ 1;
2022 list_for_each_entry(edge
, &node
->upper
, list
[LOWER
]) {
2024 if (node
->eb
&& node
->eb
->start
== edge
->blockptr
)
2027 upper
= edge
->node
[UPPER
];
2028 root
= select_reloc_root(trans
, upper
, edges
, &nr
);
2032 if (upper
->eb
&& !upper
->locked
)
2033 drop_node_buffer(upper
);
2036 ret
= btrfs_search_slot(trans
, root
, key
, path
, 0, 1);
2043 slot
= path
->slots
[upper
->level
];
2045 btrfs_unlock_up_safe(path
, upper
->level
+ 1);
2046 grab_path_buffers(path
, upper
, edges
, nr
);
2048 btrfs_release_path(NULL
, path
);
2050 ret
= btrfs_bin_search(upper
->eb
, key
, upper
->level
,
2055 bytenr
= btrfs_node_blockptr(upper
->eb
, slot
);
2057 if (node
->eb
->start
== bytenr
) {
2058 btrfs_tree_unlock(upper
->eb
);
2063 BUG_ON(node
->bytenr
!= bytenr
);
2066 blocksize
= btrfs_level_size(root
, node
->level
);
2067 generation
= btrfs_node_ptr_generation(upper
->eb
, slot
);
2068 eb
= read_tree_block(root
, bytenr
, blocksize
, generation
);
2069 btrfs_tree_lock(eb
);
2070 btrfs_set_lock_blocking(eb
);
2073 ret
= btrfs_cow_block(trans
, root
, eb
, upper
->eb
,
2079 btrfs_set_lock_blocking(eb
);
2083 btrfs_set_node_blockptr(upper
->eb
, slot
,
2085 btrfs_set_node_ptr_generation(upper
->eb
, slot
,
2087 btrfs_mark_buffer_dirty(upper
->eb
);
2089 ret
= btrfs_inc_extent_ref(trans
, root
,
2090 node
->eb
->start
, blocksize
,
2092 btrfs_header_owner(upper
->eb
),
2096 ret
= btrfs_drop_subtree(trans
, root
, eb
, upper
->eb
);
2100 btrfs_tree_unlock(upper
->eb
);
2104 path
->lowest_level
= 0;
2108 static int link_to_upper(struct btrfs_trans_handle
*trans
,
2109 struct backref_node
*node
,
2110 struct btrfs_path
*path
)
2112 struct btrfs_key key
;
2113 if (!node
->eb
|| list_empty(&node
->upper
))
2116 btrfs_node_key_to_cpu(node
->eb
, &key
, 0);
2117 return do_relocation(trans
, node
, &key
, path
, 0);
2120 static int finish_pending_nodes(struct btrfs_trans_handle
*trans
,
2121 struct backref_cache
*cache
,
2122 struct btrfs_path
*path
)
2124 struct backref_node
*node
;
2129 for (level
= 0; level
< BTRFS_MAX_LEVEL
; level
++) {
2130 while (!list_empty(&cache
->pending
[level
])) {
2131 node
= list_entry(cache
->pending
[level
].next
,
2132 struct backref_node
, lower
);
2133 BUG_ON(node
->level
!= level
);
2135 ret
= link_to_upper(trans
, node
, path
);
2139 * this remove the node from the pending list and
2140 * may add some other nodes to the level + 1
2143 remove_backref_node(cache
, node
);
2146 BUG_ON(!RB_EMPTY_ROOT(&cache
->rb_root
));
2150 static void mark_block_processed(struct reloc_control
*rc
,
2151 struct backref_node
*node
)
2154 if (node
->level
== 0 ||
2155 in_block_group(node
->bytenr
, rc
->block_group
)) {
2156 blocksize
= btrfs_level_size(rc
->extent_root
, node
->level
);
2157 set_extent_bits(&rc
->processed_blocks
, node
->bytenr
,
2158 node
->bytenr
+ blocksize
- 1, EXTENT_DIRTY
,
2161 node
->processed
= 1;
2165 * mark a block and all blocks directly/indirectly reference the block
2168 static void update_processed_blocks(struct reloc_control
*rc
,
2169 struct backref_node
*node
)
2171 struct backref_node
*next
= node
;
2172 struct backref_edge
*edge
;
2173 struct backref_edge
*edges
[BTRFS_MAX_LEVEL
- 1];
2179 if (next
->processed
)
2182 mark_block_processed(rc
, next
);
2184 if (list_empty(&next
->upper
))
2187 edge
= list_entry(next
->upper
.next
,
2188 struct backref_edge
, list
[LOWER
]);
2189 edges
[index
++] = edge
;
2190 next
= edge
->node
[UPPER
];
2192 next
= walk_down_backref(edges
, &index
);
2196 static int tree_block_processed(u64 bytenr
, u32 blocksize
,
2197 struct reloc_control
*rc
)
2199 if (test_range_bit(&rc
->processed_blocks
, bytenr
,
2200 bytenr
+ blocksize
- 1, EXTENT_DIRTY
, 1, NULL
))
2206 * check if there are any file extent pointers in the leaf point to
2207 * data require processing
2209 static int check_file_extents(struct reloc_control
*rc
,
2210 u64 bytenr
, u32 blocksize
, u64 ptr_gen
)
2212 struct btrfs_key found_key
;
2213 struct btrfs_file_extent_item
*fi
;
2214 struct extent_buffer
*leaf
;
2219 leaf
= read_tree_block(rc
->extent_root
, bytenr
, blocksize
, ptr_gen
);
2221 nritems
= btrfs_header_nritems(leaf
);
2222 for (i
= 0; i
< nritems
; i
++) {
2224 btrfs_item_key_to_cpu(leaf
, &found_key
, i
);
2225 if (found_key
.type
!= BTRFS_EXTENT_DATA_KEY
)
2227 fi
= btrfs_item_ptr(leaf
, i
, struct btrfs_file_extent_item
);
2228 if (btrfs_file_extent_type(leaf
, fi
) ==
2229 BTRFS_FILE_EXTENT_INLINE
)
2231 bytenr
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
2234 if (in_block_group(bytenr
, rc
->block_group
)) {
2239 free_extent_buffer(leaf
);
2244 * scan child blocks of a given block to find blocks require processing
2246 static int add_child_blocks(struct btrfs_trans_handle
*trans
,
2247 struct reloc_control
*rc
,
2248 struct backref_node
*node
,
2249 struct rb_root
*blocks
)
2251 struct tree_block
*block
;
2252 struct rb_node
*rb_node
;
2260 nritems
= btrfs_header_nritems(node
->eb
);
2261 blocksize
= btrfs_level_size(rc
->extent_root
, node
->level
- 1);
2262 for (i
= 0; i
< nritems
; i
++) {
2264 bytenr
= btrfs_node_blockptr(node
->eb
, i
);
2265 ptr_gen
= btrfs_node_ptr_generation(node
->eb
, i
);
2266 if (ptr_gen
== trans
->transid
)
2268 if (!in_block_group(bytenr
, rc
->block_group
) &&
2269 (node
->level
> 1 || rc
->stage
== MOVE_DATA_EXTENTS
))
2271 if (tree_block_processed(bytenr
, blocksize
, rc
))
2274 readahead_tree_block(rc
->extent_root
,
2275 bytenr
, blocksize
, ptr_gen
);
2278 for (i
= 0; i
< nritems
; i
++) {
2280 bytenr
= btrfs_node_blockptr(node
->eb
, i
);
2281 ptr_gen
= btrfs_node_ptr_generation(node
->eb
, i
);
2282 if (ptr_gen
== trans
->transid
)
2284 if (!in_block_group(bytenr
, rc
->block_group
) &&
2285 (node
->level
> 1 || rc
->stage
== MOVE_DATA_EXTENTS
))
2287 if (tree_block_processed(bytenr
, blocksize
, rc
))
2289 if (!in_block_group(bytenr
, rc
->block_group
) &&
2290 !check_file_extents(rc
, bytenr
, blocksize
, ptr_gen
))
2293 block
= kmalloc(sizeof(*block
), GFP_NOFS
);
2298 block
->bytenr
= bytenr
;
2299 btrfs_node_key_to_cpu(node
->eb
, &block
->key
, i
);
2300 block
->level
= node
->level
- 1;
2301 block
->key_ready
= 1;
2302 rb_node
= tree_insert(blocks
, block
->bytenr
, &block
->rb_node
);
2306 free_block_list(blocks
);
2311 * find adjacent blocks require processing
2313 static noinline_for_stack
2314 int add_adjacent_blocks(struct btrfs_trans_handle
*trans
,
2315 struct reloc_control
*rc
,
2316 struct backref_cache
*cache
,
2317 struct rb_root
*blocks
, int level
,
2318 struct backref_node
**upper
)
2320 struct backref_node
*node
;
2323 WARN_ON(!list_empty(&cache
->pending
[level
]));
2325 if (list_empty(&cache
->pending
[level
+ 1]))
2328 node
= list_entry(cache
->pending
[level
+ 1].next
,
2329 struct backref_node
, lower
);
2331 ret
= add_child_blocks(trans
, rc
, node
, blocks
);
2337 static int get_tree_block_key(struct reloc_control
*rc
,
2338 struct tree_block
*block
)
2340 struct extent_buffer
*eb
;
2342 BUG_ON(block
->key_ready
);
2343 eb
= read_tree_block(rc
->extent_root
, block
->bytenr
,
2344 block
->key
.objectid
, block
->key
.offset
);
2345 WARN_ON(btrfs_header_level(eb
) != block
->level
);
2346 if (block
->level
== 0)
2347 btrfs_item_key_to_cpu(eb
, &block
->key
, 0);
2349 btrfs_node_key_to_cpu(eb
, &block
->key
, 0);
2350 free_extent_buffer(eb
);
2351 block
->key_ready
= 1;
2355 static int reada_tree_block(struct reloc_control
*rc
,
2356 struct tree_block
*block
)
2358 BUG_ON(block
->key_ready
);
2359 readahead_tree_block(rc
->extent_root
, block
->bytenr
,
2360 block
->key
.objectid
, block
->key
.offset
);
2365 * helper function to relocate a tree block
2367 static int relocate_tree_block(struct btrfs_trans_handle
*trans
,
2368 struct reloc_control
*rc
,
2369 struct backref_node
*node
,
2370 struct btrfs_key
*key
,
2371 struct btrfs_path
*path
)
2373 struct btrfs_root
*root
;
2376 root
= select_one_root(trans
, node
);
2377 if (unlikely(!root
)) {
2378 rc
->found_old_snapshot
= 1;
2379 update_processed_blocks(rc
, node
);
2383 if (root
->root_key
.objectid
== BTRFS_TREE_RELOC_OBJECTID
) {
2384 ret
= do_relocation(trans
, node
, key
, path
, 1);
2387 if (node
->level
== 0 && rc
->stage
== UPDATE_DATA_PTRS
) {
2388 ret
= replace_file_extents(trans
, rc
, root
,
2393 drop_node_buffer(node
);
2394 } else if (!root
->ref_cows
) {
2395 path
->lowest_level
= node
->level
;
2396 ret
= btrfs_search_slot(trans
, root
, key
, path
, 0, 1);
2397 btrfs_release_path(root
, path
);
2400 } else if (root
!= node
->root
) {
2401 WARN_ON(node
->level
> 0 || rc
->stage
!= UPDATE_DATA_PTRS
);
2404 update_processed_blocks(rc
, node
);
2407 drop_node_buffer(node
);
2412 * relocate a list of blocks
2414 static noinline_for_stack
2415 int relocate_tree_blocks(struct btrfs_trans_handle
*trans
,
2416 struct reloc_control
*rc
, struct rb_root
*blocks
)
2418 struct backref_cache
*cache
;
2419 struct backref_node
*node
;
2420 struct btrfs_path
*path
;
2421 struct tree_block
*block
;
2422 struct rb_node
*rb_node
;
2427 path
= btrfs_alloc_path();
2431 cache
= kmalloc(sizeof(*cache
), GFP_NOFS
);
2433 btrfs_free_path(path
);
2437 backref_cache_init(cache
);
2439 rb_node
= rb_first(blocks
);
2441 block
= rb_entry(rb_node
, struct tree_block
, rb_node
);
2443 level
= block
->level
;
2445 BUG_ON(level
!= block
->level
);
2446 if (!block
->key_ready
)
2447 reada_tree_block(rc
, block
);
2448 rb_node
= rb_next(rb_node
);
2451 rb_node
= rb_first(blocks
);
2453 block
= rb_entry(rb_node
, struct tree_block
, rb_node
);
2454 if (!block
->key_ready
)
2455 get_tree_block_key(rc
, block
);
2456 rb_node
= rb_next(rb_node
);
2459 rb_node
= rb_first(blocks
);
2461 block
= rb_entry(rb_node
, struct tree_block
, rb_node
);
2463 node
= build_backref_tree(rc
, cache
, &block
->key
,
2464 block
->level
, block
->bytenr
);
2466 err
= PTR_ERR(node
);
2470 ret
= relocate_tree_block(trans
, rc
, node
, &block
->key
,
2476 remove_backref_node(cache
, node
);
2477 rb_node
= rb_next(rb_node
);
2483 free_block_list(blocks
);
2486 * now backrefs of some upper level tree blocks have been cached,
2487 * try relocating blocks referenced by these upper level blocks.
2490 struct backref_node
*upper
= NULL
;
2491 if (trans
->transaction
->in_commit
||
2492 trans
->transaction
->delayed_refs
.flushing
)
2495 ret
= add_adjacent_blocks(trans
, rc
, cache
, blocks
, level
,
2502 rb_node
= rb_first(blocks
);
2504 block
= rb_entry(rb_node
, struct tree_block
, rb_node
);
2505 if (trans
->transaction
->in_commit
||
2506 trans
->transaction
->delayed_refs
.flushing
)
2508 BUG_ON(!block
->key_ready
);
2509 node
= build_backref_tree(rc
, cache
, &block
->key
,
2510 level
, block
->bytenr
);
2512 err
= PTR_ERR(node
);
2516 ret
= relocate_tree_block(trans
, rc
, node
,
2522 remove_backref_node(cache
, node
);
2523 rb_node
= rb_next(rb_node
);
2525 free_block_list(blocks
);
2528 ret
= link_to_upper(trans
, upper
, path
);
2533 remove_backref_node(cache
, upper
);
2537 free_block_list(blocks
);
2539 ret
= finish_pending_nodes(trans
, cache
, path
);
2544 btrfs_free_path(path
);
2548 static noinline_for_stack
2549 int setup_extent_mapping(struct inode
*inode
, u64 start
, u64 end
,
2552 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2553 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
2554 struct extent_map
*em
;
2557 em
= alloc_extent_map(GFP_NOFS
);
2562 em
->len
= end
+ 1 - start
;
2563 em
->block_len
= em
->len
;
2564 em
->block_start
= block_start
;
2565 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
2566 set_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
2568 lock_extent(&BTRFS_I(inode
)->io_tree
, start
, end
, GFP_NOFS
);
2570 write_lock(&em_tree
->lock
);
2571 ret
= add_extent_mapping(em_tree
, em
);
2572 write_unlock(&em_tree
->lock
);
2573 if (ret
!= -EEXIST
) {
2574 free_extent_map(em
);
2577 btrfs_drop_extent_cache(inode
, start
, end
, 0);
2579 unlock_extent(&BTRFS_I(inode
)->io_tree
, start
, end
, GFP_NOFS
);
2583 static int relocate_file_extent_cluster(struct inode
*inode
,
2584 struct file_extent_cluster
*cluster
)
2588 u64 offset
= BTRFS_I(inode
)->index_cnt
;
2589 unsigned long index
;
2590 unsigned long last_index
;
2591 unsigned int dirty_page
= 0;
2593 struct file_ra_state
*ra
;
2600 ra
= kzalloc(sizeof(*ra
), GFP_NOFS
);
2604 index
= (cluster
->start
- offset
) >> PAGE_CACHE_SHIFT
;
2605 last_index
= (cluster
->end
- offset
) >> PAGE_CACHE_SHIFT
;
2607 mutex_lock(&inode
->i_mutex
);
2609 i_size_write(inode
, cluster
->end
+ 1 - offset
);
2610 ret
= setup_extent_mapping(inode
, cluster
->start
- offset
,
2611 cluster
->end
- offset
, cluster
->start
);
2615 file_ra_state_init(ra
, inode
->i_mapping
);
2617 WARN_ON(cluster
->start
!= cluster
->boundary
[0]);
2618 while (index
<= last_index
) {
2619 page
= find_lock_page(inode
->i_mapping
, index
);
2621 page_cache_sync_readahead(inode
->i_mapping
,
2623 last_index
+ 1 - index
);
2624 page
= grab_cache_page(inode
->i_mapping
, index
);
2631 if (PageReadahead(page
)) {
2632 page_cache_async_readahead(inode
->i_mapping
,
2633 ra
, NULL
, page
, index
,
2634 last_index
+ 1 - index
);
2637 if (!PageUptodate(page
)) {
2638 btrfs_readpage(NULL
, page
);
2640 if (!PageUptodate(page
)) {
2642 page_cache_release(page
);
2648 page_start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
2649 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
2651 lock_extent(&BTRFS_I(inode
)->io_tree
,
2652 page_start
, page_end
, GFP_NOFS
);
2654 set_page_extent_mapped(page
);
2656 if (nr
< cluster
->nr
&&
2657 page_start
+ offset
== cluster
->boundary
[nr
]) {
2658 set_extent_bits(&BTRFS_I(inode
)->io_tree
,
2659 page_start
, page_end
,
2660 EXTENT_BOUNDARY
, GFP_NOFS
);
2663 btrfs_set_extent_delalloc(inode
, page_start
, page_end
, NULL
);
2665 set_page_dirty(page
);
2668 unlock_extent(&BTRFS_I(inode
)->io_tree
,
2669 page_start
, page_end
, GFP_NOFS
);
2671 page_cache_release(page
);
2674 if (nr
< cluster
->nr
&&
2675 page_end
+ 1 + offset
== cluster
->boundary
[nr
]) {
2676 balance_dirty_pages_ratelimited_nr(inode
->i_mapping
,
2682 balance_dirty_pages_ratelimited_nr(inode
->i_mapping
,
2685 WARN_ON(nr
!= cluster
->nr
);
2687 mutex_unlock(&inode
->i_mutex
);
2692 static noinline_for_stack
2693 int relocate_data_extent(struct inode
*inode
, struct btrfs_key
*extent_key
,
2694 struct file_extent_cluster
*cluster
)
2698 if (cluster
->nr
> 0 && extent_key
->objectid
!= cluster
->end
+ 1) {
2699 ret
= relocate_file_extent_cluster(inode
, cluster
);
2706 cluster
->start
= extent_key
->objectid
;
2708 BUG_ON(cluster
->nr
>= MAX_EXTENTS
);
2709 cluster
->end
= extent_key
->objectid
+ extent_key
->offset
- 1;
2710 cluster
->boundary
[cluster
->nr
] = extent_key
->objectid
;
2713 if (cluster
->nr
>= MAX_EXTENTS
) {
2714 ret
= relocate_file_extent_cluster(inode
, cluster
);
2722 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2723 static int get_ref_objectid_v0(struct reloc_control
*rc
,
2724 struct btrfs_path
*path
,
2725 struct btrfs_key
*extent_key
,
2726 u64
*ref_objectid
, int *path_change
)
2728 struct btrfs_key key
;
2729 struct extent_buffer
*leaf
;
2730 struct btrfs_extent_ref_v0
*ref0
;
2734 leaf
= path
->nodes
[0];
2735 slot
= path
->slots
[0];
2737 if (slot
>= btrfs_header_nritems(leaf
)) {
2738 ret
= btrfs_next_leaf(rc
->extent_root
, path
);
2742 leaf
= path
->nodes
[0];
2743 slot
= path
->slots
[0];
2747 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
2748 if (key
.objectid
!= extent_key
->objectid
)
2751 if (key
.type
!= BTRFS_EXTENT_REF_V0_KEY
) {
2755 ref0
= btrfs_item_ptr(leaf
, slot
,
2756 struct btrfs_extent_ref_v0
);
2757 *ref_objectid
= btrfs_ref_objectid_v0(leaf
, ref0
);
2765 * helper to add a tree block to the list.
2766 * the major work is getting the generation and level of the block
2768 static int add_tree_block(struct reloc_control
*rc
,
2769 struct btrfs_key
*extent_key
,
2770 struct btrfs_path
*path
,
2771 struct rb_root
*blocks
)
2773 struct extent_buffer
*eb
;
2774 struct btrfs_extent_item
*ei
;
2775 struct btrfs_tree_block_info
*bi
;
2776 struct tree_block
*block
;
2777 struct rb_node
*rb_node
;
2782 eb
= path
->nodes
[0];
2783 item_size
= btrfs_item_size_nr(eb
, path
->slots
[0]);
2785 if (item_size
>= sizeof(*ei
) + sizeof(*bi
)) {
2786 ei
= btrfs_item_ptr(eb
, path
->slots
[0],
2787 struct btrfs_extent_item
);
2788 bi
= (struct btrfs_tree_block_info
*)(ei
+ 1);
2789 generation
= btrfs_extent_generation(eb
, ei
);
2790 level
= btrfs_tree_block_level(eb
, bi
);
2792 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2796 BUG_ON(item_size
!= sizeof(struct btrfs_extent_item_v0
));
2797 ret
= get_ref_objectid_v0(rc
, path
, extent_key
,
2799 BUG_ON(ref_owner
>= BTRFS_MAX_LEVEL
);
2800 level
= (int)ref_owner
;
2801 /* FIXME: get real generation */
2808 btrfs_release_path(rc
->extent_root
, path
);
2810 BUG_ON(level
== -1);
2812 block
= kmalloc(sizeof(*block
), GFP_NOFS
);
2816 block
->bytenr
= extent_key
->objectid
;
2817 block
->key
.objectid
= extent_key
->offset
;
2818 block
->key
.offset
= generation
;
2819 block
->level
= level
;
2820 block
->key_ready
= 0;
2822 rb_node
= tree_insert(blocks
, block
->bytenr
, &block
->rb_node
);
2829 * helper to add tree blocks for backref of type BTRFS_SHARED_DATA_REF_KEY
2831 static int __add_tree_block(struct reloc_control
*rc
,
2832 u64 bytenr
, u32 blocksize
,
2833 struct rb_root
*blocks
)
2835 struct btrfs_path
*path
;
2836 struct btrfs_key key
;
2839 if (tree_block_processed(bytenr
, blocksize
, rc
))
2842 if (tree_search(blocks
, bytenr
))
2845 path
= btrfs_alloc_path();
2849 key
.objectid
= bytenr
;
2850 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
2851 key
.offset
= blocksize
;
2853 path
->search_commit_root
= 1;
2854 path
->skip_locking
= 1;
2855 ret
= btrfs_search_slot(NULL
, rc
->extent_root
, &key
, path
, 0, 0);
2860 btrfs_item_key_to_cpu(path
->nodes
[0], &key
, path
->slots
[0]);
2861 ret
= add_tree_block(rc
, &key
, path
, blocks
);
2863 btrfs_free_path(path
);
2868 * helper to check if the block use full backrefs for pointers in it
2870 static int block_use_full_backref(struct reloc_control
*rc
,
2871 struct extent_buffer
*eb
)
2873 struct btrfs_path
*path
;
2874 struct btrfs_extent_item
*ei
;
2875 struct btrfs_key key
;
2879 if (btrfs_header_flag(eb
, BTRFS_HEADER_FLAG_RELOC
) ||
2880 btrfs_header_backref_rev(eb
) < BTRFS_MIXED_BACKREF_REV
)
2883 path
= btrfs_alloc_path();
2886 key
.objectid
= eb
->start
;
2887 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
2888 key
.offset
= eb
->len
;
2890 path
->search_commit_root
= 1;
2891 path
->skip_locking
= 1;
2892 ret
= btrfs_search_slot(NULL
, rc
->extent_root
,
2896 ei
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
2897 struct btrfs_extent_item
);
2898 flags
= btrfs_extent_flags(path
->nodes
[0], ei
);
2899 BUG_ON(!(flags
& BTRFS_EXTENT_FLAG_TREE_BLOCK
));
2900 if (flags
& BTRFS_BLOCK_FLAG_FULL_BACKREF
)
2904 btrfs_free_path(path
);
2909 * helper to add tree blocks for backref of type BTRFS_EXTENT_DATA_REF_KEY
2910 * this function scans fs tree to find blocks reference the data extent
2912 static int find_data_references(struct reloc_control
*rc
,
2913 struct btrfs_key
*extent_key
,
2914 struct extent_buffer
*leaf
,
2915 struct btrfs_extent_data_ref
*ref
,
2916 struct rb_root
*blocks
)
2918 struct btrfs_path
*path
;
2919 struct tree_block
*block
;
2920 struct btrfs_root
*root
;
2921 struct btrfs_file_extent_item
*fi
;
2922 struct rb_node
*rb_node
;
2923 struct btrfs_key key
;
2934 path
= btrfs_alloc_path();
2938 ref_root
= btrfs_extent_data_ref_root(leaf
, ref
);
2939 ref_objectid
= btrfs_extent_data_ref_objectid(leaf
, ref
);
2940 ref_offset
= btrfs_extent_data_ref_offset(leaf
, ref
);
2941 ref_count
= btrfs_extent_data_ref_count(leaf
, ref
);
2943 root
= read_fs_root(rc
->extent_root
->fs_info
, ref_root
);
2945 err
= PTR_ERR(root
);
2949 key
.objectid
= ref_objectid
;
2950 key
.offset
= ref_offset
;
2951 key
.type
= BTRFS_EXTENT_DATA_KEY
;
2953 path
->search_commit_root
= 1;
2954 path
->skip_locking
= 1;
2955 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2961 leaf
= path
->nodes
[0];
2962 nritems
= btrfs_header_nritems(leaf
);
2964 * the references in tree blocks that use full backrefs
2965 * are not counted in
2967 if (block_use_full_backref(rc
, leaf
))
2971 rb_node
= tree_search(blocks
, leaf
->start
);
2976 path
->slots
[0] = nritems
;
2979 while (ref_count
> 0) {
2980 while (path
->slots
[0] >= nritems
) {
2981 ret
= btrfs_next_leaf(root
, path
);
2991 leaf
= path
->nodes
[0];
2992 nritems
= btrfs_header_nritems(leaf
);
2995 if (block_use_full_backref(rc
, leaf
))
2999 rb_node
= tree_search(blocks
, leaf
->start
);
3004 path
->slots
[0] = nritems
;
3008 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
3009 if (key
.objectid
!= ref_objectid
||
3010 key
.type
!= BTRFS_EXTENT_DATA_KEY
) {
3015 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
3016 struct btrfs_file_extent_item
);
3018 if (btrfs_file_extent_type(leaf
, fi
) ==
3019 BTRFS_FILE_EXTENT_INLINE
)
3022 if (btrfs_file_extent_disk_bytenr(leaf
, fi
) !=
3023 extent_key
->objectid
)
3026 key
.offset
-= btrfs_file_extent_offset(leaf
, fi
);
3027 if (key
.offset
!= ref_offset
)
3035 if (!tree_block_processed(leaf
->start
, leaf
->len
, rc
)) {
3036 block
= kmalloc(sizeof(*block
), GFP_NOFS
);
3041 block
->bytenr
= leaf
->start
;
3042 btrfs_item_key_to_cpu(leaf
, &block
->key
, 0);
3044 block
->key_ready
= 1;
3045 rb_node
= tree_insert(blocks
, block
->bytenr
,
3052 path
->slots
[0] = nritems
;
3058 btrfs_free_path(path
);
3063 * hepler to find all tree blocks that reference a given data extent
3065 static noinline_for_stack
3066 int add_data_references(struct reloc_control
*rc
,
3067 struct btrfs_key
*extent_key
,
3068 struct btrfs_path
*path
,
3069 struct rb_root
*blocks
)
3071 struct btrfs_key key
;
3072 struct extent_buffer
*eb
;
3073 struct btrfs_extent_data_ref
*dref
;
3074 struct btrfs_extent_inline_ref
*iref
;
3081 ret
= get_new_location(rc
->data_inode
, NULL
, extent_key
->objectid
,
3082 extent_key
->offset
);
3085 /* the relocated data is fragmented */
3086 rc
->extents_skipped
++;
3087 btrfs_release_path(rc
->extent_root
, path
);
3091 blocksize
= btrfs_level_size(rc
->extent_root
, 0);
3093 eb
= path
->nodes
[0];
3094 ptr
= btrfs_item_ptr_offset(eb
, path
->slots
[0]);
3095 end
= ptr
+ btrfs_item_size_nr(eb
, path
->slots
[0]);
3096 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3097 if (ptr
+ sizeof(struct btrfs_extent_item_v0
) == end
)
3101 ptr
+= sizeof(struct btrfs_extent_item
);
3104 iref
= (struct btrfs_extent_inline_ref
*)ptr
;
3105 key
.type
= btrfs_extent_inline_ref_type(eb
, iref
);
3106 if (key
.type
== BTRFS_SHARED_DATA_REF_KEY
) {
3107 key
.offset
= btrfs_extent_inline_ref_offset(eb
, iref
);
3108 ret
= __add_tree_block(rc
, key
.offset
, blocksize
,
3110 } else if (key
.type
== BTRFS_EXTENT_DATA_REF_KEY
) {
3111 dref
= (struct btrfs_extent_data_ref
*)(&iref
->offset
);
3112 ret
= find_data_references(rc
, extent_key
,
3117 ptr
+= btrfs_extent_inline_ref_size(key
.type
);
3123 eb
= path
->nodes
[0];
3124 if (path
->slots
[0] >= btrfs_header_nritems(eb
)) {
3125 ret
= btrfs_next_leaf(rc
->extent_root
, path
);
3132 eb
= path
->nodes
[0];
3135 btrfs_item_key_to_cpu(eb
, &key
, path
->slots
[0]);
3136 if (key
.objectid
!= extent_key
->objectid
)
3139 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3140 if (key
.type
== BTRFS_SHARED_DATA_REF_KEY
||
3141 key
.type
== BTRFS_EXTENT_REF_V0_KEY
) {
3143 BUG_ON(key
.type
== BTRFS_EXTENT_REF_V0_KEY
);
3144 if (key
.type
== BTRFS_SHARED_DATA_REF_KEY
) {
3146 ret
= __add_tree_block(rc
, key
.offset
, blocksize
,
3148 } else if (key
.type
== BTRFS_EXTENT_DATA_REF_KEY
) {
3149 dref
= btrfs_item_ptr(eb
, path
->slots
[0],
3150 struct btrfs_extent_data_ref
);
3151 ret
= find_data_references(rc
, extent_key
,
3162 btrfs_release_path(rc
->extent_root
, path
);
3164 free_block_list(blocks
);
3169 * hepler to find next unprocessed extent
3171 static noinline_for_stack
3172 int find_next_extent(struct btrfs_trans_handle
*trans
,
3173 struct reloc_control
*rc
, struct btrfs_path
*path
)
3175 struct btrfs_key key
;
3176 struct extent_buffer
*leaf
;
3177 u64 start
, end
, last
;
3180 last
= rc
->block_group
->key
.objectid
+ rc
->block_group
->key
.offset
;
3183 if (rc
->search_start
>= last
) {
3188 key
.objectid
= rc
->search_start
;
3189 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
3192 path
->search_commit_root
= 1;
3193 path
->skip_locking
= 1;
3194 ret
= btrfs_search_slot(NULL
, rc
->extent_root
, &key
, path
,
3199 leaf
= path
->nodes
[0];
3200 if (path
->slots
[0] >= btrfs_header_nritems(leaf
)) {
3201 ret
= btrfs_next_leaf(rc
->extent_root
, path
);
3204 leaf
= path
->nodes
[0];
3207 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
3208 if (key
.objectid
>= last
) {
3213 if (key
.type
!= BTRFS_EXTENT_ITEM_KEY
||
3214 key
.objectid
+ key
.offset
<= rc
->search_start
) {
3219 ret
= find_first_extent_bit(&rc
->processed_blocks
,
3220 key
.objectid
, &start
, &end
,
3223 if (ret
== 0 && start
<= key
.objectid
) {
3224 btrfs_release_path(rc
->extent_root
, path
);
3225 rc
->search_start
= end
+ 1;
3227 rc
->search_start
= key
.objectid
+ key
.offset
;
3231 btrfs_release_path(rc
->extent_root
, path
);
3235 static void set_reloc_control(struct reloc_control
*rc
)
3237 struct btrfs_fs_info
*fs_info
= rc
->extent_root
->fs_info
;
3238 mutex_lock(&fs_info
->trans_mutex
);
3239 fs_info
->reloc_ctl
= rc
;
3240 mutex_unlock(&fs_info
->trans_mutex
);
3243 static void unset_reloc_control(struct reloc_control
*rc
)
3245 struct btrfs_fs_info
*fs_info
= rc
->extent_root
->fs_info
;
3246 mutex_lock(&fs_info
->trans_mutex
);
3247 fs_info
->reloc_ctl
= NULL
;
3248 mutex_unlock(&fs_info
->trans_mutex
);
3251 static int check_extent_flags(u64 flags
)
3253 if ((flags
& BTRFS_EXTENT_FLAG_DATA
) &&
3254 (flags
& BTRFS_EXTENT_FLAG_TREE_BLOCK
))
3256 if (!(flags
& BTRFS_EXTENT_FLAG_DATA
) &&
3257 !(flags
& BTRFS_EXTENT_FLAG_TREE_BLOCK
))
3259 if ((flags
& BTRFS_EXTENT_FLAG_DATA
) &&
3260 (flags
& BTRFS_BLOCK_FLAG_FULL_BACKREF
))
3266 static noinline_for_stack
int relocate_block_group(struct reloc_control
*rc
)
3268 struct rb_root blocks
= RB_ROOT
;
3269 struct btrfs_key key
;
3270 struct file_extent_cluster
*cluster
;
3271 struct btrfs_trans_handle
*trans
= NULL
;
3272 struct btrfs_path
*path
;
3273 struct btrfs_extent_item
*ei
;
3280 cluster
= kzalloc(sizeof(*cluster
), GFP_NOFS
);
3284 path
= btrfs_alloc_path();
3290 rc
->extents_found
= 0;
3291 rc
->extents_skipped
= 0;
3293 rc
->search_start
= rc
->block_group
->key
.objectid
;
3294 clear_extent_bits(&rc
->processed_blocks
, 0, (u64
)-1, EXTENT_DIRTY
,
3297 rc
->create_reloc_root
= 1;
3298 set_reloc_control(rc
);
3300 trans
= btrfs_start_transaction(rc
->extent_root
, 1);
3301 btrfs_commit_transaction(trans
, rc
->extent_root
);
3304 trans
= btrfs_start_transaction(rc
->extent_root
, 1);
3306 ret
= find_next_extent(trans
, rc
, path
);
3312 rc
->extents_found
++;
3314 ei
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
3315 struct btrfs_extent_item
);
3316 btrfs_item_key_to_cpu(path
->nodes
[0], &key
, path
->slots
[0]);
3317 item_size
= btrfs_item_size_nr(path
->nodes
[0],
3319 if (item_size
>= sizeof(*ei
)) {
3320 flags
= btrfs_extent_flags(path
->nodes
[0], ei
);
3321 ret
= check_extent_flags(flags
);
3325 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3327 int path_change
= 0;
3330 sizeof(struct btrfs_extent_item_v0
));
3331 ret
= get_ref_objectid_v0(rc
, path
, &key
, &ref_owner
,
3333 if (ref_owner
< BTRFS_FIRST_FREE_OBJECTID
)
3334 flags
= BTRFS_EXTENT_FLAG_TREE_BLOCK
;
3336 flags
= BTRFS_EXTENT_FLAG_DATA
;
3339 btrfs_release_path(rc
->extent_root
, path
);
3341 path
->search_commit_root
= 1;
3342 path
->skip_locking
= 1;
3343 ret
= btrfs_search_slot(NULL
, rc
->extent_root
,
3356 if (flags
& BTRFS_EXTENT_FLAG_TREE_BLOCK
) {
3357 ret
= add_tree_block(rc
, &key
, path
, &blocks
);
3358 } else if (rc
->stage
== UPDATE_DATA_PTRS
&&
3359 (flags
& BTRFS_EXTENT_FLAG_DATA
)) {
3360 ret
= add_data_references(rc
, &key
, path
, &blocks
);
3362 btrfs_release_path(rc
->extent_root
, path
);
3370 if (!RB_EMPTY_ROOT(&blocks
)) {
3371 ret
= relocate_tree_blocks(trans
, rc
, &blocks
);
3378 nr
= trans
->blocks_used
;
3379 btrfs_end_transaction(trans
, rc
->extent_root
);
3381 btrfs_btree_balance_dirty(rc
->extent_root
, nr
);
3383 if (rc
->stage
== MOVE_DATA_EXTENTS
&&
3384 (flags
& BTRFS_EXTENT_FLAG_DATA
)) {
3385 rc
->found_file_extent
= 1;
3386 ret
= relocate_data_extent(rc
->data_inode
,
3394 btrfs_free_path(path
);
3397 nr
= trans
->blocks_used
;
3398 btrfs_end_transaction(trans
, rc
->extent_root
);
3399 btrfs_btree_balance_dirty(rc
->extent_root
, nr
);
3403 ret
= relocate_file_extent_cluster(rc
->data_inode
, cluster
);
3410 rc
->create_reloc_root
= 0;
3413 if (rc
->extents_found
> 0) {
3414 trans
= btrfs_start_transaction(rc
->extent_root
, 1);
3415 btrfs_commit_transaction(trans
, rc
->extent_root
);
3418 merge_reloc_roots(rc
);
3420 unset_reloc_control(rc
);
3422 /* get rid of pinned extents */
3423 trans
= btrfs_start_transaction(rc
->extent_root
, 1);
3424 btrfs_commit_transaction(trans
, rc
->extent_root
);
3429 static int __insert_orphan_inode(struct btrfs_trans_handle
*trans
,
3430 struct btrfs_root
*root
, u64 objectid
)
3432 struct btrfs_path
*path
;
3433 struct btrfs_inode_item
*item
;
3434 struct extent_buffer
*leaf
;
3437 path
= btrfs_alloc_path();
3441 ret
= btrfs_insert_empty_inode(trans
, root
, path
, objectid
);
3445 leaf
= path
->nodes
[0];
3446 item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_inode_item
);
3447 memset_extent_buffer(leaf
, 0, (unsigned long)item
, sizeof(*item
));
3448 btrfs_set_inode_generation(leaf
, item
, 1);
3449 btrfs_set_inode_size(leaf
, item
, 0);
3450 btrfs_set_inode_mode(leaf
, item
, S_IFREG
| 0600);
3451 btrfs_set_inode_flags(leaf
, item
, BTRFS_INODE_NOCOMPRESS
);
3452 btrfs_mark_buffer_dirty(leaf
);
3453 btrfs_release_path(root
, path
);
3455 btrfs_free_path(path
);
3460 * helper to create inode for data relocation.
3461 * the inode is in data relocation tree and its link count is 0
3463 static struct inode
*create_reloc_inode(struct btrfs_fs_info
*fs_info
,
3464 struct btrfs_block_group_cache
*group
)
3466 struct inode
*inode
= NULL
;
3467 struct btrfs_trans_handle
*trans
;
3468 struct btrfs_root
*root
;
3469 struct btrfs_key key
;
3471 u64 objectid
= BTRFS_FIRST_FREE_OBJECTID
;
3474 root
= read_fs_root(fs_info
, BTRFS_DATA_RELOC_TREE_OBJECTID
);
3476 return ERR_CAST(root
);
3478 trans
= btrfs_start_transaction(root
, 1);
3481 err
= btrfs_find_free_objectid(trans
, root
, objectid
, &objectid
);
3485 err
= __insert_orphan_inode(trans
, root
, objectid
);
3488 key
.objectid
= objectid
;
3489 key
.type
= BTRFS_INODE_ITEM_KEY
;
3491 inode
= btrfs_iget(root
->fs_info
->sb
, &key
, root
, NULL
);
3492 BUG_ON(IS_ERR(inode
) || is_bad_inode(inode
));
3493 BTRFS_I(inode
)->index_cnt
= group
->key
.objectid
;
3495 err
= btrfs_orphan_add(trans
, inode
);
3497 nr
= trans
->blocks_used
;
3498 btrfs_end_transaction(trans
, root
);
3500 btrfs_btree_balance_dirty(root
, nr
);
3504 inode
= ERR_PTR(err
);
3510 * function to relocate all extents in a block group.
3512 int btrfs_relocate_block_group(struct btrfs_root
*extent_root
, u64 group_start
)
3514 struct btrfs_fs_info
*fs_info
= extent_root
->fs_info
;
3515 struct reloc_control
*rc
;
3519 rc
= kzalloc(sizeof(*rc
), GFP_NOFS
);
3523 mapping_tree_init(&rc
->reloc_root_tree
);
3524 extent_io_tree_init(&rc
->processed_blocks
, NULL
, GFP_NOFS
);
3525 INIT_LIST_HEAD(&rc
->reloc_roots
);
3527 rc
->block_group
= btrfs_lookup_block_group(fs_info
, group_start
);
3528 BUG_ON(!rc
->block_group
);
3530 btrfs_init_workers(&rc
->workers
, "relocate",
3531 fs_info
->thread_pool_size
, NULL
);
3533 rc
->extent_root
= extent_root
;
3534 btrfs_prepare_block_group_relocation(extent_root
, rc
->block_group
);
3536 rc
->data_inode
= create_reloc_inode(fs_info
, rc
->block_group
);
3537 if (IS_ERR(rc
->data_inode
)) {
3538 err
= PTR_ERR(rc
->data_inode
);
3539 rc
->data_inode
= NULL
;
3543 printk(KERN_INFO
"btrfs: relocating block group %llu flags %llu\n",
3544 (unsigned long long)rc
->block_group
->key
.objectid
,
3545 (unsigned long long)rc
->block_group
->flags
);
3547 btrfs_start_delalloc_inodes(fs_info
->tree_root
, 0);
3548 btrfs_wait_ordered_extents(fs_info
->tree_root
, 0, 0);
3551 rc
->extents_found
= 0;
3552 rc
->extents_skipped
= 0;
3554 mutex_lock(&fs_info
->cleaner_mutex
);
3556 btrfs_clean_old_snapshots(fs_info
->tree_root
);
3557 ret
= relocate_block_group(rc
);
3559 mutex_unlock(&fs_info
->cleaner_mutex
);
3565 if (rc
->extents_found
== 0)
3568 printk(KERN_INFO
"btrfs: found %llu extents\n",
3569 (unsigned long long)rc
->extents_found
);
3571 if (rc
->stage
== MOVE_DATA_EXTENTS
&& rc
->found_file_extent
) {
3572 btrfs_wait_ordered_range(rc
->data_inode
, 0, (u64
)-1);
3573 invalidate_mapping_pages(rc
->data_inode
->i_mapping
,
3575 rc
->stage
= UPDATE_DATA_PTRS
;
3576 } else if (rc
->stage
== UPDATE_DATA_PTRS
&&
3577 rc
->extents_skipped
>= rc
->extents_found
) {
3578 iput(rc
->data_inode
);
3579 rc
->data_inode
= create_reloc_inode(fs_info
,
3581 if (IS_ERR(rc
->data_inode
)) {
3582 err
= PTR_ERR(rc
->data_inode
);
3583 rc
->data_inode
= NULL
;
3586 rc
->stage
= MOVE_DATA_EXTENTS
;
3587 rc
->found_file_extent
= 0;
3591 filemap_write_and_wait_range(fs_info
->btree_inode
->i_mapping
,
3592 rc
->block_group
->key
.objectid
,
3593 rc
->block_group
->key
.objectid
+
3594 rc
->block_group
->key
.offset
- 1);
3596 WARN_ON(rc
->block_group
->pinned
> 0);
3597 WARN_ON(rc
->block_group
->reserved
> 0);
3598 WARN_ON(btrfs_block_group_used(&rc
->block_group
->item
) > 0);
3600 iput(rc
->data_inode
);
3601 btrfs_stop_workers(&rc
->workers
);
3602 btrfs_put_block_group(rc
->block_group
);
3607 static noinline_for_stack
int mark_garbage_root(struct btrfs_root
*root
)
3609 struct btrfs_trans_handle
*trans
;
3612 trans
= btrfs_start_transaction(root
->fs_info
->tree_root
, 1);
3614 memset(&root
->root_item
.drop_progress
, 0,
3615 sizeof(root
->root_item
.drop_progress
));
3616 root
->root_item
.drop_level
= 0;
3617 btrfs_set_root_refs(&root
->root_item
, 0);
3618 ret
= btrfs_update_root(trans
, root
->fs_info
->tree_root
,
3619 &root
->root_key
, &root
->root_item
);
3622 ret
= btrfs_end_transaction(trans
, root
->fs_info
->tree_root
);
3628 * recover relocation interrupted by system crash.
3630 * this function resumes merging reloc trees with corresponding fs trees.
3631 * this is important for keeping the sharing of tree blocks
3633 int btrfs_recover_relocation(struct btrfs_root
*root
)
3635 LIST_HEAD(reloc_roots
);
3636 struct btrfs_key key
;
3637 struct btrfs_root
*fs_root
;
3638 struct btrfs_root
*reloc_root
;
3639 struct btrfs_path
*path
;
3640 struct extent_buffer
*leaf
;
3641 struct reloc_control
*rc
= NULL
;
3642 struct btrfs_trans_handle
*trans
;
3646 path
= btrfs_alloc_path();
3650 key
.objectid
= BTRFS_TREE_RELOC_OBJECTID
;
3651 key
.type
= BTRFS_ROOT_ITEM_KEY
;
3652 key
.offset
= (u64
)-1;
3655 ret
= btrfs_search_slot(NULL
, root
->fs_info
->tree_root
, &key
,
3662 if (path
->slots
[0] == 0)
3666 leaf
= path
->nodes
[0];
3667 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
3668 btrfs_release_path(root
->fs_info
->tree_root
, path
);
3670 if (key
.objectid
!= BTRFS_TREE_RELOC_OBJECTID
||
3671 key
.type
!= BTRFS_ROOT_ITEM_KEY
)
3674 reloc_root
= btrfs_read_fs_root_no_radix(root
, &key
);
3675 if (IS_ERR(reloc_root
)) {
3676 err
= PTR_ERR(reloc_root
);
3680 list_add(&reloc_root
->root_list
, &reloc_roots
);
3682 if (btrfs_root_refs(&reloc_root
->root_item
) > 0) {
3683 fs_root
= read_fs_root(root
->fs_info
,
3684 reloc_root
->root_key
.offset
);
3685 if (IS_ERR(fs_root
)) {
3686 ret
= PTR_ERR(fs_root
);
3687 if (ret
!= -ENOENT
) {
3691 mark_garbage_root(reloc_root
);
3695 if (key
.offset
== 0)
3700 btrfs_release_path(root
->fs_info
->tree_root
, path
);
3702 if (list_empty(&reloc_roots
))
3705 rc
= kzalloc(sizeof(*rc
), GFP_NOFS
);
3711 mapping_tree_init(&rc
->reloc_root_tree
);
3712 INIT_LIST_HEAD(&rc
->reloc_roots
);
3713 btrfs_init_workers(&rc
->workers
, "relocate",
3714 root
->fs_info
->thread_pool_size
, NULL
);
3715 rc
->extent_root
= root
->fs_info
->extent_root
;
3717 set_reloc_control(rc
);
3719 while (!list_empty(&reloc_roots
)) {
3720 reloc_root
= list_entry(reloc_roots
.next
,
3721 struct btrfs_root
, root_list
);
3722 list_del(&reloc_root
->root_list
);
3724 if (btrfs_root_refs(&reloc_root
->root_item
) == 0) {
3725 list_add_tail(&reloc_root
->root_list
,
3730 fs_root
= read_fs_root(root
->fs_info
,
3731 reloc_root
->root_key
.offset
);
3732 BUG_ON(IS_ERR(fs_root
));
3734 __add_reloc_root(reloc_root
);
3735 fs_root
->reloc_root
= reloc_root
;
3738 trans
= btrfs_start_transaction(rc
->extent_root
, 1);
3739 btrfs_commit_transaction(trans
, rc
->extent_root
);
3741 merge_reloc_roots(rc
);
3743 unset_reloc_control(rc
);
3745 trans
= btrfs_start_transaction(rc
->extent_root
, 1);
3746 btrfs_commit_transaction(trans
, rc
->extent_root
);
3749 btrfs_stop_workers(&rc
->workers
);
3752 while (!list_empty(&reloc_roots
)) {
3753 reloc_root
= list_entry(reloc_roots
.next
,
3754 struct btrfs_root
, root_list
);
3755 list_del(&reloc_root
->root_list
);
3756 free_extent_buffer(reloc_root
->node
);
3757 free_extent_buffer(reloc_root
->commit_root
);
3760 btrfs_free_path(path
);
3763 /* cleanup orphan inode in data relocation tree */
3764 fs_root
= read_fs_root(root
->fs_info
,
3765 BTRFS_DATA_RELOC_TREE_OBJECTID
);
3766 if (IS_ERR(fs_root
))
3767 err
= PTR_ERR(fs_root
);
3769 btrfs_orphan_cleanup(fs_root
);
3775 * helper to add ordered checksum for data relocation.
3777 * cloning checksum properly handles the nodatasum extents.
3778 * it also saves CPU time to re-calculate the checksum.
3780 int btrfs_reloc_clone_csums(struct inode
*inode
, u64 file_pos
, u64 len
)
3782 struct btrfs_ordered_sum
*sums
;
3783 struct btrfs_sector_sum
*sector_sum
;
3784 struct btrfs_ordered_extent
*ordered
;
3785 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3791 ordered
= btrfs_lookup_ordered_extent(inode
, file_pos
);
3792 BUG_ON(ordered
->file_offset
!= file_pos
|| ordered
->len
!= len
);
3794 disk_bytenr
= file_pos
+ BTRFS_I(inode
)->index_cnt
;
3795 ret
= btrfs_lookup_csums_range(root
->fs_info
->csum_root
, disk_bytenr
,
3796 disk_bytenr
+ len
- 1, &list
);
3798 while (!list_empty(&list
)) {
3799 sums
= list_entry(list
.next
, struct btrfs_ordered_sum
, list
);
3800 list_del_init(&sums
->list
);
3802 sector_sum
= sums
->sums
;
3803 sums
->bytenr
= ordered
->start
;
3806 while (offset
< sums
->len
) {
3807 sector_sum
->bytenr
+= ordered
->start
- disk_bytenr
;
3809 offset
+= root
->sectorsize
;
3812 btrfs_add_ordered_sum(inode
, ordered
, sums
);
3814 btrfs_put_ordered_extent(ordered
);