2 * Copyright (C) 2009 Oracle. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
19 #include <linux/sched.h>
20 #include <linux/pagemap.h>
21 #include <linux/writeback.h>
22 #include <linux/blkdev.h>
23 #include <linux/rbtree.h>
24 #include <linux/slab.h>
27 #include "transaction.h"
30 #include "btrfs_inode.h"
31 #include "async-thread.h"
34 * backref_node, mapping_node and tree_block start with this
37 struct rb_node rb_node
;
42 * present a tree block in the backref cache
45 struct rb_node rb_node
;
49 /* objectid of tree block owner, can be not uptodate */
51 /* link to pending, changed or detached list */
52 struct list_head list
;
53 /* list of upper level blocks reference this block */
54 struct list_head upper
;
55 /* list of child blocks in the cache */
56 struct list_head lower
;
57 /* NULL if this node is not tree root */
58 struct btrfs_root
*root
;
59 /* extent buffer got by COW the block */
60 struct extent_buffer
*eb
;
61 /* level of tree block */
63 /* is the block in non-reference counted tree */
64 unsigned int cowonly
:1;
65 /* 1 if no child node in the cache */
66 unsigned int lowest
:1;
67 /* is the extent buffer locked */
68 unsigned int locked
:1;
69 /* has the block been processed */
70 unsigned int processed
:1;
71 /* have backrefs of this block been checked */
72 unsigned int checked
:1;
74 * 1 if corresponding block has been cowed but some upper
75 * level block pointers may not point to the new location
77 unsigned int pending
:1;
79 * 1 if the backref node isn't connected to any other
82 unsigned int detached
:1;
86 * present a block pointer in the backref cache
89 struct list_head list
[2];
90 struct backref_node
*node
[2];
96 struct backref_cache
{
97 /* red black tree of all backref nodes in the cache */
98 struct rb_root rb_root
;
99 /* for passing backref nodes to btrfs_reloc_cow_block */
100 struct backref_node
*path
[BTRFS_MAX_LEVEL
];
102 * list of blocks that have been cowed but some block
103 * pointers in upper level blocks may not reflect the
106 struct list_head pending
[BTRFS_MAX_LEVEL
];
107 /* list of backref nodes with no child node */
108 struct list_head leaves
;
109 /* list of blocks that have been cowed in current transaction */
110 struct list_head changed
;
111 /* list of detached backref node. */
112 struct list_head detached
;
121 * map address of tree root to tree
123 struct mapping_node
{
124 struct rb_node rb_node
;
129 struct mapping_tree
{
130 struct rb_root rb_root
;
135 * present a tree block to process
138 struct rb_node rb_node
;
140 struct btrfs_key key
;
141 unsigned int level
:8;
142 unsigned int key_ready
:1;
145 #define MAX_EXTENTS 128
147 struct file_extent_cluster
{
150 u64 boundary
[MAX_EXTENTS
];
154 struct reloc_control
{
155 /* block group to relocate */
156 struct btrfs_block_group_cache
*block_group
;
158 struct btrfs_root
*extent_root
;
159 /* inode for moving data */
160 struct inode
*data_inode
;
162 struct btrfs_block_rsv
*block_rsv
;
164 struct backref_cache backref_cache
;
166 struct file_extent_cluster cluster
;
167 /* tree blocks have been processed */
168 struct extent_io_tree processed_blocks
;
169 /* map start of tree root to corresponding reloc tree */
170 struct mapping_tree reloc_root_tree
;
171 /* list of reloc trees */
172 struct list_head reloc_roots
;
173 /* size of metadata reservation for merging reloc trees */
174 u64 merging_rsv_size
;
175 /* size of relocated tree nodes */
181 int block_rsv_retries
;
183 unsigned int stage
:8;
184 unsigned int create_reloc_tree
:1;
185 unsigned int merge_reloc_tree
:1;
186 unsigned int found_file_extent
:1;
187 unsigned int commit_transaction
:1;
190 /* stages of data relocation */
191 #define MOVE_DATA_EXTENTS 0
192 #define UPDATE_DATA_PTRS 1
194 static void remove_backref_node(struct backref_cache
*cache
,
195 struct backref_node
*node
);
196 static void __mark_block_processed(struct reloc_control
*rc
,
197 struct backref_node
*node
);
199 static void mapping_tree_init(struct mapping_tree
*tree
)
201 tree
->rb_root
= RB_ROOT
;
202 spin_lock_init(&tree
->lock
);
205 static void backref_cache_init(struct backref_cache
*cache
)
208 cache
->rb_root
= RB_ROOT
;
209 for (i
= 0; i
< BTRFS_MAX_LEVEL
; i
++)
210 INIT_LIST_HEAD(&cache
->pending
[i
]);
211 INIT_LIST_HEAD(&cache
->changed
);
212 INIT_LIST_HEAD(&cache
->detached
);
213 INIT_LIST_HEAD(&cache
->leaves
);
216 static void backref_cache_cleanup(struct backref_cache
*cache
)
218 struct backref_node
*node
;
221 while (!list_empty(&cache
->detached
)) {
222 node
= list_entry(cache
->detached
.next
,
223 struct backref_node
, list
);
224 remove_backref_node(cache
, node
);
227 while (!list_empty(&cache
->leaves
)) {
228 node
= list_entry(cache
->leaves
.next
,
229 struct backref_node
, lower
);
230 remove_backref_node(cache
, node
);
233 cache
->last_trans
= 0;
235 for (i
= 0; i
< BTRFS_MAX_LEVEL
; i
++)
236 BUG_ON(!list_empty(&cache
->pending
[i
]));
237 BUG_ON(!list_empty(&cache
->changed
));
238 BUG_ON(!list_empty(&cache
->detached
));
239 BUG_ON(!RB_EMPTY_ROOT(&cache
->rb_root
));
240 BUG_ON(cache
->nr_nodes
);
241 BUG_ON(cache
->nr_edges
);
244 static struct backref_node
*alloc_backref_node(struct backref_cache
*cache
)
246 struct backref_node
*node
;
248 node
= kzalloc(sizeof(*node
), GFP_NOFS
);
250 INIT_LIST_HEAD(&node
->list
);
251 INIT_LIST_HEAD(&node
->upper
);
252 INIT_LIST_HEAD(&node
->lower
);
253 RB_CLEAR_NODE(&node
->rb_node
);
259 static void free_backref_node(struct backref_cache
*cache
,
260 struct backref_node
*node
)
268 static struct backref_edge
*alloc_backref_edge(struct backref_cache
*cache
)
270 struct backref_edge
*edge
;
272 edge
= kzalloc(sizeof(*edge
), GFP_NOFS
);
278 static void free_backref_edge(struct backref_cache
*cache
,
279 struct backref_edge
*edge
)
287 static struct rb_node
*tree_insert(struct rb_root
*root
, u64 bytenr
,
288 struct rb_node
*node
)
290 struct rb_node
**p
= &root
->rb_node
;
291 struct rb_node
*parent
= NULL
;
292 struct tree_entry
*entry
;
296 entry
= rb_entry(parent
, struct tree_entry
, rb_node
);
298 if (bytenr
< entry
->bytenr
)
300 else if (bytenr
> entry
->bytenr
)
306 rb_link_node(node
, parent
, p
);
307 rb_insert_color(node
, root
);
311 static struct rb_node
*tree_search(struct rb_root
*root
, u64 bytenr
)
313 struct rb_node
*n
= root
->rb_node
;
314 struct tree_entry
*entry
;
317 entry
= rb_entry(n
, struct tree_entry
, rb_node
);
319 if (bytenr
< entry
->bytenr
)
321 else if (bytenr
> entry
->bytenr
)
330 * walk up backref nodes until reach node presents tree root
332 static struct backref_node
*walk_up_backref(struct backref_node
*node
,
333 struct backref_edge
*edges
[],
336 struct backref_edge
*edge
;
339 while (!list_empty(&node
->upper
)) {
340 edge
= list_entry(node
->upper
.next
,
341 struct backref_edge
, list
[LOWER
]);
343 node
= edge
->node
[UPPER
];
345 BUG_ON(node
->detached
);
351 * walk down backref nodes to find start of next reference path
353 static struct backref_node
*walk_down_backref(struct backref_edge
*edges
[],
356 struct backref_edge
*edge
;
357 struct backref_node
*lower
;
361 edge
= edges
[idx
- 1];
362 lower
= edge
->node
[LOWER
];
363 if (list_is_last(&edge
->list
[LOWER
], &lower
->upper
)) {
367 edge
= list_entry(edge
->list
[LOWER
].next
,
368 struct backref_edge
, list
[LOWER
]);
369 edges
[idx
- 1] = edge
;
371 return edge
->node
[UPPER
];
377 static void unlock_node_buffer(struct backref_node
*node
)
380 btrfs_tree_unlock(node
->eb
);
385 static void drop_node_buffer(struct backref_node
*node
)
388 unlock_node_buffer(node
);
389 free_extent_buffer(node
->eb
);
394 static void drop_backref_node(struct backref_cache
*tree
,
395 struct backref_node
*node
)
397 BUG_ON(!list_empty(&node
->upper
));
399 drop_node_buffer(node
);
400 list_del(&node
->list
);
401 list_del(&node
->lower
);
402 if (!RB_EMPTY_NODE(&node
->rb_node
))
403 rb_erase(&node
->rb_node
, &tree
->rb_root
);
404 free_backref_node(tree
, node
);
408 * remove a backref node from the backref cache
410 static void remove_backref_node(struct backref_cache
*cache
,
411 struct backref_node
*node
)
413 struct backref_node
*upper
;
414 struct backref_edge
*edge
;
419 BUG_ON(!node
->lowest
&& !node
->detached
);
420 while (!list_empty(&node
->upper
)) {
421 edge
= list_entry(node
->upper
.next
, struct backref_edge
,
423 upper
= edge
->node
[UPPER
];
424 list_del(&edge
->list
[LOWER
]);
425 list_del(&edge
->list
[UPPER
]);
426 free_backref_edge(cache
, edge
);
428 if (RB_EMPTY_NODE(&upper
->rb_node
)) {
429 BUG_ON(!list_empty(&node
->upper
));
430 drop_backref_node(cache
, node
);
436 * add the node to leaf node list if no other
437 * child block cached.
439 if (list_empty(&upper
->lower
)) {
440 list_add_tail(&upper
->lower
, &cache
->leaves
);
445 drop_backref_node(cache
, node
);
448 static void update_backref_node(struct backref_cache
*cache
,
449 struct backref_node
*node
, u64 bytenr
)
451 struct rb_node
*rb_node
;
452 rb_erase(&node
->rb_node
, &cache
->rb_root
);
453 node
->bytenr
= bytenr
;
454 rb_node
= tree_insert(&cache
->rb_root
, node
->bytenr
, &node
->rb_node
);
459 * update backref cache after a transaction commit
461 static int update_backref_cache(struct btrfs_trans_handle
*trans
,
462 struct backref_cache
*cache
)
464 struct backref_node
*node
;
467 if (cache
->last_trans
== 0) {
468 cache
->last_trans
= trans
->transid
;
472 if (cache
->last_trans
== trans
->transid
)
476 * detached nodes are used to avoid unnecessary backref
477 * lookup. transaction commit changes the extent tree.
478 * so the detached nodes are no longer useful.
480 while (!list_empty(&cache
->detached
)) {
481 node
= list_entry(cache
->detached
.next
,
482 struct backref_node
, list
);
483 remove_backref_node(cache
, node
);
486 while (!list_empty(&cache
->changed
)) {
487 node
= list_entry(cache
->changed
.next
,
488 struct backref_node
, list
);
489 list_del_init(&node
->list
);
490 BUG_ON(node
->pending
);
491 update_backref_node(cache
, node
, node
->new_bytenr
);
495 * some nodes can be left in the pending list if there were
496 * errors during processing the pending nodes.
498 for (level
= 0; level
< BTRFS_MAX_LEVEL
; level
++) {
499 list_for_each_entry(node
, &cache
->pending
[level
], list
) {
500 BUG_ON(!node
->pending
);
501 if (node
->bytenr
== node
->new_bytenr
)
503 update_backref_node(cache
, node
, node
->new_bytenr
);
507 cache
->last_trans
= 0;
511 static int should_ignore_root(struct btrfs_root
*root
)
513 struct btrfs_root
*reloc_root
;
518 reloc_root
= root
->reloc_root
;
522 if (btrfs_root_last_snapshot(&reloc_root
->root_item
) ==
523 root
->fs_info
->running_transaction
->transid
- 1)
526 * if there is reloc tree and it was created in previous
527 * transaction backref lookup can find the reloc tree,
528 * so backref node for the fs tree root is useless for
535 * find reloc tree by address of tree root
537 static struct btrfs_root
*find_reloc_root(struct reloc_control
*rc
,
540 struct rb_node
*rb_node
;
541 struct mapping_node
*node
;
542 struct btrfs_root
*root
= NULL
;
544 spin_lock(&rc
->reloc_root_tree
.lock
);
545 rb_node
= tree_search(&rc
->reloc_root_tree
.rb_root
, bytenr
);
547 node
= rb_entry(rb_node
, struct mapping_node
, rb_node
);
548 root
= (struct btrfs_root
*)node
->data
;
550 spin_unlock(&rc
->reloc_root_tree
.lock
);
554 static int is_cowonly_root(u64 root_objectid
)
556 if (root_objectid
== BTRFS_ROOT_TREE_OBJECTID
||
557 root_objectid
== BTRFS_EXTENT_TREE_OBJECTID
||
558 root_objectid
== BTRFS_CHUNK_TREE_OBJECTID
||
559 root_objectid
== BTRFS_DEV_TREE_OBJECTID
||
560 root_objectid
== BTRFS_TREE_LOG_OBJECTID
||
561 root_objectid
== BTRFS_CSUM_TREE_OBJECTID
)
566 static struct btrfs_root
*read_fs_root(struct btrfs_fs_info
*fs_info
,
569 struct btrfs_key key
;
571 key
.objectid
= root_objectid
;
572 key
.type
= BTRFS_ROOT_ITEM_KEY
;
573 if (is_cowonly_root(root_objectid
))
576 key
.offset
= (u64
)-1;
578 return btrfs_read_fs_root_no_name(fs_info
, &key
);
581 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
582 static noinline_for_stack
583 struct btrfs_root
*find_tree_root(struct reloc_control
*rc
,
584 struct extent_buffer
*leaf
,
585 struct btrfs_extent_ref_v0
*ref0
)
587 struct btrfs_root
*root
;
588 u64 root_objectid
= btrfs_ref_root_v0(leaf
, ref0
);
589 u64 generation
= btrfs_ref_generation_v0(leaf
, ref0
);
591 BUG_ON(root_objectid
== BTRFS_TREE_RELOC_OBJECTID
);
593 root
= read_fs_root(rc
->extent_root
->fs_info
, root_objectid
);
594 BUG_ON(IS_ERR(root
));
596 if (root
->ref_cows
&&
597 generation
!= btrfs_root_generation(&root
->root_item
))
604 static noinline_for_stack
605 int find_inline_backref(struct extent_buffer
*leaf
, int slot
,
606 unsigned long *ptr
, unsigned long *end
)
608 struct btrfs_extent_item
*ei
;
609 struct btrfs_tree_block_info
*bi
;
612 item_size
= btrfs_item_size_nr(leaf
, slot
);
613 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
614 if (item_size
< sizeof(*ei
)) {
615 WARN_ON(item_size
!= sizeof(struct btrfs_extent_item_v0
));
619 ei
= btrfs_item_ptr(leaf
, slot
, struct btrfs_extent_item
);
620 WARN_ON(!(btrfs_extent_flags(leaf
, ei
) &
621 BTRFS_EXTENT_FLAG_TREE_BLOCK
));
623 if (item_size
<= sizeof(*ei
) + sizeof(*bi
)) {
624 WARN_ON(item_size
< sizeof(*ei
) + sizeof(*bi
));
628 bi
= (struct btrfs_tree_block_info
*)(ei
+ 1);
629 *ptr
= (unsigned long)(bi
+ 1);
630 *end
= (unsigned long)ei
+ item_size
;
635 * build backref tree for a given tree block. root of the backref tree
636 * corresponds the tree block, leaves of the backref tree correspond
637 * roots of b-trees that reference the tree block.
639 * the basic idea of this function is check backrefs of a given block
640 * to find upper level blocks that refernece the block, and then check
641 * bakcrefs of these upper level blocks recursively. the recursion stop
642 * when tree root is reached or backrefs for the block is cached.
644 * NOTE: if we find backrefs for a block are cached, we know backrefs
645 * for all upper level blocks that directly/indirectly reference the
646 * block are also cached.
648 static noinline_for_stack
649 struct backref_node
*build_backref_tree(struct reloc_control
*rc
,
650 struct btrfs_key
*node_key
,
651 int level
, u64 bytenr
)
653 struct backref_cache
*cache
= &rc
->backref_cache
;
654 struct btrfs_path
*path1
;
655 struct btrfs_path
*path2
;
656 struct extent_buffer
*eb
;
657 struct btrfs_root
*root
;
658 struct backref_node
*cur
;
659 struct backref_node
*upper
;
660 struct backref_node
*lower
;
661 struct backref_node
*node
= NULL
;
662 struct backref_node
*exist
= NULL
;
663 struct backref_edge
*edge
;
664 struct rb_node
*rb_node
;
665 struct btrfs_key key
;
674 path1
= btrfs_alloc_path();
675 path2
= btrfs_alloc_path();
676 if (!path1
|| !path2
) {
681 node
= alloc_backref_node(cache
);
687 node
->bytenr
= bytenr
;
694 key
.objectid
= cur
->bytenr
;
695 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
696 key
.offset
= (u64
)-1;
698 path1
->search_commit_root
= 1;
699 path1
->skip_locking
= 1;
700 ret
= btrfs_search_slot(NULL
, rc
->extent_root
, &key
, path1
,
706 BUG_ON(!ret
|| !path1
->slots
[0]);
710 WARN_ON(cur
->checked
);
711 if (!list_empty(&cur
->upper
)) {
713 * the backref was added previously when processsing
714 * backref of type BTRFS_TREE_BLOCK_REF_KEY
716 BUG_ON(!list_is_singular(&cur
->upper
));
717 edge
= list_entry(cur
->upper
.next
, struct backref_edge
,
719 BUG_ON(!list_empty(&edge
->list
[UPPER
]));
720 exist
= edge
->node
[UPPER
];
722 * add the upper level block to pending list if we need
726 list_add_tail(&edge
->list
[UPPER
], &list
);
733 eb
= path1
->nodes
[0];
736 if (path1
->slots
[0] >= btrfs_header_nritems(eb
)) {
737 ret
= btrfs_next_leaf(rc
->extent_root
, path1
);
744 eb
= path1
->nodes
[0];
747 btrfs_item_key_to_cpu(eb
, &key
, path1
->slots
[0]);
748 if (key
.objectid
!= cur
->bytenr
) {
753 if (key
.type
== BTRFS_EXTENT_ITEM_KEY
) {
754 ret
= find_inline_backref(eb
, path1
->slots
[0],
762 /* update key for inline back ref */
763 struct btrfs_extent_inline_ref
*iref
;
764 iref
= (struct btrfs_extent_inline_ref
*)ptr
;
765 key
.type
= btrfs_extent_inline_ref_type(eb
, iref
);
766 key
.offset
= btrfs_extent_inline_ref_offset(eb
, iref
);
767 WARN_ON(key
.type
!= BTRFS_TREE_BLOCK_REF_KEY
&&
768 key
.type
!= BTRFS_SHARED_BLOCK_REF_KEY
);
772 ((key
.type
== BTRFS_TREE_BLOCK_REF_KEY
&&
773 exist
->owner
== key
.offset
) ||
774 (key
.type
== BTRFS_SHARED_BLOCK_REF_KEY
&&
775 exist
->bytenr
== key
.offset
))) {
780 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
781 if (key
.type
== BTRFS_SHARED_BLOCK_REF_KEY
||
782 key
.type
== BTRFS_EXTENT_REF_V0_KEY
) {
783 if (key
.type
== BTRFS_EXTENT_REF_V0_KEY
) {
784 struct btrfs_extent_ref_v0
*ref0
;
785 ref0
= btrfs_item_ptr(eb
, path1
->slots
[0],
786 struct btrfs_extent_ref_v0
);
787 if (key
.objectid
== key
.offset
) {
788 root
= find_tree_root(rc
, eb
, ref0
);
789 if (root
&& !should_ignore_root(root
))
792 list_add(&cur
->list
, &useless
);
795 if (is_cowonly_root(btrfs_ref_root_v0(eb
,
800 BUG_ON(key
.type
== BTRFS_EXTENT_REF_V0_KEY
);
801 if (key
.type
== BTRFS_SHARED_BLOCK_REF_KEY
) {
803 if (key
.objectid
== key
.offset
) {
805 * only root blocks of reloc trees use
806 * backref of this type.
808 root
= find_reloc_root(rc
, cur
->bytenr
);
814 edge
= alloc_backref_edge(cache
);
819 rb_node
= tree_search(&cache
->rb_root
, key
.offset
);
821 upper
= alloc_backref_node(cache
);
823 free_backref_edge(cache
, edge
);
827 upper
->bytenr
= key
.offset
;
828 upper
->level
= cur
->level
+ 1;
830 * backrefs for the upper level block isn't
831 * cached, add the block to pending list
833 list_add_tail(&edge
->list
[UPPER
], &list
);
835 upper
= rb_entry(rb_node
, struct backref_node
,
837 BUG_ON(!upper
->checked
);
838 INIT_LIST_HEAD(&edge
->list
[UPPER
]);
840 list_add_tail(&edge
->list
[LOWER
], &cur
->upper
);
841 edge
->node
[LOWER
] = cur
;
842 edge
->node
[UPPER
] = upper
;
845 } else if (key
.type
!= BTRFS_TREE_BLOCK_REF_KEY
) {
849 /* key.type == BTRFS_TREE_BLOCK_REF_KEY */
850 root
= read_fs_root(rc
->extent_root
->fs_info
, key
.offset
);
859 if (btrfs_root_level(&root
->root_item
) == cur
->level
) {
861 BUG_ON(btrfs_root_bytenr(&root
->root_item
) !=
863 if (should_ignore_root(root
))
864 list_add(&cur
->list
, &useless
);
870 level
= cur
->level
+ 1;
873 * searching the tree to find upper level blocks
874 * reference the block.
876 path2
->search_commit_root
= 1;
877 path2
->skip_locking
= 1;
878 path2
->lowest_level
= level
;
879 ret
= btrfs_search_slot(NULL
, root
, node_key
, path2
, 0, 0);
880 path2
->lowest_level
= 0;
885 if (ret
> 0 && path2
->slots
[level
] > 0)
886 path2
->slots
[level
]--;
888 eb
= path2
->nodes
[level
];
889 WARN_ON(btrfs_node_blockptr(eb
, path2
->slots
[level
]) !=
893 for (; level
< BTRFS_MAX_LEVEL
; level
++) {
894 if (!path2
->nodes
[level
]) {
895 BUG_ON(btrfs_root_bytenr(&root
->root_item
) !=
897 if (should_ignore_root(root
))
898 list_add(&lower
->list
, &useless
);
904 edge
= alloc_backref_edge(cache
);
910 eb
= path2
->nodes
[level
];
911 rb_node
= tree_search(&cache
->rb_root
, eb
->start
);
913 upper
= alloc_backref_node(cache
);
915 free_backref_edge(cache
, edge
);
919 upper
->bytenr
= eb
->start
;
920 upper
->owner
= btrfs_header_owner(eb
);
921 upper
->level
= lower
->level
+ 1;
926 * if we know the block isn't shared
927 * we can void checking its backrefs.
929 if (btrfs_block_can_be_shared(root
, eb
))
935 * add the block to pending list if we
936 * need check its backrefs. only block
937 * at 'cur->level + 1' is added to the
938 * tail of pending list. this guarantees
939 * we check backrefs from lower level
940 * blocks to upper level blocks.
942 if (!upper
->checked
&&
943 level
== cur
->level
+ 1) {
944 list_add_tail(&edge
->list
[UPPER
],
947 INIT_LIST_HEAD(&edge
->list
[UPPER
]);
949 upper
= rb_entry(rb_node
, struct backref_node
,
951 BUG_ON(!upper
->checked
);
952 INIT_LIST_HEAD(&edge
->list
[UPPER
]);
954 upper
->owner
= btrfs_header_owner(eb
);
956 list_add_tail(&edge
->list
[LOWER
], &lower
->upper
);
957 edge
->node
[LOWER
] = lower
;
958 edge
->node
[UPPER
] = upper
;
965 btrfs_release_path(root
, path2
);
968 ptr
+= btrfs_extent_inline_ref_size(key
.type
);
978 btrfs_release_path(rc
->extent_root
, path1
);
983 /* the pending list isn't empty, take the first block to process */
984 if (!list_empty(&list
)) {
985 edge
= list_entry(list
.next
, struct backref_edge
, list
[UPPER
]);
986 list_del_init(&edge
->list
[UPPER
]);
987 cur
= edge
->node
[UPPER
];
992 * everything goes well, connect backref nodes and insert backref nodes
995 BUG_ON(!node
->checked
);
996 cowonly
= node
->cowonly
;
998 rb_node
= tree_insert(&cache
->rb_root
, node
->bytenr
,
1001 list_add_tail(&node
->lower
, &cache
->leaves
);
1004 list_for_each_entry(edge
, &node
->upper
, list
[LOWER
])
1005 list_add_tail(&edge
->list
[UPPER
], &list
);
1007 while (!list_empty(&list
)) {
1008 edge
= list_entry(list
.next
, struct backref_edge
, list
[UPPER
]);
1009 list_del_init(&edge
->list
[UPPER
]);
1010 upper
= edge
->node
[UPPER
];
1011 if (upper
->detached
) {
1012 list_del(&edge
->list
[LOWER
]);
1013 lower
= edge
->node
[LOWER
];
1014 free_backref_edge(cache
, edge
);
1015 if (list_empty(&lower
->upper
))
1016 list_add(&lower
->list
, &useless
);
1020 if (!RB_EMPTY_NODE(&upper
->rb_node
)) {
1021 if (upper
->lowest
) {
1022 list_del_init(&upper
->lower
);
1026 list_add_tail(&edge
->list
[UPPER
], &upper
->lower
);
1030 BUG_ON(!upper
->checked
);
1031 BUG_ON(cowonly
!= upper
->cowonly
);
1033 rb_node
= tree_insert(&cache
->rb_root
, upper
->bytenr
,
1038 list_add_tail(&edge
->list
[UPPER
], &upper
->lower
);
1040 list_for_each_entry(edge
, &upper
->upper
, list
[LOWER
])
1041 list_add_tail(&edge
->list
[UPPER
], &list
);
1044 * process useless backref nodes. backref nodes for tree leaves
1045 * are deleted from the cache. backref nodes for upper level
1046 * tree blocks are left in the cache to avoid unnecessary backref
1049 while (!list_empty(&useless
)) {
1050 upper
= list_entry(useless
.next
, struct backref_node
, list
);
1051 list_del_init(&upper
->list
);
1052 BUG_ON(!list_empty(&upper
->upper
));
1055 if (upper
->lowest
) {
1056 list_del_init(&upper
->lower
);
1059 while (!list_empty(&upper
->lower
)) {
1060 edge
= list_entry(upper
->lower
.next
,
1061 struct backref_edge
, list
[UPPER
]);
1062 list_del(&edge
->list
[UPPER
]);
1063 list_del(&edge
->list
[LOWER
]);
1064 lower
= edge
->node
[LOWER
];
1065 free_backref_edge(cache
, edge
);
1067 if (list_empty(&lower
->upper
))
1068 list_add(&lower
->list
, &useless
);
1070 __mark_block_processed(rc
, upper
);
1071 if (upper
->level
> 0) {
1072 list_add(&upper
->list
, &cache
->detached
);
1073 upper
->detached
= 1;
1075 rb_erase(&upper
->rb_node
, &cache
->rb_root
);
1076 free_backref_node(cache
, upper
);
1080 btrfs_free_path(path1
);
1081 btrfs_free_path(path2
);
1083 while (!list_empty(&useless
)) {
1084 lower
= list_entry(useless
.next
,
1085 struct backref_node
, upper
);
1086 list_del_init(&lower
->upper
);
1089 INIT_LIST_HEAD(&list
);
1091 if (RB_EMPTY_NODE(&upper
->rb_node
)) {
1092 list_splice_tail(&upper
->upper
, &list
);
1093 free_backref_node(cache
, upper
);
1096 if (list_empty(&list
))
1099 edge
= list_entry(list
.next
, struct backref_edge
,
1101 list_del(&edge
->list
[LOWER
]);
1102 upper
= edge
->node
[UPPER
];
1103 free_backref_edge(cache
, edge
);
1105 return ERR_PTR(err
);
1107 BUG_ON(node
&& node
->detached
);
1112 * helper to add backref node for the newly created snapshot.
1113 * the backref node is created by cloning backref node that
1114 * corresponds to root of source tree
1116 static int clone_backref_node(struct btrfs_trans_handle
*trans
,
1117 struct reloc_control
*rc
,
1118 struct btrfs_root
*src
,
1119 struct btrfs_root
*dest
)
1121 struct btrfs_root
*reloc_root
= src
->reloc_root
;
1122 struct backref_cache
*cache
= &rc
->backref_cache
;
1123 struct backref_node
*node
= NULL
;
1124 struct backref_node
*new_node
;
1125 struct backref_edge
*edge
;
1126 struct backref_edge
*new_edge
;
1127 struct rb_node
*rb_node
;
1129 if (cache
->last_trans
> 0)
1130 update_backref_cache(trans
, cache
);
1132 rb_node
= tree_search(&cache
->rb_root
, src
->commit_root
->start
);
1134 node
= rb_entry(rb_node
, struct backref_node
, rb_node
);
1138 BUG_ON(node
->new_bytenr
!= reloc_root
->node
->start
);
1142 rb_node
= tree_search(&cache
->rb_root
,
1143 reloc_root
->commit_root
->start
);
1145 node
= rb_entry(rb_node
, struct backref_node
,
1147 BUG_ON(node
->detached
);
1154 new_node
= alloc_backref_node(cache
);
1158 new_node
->bytenr
= dest
->node
->start
;
1159 new_node
->level
= node
->level
;
1160 new_node
->lowest
= node
->lowest
;
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
,
1727 btrfs_tree_lock(eb
);
1729 ret
= btrfs_cow_block(trans
, dest
, eb
, parent
,
1733 btrfs_set_lock_blocking(eb
);
1735 btrfs_tree_unlock(parent
);
1736 free_extent_buffer(parent
);
1743 btrfs_tree_unlock(parent
);
1744 free_extent_buffer(parent
);
1749 btrfs_node_key_to_cpu(path
->nodes
[level
], &key
,
1750 path
->slots
[level
]);
1751 btrfs_release_path(src
, path
);
1753 path
->lowest_level
= level
;
1754 ret
= btrfs_search_slot(trans
, src
, &key
, path
, 0, 1);
1755 path
->lowest_level
= 0;
1759 * swap blocks in fs tree and reloc tree.
1761 btrfs_set_node_blockptr(parent
, slot
, new_bytenr
);
1762 btrfs_set_node_ptr_generation(parent
, slot
, new_ptr_gen
);
1763 btrfs_mark_buffer_dirty(parent
);
1765 btrfs_set_node_blockptr(path
->nodes
[level
],
1766 path
->slots
[level
], old_bytenr
);
1767 btrfs_set_node_ptr_generation(path
->nodes
[level
],
1768 path
->slots
[level
], old_ptr_gen
);
1769 btrfs_mark_buffer_dirty(path
->nodes
[level
]);
1771 ret
= btrfs_inc_extent_ref(trans
, src
, old_bytenr
, blocksize
,
1772 path
->nodes
[level
]->start
,
1773 src
->root_key
.objectid
, level
- 1, 0);
1775 ret
= btrfs_inc_extent_ref(trans
, dest
, new_bytenr
, blocksize
,
1776 0, dest
->root_key
.objectid
, level
- 1,
1780 ret
= btrfs_free_extent(trans
, src
, new_bytenr
, blocksize
,
1781 path
->nodes
[level
]->start
,
1782 src
->root_key
.objectid
, level
- 1, 0);
1785 ret
= btrfs_free_extent(trans
, dest
, old_bytenr
, blocksize
,
1786 0, dest
->root_key
.objectid
, level
- 1,
1790 btrfs_unlock_up_safe(path
, 0);
1795 btrfs_tree_unlock(parent
);
1796 free_extent_buffer(parent
);
1801 * helper to find next relocated block in reloc tree
1803 static noinline_for_stack
1804 int walk_up_reloc_tree(struct btrfs_root
*root
, struct btrfs_path
*path
,
1807 struct extent_buffer
*eb
;
1812 last_snapshot
= btrfs_root_last_snapshot(&root
->root_item
);
1814 for (i
= 0; i
< *level
; i
++) {
1815 free_extent_buffer(path
->nodes
[i
]);
1816 path
->nodes
[i
] = NULL
;
1819 for (i
= *level
; i
< BTRFS_MAX_LEVEL
&& path
->nodes
[i
]; i
++) {
1820 eb
= path
->nodes
[i
];
1821 nritems
= btrfs_header_nritems(eb
);
1822 while (path
->slots
[i
] + 1 < nritems
) {
1824 if (btrfs_node_ptr_generation(eb
, path
->slots
[i
]) <=
1831 free_extent_buffer(path
->nodes
[i
]);
1832 path
->nodes
[i
] = NULL
;
1838 * walk down reloc tree to find relocated block of lowest level
1840 static noinline_for_stack
1841 int walk_down_reloc_tree(struct btrfs_root
*root
, struct btrfs_path
*path
,
1844 struct extent_buffer
*eb
= NULL
;
1852 last_snapshot
= btrfs_root_last_snapshot(&root
->root_item
);
1854 for (i
= *level
; i
> 0; i
--) {
1855 eb
= path
->nodes
[i
];
1856 nritems
= btrfs_header_nritems(eb
);
1857 while (path
->slots
[i
] < nritems
) {
1858 ptr_gen
= btrfs_node_ptr_generation(eb
, path
->slots
[i
]);
1859 if (ptr_gen
> last_snapshot
)
1863 if (path
->slots
[i
] >= nritems
) {
1874 bytenr
= btrfs_node_blockptr(eb
, path
->slots
[i
]);
1875 blocksize
= btrfs_level_size(root
, i
- 1);
1876 eb
= read_tree_block(root
, bytenr
, blocksize
, ptr_gen
);
1877 BUG_ON(btrfs_header_level(eb
) != i
- 1);
1878 path
->nodes
[i
- 1] = eb
;
1879 path
->slots
[i
- 1] = 0;
1885 * invalidate extent cache for file extents whose key in range of
1886 * [min_key, max_key)
1888 static int invalidate_extent_cache(struct btrfs_root
*root
,
1889 struct btrfs_key
*min_key
,
1890 struct btrfs_key
*max_key
)
1892 struct inode
*inode
= NULL
;
1896 objectid
= min_key
->objectid
;
1901 if (objectid
> max_key
->objectid
)
1904 inode
= find_next_inode(root
, objectid
);
1908 if (inode
->i_ino
> max_key
->objectid
) {
1913 objectid
= inode
->i_ino
+ 1;
1914 if (!S_ISREG(inode
->i_mode
))
1917 if (unlikely(min_key
->objectid
== inode
->i_ino
)) {
1918 if (min_key
->type
> BTRFS_EXTENT_DATA_KEY
)
1920 if (min_key
->type
< BTRFS_EXTENT_DATA_KEY
)
1923 start
= min_key
->offset
;
1924 WARN_ON(!IS_ALIGNED(start
, root
->sectorsize
));
1930 if (unlikely(max_key
->objectid
== inode
->i_ino
)) {
1931 if (max_key
->type
< BTRFS_EXTENT_DATA_KEY
)
1933 if (max_key
->type
> BTRFS_EXTENT_DATA_KEY
) {
1936 if (max_key
->offset
== 0)
1938 end
= max_key
->offset
;
1939 WARN_ON(!IS_ALIGNED(end
, root
->sectorsize
));
1946 /* the lock_extent waits for readpage to complete */
1947 lock_extent(&BTRFS_I(inode
)->io_tree
, start
, end
, GFP_NOFS
);
1948 btrfs_drop_extent_cache(inode
, start
, end
, 1);
1949 unlock_extent(&BTRFS_I(inode
)->io_tree
, start
, end
, GFP_NOFS
);
1954 static int find_next_key(struct btrfs_path
*path
, int level
,
1955 struct btrfs_key
*key
)
1958 while (level
< BTRFS_MAX_LEVEL
) {
1959 if (!path
->nodes
[level
])
1961 if (path
->slots
[level
] + 1 <
1962 btrfs_header_nritems(path
->nodes
[level
])) {
1963 btrfs_node_key_to_cpu(path
->nodes
[level
], key
,
1964 path
->slots
[level
] + 1);
1973 * merge the relocated tree blocks in reloc tree with corresponding
1976 static noinline_for_stack
int merge_reloc_root(struct reloc_control
*rc
,
1977 struct btrfs_root
*root
)
1979 LIST_HEAD(inode_list
);
1980 struct btrfs_key key
;
1981 struct btrfs_key next_key
;
1982 struct btrfs_trans_handle
*trans
;
1983 struct btrfs_root
*reloc_root
;
1984 struct btrfs_root_item
*root_item
;
1985 struct btrfs_path
*path
;
1986 struct extent_buffer
*leaf
;
1995 path
= btrfs_alloc_path();
1999 reloc_root
= root
->reloc_root
;
2000 root_item
= &reloc_root
->root_item
;
2002 if (btrfs_disk_key_objectid(&root_item
->drop_progress
) == 0) {
2003 level
= btrfs_root_level(root_item
);
2004 extent_buffer_get(reloc_root
->node
);
2005 path
->nodes
[level
] = reloc_root
->node
;
2006 path
->slots
[level
] = 0;
2008 btrfs_disk_key_to_cpu(&key
, &root_item
->drop_progress
);
2010 level
= root_item
->drop_level
;
2012 path
->lowest_level
= level
;
2013 ret
= btrfs_search_slot(NULL
, reloc_root
, &key
, path
, 0, 0);
2014 path
->lowest_level
= 0;
2016 btrfs_free_path(path
);
2020 btrfs_node_key_to_cpu(path
->nodes
[level
], &next_key
,
2021 path
->slots
[level
]);
2022 WARN_ON(memcmp(&key
, &next_key
, sizeof(key
)));
2024 btrfs_unlock_up_safe(path
, 0);
2027 min_reserved
= root
->nodesize
* (BTRFS_MAX_LEVEL
- 1) * 2;
2028 memset(&next_key
, 0, sizeof(next_key
));
2031 trans
= btrfs_start_transaction(root
, 0);
2032 trans
->block_rsv
= rc
->block_rsv
;
2034 ret
= btrfs_block_rsv_check(trans
, root
, rc
->block_rsv
,
2037 BUG_ON(ret
!= -EAGAIN
);
2038 ret
= btrfs_commit_transaction(trans
, root
);
2046 ret
= walk_down_reloc_tree(reloc_root
, path
, &level
);
2054 if (!find_next_key(path
, level
, &key
) &&
2055 btrfs_comp_cpu_keys(&next_key
, &key
) >= 0) {
2058 ret
= replace_path(trans
, root
, reloc_root
, path
,
2059 &next_key
, level
, max_level
);
2068 btrfs_node_key_to_cpu(path
->nodes
[level
], &key
,
2069 path
->slots
[level
]);
2073 ret
= walk_up_reloc_tree(reloc_root
, path
, &level
);
2079 * save the merging progress in the drop_progress.
2080 * this is OK since root refs == 1 in this case.
2082 btrfs_node_key(path
->nodes
[level
], &root_item
->drop_progress
,
2083 path
->slots
[level
]);
2084 root_item
->drop_level
= level
;
2086 nr
= trans
->blocks_used
;
2087 btrfs_end_transaction_throttle(trans
, root
);
2089 btrfs_btree_balance_dirty(root
, nr
);
2091 if (replaced
&& rc
->stage
== UPDATE_DATA_PTRS
)
2092 invalidate_extent_cache(root
, &key
, &next_key
);
2096 * handle the case only one block in the fs tree need to be
2097 * relocated and the block is tree root.
2099 leaf
= btrfs_lock_root_node(root
);
2100 ret
= btrfs_cow_block(trans
, root
, leaf
, NULL
, 0, &leaf
);
2101 btrfs_tree_unlock(leaf
);
2102 free_extent_buffer(leaf
);
2106 btrfs_free_path(path
);
2109 memset(&root_item
->drop_progress
, 0,
2110 sizeof(root_item
->drop_progress
));
2111 root_item
->drop_level
= 0;
2112 btrfs_set_root_refs(root_item
, 0);
2113 btrfs_update_reloc_root(trans
, root
);
2116 nr
= trans
->blocks_used
;
2117 btrfs_end_transaction_throttle(trans
, root
);
2119 btrfs_btree_balance_dirty(root
, nr
);
2121 if (replaced
&& rc
->stage
== UPDATE_DATA_PTRS
)
2122 invalidate_extent_cache(root
, &key
, &next_key
);
2127 static noinline_for_stack
2128 int prepare_to_merge(struct reloc_control
*rc
, int err
)
2130 struct btrfs_root
*root
= rc
->extent_root
;
2131 struct btrfs_root
*reloc_root
;
2132 struct btrfs_trans_handle
*trans
;
2133 LIST_HEAD(reloc_roots
);
2138 mutex_lock(&root
->fs_info
->trans_mutex
);
2139 rc
->merging_rsv_size
+= root
->nodesize
* (BTRFS_MAX_LEVEL
- 1) * 2;
2140 rc
->merging_rsv_size
+= rc
->nodes_relocated
* 2;
2141 mutex_unlock(&root
->fs_info
->trans_mutex
);
2144 num_bytes
= rc
->merging_rsv_size
;
2145 ret
= btrfs_block_rsv_add(NULL
, root
, rc
->block_rsv
,
2146 num_bytes
, &retries
);
2151 trans
= btrfs_join_transaction(rc
->extent_root
, 1);
2154 if (num_bytes
!= rc
->merging_rsv_size
) {
2155 btrfs_end_transaction(trans
, rc
->extent_root
);
2156 btrfs_block_rsv_release(rc
->extent_root
,
2157 rc
->block_rsv
, num_bytes
);
2163 rc
->merge_reloc_tree
= 1;
2165 while (!list_empty(&rc
->reloc_roots
)) {
2166 reloc_root
= list_entry(rc
->reloc_roots
.next
,
2167 struct btrfs_root
, root_list
);
2168 list_del_init(&reloc_root
->root_list
);
2170 root
= read_fs_root(reloc_root
->fs_info
,
2171 reloc_root
->root_key
.offset
);
2172 BUG_ON(IS_ERR(root
));
2173 BUG_ON(root
->reloc_root
!= reloc_root
);
2176 * set reference count to 1, so btrfs_recover_relocation
2177 * knows it should resumes merging
2180 btrfs_set_root_refs(&reloc_root
->root_item
, 1);
2181 btrfs_update_reloc_root(trans
, root
);
2183 list_add(&reloc_root
->root_list
, &reloc_roots
);
2186 list_splice(&reloc_roots
, &rc
->reloc_roots
);
2189 btrfs_commit_transaction(trans
, rc
->extent_root
);
2191 btrfs_end_transaction(trans
, rc
->extent_root
);
2195 static noinline_for_stack
2196 int merge_reloc_roots(struct reloc_control
*rc
)
2198 struct btrfs_root
*root
;
2199 struct btrfs_root
*reloc_root
;
2200 LIST_HEAD(reloc_roots
);
2204 root
= rc
->extent_root
;
2205 mutex_lock(&root
->fs_info
->trans_mutex
);
2206 list_splice_init(&rc
->reloc_roots
, &reloc_roots
);
2207 mutex_unlock(&root
->fs_info
->trans_mutex
);
2209 while (!list_empty(&reloc_roots
)) {
2211 reloc_root
= list_entry(reloc_roots
.next
,
2212 struct btrfs_root
, root_list
);
2214 if (btrfs_root_refs(&reloc_root
->root_item
) > 0) {
2215 root
= read_fs_root(reloc_root
->fs_info
,
2216 reloc_root
->root_key
.offset
);
2217 BUG_ON(IS_ERR(root
));
2218 BUG_ON(root
->reloc_root
!= reloc_root
);
2220 ret
= merge_reloc_root(rc
, root
);
2223 list_del_init(&reloc_root
->root_list
);
2225 btrfs_drop_snapshot(reloc_root
, rc
->block_rsv
, 0);
2232 BUG_ON(!RB_EMPTY_ROOT(&rc
->reloc_root_tree
.rb_root
));
2236 static void free_block_list(struct rb_root
*blocks
)
2238 struct tree_block
*block
;
2239 struct rb_node
*rb_node
;
2240 while ((rb_node
= rb_first(blocks
))) {
2241 block
= rb_entry(rb_node
, struct tree_block
, rb_node
);
2242 rb_erase(rb_node
, blocks
);
2247 static int record_reloc_root_in_trans(struct btrfs_trans_handle
*trans
,
2248 struct btrfs_root
*reloc_root
)
2250 struct btrfs_root
*root
;
2252 if (reloc_root
->last_trans
== trans
->transid
)
2255 root
= read_fs_root(reloc_root
->fs_info
, reloc_root
->root_key
.offset
);
2256 BUG_ON(IS_ERR(root
));
2257 BUG_ON(root
->reloc_root
!= reloc_root
);
2259 return btrfs_record_root_in_trans(trans
, root
);
2262 static noinline_for_stack
2263 struct btrfs_root
*select_reloc_root(struct btrfs_trans_handle
*trans
,
2264 struct reloc_control
*rc
,
2265 struct backref_node
*node
,
2266 struct backref_edge
*edges
[], int *nr
)
2268 struct backref_node
*next
;
2269 struct btrfs_root
*root
;
2275 next
= walk_up_backref(next
, edges
, &index
);
2278 BUG_ON(!root
->ref_cows
);
2280 if (root
->root_key
.objectid
== BTRFS_TREE_RELOC_OBJECTID
) {
2281 record_reloc_root_in_trans(trans
, root
);
2285 btrfs_record_root_in_trans(trans
, root
);
2286 root
= root
->reloc_root
;
2288 if (next
->new_bytenr
!= root
->node
->start
) {
2289 BUG_ON(next
->new_bytenr
);
2290 BUG_ON(!list_empty(&next
->list
));
2291 next
->new_bytenr
= root
->node
->start
;
2293 list_add_tail(&next
->list
,
2294 &rc
->backref_cache
.changed
);
2295 __mark_block_processed(rc
, next
);
2301 next
= walk_down_backref(edges
, &index
);
2302 if (!next
|| next
->level
<= node
->level
)
2310 /* setup backref node path for btrfs_reloc_cow_block */
2312 rc
->backref_cache
.path
[next
->level
] = next
;
2315 next
= edges
[index
]->node
[UPPER
];
2321 * select a tree root for relocation. return NULL if the block
2322 * is reference counted. we should use do_relocation() in this
2323 * case. return a tree root pointer if the block isn't reference
2324 * counted. return -ENOENT if the block is root of reloc tree.
2326 static noinline_for_stack
2327 struct btrfs_root
*select_one_root(struct btrfs_trans_handle
*trans
,
2328 struct backref_node
*node
)
2330 struct backref_node
*next
;
2331 struct btrfs_root
*root
;
2332 struct btrfs_root
*fs_root
= NULL
;
2333 struct backref_edge
*edges
[BTRFS_MAX_LEVEL
- 1];
2339 next
= walk_up_backref(next
, edges
, &index
);
2343 /* no other choice for non-refernce counted tree */
2344 if (!root
->ref_cows
)
2347 if (root
->root_key
.objectid
!= BTRFS_TREE_RELOC_OBJECTID
)
2353 next
= walk_down_backref(edges
, &index
);
2354 if (!next
|| next
->level
<= node
->level
)
2359 return ERR_PTR(-ENOENT
);
2363 static noinline_for_stack
2364 u64
calcu_metadata_size(struct reloc_control
*rc
,
2365 struct backref_node
*node
, int reserve
)
2367 struct backref_node
*next
= node
;
2368 struct backref_edge
*edge
;
2369 struct backref_edge
*edges
[BTRFS_MAX_LEVEL
- 1];
2373 BUG_ON(reserve
&& node
->processed
);
2378 if (next
->processed
&& (reserve
|| next
!= node
))
2381 num_bytes
+= btrfs_level_size(rc
->extent_root
,
2384 if (list_empty(&next
->upper
))
2387 edge
= list_entry(next
->upper
.next
,
2388 struct backref_edge
, list
[LOWER
]);
2389 edges
[index
++] = edge
;
2390 next
= edge
->node
[UPPER
];
2392 next
= walk_down_backref(edges
, &index
);
2397 static int reserve_metadata_space(struct btrfs_trans_handle
*trans
,
2398 struct reloc_control
*rc
,
2399 struct backref_node
*node
)
2401 struct btrfs_root
*root
= rc
->extent_root
;
2405 num_bytes
= calcu_metadata_size(rc
, node
, 1) * 2;
2407 trans
->block_rsv
= rc
->block_rsv
;
2408 ret
= btrfs_block_rsv_add(trans
, root
, rc
->block_rsv
, num_bytes
,
2409 &rc
->block_rsv_retries
);
2412 rc
->commit_transaction
= 1;
2416 rc
->block_rsv_retries
= 0;
2420 static void release_metadata_space(struct reloc_control
*rc
,
2421 struct backref_node
*node
)
2423 u64 num_bytes
= calcu_metadata_size(rc
, node
, 0) * 2;
2424 btrfs_block_rsv_release(rc
->extent_root
, rc
->block_rsv
, num_bytes
);
2428 * relocate a block tree, and then update pointers in upper level
2429 * blocks that reference the block to point to the new location.
2431 * if called by link_to_upper, the block has already been relocated.
2432 * in that case this function just updates pointers.
2434 static int do_relocation(struct btrfs_trans_handle
*trans
,
2435 struct reloc_control
*rc
,
2436 struct backref_node
*node
,
2437 struct btrfs_key
*key
,
2438 struct btrfs_path
*path
, int lowest
)
2440 struct backref_node
*upper
;
2441 struct backref_edge
*edge
;
2442 struct backref_edge
*edges
[BTRFS_MAX_LEVEL
- 1];
2443 struct btrfs_root
*root
;
2444 struct extent_buffer
*eb
;
2453 BUG_ON(lowest
&& node
->eb
);
2455 path
->lowest_level
= node
->level
+ 1;
2456 rc
->backref_cache
.path
[node
->level
] = node
;
2457 list_for_each_entry(edge
, &node
->upper
, list
[LOWER
]) {
2460 upper
= edge
->node
[UPPER
];
2461 root
= select_reloc_root(trans
, rc
, upper
, edges
, &nr
);
2464 if (upper
->eb
&& !upper
->locked
) {
2466 ret
= btrfs_bin_search(upper
->eb
, key
,
2467 upper
->level
, &slot
);
2469 bytenr
= btrfs_node_blockptr(upper
->eb
, slot
);
2470 if (node
->eb
->start
== bytenr
)
2473 drop_node_buffer(upper
);
2477 ret
= btrfs_search_slot(trans
, root
, key
, path
, 0, 1);
2485 upper
->eb
= path
->nodes
[upper
->level
];
2486 path
->nodes
[upper
->level
] = NULL
;
2488 BUG_ON(upper
->eb
!= path
->nodes
[upper
->level
]);
2492 path
->locks
[upper
->level
] = 0;
2494 slot
= path
->slots
[upper
->level
];
2495 btrfs_release_path(NULL
, path
);
2497 ret
= btrfs_bin_search(upper
->eb
, key
, upper
->level
,
2502 bytenr
= btrfs_node_blockptr(upper
->eb
, slot
);
2504 BUG_ON(bytenr
!= node
->bytenr
);
2506 if (node
->eb
->start
== bytenr
)
2510 blocksize
= btrfs_level_size(root
, node
->level
);
2511 generation
= btrfs_node_ptr_generation(upper
->eb
, slot
);
2512 eb
= read_tree_block(root
, bytenr
, blocksize
, generation
);
2513 btrfs_tree_lock(eb
);
2514 btrfs_set_lock_blocking(eb
);
2517 ret
= btrfs_cow_block(trans
, root
, eb
, upper
->eb
,
2519 btrfs_tree_unlock(eb
);
2520 free_extent_buffer(eb
);
2525 BUG_ON(node
->eb
!= eb
);
2527 btrfs_set_node_blockptr(upper
->eb
, slot
,
2529 btrfs_set_node_ptr_generation(upper
->eb
, slot
,
2531 btrfs_mark_buffer_dirty(upper
->eb
);
2533 ret
= btrfs_inc_extent_ref(trans
, root
,
2534 node
->eb
->start
, blocksize
,
2536 btrfs_header_owner(upper
->eb
),
2540 ret
= btrfs_drop_subtree(trans
, root
, eb
, upper
->eb
);
2544 if (!upper
->pending
)
2545 drop_node_buffer(upper
);
2547 unlock_node_buffer(upper
);
2552 if (!err
&& node
->pending
) {
2553 drop_node_buffer(node
);
2554 list_move_tail(&node
->list
, &rc
->backref_cache
.changed
);
2558 path
->lowest_level
= 0;
2559 BUG_ON(err
== -ENOSPC
);
2563 static int link_to_upper(struct btrfs_trans_handle
*trans
,
2564 struct reloc_control
*rc
,
2565 struct backref_node
*node
,
2566 struct btrfs_path
*path
)
2568 struct btrfs_key key
;
2570 btrfs_node_key_to_cpu(node
->eb
, &key
, 0);
2571 return do_relocation(trans
, rc
, node
, &key
, path
, 0);
2574 static int finish_pending_nodes(struct btrfs_trans_handle
*trans
,
2575 struct reloc_control
*rc
,
2576 struct btrfs_path
*path
, int err
)
2579 struct backref_cache
*cache
= &rc
->backref_cache
;
2580 struct backref_node
*node
;
2584 for (level
= 0; level
< BTRFS_MAX_LEVEL
; level
++) {
2585 while (!list_empty(&cache
->pending
[level
])) {
2586 node
= list_entry(cache
->pending
[level
].next
,
2587 struct backref_node
, list
);
2588 list_move_tail(&node
->list
, &list
);
2589 BUG_ON(!node
->pending
);
2592 ret
= link_to_upper(trans
, rc
, node
, path
);
2597 list_splice_init(&list
, &cache
->pending
[level
]);
2602 static void mark_block_processed(struct reloc_control
*rc
,
2603 u64 bytenr
, u32 blocksize
)
2605 set_extent_bits(&rc
->processed_blocks
, bytenr
, bytenr
+ blocksize
- 1,
2606 EXTENT_DIRTY
, GFP_NOFS
);
2609 static void __mark_block_processed(struct reloc_control
*rc
,
2610 struct backref_node
*node
)
2613 if (node
->level
== 0 ||
2614 in_block_group(node
->bytenr
, rc
->block_group
)) {
2615 blocksize
= btrfs_level_size(rc
->extent_root
, node
->level
);
2616 mark_block_processed(rc
, node
->bytenr
, blocksize
);
2618 node
->processed
= 1;
2622 * mark a block and all blocks directly/indirectly reference the block
2625 static void update_processed_blocks(struct reloc_control
*rc
,
2626 struct backref_node
*node
)
2628 struct backref_node
*next
= node
;
2629 struct backref_edge
*edge
;
2630 struct backref_edge
*edges
[BTRFS_MAX_LEVEL
- 1];
2636 if (next
->processed
)
2639 __mark_block_processed(rc
, next
);
2641 if (list_empty(&next
->upper
))
2644 edge
= list_entry(next
->upper
.next
,
2645 struct backref_edge
, list
[LOWER
]);
2646 edges
[index
++] = edge
;
2647 next
= edge
->node
[UPPER
];
2649 next
= walk_down_backref(edges
, &index
);
2653 static int tree_block_processed(u64 bytenr
, u32 blocksize
,
2654 struct reloc_control
*rc
)
2656 if (test_range_bit(&rc
->processed_blocks
, bytenr
,
2657 bytenr
+ blocksize
- 1, EXTENT_DIRTY
, 1, NULL
))
2662 static int get_tree_block_key(struct reloc_control
*rc
,
2663 struct tree_block
*block
)
2665 struct extent_buffer
*eb
;
2667 BUG_ON(block
->key_ready
);
2668 eb
= read_tree_block(rc
->extent_root
, block
->bytenr
,
2669 block
->key
.objectid
, block
->key
.offset
);
2670 WARN_ON(btrfs_header_level(eb
) != block
->level
);
2671 if (block
->level
== 0)
2672 btrfs_item_key_to_cpu(eb
, &block
->key
, 0);
2674 btrfs_node_key_to_cpu(eb
, &block
->key
, 0);
2675 free_extent_buffer(eb
);
2676 block
->key_ready
= 1;
2680 static int reada_tree_block(struct reloc_control
*rc
,
2681 struct tree_block
*block
)
2683 BUG_ON(block
->key_ready
);
2684 readahead_tree_block(rc
->extent_root
, block
->bytenr
,
2685 block
->key
.objectid
, block
->key
.offset
);
2690 * helper function to relocate a tree block
2692 static int relocate_tree_block(struct btrfs_trans_handle
*trans
,
2693 struct reloc_control
*rc
,
2694 struct backref_node
*node
,
2695 struct btrfs_key
*key
,
2696 struct btrfs_path
*path
)
2698 struct btrfs_root
*root
;
2705 BUG_ON(node
->processed
);
2706 root
= select_one_root(trans
, node
);
2707 if (root
== ERR_PTR(-ENOENT
)) {
2708 update_processed_blocks(rc
, node
);
2712 if (!root
|| root
->ref_cows
) {
2713 ret
= reserve_metadata_space(trans
, rc
, node
);
2720 if (root
->ref_cows
) {
2721 BUG_ON(node
->new_bytenr
);
2722 BUG_ON(!list_empty(&node
->list
));
2723 btrfs_record_root_in_trans(trans
, root
);
2724 root
= root
->reloc_root
;
2725 node
->new_bytenr
= root
->node
->start
;
2727 list_add_tail(&node
->list
, &rc
->backref_cache
.changed
);
2729 path
->lowest_level
= node
->level
;
2730 ret
= btrfs_search_slot(trans
, root
, key
, path
, 0, 1);
2731 btrfs_release_path(root
, path
);
2736 update_processed_blocks(rc
, node
);
2738 ret
= do_relocation(trans
, rc
, node
, key
, path
, 1);
2741 if (ret
|| node
->level
== 0 || node
->cowonly
) {
2743 release_metadata_space(rc
, node
);
2744 remove_backref_node(&rc
->backref_cache
, node
);
2750 * relocate a list of blocks
2752 static noinline_for_stack
2753 int relocate_tree_blocks(struct btrfs_trans_handle
*trans
,
2754 struct reloc_control
*rc
, struct rb_root
*blocks
)
2756 struct backref_node
*node
;
2757 struct btrfs_path
*path
;
2758 struct tree_block
*block
;
2759 struct rb_node
*rb_node
;
2763 path
= btrfs_alloc_path();
2767 rb_node
= rb_first(blocks
);
2769 block
= rb_entry(rb_node
, struct tree_block
, rb_node
);
2770 if (!block
->key_ready
)
2771 reada_tree_block(rc
, block
);
2772 rb_node
= rb_next(rb_node
);
2775 rb_node
= rb_first(blocks
);
2777 block
= rb_entry(rb_node
, struct tree_block
, rb_node
);
2778 if (!block
->key_ready
)
2779 get_tree_block_key(rc
, block
);
2780 rb_node
= rb_next(rb_node
);
2783 rb_node
= rb_first(blocks
);
2785 block
= rb_entry(rb_node
, struct tree_block
, rb_node
);
2787 node
= build_backref_tree(rc
, &block
->key
,
2788 block
->level
, block
->bytenr
);
2790 err
= PTR_ERR(node
);
2794 ret
= relocate_tree_block(trans
, rc
, node
, &block
->key
,
2797 if (ret
!= -EAGAIN
|| rb_node
== rb_first(blocks
))
2801 rb_node
= rb_next(rb_node
);
2804 free_block_list(blocks
);
2805 err
= finish_pending_nodes(trans
, rc
, path
, err
);
2807 btrfs_free_path(path
);
2811 static noinline_for_stack
2812 int prealloc_file_extent_cluster(struct inode
*inode
,
2813 struct file_extent_cluster
*cluster
)
2818 u64 offset
= BTRFS_I(inode
)->index_cnt
;
2823 BUG_ON(cluster
->start
!= cluster
->boundary
[0]);
2824 mutex_lock(&inode
->i_mutex
);
2826 ret
= btrfs_check_data_free_space(inode
, cluster
->end
+
2827 1 - cluster
->start
);
2831 while (nr
< cluster
->nr
) {
2832 start
= cluster
->boundary
[nr
] - offset
;
2833 if (nr
+ 1 < cluster
->nr
)
2834 end
= cluster
->boundary
[nr
+ 1] - 1 - offset
;
2836 end
= cluster
->end
- offset
;
2838 lock_extent(&BTRFS_I(inode
)->io_tree
, start
, end
, GFP_NOFS
);
2839 num_bytes
= end
+ 1 - start
;
2840 ret
= btrfs_prealloc_file_range(inode
, 0, start
,
2841 num_bytes
, num_bytes
,
2842 end
+ 1, &alloc_hint
);
2843 unlock_extent(&BTRFS_I(inode
)->io_tree
, start
, end
, GFP_NOFS
);
2848 btrfs_free_reserved_data_space(inode
, cluster
->end
+
2849 1 - cluster
->start
);
2851 mutex_unlock(&inode
->i_mutex
);
2855 static noinline_for_stack
2856 int setup_extent_mapping(struct inode
*inode
, u64 start
, u64 end
,
2859 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2860 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
2861 struct extent_map
*em
;
2864 em
= alloc_extent_map(GFP_NOFS
);
2869 em
->len
= end
+ 1 - start
;
2870 em
->block_len
= em
->len
;
2871 em
->block_start
= block_start
;
2872 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
2873 set_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
2875 lock_extent(&BTRFS_I(inode
)->io_tree
, start
, end
, GFP_NOFS
);
2877 write_lock(&em_tree
->lock
);
2878 ret
= add_extent_mapping(em_tree
, em
);
2879 write_unlock(&em_tree
->lock
);
2880 if (ret
!= -EEXIST
) {
2881 free_extent_map(em
);
2884 btrfs_drop_extent_cache(inode
, start
, end
, 0);
2886 unlock_extent(&BTRFS_I(inode
)->io_tree
, start
, end
, GFP_NOFS
);
2890 static int relocate_file_extent_cluster(struct inode
*inode
,
2891 struct file_extent_cluster
*cluster
)
2895 u64 offset
= BTRFS_I(inode
)->index_cnt
;
2896 unsigned long index
;
2897 unsigned long last_index
;
2899 struct file_ra_state
*ra
;
2906 ra
= kzalloc(sizeof(*ra
), GFP_NOFS
);
2910 ret
= prealloc_file_extent_cluster(inode
, cluster
);
2914 file_ra_state_init(ra
, inode
->i_mapping
);
2916 ret
= setup_extent_mapping(inode
, cluster
->start
- offset
,
2917 cluster
->end
- offset
, cluster
->start
);
2921 index
= (cluster
->start
- offset
) >> PAGE_CACHE_SHIFT
;
2922 last_index
= (cluster
->end
- offset
) >> PAGE_CACHE_SHIFT
;
2923 while (index
<= last_index
) {
2924 ret
= btrfs_delalloc_reserve_metadata(inode
, PAGE_CACHE_SIZE
);
2928 page
= find_lock_page(inode
->i_mapping
, index
);
2930 page_cache_sync_readahead(inode
->i_mapping
,
2932 last_index
+ 1 - index
);
2933 page
= grab_cache_page(inode
->i_mapping
, index
);
2935 btrfs_delalloc_release_metadata(inode
,
2942 if (PageReadahead(page
)) {
2943 page_cache_async_readahead(inode
->i_mapping
,
2944 ra
, NULL
, page
, index
,
2945 last_index
+ 1 - index
);
2948 if (!PageUptodate(page
)) {
2949 btrfs_readpage(NULL
, page
);
2951 if (!PageUptodate(page
)) {
2953 page_cache_release(page
);
2954 btrfs_delalloc_release_metadata(inode
,
2961 page_start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
2962 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
2964 lock_extent(&BTRFS_I(inode
)->io_tree
,
2965 page_start
, page_end
, GFP_NOFS
);
2967 set_page_extent_mapped(page
);
2969 if (nr
< cluster
->nr
&&
2970 page_start
+ offset
== cluster
->boundary
[nr
]) {
2971 set_extent_bits(&BTRFS_I(inode
)->io_tree
,
2972 page_start
, page_end
,
2973 EXTENT_BOUNDARY
, GFP_NOFS
);
2977 btrfs_set_extent_delalloc(inode
, page_start
, page_end
, NULL
);
2978 set_page_dirty(page
);
2980 unlock_extent(&BTRFS_I(inode
)->io_tree
,
2981 page_start
, page_end
, GFP_NOFS
);
2983 page_cache_release(page
);
2986 balance_dirty_pages_ratelimited(inode
->i_mapping
);
2987 btrfs_throttle(BTRFS_I(inode
)->root
);
2989 WARN_ON(nr
!= cluster
->nr
);
2995 static noinline_for_stack
2996 int relocate_data_extent(struct inode
*inode
, struct btrfs_key
*extent_key
,
2997 struct file_extent_cluster
*cluster
)
3001 if (cluster
->nr
> 0 && extent_key
->objectid
!= cluster
->end
+ 1) {
3002 ret
= relocate_file_extent_cluster(inode
, cluster
);
3009 cluster
->start
= extent_key
->objectid
;
3011 BUG_ON(cluster
->nr
>= MAX_EXTENTS
);
3012 cluster
->end
= extent_key
->objectid
+ extent_key
->offset
- 1;
3013 cluster
->boundary
[cluster
->nr
] = extent_key
->objectid
;
3016 if (cluster
->nr
>= MAX_EXTENTS
) {
3017 ret
= relocate_file_extent_cluster(inode
, cluster
);
3025 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3026 static int get_ref_objectid_v0(struct reloc_control
*rc
,
3027 struct btrfs_path
*path
,
3028 struct btrfs_key
*extent_key
,
3029 u64
*ref_objectid
, int *path_change
)
3031 struct btrfs_key key
;
3032 struct extent_buffer
*leaf
;
3033 struct btrfs_extent_ref_v0
*ref0
;
3037 leaf
= path
->nodes
[0];
3038 slot
= path
->slots
[0];
3040 if (slot
>= btrfs_header_nritems(leaf
)) {
3041 ret
= btrfs_next_leaf(rc
->extent_root
, path
);
3045 leaf
= path
->nodes
[0];
3046 slot
= path
->slots
[0];
3050 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
3051 if (key
.objectid
!= extent_key
->objectid
)
3054 if (key
.type
!= BTRFS_EXTENT_REF_V0_KEY
) {
3058 ref0
= btrfs_item_ptr(leaf
, slot
,
3059 struct btrfs_extent_ref_v0
);
3060 *ref_objectid
= btrfs_ref_objectid_v0(leaf
, ref0
);
3068 * helper to add a tree block to the list.
3069 * the major work is getting the generation and level of the block
3071 static int add_tree_block(struct reloc_control
*rc
,
3072 struct btrfs_key
*extent_key
,
3073 struct btrfs_path
*path
,
3074 struct rb_root
*blocks
)
3076 struct extent_buffer
*eb
;
3077 struct btrfs_extent_item
*ei
;
3078 struct btrfs_tree_block_info
*bi
;
3079 struct tree_block
*block
;
3080 struct rb_node
*rb_node
;
3085 eb
= path
->nodes
[0];
3086 item_size
= btrfs_item_size_nr(eb
, path
->slots
[0]);
3088 if (item_size
>= sizeof(*ei
) + sizeof(*bi
)) {
3089 ei
= btrfs_item_ptr(eb
, path
->slots
[0],
3090 struct btrfs_extent_item
);
3091 bi
= (struct btrfs_tree_block_info
*)(ei
+ 1);
3092 generation
= btrfs_extent_generation(eb
, ei
);
3093 level
= btrfs_tree_block_level(eb
, bi
);
3095 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3099 BUG_ON(item_size
!= sizeof(struct btrfs_extent_item_v0
));
3100 ret
= get_ref_objectid_v0(rc
, path
, extent_key
,
3102 BUG_ON(ref_owner
>= BTRFS_MAX_LEVEL
);
3103 level
= (int)ref_owner
;
3104 /* FIXME: get real generation */
3111 btrfs_release_path(rc
->extent_root
, path
);
3113 BUG_ON(level
== -1);
3115 block
= kmalloc(sizeof(*block
), GFP_NOFS
);
3119 block
->bytenr
= extent_key
->objectid
;
3120 block
->key
.objectid
= extent_key
->offset
;
3121 block
->key
.offset
= generation
;
3122 block
->level
= level
;
3123 block
->key_ready
= 0;
3125 rb_node
= tree_insert(blocks
, block
->bytenr
, &block
->rb_node
);
3132 * helper to add tree blocks for backref of type BTRFS_SHARED_DATA_REF_KEY
3134 static int __add_tree_block(struct reloc_control
*rc
,
3135 u64 bytenr
, u32 blocksize
,
3136 struct rb_root
*blocks
)
3138 struct btrfs_path
*path
;
3139 struct btrfs_key key
;
3142 if (tree_block_processed(bytenr
, blocksize
, rc
))
3145 if (tree_search(blocks
, bytenr
))
3148 path
= btrfs_alloc_path();
3152 key
.objectid
= bytenr
;
3153 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
3154 key
.offset
= blocksize
;
3156 path
->search_commit_root
= 1;
3157 path
->skip_locking
= 1;
3158 ret
= btrfs_search_slot(NULL
, rc
->extent_root
, &key
, path
, 0, 0);
3163 btrfs_item_key_to_cpu(path
->nodes
[0], &key
, path
->slots
[0]);
3164 ret
= add_tree_block(rc
, &key
, path
, blocks
);
3166 btrfs_free_path(path
);
3171 * helper to check if the block use full backrefs for pointers in it
3173 static int block_use_full_backref(struct reloc_control
*rc
,
3174 struct extent_buffer
*eb
)
3179 if (btrfs_header_flag(eb
, BTRFS_HEADER_FLAG_RELOC
) ||
3180 btrfs_header_backref_rev(eb
) < BTRFS_MIXED_BACKREF_REV
)
3183 ret
= btrfs_lookup_extent_info(NULL
, rc
->extent_root
,
3184 eb
->start
, eb
->len
, NULL
, &flags
);
3187 if (flags
& BTRFS_BLOCK_FLAG_FULL_BACKREF
)
3195 * helper to add tree blocks for backref of type BTRFS_EXTENT_DATA_REF_KEY
3196 * this function scans fs tree to find blocks reference the data extent
3198 static int find_data_references(struct reloc_control
*rc
,
3199 struct btrfs_key
*extent_key
,
3200 struct extent_buffer
*leaf
,
3201 struct btrfs_extent_data_ref
*ref
,
3202 struct rb_root
*blocks
)
3204 struct btrfs_path
*path
;
3205 struct tree_block
*block
;
3206 struct btrfs_root
*root
;
3207 struct btrfs_file_extent_item
*fi
;
3208 struct rb_node
*rb_node
;
3209 struct btrfs_key key
;
3220 path
= btrfs_alloc_path();
3224 ref_root
= btrfs_extent_data_ref_root(leaf
, ref
);
3225 ref_objectid
= btrfs_extent_data_ref_objectid(leaf
, ref
);
3226 ref_offset
= btrfs_extent_data_ref_offset(leaf
, ref
);
3227 ref_count
= btrfs_extent_data_ref_count(leaf
, ref
);
3229 root
= read_fs_root(rc
->extent_root
->fs_info
, ref_root
);
3231 err
= PTR_ERR(root
);
3235 key
.objectid
= ref_objectid
;
3236 key
.offset
= ref_offset
;
3237 key
.type
= BTRFS_EXTENT_DATA_KEY
;
3239 path
->search_commit_root
= 1;
3240 path
->skip_locking
= 1;
3241 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
3247 leaf
= path
->nodes
[0];
3248 nritems
= btrfs_header_nritems(leaf
);
3250 * the references in tree blocks that use full backrefs
3251 * are not counted in
3253 if (block_use_full_backref(rc
, leaf
))
3257 rb_node
= tree_search(blocks
, leaf
->start
);
3262 path
->slots
[0] = nritems
;
3265 while (ref_count
> 0) {
3266 while (path
->slots
[0] >= nritems
) {
3267 ret
= btrfs_next_leaf(root
, path
);
3277 leaf
= path
->nodes
[0];
3278 nritems
= btrfs_header_nritems(leaf
);
3281 if (block_use_full_backref(rc
, leaf
))
3285 rb_node
= tree_search(blocks
, leaf
->start
);
3290 path
->slots
[0] = nritems
;
3294 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
3295 if (key
.objectid
!= ref_objectid
||
3296 key
.type
!= BTRFS_EXTENT_DATA_KEY
) {
3301 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
3302 struct btrfs_file_extent_item
);
3304 if (btrfs_file_extent_type(leaf
, fi
) ==
3305 BTRFS_FILE_EXTENT_INLINE
)
3308 if (btrfs_file_extent_disk_bytenr(leaf
, fi
) !=
3309 extent_key
->objectid
)
3312 key
.offset
-= btrfs_file_extent_offset(leaf
, fi
);
3313 if (key
.offset
!= ref_offset
)
3321 if (!tree_block_processed(leaf
->start
, leaf
->len
, rc
)) {
3322 block
= kmalloc(sizeof(*block
), GFP_NOFS
);
3327 block
->bytenr
= leaf
->start
;
3328 btrfs_item_key_to_cpu(leaf
, &block
->key
, 0);
3330 block
->key_ready
= 1;
3331 rb_node
= tree_insert(blocks
, block
->bytenr
,
3338 path
->slots
[0] = nritems
;
3344 btrfs_free_path(path
);
3349 * hepler to find all tree blocks that reference a given data extent
3351 static noinline_for_stack
3352 int add_data_references(struct reloc_control
*rc
,
3353 struct btrfs_key
*extent_key
,
3354 struct btrfs_path
*path
,
3355 struct rb_root
*blocks
)
3357 struct btrfs_key key
;
3358 struct extent_buffer
*eb
;
3359 struct btrfs_extent_data_ref
*dref
;
3360 struct btrfs_extent_inline_ref
*iref
;
3363 u32 blocksize
= btrfs_level_size(rc
->extent_root
, 0);
3367 eb
= path
->nodes
[0];
3368 ptr
= btrfs_item_ptr_offset(eb
, path
->slots
[0]);
3369 end
= ptr
+ btrfs_item_size_nr(eb
, path
->slots
[0]);
3370 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3371 if (ptr
+ sizeof(struct btrfs_extent_item_v0
) == end
)
3375 ptr
+= sizeof(struct btrfs_extent_item
);
3378 iref
= (struct btrfs_extent_inline_ref
*)ptr
;
3379 key
.type
= btrfs_extent_inline_ref_type(eb
, iref
);
3380 if (key
.type
== BTRFS_SHARED_DATA_REF_KEY
) {
3381 key
.offset
= btrfs_extent_inline_ref_offset(eb
, iref
);
3382 ret
= __add_tree_block(rc
, key
.offset
, blocksize
,
3384 } else if (key
.type
== BTRFS_EXTENT_DATA_REF_KEY
) {
3385 dref
= (struct btrfs_extent_data_ref
*)(&iref
->offset
);
3386 ret
= find_data_references(rc
, extent_key
,
3391 ptr
+= btrfs_extent_inline_ref_size(key
.type
);
3397 eb
= path
->nodes
[0];
3398 if (path
->slots
[0] >= btrfs_header_nritems(eb
)) {
3399 ret
= btrfs_next_leaf(rc
->extent_root
, path
);
3406 eb
= path
->nodes
[0];
3409 btrfs_item_key_to_cpu(eb
, &key
, path
->slots
[0]);
3410 if (key
.objectid
!= extent_key
->objectid
)
3413 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3414 if (key
.type
== BTRFS_SHARED_DATA_REF_KEY
||
3415 key
.type
== BTRFS_EXTENT_REF_V0_KEY
) {
3417 BUG_ON(key
.type
== BTRFS_EXTENT_REF_V0_KEY
);
3418 if (key
.type
== BTRFS_SHARED_DATA_REF_KEY
) {
3420 ret
= __add_tree_block(rc
, key
.offset
, blocksize
,
3422 } else if (key
.type
== BTRFS_EXTENT_DATA_REF_KEY
) {
3423 dref
= btrfs_item_ptr(eb
, path
->slots
[0],
3424 struct btrfs_extent_data_ref
);
3425 ret
= find_data_references(rc
, extent_key
,
3436 btrfs_release_path(rc
->extent_root
, path
);
3438 free_block_list(blocks
);
3443 * hepler to find next unprocessed extent
3445 static noinline_for_stack
3446 int find_next_extent(struct btrfs_trans_handle
*trans
,
3447 struct reloc_control
*rc
, struct btrfs_path
*path
,
3448 struct btrfs_key
*extent_key
)
3450 struct btrfs_key key
;
3451 struct extent_buffer
*leaf
;
3452 u64 start
, end
, last
;
3455 last
= rc
->block_group
->key
.objectid
+ rc
->block_group
->key
.offset
;
3458 if (rc
->search_start
>= last
) {
3463 key
.objectid
= rc
->search_start
;
3464 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
3467 path
->search_commit_root
= 1;
3468 path
->skip_locking
= 1;
3469 ret
= btrfs_search_slot(NULL
, rc
->extent_root
, &key
, path
,
3474 leaf
= path
->nodes
[0];
3475 if (path
->slots
[0] >= btrfs_header_nritems(leaf
)) {
3476 ret
= btrfs_next_leaf(rc
->extent_root
, path
);
3479 leaf
= path
->nodes
[0];
3482 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
3483 if (key
.objectid
>= last
) {
3488 if (key
.type
!= BTRFS_EXTENT_ITEM_KEY
||
3489 key
.objectid
+ key
.offset
<= rc
->search_start
) {
3494 ret
= find_first_extent_bit(&rc
->processed_blocks
,
3495 key
.objectid
, &start
, &end
,
3498 if (ret
== 0 && start
<= key
.objectid
) {
3499 btrfs_release_path(rc
->extent_root
, path
);
3500 rc
->search_start
= end
+ 1;
3502 rc
->search_start
= key
.objectid
+ key
.offset
;
3503 memcpy(extent_key
, &key
, sizeof(key
));
3507 btrfs_release_path(rc
->extent_root
, path
);
3511 static void set_reloc_control(struct reloc_control
*rc
)
3513 struct btrfs_fs_info
*fs_info
= rc
->extent_root
->fs_info
;
3514 mutex_lock(&fs_info
->trans_mutex
);
3515 fs_info
->reloc_ctl
= rc
;
3516 mutex_unlock(&fs_info
->trans_mutex
);
3519 static void unset_reloc_control(struct reloc_control
*rc
)
3521 struct btrfs_fs_info
*fs_info
= rc
->extent_root
->fs_info
;
3522 mutex_lock(&fs_info
->trans_mutex
);
3523 fs_info
->reloc_ctl
= NULL
;
3524 mutex_unlock(&fs_info
->trans_mutex
);
3527 static int check_extent_flags(u64 flags
)
3529 if ((flags
& BTRFS_EXTENT_FLAG_DATA
) &&
3530 (flags
& BTRFS_EXTENT_FLAG_TREE_BLOCK
))
3532 if (!(flags
& BTRFS_EXTENT_FLAG_DATA
) &&
3533 !(flags
& BTRFS_EXTENT_FLAG_TREE_BLOCK
))
3535 if ((flags
& BTRFS_EXTENT_FLAG_DATA
) &&
3536 (flags
& BTRFS_BLOCK_FLAG_FULL_BACKREF
))
3541 static noinline_for_stack
3542 int prepare_to_relocate(struct reloc_control
*rc
)
3544 struct btrfs_trans_handle
*trans
;
3547 rc
->block_rsv
= btrfs_alloc_block_rsv(rc
->extent_root
);
3552 * reserve some space for creating reloc trees.
3553 * btrfs_init_reloc_root will use them when there
3554 * is no reservation in transaction handle.
3556 ret
= btrfs_block_rsv_add(NULL
, rc
->extent_root
, rc
->block_rsv
,
3557 rc
->extent_root
->nodesize
* 256,
3558 &rc
->block_rsv_retries
);
3562 rc
->block_rsv
->refill_used
= 1;
3563 btrfs_add_durable_block_rsv(rc
->extent_root
->fs_info
, rc
->block_rsv
);
3565 memset(&rc
->cluster
, 0, sizeof(rc
->cluster
));
3566 rc
->search_start
= rc
->block_group
->key
.objectid
;
3567 rc
->extents_found
= 0;
3568 rc
->nodes_relocated
= 0;
3569 rc
->merging_rsv_size
= 0;
3570 rc
->block_rsv_retries
= 0;
3572 rc
->create_reloc_tree
= 1;
3573 set_reloc_control(rc
);
3575 trans
= btrfs_join_transaction(rc
->extent_root
, 1);
3576 btrfs_commit_transaction(trans
, rc
->extent_root
);
3580 static noinline_for_stack
int relocate_block_group(struct reloc_control
*rc
)
3582 struct rb_root blocks
= RB_ROOT
;
3583 struct btrfs_key key
;
3584 struct btrfs_trans_handle
*trans
= NULL
;
3585 struct btrfs_path
*path
;
3586 struct btrfs_extent_item
*ei
;
3593 path
= btrfs_alloc_path();
3597 ret
= prepare_to_relocate(rc
);
3604 trans
= btrfs_start_transaction(rc
->extent_root
, 0);
3606 if (update_backref_cache(trans
, &rc
->backref_cache
)) {
3607 btrfs_end_transaction(trans
, rc
->extent_root
);
3611 ret
= find_next_extent(trans
, rc
, path
, &key
);
3617 rc
->extents_found
++;
3619 ei
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
3620 struct btrfs_extent_item
);
3621 item_size
= btrfs_item_size_nr(path
->nodes
[0], path
->slots
[0]);
3622 if (item_size
>= sizeof(*ei
)) {
3623 flags
= btrfs_extent_flags(path
->nodes
[0], ei
);
3624 ret
= check_extent_flags(flags
);
3628 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3630 int path_change
= 0;
3633 sizeof(struct btrfs_extent_item_v0
));
3634 ret
= get_ref_objectid_v0(rc
, path
, &key
, &ref_owner
,
3636 if (ref_owner
< BTRFS_FIRST_FREE_OBJECTID
)
3637 flags
= BTRFS_EXTENT_FLAG_TREE_BLOCK
;
3639 flags
= BTRFS_EXTENT_FLAG_DATA
;
3642 btrfs_release_path(rc
->extent_root
, path
);
3644 path
->search_commit_root
= 1;
3645 path
->skip_locking
= 1;
3646 ret
= btrfs_search_slot(NULL
, rc
->extent_root
,
3659 if (flags
& BTRFS_EXTENT_FLAG_TREE_BLOCK
) {
3660 ret
= add_tree_block(rc
, &key
, path
, &blocks
);
3661 } else if (rc
->stage
== UPDATE_DATA_PTRS
&&
3662 (flags
& BTRFS_EXTENT_FLAG_DATA
)) {
3663 ret
= add_data_references(rc
, &key
, path
, &blocks
);
3665 btrfs_release_path(rc
->extent_root
, path
);
3673 if (!RB_EMPTY_ROOT(&blocks
)) {
3674 ret
= relocate_tree_blocks(trans
, rc
, &blocks
);
3676 if (ret
!= -EAGAIN
) {
3680 rc
->extents_found
--;
3681 rc
->search_start
= key
.objectid
;
3685 ret
= btrfs_block_rsv_check(trans
, rc
->extent_root
,
3686 rc
->block_rsv
, 0, 5);
3688 if (ret
!= -EAGAIN
) {
3693 rc
->commit_transaction
= 1;
3696 if (rc
->commit_transaction
) {
3697 rc
->commit_transaction
= 0;
3698 ret
= btrfs_commit_transaction(trans
, rc
->extent_root
);
3701 nr
= trans
->blocks_used
;
3702 btrfs_end_transaction_throttle(trans
, rc
->extent_root
);
3703 btrfs_btree_balance_dirty(rc
->extent_root
, nr
);
3707 if (rc
->stage
== MOVE_DATA_EXTENTS
&&
3708 (flags
& BTRFS_EXTENT_FLAG_DATA
)) {
3709 rc
->found_file_extent
= 1;
3710 ret
= relocate_data_extent(rc
->data_inode
,
3711 &key
, &rc
->cluster
);
3719 btrfs_release_path(rc
->extent_root
, path
);
3720 clear_extent_bits(&rc
->processed_blocks
, 0, (u64
)-1, EXTENT_DIRTY
,
3724 nr
= trans
->blocks_used
;
3725 btrfs_end_transaction_throttle(trans
, rc
->extent_root
);
3726 btrfs_btree_balance_dirty(rc
->extent_root
, nr
);
3730 ret
= relocate_file_extent_cluster(rc
->data_inode
,
3736 rc
->create_reloc_tree
= 0;
3737 set_reloc_control(rc
);
3739 backref_cache_cleanup(&rc
->backref_cache
);
3740 btrfs_block_rsv_release(rc
->extent_root
, rc
->block_rsv
, (u64
)-1);
3742 err
= prepare_to_merge(rc
, err
);
3744 merge_reloc_roots(rc
);
3746 rc
->merge_reloc_tree
= 0;
3747 unset_reloc_control(rc
);
3748 btrfs_block_rsv_release(rc
->extent_root
, rc
->block_rsv
, (u64
)-1);
3750 /* get rid of pinned extents */
3751 trans
= btrfs_join_transaction(rc
->extent_root
, 1);
3752 btrfs_commit_transaction(trans
, rc
->extent_root
);
3754 btrfs_free_block_rsv(rc
->extent_root
, rc
->block_rsv
);
3755 btrfs_free_path(path
);
3759 static int __insert_orphan_inode(struct btrfs_trans_handle
*trans
,
3760 struct btrfs_root
*root
, u64 objectid
)
3762 struct btrfs_path
*path
;
3763 struct btrfs_inode_item
*item
;
3764 struct extent_buffer
*leaf
;
3767 path
= btrfs_alloc_path();
3771 ret
= btrfs_insert_empty_inode(trans
, root
, path
, objectid
);
3775 leaf
= path
->nodes
[0];
3776 item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_inode_item
);
3777 memset_extent_buffer(leaf
, 0, (unsigned long)item
, sizeof(*item
));
3778 btrfs_set_inode_generation(leaf
, item
, 1);
3779 btrfs_set_inode_size(leaf
, item
, 0);
3780 btrfs_set_inode_mode(leaf
, item
, S_IFREG
| 0600);
3781 btrfs_set_inode_flags(leaf
, item
, BTRFS_INODE_NOCOMPRESS
|
3782 BTRFS_INODE_PREALLOC
);
3783 btrfs_mark_buffer_dirty(leaf
);
3784 btrfs_release_path(root
, path
);
3786 btrfs_free_path(path
);
3791 * helper to create inode for data relocation.
3792 * the inode is in data relocation tree and its link count is 0
3794 static noinline_for_stack
3795 struct inode
*create_reloc_inode(struct btrfs_fs_info
*fs_info
,
3796 struct btrfs_block_group_cache
*group
)
3798 struct inode
*inode
= NULL
;
3799 struct btrfs_trans_handle
*trans
;
3800 struct btrfs_root
*root
;
3801 struct btrfs_key key
;
3803 u64 objectid
= BTRFS_FIRST_FREE_OBJECTID
;
3806 root
= read_fs_root(fs_info
, BTRFS_DATA_RELOC_TREE_OBJECTID
);
3808 return ERR_CAST(root
);
3810 trans
= btrfs_start_transaction(root
, 6);
3812 return ERR_CAST(trans
);
3814 err
= btrfs_find_free_objectid(trans
, root
, objectid
, &objectid
);
3818 err
= __insert_orphan_inode(trans
, root
, objectid
);
3821 key
.objectid
= objectid
;
3822 key
.type
= BTRFS_INODE_ITEM_KEY
;
3824 inode
= btrfs_iget(root
->fs_info
->sb
, &key
, root
, NULL
);
3825 BUG_ON(IS_ERR(inode
) || is_bad_inode(inode
));
3826 BTRFS_I(inode
)->index_cnt
= group
->key
.objectid
;
3828 err
= btrfs_orphan_add(trans
, inode
);
3830 nr
= trans
->blocks_used
;
3831 btrfs_end_transaction(trans
, root
);
3832 btrfs_btree_balance_dirty(root
, nr
);
3836 inode
= ERR_PTR(err
);
3841 static struct reloc_control
*alloc_reloc_control(void)
3843 struct reloc_control
*rc
;
3845 rc
= kzalloc(sizeof(*rc
), GFP_NOFS
);
3849 INIT_LIST_HEAD(&rc
->reloc_roots
);
3850 backref_cache_init(&rc
->backref_cache
);
3851 mapping_tree_init(&rc
->reloc_root_tree
);
3852 extent_io_tree_init(&rc
->processed_blocks
, NULL
, GFP_NOFS
);
3857 * function to relocate all extents in a block group.
3859 int btrfs_relocate_block_group(struct btrfs_root
*extent_root
, u64 group_start
)
3861 struct btrfs_fs_info
*fs_info
= extent_root
->fs_info
;
3862 struct reloc_control
*rc
;
3867 rc
= alloc_reloc_control();
3871 rc
->extent_root
= extent_root
;
3873 rc
->block_group
= btrfs_lookup_block_group(fs_info
, group_start
);
3874 BUG_ON(!rc
->block_group
);
3876 if (!rc
->block_group
->ro
) {
3877 ret
= btrfs_set_block_group_ro(extent_root
, rc
->block_group
);
3885 rc
->data_inode
= create_reloc_inode(fs_info
, rc
->block_group
);
3886 if (IS_ERR(rc
->data_inode
)) {
3887 err
= PTR_ERR(rc
->data_inode
);
3888 rc
->data_inode
= NULL
;
3892 printk(KERN_INFO
"btrfs: relocating block group %llu flags %llu\n",
3893 (unsigned long long)rc
->block_group
->key
.objectid
,
3894 (unsigned long long)rc
->block_group
->flags
);
3896 btrfs_start_delalloc_inodes(fs_info
->tree_root
, 0);
3897 btrfs_wait_ordered_extents(fs_info
->tree_root
, 0, 0);
3900 mutex_lock(&fs_info
->cleaner_mutex
);
3902 btrfs_clean_old_snapshots(fs_info
->tree_root
);
3903 ret
= relocate_block_group(rc
);
3905 mutex_unlock(&fs_info
->cleaner_mutex
);
3911 if (rc
->extents_found
== 0)
3914 printk(KERN_INFO
"btrfs: found %llu extents\n",
3915 (unsigned long long)rc
->extents_found
);
3917 if (rc
->stage
== MOVE_DATA_EXTENTS
&& rc
->found_file_extent
) {
3918 btrfs_wait_ordered_range(rc
->data_inode
, 0, (u64
)-1);
3919 invalidate_mapping_pages(rc
->data_inode
->i_mapping
,
3921 rc
->stage
= UPDATE_DATA_PTRS
;
3925 filemap_write_and_wait_range(fs_info
->btree_inode
->i_mapping
,
3926 rc
->block_group
->key
.objectid
,
3927 rc
->block_group
->key
.objectid
+
3928 rc
->block_group
->key
.offset
- 1);
3930 WARN_ON(rc
->block_group
->pinned
> 0);
3931 WARN_ON(rc
->block_group
->reserved
> 0);
3932 WARN_ON(btrfs_block_group_used(&rc
->block_group
->item
) > 0);
3935 btrfs_set_block_group_rw(extent_root
, rc
->block_group
);
3936 iput(rc
->data_inode
);
3937 btrfs_put_block_group(rc
->block_group
);
3942 static noinline_for_stack
int mark_garbage_root(struct btrfs_root
*root
)
3944 struct btrfs_trans_handle
*trans
;
3947 trans
= btrfs_start_transaction(root
->fs_info
->tree_root
, 0);
3949 memset(&root
->root_item
.drop_progress
, 0,
3950 sizeof(root
->root_item
.drop_progress
));
3951 root
->root_item
.drop_level
= 0;
3952 btrfs_set_root_refs(&root
->root_item
, 0);
3953 ret
= btrfs_update_root(trans
, root
->fs_info
->tree_root
,
3954 &root
->root_key
, &root
->root_item
);
3957 ret
= btrfs_end_transaction(trans
, root
->fs_info
->tree_root
);
3963 * recover relocation interrupted by system crash.
3965 * this function resumes merging reloc trees with corresponding fs trees.
3966 * this is important for keeping the sharing of tree blocks
3968 int btrfs_recover_relocation(struct btrfs_root
*root
)
3970 LIST_HEAD(reloc_roots
);
3971 struct btrfs_key key
;
3972 struct btrfs_root
*fs_root
;
3973 struct btrfs_root
*reloc_root
;
3974 struct btrfs_path
*path
;
3975 struct extent_buffer
*leaf
;
3976 struct reloc_control
*rc
= NULL
;
3977 struct btrfs_trans_handle
*trans
;
3981 path
= btrfs_alloc_path();
3985 key
.objectid
= BTRFS_TREE_RELOC_OBJECTID
;
3986 key
.type
= BTRFS_ROOT_ITEM_KEY
;
3987 key
.offset
= (u64
)-1;
3990 ret
= btrfs_search_slot(NULL
, root
->fs_info
->tree_root
, &key
,
3997 if (path
->slots
[0] == 0)
4001 leaf
= path
->nodes
[0];
4002 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
4003 btrfs_release_path(root
->fs_info
->tree_root
, path
);
4005 if (key
.objectid
!= BTRFS_TREE_RELOC_OBJECTID
||
4006 key
.type
!= BTRFS_ROOT_ITEM_KEY
)
4009 reloc_root
= btrfs_read_fs_root_no_radix(root
, &key
);
4010 if (IS_ERR(reloc_root
)) {
4011 err
= PTR_ERR(reloc_root
);
4015 list_add(&reloc_root
->root_list
, &reloc_roots
);
4017 if (btrfs_root_refs(&reloc_root
->root_item
) > 0) {
4018 fs_root
= read_fs_root(root
->fs_info
,
4019 reloc_root
->root_key
.offset
);
4020 if (IS_ERR(fs_root
)) {
4021 ret
= PTR_ERR(fs_root
);
4022 if (ret
!= -ENOENT
) {
4026 mark_garbage_root(reloc_root
);
4030 if (key
.offset
== 0)
4035 btrfs_release_path(root
->fs_info
->tree_root
, path
);
4037 if (list_empty(&reloc_roots
))
4040 rc
= alloc_reloc_control();
4046 rc
->extent_root
= root
->fs_info
->extent_root
;
4048 set_reloc_control(rc
);
4050 trans
= btrfs_join_transaction(rc
->extent_root
, 1);
4052 rc
->merge_reloc_tree
= 1;
4054 while (!list_empty(&reloc_roots
)) {
4055 reloc_root
= list_entry(reloc_roots
.next
,
4056 struct btrfs_root
, root_list
);
4057 list_del(&reloc_root
->root_list
);
4059 if (btrfs_root_refs(&reloc_root
->root_item
) == 0) {
4060 list_add_tail(&reloc_root
->root_list
,
4065 fs_root
= read_fs_root(root
->fs_info
,
4066 reloc_root
->root_key
.offset
);
4067 BUG_ON(IS_ERR(fs_root
));
4069 __add_reloc_root(reloc_root
);
4070 fs_root
->reloc_root
= reloc_root
;
4073 btrfs_commit_transaction(trans
, rc
->extent_root
);
4075 merge_reloc_roots(rc
);
4077 unset_reloc_control(rc
);
4079 trans
= btrfs_join_transaction(rc
->extent_root
, 1);
4080 btrfs_commit_transaction(trans
, rc
->extent_root
);
4083 while (!list_empty(&reloc_roots
)) {
4084 reloc_root
= list_entry(reloc_roots
.next
,
4085 struct btrfs_root
, root_list
);
4086 list_del(&reloc_root
->root_list
);
4087 free_extent_buffer(reloc_root
->node
);
4088 free_extent_buffer(reloc_root
->commit_root
);
4091 btrfs_free_path(path
);
4094 /* cleanup orphan inode in data relocation tree */
4095 fs_root
= read_fs_root(root
->fs_info
,
4096 BTRFS_DATA_RELOC_TREE_OBJECTID
);
4097 if (IS_ERR(fs_root
))
4098 err
= PTR_ERR(fs_root
);
4100 btrfs_orphan_cleanup(fs_root
);
4106 * helper to add ordered checksum for data relocation.
4108 * cloning checksum properly handles the nodatasum extents.
4109 * it also saves CPU time to re-calculate the checksum.
4111 int btrfs_reloc_clone_csums(struct inode
*inode
, u64 file_pos
, u64 len
)
4113 struct btrfs_ordered_sum
*sums
;
4114 struct btrfs_sector_sum
*sector_sum
;
4115 struct btrfs_ordered_extent
*ordered
;
4116 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4122 ordered
= btrfs_lookup_ordered_extent(inode
, file_pos
);
4123 BUG_ON(ordered
->file_offset
!= file_pos
|| ordered
->len
!= len
);
4125 disk_bytenr
= file_pos
+ BTRFS_I(inode
)->index_cnt
;
4126 ret
= btrfs_lookup_csums_range(root
->fs_info
->csum_root
, disk_bytenr
,
4127 disk_bytenr
+ len
- 1, &list
);
4129 while (!list_empty(&list
)) {
4130 sums
= list_entry(list
.next
, struct btrfs_ordered_sum
, list
);
4131 list_del_init(&sums
->list
);
4133 sector_sum
= sums
->sums
;
4134 sums
->bytenr
= ordered
->start
;
4137 while (offset
< sums
->len
) {
4138 sector_sum
->bytenr
+= ordered
->start
- disk_bytenr
;
4140 offset
+= root
->sectorsize
;
4143 btrfs_add_ordered_sum(inode
, ordered
, sums
);
4145 btrfs_put_ordered_extent(ordered
);
4149 void btrfs_reloc_cow_block(struct btrfs_trans_handle
*trans
,
4150 struct btrfs_root
*root
, struct extent_buffer
*buf
,
4151 struct extent_buffer
*cow
)
4153 struct reloc_control
*rc
;
4154 struct backref_node
*node
;
4159 rc
= root
->fs_info
->reloc_ctl
;
4163 BUG_ON(rc
->stage
== UPDATE_DATA_PTRS
&&
4164 root
->root_key
.objectid
== BTRFS_DATA_RELOC_TREE_OBJECTID
);
4166 level
= btrfs_header_level(buf
);
4167 if (btrfs_header_generation(buf
) <=
4168 btrfs_root_last_snapshot(&root
->root_item
))
4171 if (root
->root_key
.objectid
== BTRFS_TREE_RELOC_OBJECTID
&&
4172 rc
->create_reloc_tree
) {
4173 WARN_ON(!first_cow
&& level
== 0);
4175 node
= rc
->backref_cache
.path
[level
];
4176 BUG_ON(node
->bytenr
!= buf
->start
&&
4177 node
->new_bytenr
!= buf
->start
);
4179 drop_node_buffer(node
);
4180 extent_buffer_get(cow
);
4182 node
->new_bytenr
= cow
->start
;
4184 if (!node
->pending
) {
4185 list_move_tail(&node
->list
,
4186 &rc
->backref_cache
.pending
[level
]);
4191 __mark_block_processed(rc
, node
);
4193 if (first_cow
&& level
> 0)
4194 rc
->nodes_relocated
+= buf
->len
;
4197 if (level
== 0 && first_cow
&& rc
->stage
== UPDATE_DATA_PTRS
) {
4198 ret
= replace_file_extents(trans
, rc
, root
, cow
);
4204 * called before creating snapshot. it calculates metadata reservation
4205 * requried for relocating tree blocks in the snapshot
4207 void btrfs_reloc_pre_snapshot(struct btrfs_trans_handle
*trans
,
4208 struct btrfs_pending_snapshot
*pending
,
4209 u64
*bytes_to_reserve
)
4211 struct btrfs_root
*root
;
4212 struct reloc_control
*rc
;
4214 root
= pending
->root
;
4215 if (!root
->reloc_root
)
4218 rc
= root
->fs_info
->reloc_ctl
;
4219 if (!rc
->merge_reloc_tree
)
4222 root
= root
->reloc_root
;
4223 BUG_ON(btrfs_root_refs(&root
->root_item
) == 0);
4225 * relocation is in the stage of merging trees. the space
4226 * used by merging a reloc tree is twice the size of
4227 * relocated tree nodes in the worst case. half for cowing
4228 * the reloc tree, half for cowing the fs tree. the space
4229 * used by cowing the reloc tree will be freed after the
4230 * tree is dropped. if we create snapshot, cowing the fs
4231 * tree may use more space than it frees. so we need
4232 * reserve extra space.
4234 *bytes_to_reserve
+= rc
->nodes_relocated
;
4238 * called after snapshot is created. migrate block reservation
4239 * and create reloc root for the newly created snapshot
4241 void btrfs_reloc_post_snapshot(struct btrfs_trans_handle
*trans
,
4242 struct btrfs_pending_snapshot
*pending
)
4244 struct btrfs_root
*root
= pending
->root
;
4245 struct btrfs_root
*reloc_root
;
4246 struct btrfs_root
*new_root
;
4247 struct reloc_control
*rc
;
4250 if (!root
->reloc_root
)
4253 rc
= root
->fs_info
->reloc_ctl
;
4254 rc
->merging_rsv_size
+= rc
->nodes_relocated
;
4256 if (rc
->merge_reloc_tree
) {
4257 ret
= btrfs_block_rsv_migrate(&pending
->block_rsv
,
4259 rc
->nodes_relocated
);
4263 new_root
= pending
->snap
;
4264 reloc_root
= create_reloc_root(trans
, root
->reloc_root
,
4265 new_root
->root_key
.objectid
);
4267 __add_reloc_root(reloc_root
);
4268 new_root
->reloc_root
= reloc_root
;
4270 if (rc
->create_reloc_tree
) {
4271 ret
= clone_backref_node(trans
, rc
, root
, reloc_root
);