2 * Copyright (C) 2009 Oracle. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
19 #include <linux/sched.h>
20 #include <linux/pagemap.h>
21 #include <linux/writeback.h>
22 #include <linux/blkdev.h>
23 #include <linux/rbtree.h>
24 #include <linux/slab.h>
27 #include "transaction.h"
30 #include "btrfs_inode.h"
31 #include "async-thread.h"
32 #include "free-space-cache.h"
35 * backref_node, mapping_node and tree_block start with this
38 struct rb_node rb_node
;
43 * present a tree block in the backref cache
46 struct rb_node rb_node
;
50 /* objectid of tree block owner, can be not uptodate */
52 /* link to pending, changed or detached list */
53 struct list_head list
;
54 /* list of upper level blocks reference this block */
55 struct list_head upper
;
56 /* list of child blocks in the cache */
57 struct list_head lower
;
58 /* NULL if this node is not tree root */
59 struct btrfs_root
*root
;
60 /* extent buffer got by COW the block */
61 struct extent_buffer
*eb
;
62 /* level of tree block */
64 /* is the block in non-reference counted tree */
65 unsigned int cowonly
:1;
66 /* 1 if no child node in the cache */
67 unsigned int lowest
:1;
68 /* is the extent buffer locked */
69 unsigned int locked
:1;
70 /* has the block been processed */
71 unsigned int processed
:1;
72 /* have backrefs of this block been checked */
73 unsigned int checked
:1;
75 * 1 if corresponding block has been cowed but some upper
76 * level block pointers may not point to the new location
78 unsigned int pending
:1;
80 * 1 if the backref node isn't connected to any other
83 unsigned int detached
:1;
87 * present a block pointer in the backref cache
90 struct list_head list
[2];
91 struct backref_node
*node
[2];
97 struct backref_cache
{
98 /* red black tree of all backref nodes in the cache */
99 struct rb_root rb_root
;
100 /* for passing backref nodes to btrfs_reloc_cow_block */
101 struct backref_node
*path
[BTRFS_MAX_LEVEL
];
103 * list of blocks that have been cowed but some block
104 * pointers in upper level blocks may not reflect the
107 struct list_head pending
[BTRFS_MAX_LEVEL
];
108 /* list of backref nodes with no child node */
109 struct list_head leaves
;
110 /* list of blocks that have been cowed in current transaction */
111 struct list_head changed
;
112 /* list of detached backref node. */
113 struct list_head detached
;
122 * map address of tree root to tree
124 struct mapping_node
{
125 struct rb_node rb_node
;
130 struct mapping_tree
{
131 struct rb_root rb_root
;
136 * present a tree block to process
139 struct rb_node rb_node
;
141 struct btrfs_key key
;
142 unsigned int level
:8;
143 unsigned int key_ready
:1;
146 #define MAX_EXTENTS 128
148 struct file_extent_cluster
{
151 u64 boundary
[MAX_EXTENTS
];
155 struct reloc_control
{
156 /* block group to relocate */
157 struct btrfs_block_group_cache
*block_group
;
159 struct btrfs_root
*extent_root
;
160 /* inode for moving data */
161 struct inode
*data_inode
;
163 struct btrfs_block_rsv
*block_rsv
;
165 struct backref_cache backref_cache
;
167 struct file_extent_cluster cluster
;
168 /* tree blocks have been processed */
169 struct extent_io_tree processed_blocks
;
170 /* map start of tree root to corresponding reloc tree */
171 struct mapping_tree reloc_root_tree
;
172 /* list of reloc trees */
173 struct list_head reloc_roots
;
174 /* size of metadata reservation for merging reloc trees */
175 u64 merging_rsv_size
;
176 /* size of relocated tree nodes */
182 unsigned int stage
:8;
183 unsigned int create_reloc_tree
:1;
184 unsigned int merge_reloc_tree
:1;
185 unsigned int found_file_extent
:1;
186 unsigned int commit_transaction
:1;
189 /* stages of data relocation */
190 #define MOVE_DATA_EXTENTS 0
191 #define UPDATE_DATA_PTRS 1
193 static void remove_backref_node(struct backref_cache
*cache
,
194 struct backref_node
*node
);
195 static void __mark_block_processed(struct reloc_control
*rc
,
196 struct backref_node
*node
);
198 static void mapping_tree_init(struct mapping_tree
*tree
)
200 tree
->rb_root
= RB_ROOT
;
201 spin_lock_init(&tree
->lock
);
204 static void backref_cache_init(struct backref_cache
*cache
)
207 cache
->rb_root
= RB_ROOT
;
208 for (i
= 0; i
< BTRFS_MAX_LEVEL
; i
++)
209 INIT_LIST_HEAD(&cache
->pending
[i
]);
210 INIT_LIST_HEAD(&cache
->changed
);
211 INIT_LIST_HEAD(&cache
->detached
);
212 INIT_LIST_HEAD(&cache
->leaves
);
215 static void backref_cache_cleanup(struct backref_cache
*cache
)
217 struct backref_node
*node
;
220 while (!list_empty(&cache
->detached
)) {
221 node
= list_entry(cache
->detached
.next
,
222 struct backref_node
, list
);
223 remove_backref_node(cache
, node
);
226 while (!list_empty(&cache
->leaves
)) {
227 node
= list_entry(cache
->leaves
.next
,
228 struct backref_node
, lower
);
229 remove_backref_node(cache
, node
);
232 cache
->last_trans
= 0;
234 for (i
= 0; i
< BTRFS_MAX_LEVEL
; i
++)
235 BUG_ON(!list_empty(&cache
->pending
[i
]));
236 BUG_ON(!list_empty(&cache
->changed
));
237 BUG_ON(!list_empty(&cache
->detached
));
238 BUG_ON(!RB_EMPTY_ROOT(&cache
->rb_root
));
239 BUG_ON(cache
->nr_nodes
);
240 BUG_ON(cache
->nr_edges
);
243 static struct backref_node
*alloc_backref_node(struct backref_cache
*cache
)
245 struct backref_node
*node
;
247 node
= kzalloc(sizeof(*node
), GFP_NOFS
);
249 INIT_LIST_HEAD(&node
->list
);
250 INIT_LIST_HEAD(&node
->upper
);
251 INIT_LIST_HEAD(&node
->lower
);
252 RB_CLEAR_NODE(&node
->rb_node
);
258 static void free_backref_node(struct backref_cache
*cache
,
259 struct backref_node
*node
)
267 static struct backref_edge
*alloc_backref_edge(struct backref_cache
*cache
)
269 struct backref_edge
*edge
;
271 edge
= kzalloc(sizeof(*edge
), GFP_NOFS
);
277 static void free_backref_edge(struct backref_cache
*cache
,
278 struct backref_edge
*edge
)
286 static struct rb_node
*tree_insert(struct rb_root
*root
, u64 bytenr
,
287 struct rb_node
*node
)
289 struct rb_node
**p
= &root
->rb_node
;
290 struct rb_node
*parent
= NULL
;
291 struct tree_entry
*entry
;
295 entry
= rb_entry(parent
, struct tree_entry
, rb_node
);
297 if (bytenr
< entry
->bytenr
)
299 else if (bytenr
> entry
->bytenr
)
305 rb_link_node(node
, parent
, p
);
306 rb_insert_color(node
, root
);
310 static struct rb_node
*tree_search(struct rb_root
*root
, u64 bytenr
)
312 struct rb_node
*n
= root
->rb_node
;
313 struct tree_entry
*entry
;
316 entry
= rb_entry(n
, struct tree_entry
, rb_node
);
318 if (bytenr
< entry
->bytenr
)
320 else if (bytenr
> entry
->bytenr
)
329 * walk up backref nodes until reach node presents tree root
331 static struct backref_node
*walk_up_backref(struct backref_node
*node
,
332 struct backref_edge
*edges
[],
335 struct backref_edge
*edge
;
338 while (!list_empty(&node
->upper
)) {
339 edge
= list_entry(node
->upper
.next
,
340 struct backref_edge
, list
[LOWER
]);
342 node
= edge
->node
[UPPER
];
344 BUG_ON(node
->detached
);
350 * walk down backref nodes to find start of next reference path
352 static struct backref_node
*walk_down_backref(struct backref_edge
*edges
[],
355 struct backref_edge
*edge
;
356 struct backref_node
*lower
;
360 edge
= edges
[idx
- 1];
361 lower
= edge
->node
[LOWER
];
362 if (list_is_last(&edge
->list
[LOWER
], &lower
->upper
)) {
366 edge
= list_entry(edge
->list
[LOWER
].next
,
367 struct backref_edge
, list
[LOWER
]);
368 edges
[idx
- 1] = edge
;
370 return edge
->node
[UPPER
];
376 static void unlock_node_buffer(struct backref_node
*node
)
379 btrfs_tree_unlock(node
->eb
);
384 static void drop_node_buffer(struct backref_node
*node
)
387 unlock_node_buffer(node
);
388 free_extent_buffer(node
->eb
);
393 static void drop_backref_node(struct backref_cache
*tree
,
394 struct backref_node
*node
)
396 BUG_ON(!list_empty(&node
->upper
));
398 drop_node_buffer(node
);
399 list_del(&node
->list
);
400 list_del(&node
->lower
);
401 if (!RB_EMPTY_NODE(&node
->rb_node
))
402 rb_erase(&node
->rb_node
, &tree
->rb_root
);
403 free_backref_node(tree
, node
);
407 * remove a backref node from the backref cache
409 static void remove_backref_node(struct backref_cache
*cache
,
410 struct backref_node
*node
)
412 struct backref_node
*upper
;
413 struct backref_edge
*edge
;
418 BUG_ON(!node
->lowest
&& !node
->detached
);
419 while (!list_empty(&node
->upper
)) {
420 edge
= list_entry(node
->upper
.next
, struct backref_edge
,
422 upper
= edge
->node
[UPPER
];
423 list_del(&edge
->list
[LOWER
]);
424 list_del(&edge
->list
[UPPER
]);
425 free_backref_edge(cache
, edge
);
427 if (RB_EMPTY_NODE(&upper
->rb_node
)) {
428 BUG_ON(!list_empty(&node
->upper
));
429 drop_backref_node(cache
, node
);
435 * add the node to leaf node list if no other
436 * child block cached.
438 if (list_empty(&upper
->lower
)) {
439 list_add_tail(&upper
->lower
, &cache
->leaves
);
444 drop_backref_node(cache
, node
);
447 static void update_backref_node(struct backref_cache
*cache
,
448 struct backref_node
*node
, u64 bytenr
)
450 struct rb_node
*rb_node
;
451 rb_erase(&node
->rb_node
, &cache
->rb_root
);
452 node
->bytenr
= bytenr
;
453 rb_node
= tree_insert(&cache
->rb_root
, node
->bytenr
, &node
->rb_node
);
458 * update backref cache after a transaction commit
460 static int update_backref_cache(struct btrfs_trans_handle
*trans
,
461 struct backref_cache
*cache
)
463 struct backref_node
*node
;
466 if (cache
->last_trans
== 0) {
467 cache
->last_trans
= trans
->transid
;
471 if (cache
->last_trans
== trans
->transid
)
475 * detached nodes are used to avoid unnecessary backref
476 * lookup. transaction commit changes the extent tree.
477 * so the detached nodes are no longer useful.
479 while (!list_empty(&cache
->detached
)) {
480 node
= list_entry(cache
->detached
.next
,
481 struct backref_node
, list
);
482 remove_backref_node(cache
, node
);
485 while (!list_empty(&cache
->changed
)) {
486 node
= list_entry(cache
->changed
.next
,
487 struct backref_node
, list
);
488 list_del_init(&node
->list
);
489 BUG_ON(node
->pending
);
490 update_backref_node(cache
, node
, node
->new_bytenr
);
494 * some nodes can be left in the pending list if there were
495 * errors during processing the pending nodes.
497 for (level
= 0; level
< BTRFS_MAX_LEVEL
; level
++) {
498 list_for_each_entry(node
, &cache
->pending
[level
], list
) {
499 BUG_ON(!node
->pending
);
500 if (node
->bytenr
== node
->new_bytenr
)
502 update_backref_node(cache
, node
, node
->new_bytenr
);
506 cache
->last_trans
= 0;
510 static int should_ignore_root(struct btrfs_root
*root
)
512 struct btrfs_root
*reloc_root
;
517 reloc_root
= root
->reloc_root
;
521 if (btrfs_root_last_snapshot(&reloc_root
->root_item
) ==
522 root
->fs_info
->running_transaction
->transid
- 1)
525 * if there is reloc tree and it was created in previous
526 * transaction backref lookup can find the reloc tree,
527 * so backref node for the fs tree root is useless for
534 * find reloc tree by address of tree root
536 static struct btrfs_root
*find_reloc_root(struct reloc_control
*rc
,
539 struct rb_node
*rb_node
;
540 struct mapping_node
*node
;
541 struct btrfs_root
*root
= NULL
;
543 spin_lock(&rc
->reloc_root_tree
.lock
);
544 rb_node
= tree_search(&rc
->reloc_root_tree
.rb_root
, bytenr
);
546 node
= rb_entry(rb_node
, struct mapping_node
, rb_node
);
547 root
= (struct btrfs_root
*)node
->data
;
549 spin_unlock(&rc
->reloc_root_tree
.lock
);
553 static int is_cowonly_root(u64 root_objectid
)
555 if (root_objectid
== BTRFS_ROOT_TREE_OBJECTID
||
556 root_objectid
== BTRFS_EXTENT_TREE_OBJECTID
||
557 root_objectid
== BTRFS_CHUNK_TREE_OBJECTID
||
558 root_objectid
== BTRFS_DEV_TREE_OBJECTID
||
559 root_objectid
== BTRFS_TREE_LOG_OBJECTID
||
560 root_objectid
== BTRFS_CSUM_TREE_OBJECTID
)
565 static struct btrfs_root
*read_fs_root(struct btrfs_fs_info
*fs_info
,
568 struct btrfs_key key
;
570 key
.objectid
= root_objectid
;
571 key
.type
= BTRFS_ROOT_ITEM_KEY
;
572 if (is_cowonly_root(root_objectid
))
575 key
.offset
= (u64
)-1;
577 return btrfs_read_fs_root_no_name(fs_info
, &key
);
580 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
581 static noinline_for_stack
582 struct btrfs_root
*find_tree_root(struct reloc_control
*rc
,
583 struct extent_buffer
*leaf
,
584 struct btrfs_extent_ref_v0
*ref0
)
586 struct btrfs_root
*root
;
587 u64 root_objectid
= btrfs_ref_root_v0(leaf
, ref0
);
588 u64 generation
= btrfs_ref_generation_v0(leaf
, ref0
);
590 BUG_ON(root_objectid
== BTRFS_TREE_RELOC_OBJECTID
);
592 root
= read_fs_root(rc
->extent_root
->fs_info
, root_objectid
);
593 BUG_ON(IS_ERR(root
));
595 if (root
->ref_cows
&&
596 generation
!= btrfs_root_generation(&root
->root_item
))
603 static noinline_for_stack
604 int find_inline_backref(struct extent_buffer
*leaf
, int slot
,
605 unsigned long *ptr
, unsigned long *end
)
607 struct btrfs_extent_item
*ei
;
608 struct btrfs_tree_block_info
*bi
;
611 item_size
= btrfs_item_size_nr(leaf
, slot
);
612 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
613 if (item_size
< sizeof(*ei
)) {
614 WARN_ON(item_size
!= sizeof(struct btrfs_extent_item_v0
));
618 ei
= btrfs_item_ptr(leaf
, slot
, struct btrfs_extent_item
);
619 WARN_ON(!(btrfs_extent_flags(leaf
, ei
) &
620 BTRFS_EXTENT_FLAG_TREE_BLOCK
));
622 if (item_size
<= sizeof(*ei
) + sizeof(*bi
)) {
623 WARN_ON(item_size
< sizeof(*ei
) + sizeof(*bi
));
627 bi
= (struct btrfs_tree_block_info
*)(ei
+ 1);
628 *ptr
= (unsigned long)(bi
+ 1);
629 *end
= (unsigned long)ei
+ item_size
;
634 * build backref tree for a given tree block. root of the backref tree
635 * corresponds the tree block, leaves of the backref tree correspond
636 * roots of b-trees that reference the tree block.
638 * the basic idea of this function is check backrefs of a given block
639 * to find upper level blocks that refernece the block, and then check
640 * bakcrefs of these upper level blocks recursively. the recursion stop
641 * when tree root is reached or backrefs for the block is cached.
643 * NOTE: if we find backrefs for a block are cached, we know backrefs
644 * for all upper level blocks that directly/indirectly reference the
645 * block are also cached.
647 static noinline_for_stack
648 struct backref_node
*build_backref_tree(struct reloc_control
*rc
,
649 struct btrfs_key
*node_key
,
650 int level
, u64 bytenr
)
652 struct backref_cache
*cache
= &rc
->backref_cache
;
653 struct btrfs_path
*path1
;
654 struct btrfs_path
*path2
;
655 struct extent_buffer
*eb
;
656 struct btrfs_root
*root
;
657 struct backref_node
*cur
;
658 struct backref_node
*upper
;
659 struct backref_node
*lower
;
660 struct backref_node
*node
= NULL
;
661 struct backref_node
*exist
= NULL
;
662 struct backref_edge
*edge
;
663 struct rb_node
*rb_node
;
664 struct btrfs_key key
;
673 path1
= btrfs_alloc_path();
674 path2
= btrfs_alloc_path();
675 if (!path1
|| !path2
) {
680 node
= alloc_backref_node(cache
);
686 node
->bytenr
= bytenr
;
693 key
.objectid
= cur
->bytenr
;
694 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
695 key
.offset
= (u64
)-1;
697 path1
->search_commit_root
= 1;
698 path1
->skip_locking
= 1;
699 ret
= btrfs_search_slot(NULL
, rc
->extent_root
, &key
, path1
,
705 BUG_ON(!ret
|| !path1
->slots
[0]);
709 WARN_ON(cur
->checked
);
710 if (!list_empty(&cur
->upper
)) {
712 * the backref was added previously when processsing
713 * backref of type BTRFS_TREE_BLOCK_REF_KEY
715 BUG_ON(!list_is_singular(&cur
->upper
));
716 edge
= list_entry(cur
->upper
.next
, struct backref_edge
,
718 BUG_ON(!list_empty(&edge
->list
[UPPER
]));
719 exist
= edge
->node
[UPPER
];
721 * add the upper level block to pending list if we need
725 list_add_tail(&edge
->list
[UPPER
], &list
);
732 eb
= path1
->nodes
[0];
735 if (path1
->slots
[0] >= btrfs_header_nritems(eb
)) {
736 ret
= btrfs_next_leaf(rc
->extent_root
, path1
);
743 eb
= path1
->nodes
[0];
746 btrfs_item_key_to_cpu(eb
, &key
, path1
->slots
[0]);
747 if (key
.objectid
!= cur
->bytenr
) {
752 if (key
.type
== BTRFS_EXTENT_ITEM_KEY
) {
753 ret
= find_inline_backref(eb
, path1
->slots
[0],
761 /* update key for inline back ref */
762 struct btrfs_extent_inline_ref
*iref
;
763 iref
= (struct btrfs_extent_inline_ref
*)ptr
;
764 key
.type
= btrfs_extent_inline_ref_type(eb
, iref
);
765 key
.offset
= btrfs_extent_inline_ref_offset(eb
, iref
);
766 WARN_ON(key
.type
!= BTRFS_TREE_BLOCK_REF_KEY
&&
767 key
.type
!= BTRFS_SHARED_BLOCK_REF_KEY
);
771 ((key
.type
== BTRFS_TREE_BLOCK_REF_KEY
&&
772 exist
->owner
== key
.offset
) ||
773 (key
.type
== BTRFS_SHARED_BLOCK_REF_KEY
&&
774 exist
->bytenr
== key
.offset
))) {
779 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
780 if (key
.type
== BTRFS_SHARED_BLOCK_REF_KEY
||
781 key
.type
== BTRFS_EXTENT_REF_V0_KEY
) {
782 if (key
.type
== BTRFS_EXTENT_REF_V0_KEY
) {
783 struct btrfs_extent_ref_v0
*ref0
;
784 ref0
= btrfs_item_ptr(eb
, path1
->slots
[0],
785 struct btrfs_extent_ref_v0
);
786 if (key
.objectid
== key
.offset
) {
787 root
= find_tree_root(rc
, eb
, ref0
);
788 if (root
&& !should_ignore_root(root
))
791 list_add(&cur
->list
, &useless
);
794 if (is_cowonly_root(btrfs_ref_root_v0(eb
,
799 BUG_ON(key
.type
== BTRFS_EXTENT_REF_V0_KEY
);
800 if (key
.type
== BTRFS_SHARED_BLOCK_REF_KEY
) {
802 if (key
.objectid
== key
.offset
) {
804 * only root blocks of reloc trees use
805 * backref of this type.
807 root
= find_reloc_root(rc
, cur
->bytenr
);
813 edge
= alloc_backref_edge(cache
);
818 rb_node
= tree_search(&cache
->rb_root
, key
.offset
);
820 upper
= alloc_backref_node(cache
);
822 free_backref_edge(cache
, edge
);
826 upper
->bytenr
= key
.offset
;
827 upper
->level
= cur
->level
+ 1;
829 * backrefs for the upper level block isn't
830 * cached, add the block to pending list
832 list_add_tail(&edge
->list
[UPPER
], &list
);
834 upper
= rb_entry(rb_node
, struct backref_node
,
836 BUG_ON(!upper
->checked
);
837 INIT_LIST_HEAD(&edge
->list
[UPPER
]);
839 list_add_tail(&edge
->list
[LOWER
], &cur
->upper
);
840 edge
->node
[LOWER
] = cur
;
841 edge
->node
[UPPER
] = upper
;
844 } else if (key
.type
!= BTRFS_TREE_BLOCK_REF_KEY
) {
848 /* key.type == BTRFS_TREE_BLOCK_REF_KEY */
849 root
= read_fs_root(rc
->extent_root
->fs_info
, key
.offset
);
858 if (btrfs_root_level(&root
->root_item
) == cur
->level
) {
860 BUG_ON(btrfs_root_bytenr(&root
->root_item
) !=
862 if (should_ignore_root(root
))
863 list_add(&cur
->list
, &useless
);
869 level
= cur
->level
+ 1;
872 * searching the tree to find upper level blocks
873 * reference the block.
875 path2
->search_commit_root
= 1;
876 path2
->skip_locking
= 1;
877 path2
->lowest_level
= level
;
878 ret
= btrfs_search_slot(NULL
, root
, node_key
, path2
, 0, 0);
879 path2
->lowest_level
= 0;
884 if (ret
> 0 && path2
->slots
[level
] > 0)
885 path2
->slots
[level
]--;
887 eb
= path2
->nodes
[level
];
888 WARN_ON(btrfs_node_blockptr(eb
, path2
->slots
[level
]) !=
892 for (; level
< BTRFS_MAX_LEVEL
; level
++) {
893 if (!path2
->nodes
[level
]) {
894 BUG_ON(btrfs_root_bytenr(&root
->root_item
) !=
896 if (should_ignore_root(root
))
897 list_add(&lower
->list
, &useless
);
903 edge
= alloc_backref_edge(cache
);
909 eb
= path2
->nodes
[level
];
910 rb_node
= tree_search(&cache
->rb_root
, eb
->start
);
912 upper
= alloc_backref_node(cache
);
914 free_backref_edge(cache
, edge
);
918 upper
->bytenr
= eb
->start
;
919 upper
->owner
= btrfs_header_owner(eb
);
920 upper
->level
= lower
->level
+ 1;
925 * if we know the block isn't shared
926 * we can void checking its backrefs.
928 if (btrfs_block_can_be_shared(root
, eb
))
934 * add the block to pending list if we
935 * need check its backrefs. only block
936 * at 'cur->level + 1' is added to the
937 * tail of pending list. this guarantees
938 * we check backrefs from lower level
939 * blocks to upper level blocks.
941 if (!upper
->checked
&&
942 level
== cur
->level
+ 1) {
943 list_add_tail(&edge
->list
[UPPER
],
946 INIT_LIST_HEAD(&edge
->list
[UPPER
]);
948 upper
= rb_entry(rb_node
, struct backref_node
,
950 BUG_ON(!upper
->checked
);
951 INIT_LIST_HEAD(&edge
->list
[UPPER
]);
953 upper
->owner
= btrfs_header_owner(eb
);
955 list_add_tail(&edge
->list
[LOWER
], &lower
->upper
);
956 edge
->node
[LOWER
] = lower
;
957 edge
->node
[UPPER
] = upper
;
964 btrfs_release_path(root
, path2
);
967 ptr
+= btrfs_extent_inline_ref_size(key
.type
);
977 btrfs_release_path(rc
->extent_root
, path1
);
982 /* the pending list isn't empty, take the first block to process */
983 if (!list_empty(&list
)) {
984 edge
= list_entry(list
.next
, struct backref_edge
, list
[UPPER
]);
985 list_del_init(&edge
->list
[UPPER
]);
986 cur
= edge
->node
[UPPER
];
991 * everything goes well, connect backref nodes and insert backref nodes
994 BUG_ON(!node
->checked
);
995 cowonly
= node
->cowonly
;
997 rb_node
= tree_insert(&cache
->rb_root
, node
->bytenr
,
1000 list_add_tail(&node
->lower
, &cache
->leaves
);
1003 list_for_each_entry(edge
, &node
->upper
, list
[LOWER
])
1004 list_add_tail(&edge
->list
[UPPER
], &list
);
1006 while (!list_empty(&list
)) {
1007 edge
= list_entry(list
.next
, struct backref_edge
, list
[UPPER
]);
1008 list_del_init(&edge
->list
[UPPER
]);
1009 upper
= edge
->node
[UPPER
];
1010 if (upper
->detached
) {
1011 list_del(&edge
->list
[LOWER
]);
1012 lower
= edge
->node
[LOWER
];
1013 free_backref_edge(cache
, edge
);
1014 if (list_empty(&lower
->upper
))
1015 list_add(&lower
->list
, &useless
);
1019 if (!RB_EMPTY_NODE(&upper
->rb_node
)) {
1020 if (upper
->lowest
) {
1021 list_del_init(&upper
->lower
);
1025 list_add_tail(&edge
->list
[UPPER
], &upper
->lower
);
1029 BUG_ON(!upper
->checked
);
1030 BUG_ON(cowonly
!= upper
->cowonly
);
1032 rb_node
= tree_insert(&cache
->rb_root
, upper
->bytenr
,
1037 list_add_tail(&edge
->list
[UPPER
], &upper
->lower
);
1039 list_for_each_entry(edge
, &upper
->upper
, list
[LOWER
])
1040 list_add_tail(&edge
->list
[UPPER
], &list
);
1043 * process useless backref nodes. backref nodes for tree leaves
1044 * are deleted from the cache. backref nodes for upper level
1045 * tree blocks are left in the cache to avoid unnecessary backref
1048 while (!list_empty(&useless
)) {
1049 upper
= list_entry(useless
.next
, struct backref_node
, list
);
1050 list_del_init(&upper
->list
);
1051 BUG_ON(!list_empty(&upper
->upper
));
1054 if (upper
->lowest
) {
1055 list_del_init(&upper
->lower
);
1058 while (!list_empty(&upper
->lower
)) {
1059 edge
= list_entry(upper
->lower
.next
,
1060 struct backref_edge
, list
[UPPER
]);
1061 list_del(&edge
->list
[UPPER
]);
1062 list_del(&edge
->list
[LOWER
]);
1063 lower
= edge
->node
[LOWER
];
1064 free_backref_edge(cache
, edge
);
1066 if (list_empty(&lower
->upper
))
1067 list_add(&lower
->list
, &useless
);
1069 __mark_block_processed(rc
, upper
);
1070 if (upper
->level
> 0) {
1071 list_add(&upper
->list
, &cache
->detached
);
1072 upper
->detached
= 1;
1074 rb_erase(&upper
->rb_node
, &cache
->rb_root
);
1075 free_backref_node(cache
, upper
);
1079 btrfs_free_path(path1
);
1080 btrfs_free_path(path2
);
1082 while (!list_empty(&useless
)) {
1083 lower
= list_entry(useless
.next
,
1084 struct backref_node
, upper
);
1085 list_del_init(&lower
->upper
);
1088 INIT_LIST_HEAD(&list
);
1090 if (RB_EMPTY_NODE(&upper
->rb_node
)) {
1091 list_splice_tail(&upper
->upper
, &list
);
1092 free_backref_node(cache
, upper
);
1095 if (list_empty(&list
))
1098 edge
= list_entry(list
.next
, struct backref_edge
,
1100 list_del(&edge
->list
[LOWER
]);
1101 upper
= edge
->node
[UPPER
];
1102 free_backref_edge(cache
, edge
);
1104 return ERR_PTR(err
);
1106 BUG_ON(node
&& node
->detached
);
1111 * helper to add backref node for the newly created snapshot.
1112 * the backref node is created by cloning backref node that
1113 * corresponds to root of source tree
1115 static int clone_backref_node(struct btrfs_trans_handle
*trans
,
1116 struct reloc_control
*rc
,
1117 struct btrfs_root
*src
,
1118 struct btrfs_root
*dest
)
1120 struct btrfs_root
*reloc_root
= src
->reloc_root
;
1121 struct backref_cache
*cache
= &rc
->backref_cache
;
1122 struct backref_node
*node
= NULL
;
1123 struct backref_node
*new_node
;
1124 struct backref_edge
*edge
;
1125 struct backref_edge
*new_edge
;
1126 struct rb_node
*rb_node
;
1128 if (cache
->last_trans
> 0)
1129 update_backref_cache(trans
, cache
);
1131 rb_node
= tree_search(&cache
->rb_root
, src
->commit_root
->start
);
1133 node
= rb_entry(rb_node
, struct backref_node
, rb_node
);
1137 BUG_ON(node
->new_bytenr
!= reloc_root
->node
->start
);
1141 rb_node
= tree_search(&cache
->rb_root
,
1142 reloc_root
->commit_root
->start
);
1144 node
= rb_entry(rb_node
, struct backref_node
,
1146 BUG_ON(node
->detached
);
1153 new_node
= alloc_backref_node(cache
);
1157 new_node
->bytenr
= dest
->node
->start
;
1158 new_node
->level
= node
->level
;
1159 new_node
->lowest
= node
->lowest
;
1160 new_node
->checked
= 1;
1161 new_node
->root
= dest
;
1163 if (!node
->lowest
) {
1164 list_for_each_entry(edge
, &node
->lower
, list
[UPPER
]) {
1165 new_edge
= alloc_backref_edge(cache
);
1169 new_edge
->node
[UPPER
] = new_node
;
1170 new_edge
->node
[LOWER
] = edge
->node
[LOWER
];
1171 list_add_tail(&new_edge
->list
[UPPER
],
1176 rb_node
= tree_insert(&cache
->rb_root
, new_node
->bytenr
,
1177 &new_node
->rb_node
);
1180 if (!new_node
->lowest
) {
1181 list_for_each_entry(new_edge
, &new_node
->lower
, list
[UPPER
]) {
1182 list_add_tail(&new_edge
->list
[LOWER
],
1183 &new_edge
->node
[LOWER
]->upper
);
1188 while (!list_empty(&new_node
->lower
)) {
1189 new_edge
= list_entry(new_node
->lower
.next
,
1190 struct backref_edge
, list
[UPPER
]);
1191 list_del(&new_edge
->list
[UPPER
]);
1192 free_backref_edge(cache
, new_edge
);
1194 free_backref_node(cache
, new_node
);
1199 * helper to add 'address of tree root -> reloc tree' mapping
1201 static int __add_reloc_root(struct btrfs_root
*root
)
1203 struct rb_node
*rb_node
;
1204 struct mapping_node
*node
;
1205 struct reloc_control
*rc
= root
->fs_info
->reloc_ctl
;
1207 node
= kmalloc(sizeof(*node
), GFP_NOFS
);
1210 node
->bytenr
= root
->node
->start
;
1213 spin_lock(&rc
->reloc_root_tree
.lock
);
1214 rb_node
= tree_insert(&rc
->reloc_root_tree
.rb_root
,
1215 node
->bytenr
, &node
->rb_node
);
1216 spin_unlock(&rc
->reloc_root_tree
.lock
);
1219 list_add_tail(&root
->root_list
, &rc
->reloc_roots
);
1224 * helper to update/delete the 'address of tree root -> reloc tree'
1227 static int __update_reloc_root(struct btrfs_root
*root
, int del
)
1229 struct rb_node
*rb_node
;
1230 struct mapping_node
*node
= NULL
;
1231 struct reloc_control
*rc
= root
->fs_info
->reloc_ctl
;
1233 spin_lock(&rc
->reloc_root_tree
.lock
);
1234 rb_node
= tree_search(&rc
->reloc_root_tree
.rb_root
,
1235 root
->commit_root
->start
);
1237 node
= rb_entry(rb_node
, struct mapping_node
, rb_node
);
1238 rb_erase(&node
->rb_node
, &rc
->reloc_root_tree
.rb_root
);
1240 spin_unlock(&rc
->reloc_root_tree
.lock
);
1242 BUG_ON((struct btrfs_root
*)node
->data
!= root
);
1245 spin_lock(&rc
->reloc_root_tree
.lock
);
1246 node
->bytenr
= root
->node
->start
;
1247 rb_node
= tree_insert(&rc
->reloc_root_tree
.rb_root
,
1248 node
->bytenr
, &node
->rb_node
);
1249 spin_unlock(&rc
->reloc_root_tree
.lock
);
1252 list_del_init(&root
->root_list
);
1258 static struct btrfs_root
*create_reloc_root(struct btrfs_trans_handle
*trans
,
1259 struct btrfs_root
*root
, u64 objectid
)
1261 struct btrfs_root
*reloc_root
;
1262 struct extent_buffer
*eb
;
1263 struct btrfs_root_item
*root_item
;
1264 struct btrfs_key root_key
;
1267 root_item
= kmalloc(sizeof(*root_item
), GFP_NOFS
);
1270 root_key
.objectid
= BTRFS_TREE_RELOC_OBJECTID
;
1271 root_key
.type
= BTRFS_ROOT_ITEM_KEY
;
1272 root_key
.offset
= objectid
;
1274 if (root
->root_key
.objectid
== objectid
) {
1275 /* called by btrfs_init_reloc_root */
1276 ret
= btrfs_copy_root(trans
, root
, root
->commit_root
, &eb
,
1277 BTRFS_TREE_RELOC_OBJECTID
);
1280 btrfs_set_root_last_snapshot(&root
->root_item
,
1281 trans
->transid
- 1);
1284 * called by btrfs_reloc_post_snapshot_hook.
1285 * the source tree is a reloc tree, all tree blocks
1286 * modified after it was created have RELOC flag
1287 * set in their headers. so it's OK to not update
1288 * the 'last_snapshot'.
1290 ret
= btrfs_copy_root(trans
, root
, root
->node
, &eb
,
1291 BTRFS_TREE_RELOC_OBJECTID
);
1295 memcpy(root_item
, &root
->root_item
, sizeof(*root_item
));
1296 btrfs_set_root_bytenr(root_item
, eb
->start
);
1297 btrfs_set_root_level(root_item
, btrfs_header_level(eb
));
1298 btrfs_set_root_generation(root_item
, trans
->transid
);
1300 if (root
->root_key
.objectid
== objectid
) {
1301 btrfs_set_root_refs(root_item
, 0);
1302 memset(&root_item
->drop_progress
, 0,
1303 sizeof(struct btrfs_disk_key
));
1304 root_item
->drop_level
= 0;
1307 btrfs_tree_unlock(eb
);
1308 free_extent_buffer(eb
);
1310 ret
= btrfs_insert_root(trans
, root
->fs_info
->tree_root
,
1311 &root_key
, root_item
);
1315 reloc_root
= btrfs_read_fs_root_no_radix(root
->fs_info
->tree_root
,
1317 BUG_ON(IS_ERR(reloc_root
));
1318 reloc_root
->last_trans
= trans
->transid
;
1323 * create reloc tree for a given fs tree. reloc tree is just a
1324 * snapshot of the fs tree with special root objectid.
1326 int btrfs_init_reloc_root(struct btrfs_trans_handle
*trans
,
1327 struct btrfs_root
*root
)
1329 struct btrfs_root
*reloc_root
;
1330 struct reloc_control
*rc
= root
->fs_info
->reloc_ctl
;
1333 if (root
->reloc_root
) {
1334 reloc_root
= root
->reloc_root
;
1335 reloc_root
->last_trans
= trans
->transid
;
1339 if (!rc
|| !rc
->create_reloc_tree
||
1340 root
->root_key
.objectid
== BTRFS_TREE_RELOC_OBJECTID
)
1343 if (!trans
->block_rsv
) {
1344 trans
->block_rsv
= rc
->block_rsv
;
1347 reloc_root
= create_reloc_root(trans
, root
, root
->root_key
.objectid
);
1349 trans
->block_rsv
= NULL
;
1351 __add_reloc_root(reloc_root
);
1352 root
->reloc_root
= reloc_root
;
1357 * update root item of reloc tree
1359 int btrfs_update_reloc_root(struct btrfs_trans_handle
*trans
,
1360 struct btrfs_root
*root
)
1362 struct btrfs_root
*reloc_root
;
1363 struct btrfs_root_item
*root_item
;
1367 if (!root
->reloc_root
)
1370 reloc_root
= root
->reloc_root
;
1371 root_item
= &reloc_root
->root_item
;
1373 if (root
->fs_info
->reloc_ctl
->merge_reloc_tree
&&
1374 btrfs_root_refs(root_item
) == 0) {
1375 root
->reloc_root
= NULL
;
1379 __update_reloc_root(reloc_root
, del
);
1381 if (reloc_root
->commit_root
!= reloc_root
->node
) {
1382 btrfs_set_root_node(root_item
, reloc_root
->node
);
1383 free_extent_buffer(reloc_root
->commit_root
);
1384 reloc_root
->commit_root
= btrfs_root_node(reloc_root
);
1387 ret
= btrfs_update_root(trans
, root
->fs_info
->tree_root
,
1388 &reloc_root
->root_key
, root_item
);
1394 * helper to find first cached inode with inode number >= objectid
1397 static struct inode
*find_next_inode(struct btrfs_root
*root
, u64 objectid
)
1399 struct rb_node
*node
;
1400 struct rb_node
*prev
;
1401 struct btrfs_inode
*entry
;
1402 struct inode
*inode
;
1404 spin_lock(&root
->inode_lock
);
1406 node
= root
->inode_tree
.rb_node
;
1410 entry
= rb_entry(node
, struct btrfs_inode
, rb_node
);
1412 if (objectid
< entry
->vfs_inode
.i_ino
)
1413 node
= node
->rb_left
;
1414 else if (objectid
> entry
->vfs_inode
.i_ino
)
1415 node
= node
->rb_right
;
1421 entry
= rb_entry(prev
, struct btrfs_inode
, rb_node
);
1422 if (objectid
<= entry
->vfs_inode
.i_ino
) {
1426 prev
= rb_next(prev
);
1430 entry
= rb_entry(node
, struct btrfs_inode
, rb_node
);
1431 inode
= igrab(&entry
->vfs_inode
);
1433 spin_unlock(&root
->inode_lock
);
1437 objectid
= entry
->vfs_inode
.i_ino
+ 1;
1438 if (cond_resched_lock(&root
->inode_lock
))
1441 node
= rb_next(node
);
1443 spin_unlock(&root
->inode_lock
);
1447 static int in_block_group(u64 bytenr
,
1448 struct btrfs_block_group_cache
*block_group
)
1450 if (bytenr
>= block_group
->key
.objectid
&&
1451 bytenr
< block_group
->key
.objectid
+ block_group
->key
.offset
)
1457 * get new location of data
1459 static int get_new_location(struct inode
*reloc_inode
, u64
*new_bytenr
,
1460 u64 bytenr
, u64 num_bytes
)
1462 struct btrfs_root
*root
= BTRFS_I(reloc_inode
)->root
;
1463 struct btrfs_path
*path
;
1464 struct btrfs_file_extent_item
*fi
;
1465 struct extent_buffer
*leaf
;
1468 path
= btrfs_alloc_path();
1472 bytenr
-= BTRFS_I(reloc_inode
)->index_cnt
;
1473 ret
= btrfs_lookup_file_extent(NULL
, root
, path
, reloc_inode
->i_ino
,
1482 leaf
= path
->nodes
[0];
1483 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
1484 struct btrfs_file_extent_item
);
1486 BUG_ON(btrfs_file_extent_offset(leaf
, fi
) ||
1487 btrfs_file_extent_compression(leaf
, fi
) ||
1488 btrfs_file_extent_encryption(leaf
, fi
) ||
1489 btrfs_file_extent_other_encoding(leaf
, fi
));
1491 if (num_bytes
!= btrfs_file_extent_disk_num_bytes(leaf
, fi
)) {
1496 *new_bytenr
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
1499 btrfs_free_path(path
);
1504 * update file extent items in the tree leaf to point to
1505 * the new locations.
1507 static noinline_for_stack
1508 int replace_file_extents(struct btrfs_trans_handle
*trans
,
1509 struct reloc_control
*rc
,
1510 struct btrfs_root
*root
,
1511 struct extent_buffer
*leaf
)
1513 struct btrfs_key key
;
1514 struct btrfs_file_extent_item
*fi
;
1515 struct inode
*inode
= NULL
;
1527 if (rc
->stage
!= UPDATE_DATA_PTRS
)
1530 /* reloc trees always use full backref */
1531 if (root
->root_key
.objectid
== BTRFS_TREE_RELOC_OBJECTID
)
1532 parent
= leaf
->start
;
1536 nritems
= btrfs_header_nritems(leaf
);
1537 for (i
= 0; i
< nritems
; i
++) {
1539 btrfs_item_key_to_cpu(leaf
, &key
, i
);
1540 if (key
.type
!= BTRFS_EXTENT_DATA_KEY
)
1542 fi
= btrfs_item_ptr(leaf
, i
, struct btrfs_file_extent_item
);
1543 if (btrfs_file_extent_type(leaf
, fi
) ==
1544 BTRFS_FILE_EXTENT_INLINE
)
1546 bytenr
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
1547 num_bytes
= btrfs_file_extent_disk_num_bytes(leaf
, fi
);
1550 if (!in_block_group(bytenr
, rc
->block_group
))
1554 * if we are modifying block in fs tree, wait for readpage
1555 * to complete and drop the extent cache
1557 if (root
->root_key
.objectid
!= BTRFS_TREE_RELOC_OBJECTID
) {
1559 inode
= find_next_inode(root
, key
.objectid
);
1561 } else if (inode
&& inode
->i_ino
< key
.objectid
) {
1562 btrfs_add_delayed_iput(inode
);
1563 inode
= find_next_inode(root
, key
.objectid
);
1565 if (inode
&& inode
->i_ino
== key
.objectid
) {
1567 btrfs_file_extent_num_bytes(leaf
, fi
);
1568 WARN_ON(!IS_ALIGNED(key
.offset
,
1570 WARN_ON(!IS_ALIGNED(end
, root
->sectorsize
));
1572 ret
= try_lock_extent(&BTRFS_I(inode
)->io_tree
,
1578 btrfs_drop_extent_cache(inode
, key
.offset
, end
,
1580 unlock_extent(&BTRFS_I(inode
)->io_tree
,
1581 key
.offset
, end
, GFP_NOFS
);
1585 ret
= get_new_location(rc
->data_inode
, &new_bytenr
,
1593 btrfs_set_file_extent_disk_bytenr(leaf
, fi
, new_bytenr
);
1596 key
.offset
-= btrfs_file_extent_offset(leaf
, fi
);
1597 ret
= btrfs_inc_extent_ref(trans
, root
, new_bytenr
,
1599 btrfs_header_owner(leaf
),
1600 key
.objectid
, key
.offset
);
1603 ret
= btrfs_free_extent(trans
, root
, bytenr
, num_bytes
,
1604 parent
, btrfs_header_owner(leaf
),
1605 key
.objectid
, key
.offset
);
1609 btrfs_mark_buffer_dirty(leaf
);
1611 btrfs_add_delayed_iput(inode
);
1615 static noinline_for_stack
1616 int memcmp_node_keys(struct extent_buffer
*eb
, int slot
,
1617 struct btrfs_path
*path
, int level
)
1619 struct btrfs_disk_key key1
;
1620 struct btrfs_disk_key key2
;
1621 btrfs_node_key(eb
, &key1
, slot
);
1622 btrfs_node_key(path
->nodes
[level
], &key2
, path
->slots
[level
]);
1623 return memcmp(&key1
, &key2
, sizeof(key1
));
1627 * try to replace tree blocks in fs tree with the new blocks
1628 * in reloc tree. tree blocks haven't been modified since the
1629 * reloc tree was create can be replaced.
1631 * if a block was replaced, level of the block + 1 is returned.
1632 * if no block got replaced, 0 is returned. if there are other
1633 * errors, a negative error number is returned.
1635 static noinline_for_stack
1636 int replace_path(struct btrfs_trans_handle
*trans
,
1637 struct btrfs_root
*dest
, struct btrfs_root
*src
,
1638 struct btrfs_path
*path
, struct btrfs_key
*next_key
,
1639 int lowest_level
, int max_level
)
1641 struct extent_buffer
*eb
;
1642 struct extent_buffer
*parent
;
1643 struct btrfs_key key
;
1655 BUG_ON(src
->root_key
.objectid
!= BTRFS_TREE_RELOC_OBJECTID
);
1656 BUG_ON(dest
->root_key
.objectid
== BTRFS_TREE_RELOC_OBJECTID
);
1658 last_snapshot
= btrfs_root_last_snapshot(&src
->root_item
);
1660 slot
= path
->slots
[lowest_level
];
1661 btrfs_node_key_to_cpu(path
->nodes
[lowest_level
], &key
, slot
);
1663 eb
= btrfs_lock_root_node(dest
);
1664 btrfs_set_lock_blocking(eb
);
1665 level
= btrfs_header_level(eb
);
1667 if (level
< lowest_level
) {
1668 btrfs_tree_unlock(eb
);
1669 free_extent_buffer(eb
);
1674 ret
= btrfs_cow_block(trans
, dest
, eb
, NULL
, 0, &eb
);
1677 btrfs_set_lock_blocking(eb
);
1680 next_key
->objectid
= (u64
)-1;
1681 next_key
->type
= (u8
)-1;
1682 next_key
->offset
= (u64
)-1;
1687 level
= btrfs_header_level(parent
);
1688 BUG_ON(level
< lowest_level
);
1690 ret
= btrfs_bin_search(parent
, &key
, level
, &slot
);
1691 if (ret
&& slot
> 0)
1694 if (next_key
&& slot
+ 1 < btrfs_header_nritems(parent
))
1695 btrfs_node_key_to_cpu(parent
, next_key
, slot
+ 1);
1697 old_bytenr
= btrfs_node_blockptr(parent
, slot
);
1698 blocksize
= btrfs_level_size(dest
, level
- 1);
1699 old_ptr_gen
= btrfs_node_ptr_generation(parent
, slot
);
1701 if (level
<= max_level
) {
1702 eb
= path
->nodes
[level
];
1703 new_bytenr
= btrfs_node_blockptr(eb
,
1704 path
->slots
[level
]);
1705 new_ptr_gen
= btrfs_node_ptr_generation(eb
,
1706 path
->slots
[level
]);
1712 if (new_bytenr
> 0 && new_bytenr
== old_bytenr
) {
1718 if (new_bytenr
== 0 || old_ptr_gen
> last_snapshot
||
1719 memcmp_node_keys(parent
, slot
, path
, level
)) {
1720 if (level
<= lowest_level
) {
1725 eb
= read_tree_block(dest
, old_bytenr
, blocksize
,
1728 btrfs_tree_lock(eb
);
1730 ret
= btrfs_cow_block(trans
, dest
, eb
, parent
,
1734 btrfs_set_lock_blocking(eb
);
1736 btrfs_tree_unlock(parent
);
1737 free_extent_buffer(parent
);
1744 btrfs_tree_unlock(parent
);
1745 free_extent_buffer(parent
);
1750 btrfs_node_key_to_cpu(path
->nodes
[level
], &key
,
1751 path
->slots
[level
]);
1752 btrfs_release_path(src
, path
);
1754 path
->lowest_level
= level
;
1755 ret
= btrfs_search_slot(trans
, src
, &key
, path
, 0, 1);
1756 path
->lowest_level
= 0;
1760 * swap blocks in fs tree and reloc tree.
1762 btrfs_set_node_blockptr(parent
, slot
, new_bytenr
);
1763 btrfs_set_node_ptr_generation(parent
, slot
, new_ptr_gen
);
1764 btrfs_mark_buffer_dirty(parent
);
1766 btrfs_set_node_blockptr(path
->nodes
[level
],
1767 path
->slots
[level
], old_bytenr
);
1768 btrfs_set_node_ptr_generation(path
->nodes
[level
],
1769 path
->slots
[level
], old_ptr_gen
);
1770 btrfs_mark_buffer_dirty(path
->nodes
[level
]);
1772 ret
= btrfs_inc_extent_ref(trans
, src
, old_bytenr
, blocksize
,
1773 path
->nodes
[level
]->start
,
1774 src
->root_key
.objectid
, level
- 1, 0);
1776 ret
= btrfs_inc_extent_ref(trans
, dest
, new_bytenr
, blocksize
,
1777 0, dest
->root_key
.objectid
, level
- 1,
1781 ret
= btrfs_free_extent(trans
, src
, new_bytenr
, blocksize
,
1782 path
->nodes
[level
]->start
,
1783 src
->root_key
.objectid
, level
- 1, 0);
1786 ret
= btrfs_free_extent(trans
, dest
, old_bytenr
, blocksize
,
1787 0, dest
->root_key
.objectid
, level
- 1,
1791 btrfs_unlock_up_safe(path
, 0);
1796 btrfs_tree_unlock(parent
);
1797 free_extent_buffer(parent
);
1802 * helper to find next relocated block in reloc tree
1804 static noinline_for_stack
1805 int walk_up_reloc_tree(struct btrfs_root
*root
, struct btrfs_path
*path
,
1808 struct extent_buffer
*eb
;
1813 last_snapshot
= btrfs_root_last_snapshot(&root
->root_item
);
1815 for (i
= 0; i
< *level
; i
++) {
1816 free_extent_buffer(path
->nodes
[i
]);
1817 path
->nodes
[i
] = NULL
;
1820 for (i
= *level
; i
< BTRFS_MAX_LEVEL
&& path
->nodes
[i
]; i
++) {
1821 eb
= path
->nodes
[i
];
1822 nritems
= btrfs_header_nritems(eb
);
1823 while (path
->slots
[i
] + 1 < nritems
) {
1825 if (btrfs_node_ptr_generation(eb
, path
->slots
[i
]) <=
1832 free_extent_buffer(path
->nodes
[i
]);
1833 path
->nodes
[i
] = NULL
;
1839 * walk down reloc tree to find relocated block of lowest level
1841 static noinline_for_stack
1842 int walk_down_reloc_tree(struct btrfs_root
*root
, struct btrfs_path
*path
,
1845 struct extent_buffer
*eb
= NULL
;
1853 last_snapshot
= btrfs_root_last_snapshot(&root
->root_item
);
1855 for (i
= *level
; i
> 0; i
--) {
1856 eb
= path
->nodes
[i
];
1857 nritems
= btrfs_header_nritems(eb
);
1858 while (path
->slots
[i
] < nritems
) {
1859 ptr_gen
= btrfs_node_ptr_generation(eb
, path
->slots
[i
]);
1860 if (ptr_gen
> last_snapshot
)
1864 if (path
->slots
[i
] >= nritems
) {
1875 bytenr
= btrfs_node_blockptr(eb
, path
->slots
[i
]);
1876 blocksize
= btrfs_level_size(root
, i
- 1);
1877 eb
= read_tree_block(root
, bytenr
, blocksize
, ptr_gen
);
1878 BUG_ON(btrfs_header_level(eb
) != i
- 1);
1879 path
->nodes
[i
- 1] = eb
;
1880 path
->slots
[i
- 1] = 0;
1886 * invalidate extent cache for file extents whose key in range of
1887 * [min_key, max_key)
1889 static int invalidate_extent_cache(struct btrfs_root
*root
,
1890 struct btrfs_key
*min_key
,
1891 struct btrfs_key
*max_key
)
1893 struct inode
*inode
= NULL
;
1897 objectid
= min_key
->objectid
;
1902 if (objectid
> max_key
->objectid
)
1905 inode
= find_next_inode(root
, objectid
);
1909 if (inode
->i_ino
> max_key
->objectid
) {
1914 objectid
= inode
->i_ino
+ 1;
1915 if (!S_ISREG(inode
->i_mode
))
1918 if (unlikely(min_key
->objectid
== inode
->i_ino
)) {
1919 if (min_key
->type
> BTRFS_EXTENT_DATA_KEY
)
1921 if (min_key
->type
< BTRFS_EXTENT_DATA_KEY
)
1924 start
= min_key
->offset
;
1925 WARN_ON(!IS_ALIGNED(start
, root
->sectorsize
));
1931 if (unlikely(max_key
->objectid
== inode
->i_ino
)) {
1932 if (max_key
->type
< BTRFS_EXTENT_DATA_KEY
)
1934 if (max_key
->type
> BTRFS_EXTENT_DATA_KEY
) {
1937 if (max_key
->offset
== 0)
1939 end
= max_key
->offset
;
1940 WARN_ON(!IS_ALIGNED(end
, root
->sectorsize
));
1947 /* the lock_extent waits for readpage to complete */
1948 lock_extent(&BTRFS_I(inode
)->io_tree
, start
, end
, GFP_NOFS
);
1949 btrfs_drop_extent_cache(inode
, start
, end
, 1);
1950 unlock_extent(&BTRFS_I(inode
)->io_tree
, start
, end
, GFP_NOFS
);
1955 static int find_next_key(struct btrfs_path
*path
, int level
,
1956 struct btrfs_key
*key
)
1959 while (level
< BTRFS_MAX_LEVEL
) {
1960 if (!path
->nodes
[level
])
1962 if (path
->slots
[level
] + 1 <
1963 btrfs_header_nritems(path
->nodes
[level
])) {
1964 btrfs_node_key_to_cpu(path
->nodes
[level
], key
,
1965 path
->slots
[level
] + 1);
1974 * merge the relocated tree blocks in reloc tree with corresponding
1977 static noinline_for_stack
int merge_reloc_root(struct reloc_control
*rc
,
1978 struct btrfs_root
*root
)
1980 LIST_HEAD(inode_list
);
1981 struct btrfs_key key
;
1982 struct btrfs_key next_key
;
1983 struct btrfs_trans_handle
*trans
;
1984 struct btrfs_root
*reloc_root
;
1985 struct btrfs_root_item
*root_item
;
1986 struct btrfs_path
*path
;
1987 struct extent_buffer
*leaf
;
1996 path
= btrfs_alloc_path();
2000 reloc_root
= root
->reloc_root
;
2001 root_item
= &reloc_root
->root_item
;
2003 if (btrfs_disk_key_objectid(&root_item
->drop_progress
) == 0) {
2004 level
= btrfs_root_level(root_item
);
2005 extent_buffer_get(reloc_root
->node
);
2006 path
->nodes
[level
] = reloc_root
->node
;
2007 path
->slots
[level
] = 0;
2009 btrfs_disk_key_to_cpu(&key
, &root_item
->drop_progress
);
2011 level
= root_item
->drop_level
;
2013 path
->lowest_level
= level
;
2014 ret
= btrfs_search_slot(NULL
, reloc_root
, &key
, path
, 0, 0);
2015 path
->lowest_level
= 0;
2017 btrfs_free_path(path
);
2021 btrfs_node_key_to_cpu(path
->nodes
[level
], &next_key
,
2022 path
->slots
[level
]);
2023 WARN_ON(memcmp(&key
, &next_key
, sizeof(key
)));
2025 btrfs_unlock_up_safe(path
, 0);
2028 min_reserved
= root
->nodesize
* (BTRFS_MAX_LEVEL
- 1) * 2;
2029 memset(&next_key
, 0, sizeof(next_key
));
2032 trans
= btrfs_start_transaction(root
, 0);
2033 BUG_ON(IS_ERR(trans
));
2034 trans
->block_rsv
= rc
->block_rsv
;
2036 ret
= btrfs_block_rsv_check(trans
, root
, rc
->block_rsv
,
2039 BUG_ON(ret
!= -EAGAIN
);
2040 ret
= btrfs_commit_transaction(trans
, root
);
2048 ret
= walk_down_reloc_tree(reloc_root
, path
, &level
);
2056 if (!find_next_key(path
, level
, &key
) &&
2057 btrfs_comp_cpu_keys(&next_key
, &key
) >= 0) {
2060 ret
= replace_path(trans
, root
, reloc_root
, path
,
2061 &next_key
, level
, max_level
);
2070 btrfs_node_key_to_cpu(path
->nodes
[level
], &key
,
2071 path
->slots
[level
]);
2075 ret
= walk_up_reloc_tree(reloc_root
, path
, &level
);
2081 * save the merging progress in the drop_progress.
2082 * this is OK since root refs == 1 in this case.
2084 btrfs_node_key(path
->nodes
[level
], &root_item
->drop_progress
,
2085 path
->slots
[level
]);
2086 root_item
->drop_level
= level
;
2088 nr
= trans
->blocks_used
;
2089 btrfs_end_transaction_throttle(trans
, root
);
2091 btrfs_btree_balance_dirty(root
, nr
);
2093 if (replaced
&& rc
->stage
== UPDATE_DATA_PTRS
)
2094 invalidate_extent_cache(root
, &key
, &next_key
);
2098 * handle the case only one block in the fs tree need to be
2099 * relocated and the block is tree root.
2101 leaf
= btrfs_lock_root_node(root
);
2102 ret
= btrfs_cow_block(trans
, root
, leaf
, NULL
, 0, &leaf
);
2103 btrfs_tree_unlock(leaf
);
2104 free_extent_buffer(leaf
);
2108 btrfs_free_path(path
);
2111 memset(&root_item
->drop_progress
, 0,
2112 sizeof(root_item
->drop_progress
));
2113 root_item
->drop_level
= 0;
2114 btrfs_set_root_refs(root_item
, 0);
2115 btrfs_update_reloc_root(trans
, root
);
2118 nr
= trans
->blocks_used
;
2119 btrfs_end_transaction_throttle(trans
, root
);
2121 btrfs_btree_balance_dirty(root
, nr
);
2123 if (replaced
&& rc
->stage
== UPDATE_DATA_PTRS
)
2124 invalidate_extent_cache(root
, &key
, &next_key
);
2129 static noinline_for_stack
2130 int prepare_to_merge(struct reloc_control
*rc
, int err
)
2132 struct btrfs_root
*root
= rc
->extent_root
;
2133 struct btrfs_root
*reloc_root
;
2134 struct btrfs_trans_handle
*trans
;
2135 LIST_HEAD(reloc_roots
);
2139 mutex_lock(&root
->fs_info
->trans_mutex
);
2140 rc
->merging_rsv_size
+= root
->nodesize
* (BTRFS_MAX_LEVEL
- 1) * 2;
2141 rc
->merging_rsv_size
+= rc
->nodes_relocated
* 2;
2142 mutex_unlock(&root
->fs_info
->trans_mutex
);
2145 num_bytes
= rc
->merging_rsv_size
;
2146 ret
= btrfs_block_rsv_add(NULL
, root
, rc
->block_rsv
,
2152 trans
= btrfs_join_transaction(rc
->extent_root
, 1);
2153 if (IS_ERR(trans
)) {
2155 btrfs_block_rsv_release(rc
->extent_root
,
2156 rc
->block_rsv
, num_bytes
);
2157 return PTR_ERR(trans
);
2161 if (num_bytes
!= rc
->merging_rsv_size
) {
2162 btrfs_end_transaction(trans
, rc
->extent_root
);
2163 btrfs_block_rsv_release(rc
->extent_root
,
2164 rc
->block_rsv
, num_bytes
);
2169 rc
->merge_reloc_tree
= 1;
2171 while (!list_empty(&rc
->reloc_roots
)) {
2172 reloc_root
= list_entry(rc
->reloc_roots
.next
,
2173 struct btrfs_root
, root_list
);
2174 list_del_init(&reloc_root
->root_list
);
2176 root
= read_fs_root(reloc_root
->fs_info
,
2177 reloc_root
->root_key
.offset
);
2178 BUG_ON(IS_ERR(root
));
2179 BUG_ON(root
->reloc_root
!= reloc_root
);
2182 * set reference count to 1, so btrfs_recover_relocation
2183 * knows it should resumes merging
2186 btrfs_set_root_refs(&reloc_root
->root_item
, 1);
2187 btrfs_update_reloc_root(trans
, root
);
2189 list_add(&reloc_root
->root_list
, &reloc_roots
);
2192 list_splice(&reloc_roots
, &rc
->reloc_roots
);
2195 btrfs_commit_transaction(trans
, rc
->extent_root
);
2197 btrfs_end_transaction(trans
, rc
->extent_root
);
2201 static noinline_for_stack
2202 int merge_reloc_roots(struct reloc_control
*rc
)
2204 struct btrfs_root
*root
;
2205 struct btrfs_root
*reloc_root
;
2206 LIST_HEAD(reloc_roots
);
2210 root
= rc
->extent_root
;
2211 mutex_lock(&root
->fs_info
->trans_mutex
);
2212 list_splice_init(&rc
->reloc_roots
, &reloc_roots
);
2213 mutex_unlock(&root
->fs_info
->trans_mutex
);
2215 while (!list_empty(&reloc_roots
)) {
2217 reloc_root
= list_entry(reloc_roots
.next
,
2218 struct btrfs_root
, root_list
);
2220 if (btrfs_root_refs(&reloc_root
->root_item
) > 0) {
2221 root
= read_fs_root(reloc_root
->fs_info
,
2222 reloc_root
->root_key
.offset
);
2223 BUG_ON(IS_ERR(root
));
2224 BUG_ON(root
->reloc_root
!= reloc_root
);
2226 ret
= merge_reloc_root(rc
, root
);
2229 list_del_init(&reloc_root
->root_list
);
2231 btrfs_drop_snapshot(reloc_root
, rc
->block_rsv
, 0);
2238 BUG_ON(!RB_EMPTY_ROOT(&rc
->reloc_root_tree
.rb_root
));
2242 static void free_block_list(struct rb_root
*blocks
)
2244 struct tree_block
*block
;
2245 struct rb_node
*rb_node
;
2246 while ((rb_node
= rb_first(blocks
))) {
2247 block
= rb_entry(rb_node
, struct tree_block
, rb_node
);
2248 rb_erase(rb_node
, blocks
);
2253 static int record_reloc_root_in_trans(struct btrfs_trans_handle
*trans
,
2254 struct btrfs_root
*reloc_root
)
2256 struct btrfs_root
*root
;
2258 if (reloc_root
->last_trans
== trans
->transid
)
2261 root
= read_fs_root(reloc_root
->fs_info
, reloc_root
->root_key
.offset
);
2262 BUG_ON(IS_ERR(root
));
2263 BUG_ON(root
->reloc_root
!= reloc_root
);
2265 return btrfs_record_root_in_trans(trans
, root
);
2268 static noinline_for_stack
2269 struct btrfs_root
*select_reloc_root(struct btrfs_trans_handle
*trans
,
2270 struct reloc_control
*rc
,
2271 struct backref_node
*node
,
2272 struct backref_edge
*edges
[], int *nr
)
2274 struct backref_node
*next
;
2275 struct btrfs_root
*root
;
2281 next
= walk_up_backref(next
, edges
, &index
);
2284 BUG_ON(!root
->ref_cows
);
2286 if (root
->root_key
.objectid
== BTRFS_TREE_RELOC_OBJECTID
) {
2287 record_reloc_root_in_trans(trans
, root
);
2291 btrfs_record_root_in_trans(trans
, root
);
2292 root
= root
->reloc_root
;
2294 if (next
->new_bytenr
!= root
->node
->start
) {
2295 BUG_ON(next
->new_bytenr
);
2296 BUG_ON(!list_empty(&next
->list
));
2297 next
->new_bytenr
= root
->node
->start
;
2299 list_add_tail(&next
->list
,
2300 &rc
->backref_cache
.changed
);
2301 __mark_block_processed(rc
, next
);
2307 next
= walk_down_backref(edges
, &index
);
2308 if (!next
|| next
->level
<= node
->level
)
2316 /* setup backref node path for btrfs_reloc_cow_block */
2318 rc
->backref_cache
.path
[next
->level
] = next
;
2321 next
= edges
[index
]->node
[UPPER
];
2327 * select a tree root for relocation. return NULL if the block
2328 * is reference counted. we should use do_relocation() in this
2329 * case. return a tree root pointer if the block isn't reference
2330 * counted. return -ENOENT if the block is root of reloc tree.
2332 static noinline_for_stack
2333 struct btrfs_root
*select_one_root(struct btrfs_trans_handle
*trans
,
2334 struct backref_node
*node
)
2336 struct backref_node
*next
;
2337 struct btrfs_root
*root
;
2338 struct btrfs_root
*fs_root
= NULL
;
2339 struct backref_edge
*edges
[BTRFS_MAX_LEVEL
- 1];
2345 next
= walk_up_backref(next
, edges
, &index
);
2349 /* no other choice for non-refernce counted tree */
2350 if (!root
->ref_cows
)
2353 if (root
->root_key
.objectid
!= BTRFS_TREE_RELOC_OBJECTID
)
2359 next
= walk_down_backref(edges
, &index
);
2360 if (!next
|| next
->level
<= node
->level
)
2365 return ERR_PTR(-ENOENT
);
2369 static noinline_for_stack
2370 u64
calcu_metadata_size(struct reloc_control
*rc
,
2371 struct backref_node
*node
, int reserve
)
2373 struct backref_node
*next
= node
;
2374 struct backref_edge
*edge
;
2375 struct backref_edge
*edges
[BTRFS_MAX_LEVEL
- 1];
2379 BUG_ON(reserve
&& node
->processed
);
2384 if (next
->processed
&& (reserve
|| next
!= node
))
2387 num_bytes
+= btrfs_level_size(rc
->extent_root
,
2390 if (list_empty(&next
->upper
))
2393 edge
= list_entry(next
->upper
.next
,
2394 struct backref_edge
, list
[LOWER
]);
2395 edges
[index
++] = edge
;
2396 next
= edge
->node
[UPPER
];
2398 next
= walk_down_backref(edges
, &index
);
2403 static int reserve_metadata_space(struct btrfs_trans_handle
*trans
,
2404 struct reloc_control
*rc
,
2405 struct backref_node
*node
)
2407 struct btrfs_root
*root
= rc
->extent_root
;
2411 num_bytes
= calcu_metadata_size(rc
, node
, 1) * 2;
2413 trans
->block_rsv
= rc
->block_rsv
;
2414 ret
= btrfs_block_rsv_add(trans
, root
, rc
->block_rsv
, num_bytes
);
2417 rc
->commit_transaction
= 1;
2424 static void release_metadata_space(struct reloc_control
*rc
,
2425 struct backref_node
*node
)
2427 u64 num_bytes
= calcu_metadata_size(rc
, node
, 0) * 2;
2428 btrfs_block_rsv_release(rc
->extent_root
, rc
->block_rsv
, num_bytes
);
2432 * relocate a block tree, and then update pointers in upper level
2433 * blocks that reference the block to point to the new location.
2435 * if called by link_to_upper, the block has already been relocated.
2436 * in that case this function just updates pointers.
2438 static int do_relocation(struct btrfs_trans_handle
*trans
,
2439 struct reloc_control
*rc
,
2440 struct backref_node
*node
,
2441 struct btrfs_key
*key
,
2442 struct btrfs_path
*path
, int lowest
)
2444 struct backref_node
*upper
;
2445 struct backref_edge
*edge
;
2446 struct backref_edge
*edges
[BTRFS_MAX_LEVEL
- 1];
2447 struct btrfs_root
*root
;
2448 struct extent_buffer
*eb
;
2457 BUG_ON(lowest
&& node
->eb
);
2459 path
->lowest_level
= node
->level
+ 1;
2460 rc
->backref_cache
.path
[node
->level
] = node
;
2461 list_for_each_entry(edge
, &node
->upper
, list
[LOWER
]) {
2464 upper
= edge
->node
[UPPER
];
2465 root
= select_reloc_root(trans
, rc
, upper
, edges
, &nr
);
2468 if (upper
->eb
&& !upper
->locked
) {
2470 ret
= btrfs_bin_search(upper
->eb
, key
,
2471 upper
->level
, &slot
);
2473 bytenr
= btrfs_node_blockptr(upper
->eb
, slot
);
2474 if (node
->eb
->start
== bytenr
)
2477 drop_node_buffer(upper
);
2481 ret
= btrfs_search_slot(trans
, root
, key
, path
, 0, 1);
2489 upper
->eb
= path
->nodes
[upper
->level
];
2490 path
->nodes
[upper
->level
] = NULL
;
2492 BUG_ON(upper
->eb
!= path
->nodes
[upper
->level
]);
2496 path
->locks
[upper
->level
] = 0;
2498 slot
= path
->slots
[upper
->level
];
2499 btrfs_release_path(NULL
, path
);
2501 ret
= btrfs_bin_search(upper
->eb
, key
, upper
->level
,
2506 bytenr
= btrfs_node_blockptr(upper
->eb
, slot
);
2508 BUG_ON(bytenr
!= node
->bytenr
);
2510 if (node
->eb
->start
== bytenr
)
2514 blocksize
= btrfs_level_size(root
, node
->level
);
2515 generation
= btrfs_node_ptr_generation(upper
->eb
, slot
);
2516 eb
= read_tree_block(root
, bytenr
, blocksize
, generation
);
2521 btrfs_tree_lock(eb
);
2522 btrfs_set_lock_blocking(eb
);
2525 ret
= btrfs_cow_block(trans
, root
, eb
, upper
->eb
,
2527 btrfs_tree_unlock(eb
);
2528 free_extent_buffer(eb
);
2533 BUG_ON(node
->eb
!= eb
);
2535 btrfs_set_node_blockptr(upper
->eb
, slot
,
2537 btrfs_set_node_ptr_generation(upper
->eb
, slot
,
2539 btrfs_mark_buffer_dirty(upper
->eb
);
2541 ret
= btrfs_inc_extent_ref(trans
, root
,
2542 node
->eb
->start
, blocksize
,
2544 btrfs_header_owner(upper
->eb
),
2548 ret
= btrfs_drop_subtree(trans
, root
, eb
, upper
->eb
);
2552 if (!upper
->pending
)
2553 drop_node_buffer(upper
);
2555 unlock_node_buffer(upper
);
2560 if (!err
&& node
->pending
) {
2561 drop_node_buffer(node
);
2562 list_move_tail(&node
->list
, &rc
->backref_cache
.changed
);
2566 path
->lowest_level
= 0;
2567 BUG_ON(err
== -ENOSPC
);
2571 static int link_to_upper(struct btrfs_trans_handle
*trans
,
2572 struct reloc_control
*rc
,
2573 struct backref_node
*node
,
2574 struct btrfs_path
*path
)
2576 struct btrfs_key key
;
2578 btrfs_node_key_to_cpu(node
->eb
, &key
, 0);
2579 return do_relocation(trans
, rc
, node
, &key
, path
, 0);
2582 static int finish_pending_nodes(struct btrfs_trans_handle
*trans
,
2583 struct reloc_control
*rc
,
2584 struct btrfs_path
*path
, int err
)
2587 struct backref_cache
*cache
= &rc
->backref_cache
;
2588 struct backref_node
*node
;
2592 for (level
= 0; level
< BTRFS_MAX_LEVEL
; level
++) {
2593 while (!list_empty(&cache
->pending
[level
])) {
2594 node
= list_entry(cache
->pending
[level
].next
,
2595 struct backref_node
, list
);
2596 list_move_tail(&node
->list
, &list
);
2597 BUG_ON(!node
->pending
);
2600 ret
= link_to_upper(trans
, rc
, node
, path
);
2605 list_splice_init(&list
, &cache
->pending
[level
]);
2610 static void mark_block_processed(struct reloc_control
*rc
,
2611 u64 bytenr
, u32 blocksize
)
2613 set_extent_bits(&rc
->processed_blocks
, bytenr
, bytenr
+ blocksize
- 1,
2614 EXTENT_DIRTY
, GFP_NOFS
);
2617 static void __mark_block_processed(struct reloc_control
*rc
,
2618 struct backref_node
*node
)
2621 if (node
->level
== 0 ||
2622 in_block_group(node
->bytenr
, rc
->block_group
)) {
2623 blocksize
= btrfs_level_size(rc
->extent_root
, node
->level
);
2624 mark_block_processed(rc
, node
->bytenr
, blocksize
);
2626 node
->processed
= 1;
2630 * mark a block and all blocks directly/indirectly reference the block
2633 static void update_processed_blocks(struct reloc_control
*rc
,
2634 struct backref_node
*node
)
2636 struct backref_node
*next
= node
;
2637 struct backref_edge
*edge
;
2638 struct backref_edge
*edges
[BTRFS_MAX_LEVEL
- 1];
2644 if (next
->processed
)
2647 __mark_block_processed(rc
, next
);
2649 if (list_empty(&next
->upper
))
2652 edge
= list_entry(next
->upper
.next
,
2653 struct backref_edge
, list
[LOWER
]);
2654 edges
[index
++] = edge
;
2655 next
= edge
->node
[UPPER
];
2657 next
= walk_down_backref(edges
, &index
);
2661 static int tree_block_processed(u64 bytenr
, u32 blocksize
,
2662 struct reloc_control
*rc
)
2664 if (test_range_bit(&rc
->processed_blocks
, bytenr
,
2665 bytenr
+ blocksize
- 1, EXTENT_DIRTY
, 1, NULL
))
2670 static int get_tree_block_key(struct reloc_control
*rc
,
2671 struct tree_block
*block
)
2673 struct extent_buffer
*eb
;
2675 BUG_ON(block
->key_ready
);
2676 eb
= read_tree_block(rc
->extent_root
, block
->bytenr
,
2677 block
->key
.objectid
, block
->key
.offset
);
2679 WARN_ON(btrfs_header_level(eb
) != block
->level
);
2680 if (block
->level
== 0)
2681 btrfs_item_key_to_cpu(eb
, &block
->key
, 0);
2683 btrfs_node_key_to_cpu(eb
, &block
->key
, 0);
2684 free_extent_buffer(eb
);
2685 block
->key_ready
= 1;
2689 static int reada_tree_block(struct reloc_control
*rc
,
2690 struct tree_block
*block
)
2692 BUG_ON(block
->key_ready
);
2693 readahead_tree_block(rc
->extent_root
, block
->bytenr
,
2694 block
->key
.objectid
, block
->key
.offset
);
2699 * helper function to relocate a tree block
2701 static int relocate_tree_block(struct btrfs_trans_handle
*trans
,
2702 struct reloc_control
*rc
,
2703 struct backref_node
*node
,
2704 struct btrfs_key
*key
,
2705 struct btrfs_path
*path
)
2707 struct btrfs_root
*root
;
2714 BUG_ON(node
->processed
);
2715 root
= select_one_root(trans
, node
);
2716 if (root
== ERR_PTR(-ENOENT
)) {
2717 update_processed_blocks(rc
, node
);
2721 if (!root
|| root
->ref_cows
) {
2722 ret
= reserve_metadata_space(trans
, rc
, node
);
2729 if (root
->ref_cows
) {
2730 BUG_ON(node
->new_bytenr
);
2731 BUG_ON(!list_empty(&node
->list
));
2732 btrfs_record_root_in_trans(trans
, root
);
2733 root
= root
->reloc_root
;
2734 node
->new_bytenr
= root
->node
->start
;
2736 list_add_tail(&node
->list
, &rc
->backref_cache
.changed
);
2738 path
->lowest_level
= node
->level
;
2739 ret
= btrfs_search_slot(trans
, root
, key
, path
, 0, 1);
2740 btrfs_release_path(root
, path
);
2745 update_processed_blocks(rc
, node
);
2747 ret
= do_relocation(trans
, rc
, node
, key
, path
, 1);
2750 if (ret
|| node
->level
== 0 || node
->cowonly
) {
2752 release_metadata_space(rc
, node
);
2753 remove_backref_node(&rc
->backref_cache
, node
);
2759 * relocate a list of blocks
2761 static noinline_for_stack
2762 int relocate_tree_blocks(struct btrfs_trans_handle
*trans
,
2763 struct reloc_control
*rc
, struct rb_root
*blocks
)
2765 struct backref_node
*node
;
2766 struct btrfs_path
*path
;
2767 struct tree_block
*block
;
2768 struct rb_node
*rb_node
;
2772 path
= btrfs_alloc_path();
2776 rb_node
= rb_first(blocks
);
2778 block
= rb_entry(rb_node
, struct tree_block
, rb_node
);
2779 if (!block
->key_ready
)
2780 reada_tree_block(rc
, block
);
2781 rb_node
= rb_next(rb_node
);
2784 rb_node
= rb_first(blocks
);
2786 block
= rb_entry(rb_node
, struct tree_block
, rb_node
);
2787 if (!block
->key_ready
)
2788 get_tree_block_key(rc
, block
);
2789 rb_node
= rb_next(rb_node
);
2792 rb_node
= rb_first(blocks
);
2794 block
= rb_entry(rb_node
, struct tree_block
, rb_node
);
2796 node
= build_backref_tree(rc
, &block
->key
,
2797 block
->level
, block
->bytenr
);
2799 err
= PTR_ERR(node
);
2803 ret
= relocate_tree_block(trans
, rc
, node
, &block
->key
,
2806 if (ret
!= -EAGAIN
|| rb_node
== rb_first(blocks
))
2810 rb_node
= rb_next(rb_node
);
2813 free_block_list(blocks
);
2814 err
= finish_pending_nodes(trans
, rc
, path
, err
);
2816 btrfs_free_path(path
);
2820 static noinline_for_stack
2821 int prealloc_file_extent_cluster(struct inode
*inode
,
2822 struct file_extent_cluster
*cluster
)
2827 u64 offset
= BTRFS_I(inode
)->index_cnt
;
2832 BUG_ON(cluster
->start
!= cluster
->boundary
[0]);
2833 mutex_lock(&inode
->i_mutex
);
2835 ret
= btrfs_check_data_free_space(inode
, cluster
->end
+
2836 1 - cluster
->start
);
2840 while (nr
< cluster
->nr
) {
2841 start
= cluster
->boundary
[nr
] - offset
;
2842 if (nr
+ 1 < cluster
->nr
)
2843 end
= cluster
->boundary
[nr
+ 1] - 1 - offset
;
2845 end
= cluster
->end
- offset
;
2847 lock_extent(&BTRFS_I(inode
)->io_tree
, start
, end
, GFP_NOFS
);
2848 num_bytes
= end
+ 1 - start
;
2849 ret
= btrfs_prealloc_file_range(inode
, 0, start
,
2850 num_bytes
, num_bytes
,
2851 end
+ 1, &alloc_hint
);
2852 unlock_extent(&BTRFS_I(inode
)->io_tree
, start
, end
, GFP_NOFS
);
2857 btrfs_free_reserved_data_space(inode
, cluster
->end
+
2858 1 - cluster
->start
);
2860 mutex_unlock(&inode
->i_mutex
);
2864 static noinline_for_stack
2865 int setup_extent_mapping(struct inode
*inode
, u64 start
, u64 end
,
2868 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2869 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
2870 struct extent_map
*em
;
2873 em
= alloc_extent_map(GFP_NOFS
);
2878 em
->len
= end
+ 1 - start
;
2879 em
->block_len
= em
->len
;
2880 em
->block_start
= block_start
;
2881 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
2882 set_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
2884 lock_extent(&BTRFS_I(inode
)->io_tree
, start
, end
, GFP_NOFS
);
2886 write_lock(&em_tree
->lock
);
2887 ret
= add_extent_mapping(em_tree
, em
);
2888 write_unlock(&em_tree
->lock
);
2889 if (ret
!= -EEXIST
) {
2890 free_extent_map(em
);
2893 btrfs_drop_extent_cache(inode
, start
, end
, 0);
2895 unlock_extent(&BTRFS_I(inode
)->io_tree
, start
, end
, GFP_NOFS
);
2899 static int relocate_file_extent_cluster(struct inode
*inode
,
2900 struct file_extent_cluster
*cluster
)
2904 u64 offset
= BTRFS_I(inode
)->index_cnt
;
2905 unsigned long index
;
2906 unsigned long last_index
;
2908 struct file_ra_state
*ra
;
2915 ra
= kzalloc(sizeof(*ra
), GFP_NOFS
);
2919 ret
= prealloc_file_extent_cluster(inode
, cluster
);
2923 file_ra_state_init(ra
, inode
->i_mapping
);
2925 ret
= setup_extent_mapping(inode
, cluster
->start
- offset
,
2926 cluster
->end
- offset
, cluster
->start
);
2930 index
= (cluster
->start
- offset
) >> PAGE_CACHE_SHIFT
;
2931 last_index
= (cluster
->end
- offset
) >> PAGE_CACHE_SHIFT
;
2932 while (index
<= last_index
) {
2933 ret
= btrfs_delalloc_reserve_metadata(inode
, PAGE_CACHE_SIZE
);
2937 page
= find_lock_page(inode
->i_mapping
, index
);
2939 page_cache_sync_readahead(inode
->i_mapping
,
2941 last_index
+ 1 - index
);
2942 page
= grab_cache_page(inode
->i_mapping
, index
);
2944 btrfs_delalloc_release_metadata(inode
,
2951 if (PageReadahead(page
)) {
2952 page_cache_async_readahead(inode
->i_mapping
,
2953 ra
, NULL
, page
, index
,
2954 last_index
+ 1 - index
);
2957 if (!PageUptodate(page
)) {
2958 btrfs_readpage(NULL
, page
);
2960 if (!PageUptodate(page
)) {
2962 page_cache_release(page
);
2963 btrfs_delalloc_release_metadata(inode
,
2970 page_start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
2971 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
2973 lock_extent(&BTRFS_I(inode
)->io_tree
,
2974 page_start
, page_end
, GFP_NOFS
);
2976 set_page_extent_mapped(page
);
2978 if (nr
< cluster
->nr
&&
2979 page_start
+ offset
== cluster
->boundary
[nr
]) {
2980 set_extent_bits(&BTRFS_I(inode
)->io_tree
,
2981 page_start
, page_end
,
2982 EXTENT_BOUNDARY
, GFP_NOFS
);
2986 btrfs_set_extent_delalloc(inode
, page_start
, page_end
, NULL
);
2987 set_page_dirty(page
);
2989 unlock_extent(&BTRFS_I(inode
)->io_tree
,
2990 page_start
, page_end
, GFP_NOFS
);
2992 page_cache_release(page
);
2995 balance_dirty_pages_ratelimited(inode
->i_mapping
);
2996 btrfs_throttle(BTRFS_I(inode
)->root
);
2998 WARN_ON(nr
!= cluster
->nr
);
3004 static noinline_for_stack
3005 int relocate_data_extent(struct inode
*inode
, struct btrfs_key
*extent_key
,
3006 struct file_extent_cluster
*cluster
)
3010 if (cluster
->nr
> 0 && extent_key
->objectid
!= cluster
->end
+ 1) {
3011 ret
= relocate_file_extent_cluster(inode
, cluster
);
3018 cluster
->start
= extent_key
->objectid
;
3020 BUG_ON(cluster
->nr
>= MAX_EXTENTS
);
3021 cluster
->end
= extent_key
->objectid
+ extent_key
->offset
- 1;
3022 cluster
->boundary
[cluster
->nr
] = extent_key
->objectid
;
3025 if (cluster
->nr
>= MAX_EXTENTS
) {
3026 ret
= relocate_file_extent_cluster(inode
, cluster
);
3034 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3035 static int get_ref_objectid_v0(struct reloc_control
*rc
,
3036 struct btrfs_path
*path
,
3037 struct btrfs_key
*extent_key
,
3038 u64
*ref_objectid
, int *path_change
)
3040 struct btrfs_key key
;
3041 struct extent_buffer
*leaf
;
3042 struct btrfs_extent_ref_v0
*ref0
;
3046 leaf
= path
->nodes
[0];
3047 slot
= path
->slots
[0];
3049 if (slot
>= btrfs_header_nritems(leaf
)) {
3050 ret
= btrfs_next_leaf(rc
->extent_root
, path
);
3054 leaf
= path
->nodes
[0];
3055 slot
= path
->slots
[0];
3059 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
3060 if (key
.objectid
!= extent_key
->objectid
)
3063 if (key
.type
!= BTRFS_EXTENT_REF_V0_KEY
) {
3067 ref0
= btrfs_item_ptr(leaf
, slot
,
3068 struct btrfs_extent_ref_v0
);
3069 *ref_objectid
= btrfs_ref_objectid_v0(leaf
, ref0
);
3077 * helper to add a tree block to the list.
3078 * the major work is getting the generation and level of the block
3080 static int add_tree_block(struct reloc_control
*rc
,
3081 struct btrfs_key
*extent_key
,
3082 struct btrfs_path
*path
,
3083 struct rb_root
*blocks
)
3085 struct extent_buffer
*eb
;
3086 struct btrfs_extent_item
*ei
;
3087 struct btrfs_tree_block_info
*bi
;
3088 struct tree_block
*block
;
3089 struct rb_node
*rb_node
;
3094 eb
= path
->nodes
[0];
3095 item_size
= btrfs_item_size_nr(eb
, path
->slots
[0]);
3097 if (item_size
>= sizeof(*ei
) + sizeof(*bi
)) {
3098 ei
= btrfs_item_ptr(eb
, path
->slots
[0],
3099 struct btrfs_extent_item
);
3100 bi
= (struct btrfs_tree_block_info
*)(ei
+ 1);
3101 generation
= btrfs_extent_generation(eb
, ei
);
3102 level
= btrfs_tree_block_level(eb
, bi
);
3104 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3108 BUG_ON(item_size
!= sizeof(struct btrfs_extent_item_v0
));
3109 ret
= get_ref_objectid_v0(rc
, path
, extent_key
,
3113 BUG_ON(ref_owner
>= BTRFS_MAX_LEVEL
);
3114 level
= (int)ref_owner
;
3115 /* FIXME: get real generation */
3122 btrfs_release_path(rc
->extent_root
, path
);
3124 BUG_ON(level
== -1);
3126 block
= kmalloc(sizeof(*block
), GFP_NOFS
);
3130 block
->bytenr
= extent_key
->objectid
;
3131 block
->key
.objectid
= extent_key
->offset
;
3132 block
->key
.offset
= generation
;
3133 block
->level
= level
;
3134 block
->key_ready
= 0;
3136 rb_node
= tree_insert(blocks
, block
->bytenr
, &block
->rb_node
);
3143 * helper to add tree blocks for backref of type BTRFS_SHARED_DATA_REF_KEY
3145 static int __add_tree_block(struct reloc_control
*rc
,
3146 u64 bytenr
, u32 blocksize
,
3147 struct rb_root
*blocks
)
3149 struct btrfs_path
*path
;
3150 struct btrfs_key key
;
3153 if (tree_block_processed(bytenr
, blocksize
, rc
))
3156 if (tree_search(blocks
, bytenr
))
3159 path
= btrfs_alloc_path();
3163 key
.objectid
= bytenr
;
3164 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
3165 key
.offset
= blocksize
;
3167 path
->search_commit_root
= 1;
3168 path
->skip_locking
= 1;
3169 ret
= btrfs_search_slot(NULL
, rc
->extent_root
, &key
, path
, 0, 0);
3174 btrfs_item_key_to_cpu(path
->nodes
[0], &key
, path
->slots
[0]);
3175 ret
= add_tree_block(rc
, &key
, path
, blocks
);
3177 btrfs_free_path(path
);
3182 * helper to check if the block use full backrefs for pointers in it
3184 static int block_use_full_backref(struct reloc_control
*rc
,
3185 struct extent_buffer
*eb
)
3190 if (btrfs_header_flag(eb
, BTRFS_HEADER_FLAG_RELOC
) ||
3191 btrfs_header_backref_rev(eb
) < BTRFS_MIXED_BACKREF_REV
)
3194 ret
= btrfs_lookup_extent_info(NULL
, rc
->extent_root
,
3195 eb
->start
, eb
->len
, NULL
, &flags
);
3198 if (flags
& BTRFS_BLOCK_FLAG_FULL_BACKREF
)
3205 static int delete_block_group_cache(struct btrfs_fs_info
*fs_info
,
3206 struct inode
*inode
, u64 ino
)
3208 struct btrfs_key key
;
3209 struct btrfs_path
*path
;
3210 struct btrfs_root
*root
= fs_info
->tree_root
;
3211 struct btrfs_trans_handle
*trans
;
3219 key
.type
= BTRFS_INODE_ITEM_KEY
;
3222 inode
= btrfs_iget(fs_info
->sb
, &key
, root
, NULL
);
3223 if (!inode
|| IS_ERR(inode
) || is_bad_inode(inode
)) {
3224 if (inode
&& !IS_ERR(inode
))
3230 path
= btrfs_alloc_path();
3236 trans
= btrfs_join_transaction(root
, 0);
3237 if (IS_ERR(trans
)) {
3238 btrfs_free_path(path
);
3239 ret
= PTR_ERR(trans
);
3243 ret
= btrfs_truncate_free_space_cache(root
, trans
, path
, inode
);
3245 btrfs_free_path(path
);
3246 nr
= trans
->blocks_used
;
3247 btrfs_end_transaction(trans
, root
);
3248 btrfs_btree_balance_dirty(root
, nr
);
3255 * helper to add tree blocks for backref of type BTRFS_EXTENT_DATA_REF_KEY
3256 * this function scans fs tree to find blocks reference the data extent
3258 static int find_data_references(struct reloc_control
*rc
,
3259 struct btrfs_key
*extent_key
,
3260 struct extent_buffer
*leaf
,
3261 struct btrfs_extent_data_ref
*ref
,
3262 struct rb_root
*blocks
)
3264 struct btrfs_path
*path
;
3265 struct tree_block
*block
;
3266 struct btrfs_root
*root
;
3267 struct btrfs_file_extent_item
*fi
;
3268 struct rb_node
*rb_node
;
3269 struct btrfs_key key
;
3280 ref_root
= btrfs_extent_data_ref_root(leaf
, ref
);
3281 ref_objectid
= btrfs_extent_data_ref_objectid(leaf
, ref
);
3282 ref_offset
= btrfs_extent_data_ref_offset(leaf
, ref
);
3283 ref_count
= btrfs_extent_data_ref_count(leaf
, ref
);
3286 * This is an extent belonging to the free space cache, lets just delete
3287 * it and redo the search.
3289 if (ref_root
== BTRFS_ROOT_TREE_OBJECTID
) {
3290 ret
= delete_block_group_cache(rc
->extent_root
->fs_info
,
3291 NULL
, ref_objectid
);
3297 path
= btrfs_alloc_path();
3301 root
= read_fs_root(rc
->extent_root
->fs_info
, ref_root
);
3303 err
= PTR_ERR(root
);
3307 key
.objectid
= ref_objectid
;
3308 key
.offset
= ref_offset
;
3309 key
.type
= BTRFS_EXTENT_DATA_KEY
;
3311 path
->search_commit_root
= 1;
3312 path
->skip_locking
= 1;
3313 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
3319 leaf
= path
->nodes
[0];
3320 nritems
= btrfs_header_nritems(leaf
);
3322 * the references in tree blocks that use full backrefs
3323 * are not counted in
3325 if (block_use_full_backref(rc
, leaf
))
3329 rb_node
= tree_search(blocks
, leaf
->start
);
3334 path
->slots
[0] = nritems
;
3337 while (ref_count
> 0) {
3338 while (path
->slots
[0] >= nritems
) {
3339 ret
= btrfs_next_leaf(root
, path
);
3349 leaf
= path
->nodes
[0];
3350 nritems
= btrfs_header_nritems(leaf
);
3353 if (block_use_full_backref(rc
, leaf
))
3357 rb_node
= tree_search(blocks
, leaf
->start
);
3362 path
->slots
[0] = nritems
;
3366 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
3367 if (key
.objectid
!= ref_objectid
||
3368 key
.type
!= BTRFS_EXTENT_DATA_KEY
) {
3373 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
3374 struct btrfs_file_extent_item
);
3376 if (btrfs_file_extent_type(leaf
, fi
) ==
3377 BTRFS_FILE_EXTENT_INLINE
)
3380 if (btrfs_file_extent_disk_bytenr(leaf
, fi
) !=
3381 extent_key
->objectid
)
3384 key
.offset
-= btrfs_file_extent_offset(leaf
, fi
);
3385 if (key
.offset
!= ref_offset
)
3393 if (!tree_block_processed(leaf
->start
, leaf
->len
, rc
)) {
3394 block
= kmalloc(sizeof(*block
), GFP_NOFS
);
3399 block
->bytenr
= leaf
->start
;
3400 btrfs_item_key_to_cpu(leaf
, &block
->key
, 0);
3402 block
->key_ready
= 1;
3403 rb_node
= tree_insert(blocks
, block
->bytenr
,
3410 path
->slots
[0] = nritems
;
3416 btrfs_free_path(path
);
3421 * hepler to find all tree blocks that reference a given data extent
3423 static noinline_for_stack
3424 int add_data_references(struct reloc_control
*rc
,
3425 struct btrfs_key
*extent_key
,
3426 struct btrfs_path
*path
,
3427 struct rb_root
*blocks
)
3429 struct btrfs_key key
;
3430 struct extent_buffer
*eb
;
3431 struct btrfs_extent_data_ref
*dref
;
3432 struct btrfs_extent_inline_ref
*iref
;
3435 u32 blocksize
= btrfs_level_size(rc
->extent_root
, 0);
3439 eb
= path
->nodes
[0];
3440 ptr
= btrfs_item_ptr_offset(eb
, path
->slots
[0]);
3441 end
= ptr
+ btrfs_item_size_nr(eb
, path
->slots
[0]);
3442 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3443 if (ptr
+ sizeof(struct btrfs_extent_item_v0
) == end
)
3447 ptr
+= sizeof(struct btrfs_extent_item
);
3450 iref
= (struct btrfs_extent_inline_ref
*)ptr
;
3451 key
.type
= btrfs_extent_inline_ref_type(eb
, iref
);
3452 if (key
.type
== BTRFS_SHARED_DATA_REF_KEY
) {
3453 key
.offset
= btrfs_extent_inline_ref_offset(eb
, iref
);
3454 ret
= __add_tree_block(rc
, key
.offset
, blocksize
,
3456 } else if (key
.type
== BTRFS_EXTENT_DATA_REF_KEY
) {
3457 dref
= (struct btrfs_extent_data_ref
*)(&iref
->offset
);
3458 ret
= find_data_references(rc
, extent_key
,
3463 ptr
+= btrfs_extent_inline_ref_size(key
.type
);
3469 eb
= path
->nodes
[0];
3470 if (path
->slots
[0] >= btrfs_header_nritems(eb
)) {
3471 ret
= btrfs_next_leaf(rc
->extent_root
, path
);
3478 eb
= path
->nodes
[0];
3481 btrfs_item_key_to_cpu(eb
, &key
, path
->slots
[0]);
3482 if (key
.objectid
!= extent_key
->objectid
)
3485 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3486 if (key
.type
== BTRFS_SHARED_DATA_REF_KEY
||
3487 key
.type
== BTRFS_EXTENT_REF_V0_KEY
) {
3489 BUG_ON(key
.type
== BTRFS_EXTENT_REF_V0_KEY
);
3490 if (key
.type
== BTRFS_SHARED_DATA_REF_KEY
) {
3492 ret
= __add_tree_block(rc
, key
.offset
, blocksize
,
3494 } else if (key
.type
== BTRFS_EXTENT_DATA_REF_KEY
) {
3495 dref
= btrfs_item_ptr(eb
, path
->slots
[0],
3496 struct btrfs_extent_data_ref
);
3497 ret
= find_data_references(rc
, extent_key
,
3508 btrfs_release_path(rc
->extent_root
, path
);
3510 free_block_list(blocks
);
3515 * hepler to find next unprocessed extent
3517 static noinline_for_stack
3518 int find_next_extent(struct btrfs_trans_handle
*trans
,
3519 struct reloc_control
*rc
, struct btrfs_path
*path
,
3520 struct btrfs_key
*extent_key
)
3522 struct btrfs_key key
;
3523 struct extent_buffer
*leaf
;
3524 u64 start
, end
, last
;
3527 last
= rc
->block_group
->key
.objectid
+ rc
->block_group
->key
.offset
;
3530 if (rc
->search_start
>= last
) {
3535 key
.objectid
= rc
->search_start
;
3536 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
3539 path
->search_commit_root
= 1;
3540 path
->skip_locking
= 1;
3541 ret
= btrfs_search_slot(NULL
, rc
->extent_root
, &key
, path
,
3546 leaf
= path
->nodes
[0];
3547 if (path
->slots
[0] >= btrfs_header_nritems(leaf
)) {
3548 ret
= btrfs_next_leaf(rc
->extent_root
, path
);
3551 leaf
= path
->nodes
[0];
3554 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
3555 if (key
.objectid
>= last
) {
3560 if (key
.type
!= BTRFS_EXTENT_ITEM_KEY
||
3561 key
.objectid
+ key
.offset
<= rc
->search_start
) {
3566 ret
= find_first_extent_bit(&rc
->processed_blocks
,
3567 key
.objectid
, &start
, &end
,
3570 if (ret
== 0 && start
<= key
.objectid
) {
3571 btrfs_release_path(rc
->extent_root
, path
);
3572 rc
->search_start
= end
+ 1;
3574 rc
->search_start
= key
.objectid
+ key
.offset
;
3575 memcpy(extent_key
, &key
, sizeof(key
));
3579 btrfs_release_path(rc
->extent_root
, path
);
3583 static void set_reloc_control(struct reloc_control
*rc
)
3585 struct btrfs_fs_info
*fs_info
= rc
->extent_root
->fs_info
;
3586 mutex_lock(&fs_info
->trans_mutex
);
3587 fs_info
->reloc_ctl
= rc
;
3588 mutex_unlock(&fs_info
->trans_mutex
);
3591 static void unset_reloc_control(struct reloc_control
*rc
)
3593 struct btrfs_fs_info
*fs_info
= rc
->extent_root
->fs_info
;
3594 mutex_lock(&fs_info
->trans_mutex
);
3595 fs_info
->reloc_ctl
= NULL
;
3596 mutex_unlock(&fs_info
->trans_mutex
);
3599 static int check_extent_flags(u64 flags
)
3601 if ((flags
& BTRFS_EXTENT_FLAG_DATA
) &&
3602 (flags
& BTRFS_EXTENT_FLAG_TREE_BLOCK
))
3604 if (!(flags
& BTRFS_EXTENT_FLAG_DATA
) &&
3605 !(flags
& BTRFS_EXTENT_FLAG_TREE_BLOCK
))
3607 if ((flags
& BTRFS_EXTENT_FLAG_DATA
) &&
3608 (flags
& BTRFS_BLOCK_FLAG_FULL_BACKREF
))
3613 static noinline_for_stack
3614 int prepare_to_relocate(struct reloc_control
*rc
)
3616 struct btrfs_trans_handle
*trans
;
3619 rc
->block_rsv
= btrfs_alloc_block_rsv(rc
->extent_root
);
3624 * reserve some space for creating reloc trees.
3625 * btrfs_init_reloc_root will use them when there
3626 * is no reservation in transaction handle.
3628 ret
= btrfs_block_rsv_add(NULL
, rc
->extent_root
, rc
->block_rsv
,
3629 rc
->extent_root
->nodesize
* 256);
3633 rc
->block_rsv
->refill_used
= 1;
3634 btrfs_add_durable_block_rsv(rc
->extent_root
->fs_info
, rc
->block_rsv
);
3636 memset(&rc
->cluster
, 0, sizeof(rc
->cluster
));
3637 rc
->search_start
= rc
->block_group
->key
.objectid
;
3638 rc
->extents_found
= 0;
3639 rc
->nodes_relocated
= 0;
3640 rc
->merging_rsv_size
= 0;
3642 rc
->create_reloc_tree
= 1;
3643 set_reloc_control(rc
);
3645 trans
= btrfs_join_transaction(rc
->extent_root
, 1);
3646 BUG_ON(IS_ERR(trans
));
3647 btrfs_commit_transaction(trans
, rc
->extent_root
);
3651 static noinline_for_stack
int relocate_block_group(struct reloc_control
*rc
)
3653 struct rb_root blocks
= RB_ROOT
;
3654 struct btrfs_key key
;
3655 struct btrfs_trans_handle
*trans
= NULL
;
3656 struct btrfs_path
*path
;
3657 struct btrfs_extent_item
*ei
;
3665 path
= btrfs_alloc_path();
3669 ret
= prepare_to_relocate(rc
);
3677 trans
= btrfs_start_transaction(rc
->extent_root
, 0);
3678 BUG_ON(IS_ERR(trans
));
3680 if (update_backref_cache(trans
, &rc
->backref_cache
)) {
3681 btrfs_end_transaction(trans
, rc
->extent_root
);
3685 ret
= find_next_extent(trans
, rc
, path
, &key
);
3691 rc
->extents_found
++;
3693 ei
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
3694 struct btrfs_extent_item
);
3695 item_size
= btrfs_item_size_nr(path
->nodes
[0], path
->slots
[0]);
3696 if (item_size
>= sizeof(*ei
)) {
3697 flags
= btrfs_extent_flags(path
->nodes
[0], ei
);
3698 ret
= check_extent_flags(flags
);
3702 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3704 int path_change
= 0;
3707 sizeof(struct btrfs_extent_item_v0
));
3708 ret
= get_ref_objectid_v0(rc
, path
, &key
, &ref_owner
,
3710 if (ref_owner
< BTRFS_FIRST_FREE_OBJECTID
)
3711 flags
= BTRFS_EXTENT_FLAG_TREE_BLOCK
;
3713 flags
= BTRFS_EXTENT_FLAG_DATA
;
3716 btrfs_release_path(rc
->extent_root
, path
);
3718 path
->search_commit_root
= 1;
3719 path
->skip_locking
= 1;
3720 ret
= btrfs_search_slot(NULL
, rc
->extent_root
,
3733 if (flags
& BTRFS_EXTENT_FLAG_TREE_BLOCK
) {
3734 ret
= add_tree_block(rc
, &key
, path
, &blocks
);
3735 } else if (rc
->stage
== UPDATE_DATA_PTRS
&&
3736 (flags
& BTRFS_EXTENT_FLAG_DATA
)) {
3737 ret
= add_data_references(rc
, &key
, path
, &blocks
);
3739 btrfs_release_path(rc
->extent_root
, path
);
3747 if (!RB_EMPTY_ROOT(&blocks
)) {
3748 ret
= relocate_tree_blocks(trans
, rc
, &blocks
);
3750 if (ret
!= -EAGAIN
) {
3754 rc
->extents_found
--;
3755 rc
->search_start
= key
.objectid
;
3759 ret
= btrfs_block_rsv_check(trans
, rc
->extent_root
,
3760 rc
->block_rsv
, 0, 5);
3762 if (ret
!= -EAGAIN
) {
3767 rc
->commit_transaction
= 1;
3770 if (rc
->commit_transaction
) {
3771 rc
->commit_transaction
= 0;
3772 ret
= btrfs_commit_transaction(trans
, rc
->extent_root
);
3775 nr
= trans
->blocks_used
;
3776 btrfs_end_transaction_throttle(trans
, rc
->extent_root
);
3777 btrfs_btree_balance_dirty(rc
->extent_root
, nr
);
3781 if (rc
->stage
== MOVE_DATA_EXTENTS
&&
3782 (flags
& BTRFS_EXTENT_FLAG_DATA
)) {
3783 rc
->found_file_extent
= 1;
3784 ret
= relocate_data_extent(rc
->data_inode
,
3785 &key
, &rc
->cluster
);
3792 if (trans
&& progress
&& err
== -ENOSPC
) {
3793 ret
= btrfs_force_chunk_alloc(trans
, rc
->extent_root
,
3794 rc
->block_group
->flags
);
3802 btrfs_release_path(rc
->extent_root
, path
);
3803 clear_extent_bits(&rc
->processed_blocks
, 0, (u64
)-1, EXTENT_DIRTY
,
3807 nr
= trans
->blocks_used
;
3808 btrfs_end_transaction_throttle(trans
, rc
->extent_root
);
3809 btrfs_btree_balance_dirty(rc
->extent_root
, nr
);
3813 ret
= relocate_file_extent_cluster(rc
->data_inode
,
3819 rc
->create_reloc_tree
= 0;
3820 set_reloc_control(rc
);
3822 backref_cache_cleanup(&rc
->backref_cache
);
3823 btrfs_block_rsv_release(rc
->extent_root
, rc
->block_rsv
, (u64
)-1);
3825 err
= prepare_to_merge(rc
, err
);
3827 merge_reloc_roots(rc
);
3829 rc
->merge_reloc_tree
= 0;
3830 unset_reloc_control(rc
);
3831 btrfs_block_rsv_release(rc
->extent_root
, rc
->block_rsv
, (u64
)-1);
3833 /* get rid of pinned extents */
3834 trans
= btrfs_join_transaction(rc
->extent_root
, 1);
3836 err
= PTR_ERR(trans
);
3838 btrfs_commit_transaction(trans
, rc
->extent_root
);
3840 btrfs_free_block_rsv(rc
->extent_root
, rc
->block_rsv
);
3841 btrfs_free_path(path
);
3845 static int __insert_orphan_inode(struct btrfs_trans_handle
*trans
,
3846 struct btrfs_root
*root
, u64 objectid
)
3848 struct btrfs_path
*path
;
3849 struct btrfs_inode_item
*item
;
3850 struct extent_buffer
*leaf
;
3853 path
= btrfs_alloc_path();
3857 ret
= btrfs_insert_empty_inode(trans
, root
, path
, objectid
);
3861 leaf
= path
->nodes
[0];
3862 item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_inode_item
);
3863 memset_extent_buffer(leaf
, 0, (unsigned long)item
, sizeof(*item
));
3864 btrfs_set_inode_generation(leaf
, item
, 1);
3865 btrfs_set_inode_size(leaf
, item
, 0);
3866 btrfs_set_inode_mode(leaf
, item
, S_IFREG
| 0600);
3867 btrfs_set_inode_flags(leaf
, item
, BTRFS_INODE_NOCOMPRESS
|
3868 BTRFS_INODE_PREALLOC
);
3869 btrfs_mark_buffer_dirty(leaf
);
3870 btrfs_release_path(root
, path
);
3872 btrfs_free_path(path
);
3877 * helper to create inode for data relocation.
3878 * the inode is in data relocation tree and its link count is 0
3880 static noinline_for_stack
3881 struct inode
*create_reloc_inode(struct btrfs_fs_info
*fs_info
,
3882 struct btrfs_block_group_cache
*group
)
3884 struct inode
*inode
= NULL
;
3885 struct btrfs_trans_handle
*trans
;
3886 struct btrfs_root
*root
;
3887 struct btrfs_key key
;
3889 u64 objectid
= BTRFS_FIRST_FREE_OBJECTID
;
3892 root
= read_fs_root(fs_info
, BTRFS_DATA_RELOC_TREE_OBJECTID
);
3894 return ERR_CAST(root
);
3896 trans
= btrfs_start_transaction(root
, 6);
3898 return ERR_CAST(trans
);
3900 err
= btrfs_find_free_objectid(trans
, root
, objectid
, &objectid
);
3904 err
= __insert_orphan_inode(trans
, root
, objectid
);
3907 key
.objectid
= objectid
;
3908 key
.type
= BTRFS_INODE_ITEM_KEY
;
3910 inode
= btrfs_iget(root
->fs_info
->sb
, &key
, root
, NULL
);
3911 BUG_ON(IS_ERR(inode
) || is_bad_inode(inode
));
3912 BTRFS_I(inode
)->index_cnt
= group
->key
.objectid
;
3914 err
= btrfs_orphan_add(trans
, inode
);
3916 nr
= trans
->blocks_used
;
3917 btrfs_end_transaction(trans
, root
);
3918 btrfs_btree_balance_dirty(root
, nr
);
3922 inode
= ERR_PTR(err
);
3927 static struct reloc_control
*alloc_reloc_control(void)
3929 struct reloc_control
*rc
;
3931 rc
= kzalloc(sizeof(*rc
), GFP_NOFS
);
3935 INIT_LIST_HEAD(&rc
->reloc_roots
);
3936 backref_cache_init(&rc
->backref_cache
);
3937 mapping_tree_init(&rc
->reloc_root_tree
);
3938 extent_io_tree_init(&rc
->processed_blocks
, NULL
, GFP_NOFS
);
3943 * function to relocate all extents in a block group.
3945 int btrfs_relocate_block_group(struct btrfs_root
*extent_root
, u64 group_start
)
3947 struct btrfs_fs_info
*fs_info
= extent_root
->fs_info
;
3948 struct reloc_control
*rc
;
3949 struct inode
*inode
;
3950 struct btrfs_path
*path
;
3955 rc
= alloc_reloc_control();
3959 rc
->extent_root
= extent_root
;
3961 rc
->block_group
= btrfs_lookup_block_group(fs_info
, group_start
);
3962 BUG_ON(!rc
->block_group
);
3964 if (!rc
->block_group
->ro
) {
3965 ret
= btrfs_set_block_group_ro(extent_root
, rc
->block_group
);
3973 path
= btrfs_alloc_path();
3979 inode
= lookup_free_space_inode(fs_info
->tree_root
, rc
->block_group
,
3981 btrfs_free_path(path
);
3984 ret
= delete_block_group_cache(fs_info
, inode
, 0);
3986 ret
= PTR_ERR(inode
);
3988 if (ret
&& ret
!= -ENOENT
) {
3993 rc
->data_inode
= create_reloc_inode(fs_info
, rc
->block_group
);
3994 if (IS_ERR(rc
->data_inode
)) {
3995 err
= PTR_ERR(rc
->data_inode
);
3996 rc
->data_inode
= NULL
;
4000 printk(KERN_INFO
"btrfs: relocating block group %llu flags %llu\n",
4001 (unsigned long long)rc
->block_group
->key
.objectid
,
4002 (unsigned long long)rc
->block_group
->flags
);
4004 btrfs_start_delalloc_inodes(fs_info
->tree_root
, 0);
4005 btrfs_wait_ordered_extents(fs_info
->tree_root
, 0, 0);
4008 mutex_lock(&fs_info
->cleaner_mutex
);
4010 btrfs_clean_old_snapshots(fs_info
->tree_root
);
4011 ret
= relocate_block_group(rc
);
4013 mutex_unlock(&fs_info
->cleaner_mutex
);
4019 if (rc
->extents_found
== 0)
4022 printk(KERN_INFO
"btrfs: found %llu extents\n",
4023 (unsigned long long)rc
->extents_found
);
4025 if (rc
->stage
== MOVE_DATA_EXTENTS
&& rc
->found_file_extent
) {
4026 btrfs_wait_ordered_range(rc
->data_inode
, 0, (u64
)-1);
4027 invalidate_mapping_pages(rc
->data_inode
->i_mapping
,
4029 rc
->stage
= UPDATE_DATA_PTRS
;
4033 filemap_write_and_wait_range(fs_info
->btree_inode
->i_mapping
,
4034 rc
->block_group
->key
.objectid
,
4035 rc
->block_group
->key
.objectid
+
4036 rc
->block_group
->key
.offset
- 1);
4038 WARN_ON(rc
->block_group
->pinned
> 0);
4039 WARN_ON(rc
->block_group
->reserved
> 0);
4040 WARN_ON(btrfs_block_group_used(&rc
->block_group
->item
) > 0);
4043 btrfs_set_block_group_rw(extent_root
, rc
->block_group
);
4044 iput(rc
->data_inode
);
4045 btrfs_put_block_group(rc
->block_group
);
4050 static noinline_for_stack
int mark_garbage_root(struct btrfs_root
*root
)
4052 struct btrfs_trans_handle
*trans
;
4055 trans
= btrfs_start_transaction(root
->fs_info
->tree_root
, 0);
4056 BUG_ON(IS_ERR(trans
));
4058 memset(&root
->root_item
.drop_progress
, 0,
4059 sizeof(root
->root_item
.drop_progress
));
4060 root
->root_item
.drop_level
= 0;
4061 btrfs_set_root_refs(&root
->root_item
, 0);
4062 ret
= btrfs_update_root(trans
, root
->fs_info
->tree_root
,
4063 &root
->root_key
, &root
->root_item
);
4066 ret
= btrfs_end_transaction(trans
, root
->fs_info
->tree_root
);
4072 * recover relocation interrupted by system crash.
4074 * this function resumes merging reloc trees with corresponding fs trees.
4075 * this is important for keeping the sharing of tree blocks
4077 int btrfs_recover_relocation(struct btrfs_root
*root
)
4079 LIST_HEAD(reloc_roots
);
4080 struct btrfs_key key
;
4081 struct btrfs_root
*fs_root
;
4082 struct btrfs_root
*reloc_root
;
4083 struct btrfs_path
*path
;
4084 struct extent_buffer
*leaf
;
4085 struct reloc_control
*rc
= NULL
;
4086 struct btrfs_trans_handle
*trans
;
4090 path
= btrfs_alloc_path();
4094 key
.objectid
= BTRFS_TREE_RELOC_OBJECTID
;
4095 key
.type
= BTRFS_ROOT_ITEM_KEY
;
4096 key
.offset
= (u64
)-1;
4099 ret
= btrfs_search_slot(NULL
, root
->fs_info
->tree_root
, &key
,
4106 if (path
->slots
[0] == 0)
4110 leaf
= path
->nodes
[0];
4111 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
4112 btrfs_release_path(root
->fs_info
->tree_root
, path
);
4114 if (key
.objectid
!= BTRFS_TREE_RELOC_OBJECTID
||
4115 key
.type
!= BTRFS_ROOT_ITEM_KEY
)
4118 reloc_root
= btrfs_read_fs_root_no_radix(root
, &key
);
4119 if (IS_ERR(reloc_root
)) {
4120 err
= PTR_ERR(reloc_root
);
4124 list_add(&reloc_root
->root_list
, &reloc_roots
);
4126 if (btrfs_root_refs(&reloc_root
->root_item
) > 0) {
4127 fs_root
= read_fs_root(root
->fs_info
,
4128 reloc_root
->root_key
.offset
);
4129 if (IS_ERR(fs_root
)) {
4130 ret
= PTR_ERR(fs_root
);
4131 if (ret
!= -ENOENT
) {
4135 mark_garbage_root(reloc_root
);
4139 if (key
.offset
== 0)
4144 btrfs_release_path(root
->fs_info
->tree_root
, path
);
4146 if (list_empty(&reloc_roots
))
4149 rc
= alloc_reloc_control();
4155 rc
->extent_root
= root
->fs_info
->extent_root
;
4157 set_reloc_control(rc
);
4159 trans
= btrfs_join_transaction(rc
->extent_root
, 1);
4160 if (IS_ERR(trans
)) {
4161 unset_reloc_control(rc
);
4162 err
= PTR_ERR(trans
);
4166 rc
->merge_reloc_tree
= 1;
4168 while (!list_empty(&reloc_roots
)) {
4169 reloc_root
= list_entry(reloc_roots
.next
,
4170 struct btrfs_root
, root_list
);
4171 list_del(&reloc_root
->root_list
);
4173 if (btrfs_root_refs(&reloc_root
->root_item
) == 0) {
4174 list_add_tail(&reloc_root
->root_list
,
4179 fs_root
= read_fs_root(root
->fs_info
,
4180 reloc_root
->root_key
.offset
);
4181 BUG_ON(IS_ERR(fs_root
));
4183 __add_reloc_root(reloc_root
);
4184 fs_root
->reloc_root
= reloc_root
;
4187 btrfs_commit_transaction(trans
, rc
->extent_root
);
4189 merge_reloc_roots(rc
);
4191 unset_reloc_control(rc
);
4193 trans
= btrfs_join_transaction(rc
->extent_root
, 1);
4195 err
= PTR_ERR(trans
);
4197 btrfs_commit_transaction(trans
, rc
->extent_root
);
4201 while (!list_empty(&reloc_roots
)) {
4202 reloc_root
= list_entry(reloc_roots
.next
,
4203 struct btrfs_root
, root_list
);
4204 list_del(&reloc_root
->root_list
);
4205 free_extent_buffer(reloc_root
->node
);
4206 free_extent_buffer(reloc_root
->commit_root
);
4209 btrfs_free_path(path
);
4212 /* cleanup orphan inode in data relocation tree */
4213 fs_root
= read_fs_root(root
->fs_info
,
4214 BTRFS_DATA_RELOC_TREE_OBJECTID
);
4215 if (IS_ERR(fs_root
))
4216 err
= PTR_ERR(fs_root
);
4218 err
= btrfs_orphan_cleanup(fs_root
);
4224 * helper to add ordered checksum for data relocation.
4226 * cloning checksum properly handles the nodatasum extents.
4227 * it also saves CPU time to re-calculate the checksum.
4229 int btrfs_reloc_clone_csums(struct inode
*inode
, u64 file_pos
, u64 len
)
4231 struct btrfs_ordered_sum
*sums
;
4232 struct btrfs_sector_sum
*sector_sum
;
4233 struct btrfs_ordered_extent
*ordered
;
4234 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4240 ordered
= btrfs_lookup_ordered_extent(inode
, file_pos
);
4241 BUG_ON(ordered
->file_offset
!= file_pos
|| ordered
->len
!= len
);
4243 disk_bytenr
= file_pos
+ BTRFS_I(inode
)->index_cnt
;
4244 ret
= btrfs_lookup_csums_range(root
->fs_info
->csum_root
, disk_bytenr
,
4245 disk_bytenr
+ len
- 1, &list
);
4247 while (!list_empty(&list
)) {
4248 sums
= list_entry(list
.next
, struct btrfs_ordered_sum
, list
);
4249 list_del_init(&sums
->list
);
4251 sector_sum
= sums
->sums
;
4252 sums
->bytenr
= ordered
->start
;
4255 while (offset
< sums
->len
) {
4256 sector_sum
->bytenr
+= ordered
->start
- disk_bytenr
;
4258 offset
+= root
->sectorsize
;
4261 btrfs_add_ordered_sum(inode
, ordered
, sums
);
4263 btrfs_put_ordered_extent(ordered
);
4267 void btrfs_reloc_cow_block(struct btrfs_trans_handle
*trans
,
4268 struct btrfs_root
*root
, struct extent_buffer
*buf
,
4269 struct extent_buffer
*cow
)
4271 struct reloc_control
*rc
;
4272 struct backref_node
*node
;
4277 rc
= root
->fs_info
->reloc_ctl
;
4281 BUG_ON(rc
->stage
== UPDATE_DATA_PTRS
&&
4282 root
->root_key
.objectid
== BTRFS_DATA_RELOC_TREE_OBJECTID
);
4284 level
= btrfs_header_level(buf
);
4285 if (btrfs_header_generation(buf
) <=
4286 btrfs_root_last_snapshot(&root
->root_item
))
4289 if (root
->root_key
.objectid
== BTRFS_TREE_RELOC_OBJECTID
&&
4290 rc
->create_reloc_tree
) {
4291 WARN_ON(!first_cow
&& level
== 0);
4293 node
= rc
->backref_cache
.path
[level
];
4294 BUG_ON(node
->bytenr
!= buf
->start
&&
4295 node
->new_bytenr
!= buf
->start
);
4297 drop_node_buffer(node
);
4298 extent_buffer_get(cow
);
4300 node
->new_bytenr
= cow
->start
;
4302 if (!node
->pending
) {
4303 list_move_tail(&node
->list
,
4304 &rc
->backref_cache
.pending
[level
]);
4309 __mark_block_processed(rc
, node
);
4311 if (first_cow
&& level
> 0)
4312 rc
->nodes_relocated
+= buf
->len
;
4315 if (level
== 0 && first_cow
&& rc
->stage
== UPDATE_DATA_PTRS
) {
4316 ret
= replace_file_extents(trans
, rc
, root
, cow
);
4322 * called before creating snapshot. it calculates metadata reservation
4323 * requried for relocating tree blocks in the snapshot
4325 void btrfs_reloc_pre_snapshot(struct btrfs_trans_handle
*trans
,
4326 struct btrfs_pending_snapshot
*pending
,
4327 u64
*bytes_to_reserve
)
4329 struct btrfs_root
*root
;
4330 struct reloc_control
*rc
;
4332 root
= pending
->root
;
4333 if (!root
->reloc_root
)
4336 rc
= root
->fs_info
->reloc_ctl
;
4337 if (!rc
->merge_reloc_tree
)
4340 root
= root
->reloc_root
;
4341 BUG_ON(btrfs_root_refs(&root
->root_item
) == 0);
4343 * relocation is in the stage of merging trees. the space
4344 * used by merging a reloc tree is twice the size of
4345 * relocated tree nodes in the worst case. half for cowing
4346 * the reloc tree, half for cowing the fs tree. the space
4347 * used by cowing the reloc tree will be freed after the
4348 * tree is dropped. if we create snapshot, cowing the fs
4349 * tree may use more space than it frees. so we need
4350 * reserve extra space.
4352 *bytes_to_reserve
+= rc
->nodes_relocated
;
4356 * called after snapshot is created. migrate block reservation
4357 * and create reloc root for the newly created snapshot
4359 void btrfs_reloc_post_snapshot(struct btrfs_trans_handle
*trans
,
4360 struct btrfs_pending_snapshot
*pending
)
4362 struct btrfs_root
*root
= pending
->root
;
4363 struct btrfs_root
*reloc_root
;
4364 struct btrfs_root
*new_root
;
4365 struct reloc_control
*rc
;
4368 if (!root
->reloc_root
)
4371 rc
= root
->fs_info
->reloc_ctl
;
4372 rc
->merging_rsv_size
+= rc
->nodes_relocated
;
4374 if (rc
->merge_reloc_tree
) {
4375 ret
= btrfs_block_rsv_migrate(&pending
->block_rsv
,
4377 rc
->nodes_relocated
);
4381 new_root
= pending
->snap
;
4382 reloc_root
= create_reloc_root(trans
, root
->reloc_root
,
4383 new_root
->root_key
.objectid
);
4385 __add_reloc_root(reloc_root
);
4386 new_root
->reloc_root
= reloc_root
;
4388 if (rc
->create_reloc_tree
) {
4389 ret
= clone_backref_node(trans
, rc
, root
, reloc_root
);