1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2009 Oracle. All rights reserved.
6 #include <linux/sched.h>
7 #include <linux/pagemap.h>
8 #include <linux/writeback.h>
9 #include <linux/blkdev.h>
10 #include <linux/rbtree.h>
11 #include <linux/slab.h>
14 #include "transaction.h"
17 #include "btrfs_inode.h"
18 #include "async-thread.h"
19 #include "free-space-cache.h"
20 #include "inode-map.h"
22 #include "print-tree.h"
23 #include "delalloc-space.h"
24 #include "block-group.h"
27 * backref_node, mapping_node and tree_block start with this
30 struct rb_node rb_node
;
35 * present a tree block in the backref cache
38 struct rb_node rb_node
;
42 /* objectid of tree block owner, can be not uptodate */
44 /* link to pending, changed or detached list */
45 struct list_head list
;
46 /* list of upper level blocks reference this block */
47 struct list_head upper
;
48 /* list of child blocks in the cache */
49 struct list_head lower
;
50 /* NULL if this node is not tree root */
51 struct btrfs_root
*root
;
52 /* extent buffer got by COW the block */
53 struct extent_buffer
*eb
;
54 /* level of tree block */
56 /* is the block in non-reference counted tree */
57 unsigned int cowonly
:1;
58 /* 1 if no child node in the cache */
59 unsigned int lowest
:1;
60 /* is the extent buffer locked */
61 unsigned int locked
:1;
62 /* has the block been processed */
63 unsigned int processed
:1;
64 /* have backrefs of this block been checked */
65 unsigned int checked
:1;
67 * 1 if corresponding block has been cowed but some upper
68 * level block pointers may not point to the new location
70 unsigned int pending
:1;
72 * 1 if the backref node isn't connected to any other
75 unsigned int detached
:1;
79 * present a block pointer in the backref cache
82 struct list_head list
[2];
83 struct backref_node
*node
[2];
88 #define RELOCATION_RESERVED_NODES 256
90 struct backref_cache
{
91 /* red black tree of all backref nodes in the cache */
92 struct rb_root rb_root
;
93 /* for passing backref nodes to btrfs_reloc_cow_block */
94 struct backref_node
*path
[BTRFS_MAX_LEVEL
];
96 * list of blocks that have been cowed but some block
97 * pointers in upper level blocks may not reflect the
100 struct list_head pending
[BTRFS_MAX_LEVEL
];
101 /* list of backref nodes with no child node */
102 struct list_head leaves
;
103 /* list of blocks that have been cowed in current transaction */
104 struct list_head changed
;
105 /* list of detached backref node. */
106 struct list_head detached
;
115 * map address of tree root to tree
117 struct mapping_node
{
118 struct rb_node rb_node
;
123 struct mapping_tree
{
124 struct rb_root rb_root
;
129 * present a tree block to process
132 struct rb_node rb_node
;
134 struct btrfs_key key
;
135 unsigned int level
:8;
136 unsigned int key_ready
:1;
139 #define MAX_EXTENTS 128
141 struct file_extent_cluster
{
144 u64 boundary
[MAX_EXTENTS
];
148 struct reloc_control
{
149 /* block group to relocate */
150 struct btrfs_block_group
*block_group
;
152 struct btrfs_root
*extent_root
;
153 /* inode for moving data */
154 struct inode
*data_inode
;
156 struct btrfs_block_rsv
*block_rsv
;
158 struct backref_cache backref_cache
;
160 struct file_extent_cluster cluster
;
161 /* tree blocks have been processed */
162 struct extent_io_tree processed_blocks
;
163 /* map start of tree root to corresponding reloc tree */
164 struct mapping_tree reloc_root_tree
;
165 /* list of reloc trees */
166 struct list_head reloc_roots
;
167 /* list of subvolume trees that get relocated */
168 struct list_head dirty_subvol_roots
;
169 /* size of metadata reservation for merging reloc trees */
170 u64 merging_rsv_size
;
171 /* size of relocated tree nodes */
173 /* reserved size for block group relocation*/
179 unsigned int stage
:8;
180 unsigned int create_reloc_tree
:1;
181 unsigned int merge_reloc_tree
:1;
182 unsigned int found_file_extent
:1;
185 /* stages of data relocation */
186 #define MOVE_DATA_EXTENTS 0
187 #define UPDATE_DATA_PTRS 1
189 static void remove_backref_node(struct backref_cache
*cache
,
190 struct backref_node
*node
);
191 static void __mark_block_processed(struct reloc_control
*rc
,
192 struct backref_node
*node
);
194 static void mapping_tree_init(struct mapping_tree
*tree
)
196 tree
->rb_root
= RB_ROOT
;
197 spin_lock_init(&tree
->lock
);
200 static void backref_cache_init(struct backref_cache
*cache
)
203 cache
->rb_root
= RB_ROOT
;
204 for (i
= 0; i
< BTRFS_MAX_LEVEL
; i
++)
205 INIT_LIST_HEAD(&cache
->pending
[i
]);
206 INIT_LIST_HEAD(&cache
->changed
);
207 INIT_LIST_HEAD(&cache
->detached
);
208 INIT_LIST_HEAD(&cache
->leaves
);
211 static void backref_cache_cleanup(struct backref_cache
*cache
)
213 struct backref_node
*node
;
216 while (!list_empty(&cache
->detached
)) {
217 node
= list_entry(cache
->detached
.next
,
218 struct backref_node
, list
);
219 remove_backref_node(cache
, node
);
222 while (!list_empty(&cache
->leaves
)) {
223 node
= list_entry(cache
->leaves
.next
,
224 struct backref_node
, lower
);
225 remove_backref_node(cache
, node
);
228 cache
->last_trans
= 0;
230 for (i
= 0; i
< BTRFS_MAX_LEVEL
; i
++)
231 ASSERT(list_empty(&cache
->pending
[i
]));
232 ASSERT(list_empty(&cache
->changed
));
233 ASSERT(list_empty(&cache
->detached
));
234 ASSERT(RB_EMPTY_ROOT(&cache
->rb_root
));
235 ASSERT(!cache
->nr_nodes
);
236 ASSERT(!cache
->nr_edges
);
239 static struct backref_node
*alloc_backref_node(struct backref_cache
*cache
)
241 struct backref_node
*node
;
243 node
= kzalloc(sizeof(*node
), GFP_NOFS
);
245 INIT_LIST_HEAD(&node
->list
);
246 INIT_LIST_HEAD(&node
->upper
);
247 INIT_LIST_HEAD(&node
->lower
);
248 RB_CLEAR_NODE(&node
->rb_node
);
254 static void free_backref_node(struct backref_cache
*cache
,
255 struct backref_node
*node
)
263 static struct backref_edge
*alloc_backref_edge(struct backref_cache
*cache
)
265 struct backref_edge
*edge
;
267 edge
= kzalloc(sizeof(*edge
), GFP_NOFS
);
273 static void free_backref_edge(struct backref_cache
*cache
,
274 struct backref_edge
*edge
)
282 static struct rb_node
*tree_insert(struct rb_root
*root
, u64 bytenr
,
283 struct rb_node
*node
)
285 struct rb_node
**p
= &root
->rb_node
;
286 struct rb_node
*parent
= NULL
;
287 struct tree_entry
*entry
;
291 entry
= rb_entry(parent
, struct tree_entry
, rb_node
);
293 if (bytenr
< entry
->bytenr
)
295 else if (bytenr
> entry
->bytenr
)
301 rb_link_node(node
, parent
, p
);
302 rb_insert_color(node
, root
);
306 static struct rb_node
*tree_search(struct rb_root
*root
, u64 bytenr
)
308 struct rb_node
*n
= root
->rb_node
;
309 struct tree_entry
*entry
;
312 entry
= rb_entry(n
, struct tree_entry
, rb_node
);
314 if (bytenr
< entry
->bytenr
)
316 else if (bytenr
> entry
->bytenr
)
324 static void backref_tree_panic(struct rb_node
*rb_node
, int errno
, u64 bytenr
)
327 struct btrfs_fs_info
*fs_info
= NULL
;
328 struct backref_node
*bnode
= rb_entry(rb_node
, struct backref_node
,
331 fs_info
= bnode
->root
->fs_info
;
332 btrfs_panic(fs_info
, errno
,
333 "Inconsistency in backref cache found at offset %llu",
338 * walk up backref nodes until reach node presents tree root
340 static struct backref_node
*walk_up_backref(struct backref_node
*node
,
341 struct backref_edge
*edges
[],
344 struct backref_edge
*edge
;
347 while (!list_empty(&node
->upper
)) {
348 edge
= list_entry(node
->upper
.next
,
349 struct backref_edge
, list
[LOWER
]);
351 node
= edge
->node
[UPPER
];
353 BUG_ON(node
->detached
);
359 * walk down backref nodes to find start of next reference path
361 static struct backref_node
*walk_down_backref(struct backref_edge
*edges
[],
364 struct backref_edge
*edge
;
365 struct backref_node
*lower
;
369 edge
= edges
[idx
- 1];
370 lower
= edge
->node
[LOWER
];
371 if (list_is_last(&edge
->list
[LOWER
], &lower
->upper
)) {
375 edge
= list_entry(edge
->list
[LOWER
].next
,
376 struct backref_edge
, list
[LOWER
]);
377 edges
[idx
- 1] = edge
;
379 return edge
->node
[UPPER
];
385 static void unlock_node_buffer(struct backref_node
*node
)
388 btrfs_tree_unlock(node
->eb
);
393 static void drop_node_buffer(struct backref_node
*node
)
396 unlock_node_buffer(node
);
397 free_extent_buffer(node
->eb
);
402 static void drop_backref_node(struct backref_cache
*tree
,
403 struct backref_node
*node
)
405 BUG_ON(!list_empty(&node
->upper
));
407 drop_node_buffer(node
);
408 list_del(&node
->list
);
409 list_del(&node
->lower
);
410 if (!RB_EMPTY_NODE(&node
->rb_node
))
411 rb_erase(&node
->rb_node
, &tree
->rb_root
);
412 free_backref_node(tree
, node
);
416 * remove a backref node from the backref cache
418 static void remove_backref_node(struct backref_cache
*cache
,
419 struct backref_node
*node
)
421 struct backref_node
*upper
;
422 struct backref_edge
*edge
;
427 BUG_ON(!node
->lowest
&& !node
->detached
);
428 while (!list_empty(&node
->upper
)) {
429 edge
= list_entry(node
->upper
.next
, struct backref_edge
,
431 upper
= edge
->node
[UPPER
];
432 list_del(&edge
->list
[LOWER
]);
433 list_del(&edge
->list
[UPPER
]);
434 free_backref_edge(cache
, edge
);
436 if (RB_EMPTY_NODE(&upper
->rb_node
)) {
437 BUG_ON(!list_empty(&node
->upper
));
438 drop_backref_node(cache
, node
);
444 * add the node to leaf node list if no other
445 * child block cached.
447 if (list_empty(&upper
->lower
)) {
448 list_add_tail(&upper
->lower
, &cache
->leaves
);
453 drop_backref_node(cache
, node
);
456 static void update_backref_node(struct backref_cache
*cache
,
457 struct backref_node
*node
, u64 bytenr
)
459 struct rb_node
*rb_node
;
460 rb_erase(&node
->rb_node
, &cache
->rb_root
);
461 node
->bytenr
= bytenr
;
462 rb_node
= tree_insert(&cache
->rb_root
, node
->bytenr
, &node
->rb_node
);
464 backref_tree_panic(rb_node
, -EEXIST
, bytenr
);
468 * update backref cache after a transaction commit
470 static int update_backref_cache(struct btrfs_trans_handle
*trans
,
471 struct backref_cache
*cache
)
473 struct backref_node
*node
;
476 if (cache
->last_trans
== 0) {
477 cache
->last_trans
= trans
->transid
;
481 if (cache
->last_trans
== trans
->transid
)
485 * detached nodes are used to avoid unnecessary backref
486 * lookup. transaction commit changes the extent tree.
487 * so the detached nodes are no longer useful.
489 while (!list_empty(&cache
->detached
)) {
490 node
= list_entry(cache
->detached
.next
,
491 struct backref_node
, list
);
492 remove_backref_node(cache
, node
);
495 while (!list_empty(&cache
->changed
)) {
496 node
= list_entry(cache
->changed
.next
,
497 struct backref_node
, list
);
498 list_del_init(&node
->list
);
499 BUG_ON(node
->pending
);
500 update_backref_node(cache
, node
, node
->new_bytenr
);
504 * some nodes can be left in the pending list if there were
505 * errors during processing the pending nodes.
507 for (level
= 0; level
< BTRFS_MAX_LEVEL
; level
++) {
508 list_for_each_entry(node
, &cache
->pending
[level
], list
) {
509 BUG_ON(!node
->pending
);
510 if (node
->bytenr
== node
->new_bytenr
)
512 update_backref_node(cache
, node
, node
->new_bytenr
);
516 cache
->last_trans
= 0;
520 static bool reloc_root_is_dead(struct btrfs_root
*root
)
523 * Pair with set_bit/clear_bit in clean_dirty_subvols and
524 * btrfs_update_reloc_root. We need to see the updated bit before
525 * trying to access reloc_root
528 if (test_bit(BTRFS_ROOT_DEAD_RELOC_TREE
, &root
->state
))
534 * Check if this subvolume tree has valid reloc tree.
536 * Reloc tree after swap is considered dead, thus not considered as valid.
537 * This is enough for most callers, as they don't distinguish dead reloc root
538 * from no reloc root. But should_ignore_root() below is a special case.
540 static bool have_reloc_root(struct btrfs_root
*root
)
542 if (reloc_root_is_dead(root
))
544 if (!root
->reloc_root
)
549 static int should_ignore_root(struct btrfs_root
*root
)
551 struct btrfs_root
*reloc_root
;
553 if (!test_bit(BTRFS_ROOT_REF_COWS
, &root
->state
))
556 /* This root has been merged with its reloc tree, we can ignore it */
557 if (reloc_root_is_dead(root
))
560 reloc_root
= root
->reloc_root
;
564 if (btrfs_root_last_snapshot(&reloc_root
->root_item
) ==
565 root
->fs_info
->running_transaction
->transid
- 1)
568 * if there is reloc tree and it was created in previous
569 * transaction backref lookup can find the reloc tree,
570 * so backref node for the fs tree root is useless for
576 * find reloc tree by address of tree root
578 static struct btrfs_root
*find_reloc_root(struct reloc_control
*rc
,
581 struct rb_node
*rb_node
;
582 struct mapping_node
*node
;
583 struct btrfs_root
*root
= NULL
;
585 spin_lock(&rc
->reloc_root_tree
.lock
);
586 rb_node
= tree_search(&rc
->reloc_root_tree
.rb_root
, bytenr
);
588 node
= rb_entry(rb_node
, struct mapping_node
, rb_node
);
589 root
= (struct btrfs_root
*)node
->data
;
591 spin_unlock(&rc
->reloc_root_tree
.lock
);
595 static int is_cowonly_root(u64 root_objectid
)
597 if (root_objectid
== BTRFS_ROOT_TREE_OBJECTID
||
598 root_objectid
== BTRFS_EXTENT_TREE_OBJECTID
||
599 root_objectid
== BTRFS_CHUNK_TREE_OBJECTID
||
600 root_objectid
== BTRFS_DEV_TREE_OBJECTID
||
601 root_objectid
== BTRFS_TREE_LOG_OBJECTID
||
602 root_objectid
== BTRFS_CSUM_TREE_OBJECTID
||
603 root_objectid
== BTRFS_UUID_TREE_OBJECTID
||
604 root_objectid
== BTRFS_QUOTA_TREE_OBJECTID
||
605 root_objectid
== BTRFS_FREE_SPACE_TREE_OBJECTID
)
610 static struct btrfs_root
*read_fs_root(struct btrfs_fs_info
*fs_info
,
613 struct btrfs_key key
;
615 key
.objectid
= root_objectid
;
616 key
.type
= BTRFS_ROOT_ITEM_KEY
;
617 if (is_cowonly_root(root_objectid
))
620 key
.offset
= (u64
)-1;
622 return btrfs_get_fs_root(fs_info
, &key
, false);
625 static noinline_for_stack
626 int find_inline_backref(struct extent_buffer
*leaf
, int slot
,
627 unsigned long *ptr
, unsigned long *end
)
629 struct btrfs_key key
;
630 struct btrfs_extent_item
*ei
;
631 struct btrfs_tree_block_info
*bi
;
634 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
636 item_size
= btrfs_item_size_nr(leaf
, slot
);
637 if (item_size
< sizeof(*ei
)) {
638 btrfs_print_v0_err(leaf
->fs_info
);
639 btrfs_handle_fs_error(leaf
->fs_info
, -EINVAL
, NULL
);
642 ei
= btrfs_item_ptr(leaf
, slot
, struct btrfs_extent_item
);
643 WARN_ON(!(btrfs_extent_flags(leaf
, ei
) &
644 BTRFS_EXTENT_FLAG_TREE_BLOCK
));
646 if (key
.type
== BTRFS_EXTENT_ITEM_KEY
&&
647 item_size
<= sizeof(*ei
) + sizeof(*bi
)) {
648 WARN_ON(item_size
< sizeof(*ei
) + sizeof(*bi
));
651 if (key
.type
== BTRFS_METADATA_ITEM_KEY
&&
652 item_size
<= sizeof(*ei
)) {
653 WARN_ON(item_size
< sizeof(*ei
));
657 if (key
.type
== BTRFS_EXTENT_ITEM_KEY
) {
658 bi
= (struct btrfs_tree_block_info
*)(ei
+ 1);
659 *ptr
= (unsigned long)(bi
+ 1);
661 *ptr
= (unsigned long)(ei
+ 1);
663 *end
= (unsigned long)ei
+ item_size
;
668 * build backref tree for a given tree block. root of the backref tree
669 * corresponds the tree block, leaves of the backref tree correspond
670 * roots of b-trees that reference the tree block.
672 * the basic idea of this function is check backrefs of a given block
673 * to find upper level blocks that reference the block, and then check
674 * backrefs of these upper level blocks recursively. the recursion stop
675 * when tree root is reached or backrefs for the block is cached.
677 * NOTE: if we find backrefs for a block are cached, we know backrefs
678 * for all upper level blocks that directly/indirectly reference the
679 * block are also cached.
681 static noinline_for_stack
682 struct backref_node
*build_backref_tree(struct reloc_control
*rc
,
683 struct btrfs_key
*node_key
,
684 int level
, u64 bytenr
)
686 struct backref_cache
*cache
= &rc
->backref_cache
;
687 struct btrfs_path
*path1
; /* For searching extent root */
688 struct btrfs_path
*path2
; /* For searching parent of TREE_BLOCK_REF */
689 struct extent_buffer
*eb
;
690 struct btrfs_root
*root
;
691 struct backref_node
*cur
;
692 struct backref_node
*upper
;
693 struct backref_node
*lower
;
694 struct backref_node
*node
= NULL
;
695 struct backref_node
*exist
= NULL
;
696 struct backref_edge
*edge
;
697 struct rb_node
*rb_node
;
698 struct btrfs_key key
;
701 LIST_HEAD(list
); /* Pending edge list, upper node needs to be checked */
706 bool need_check
= true;
708 path1
= btrfs_alloc_path();
709 path2
= btrfs_alloc_path();
710 if (!path1
|| !path2
) {
714 path1
->reada
= READA_FORWARD
;
715 path2
->reada
= READA_FORWARD
;
717 node
= alloc_backref_node(cache
);
723 node
->bytenr
= bytenr
;
730 key
.objectid
= cur
->bytenr
;
731 key
.type
= BTRFS_METADATA_ITEM_KEY
;
732 key
.offset
= (u64
)-1;
734 path1
->search_commit_root
= 1;
735 path1
->skip_locking
= 1;
736 ret
= btrfs_search_slot(NULL
, rc
->extent_root
, &key
, path1
,
743 ASSERT(path1
->slots
[0]);
747 WARN_ON(cur
->checked
);
748 if (!list_empty(&cur
->upper
)) {
750 * the backref was added previously when processing
751 * backref of type BTRFS_TREE_BLOCK_REF_KEY
753 ASSERT(list_is_singular(&cur
->upper
));
754 edge
= list_entry(cur
->upper
.next
, struct backref_edge
,
756 ASSERT(list_empty(&edge
->list
[UPPER
]));
757 exist
= edge
->node
[UPPER
];
759 * add the upper level block to pending list if we need
763 list_add_tail(&edge
->list
[UPPER
], &list
);
770 eb
= path1
->nodes
[0];
773 if (path1
->slots
[0] >= btrfs_header_nritems(eb
)) {
774 ret
= btrfs_next_leaf(rc
->extent_root
, path1
);
781 eb
= path1
->nodes
[0];
784 btrfs_item_key_to_cpu(eb
, &key
, path1
->slots
[0]);
785 if (key
.objectid
!= cur
->bytenr
) {
790 if (key
.type
== BTRFS_EXTENT_ITEM_KEY
||
791 key
.type
== BTRFS_METADATA_ITEM_KEY
) {
792 ret
= find_inline_backref(eb
, path1
->slots
[0],
800 /* update key for inline back ref */
801 struct btrfs_extent_inline_ref
*iref
;
803 iref
= (struct btrfs_extent_inline_ref
*)ptr
;
804 type
= btrfs_get_extent_inline_ref_type(eb
, iref
,
805 BTRFS_REF_TYPE_BLOCK
);
806 if (type
== BTRFS_REF_TYPE_INVALID
) {
811 key
.offset
= btrfs_extent_inline_ref_offset(eb
, iref
);
813 WARN_ON(key
.type
!= BTRFS_TREE_BLOCK_REF_KEY
&&
814 key
.type
!= BTRFS_SHARED_BLOCK_REF_KEY
);
818 * Parent node found and matches current inline ref, no need to
819 * rebuild this node for this inline ref.
822 ((key
.type
== BTRFS_TREE_BLOCK_REF_KEY
&&
823 exist
->owner
== key
.offset
) ||
824 (key
.type
== BTRFS_SHARED_BLOCK_REF_KEY
&&
825 exist
->bytenr
== key
.offset
))) {
830 /* SHARED_BLOCK_REF means key.offset is the parent bytenr */
831 if (key
.type
== BTRFS_SHARED_BLOCK_REF_KEY
) {
832 if (key
.objectid
== key
.offset
) {
834 * Only root blocks of reloc trees use backref
835 * pointing to itself.
837 root
= find_reloc_root(rc
, cur
->bytenr
);
843 edge
= alloc_backref_edge(cache
);
848 rb_node
= tree_search(&cache
->rb_root
, key
.offset
);
850 upper
= alloc_backref_node(cache
);
852 free_backref_edge(cache
, edge
);
856 upper
->bytenr
= key
.offset
;
857 upper
->level
= cur
->level
+ 1;
859 * backrefs for the upper level block isn't
860 * cached, add the block to pending list
862 list_add_tail(&edge
->list
[UPPER
], &list
);
864 upper
= rb_entry(rb_node
, struct backref_node
,
866 ASSERT(upper
->checked
);
867 INIT_LIST_HEAD(&edge
->list
[UPPER
]);
869 list_add_tail(&edge
->list
[LOWER
], &cur
->upper
);
870 edge
->node
[LOWER
] = cur
;
871 edge
->node
[UPPER
] = upper
;
874 } else if (unlikely(key
.type
== BTRFS_EXTENT_REF_V0_KEY
)) {
876 btrfs_print_v0_err(rc
->extent_root
->fs_info
);
877 btrfs_handle_fs_error(rc
->extent_root
->fs_info
, err
,
880 } else if (key
.type
!= BTRFS_TREE_BLOCK_REF_KEY
) {
885 * key.type == BTRFS_TREE_BLOCK_REF_KEY, inline ref offset
886 * means the root objectid. We need to search the tree to get
889 root
= read_fs_root(rc
->extent_root
->fs_info
, key
.offset
);
895 if (!test_bit(BTRFS_ROOT_REF_COWS
, &root
->state
))
898 if (btrfs_root_level(&root
->root_item
) == cur
->level
) {
900 ASSERT(btrfs_root_bytenr(&root
->root_item
) ==
902 if (should_ignore_root(root
))
903 list_add(&cur
->list
, &useless
);
909 level
= cur
->level
+ 1;
911 /* Search the tree to find parent blocks referring the block. */
912 path2
->search_commit_root
= 1;
913 path2
->skip_locking
= 1;
914 path2
->lowest_level
= level
;
915 ret
= btrfs_search_slot(NULL
, root
, node_key
, path2
, 0, 0);
916 path2
->lowest_level
= 0;
921 if (ret
> 0 && path2
->slots
[level
] > 0)
922 path2
->slots
[level
]--;
924 eb
= path2
->nodes
[level
];
925 if (btrfs_node_blockptr(eb
, path2
->slots
[level
]) !=
927 btrfs_err(root
->fs_info
,
928 "couldn't find block (%llu) (level %d) in tree (%llu) with key (%llu %u %llu)",
929 cur
->bytenr
, level
- 1,
930 root
->root_key
.objectid
,
931 node_key
->objectid
, node_key
->type
,
939 /* Add all nodes and edges in the path */
940 for (; level
< BTRFS_MAX_LEVEL
; level
++) {
941 if (!path2
->nodes
[level
]) {
942 ASSERT(btrfs_root_bytenr(&root
->root_item
) ==
944 if (should_ignore_root(root
))
945 list_add(&lower
->list
, &useless
);
951 edge
= alloc_backref_edge(cache
);
957 eb
= path2
->nodes
[level
];
958 rb_node
= tree_search(&cache
->rb_root
, eb
->start
);
960 upper
= alloc_backref_node(cache
);
962 free_backref_edge(cache
, edge
);
966 upper
->bytenr
= eb
->start
;
967 upper
->owner
= btrfs_header_owner(eb
);
968 upper
->level
= lower
->level
+ 1;
969 if (!test_bit(BTRFS_ROOT_REF_COWS
,
974 * if we know the block isn't shared
975 * we can void checking its backrefs.
977 if (btrfs_block_can_be_shared(root
, eb
))
983 * add the block to pending list if we
984 * need check its backrefs, we only do this once
985 * while walking up a tree as we will catch
986 * anything else later on.
988 if (!upper
->checked
&& need_check
) {
990 list_add_tail(&edge
->list
[UPPER
],
995 INIT_LIST_HEAD(&edge
->list
[UPPER
]);
998 upper
= rb_entry(rb_node
, struct backref_node
,
1000 ASSERT(upper
->checked
);
1001 INIT_LIST_HEAD(&edge
->list
[UPPER
]);
1003 upper
->owner
= btrfs_header_owner(eb
);
1005 list_add_tail(&edge
->list
[LOWER
], &lower
->upper
);
1006 edge
->node
[LOWER
] = lower
;
1007 edge
->node
[UPPER
] = upper
;
1014 btrfs_release_path(path2
);
1017 ptr
+= btrfs_extent_inline_ref_size(key
.type
);
1027 btrfs_release_path(path1
);
1032 /* the pending list isn't empty, take the first block to process */
1033 if (!list_empty(&list
)) {
1034 edge
= list_entry(list
.next
, struct backref_edge
, list
[UPPER
]);
1035 list_del_init(&edge
->list
[UPPER
]);
1036 cur
= edge
->node
[UPPER
];
1041 * everything goes well, connect backref nodes and insert backref nodes
1044 ASSERT(node
->checked
);
1045 cowonly
= node
->cowonly
;
1047 rb_node
= tree_insert(&cache
->rb_root
, node
->bytenr
,
1050 backref_tree_panic(rb_node
, -EEXIST
, node
->bytenr
);
1051 list_add_tail(&node
->lower
, &cache
->leaves
);
1054 list_for_each_entry(edge
, &node
->upper
, list
[LOWER
])
1055 list_add_tail(&edge
->list
[UPPER
], &list
);
1057 while (!list_empty(&list
)) {
1058 edge
= list_entry(list
.next
, struct backref_edge
, list
[UPPER
]);
1059 list_del_init(&edge
->list
[UPPER
]);
1060 upper
= edge
->node
[UPPER
];
1061 if (upper
->detached
) {
1062 list_del(&edge
->list
[LOWER
]);
1063 lower
= edge
->node
[LOWER
];
1064 free_backref_edge(cache
, edge
);
1065 if (list_empty(&lower
->upper
))
1066 list_add(&lower
->list
, &useless
);
1070 if (!RB_EMPTY_NODE(&upper
->rb_node
)) {
1071 if (upper
->lowest
) {
1072 list_del_init(&upper
->lower
);
1076 list_add_tail(&edge
->list
[UPPER
], &upper
->lower
);
1080 if (!upper
->checked
) {
1082 * Still want to blow up for developers since this is a
1089 if (cowonly
!= upper
->cowonly
) {
1096 rb_node
= tree_insert(&cache
->rb_root
, upper
->bytenr
,
1099 backref_tree_panic(rb_node
, -EEXIST
,
1103 list_add_tail(&edge
->list
[UPPER
], &upper
->lower
);
1105 list_for_each_entry(edge
, &upper
->upper
, list
[LOWER
])
1106 list_add_tail(&edge
->list
[UPPER
], &list
);
1109 * process useless backref nodes. backref nodes for tree leaves
1110 * are deleted from the cache. backref nodes for upper level
1111 * tree blocks are left in the cache to avoid unnecessary backref
1114 while (!list_empty(&useless
)) {
1115 upper
= list_entry(useless
.next
, struct backref_node
, list
);
1116 list_del_init(&upper
->list
);
1117 ASSERT(list_empty(&upper
->upper
));
1120 if (upper
->lowest
) {
1121 list_del_init(&upper
->lower
);
1124 while (!list_empty(&upper
->lower
)) {
1125 edge
= list_entry(upper
->lower
.next
,
1126 struct backref_edge
, list
[UPPER
]);
1127 list_del(&edge
->list
[UPPER
]);
1128 list_del(&edge
->list
[LOWER
]);
1129 lower
= edge
->node
[LOWER
];
1130 free_backref_edge(cache
, edge
);
1132 if (list_empty(&lower
->upper
))
1133 list_add(&lower
->list
, &useless
);
1135 __mark_block_processed(rc
, upper
);
1136 if (upper
->level
> 0) {
1137 list_add(&upper
->list
, &cache
->detached
);
1138 upper
->detached
= 1;
1140 rb_erase(&upper
->rb_node
, &cache
->rb_root
);
1141 free_backref_node(cache
, upper
);
1145 btrfs_free_path(path1
);
1146 btrfs_free_path(path2
);
1148 while (!list_empty(&useless
)) {
1149 lower
= list_entry(useless
.next
,
1150 struct backref_node
, list
);
1151 list_del_init(&lower
->list
);
1153 while (!list_empty(&list
)) {
1154 edge
= list_first_entry(&list
, struct backref_edge
,
1156 list_del(&edge
->list
[UPPER
]);
1157 list_del(&edge
->list
[LOWER
]);
1158 lower
= edge
->node
[LOWER
];
1159 upper
= edge
->node
[UPPER
];
1160 free_backref_edge(cache
, edge
);
1163 * Lower is no longer linked to any upper backref nodes
1164 * and isn't in the cache, we can free it ourselves.
1166 if (list_empty(&lower
->upper
) &&
1167 RB_EMPTY_NODE(&lower
->rb_node
))
1168 list_add(&lower
->list
, &useless
);
1170 if (!RB_EMPTY_NODE(&upper
->rb_node
))
1173 /* Add this guy's upper edges to the list to process */
1174 list_for_each_entry(edge
, &upper
->upper
, list
[LOWER
])
1175 list_add_tail(&edge
->list
[UPPER
], &list
);
1176 if (list_empty(&upper
->upper
))
1177 list_add(&upper
->list
, &useless
);
1180 while (!list_empty(&useless
)) {
1181 lower
= list_entry(useless
.next
,
1182 struct backref_node
, list
);
1183 list_del_init(&lower
->list
);
1186 free_backref_node(cache
, lower
);
1189 free_backref_node(cache
, node
);
1190 return ERR_PTR(err
);
1192 ASSERT(!node
|| !node
->detached
);
1197 * helper to add backref node for the newly created snapshot.
1198 * the backref node is created by cloning backref node that
1199 * corresponds to root of source tree
1201 static int clone_backref_node(struct btrfs_trans_handle
*trans
,
1202 struct reloc_control
*rc
,
1203 struct btrfs_root
*src
,
1204 struct btrfs_root
*dest
)
1206 struct btrfs_root
*reloc_root
= src
->reloc_root
;
1207 struct backref_cache
*cache
= &rc
->backref_cache
;
1208 struct backref_node
*node
= NULL
;
1209 struct backref_node
*new_node
;
1210 struct backref_edge
*edge
;
1211 struct backref_edge
*new_edge
;
1212 struct rb_node
*rb_node
;
1214 if (cache
->last_trans
> 0)
1215 update_backref_cache(trans
, cache
);
1217 rb_node
= tree_search(&cache
->rb_root
, src
->commit_root
->start
);
1219 node
= rb_entry(rb_node
, struct backref_node
, rb_node
);
1223 BUG_ON(node
->new_bytenr
!= reloc_root
->node
->start
);
1227 rb_node
= tree_search(&cache
->rb_root
,
1228 reloc_root
->commit_root
->start
);
1230 node
= rb_entry(rb_node
, struct backref_node
,
1232 BUG_ON(node
->detached
);
1239 new_node
= alloc_backref_node(cache
);
1243 new_node
->bytenr
= dest
->node
->start
;
1244 new_node
->level
= node
->level
;
1245 new_node
->lowest
= node
->lowest
;
1246 new_node
->checked
= 1;
1247 new_node
->root
= dest
;
1249 if (!node
->lowest
) {
1250 list_for_each_entry(edge
, &node
->lower
, list
[UPPER
]) {
1251 new_edge
= alloc_backref_edge(cache
);
1255 new_edge
->node
[UPPER
] = new_node
;
1256 new_edge
->node
[LOWER
] = edge
->node
[LOWER
];
1257 list_add_tail(&new_edge
->list
[UPPER
],
1261 list_add_tail(&new_node
->lower
, &cache
->leaves
);
1264 rb_node
= tree_insert(&cache
->rb_root
, new_node
->bytenr
,
1265 &new_node
->rb_node
);
1267 backref_tree_panic(rb_node
, -EEXIST
, new_node
->bytenr
);
1269 if (!new_node
->lowest
) {
1270 list_for_each_entry(new_edge
, &new_node
->lower
, list
[UPPER
]) {
1271 list_add_tail(&new_edge
->list
[LOWER
],
1272 &new_edge
->node
[LOWER
]->upper
);
1277 while (!list_empty(&new_node
->lower
)) {
1278 new_edge
= list_entry(new_node
->lower
.next
,
1279 struct backref_edge
, list
[UPPER
]);
1280 list_del(&new_edge
->list
[UPPER
]);
1281 free_backref_edge(cache
, new_edge
);
1283 free_backref_node(cache
, new_node
);
1288 * helper to add 'address of tree root -> reloc tree' mapping
1290 static int __must_check
__add_reloc_root(struct btrfs_root
*root
)
1292 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
1293 struct rb_node
*rb_node
;
1294 struct mapping_node
*node
;
1295 struct reloc_control
*rc
= fs_info
->reloc_ctl
;
1297 node
= kmalloc(sizeof(*node
), GFP_NOFS
);
1301 node
->bytenr
= root
->node
->start
;
1304 spin_lock(&rc
->reloc_root_tree
.lock
);
1305 rb_node
= tree_insert(&rc
->reloc_root_tree
.rb_root
,
1306 node
->bytenr
, &node
->rb_node
);
1307 spin_unlock(&rc
->reloc_root_tree
.lock
);
1309 btrfs_panic(fs_info
, -EEXIST
,
1310 "Duplicate root found for start=%llu while inserting into relocation tree",
1314 list_add_tail(&root
->root_list
, &rc
->reloc_roots
);
1319 * helper to delete the 'address of tree root -> reloc tree'
1322 static void __del_reloc_root(struct btrfs_root
*root
)
1324 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
1325 struct rb_node
*rb_node
;
1326 struct mapping_node
*node
= NULL
;
1327 struct reloc_control
*rc
= fs_info
->reloc_ctl
;
1329 if (rc
&& root
->node
) {
1330 spin_lock(&rc
->reloc_root_tree
.lock
);
1331 rb_node
= tree_search(&rc
->reloc_root_tree
.rb_root
,
1334 node
= rb_entry(rb_node
, struct mapping_node
, rb_node
);
1335 rb_erase(&node
->rb_node
, &rc
->reloc_root_tree
.rb_root
);
1337 spin_unlock(&rc
->reloc_root_tree
.lock
);
1340 BUG_ON((struct btrfs_root
*)node
->data
!= root
);
1343 spin_lock(&fs_info
->trans_lock
);
1344 list_del_init(&root
->root_list
);
1345 spin_unlock(&fs_info
->trans_lock
);
1350 * helper to update the 'address of tree root -> reloc tree'
1353 static int __update_reloc_root(struct btrfs_root
*root
, u64 new_bytenr
)
1355 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
1356 struct rb_node
*rb_node
;
1357 struct mapping_node
*node
= NULL
;
1358 struct reloc_control
*rc
= fs_info
->reloc_ctl
;
1360 spin_lock(&rc
->reloc_root_tree
.lock
);
1361 rb_node
= tree_search(&rc
->reloc_root_tree
.rb_root
,
1364 node
= rb_entry(rb_node
, struct mapping_node
, rb_node
);
1365 rb_erase(&node
->rb_node
, &rc
->reloc_root_tree
.rb_root
);
1367 spin_unlock(&rc
->reloc_root_tree
.lock
);
1371 BUG_ON((struct btrfs_root
*)node
->data
!= root
);
1373 spin_lock(&rc
->reloc_root_tree
.lock
);
1374 node
->bytenr
= new_bytenr
;
1375 rb_node
= tree_insert(&rc
->reloc_root_tree
.rb_root
,
1376 node
->bytenr
, &node
->rb_node
);
1377 spin_unlock(&rc
->reloc_root_tree
.lock
);
1379 backref_tree_panic(rb_node
, -EEXIST
, node
->bytenr
);
1383 static struct btrfs_root
*create_reloc_root(struct btrfs_trans_handle
*trans
,
1384 struct btrfs_root
*root
, u64 objectid
)
1386 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
1387 struct btrfs_root
*reloc_root
;
1388 struct extent_buffer
*eb
;
1389 struct btrfs_root_item
*root_item
;
1390 struct btrfs_key root_key
;
1393 root_item
= kmalloc(sizeof(*root_item
), GFP_NOFS
);
1396 root_key
.objectid
= BTRFS_TREE_RELOC_OBJECTID
;
1397 root_key
.type
= BTRFS_ROOT_ITEM_KEY
;
1398 root_key
.offset
= objectid
;
1400 if (root
->root_key
.objectid
== objectid
) {
1401 u64 commit_root_gen
;
1403 /* called by btrfs_init_reloc_root */
1404 ret
= btrfs_copy_root(trans
, root
, root
->commit_root
, &eb
,
1405 BTRFS_TREE_RELOC_OBJECTID
);
1408 * Set the last_snapshot field to the generation of the commit
1409 * root - like this ctree.c:btrfs_block_can_be_shared() behaves
1410 * correctly (returns true) when the relocation root is created
1411 * either inside the critical section of a transaction commit
1412 * (through transaction.c:qgroup_account_snapshot()) and when
1413 * it's created before the transaction commit is started.
1415 commit_root_gen
= btrfs_header_generation(root
->commit_root
);
1416 btrfs_set_root_last_snapshot(&root
->root_item
, commit_root_gen
);
1419 * called by btrfs_reloc_post_snapshot_hook.
1420 * the source tree is a reloc tree, all tree blocks
1421 * modified after it was created have RELOC flag
1422 * set in their headers. so it's OK to not update
1423 * the 'last_snapshot'.
1425 ret
= btrfs_copy_root(trans
, root
, root
->node
, &eb
,
1426 BTRFS_TREE_RELOC_OBJECTID
);
1430 memcpy(root_item
, &root
->root_item
, sizeof(*root_item
));
1431 btrfs_set_root_bytenr(root_item
, eb
->start
);
1432 btrfs_set_root_level(root_item
, btrfs_header_level(eb
));
1433 btrfs_set_root_generation(root_item
, trans
->transid
);
1435 if (root
->root_key
.objectid
== objectid
) {
1436 btrfs_set_root_refs(root_item
, 0);
1437 memset(&root_item
->drop_progress
, 0,
1438 sizeof(struct btrfs_disk_key
));
1439 root_item
->drop_level
= 0;
1442 btrfs_tree_unlock(eb
);
1443 free_extent_buffer(eb
);
1445 ret
= btrfs_insert_root(trans
, fs_info
->tree_root
,
1446 &root_key
, root_item
);
1450 reloc_root
= btrfs_read_fs_root(fs_info
->tree_root
, &root_key
);
1451 BUG_ON(IS_ERR(reloc_root
));
1452 reloc_root
->last_trans
= trans
->transid
;
1457 * create reloc tree for a given fs tree. reloc tree is just a
1458 * snapshot of the fs tree with special root objectid.
1460 int btrfs_init_reloc_root(struct btrfs_trans_handle
*trans
,
1461 struct btrfs_root
*root
)
1463 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
1464 struct btrfs_root
*reloc_root
;
1465 struct reloc_control
*rc
= fs_info
->reloc_ctl
;
1466 struct btrfs_block_rsv
*rsv
;
1471 * The subvolume has reloc tree but the swap is finished, no need to
1472 * create/update the dead reloc tree
1474 if (reloc_root_is_dead(root
))
1477 if (root
->reloc_root
) {
1478 reloc_root
= root
->reloc_root
;
1479 reloc_root
->last_trans
= trans
->transid
;
1483 if (!rc
|| !rc
->create_reloc_tree
||
1484 root
->root_key
.objectid
== BTRFS_TREE_RELOC_OBJECTID
)
1487 if (!trans
->reloc_reserved
) {
1488 rsv
= trans
->block_rsv
;
1489 trans
->block_rsv
= rc
->block_rsv
;
1492 reloc_root
= create_reloc_root(trans
, root
, root
->root_key
.objectid
);
1494 trans
->block_rsv
= rsv
;
1496 ret
= __add_reloc_root(reloc_root
);
1498 root
->reloc_root
= reloc_root
;
1503 * update root item of reloc tree
1505 int btrfs_update_reloc_root(struct btrfs_trans_handle
*trans
,
1506 struct btrfs_root
*root
)
1508 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
1509 struct btrfs_root
*reloc_root
;
1510 struct btrfs_root_item
*root_item
;
1513 if (!have_reloc_root(root
))
1516 reloc_root
= root
->reloc_root
;
1517 root_item
= &reloc_root
->root_item
;
1519 /* root->reloc_root will stay until current relocation finished */
1520 if (fs_info
->reloc_ctl
->merge_reloc_tree
&&
1521 btrfs_root_refs(root_item
) == 0) {
1522 set_bit(BTRFS_ROOT_DEAD_RELOC_TREE
, &root
->state
);
1524 * Mark the tree as dead before we change reloc_root so
1525 * have_reloc_root will not touch it from now on.
1528 __del_reloc_root(reloc_root
);
1531 if (reloc_root
->commit_root
!= reloc_root
->node
) {
1532 btrfs_set_root_node(root_item
, reloc_root
->node
);
1533 free_extent_buffer(reloc_root
->commit_root
);
1534 reloc_root
->commit_root
= btrfs_root_node(reloc_root
);
1537 ret
= btrfs_update_root(trans
, fs_info
->tree_root
,
1538 &reloc_root
->root_key
, root_item
);
1546 * helper to find first cached inode with inode number >= objectid
1549 static struct inode
*find_next_inode(struct btrfs_root
*root
, u64 objectid
)
1551 struct rb_node
*node
;
1552 struct rb_node
*prev
;
1553 struct btrfs_inode
*entry
;
1554 struct inode
*inode
;
1556 spin_lock(&root
->inode_lock
);
1558 node
= root
->inode_tree
.rb_node
;
1562 entry
= rb_entry(node
, struct btrfs_inode
, rb_node
);
1564 if (objectid
< btrfs_ino(entry
))
1565 node
= node
->rb_left
;
1566 else if (objectid
> btrfs_ino(entry
))
1567 node
= node
->rb_right
;
1573 entry
= rb_entry(prev
, struct btrfs_inode
, rb_node
);
1574 if (objectid
<= btrfs_ino(entry
)) {
1578 prev
= rb_next(prev
);
1582 entry
= rb_entry(node
, struct btrfs_inode
, rb_node
);
1583 inode
= igrab(&entry
->vfs_inode
);
1585 spin_unlock(&root
->inode_lock
);
1589 objectid
= btrfs_ino(entry
) + 1;
1590 if (cond_resched_lock(&root
->inode_lock
))
1593 node
= rb_next(node
);
1595 spin_unlock(&root
->inode_lock
);
1599 static int in_block_group(u64 bytenr
, struct btrfs_block_group
*block_group
)
1601 if (bytenr
>= block_group
->start
&&
1602 bytenr
< block_group
->start
+ block_group
->length
)
1608 * get new location of data
1610 static int get_new_location(struct inode
*reloc_inode
, u64
*new_bytenr
,
1611 u64 bytenr
, u64 num_bytes
)
1613 struct btrfs_root
*root
= BTRFS_I(reloc_inode
)->root
;
1614 struct btrfs_path
*path
;
1615 struct btrfs_file_extent_item
*fi
;
1616 struct extent_buffer
*leaf
;
1619 path
= btrfs_alloc_path();
1623 bytenr
-= BTRFS_I(reloc_inode
)->index_cnt
;
1624 ret
= btrfs_lookup_file_extent(NULL
, root
, path
,
1625 btrfs_ino(BTRFS_I(reloc_inode
)), bytenr
, 0);
1633 leaf
= path
->nodes
[0];
1634 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
1635 struct btrfs_file_extent_item
);
1637 BUG_ON(btrfs_file_extent_offset(leaf
, fi
) ||
1638 btrfs_file_extent_compression(leaf
, fi
) ||
1639 btrfs_file_extent_encryption(leaf
, fi
) ||
1640 btrfs_file_extent_other_encoding(leaf
, fi
));
1642 if (num_bytes
!= btrfs_file_extent_disk_num_bytes(leaf
, fi
)) {
1647 *new_bytenr
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
1650 btrfs_free_path(path
);
1655 * update file extent items in the tree leaf to point to
1656 * the new locations.
1658 static noinline_for_stack
1659 int replace_file_extents(struct btrfs_trans_handle
*trans
,
1660 struct reloc_control
*rc
,
1661 struct btrfs_root
*root
,
1662 struct extent_buffer
*leaf
)
1664 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
1665 struct btrfs_key key
;
1666 struct btrfs_file_extent_item
*fi
;
1667 struct inode
*inode
= NULL
;
1679 if (rc
->stage
!= UPDATE_DATA_PTRS
)
1682 /* reloc trees always use full backref */
1683 if (root
->root_key
.objectid
== BTRFS_TREE_RELOC_OBJECTID
)
1684 parent
= leaf
->start
;
1688 nritems
= btrfs_header_nritems(leaf
);
1689 for (i
= 0; i
< nritems
; i
++) {
1690 struct btrfs_ref ref
= { 0 };
1693 btrfs_item_key_to_cpu(leaf
, &key
, i
);
1694 if (key
.type
!= BTRFS_EXTENT_DATA_KEY
)
1696 fi
= btrfs_item_ptr(leaf
, i
, struct btrfs_file_extent_item
);
1697 if (btrfs_file_extent_type(leaf
, fi
) ==
1698 BTRFS_FILE_EXTENT_INLINE
)
1700 bytenr
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
1701 num_bytes
= btrfs_file_extent_disk_num_bytes(leaf
, fi
);
1704 if (!in_block_group(bytenr
, rc
->block_group
))
1708 * if we are modifying block in fs tree, wait for readpage
1709 * to complete and drop the extent cache
1711 if (root
->root_key
.objectid
!= BTRFS_TREE_RELOC_OBJECTID
) {
1713 inode
= find_next_inode(root
, key
.objectid
);
1715 } else if (inode
&& btrfs_ino(BTRFS_I(inode
)) < key
.objectid
) {
1716 btrfs_add_delayed_iput(inode
);
1717 inode
= find_next_inode(root
, key
.objectid
);
1719 if (inode
&& btrfs_ino(BTRFS_I(inode
)) == key
.objectid
) {
1721 btrfs_file_extent_num_bytes(leaf
, fi
);
1722 WARN_ON(!IS_ALIGNED(key
.offset
,
1723 fs_info
->sectorsize
));
1724 WARN_ON(!IS_ALIGNED(end
, fs_info
->sectorsize
));
1726 ret
= try_lock_extent(&BTRFS_I(inode
)->io_tree
,
1731 btrfs_drop_extent_cache(BTRFS_I(inode
),
1732 key
.offset
, end
, 1);
1733 unlock_extent(&BTRFS_I(inode
)->io_tree
,
1738 ret
= get_new_location(rc
->data_inode
, &new_bytenr
,
1742 * Don't have to abort since we've not changed anything
1743 * in the file extent yet.
1748 btrfs_set_file_extent_disk_bytenr(leaf
, fi
, new_bytenr
);
1751 key
.offset
-= btrfs_file_extent_offset(leaf
, fi
);
1752 btrfs_init_generic_ref(&ref
, BTRFS_ADD_DELAYED_REF
, new_bytenr
,
1754 ref
.real_root
= root
->root_key
.objectid
;
1755 btrfs_init_data_ref(&ref
, btrfs_header_owner(leaf
),
1756 key
.objectid
, key
.offset
);
1757 ret
= btrfs_inc_extent_ref(trans
, &ref
);
1759 btrfs_abort_transaction(trans
, ret
);
1763 btrfs_init_generic_ref(&ref
, BTRFS_DROP_DELAYED_REF
, bytenr
,
1765 ref
.real_root
= root
->root_key
.objectid
;
1766 btrfs_init_data_ref(&ref
, btrfs_header_owner(leaf
),
1767 key
.objectid
, key
.offset
);
1768 ret
= btrfs_free_extent(trans
, &ref
);
1770 btrfs_abort_transaction(trans
, ret
);
1775 btrfs_mark_buffer_dirty(leaf
);
1777 btrfs_add_delayed_iput(inode
);
1781 static noinline_for_stack
1782 int memcmp_node_keys(struct extent_buffer
*eb
, int slot
,
1783 struct btrfs_path
*path
, int level
)
1785 struct btrfs_disk_key key1
;
1786 struct btrfs_disk_key key2
;
1787 btrfs_node_key(eb
, &key1
, slot
);
1788 btrfs_node_key(path
->nodes
[level
], &key2
, path
->slots
[level
]);
1789 return memcmp(&key1
, &key2
, sizeof(key1
));
1793 * try to replace tree blocks in fs tree with the new blocks
1794 * in reloc tree. tree blocks haven't been modified since the
1795 * reloc tree was create can be replaced.
1797 * if a block was replaced, level of the block + 1 is returned.
1798 * if no block got replaced, 0 is returned. if there are other
1799 * errors, a negative error number is returned.
1801 static noinline_for_stack
1802 int replace_path(struct btrfs_trans_handle
*trans
, struct reloc_control
*rc
,
1803 struct btrfs_root
*dest
, struct btrfs_root
*src
,
1804 struct btrfs_path
*path
, struct btrfs_key
*next_key
,
1805 int lowest_level
, int max_level
)
1807 struct btrfs_fs_info
*fs_info
= dest
->fs_info
;
1808 struct extent_buffer
*eb
;
1809 struct extent_buffer
*parent
;
1810 struct btrfs_ref ref
= { 0 };
1811 struct btrfs_key key
;
1823 BUG_ON(src
->root_key
.objectid
!= BTRFS_TREE_RELOC_OBJECTID
);
1824 BUG_ON(dest
->root_key
.objectid
== BTRFS_TREE_RELOC_OBJECTID
);
1826 last_snapshot
= btrfs_root_last_snapshot(&src
->root_item
);
1828 slot
= path
->slots
[lowest_level
];
1829 btrfs_node_key_to_cpu(path
->nodes
[lowest_level
], &key
, slot
);
1831 eb
= btrfs_lock_root_node(dest
);
1832 btrfs_set_lock_blocking_write(eb
);
1833 level
= btrfs_header_level(eb
);
1835 if (level
< lowest_level
) {
1836 btrfs_tree_unlock(eb
);
1837 free_extent_buffer(eb
);
1842 ret
= btrfs_cow_block(trans
, dest
, eb
, NULL
, 0, &eb
);
1845 btrfs_set_lock_blocking_write(eb
);
1848 next_key
->objectid
= (u64
)-1;
1849 next_key
->type
= (u8
)-1;
1850 next_key
->offset
= (u64
)-1;
1855 struct btrfs_key first_key
;
1857 level
= btrfs_header_level(parent
);
1858 BUG_ON(level
< lowest_level
);
1860 ret
= btrfs_bin_search(parent
, &key
, level
, &slot
);
1863 if (ret
&& slot
> 0)
1866 if (next_key
&& slot
+ 1 < btrfs_header_nritems(parent
))
1867 btrfs_node_key_to_cpu(parent
, next_key
, slot
+ 1);
1869 old_bytenr
= btrfs_node_blockptr(parent
, slot
);
1870 blocksize
= fs_info
->nodesize
;
1871 old_ptr_gen
= btrfs_node_ptr_generation(parent
, slot
);
1872 btrfs_node_key_to_cpu(parent
, &first_key
, slot
);
1874 if (level
<= max_level
) {
1875 eb
= path
->nodes
[level
];
1876 new_bytenr
= btrfs_node_blockptr(eb
,
1877 path
->slots
[level
]);
1878 new_ptr_gen
= btrfs_node_ptr_generation(eb
,
1879 path
->slots
[level
]);
1885 if (WARN_ON(new_bytenr
> 0 && new_bytenr
== old_bytenr
)) {
1890 if (new_bytenr
== 0 || old_ptr_gen
> last_snapshot
||
1891 memcmp_node_keys(parent
, slot
, path
, level
)) {
1892 if (level
<= lowest_level
) {
1897 eb
= read_tree_block(fs_info
, old_bytenr
, old_ptr_gen
,
1898 level
- 1, &first_key
);
1902 } else if (!extent_buffer_uptodate(eb
)) {
1904 free_extent_buffer(eb
);
1907 btrfs_tree_lock(eb
);
1909 ret
= btrfs_cow_block(trans
, dest
, eb
, parent
,
1913 btrfs_set_lock_blocking_write(eb
);
1915 btrfs_tree_unlock(parent
);
1916 free_extent_buffer(parent
);
1923 btrfs_tree_unlock(parent
);
1924 free_extent_buffer(parent
);
1929 btrfs_node_key_to_cpu(path
->nodes
[level
], &key
,
1930 path
->slots
[level
]);
1931 btrfs_release_path(path
);
1933 path
->lowest_level
= level
;
1934 ret
= btrfs_search_slot(trans
, src
, &key
, path
, 0, 1);
1935 path
->lowest_level
= 0;
1939 * Info qgroup to trace both subtrees.
1941 * We must trace both trees.
1942 * 1) Tree reloc subtree
1943 * If not traced, we will leak data numbers
1945 * If not traced, we will double count old data
1947 * We don't scan the subtree right now, but only record
1948 * the swapped tree blocks.
1949 * The real subtree rescan is delayed until we have new
1950 * CoW on the subtree root node before transaction commit.
1952 ret
= btrfs_qgroup_add_swapped_blocks(trans
, dest
,
1953 rc
->block_group
, parent
, slot
,
1954 path
->nodes
[level
], path
->slots
[level
],
1959 * swap blocks in fs tree and reloc tree.
1961 btrfs_set_node_blockptr(parent
, slot
, new_bytenr
);
1962 btrfs_set_node_ptr_generation(parent
, slot
, new_ptr_gen
);
1963 btrfs_mark_buffer_dirty(parent
);
1965 btrfs_set_node_blockptr(path
->nodes
[level
],
1966 path
->slots
[level
], old_bytenr
);
1967 btrfs_set_node_ptr_generation(path
->nodes
[level
],
1968 path
->slots
[level
], old_ptr_gen
);
1969 btrfs_mark_buffer_dirty(path
->nodes
[level
]);
1971 btrfs_init_generic_ref(&ref
, BTRFS_ADD_DELAYED_REF
, old_bytenr
,
1972 blocksize
, path
->nodes
[level
]->start
);
1973 ref
.skip_qgroup
= true;
1974 btrfs_init_tree_ref(&ref
, level
- 1, src
->root_key
.objectid
);
1975 ret
= btrfs_inc_extent_ref(trans
, &ref
);
1977 btrfs_init_generic_ref(&ref
, BTRFS_ADD_DELAYED_REF
, new_bytenr
,
1979 ref
.skip_qgroup
= true;
1980 btrfs_init_tree_ref(&ref
, level
- 1, dest
->root_key
.objectid
);
1981 ret
= btrfs_inc_extent_ref(trans
, &ref
);
1984 btrfs_init_generic_ref(&ref
, BTRFS_DROP_DELAYED_REF
, new_bytenr
,
1985 blocksize
, path
->nodes
[level
]->start
);
1986 btrfs_init_tree_ref(&ref
, level
- 1, src
->root_key
.objectid
);
1987 ref
.skip_qgroup
= true;
1988 ret
= btrfs_free_extent(trans
, &ref
);
1991 btrfs_init_generic_ref(&ref
, BTRFS_DROP_DELAYED_REF
, old_bytenr
,
1993 btrfs_init_tree_ref(&ref
, level
- 1, dest
->root_key
.objectid
);
1994 ref
.skip_qgroup
= true;
1995 ret
= btrfs_free_extent(trans
, &ref
);
1998 btrfs_unlock_up_safe(path
, 0);
2003 btrfs_tree_unlock(parent
);
2004 free_extent_buffer(parent
);
2009 * helper to find next relocated block in reloc tree
2011 static noinline_for_stack
2012 int walk_up_reloc_tree(struct btrfs_root
*root
, struct btrfs_path
*path
,
2015 struct extent_buffer
*eb
;
2020 last_snapshot
= btrfs_root_last_snapshot(&root
->root_item
);
2022 for (i
= 0; i
< *level
; i
++) {
2023 free_extent_buffer(path
->nodes
[i
]);
2024 path
->nodes
[i
] = NULL
;
2027 for (i
= *level
; i
< BTRFS_MAX_LEVEL
&& path
->nodes
[i
]; i
++) {
2028 eb
= path
->nodes
[i
];
2029 nritems
= btrfs_header_nritems(eb
);
2030 while (path
->slots
[i
] + 1 < nritems
) {
2032 if (btrfs_node_ptr_generation(eb
, path
->slots
[i
]) <=
2039 free_extent_buffer(path
->nodes
[i
]);
2040 path
->nodes
[i
] = NULL
;
2046 * walk down reloc tree to find relocated block of lowest level
2048 static noinline_for_stack
2049 int walk_down_reloc_tree(struct btrfs_root
*root
, struct btrfs_path
*path
,
2052 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
2053 struct extent_buffer
*eb
= NULL
;
2060 last_snapshot
= btrfs_root_last_snapshot(&root
->root_item
);
2062 for (i
= *level
; i
> 0; i
--) {
2063 struct btrfs_key first_key
;
2065 eb
= path
->nodes
[i
];
2066 nritems
= btrfs_header_nritems(eb
);
2067 while (path
->slots
[i
] < nritems
) {
2068 ptr_gen
= btrfs_node_ptr_generation(eb
, path
->slots
[i
]);
2069 if (ptr_gen
> last_snapshot
)
2073 if (path
->slots
[i
] >= nritems
) {
2084 bytenr
= btrfs_node_blockptr(eb
, path
->slots
[i
]);
2085 btrfs_node_key_to_cpu(eb
, &first_key
, path
->slots
[i
]);
2086 eb
= read_tree_block(fs_info
, bytenr
, ptr_gen
, i
- 1,
2090 } else if (!extent_buffer_uptodate(eb
)) {
2091 free_extent_buffer(eb
);
2094 BUG_ON(btrfs_header_level(eb
) != i
- 1);
2095 path
->nodes
[i
- 1] = eb
;
2096 path
->slots
[i
- 1] = 0;
2102 * invalidate extent cache for file extents whose key in range of
2103 * [min_key, max_key)
2105 static int invalidate_extent_cache(struct btrfs_root
*root
,
2106 struct btrfs_key
*min_key
,
2107 struct btrfs_key
*max_key
)
2109 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
2110 struct inode
*inode
= NULL
;
2115 objectid
= min_key
->objectid
;
2120 if (objectid
> max_key
->objectid
)
2123 inode
= find_next_inode(root
, objectid
);
2126 ino
= btrfs_ino(BTRFS_I(inode
));
2128 if (ino
> max_key
->objectid
) {
2134 if (!S_ISREG(inode
->i_mode
))
2137 if (unlikely(min_key
->objectid
== ino
)) {
2138 if (min_key
->type
> BTRFS_EXTENT_DATA_KEY
)
2140 if (min_key
->type
< BTRFS_EXTENT_DATA_KEY
)
2143 start
= min_key
->offset
;
2144 WARN_ON(!IS_ALIGNED(start
, fs_info
->sectorsize
));
2150 if (unlikely(max_key
->objectid
== ino
)) {
2151 if (max_key
->type
< BTRFS_EXTENT_DATA_KEY
)
2153 if (max_key
->type
> BTRFS_EXTENT_DATA_KEY
) {
2156 if (max_key
->offset
== 0)
2158 end
= max_key
->offset
;
2159 WARN_ON(!IS_ALIGNED(end
, fs_info
->sectorsize
));
2166 /* the lock_extent waits for readpage to complete */
2167 lock_extent(&BTRFS_I(inode
)->io_tree
, start
, end
);
2168 btrfs_drop_extent_cache(BTRFS_I(inode
), start
, end
, 1);
2169 unlock_extent(&BTRFS_I(inode
)->io_tree
, start
, end
);
2174 static int find_next_key(struct btrfs_path
*path
, int level
,
2175 struct btrfs_key
*key
)
2178 while (level
< BTRFS_MAX_LEVEL
) {
2179 if (!path
->nodes
[level
])
2181 if (path
->slots
[level
] + 1 <
2182 btrfs_header_nritems(path
->nodes
[level
])) {
2183 btrfs_node_key_to_cpu(path
->nodes
[level
], key
,
2184 path
->slots
[level
] + 1);
2193 * Insert current subvolume into reloc_control::dirty_subvol_roots
2195 static void insert_dirty_subvol(struct btrfs_trans_handle
*trans
,
2196 struct reloc_control
*rc
,
2197 struct btrfs_root
*root
)
2199 struct btrfs_root
*reloc_root
= root
->reloc_root
;
2200 struct btrfs_root_item
*reloc_root_item
;
2202 /* @root must be a subvolume tree root with a valid reloc tree */
2203 ASSERT(root
->root_key
.objectid
!= BTRFS_TREE_RELOC_OBJECTID
);
2206 reloc_root_item
= &reloc_root
->root_item
;
2207 memset(&reloc_root_item
->drop_progress
, 0,
2208 sizeof(reloc_root_item
->drop_progress
));
2209 reloc_root_item
->drop_level
= 0;
2210 btrfs_set_root_refs(reloc_root_item
, 0);
2211 btrfs_update_reloc_root(trans
, root
);
2213 if (list_empty(&root
->reloc_dirty_list
)) {
2214 btrfs_grab_fs_root(root
);
2215 list_add_tail(&root
->reloc_dirty_list
, &rc
->dirty_subvol_roots
);
2219 static int clean_dirty_subvols(struct reloc_control
*rc
)
2221 struct btrfs_root
*root
;
2222 struct btrfs_root
*next
;
2226 list_for_each_entry_safe(root
, next
, &rc
->dirty_subvol_roots
,
2228 if (root
->root_key
.objectid
!= BTRFS_TREE_RELOC_OBJECTID
) {
2229 /* Merged subvolume, cleanup its reloc root */
2230 struct btrfs_root
*reloc_root
= root
->reloc_root
;
2232 list_del_init(&root
->reloc_dirty_list
);
2233 root
->reloc_root
= NULL
;
2236 ret2
= btrfs_drop_snapshot(reloc_root
, NULL
, 0, 1);
2237 if (ret2
< 0 && !ret
)
2241 * Need barrier to ensure clear_bit() only happens after
2242 * root->reloc_root = NULL. Pairs with have_reloc_root.
2245 clear_bit(BTRFS_ROOT_DEAD_RELOC_TREE
, &root
->state
);
2246 btrfs_put_fs_root(root
);
2248 /* Orphan reloc tree, just clean it up */
2249 ret2
= btrfs_drop_snapshot(root
, NULL
, 0, 1);
2250 if (ret2
< 0 && !ret
)
2258 * merge the relocated tree blocks in reloc tree with corresponding
2261 static noinline_for_stack
int merge_reloc_root(struct reloc_control
*rc
,
2262 struct btrfs_root
*root
)
2264 struct btrfs_fs_info
*fs_info
= rc
->extent_root
->fs_info
;
2265 struct btrfs_key key
;
2266 struct btrfs_key next_key
;
2267 struct btrfs_trans_handle
*trans
= NULL
;
2268 struct btrfs_root
*reloc_root
;
2269 struct btrfs_root_item
*root_item
;
2270 struct btrfs_path
*path
;
2271 struct extent_buffer
*leaf
;
2279 path
= btrfs_alloc_path();
2282 path
->reada
= READA_FORWARD
;
2284 reloc_root
= root
->reloc_root
;
2285 root_item
= &reloc_root
->root_item
;
2287 if (btrfs_disk_key_objectid(&root_item
->drop_progress
) == 0) {
2288 level
= btrfs_root_level(root_item
);
2289 atomic_inc(&reloc_root
->node
->refs
);
2290 path
->nodes
[level
] = reloc_root
->node
;
2291 path
->slots
[level
] = 0;
2293 btrfs_disk_key_to_cpu(&key
, &root_item
->drop_progress
);
2295 level
= root_item
->drop_level
;
2297 path
->lowest_level
= level
;
2298 ret
= btrfs_search_slot(NULL
, reloc_root
, &key
, path
, 0, 0);
2299 path
->lowest_level
= 0;
2301 btrfs_free_path(path
);
2305 btrfs_node_key_to_cpu(path
->nodes
[level
], &next_key
,
2306 path
->slots
[level
]);
2307 WARN_ON(memcmp(&key
, &next_key
, sizeof(key
)));
2309 btrfs_unlock_up_safe(path
, 0);
2312 min_reserved
= fs_info
->nodesize
* (BTRFS_MAX_LEVEL
- 1) * 2;
2313 memset(&next_key
, 0, sizeof(next_key
));
2316 ret
= btrfs_block_rsv_refill(root
, rc
->block_rsv
, min_reserved
,
2317 BTRFS_RESERVE_FLUSH_ALL
);
2322 trans
= btrfs_start_transaction(root
, 0);
2323 if (IS_ERR(trans
)) {
2324 err
= PTR_ERR(trans
);
2328 trans
->block_rsv
= rc
->block_rsv
;
2333 ret
= walk_down_reloc_tree(reloc_root
, path
, &level
);
2341 if (!find_next_key(path
, level
, &key
) &&
2342 btrfs_comp_cpu_keys(&next_key
, &key
) >= 0) {
2345 ret
= replace_path(trans
, rc
, root
, reloc_root
, path
,
2346 &next_key
, level
, max_level
);
2355 btrfs_node_key_to_cpu(path
->nodes
[level
], &key
,
2356 path
->slots
[level
]);
2360 ret
= walk_up_reloc_tree(reloc_root
, path
, &level
);
2366 * save the merging progress in the drop_progress.
2367 * this is OK since root refs == 1 in this case.
2369 btrfs_node_key(path
->nodes
[level
], &root_item
->drop_progress
,
2370 path
->slots
[level
]);
2371 root_item
->drop_level
= level
;
2373 btrfs_end_transaction_throttle(trans
);
2376 btrfs_btree_balance_dirty(fs_info
);
2378 if (replaced
&& rc
->stage
== UPDATE_DATA_PTRS
)
2379 invalidate_extent_cache(root
, &key
, &next_key
);
2383 * handle the case only one block in the fs tree need to be
2384 * relocated and the block is tree root.
2386 leaf
= btrfs_lock_root_node(root
);
2387 ret
= btrfs_cow_block(trans
, root
, leaf
, NULL
, 0, &leaf
);
2388 btrfs_tree_unlock(leaf
);
2389 free_extent_buffer(leaf
);
2393 btrfs_free_path(path
);
2396 insert_dirty_subvol(trans
, rc
, root
);
2399 btrfs_end_transaction_throttle(trans
);
2401 btrfs_btree_balance_dirty(fs_info
);
2403 if (replaced
&& rc
->stage
== UPDATE_DATA_PTRS
)
2404 invalidate_extent_cache(root
, &key
, &next_key
);
2409 static noinline_for_stack
2410 int prepare_to_merge(struct reloc_control
*rc
, int err
)
2412 struct btrfs_root
*root
= rc
->extent_root
;
2413 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
2414 struct btrfs_root
*reloc_root
;
2415 struct btrfs_trans_handle
*trans
;
2416 LIST_HEAD(reloc_roots
);
2420 mutex_lock(&fs_info
->reloc_mutex
);
2421 rc
->merging_rsv_size
+= fs_info
->nodesize
* (BTRFS_MAX_LEVEL
- 1) * 2;
2422 rc
->merging_rsv_size
+= rc
->nodes_relocated
* 2;
2423 mutex_unlock(&fs_info
->reloc_mutex
);
2427 num_bytes
= rc
->merging_rsv_size
;
2428 ret
= btrfs_block_rsv_add(root
, rc
->block_rsv
, num_bytes
,
2429 BTRFS_RESERVE_FLUSH_ALL
);
2434 trans
= btrfs_join_transaction(rc
->extent_root
);
2435 if (IS_ERR(trans
)) {
2437 btrfs_block_rsv_release(fs_info
, rc
->block_rsv
,
2439 return PTR_ERR(trans
);
2443 if (num_bytes
!= rc
->merging_rsv_size
) {
2444 btrfs_end_transaction(trans
);
2445 btrfs_block_rsv_release(fs_info
, rc
->block_rsv
,
2451 rc
->merge_reloc_tree
= 1;
2453 while (!list_empty(&rc
->reloc_roots
)) {
2454 reloc_root
= list_entry(rc
->reloc_roots
.next
,
2455 struct btrfs_root
, root_list
);
2456 list_del_init(&reloc_root
->root_list
);
2458 root
= read_fs_root(fs_info
, reloc_root
->root_key
.offset
);
2459 BUG_ON(IS_ERR(root
));
2460 BUG_ON(root
->reloc_root
!= reloc_root
);
2463 * set reference count to 1, so btrfs_recover_relocation
2464 * knows it should resumes merging
2467 btrfs_set_root_refs(&reloc_root
->root_item
, 1);
2468 btrfs_update_reloc_root(trans
, root
);
2470 list_add(&reloc_root
->root_list
, &reloc_roots
);
2473 list_splice(&reloc_roots
, &rc
->reloc_roots
);
2476 btrfs_commit_transaction(trans
);
2478 btrfs_end_transaction(trans
);
2482 static noinline_for_stack
2483 void free_reloc_roots(struct list_head
*list
)
2485 struct btrfs_root
*reloc_root
;
2487 while (!list_empty(list
)) {
2488 reloc_root
= list_entry(list
->next
, struct btrfs_root
,
2490 __del_reloc_root(reloc_root
);
2491 free_extent_buffer(reloc_root
->node
);
2492 free_extent_buffer(reloc_root
->commit_root
);
2493 reloc_root
->node
= NULL
;
2494 reloc_root
->commit_root
= NULL
;
2498 static noinline_for_stack
2499 void merge_reloc_roots(struct reloc_control
*rc
)
2501 struct btrfs_fs_info
*fs_info
= rc
->extent_root
->fs_info
;
2502 struct btrfs_root
*root
;
2503 struct btrfs_root
*reloc_root
;
2504 LIST_HEAD(reloc_roots
);
2508 root
= rc
->extent_root
;
2511 * this serializes us with btrfs_record_root_in_transaction,
2512 * we have to make sure nobody is in the middle of
2513 * adding their roots to the list while we are
2516 mutex_lock(&fs_info
->reloc_mutex
);
2517 list_splice_init(&rc
->reloc_roots
, &reloc_roots
);
2518 mutex_unlock(&fs_info
->reloc_mutex
);
2520 while (!list_empty(&reloc_roots
)) {
2522 reloc_root
= list_entry(reloc_roots
.next
,
2523 struct btrfs_root
, root_list
);
2525 if (btrfs_root_refs(&reloc_root
->root_item
) > 0) {
2526 root
= read_fs_root(fs_info
,
2527 reloc_root
->root_key
.offset
);
2528 BUG_ON(IS_ERR(root
));
2529 BUG_ON(root
->reloc_root
!= reloc_root
);
2531 ret
= merge_reloc_root(rc
, root
);
2533 if (list_empty(&reloc_root
->root_list
))
2534 list_add_tail(&reloc_root
->root_list
,
2539 list_del_init(&reloc_root
->root_list
);
2540 /* Don't forget to queue this reloc root for cleanup */
2541 list_add_tail(&reloc_root
->reloc_dirty_list
,
2542 &rc
->dirty_subvol_roots
);
2552 btrfs_handle_fs_error(fs_info
, ret
, NULL
);
2553 if (!list_empty(&reloc_roots
))
2554 free_reloc_roots(&reloc_roots
);
2556 /* new reloc root may be added */
2557 mutex_lock(&fs_info
->reloc_mutex
);
2558 list_splice_init(&rc
->reloc_roots
, &reloc_roots
);
2559 mutex_unlock(&fs_info
->reloc_mutex
);
2560 if (!list_empty(&reloc_roots
))
2561 free_reloc_roots(&reloc_roots
);
2564 BUG_ON(!RB_EMPTY_ROOT(&rc
->reloc_root_tree
.rb_root
));
2567 static void free_block_list(struct rb_root
*blocks
)
2569 struct tree_block
*block
;
2570 struct rb_node
*rb_node
;
2571 while ((rb_node
= rb_first(blocks
))) {
2572 block
= rb_entry(rb_node
, struct tree_block
, rb_node
);
2573 rb_erase(rb_node
, blocks
);
2578 static int record_reloc_root_in_trans(struct btrfs_trans_handle
*trans
,
2579 struct btrfs_root
*reloc_root
)
2581 struct btrfs_fs_info
*fs_info
= reloc_root
->fs_info
;
2582 struct btrfs_root
*root
;
2584 if (reloc_root
->last_trans
== trans
->transid
)
2587 root
= read_fs_root(fs_info
, reloc_root
->root_key
.offset
);
2588 BUG_ON(IS_ERR(root
));
2589 BUG_ON(root
->reloc_root
!= reloc_root
);
2591 return btrfs_record_root_in_trans(trans
, root
);
2594 static noinline_for_stack
2595 struct btrfs_root
*select_reloc_root(struct btrfs_trans_handle
*trans
,
2596 struct reloc_control
*rc
,
2597 struct backref_node
*node
,
2598 struct backref_edge
*edges
[])
2600 struct backref_node
*next
;
2601 struct btrfs_root
*root
;
2607 next
= walk_up_backref(next
, edges
, &index
);
2610 BUG_ON(!test_bit(BTRFS_ROOT_REF_COWS
, &root
->state
));
2612 if (root
->root_key
.objectid
== BTRFS_TREE_RELOC_OBJECTID
) {
2613 record_reloc_root_in_trans(trans
, root
);
2617 btrfs_record_root_in_trans(trans
, root
);
2618 root
= root
->reloc_root
;
2620 if (next
->new_bytenr
!= root
->node
->start
) {
2621 BUG_ON(next
->new_bytenr
);
2622 BUG_ON(!list_empty(&next
->list
));
2623 next
->new_bytenr
= root
->node
->start
;
2625 list_add_tail(&next
->list
,
2626 &rc
->backref_cache
.changed
);
2627 __mark_block_processed(rc
, next
);
2633 next
= walk_down_backref(edges
, &index
);
2634 if (!next
|| next
->level
<= node
->level
)
2641 /* setup backref node path for btrfs_reloc_cow_block */
2643 rc
->backref_cache
.path
[next
->level
] = next
;
2646 next
= edges
[index
]->node
[UPPER
];
2652 * select a tree root for relocation. return NULL if the block
2653 * is reference counted. we should use do_relocation() in this
2654 * case. return a tree root pointer if the block isn't reference
2655 * counted. return -ENOENT if the block is root of reloc tree.
2657 static noinline_for_stack
2658 struct btrfs_root
*select_one_root(struct backref_node
*node
)
2660 struct backref_node
*next
;
2661 struct btrfs_root
*root
;
2662 struct btrfs_root
*fs_root
= NULL
;
2663 struct backref_edge
*edges
[BTRFS_MAX_LEVEL
- 1];
2669 next
= walk_up_backref(next
, edges
, &index
);
2673 /* no other choice for non-references counted tree */
2674 if (!test_bit(BTRFS_ROOT_REF_COWS
, &root
->state
))
2677 if (root
->root_key
.objectid
!= BTRFS_TREE_RELOC_OBJECTID
)
2683 next
= walk_down_backref(edges
, &index
);
2684 if (!next
|| next
->level
<= node
->level
)
2689 return ERR_PTR(-ENOENT
);
2693 static noinline_for_stack
2694 u64
calcu_metadata_size(struct reloc_control
*rc
,
2695 struct backref_node
*node
, int reserve
)
2697 struct btrfs_fs_info
*fs_info
= rc
->extent_root
->fs_info
;
2698 struct backref_node
*next
= node
;
2699 struct backref_edge
*edge
;
2700 struct backref_edge
*edges
[BTRFS_MAX_LEVEL
- 1];
2704 BUG_ON(reserve
&& node
->processed
);
2709 if (next
->processed
&& (reserve
|| next
!= node
))
2712 num_bytes
+= fs_info
->nodesize
;
2714 if (list_empty(&next
->upper
))
2717 edge
= list_entry(next
->upper
.next
,
2718 struct backref_edge
, list
[LOWER
]);
2719 edges
[index
++] = edge
;
2720 next
= edge
->node
[UPPER
];
2722 next
= walk_down_backref(edges
, &index
);
2727 static int reserve_metadata_space(struct btrfs_trans_handle
*trans
,
2728 struct reloc_control
*rc
,
2729 struct backref_node
*node
)
2731 struct btrfs_root
*root
= rc
->extent_root
;
2732 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
2737 num_bytes
= calcu_metadata_size(rc
, node
, 1) * 2;
2739 trans
->block_rsv
= rc
->block_rsv
;
2740 rc
->reserved_bytes
+= num_bytes
;
2743 * We are under a transaction here so we can only do limited flushing.
2744 * If we get an enospc just kick back -EAGAIN so we know to drop the
2745 * transaction and try to refill when we can flush all the things.
2747 ret
= btrfs_block_rsv_refill(root
, rc
->block_rsv
, num_bytes
,
2748 BTRFS_RESERVE_FLUSH_LIMIT
);
2750 tmp
= fs_info
->nodesize
* RELOCATION_RESERVED_NODES
;
2751 while (tmp
<= rc
->reserved_bytes
)
2754 * only one thread can access block_rsv at this point,
2755 * so we don't need hold lock to protect block_rsv.
2756 * we expand more reservation size here to allow enough
2757 * space for relocation and we will return earlier in
2760 rc
->block_rsv
->size
= tmp
+ fs_info
->nodesize
*
2761 RELOCATION_RESERVED_NODES
;
2769 * relocate a block tree, and then update pointers in upper level
2770 * blocks that reference the block to point to the new location.
2772 * if called by link_to_upper, the block has already been relocated.
2773 * in that case this function just updates pointers.
2775 static int do_relocation(struct btrfs_trans_handle
*trans
,
2776 struct reloc_control
*rc
,
2777 struct backref_node
*node
,
2778 struct btrfs_key
*key
,
2779 struct btrfs_path
*path
, int lowest
)
2781 struct btrfs_fs_info
*fs_info
= rc
->extent_root
->fs_info
;
2782 struct backref_node
*upper
;
2783 struct backref_edge
*edge
;
2784 struct backref_edge
*edges
[BTRFS_MAX_LEVEL
- 1];
2785 struct btrfs_root
*root
;
2786 struct extent_buffer
*eb
;
2794 BUG_ON(lowest
&& node
->eb
);
2796 path
->lowest_level
= node
->level
+ 1;
2797 rc
->backref_cache
.path
[node
->level
] = node
;
2798 list_for_each_entry(edge
, &node
->upper
, list
[LOWER
]) {
2799 struct btrfs_key first_key
;
2800 struct btrfs_ref ref
= { 0 };
2804 upper
= edge
->node
[UPPER
];
2805 root
= select_reloc_root(trans
, rc
, upper
, edges
);
2808 if (upper
->eb
&& !upper
->locked
) {
2810 ret
= btrfs_bin_search(upper
->eb
, key
,
2811 upper
->level
, &slot
);
2817 bytenr
= btrfs_node_blockptr(upper
->eb
, slot
);
2818 if (node
->eb
->start
== bytenr
)
2821 drop_node_buffer(upper
);
2825 ret
= btrfs_search_slot(trans
, root
, key
, path
, 0, 1);
2832 btrfs_release_path(path
);
2837 upper
->eb
= path
->nodes
[upper
->level
];
2838 path
->nodes
[upper
->level
] = NULL
;
2840 BUG_ON(upper
->eb
!= path
->nodes
[upper
->level
]);
2844 path
->locks
[upper
->level
] = 0;
2846 slot
= path
->slots
[upper
->level
];
2847 btrfs_release_path(path
);
2849 ret
= btrfs_bin_search(upper
->eb
, key
, upper
->level
,
2858 bytenr
= btrfs_node_blockptr(upper
->eb
, slot
);
2860 if (bytenr
!= node
->bytenr
) {
2861 btrfs_err(root
->fs_info
,
2862 "lowest leaf/node mismatch: bytenr %llu node->bytenr %llu slot %d upper %llu",
2863 bytenr
, node
->bytenr
, slot
,
2869 if (node
->eb
->start
== bytenr
)
2873 blocksize
= root
->fs_info
->nodesize
;
2874 generation
= btrfs_node_ptr_generation(upper
->eb
, slot
);
2875 btrfs_node_key_to_cpu(upper
->eb
, &first_key
, slot
);
2876 eb
= read_tree_block(fs_info
, bytenr
, generation
,
2877 upper
->level
- 1, &first_key
);
2881 } else if (!extent_buffer_uptodate(eb
)) {
2882 free_extent_buffer(eb
);
2886 btrfs_tree_lock(eb
);
2887 btrfs_set_lock_blocking_write(eb
);
2890 ret
= btrfs_cow_block(trans
, root
, eb
, upper
->eb
,
2892 btrfs_tree_unlock(eb
);
2893 free_extent_buffer(eb
);
2898 BUG_ON(node
->eb
!= eb
);
2900 btrfs_set_node_blockptr(upper
->eb
, slot
,
2902 btrfs_set_node_ptr_generation(upper
->eb
, slot
,
2904 btrfs_mark_buffer_dirty(upper
->eb
);
2906 btrfs_init_generic_ref(&ref
, BTRFS_ADD_DELAYED_REF
,
2907 node
->eb
->start
, blocksize
,
2909 ref
.real_root
= root
->root_key
.objectid
;
2910 btrfs_init_tree_ref(&ref
, node
->level
,
2911 btrfs_header_owner(upper
->eb
));
2912 ret
= btrfs_inc_extent_ref(trans
, &ref
);
2915 ret
= btrfs_drop_subtree(trans
, root
, eb
, upper
->eb
);
2919 if (!upper
->pending
)
2920 drop_node_buffer(upper
);
2922 unlock_node_buffer(upper
);
2927 if (!err
&& node
->pending
) {
2928 drop_node_buffer(node
);
2929 list_move_tail(&node
->list
, &rc
->backref_cache
.changed
);
2933 path
->lowest_level
= 0;
2934 BUG_ON(err
== -ENOSPC
);
2938 static int link_to_upper(struct btrfs_trans_handle
*trans
,
2939 struct reloc_control
*rc
,
2940 struct backref_node
*node
,
2941 struct btrfs_path
*path
)
2943 struct btrfs_key key
;
2945 btrfs_node_key_to_cpu(node
->eb
, &key
, 0);
2946 return do_relocation(trans
, rc
, node
, &key
, path
, 0);
2949 static int finish_pending_nodes(struct btrfs_trans_handle
*trans
,
2950 struct reloc_control
*rc
,
2951 struct btrfs_path
*path
, int err
)
2954 struct backref_cache
*cache
= &rc
->backref_cache
;
2955 struct backref_node
*node
;
2959 for (level
= 0; level
< BTRFS_MAX_LEVEL
; level
++) {
2960 while (!list_empty(&cache
->pending
[level
])) {
2961 node
= list_entry(cache
->pending
[level
].next
,
2962 struct backref_node
, list
);
2963 list_move_tail(&node
->list
, &list
);
2964 BUG_ON(!node
->pending
);
2967 ret
= link_to_upper(trans
, rc
, node
, path
);
2972 list_splice_init(&list
, &cache
->pending
[level
]);
2977 static void mark_block_processed(struct reloc_control
*rc
,
2978 u64 bytenr
, u32 blocksize
)
2980 set_extent_bits(&rc
->processed_blocks
, bytenr
, bytenr
+ blocksize
- 1,
2984 static void __mark_block_processed(struct reloc_control
*rc
,
2985 struct backref_node
*node
)
2988 if (node
->level
== 0 ||
2989 in_block_group(node
->bytenr
, rc
->block_group
)) {
2990 blocksize
= rc
->extent_root
->fs_info
->nodesize
;
2991 mark_block_processed(rc
, node
->bytenr
, blocksize
);
2993 node
->processed
= 1;
2997 * mark a block and all blocks directly/indirectly reference the block
3000 static void update_processed_blocks(struct reloc_control
*rc
,
3001 struct backref_node
*node
)
3003 struct backref_node
*next
= node
;
3004 struct backref_edge
*edge
;
3005 struct backref_edge
*edges
[BTRFS_MAX_LEVEL
- 1];
3011 if (next
->processed
)
3014 __mark_block_processed(rc
, next
);
3016 if (list_empty(&next
->upper
))
3019 edge
= list_entry(next
->upper
.next
,
3020 struct backref_edge
, list
[LOWER
]);
3021 edges
[index
++] = edge
;
3022 next
= edge
->node
[UPPER
];
3024 next
= walk_down_backref(edges
, &index
);
3028 static int tree_block_processed(u64 bytenr
, struct reloc_control
*rc
)
3030 u32 blocksize
= rc
->extent_root
->fs_info
->nodesize
;
3032 if (test_range_bit(&rc
->processed_blocks
, bytenr
,
3033 bytenr
+ blocksize
- 1, EXTENT_DIRTY
, 1, NULL
))
3038 static int get_tree_block_key(struct btrfs_fs_info
*fs_info
,
3039 struct tree_block
*block
)
3041 struct extent_buffer
*eb
;
3043 BUG_ON(block
->key_ready
);
3044 eb
= read_tree_block(fs_info
, block
->bytenr
, block
->key
.offset
,
3045 block
->level
, NULL
);
3048 } else if (!extent_buffer_uptodate(eb
)) {
3049 free_extent_buffer(eb
);
3052 if (block
->level
== 0)
3053 btrfs_item_key_to_cpu(eb
, &block
->key
, 0);
3055 btrfs_node_key_to_cpu(eb
, &block
->key
, 0);
3056 free_extent_buffer(eb
);
3057 block
->key_ready
= 1;
3062 * helper function to relocate a tree block
3064 static int relocate_tree_block(struct btrfs_trans_handle
*trans
,
3065 struct reloc_control
*rc
,
3066 struct backref_node
*node
,
3067 struct btrfs_key
*key
,
3068 struct btrfs_path
*path
)
3070 struct btrfs_root
*root
;
3076 BUG_ON(node
->processed
);
3077 root
= select_one_root(node
);
3078 if (root
== ERR_PTR(-ENOENT
)) {
3079 update_processed_blocks(rc
, node
);
3083 if (!root
|| test_bit(BTRFS_ROOT_REF_COWS
, &root
->state
)) {
3084 ret
= reserve_metadata_space(trans
, rc
, node
);
3090 if (test_bit(BTRFS_ROOT_REF_COWS
, &root
->state
)) {
3091 BUG_ON(node
->new_bytenr
);
3092 BUG_ON(!list_empty(&node
->list
));
3093 btrfs_record_root_in_trans(trans
, root
);
3094 root
= root
->reloc_root
;
3095 node
->new_bytenr
= root
->node
->start
;
3097 list_add_tail(&node
->list
, &rc
->backref_cache
.changed
);
3099 path
->lowest_level
= node
->level
;
3100 ret
= btrfs_search_slot(trans
, root
, key
, path
, 0, 1);
3101 btrfs_release_path(path
);
3106 update_processed_blocks(rc
, node
);
3108 ret
= do_relocation(trans
, rc
, node
, key
, path
, 1);
3111 if (ret
|| node
->level
== 0 || node
->cowonly
)
3112 remove_backref_node(&rc
->backref_cache
, node
);
3117 * relocate a list of blocks
3119 static noinline_for_stack
3120 int relocate_tree_blocks(struct btrfs_trans_handle
*trans
,
3121 struct reloc_control
*rc
, struct rb_root
*blocks
)
3123 struct btrfs_fs_info
*fs_info
= rc
->extent_root
->fs_info
;
3124 struct backref_node
*node
;
3125 struct btrfs_path
*path
;
3126 struct tree_block
*block
;
3127 struct tree_block
*next
;
3131 path
= btrfs_alloc_path();
3134 goto out_free_blocks
;
3137 /* Kick in readahead for tree blocks with missing keys */
3138 rbtree_postorder_for_each_entry_safe(block
, next
, blocks
, rb_node
) {
3139 if (!block
->key_ready
)
3140 readahead_tree_block(fs_info
, block
->bytenr
);
3143 /* Get first keys */
3144 rbtree_postorder_for_each_entry_safe(block
, next
, blocks
, rb_node
) {
3145 if (!block
->key_ready
) {
3146 err
= get_tree_block_key(fs_info
, block
);
3152 /* Do tree relocation */
3153 rbtree_postorder_for_each_entry_safe(block
, next
, blocks
, rb_node
) {
3154 node
= build_backref_tree(rc
, &block
->key
,
3155 block
->level
, block
->bytenr
);
3157 err
= PTR_ERR(node
);
3161 ret
= relocate_tree_block(trans
, rc
, node
, &block
->key
,
3164 if (ret
!= -EAGAIN
|| &block
->rb_node
== rb_first(blocks
))
3170 err
= finish_pending_nodes(trans
, rc
, path
, err
);
3173 btrfs_free_path(path
);
3175 free_block_list(blocks
);
3179 static noinline_for_stack
3180 int prealloc_file_extent_cluster(struct inode
*inode
,
3181 struct file_extent_cluster
*cluster
)
3186 u64 offset
= BTRFS_I(inode
)->index_cnt
;
3190 u64 prealloc_start
= cluster
->start
- offset
;
3191 u64 prealloc_end
= cluster
->end
- offset
;
3193 struct extent_changeset
*data_reserved
= NULL
;
3195 BUG_ON(cluster
->start
!= cluster
->boundary
[0]);
3198 ret
= btrfs_check_data_free_space(inode
, &data_reserved
, prealloc_start
,
3199 prealloc_end
+ 1 - prealloc_start
);
3203 cur_offset
= prealloc_start
;
3204 while (nr
< cluster
->nr
) {
3205 start
= cluster
->boundary
[nr
] - offset
;
3206 if (nr
+ 1 < cluster
->nr
)
3207 end
= cluster
->boundary
[nr
+ 1] - 1 - offset
;
3209 end
= cluster
->end
- offset
;
3211 lock_extent(&BTRFS_I(inode
)->io_tree
, start
, end
);
3212 num_bytes
= end
+ 1 - start
;
3213 if (cur_offset
< start
)
3214 btrfs_free_reserved_data_space(inode
, data_reserved
,
3215 cur_offset
, start
- cur_offset
);
3216 ret
= btrfs_prealloc_file_range(inode
, 0, start
,
3217 num_bytes
, num_bytes
,
3218 end
+ 1, &alloc_hint
);
3219 cur_offset
= end
+ 1;
3220 unlock_extent(&BTRFS_I(inode
)->io_tree
, start
, end
);
3225 if (cur_offset
< prealloc_end
)
3226 btrfs_free_reserved_data_space(inode
, data_reserved
,
3227 cur_offset
, prealloc_end
+ 1 - cur_offset
);
3229 inode_unlock(inode
);
3230 extent_changeset_free(data_reserved
);
3234 static noinline_for_stack
3235 int setup_extent_mapping(struct inode
*inode
, u64 start
, u64 end
,
3238 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
3239 struct extent_map
*em
;
3242 em
= alloc_extent_map();
3247 em
->len
= end
+ 1 - start
;
3248 em
->block_len
= em
->len
;
3249 em
->block_start
= block_start
;
3250 set_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
3252 lock_extent(&BTRFS_I(inode
)->io_tree
, start
, end
);
3254 write_lock(&em_tree
->lock
);
3255 ret
= add_extent_mapping(em_tree
, em
, 0);
3256 write_unlock(&em_tree
->lock
);
3257 if (ret
!= -EEXIST
) {
3258 free_extent_map(em
);
3261 btrfs_drop_extent_cache(BTRFS_I(inode
), start
, end
, 0);
3263 unlock_extent(&BTRFS_I(inode
)->io_tree
, start
, end
);
3267 static int relocate_file_extent_cluster(struct inode
*inode
,
3268 struct file_extent_cluster
*cluster
)
3270 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
3273 u64 offset
= BTRFS_I(inode
)->index_cnt
;
3274 unsigned long index
;
3275 unsigned long last_index
;
3277 struct file_ra_state
*ra
;
3278 gfp_t mask
= btrfs_alloc_write_mask(inode
->i_mapping
);
3285 ra
= kzalloc(sizeof(*ra
), GFP_NOFS
);
3289 ret
= prealloc_file_extent_cluster(inode
, cluster
);
3293 file_ra_state_init(ra
, inode
->i_mapping
);
3295 ret
= setup_extent_mapping(inode
, cluster
->start
- offset
,
3296 cluster
->end
- offset
, cluster
->start
);
3300 index
= (cluster
->start
- offset
) >> PAGE_SHIFT
;
3301 last_index
= (cluster
->end
- offset
) >> PAGE_SHIFT
;
3302 while (index
<= last_index
) {
3303 ret
= btrfs_delalloc_reserve_metadata(BTRFS_I(inode
),
3308 page
= find_lock_page(inode
->i_mapping
, index
);
3310 page_cache_sync_readahead(inode
->i_mapping
,
3312 last_index
+ 1 - index
);
3313 page
= find_or_create_page(inode
->i_mapping
, index
,
3316 btrfs_delalloc_release_metadata(BTRFS_I(inode
),
3318 btrfs_delalloc_release_extents(BTRFS_I(inode
),
3325 if (PageReadahead(page
)) {
3326 page_cache_async_readahead(inode
->i_mapping
,
3327 ra
, NULL
, page
, index
,
3328 last_index
+ 1 - index
);
3331 if (!PageUptodate(page
)) {
3332 btrfs_readpage(NULL
, page
);
3334 if (!PageUptodate(page
)) {
3337 btrfs_delalloc_release_metadata(BTRFS_I(inode
),
3339 btrfs_delalloc_release_extents(BTRFS_I(inode
),
3346 page_start
= page_offset(page
);
3347 page_end
= page_start
+ PAGE_SIZE
- 1;
3349 lock_extent(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
);
3351 set_page_extent_mapped(page
);
3353 if (nr
< cluster
->nr
&&
3354 page_start
+ offset
== cluster
->boundary
[nr
]) {
3355 set_extent_bits(&BTRFS_I(inode
)->io_tree
,
3356 page_start
, page_end
,
3361 ret
= btrfs_set_extent_delalloc(inode
, page_start
, page_end
, 0,
3366 btrfs_delalloc_release_metadata(BTRFS_I(inode
),
3368 btrfs_delalloc_release_extents(BTRFS_I(inode
),
3371 clear_extent_bits(&BTRFS_I(inode
)->io_tree
,
3372 page_start
, page_end
,
3373 EXTENT_LOCKED
| EXTENT_BOUNDARY
);
3377 set_page_dirty(page
);
3379 unlock_extent(&BTRFS_I(inode
)->io_tree
,
3380 page_start
, page_end
);
3385 btrfs_delalloc_release_extents(BTRFS_I(inode
), PAGE_SIZE
);
3386 balance_dirty_pages_ratelimited(inode
->i_mapping
);
3387 btrfs_throttle(fs_info
);
3389 WARN_ON(nr
!= cluster
->nr
);
3395 static noinline_for_stack
3396 int relocate_data_extent(struct inode
*inode
, struct btrfs_key
*extent_key
,
3397 struct file_extent_cluster
*cluster
)
3401 if (cluster
->nr
> 0 && extent_key
->objectid
!= cluster
->end
+ 1) {
3402 ret
= relocate_file_extent_cluster(inode
, cluster
);
3409 cluster
->start
= extent_key
->objectid
;
3411 BUG_ON(cluster
->nr
>= MAX_EXTENTS
);
3412 cluster
->end
= extent_key
->objectid
+ extent_key
->offset
- 1;
3413 cluster
->boundary
[cluster
->nr
] = extent_key
->objectid
;
3416 if (cluster
->nr
>= MAX_EXTENTS
) {
3417 ret
= relocate_file_extent_cluster(inode
, cluster
);
3426 * helper to add a tree block to the list.
3427 * the major work is getting the generation and level of the block
3429 static int add_tree_block(struct reloc_control
*rc
,
3430 struct btrfs_key
*extent_key
,
3431 struct btrfs_path
*path
,
3432 struct rb_root
*blocks
)
3434 struct extent_buffer
*eb
;
3435 struct btrfs_extent_item
*ei
;
3436 struct btrfs_tree_block_info
*bi
;
3437 struct tree_block
*block
;
3438 struct rb_node
*rb_node
;
3443 eb
= path
->nodes
[0];
3444 item_size
= btrfs_item_size_nr(eb
, path
->slots
[0]);
3446 if (extent_key
->type
== BTRFS_METADATA_ITEM_KEY
||
3447 item_size
>= sizeof(*ei
) + sizeof(*bi
)) {
3448 ei
= btrfs_item_ptr(eb
, path
->slots
[0],
3449 struct btrfs_extent_item
);
3450 if (extent_key
->type
== BTRFS_EXTENT_ITEM_KEY
) {
3451 bi
= (struct btrfs_tree_block_info
*)(ei
+ 1);
3452 level
= btrfs_tree_block_level(eb
, bi
);
3454 level
= (int)extent_key
->offset
;
3456 generation
= btrfs_extent_generation(eb
, ei
);
3457 } else if (unlikely(item_size
== sizeof(struct btrfs_extent_item_v0
))) {
3458 btrfs_print_v0_err(eb
->fs_info
);
3459 btrfs_handle_fs_error(eb
->fs_info
, -EINVAL
, NULL
);
3465 btrfs_release_path(path
);
3467 BUG_ON(level
== -1);
3469 block
= kmalloc(sizeof(*block
), GFP_NOFS
);
3473 block
->bytenr
= extent_key
->objectid
;
3474 block
->key
.objectid
= rc
->extent_root
->fs_info
->nodesize
;
3475 block
->key
.offset
= generation
;
3476 block
->level
= level
;
3477 block
->key_ready
= 0;
3479 rb_node
= tree_insert(blocks
, block
->bytenr
, &block
->rb_node
);
3481 backref_tree_panic(rb_node
, -EEXIST
, block
->bytenr
);
3487 * helper to add tree blocks for backref of type BTRFS_SHARED_DATA_REF_KEY
3489 static int __add_tree_block(struct reloc_control
*rc
,
3490 u64 bytenr
, u32 blocksize
,
3491 struct rb_root
*blocks
)
3493 struct btrfs_fs_info
*fs_info
= rc
->extent_root
->fs_info
;
3494 struct btrfs_path
*path
;
3495 struct btrfs_key key
;
3497 bool skinny
= btrfs_fs_incompat(fs_info
, SKINNY_METADATA
);
3499 if (tree_block_processed(bytenr
, rc
))
3502 if (tree_search(blocks
, bytenr
))
3505 path
= btrfs_alloc_path();
3509 key
.objectid
= bytenr
;
3511 key
.type
= BTRFS_METADATA_ITEM_KEY
;
3512 key
.offset
= (u64
)-1;
3514 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
3515 key
.offset
= blocksize
;
3518 path
->search_commit_root
= 1;
3519 path
->skip_locking
= 1;
3520 ret
= btrfs_search_slot(NULL
, rc
->extent_root
, &key
, path
, 0, 0);
3524 if (ret
> 0 && skinny
) {
3525 if (path
->slots
[0]) {
3527 btrfs_item_key_to_cpu(path
->nodes
[0], &key
,
3529 if (key
.objectid
== bytenr
&&
3530 (key
.type
== BTRFS_METADATA_ITEM_KEY
||
3531 (key
.type
== BTRFS_EXTENT_ITEM_KEY
&&
3532 key
.offset
== blocksize
)))
3538 btrfs_release_path(path
);
3544 btrfs_print_leaf(path
->nodes
[0]);
3546 "tree block extent item (%llu) is not found in extent tree",
3553 ret
= add_tree_block(rc
, &key
, path
, blocks
);
3555 btrfs_free_path(path
);
3560 * helper to check if the block use full backrefs for pointers in it
3562 static int block_use_full_backref(struct reloc_control
*rc
,
3563 struct extent_buffer
*eb
)
3568 if (btrfs_header_flag(eb
, BTRFS_HEADER_FLAG_RELOC
) ||
3569 btrfs_header_backref_rev(eb
) < BTRFS_MIXED_BACKREF_REV
)
3572 ret
= btrfs_lookup_extent_info(NULL
, rc
->extent_root
->fs_info
,
3573 eb
->start
, btrfs_header_level(eb
), 1,
3577 if (flags
& BTRFS_BLOCK_FLAG_FULL_BACKREF
)
3584 static int delete_block_group_cache(struct btrfs_fs_info
*fs_info
,
3585 struct btrfs_block_group
*block_group
,
3586 struct inode
*inode
,
3589 struct btrfs_key key
;
3590 struct btrfs_root
*root
= fs_info
->tree_root
;
3591 struct btrfs_trans_handle
*trans
;
3598 key
.type
= BTRFS_INODE_ITEM_KEY
;
3601 inode
= btrfs_iget(fs_info
->sb
, &key
, root
);
3606 ret
= btrfs_check_trunc_cache_free_space(fs_info
,
3607 &fs_info
->global_block_rsv
);
3611 trans
= btrfs_join_transaction(root
);
3612 if (IS_ERR(trans
)) {
3613 ret
= PTR_ERR(trans
);
3617 ret
= btrfs_truncate_free_space_cache(trans
, block_group
, inode
);
3619 btrfs_end_transaction(trans
);
3620 btrfs_btree_balance_dirty(fs_info
);
3627 * helper to add tree blocks for backref of type BTRFS_EXTENT_DATA_REF_KEY
3628 * this function scans fs tree to find blocks reference the data extent
3630 static int find_data_references(struct reloc_control
*rc
,
3631 struct btrfs_key
*extent_key
,
3632 struct extent_buffer
*leaf
,
3633 struct btrfs_extent_data_ref
*ref
,
3634 struct rb_root
*blocks
)
3636 struct btrfs_fs_info
*fs_info
= rc
->extent_root
->fs_info
;
3637 struct btrfs_path
*path
;
3638 struct tree_block
*block
;
3639 struct btrfs_root
*root
;
3640 struct btrfs_file_extent_item
*fi
;
3641 struct rb_node
*rb_node
;
3642 struct btrfs_key key
;
3653 ref_root
= btrfs_extent_data_ref_root(leaf
, ref
);
3654 ref_objectid
= btrfs_extent_data_ref_objectid(leaf
, ref
);
3655 ref_offset
= btrfs_extent_data_ref_offset(leaf
, ref
);
3656 ref_count
= btrfs_extent_data_ref_count(leaf
, ref
);
3659 * This is an extent belonging to the free space cache, lets just delete
3660 * it and redo the search.
3662 if (ref_root
== BTRFS_ROOT_TREE_OBJECTID
) {
3663 ret
= delete_block_group_cache(fs_info
, rc
->block_group
,
3664 NULL
, ref_objectid
);
3670 path
= btrfs_alloc_path();
3673 path
->reada
= READA_FORWARD
;
3675 root
= read_fs_root(fs_info
, ref_root
);
3677 err
= PTR_ERR(root
);
3681 key
.objectid
= ref_objectid
;
3682 key
.type
= BTRFS_EXTENT_DATA_KEY
;
3683 if (ref_offset
> ((u64
)-1 << 32))
3686 key
.offset
= ref_offset
;
3688 path
->search_commit_root
= 1;
3689 path
->skip_locking
= 1;
3690 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
3696 leaf
= path
->nodes
[0];
3697 nritems
= btrfs_header_nritems(leaf
);
3699 * the references in tree blocks that use full backrefs
3700 * are not counted in
3702 if (block_use_full_backref(rc
, leaf
))
3706 rb_node
= tree_search(blocks
, leaf
->start
);
3711 path
->slots
[0] = nritems
;
3714 while (ref_count
> 0) {
3715 while (path
->slots
[0] >= nritems
) {
3716 ret
= btrfs_next_leaf(root
, path
);
3721 if (WARN_ON(ret
> 0))
3724 leaf
= path
->nodes
[0];
3725 nritems
= btrfs_header_nritems(leaf
);
3728 if (block_use_full_backref(rc
, leaf
))
3732 rb_node
= tree_search(blocks
, leaf
->start
);
3737 path
->slots
[0] = nritems
;
3741 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
3742 if (WARN_ON(key
.objectid
!= ref_objectid
||
3743 key
.type
!= BTRFS_EXTENT_DATA_KEY
))
3746 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
3747 struct btrfs_file_extent_item
);
3749 if (btrfs_file_extent_type(leaf
, fi
) ==
3750 BTRFS_FILE_EXTENT_INLINE
)
3753 if (btrfs_file_extent_disk_bytenr(leaf
, fi
) !=
3754 extent_key
->objectid
)
3757 key
.offset
-= btrfs_file_extent_offset(leaf
, fi
);
3758 if (key
.offset
!= ref_offset
)
3766 if (!tree_block_processed(leaf
->start
, rc
)) {
3767 block
= kmalloc(sizeof(*block
), GFP_NOFS
);
3772 block
->bytenr
= leaf
->start
;
3773 btrfs_item_key_to_cpu(leaf
, &block
->key
, 0);
3775 block
->key_ready
= 1;
3776 rb_node
= tree_insert(blocks
, block
->bytenr
,
3779 backref_tree_panic(rb_node
, -EEXIST
,
3785 path
->slots
[0] = nritems
;
3791 btrfs_free_path(path
);
3796 * helper to find all tree blocks that reference a given data extent
3798 static noinline_for_stack
3799 int add_data_references(struct reloc_control
*rc
,
3800 struct btrfs_key
*extent_key
,
3801 struct btrfs_path
*path
,
3802 struct rb_root
*blocks
)
3804 struct btrfs_key key
;
3805 struct extent_buffer
*eb
;
3806 struct btrfs_extent_data_ref
*dref
;
3807 struct btrfs_extent_inline_ref
*iref
;
3810 u32 blocksize
= rc
->extent_root
->fs_info
->nodesize
;
3814 eb
= path
->nodes
[0];
3815 ptr
= btrfs_item_ptr_offset(eb
, path
->slots
[0]);
3816 end
= ptr
+ btrfs_item_size_nr(eb
, path
->slots
[0]);
3817 ptr
+= sizeof(struct btrfs_extent_item
);
3820 iref
= (struct btrfs_extent_inline_ref
*)ptr
;
3821 key
.type
= btrfs_get_extent_inline_ref_type(eb
, iref
,
3822 BTRFS_REF_TYPE_DATA
);
3823 if (key
.type
== BTRFS_SHARED_DATA_REF_KEY
) {
3824 key
.offset
= btrfs_extent_inline_ref_offset(eb
, iref
);
3825 ret
= __add_tree_block(rc
, key
.offset
, blocksize
,
3827 } else if (key
.type
== BTRFS_EXTENT_DATA_REF_KEY
) {
3828 dref
= (struct btrfs_extent_data_ref
*)(&iref
->offset
);
3829 ret
= find_data_references(rc
, extent_key
,
3833 btrfs_err(rc
->extent_root
->fs_info
,
3834 "extent %llu slot %d has an invalid inline ref type",
3835 eb
->start
, path
->slots
[0]);
3841 ptr
+= btrfs_extent_inline_ref_size(key
.type
);
3847 eb
= path
->nodes
[0];
3848 if (path
->slots
[0] >= btrfs_header_nritems(eb
)) {
3849 ret
= btrfs_next_leaf(rc
->extent_root
, path
);
3856 eb
= path
->nodes
[0];
3859 btrfs_item_key_to_cpu(eb
, &key
, path
->slots
[0]);
3860 if (key
.objectid
!= extent_key
->objectid
)
3863 if (key
.type
== BTRFS_SHARED_DATA_REF_KEY
) {
3864 ret
= __add_tree_block(rc
, key
.offset
, blocksize
,
3866 } else if (key
.type
== BTRFS_EXTENT_DATA_REF_KEY
) {
3867 dref
= btrfs_item_ptr(eb
, path
->slots
[0],
3868 struct btrfs_extent_data_ref
);
3869 ret
= find_data_references(rc
, extent_key
,
3871 } else if (unlikely(key
.type
== BTRFS_EXTENT_REF_V0_KEY
)) {
3872 btrfs_print_v0_err(eb
->fs_info
);
3873 btrfs_handle_fs_error(eb
->fs_info
, -EINVAL
, NULL
);
3885 btrfs_release_path(path
);
3887 free_block_list(blocks
);
3892 * helper to find next unprocessed extent
3894 static noinline_for_stack
3895 int find_next_extent(struct reloc_control
*rc
, struct btrfs_path
*path
,
3896 struct btrfs_key
*extent_key
)
3898 struct btrfs_fs_info
*fs_info
= rc
->extent_root
->fs_info
;
3899 struct btrfs_key key
;
3900 struct extent_buffer
*leaf
;
3901 u64 start
, end
, last
;
3904 last
= rc
->block_group
->start
+ rc
->block_group
->length
;
3907 if (rc
->search_start
>= last
) {
3912 key
.objectid
= rc
->search_start
;
3913 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
3916 path
->search_commit_root
= 1;
3917 path
->skip_locking
= 1;
3918 ret
= btrfs_search_slot(NULL
, rc
->extent_root
, &key
, path
,
3923 leaf
= path
->nodes
[0];
3924 if (path
->slots
[0] >= btrfs_header_nritems(leaf
)) {
3925 ret
= btrfs_next_leaf(rc
->extent_root
, path
);
3928 leaf
= path
->nodes
[0];
3931 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
3932 if (key
.objectid
>= last
) {
3937 if (key
.type
!= BTRFS_EXTENT_ITEM_KEY
&&
3938 key
.type
!= BTRFS_METADATA_ITEM_KEY
) {
3943 if (key
.type
== BTRFS_EXTENT_ITEM_KEY
&&
3944 key
.objectid
+ key
.offset
<= rc
->search_start
) {
3949 if (key
.type
== BTRFS_METADATA_ITEM_KEY
&&
3950 key
.objectid
+ fs_info
->nodesize
<=
3956 ret
= find_first_extent_bit(&rc
->processed_blocks
,
3957 key
.objectid
, &start
, &end
,
3958 EXTENT_DIRTY
, NULL
);
3960 if (ret
== 0 && start
<= key
.objectid
) {
3961 btrfs_release_path(path
);
3962 rc
->search_start
= end
+ 1;
3964 if (key
.type
== BTRFS_EXTENT_ITEM_KEY
)
3965 rc
->search_start
= key
.objectid
+ key
.offset
;
3967 rc
->search_start
= key
.objectid
+
3969 memcpy(extent_key
, &key
, sizeof(key
));
3973 btrfs_release_path(path
);
3977 static void set_reloc_control(struct reloc_control
*rc
)
3979 struct btrfs_fs_info
*fs_info
= rc
->extent_root
->fs_info
;
3981 mutex_lock(&fs_info
->reloc_mutex
);
3982 fs_info
->reloc_ctl
= rc
;
3983 mutex_unlock(&fs_info
->reloc_mutex
);
3986 static void unset_reloc_control(struct reloc_control
*rc
)
3988 struct btrfs_fs_info
*fs_info
= rc
->extent_root
->fs_info
;
3990 mutex_lock(&fs_info
->reloc_mutex
);
3991 fs_info
->reloc_ctl
= NULL
;
3992 mutex_unlock(&fs_info
->reloc_mutex
);
3995 static int check_extent_flags(u64 flags
)
3997 if ((flags
& BTRFS_EXTENT_FLAG_DATA
) &&
3998 (flags
& BTRFS_EXTENT_FLAG_TREE_BLOCK
))
4000 if (!(flags
& BTRFS_EXTENT_FLAG_DATA
) &&
4001 !(flags
& BTRFS_EXTENT_FLAG_TREE_BLOCK
))
4003 if ((flags
& BTRFS_EXTENT_FLAG_DATA
) &&
4004 (flags
& BTRFS_BLOCK_FLAG_FULL_BACKREF
))
4009 static noinline_for_stack
4010 int prepare_to_relocate(struct reloc_control
*rc
)
4012 struct btrfs_trans_handle
*trans
;
4015 rc
->block_rsv
= btrfs_alloc_block_rsv(rc
->extent_root
->fs_info
,
4016 BTRFS_BLOCK_RSV_TEMP
);
4020 memset(&rc
->cluster
, 0, sizeof(rc
->cluster
));
4021 rc
->search_start
= rc
->block_group
->start
;
4022 rc
->extents_found
= 0;
4023 rc
->nodes_relocated
= 0;
4024 rc
->merging_rsv_size
= 0;
4025 rc
->reserved_bytes
= 0;
4026 rc
->block_rsv
->size
= rc
->extent_root
->fs_info
->nodesize
*
4027 RELOCATION_RESERVED_NODES
;
4028 ret
= btrfs_block_rsv_refill(rc
->extent_root
,
4029 rc
->block_rsv
, rc
->block_rsv
->size
,
4030 BTRFS_RESERVE_FLUSH_ALL
);
4034 rc
->create_reloc_tree
= 1;
4035 set_reloc_control(rc
);
4037 trans
= btrfs_join_transaction(rc
->extent_root
);
4038 if (IS_ERR(trans
)) {
4039 unset_reloc_control(rc
);
4041 * extent tree is not a ref_cow tree and has no reloc_root to
4042 * cleanup. And callers are responsible to free the above
4045 return PTR_ERR(trans
);
4047 btrfs_commit_transaction(trans
);
4051 static noinline_for_stack
int relocate_block_group(struct reloc_control
*rc
)
4053 struct btrfs_fs_info
*fs_info
= rc
->extent_root
->fs_info
;
4054 struct rb_root blocks
= RB_ROOT
;
4055 struct btrfs_key key
;
4056 struct btrfs_trans_handle
*trans
= NULL
;
4057 struct btrfs_path
*path
;
4058 struct btrfs_extent_item
*ei
;
4065 path
= btrfs_alloc_path();
4068 path
->reada
= READA_FORWARD
;
4070 ret
= prepare_to_relocate(rc
);
4077 rc
->reserved_bytes
= 0;
4078 ret
= btrfs_block_rsv_refill(rc
->extent_root
,
4079 rc
->block_rsv
, rc
->block_rsv
->size
,
4080 BTRFS_RESERVE_FLUSH_ALL
);
4086 trans
= btrfs_start_transaction(rc
->extent_root
, 0);
4087 if (IS_ERR(trans
)) {
4088 err
= PTR_ERR(trans
);
4093 if (update_backref_cache(trans
, &rc
->backref_cache
)) {
4094 btrfs_end_transaction(trans
);
4099 ret
= find_next_extent(rc
, path
, &key
);
4105 rc
->extents_found
++;
4107 ei
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
4108 struct btrfs_extent_item
);
4109 item_size
= btrfs_item_size_nr(path
->nodes
[0], path
->slots
[0]);
4110 if (item_size
>= sizeof(*ei
)) {
4111 flags
= btrfs_extent_flags(path
->nodes
[0], ei
);
4112 ret
= check_extent_flags(flags
);
4114 } else if (unlikely(item_size
== sizeof(struct btrfs_extent_item_v0
))) {
4116 btrfs_print_v0_err(trans
->fs_info
);
4117 btrfs_abort_transaction(trans
, err
);
4123 if (flags
& BTRFS_EXTENT_FLAG_TREE_BLOCK
) {
4124 ret
= add_tree_block(rc
, &key
, path
, &blocks
);
4125 } else if (rc
->stage
== UPDATE_DATA_PTRS
&&
4126 (flags
& BTRFS_EXTENT_FLAG_DATA
)) {
4127 ret
= add_data_references(rc
, &key
, path
, &blocks
);
4129 btrfs_release_path(path
);
4137 if (!RB_EMPTY_ROOT(&blocks
)) {
4138 ret
= relocate_tree_blocks(trans
, rc
, &blocks
);
4141 * if we fail to relocate tree blocks, force to update
4142 * backref cache when committing transaction.
4144 rc
->backref_cache
.last_trans
= trans
->transid
- 1;
4146 if (ret
!= -EAGAIN
) {
4150 rc
->extents_found
--;
4151 rc
->search_start
= key
.objectid
;
4155 btrfs_end_transaction_throttle(trans
);
4156 btrfs_btree_balance_dirty(fs_info
);
4159 if (rc
->stage
== MOVE_DATA_EXTENTS
&&
4160 (flags
& BTRFS_EXTENT_FLAG_DATA
)) {
4161 rc
->found_file_extent
= 1;
4162 ret
= relocate_data_extent(rc
->data_inode
,
4163 &key
, &rc
->cluster
);
4170 if (trans
&& progress
&& err
== -ENOSPC
) {
4171 ret
= btrfs_force_chunk_alloc(trans
, rc
->block_group
->flags
);
4179 btrfs_release_path(path
);
4180 clear_extent_bits(&rc
->processed_blocks
, 0, (u64
)-1, EXTENT_DIRTY
);
4183 btrfs_end_transaction_throttle(trans
);
4184 btrfs_btree_balance_dirty(fs_info
);
4188 ret
= relocate_file_extent_cluster(rc
->data_inode
,
4194 rc
->create_reloc_tree
= 0;
4195 set_reloc_control(rc
);
4197 backref_cache_cleanup(&rc
->backref_cache
);
4198 btrfs_block_rsv_release(fs_info
, rc
->block_rsv
, (u64
)-1);
4200 err
= prepare_to_merge(rc
, err
);
4202 merge_reloc_roots(rc
);
4204 rc
->merge_reloc_tree
= 0;
4205 unset_reloc_control(rc
);
4206 btrfs_block_rsv_release(fs_info
, rc
->block_rsv
, (u64
)-1);
4208 /* get rid of pinned extents */
4209 trans
= btrfs_join_transaction(rc
->extent_root
);
4210 if (IS_ERR(trans
)) {
4211 err
= PTR_ERR(trans
);
4214 btrfs_commit_transaction(trans
);
4215 ret
= clean_dirty_subvols(rc
);
4216 if (ret
< 0 && !err
)
4219 btrfs_free_block_rsv(fs_info
, rc
->block_rsv
);
4220 btrfs_free_path(path
);
4224 static int __insert_orphan_inode(struct btrfs_trans_handle
*trans
,
4225 struct btrfs_root
*root
, u64 objectid
)
4227 struct btrfs_path
*path
;
4228 struct btrfs_inode_item
*item
;
4229 struct extent_buffer
*leaf
;
4232 path
= btrfs_alloc_path();
4236 ret
= btrfs_insert_empty_inode(trans
, root
, path
, objectid
);
4240 leaf
= path
->nodes
[0];
4241 item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_inode_item
);
4242 memzero_extent_buffer(leaf
, (unsigned long)item
, sizeof(*item
));
4243 btrfs_set_inode_generation(leaf
, item
, 1);
4244 btrfs_set_inode_size(leaf
, item
, 0);
4245 btrfs_set_inode_mode(leaf
, item
, S_IFREG
| 0600);
4246 btrfs_set_inode_flags(leaf
, item
, BTRFS_INODE_NOCOMPRESS
|
4247 BTRFS_INODE_PREALLOC
);
4248 btrfs_mark_buffer_dirty(leaf
);
4250 btrfs_free_path(path
);
4255 * helper to create inode for data relocation.
4256 * the inode is in data relocation tree and its link count is 0
4258 static noinline_for_stack
4259 struct inode
*create_reloc_inode(struct btrfs_fs_info
*fs_info
,
4260 struct btrfs_block_group
*group
)
4262 struct inode
*inode
= NULL
;
4263 struct btrfs_trans_handle
*trans
;
4264 struct btrfs_root
*root
;
4265 struct btrfs_key key
;
4269 root
= read_fs_root(fs_info
, BTRFS_DATA_RELOC_TREE_OBJECTID
);
4271 return ERR_CAST(root
);
4273 trans
= btrfs_start_transaction(root
, 6);
4275 return ERR_CAST(trans
);
4277 err
= btrfs_find_free_objectid(root
, &objectid
);
4281 err
= __insert_orphan_inode(trans
, root
, objectid
);
4284 key
.objectid
= objectid
;
4285 key
.type
= BTRFS_INODE_ITEM_KEY
;
4287 inode
= btrfs_iget(fs_info
->sb
, &key
, root
);
4288 BUG_ON(IS_ERR(inode
));
4289 BTRFS_I(inode
)->index_cnt
= group
->start
;
4291 err
= btrfs_orphan_add(trans
, BTRFS_I(inode
));
4293 btrfs_end_transaction(trans
);
4294 btrfs_btree_balance_dirty(fs_info
);
4298 inode
= ERR_PTR(err
);
4303 static struct reloc_control
*alloc_reloc_control(struct btrfs_fs_info
*fs_info
)
4305 struct reloc_control
*rc
;
4307 rc
= kzalloc(sizeof(*rc
), GFP_NOFS
);
4311 INIT_LIST_HEAD(&rc
->reloc_roots
);
4312 INIT_LIST_HEAD(&rc
->dirty_subvol_roots
);
4313 backref_cache_init(&rc
->backref_cache
);
4314 mapping_tree_init(&rc
->reloc_root_tree
);
4315 extent_io_tree_init(fs_info
, &rc
->processed_blocks
,
4316 IO_TREE_RELOC_BLOCKS
, NULL
);
4321 * Print the block group being relocated
4323 static void describe_relocation(struct btrfs_fs_info
*fs_info
,
4324 struct btrfs_block_group
*block_group
)
4326 char buf
[128] = {'\0'};
4328 btrfs_describe_block_groups(block_group
->flags
, buf
, sizeof(buf
));
4331 "relocating block group %llu flags %s",
4332 block_group
->start
, buf
);
4335 static const char *stage_to_string(int stage
)
4337 if (stage
== MOVE_DATA_EXTENTS
)
4338 return "move data extents";
4339 if (stage
== UPDATE_DATA_PTRS
)
4340 return "update data pointers";
4345 * function to relocate all extents in a block group.
4347 int btrfs_relocate_block_group(struct btrfs_fs_info
*fs_info
, u64 group_start
)
4349 struct btrfs_block_group
*bg
;
4350 struct btrfs_root
*extent_root
= fs_info
->extent_root
;
4351 struct reloc_control
*rc
;
4352 struct inode
*inode
;
4353 struct btrfs_path
*path
;
4358 bg
= btrfs_lookup_block_group(fs_info
, group_start
);
4362 if (btrfs_pinned_by_swapfile(fs_info
, bg
)) {
4363 btrfs_put_block_group(bg
);
4367 rc
= alloc_reloc_control(fs_info
);
4369 btrfs_put_block_group(bg
);
4373 rc
->extent_root
= extent_root
;
4374 rc
->block_group
= bg
;
4376 ret
= btrfs_inc_block_group_ro(rc
->block_group
, true);
4383 path
= btrfs_alloc_path();
4389 inode
= lookup_free_space_inode(rc
->block_group
, path
);
4390 btrfs_free_path(path
);
4393 ret
= delete_block_group_cache(fs_info
, rc
->block_group
, inode
, 0);
4395 ret
= PTR_ERR(inode
);
4397 if (ret
&& ret
!= -ENOENT
) {
4402 rc
->data_inode
= create_reloc_inode(fs_info
, rc
->block_group
);
4403 if (IS_ERR(rc
->data_inode
)) {
4404 err
= PTR_ERR(rc
->data_inode
);
4405 rc
->data_inode
= NULL
;
4409 describe_relocation(fs_info
, rc
->block_group
);
4411 btrfs_wait_block_group_reservations(rc
->block_group
);
4412 btrfs_wait_nocow_writers(rc
->block_group
);
4413 btrfs_wait_ordered_roots(fs_info
, U64_MAX
,
4414 rc
->block_group
->start
,
4415 rc
->block_group
->length
);
4420 mutex_lock(&fs_info
->cleaner_mutex
);
4421 ret
= relocate_block_group(rc
);
4422 mutex_unlock(&fs_info
->cleaner_mutex
);
4426 finishes_stage
= rc
->stage
;
4428 * We may have gotten ENOSPC after we already dirtied some
4429 * extents. If writeout happens while we're relocating a
4430 * different block group we could end up hitting the
4431 * BUG_ON(rc->stage == UPDATE_DATA_PTRS) in
4432 * btrfs_reloc_cow_block. Make sure we write everything out
4433 * properly so we don't trip over this problem, and then break
4434 * out of the loop if we hit an error.
4436 if (rc
->stage
== MOVE_DATA_EXTENTS
&& rc
->found_file_extent
) {
4437 ret
= btrfs_wait_ordered_range(rc
->data_inode
, 0,
4441 invalidate_mapping_pages(rc
->data_inode
->i_mapping
,
4443 rc
->stage
= UPDATE_DATA_PTRS
;
4449 if (rc
->extents_found
== 0)
4452 btrfs_info(fs_info
, "found %llu extents, stage: %s",
4453 rc
->extents_found
, stage_to_string(finishes_stage
));
4456 WARN_ON(rc
->block_group
->pinned
> 0);
4457 WARN_ON(rc
->block_group
->reserved
> 0);
4458 WARN_ON(rc
->block_group
->used
> 0);
4461 btrfs_dec_block_group_ro(rc
->block_group
);
4462 iput(rc
->data_inode
);
4463 btrfs_put_block_group(rc
->block_group
);
4468 static noinline_for_stack
int mark_garbage_root(struct btrfs_root
*root
)
4470 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
4471 struct btrfs_trans_handle
*trans
;
4474 trans
= btrfs_start_transaction(fs_info
->tree_root
, 0);
4476 return PTR_ERR(trans
);
4478 memset(&root
->root_item
.drop_progress
, 0,
4479 sizeof(root
->root_item
.drop_progress
));
4480 root
->root_item
.drop_level
= 0;
4481 btrfs_set_root_refs(&root
->root_item
, 0);
4482 ret
= btrfs_update_root(trans
, fs_info
->tree_root
,
4483 &root
->root_key
, &root
->root_item
);
4485 err
= btrfs_end_transaction(trans
);
4492 * recover relocation interrupted by system crash.
4494 * this function resumes merging reloc trees with corresponding fs trees.
4495 * this is important for keeping the sharing of tree blocks
4497 int btrfs_recover_relocation(struct btrfs_root
*root
)
4499 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
4500 LIST_HEAD(reloc_roots
);
4501 struct btrfs_key key
;
4502 struct btrfs_root
*fs_root
;
4503 struct btrfs_root
*reloc_root
;
4504 struct btrfs_path
*path
;
4505 struct extent_buffer
*leaf
;
4506 struct reloc_control
*rc
= NULL
;
4507 struct btrfs_trans_handle
*trans
;
4511 path
= btrfs_alloc_path();
4514 path
->reada
= READA_BACK
;
4516 key
.objectid
= BTRFS_TREE_RELOC_OBJECTID
;
4517 key
.type
= BTRFS_ROOT_ITEM_KEY
;
4518 key
.offset
= (u64
)-1;
4521 ret
= btrfs_search_slot(NULL
, fs_info
->tree_root
, &key
,
4528 if (path
->slots
[0] == 0)
4532 leaf
= path
->nodes
[0];
4533 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
4534 btrfs_release_path(path
);
4536 if (key
.objectid
!= BTRFS_TREE_RELOC_OBJECTID
||
4537 key
.type
!= BTRFS_ROOT_ITEM_KEY
)
4540 reloc_root
= btrfs_read_fs_root(root
, &key
);
4541 if (IS_ERR(reloc_root
)) {
4542 err
= PTR_ERR(reloc_root
);
4546 list_add(&reloc_root
->root_list
, &reloc_roots
);
4548 if (btrfs_root_refs(&reloc_root
->root_item
) > 0) {
4549 fs_root
= read_fs_root(fs_info
,
4550 reloc_root
->root_key
.offset
);
4551 if (IS_ERR(fs_root
)) {
4552 ret
= PTR_ERR(fs_root
);
4553 if (ret
!= -ENOENT
) {
4557 ret
= mark_garbage_root(reloc_root
);
4565 if (key
.offset
== 0)
4570 btrfs_release_path(path
);
4572 if (list_empty(&reloc_roots
))
4575 rc
= alloc_reloc_control(fs_info
);
4581 rc
->extent_root
= fs_info
->extent_root
;
4583 set_reloc_control(rc
);
4585 trans
= btrfs_join_transaction(rc
->extent_root
);
4586 if (IS_ERR(trans
)) {
4587 unset_reloc_control(rc
);
4588 err
= PTR_ERR(trans
);
4592 rc
->merge_reloc_tree
= 1;
4594 while (!list_empty(&reloc_roots
)) {
4595 reloc_root
= list_entry(reloc_roots
.next
,
4596 struct btrfs_root
, root_list
);
4597 list_del(&reloc_root
->root_list
);
4599 if (btrfs_root_refs(&reloc_root
->root_item
) == 0) {
4600 list_add_tail(&reloc_root
->root_list
,
4605 fs_root
= read_fs_root(fs_info
, reloc_root
->root_key
.offset
);
4606 if (IS_ERR(fs_root
)) {
4607 err
= PTR_ERR(fs_root
);
4608 list_add_tail(&reloc_root
->root_list
, &reloc_roots
);
4612 err
= __add_reloc_root(reloc_root
);
4613 BUG_ON(err
< 0); /* -ENOMEM or logic error */
4614 fs_root
->reloc_root
= reloc_root
;
4617 err
= btrfs_commit_transaction(trans
);
4621 merge_reloc_roots(rc
);
4623 unset_reloc_control(rc
);
4625 trans
= btrfs_join_transaction(rc
->extent_root
);
4626 if (IS_ERR(trans
)) {
4627 err
= PTR_ERR(trans
);
4630 err
= btrfs_commit_transaction(trans
);
4632 ret
= clean_dirty_subvols(rc
);
4633 if (ret
< 0 && !err
)
4638 if (!list_empty(&reloc_roots
))
4639 free_reloc_roots(&reloc_roots
);
4641 btrfs_free_path(path
);
4644 /* cleanup orphan inode in data relocation tree */
4645 fs_root
= read_fs_root(fs_info
, BTRFS_DATA_RELOC_TREE_OBJECTID
);
4646 if (IS_ERR(fs_root
))
4647 err
= PTR_ERR(fs_root
);
4649 err
= btrfs_orphan_cleanup(fs_root
);
4655 * helper to add ordered checksum for data relocation.
4657 * cloning checksum properly handles the nodatasum extents.
4658 * it also saves CPU time to re-calculate the checksum.
4660 int btrfs_reloc_clone_csums(struct inode
*inode
, u64 file_pos
, u64 len
)
4662 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
4663 struct btrfs_ordered_sum
*sums
;
4664 struct btrfs_ordered_extent
*ordered
;
4670 ordered
= btrfs_lookup_ordered_extent(inode
, file_pos
);
4671 BUG_ON(ordered
->file_offset
!= file_pos
|| ordered
->num_bytes
!= len
);
4673 disk_bytenr
= file_pos
+ BTRFS_I(inode
)->index_cnt
;
4674 ret
= btrfs_lookup_csums_range(fs_info
->csum_root
, disk_bytenr
,
4675 disk_bytenr
+ len
- 1, &list
, 0);
4679 while (!list_empty(&list
)) {
4680 sums
= list_entry(list
.next
, struct btrfs_ordered_sum
, list
);
4681 list_del_init(&sums
->list
);
4684 * We need to offset the new_bytenr based on where the csum is.
4685 * We need to do this because we will read in entire prealloc
4686 * extents but we may have written to say the middle of the
4687 * prealloc extent, so we need to make sure the csum goes with
4688 * the right disk offset.
4690 * We can do this because the data reloc inode refers strictly
4691 * to the on disk bytes, so we don't have to worry about
4692 * disk_len vs real len like with real inodes since it's all
4695 new_bytenr
= ordered
->disk_bytenr
+ sums
->bytenr
- disk_bytenr
;
4696 sums
->bytenr
= new_bytenr
;
4698 btrfs_add_ordered_sum(ordered
, sums
);
4701 btrfs_put_ordered_extent(ordered
);
4705 int btrfs_reloc_cow_block(struct btrfs_trans_handle
*trans
,
4706 struct btrfs_root
*root
, struct extent_buffer
*buf
,
4707 struct extent_buffer
*cow
)
4709 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
4710 struct reloc_control
*rc
;
4711 struct backref_node
*node
;
4716 rc
= fs_info
->reloc_ctl
;
4720 BUG_ON(rc
->stage
== UPDATE_DATA_PTRS
&&
4721 root
->root_key
.objectid
== BTRFS_DATA_RELOC_TREE_OBJECTID
);
4723 if (root
->root_key
.objectid
== BTRFS_TREE_RELOC_OBJECTID
) {
4724 if (buf
== root
->node
)
4725 __update_reloc_root(root
, cow
->start
);
4728 level
= btrfs_header_level(buf
);
4729 if (btrfs_header_generation(buf
) <=
4730 btrfs_root_last_snapshot(&root
->root_item
))
4733 if (root
->root_key
.objectid
== BTRFS_TREE_RELOC_OBJECTID
&&
4734 rc
->create_reloc_tree
) {
4735 WARN_ON(!first_cow
&& level
== 0);
4737 node
= rc
->backref_cache
.path
[level
];
4738 BUG_ON(node
->bytenr
!= buf
->start
&&
4739 node
->new_bytenr
!= buf
->start
);
4741 drop_node_buffer(node
);
4742 atomic_inc(&cow
->refs
);
4744 node
->new_bytenr
= cow
->start
;
4746 if (!node
->pending
) {
4747 list_move_tail(&node
->list
,
4748 &rc
->backref_cache
.pending
[level
]);
4753 __mark_block_processed(rc
, node
);
4755 if (first_cow
&& level
> 0)
4756 rc
->nodes_relocated
+= buf
->len
;
4759 if (level
== 0 && first_cow
&& rc
->stage
== UPDATE_DATA_PTRS
)
4760 ret
= replace_file_extents(trans
, rc
, root
, cow
);
4765 * called before creating snapshot. it calculates metadata reservation
4766 * required for relocating tree blocks in the snapshot
4768 void btrfs_reloc_pre_snapshot(struct btrfs_pending_snapshot
*pending
,
4769 u64
*bytes_to_reserve
)
4771 struct btrfs_root
*root
= pending
->root
;
4772 struct reloc_control
*rc
= root
->fs_info
->reloc_ctl
;
4774 if (!rc
|| !have_reloc_root(root
))
4777 if (!rc
->merge_reloc_tree
)
4780 root
= root
->reloc_root
;
4781 BUG_ON(btrfs_root_refs(&root
->root_item
) == 0);
4783 * relocation is in the stage of merging trees. the space
4784 * used by merging a reloc tree is twice the size of
4785 * relocated tree nodes in the worst case. half for cowing
4786 * the reloc tree, half for cowing the fs tree. the space
4787 * used by cowing the reloc tree will be freed after the
4788 * tree is dropped. if we create snapshot, cowing the fs
4789 * tree may use more space than it frees. so we need
4790 * reserve extra space.
4792 *bytes_to_reserve
+= rc
->nodes_relocated
;
4796 * called after snapshot is created. migrate block reservation
4797 * and create reloc root for the newly created snapshot
4799 int btrfs_reloc_post_snapshot(struct btrfs_trans_handle
*trans
,
4800 struct btrfs_pending_snapshot
*pending
)
4802 struct btrfs_root
*root
= pending
->root
;
4803 struct btrfs_root
*reloc_root
;
4804 struct btrfs_root
*new_root
;
4805 struct reloc_control
*rc
= root
->fs_info
->reloc_ctl
;
4808 if (!rc
|| !have_reloc_root(root
))
4811 rc
= root
->fs_info
->reloc_ctl
;
4812 rc
->merging_rsv_size
+= rc
->nodes_relocated
;
4814 if (rc
->merge_reloc_tree
) {
4815 ret
= btrfs_block_rsv_migrate(&pending
->block_rsv
,
4817 rc
->nodes_relocated
, true);
4822 new_root
= pending
->snap
;
4823 reloc_root
= create_reloc_root(trans
, root
->reloc_root
,
4824 new_root
->root_key
.objectid
);
4825 if (IS_ERR(reloc_root
))
4826 return PTR_ERR(reloc_root
);
4828 ret
= __add_reloc_root(reloc_root
);
4830 new_root
->reloc_root
= reloc_root
;
4832 if (rc
->create_reloc_tree
)
4833 ret
= clone_backref_node(trans
, rc
, root
, reloc_root
);