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 root
= find_tree_root(rc
, eb
, ref0
);
790 if (key
.objectid
== key
.offset
) {
791 if (root
&& !should_ignore_root(root
))
794 list_add(&cur
->list
, &useless
);
799 BUG_ON(key
.type
== BTRFS_EXTENT_REF_V0_KEY
);
800 if (key
.type
== BTRFS_SHARED_BLOCK_REF_KEY
) {
802 if (key
.objectid
== key
.offset
) {
804 * only root blocks of reloc trees use
805 * backref of this type.
807 root
= find_reloc_root(rc
, cur
->bytenr
);
813 edge
= alloc_backref_edge(cache
);
818 rb_node
= tree_search(&cache
->rb_root
, key
.offset
);
820 upper
= alloc_backref_node(cache
);
822 free_backref_edge(cache
, edge
);
826 upper
->bytenr
= key
.offset
;
827 upper
->level
= cur
->level
+ 1;
829 * backrefs for the upper level block isn't
830 * cached, add the block to pending list
832 list_add_tail(&edge
->list
[UPPER
], &list
);
834 upper
= rb_entry(rb_node
, struct backref_node
,
836 BUG_ON(!upper
->checked
);
837 INIT_LIST_HEAD(&edge
->list
[UPPER
]);
839 list_add_tail(&edge
->list
[LOWER
], &cur
->upper
);
840 edge
->node
[LOWER
] = cur
;
841 edge
->node
[UPPER
] = upper
;
844 } else if (key
.type
!= BTRFS_TREE_BLOCK_REF_KEY
) {
848 /* key.type == BTRFS_TREE_BLOCK_REF_KEY */
849 root
= read_fs_root(rc
->extent_root
->fs_info
, key
.offset
);
858 if (btrfs_root_level(&root
->root_item
) == cur
->level
) {
860 BUG_ON(btrfs_root_bytenr(&root
->root_item
) !=
862 if (should_ignore_root(root
))
863 list_add(&cur
->list
, &useless
);
869 level
= cur
->level
+ 1;
872 * searching the tree to find upper level blocks
873 * reference the block.
875 path2
->search_commit_root
= 1;
876 path2
->skip_locking
= 1;
877 path2
->lowest_level
= level
;
878 ret
= btrfs_search_slot(NULL
, root
, node_key
, path2
, 0, 0);
879 path2
->lowest_level
= 0;
884 if (ret
> 0 && path2
->slots
[level
] > 0)
885 path2
->slots
[level
]--;
887 eb
= path2
->nodes
[level
];
888 WARN_ON(btrfs_node_blockptr(eb
, path2
->slots
[level
]) !=
892 for (; level
< BTRFS_MAX_LEVEL
; level
++) {
893 if (!path2
->nodes
[level
]) {
894 BUG_ON(btrfs_root_bytenr(&root
->root_item
) !=
896 if (should_ignore_root(root
))
897 list_add(&lower
->list
, &useless
);
903 edge
= alloc_backref_edge(cache
);
909 eb
= path2
->nodes
[level
];
910 rb_node
= tree_search(&cache
->rb_root
, eb
->start
);
912 upper
= alloc_backref_node(cache
);
914 free_backref_edge(cache
, edge
);
918 upper
->bytenr
= eb
->start
;
919 upper
->owner
= btrfs_header_owner(eb
);
920 upper
->level
= lower
->level
+ 1;
925 * if we know the block isn't shared
926 * we can void checking its backrefs.
928 if (btrfs_block_can_be_shared(root
, eb
))
934 * add the block to pending list if we
935 * need check its backrefs. only block
936 * at 'cur->level + 1' is added to the
937 * tail of pending list. this guarantees
938 * we check backrefs from lower level
939 * blocks to upper level blocks.
941 if (!upper
->checked
&&
942 level
== cur
->level
+ 1) {
943 list_add_tail(&edge
->list
[UPPER
],
946 INIT_LIST_HEAD(&edge
->list
[UPPER
]);
948 upper
= rb_entry(rb_node
, struct backref_node
,
950 BUG_ON(!upper
->checked
);
951 INIT_LIST_HEAD(&edge
->list
[UPPER
]);
953 upper
->owner
= btrfs_header_owner(eb
);
955 list_add_tail(&edge
->list
[LOWER
], &lower
->upper
);
956 edge
->node
[LOWER
] = lower
;
957 edge
->node
[UPPER
] = upper
;
964 btrfs_release_path(root
, path2
);
967 ptr
+= btrfs_extent_inline_ref_size(key
.type
);
977 btrfs_release_path(rc
->extent_root
, path1
);
982 /* the pending list isn't empty, take the first block to process */
983 if (!list_empty(&list
)) {
984 edge
= list_entry(list
.next
, struct backref_edge
, list
[UPPER
]);
985 list_del_init(&edge
->list
[UPPER
]);
986 cur
= edge
->node
[UPPER
];
991 * everything goes well, connect backref nodes and insert backref nodes
994 BUG_ON(!node
->checked
);
995 cowonly
= node
->cowonly
;
997 rb_node
= tree_insert(&cache
->rb_root
, node
->bytenr
,
1000 list_add_tail(&node
->lower
, &cache
->leaves
);
1003 list_for_each_entry(edge
, &node
->upper
, list
[LOWER
])
1004 list_add_tail(&edge
->list
[UPPER
], &list
);
1006 while (!list_empty(&list
)) {
1007 edge
= list_entry(list
.next
, struct backref_edge
, list
[UPPER
]);
1008 list_del_init(&edge
->list
[UPPER
]);
1009 upper
= edge
->node
[UPPER
];
1010 if (upper
->detached
) {
1011 list_del(&edge
->list
[LOWER
]);
1012 lower
= edge
->node
[LOWER
];
1013 free_backref_edge(cache
, edge
);
1014 if (list_empty(&lower
->upper
))
1015 list_add(&lower
->list
, &useless
);
1019 if (!RB_EMPTY_NODE(&upper
->rb_node
)) {
1020 if (upper
->lowest
) {
1021 list_del_init(&upper
->lower
);
1025 list_add_tail(&edge
->list
[UPPER
], &upper
->lower
);
1029 BUG_ON(!upper
->checked
);
1030 BUG_ON(cowonly
!= upper
->cowonly
);
1032 rb_node
= tree_insert(&cache
->rb_root
, upper
->bytenr
,
1037 list_add_tail(&edge
->list
[UPPER
], &upper
->lower
);
1039 list_for_each_entry(edge
, &upper
->upper
, list
[LOWER
])
1040 list_add_tail(&edge
->list
[UPPER
], &list
);
1043 * process useless backref nodes. backref nodes for tree leaves
1044 * are deleted from the cache. backref nodes for upper level
1045 * tree blocks are left in the cache to avoid unnecessary backref
1048 while (!list_empty(&useless
)) {
1049 upper
= list_entry(useless
.next
, struct backref_node
, list
);
1050 list_del_init(&upper
->list
);
1051 BUG_ON(!list_empty(&upper
->upper
));
1054 if (upper
->lowest
) {
1055 list_del_init(&upper
->lower
);
1058 while (!list_empty(&upper
->lower
)) {
1059 edge
= list_entry(upper
->lower
.next
,
1060 struct backref_edge
, list
[UPPER
]);
1061 list_del(&edge
->list
[UPPER
]);
1062 list_del(&edge
->list
[LOWER
]);
1063 lower
= edge
->node
[LOWER
];
1064 free_backref_edge(cache
, edge
);
1066 if (list_empty(&lower
->upper
))
1067 list_add(&lower
->list
, &useless
);
1069 __mark_block_processed(rc
, upper
);
1070 if (upper
->level
> 0) {
1071 list_add(&upper
->list
, &cache
->detached
);
1072 upper
->detached
= 1;
1074 rb_erase(&upper
->rb_node
, &cache
->rb_root
);
1075 free_backref_node(cache
, upper
);
1079 btrfs_free_path(path1
);
1080 btrfs_free_path(path2
);
1082 while (!list_empty(&useless
)) {
1083 lower
= list_entry(useless
.next
,
1084 struct backref_node
, upper
);
1085 list_del_init(&lower
->upper
);
1088 INIT_LIST_HEAD(&list
);
1090 if (RB_EMPTY_NODE(&upper
->rb_node
)) {
1091 list_splice_tail(&upper
->upper
, &list
);
1092 free_backref_node(cache
, upper
);
1095 if (list_empty(&list
))
1098 edge
= list_entry(list
.next
, struct backref_edge
,
1100 list_del(&edge
->list
[LOWER
]);
1101 upper
= edge
->node
[UPPER
];
1102 free_backref_edge(cache
, edge
);
1104 return ERR_PTR(err
);
1106 BUG_ON(node
&& node
->detached
);
1111 * helper to add backref node for the newly created snapshot.
1112 * the backref node is created by cloning backref node that
1113 * corresponds to root of source tree
1115 static int clone_backref_node(struct btrfs_trans_handle
*trans
,
1116 struct reloc_control
*rc
,
1117 struct btrfs_root
*src
,
1118 struct btrfs_root
*dest
)
1120 struct btrfs_root
*reloc_root
= src
->reloc_root
;
1121 struct backref_cache
*cache
= &rc
->backref_cache
;
1122 struct backref_node
*node
= NULL
;
1123 struct backref_node
*new_node
;
1124 struct backref_edge
*edge
;
1125 struct backref_edge
*new_edge
;
1126 struct rb_node
*rb_node
;
1128 if (cache
->last_trans
> 0)
1129 update_backref_cache(trans
, cache
);
1131 rb_node
= tree_search(&cache
->rb_root
, src
->commit_root
->start
);
1133 node
= rb_entry(rb_node
, struct backref_node
, rb_node
);
1137 BUG_ON(node
->new_bytenr
!= reloc_root
->node
->start
);
1141 rb_node
= tree_search(&cache
->rb_root
,
1142 reloc_root
->commit_root
->start
);
1144 node
= rb_entry(rb_node
, struct backref_node
,
1146 BUG_ON(node
->detached
);
1153 new_node
= alloc_backref_node(cache
);
1157 new_node
->bytenr
= dest
->node
->start
;
1158 new_node
->level
= node
->level
;
1159 new_node
->lowest
= node
->lowest
;
1160 new_node
->root
= dest
;
1162 if (!node
->lowest
) {
1163 list_for_each_entry(edge
, &node
->lower
, list
[UPPER
]) {
1164 new_edge
= alloc_backref_edge(cache
);
1168 new_edge
->node
[UPPER
] = new_node
;
1169 new_edge
->node
[LOWER
] = edge
->node
[LOWER
];
1170 list_add_tail(&new_edge
->list
[UPPER
],
1175 rb_node
= tree_insert(&cache
->rb_root
, new_node
->bytenr
,
1176 &new_node
->rb_node
);
1179 if (!new_node
->lowest
) {
1180 list_for_each_entry(new_edge
, &new_node
->lower
, list
[UPPER
]) {
1181 list_add_tail(&new_edge
->list
[LOWER
],
1182 &new_edge
->node
[LOWER
]->upper
);
1187 while (!list_empty(&new_node
->lower
)) {
1188 new_edge
= list_entry(new_node
->lower
.next
,
1189 struct backref_edge
, list
[UPPER
]);
1190 list_del(&new_edge
->list
[UPPER
]);
1191 free_backref_edge(cache
, new_edge
);
1193 free_backref_node(cache
, new_node
);
1198 * helper to add 'address of tree root -> reloc tree' mapping
1200 static int __add_reloc_root(struct btrfs_root
*root
)
1202 struct rb_node
*rb_node
;
1203 struct mapping_node
*node
;
1204 struct reloc_control
*rc
= root
->fs_info
->reloc_ctl
;
1206 node
= kmalloc(sizeof(*node
), GFP_NOFS
);
1209 node
->bytenr
= root
->node
->start
;
1212 spin_lock(&rc
->reloc_root_tree
.lock
);
1213 rb_node
= tree_insert(&rc
->reloc_root_tree
.rb_root
,
1214 node
->bytenr
, &node
->rb_node
);
1215 spin_unlock(&rc
->reloc_root_tree
.lock
);
1218 list_add_tail(&root
->root_list
, &rc
->reloc_roots
);
1223 * helper to update/delete the 'address of tree root -> reloc tree'
1226 static int __update_reloc_root(struct btrfs_root
*root
, int del
)
1228 struct rb_node
*rb_node
;
1229 struct mapping_node
*node
= NULL
;
1230 struct reloc_control
*rc
= root
->fs_info
->reloc_ctl
;
1232 spin_lock(&rc
->reloc_root_tree
.lock
);
1233 rb_node
= tree_search(&rc
->reloc_root_tree
.rb_root
,
1234 root
->commit_root
->start
);
1236 node
= rb_entry(rb_node
, struct mapping_node
, rb_node
);
1237 rb_erase(&node
->rb_node
, &rc
->reloc_root_tree
.rb_root
);
1239 spin_unlock(&rc
->reloc_root_tree
.lock
);
1241 BUG_ON((struct btrfs_root
*)node
->data
!= root
);
1244 spin_lock(&rc
->reloc_root_tree
.lock
);
1245 node
->bytenr
= root
->node
->start
;
1246 rb_node
= tree_insert(&rc
->reloc_root_tree
.rb_root
,
1247 node
->bytenr
, &node
->rb_node
);
1248 spin_unlock(&rc
->reloc_root_tree
.lock
);
1251 list_del_init(&root
->root_list
);
1257 static struct btrfs_root
*create_reloc_root(struct btrfs_trans_handle
*trans
,
1258 struct btrfs_root
*root
, u64 objectid
)
1260 struct btrfs_root
*reloc_root
;
1261 struct extent_buffer
*eb
;
1262 struct btrfs_root_item
*root_item
;
1263 struct btrfs_key root_key
;
1266 root_item
= kmalloc(sizeof(*root_item
), GFP_NOFS
);
1269 root_key
.objectid
= BTRFS_TREE_RELOC_OBJECTID
;
1270 root_key
.type
= BTRFS_ROOT_ITEM_KEY
;
1271 root_key
.offset
= objectid
;
1273 if (root
->root_key
.objectid
== objectid
) {
1274 /* called by btrfs_init_reloc_root */
1275 ret
= btrfs_copy_root(trans
, root
, root
->commit_root
, &eb
,
1276 BTRFS_TREE_RELOC_OBJECTID
);
1279 btrfs_set_root_last_snapshot(&root
->root_item
,
1280 trans
->transid
- 1);
1283 * called by btrfs_reloc_post_snapshot_hook.
1284 * the source tree is a reloc tree, all tree blocks
1285 * modified after it was created have RELOC flag
1286 * set in their headers. so it's OK to not update
1287 * the 'last_snapshot'.
1289 ret
= btrfs_copy_root(trans
, root
, root
->node
, &eb
,
1290 BTRFS_TREE_RELOC_OBJECTID
);
1294 memcpy(root_item
, &root
->root_item
, sizeof(*root_item
));
1295 btrfs_set_root_bytenr(root_item
, eb
->start
);
1296 btrfs_set_root_level(root_item
, btrfs_header_level(eb
));
1297 btrfs_set_root_generation(root_item
, trans
->transid
);
1299 if (root
->root_key
.objectid
== objectid
) {
1300 btrfs_set_root_refs(root_item
, 0);
1301 memset(&root_item
->drop_progress
, 0,
1302 sizeof(struct btrfs_disk_key
));
1303 root_item
->drop_level
= 0;
1306 btrfs_tree_unlock(eb
);
1307 free_extent_buffer(eb
);
1309 ret
= btrfs_insert_root(trans
, root
->fs_info
->tree_root
,
1310 &root_key
, root_item
);
1314 reloc_root
= btrfs_read_fs_root_no_radix(root
->fs_info
->tree_root
,
1316 BUG_ON(IS_ERR(reloc_root
));
1317 reloc_root
->last_trans
= trans
->transid
;
1322 * create reloc tree for a given fs tree. reloc tree is just a
1323 * snapshot of the fs tree with special root objectid.
1325 int btrfs_init_reloc_root(struct btrfs_trans_handle
*trans
,
1326 struct btrfs_root
*root
)
1328 struct btrfs_root
*reloc_root
;
1329 struct reloc_control
*rc
= root
->fs_info
->reloc_ctl
;
1332 if (root
->reloc_root
) {
1333 reloc_root
= root
->reloc_root
;
1334 reloc_root
->last_trans
= trans
->transid
;
1338 if (!rc
|| !rc
->create_reloc_tree
||
1339 root
->root_key
.objectid
== BTRFS_TREE_RELOC_OBJECTID
)
1342 if (!trans
->block_rsv
) {
1343 trans
->block_rsv
= rc
->block_rsv
;
1346 reloc_root
= create_reloc_root(trans
, root
, root
->root_key
.objectid
);
1348 trans
->block_rsv
= NULL
;
1350 __add_reloc_root(reloc_root
);
1351 root
->reloc_root
= reloc_root
;
1356 * update root item of reloc tree
1358 int btrfs_update_reloc_root(struct btrfs_trans_handle
*trans
,
1359 struct btrfs_root
*root
)
1361 struct btrfs_root
*reloc_root
;
1362 struct btrfs_root_item
*root_item
;
1366 if (!root
->reloc_root
)
1369 reloc_root
= root
->reloc_root
;
1370 root_item
= &reloc_root
->root_item
;
1372 if (root
->fs_info
->reloc_ctl
->merge_reloc_tree
&&
1373 btrfs_root_refs(root_item
) == 0) {
1374 root
->reloc_root
= NULL
;
1378 __update_reloc_root(reloc_root
, del
);
1380 if (reloc_root
->commit_root
!= reloc_root
->node
) {
1381 btrfs_set_root_node(root_item
, reloc_root
->node
);
1382 free_extent_buffer(reloc_root
->commit_root
);
1383 reloc_root
->commit_root
= btrfs_root_node(reloc_root
);
1386 ret
= btrfs_update_root(trans
, root
->fs_info
->tree_root
,
1387 &reloc_root
->root_key
, root_item
);
1393 * helper to find first cached inode with inode number >= objectid
1396 static struct inode
*find_next_inode(struct btrfs_root
*root
, u64 objectid
)
1398 struct rb_node
*node
;
1399 struct rb_node
*prev
;
1400 struct btrfs_inode
*entry
;
1401 struct inode
*inode
;
1403 spin_lock(&root
->inode_lock
);
1405 node
= root
->inode_tree
.rb_node
;
1409 entry
= rb_entry(node
, struct btrfs_inode
, rb_node
);
1411 if (objectid
< entry
->vfs_inode
.i_ino
)
1412 node
= node
->rb_left
;
1413 else if (objectid
> entry
->vfs_inode
.i_ino
)
1414 node
= node
->rb_right
;
1420 entry
= rb_entry(prev
, struct btrfs_inode
, rb_node
);
1421 if (objectid
<= entry
->vfs_inode
.i_ino
) {
1425 prev
= rb_next(prev
);
1429 entry
= rb_entry(node
, struct btrfs_inode
, rb_node
);
1430 inode
= igrab(&entry
->vfs_inode
);
1432 spin_unlock(&root
->inode_lock
);
1436 objectid
= entry
->vfs_inode
.i_ino
+ 1;
1437 if (cond_resched_lock(&root
->inode_lock
))
1440 node
= rb_next(node
);
1442 spin_unlock(&root
->inode_lock
);
1446 static int in_block_group(u64 bytenr
,
1447 struct btrfs_block_group_cache
*block_group
)
1449 if (bytenr
>= block_group
->key
.objectid
&&
1450 bytenr
< block_group
->key
.objectid
+ block_group
->key
.offset
)
1456 * get new location of data
1458 static int get_new_location(struct inode
*reloc_inode
, u64
*new_bytenr
,
1459 u64 bytenr
, u64 num_bytes
)
1461 struct btrfs_root
*root
= BTRFS_I(reloc_inode
)->root
;
1462 struct btrfs_path
*path
;
1463 struct btrfs_file_extent_item
*fi
;
1464 struct extent_buffer
*leaf
;
1467 path
= btrfs_alloc_path();
1471 bytenr
-= BTRFS_I(reloc_inode
)->index_cnt
;
1472 ret
= btrfs_lookup_file_extent(NULL
, root
, path
, reloc_inode
->i_ino
,
1481 leaf
= path
->nodes
[0];
1482 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
1483 struct btrfs_file_extent_item
);
1485 BUG_ON(btrfs_file_extent_offset(leaf
, fi
) ||
1486 btrfs_file_extent_compression(leaf
, fi
) ||
1487 btrfs_file_extent_encryption(leaf
, fi
) ||
1488 btrfs_file_extent_other_encoding(leaf
, fi
));
1490 if (num_bytes
!= btrfs_file_extent_disk_num_bytes(leaf
, fi
)) {
1495 *new_bytenr
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
1498 btrfs_free_path(path
);
1503 * update file extent items in the tree leaf to point to
1504 * the new locations.
1506 static noinline_for_stack
1507 int replace_file_extents(struct btrfs_trans_handle
*trans
,
1508 struct reloc_control
*rc
,
1509 struct btrfs_root
*root
,
1510 struct extent_buffer
*leaf
)
1512 struct btrfs_key key
;
1513 struct btrfs_file_extent_item
*fi
;
1514 struct inode
*inode
= NULL
;
1526 if (rc
->stage
!= UPDATE_DATA_PTRS
)
1529 /* reloc trees always use full backref */
1530 if (root
->root_key
.objectid
== BTRFS_TREE_RELOC_OBJECTID
)
1531 parent
= leaf
->start
;
1535 nritems
= btrfs_header_nritems(leaf
);
1536 for (i
= 0; i
< nritems
; i
++) {
1538 btrfs_item_key_to_cpu(leaf
, &key
, i
);
1539 if (key
.type
!= BTRFS_EXTENT_DATA_KEY
)
1541 fi
= btrfs_item_ptr(leaf
, i
, struct btrfs_file_extent_item
);
1542 if (btrfs_file_extent_type(leaf
, fi
) ==
1543 BTRFS_FILE_EXTENT_INLINE
)
1545 bytenr
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
1546 num_bytes
= btrfs_file_extent_disk_num_bytes(leaf
, fi
);
1549 if (!in_block_group(bytenr
, rc
->block_group
))
1553 * if we are modifying block in fs tree, wait for readpage
1554 * to complete and drop the extent cache
1556 if (root
->root_key
.objectid
!= BTRFS_TREE_RELOC_OBJECTID
) {
1558 inode
= find_next_inode(root
, key
.objectid
);
1560 } else if (inode
&& inode
->i_ino
< key
.objectid
) {
1561 btrfs_add_delayed_iput(inode
);
1562 inode
= find_next_inode(root
, key
.objectid
);
1564 if (inode
&& inode
->i_ino
== key
.objectid
) {
1566 btrfs_file_extent_num_bytes(leaf
, fi
);
1567 WARN_ON(!IS_ALIGNED(key
.offset
,
1569 WARN_ON(!IS_ALIGNED(end
, root
->sectorsize
));
1571 ret
= try_lock_extent(&BTRFS_I(inode
)->io_tree
,
1577 btrfs_drop_extent_cache(inode
, key
.offset
, end
,
1579 unlock_extent(&BTRFS_I(inode
)->io_tree
,
1580 key
.offset
, end
, GFP_NOFS
);
1584 ret
= get_new_location(rc
->data_inode
, &new_bytenr
,
1592 btrfs_set_file_extent_disk_bytenr(leaf
, fi
, new_bytenr
);
1595 key
.offset
-= btrfs_file_extent_offset(leaf
, fi
);
1596 ret
= btrfs_inc_extent_ref(trans
, root
, new_bytenr
,
1598 btrfs_header_owner(leaf
),
1599 key
.objectid
, key
.offset
);
1602 ret
= btrfs_free_extent(trans
, root
, bytenr
, num_bytes
,
1603 parent
, btrfs_header_owner(leaf
),
1604 key
.objectid
, key
.offset
);
1608 btrfs_mark_buffer_dirty(leaf
);
1610 btrfs_add_delayed_iput(inode
);
1614 static noinline_for_stack
1615 int memcmp_node_keys(struct extent_buffer
*eb
, int slot
,
1616 struct btrfs_path
*path
, int level
)
1618 struct btrfs_disk_key key1
;
1619 struct btrfs_disk_key key2
;
1620 btrfs_node_key(eb
, &key1
, slot
);
1621 btrfs_node_key(path
->nodes
[level
], &key2
, path
->slots
[level
]);
1622 return memcmp(&key1
, &key2
, sizeof(key1
));
1626 * try to replace tree blocks in fs tree with the new blocks
1627 * in reloc tree. tree blocks haven't been modified since the
1628 * reloc tree was create can be replaced.
1630 * if a block was replaced, level of the block + 1 is returned.
1631 * if no block got replaced, 0 is returned. if there are other
1632 * errors, a negative error number is returned.
1634 static noinline_for_stack
1635 int replace_path(struct btrfs_trans_handle
*trans
,
1636 struct btrfs_root
*dest
, struct btrfs_root
*src
,
1637 struct btrfs_path
*path
, struct btrfs_key
*next_key
,
1638 int lowest_level
, int max_level
)
1640 struct extent_buffer
*eb
;
1641 struct extent_buffer
*parent
;
1642 struct btrfs_key key
;
1654 BUG_ON(src
->root_key
.objectid
!= BTRFS_TREE_RELOC_OBJECTID
);
1655 BUG_ON(dest
->root_key
.objectid
== BTRFS_TREE_RELOC_OBJECTID
);
1657 last_snapshot
= btrfs_root_last_snapshot(&src
->root_item
);
1659 slot
= path
->slots
[lowest_level
];
1660 btrfs_node_key_to_cpu(path
->nodes
[lowest_level
], &key
, slot
);
1662 eb
= btrfs_lock_root_node(dest
);
1663 btrfs_set_lock_blocking(eb
);
1664 level
= btrfs_header_level(eb
);
1666 if (level
< lowest_level
) {
1667 btrfs_tree_unlock(eb
);
1668 free_extent_buffer(eb
);
1673 ret
= btrfs_cow_block(trans
, dest
, eb
, NULL
, 0, &eb
);
1676 btrfs_set_lock_blocking(eb
);
1679 next_key
->objectid
= (u64
)-1;
1680 next_key
->type
= (u8
)-1;
1681 next_key
->offset
= (u64
)-1;
1686 level
= btrfs_header_level(parent
);
1687 BUG_ON(level
< lowest_level
);
1689 ret
= btrfs_bin_search(parent
, &key
, level
, &slot
);
1690 if (ret
&& slot
> 0)
1693 if (next_key
&& slot
+ 1 < btrfs_header_nritems(parent
))
1694 btrfs_node_key_to_cpu(parent
, next_key
, slot
+ 1);
1696 old_bytenr
= btrfs_node_blockptr(parent
, slot
);
1697 blocksize
= btrfs_level_size(dest
, level
- 1);
1698 old_ptr_gen
= btrfs_node_ptr_generation(parent
, slot
);
1700 if (level
<= max_level
) {
1701 eb
= path
->nodes
[level
];
1702 new_bytenr
= btrfs_node_blockptr(eb
,
1703 path
->slots
[level
]);
1704 new_ptr_gen
= btrfs_node_ptr_generation(eb
,
1705 path
->slots
[level
]);
1711 if (new_bytenr
> 0 && new_bytenr
== old_bytenr
) {
1717 if (new_bytenr
== 0 || old_ptr_gen
> last_snapshot
||
1718 memcmp_node_keys(parent
, slot
, path
, level
)) {
1719 if (level
<= lowest_level
) {
1724 eb
= read_tree_block(dest
, old_bytenr
, blocksize
,
1726 btrfs_tree_lock(eb
);
1728 ret
= btrfs_cow_block(trans
, dest
, eb
, parent
,
1732 btrfs_set_lock_blocking(eb
);
1734 btrfs_tree_unlock(parent
);
1735 free_extent_buffer(parent
);
1742 btrfs_tree_unlock(parent
);
1743 free_extent_buffer(parent
);
1748 btrfs_node_key_to_cpu(path
->nodes
[level
], &key
,
1749 path
->slots
[level
]);
1750 btrfs_release_path(src
, path
);
1752 path
->lowest_level
= level
;
1753 ret
= btrfs_search_slot(trans
, src
, &key
, path
, 0, 1);
1754 path
->lowest_level
= 0;
1758 * swap blocks in fs tree and reloc tree.
1760 btrfs_set_node_blockptr(parent
, slot
, new_bytenr
);
1761 btrfs_set_node_ptr_generation(parent
, slot
, new_ptr_gen
);
1762 btrfs_mark_buffer_dirty(parent
);
1764 btrfs_set_node_blockptr(path
->nodes
[level
],
1765 path
->slots
[level
], old_bytenr
);
1766 btrfs_set_node_ptr_generation(path
->nodes
[level
],
1767 path
->slots
[level
], old_ptr_gen
);
1768 btrfs_mark_buffer_dirty(path
->nodes
[level
]);
1770 ret
= btrfs_inc_extent_ref(trans
, src
, old_bytenr
, blocksize
,
1771 path
->nodes
[level
]->start
,
1772 src
->root_key
.objectid
, level
- 1, 0);
1774 ret
= btrfs_inc_extent_ref(trans
, dest
, new_bytenr
, blocksize
,
1775 0, dest
->root_key
.objectid
, level
- 1,
1779 ret
= btrfs_free_extent(trans
, src
, new_bytenr
, blocksize
,
1780 path
->nodes
[level
]->start
,
1781 src
->root_key
.objectid
, level
- 1, 0);
1784 ret
= btrfs_free_extent(trans
, dest
, old_bytenr
, blocksize
,
1785 0, dest
->root_key
.objectid
, level
- 1,
1789 btrfs_unlock_up_safe(path
, 0);
1794 btrfs_tree_unlock(parent
);
1795 free_extent_buffer(parent
);
1800 * helper to find next relocated block in reloc tree
1802 static noinline_for_stack
1803 int walk_up_reloc_tree(struct btrfs_root
*root
, struct btrfs_path
*path
,
1806 struct extent_buffer
*eb
;
1811 last_snapshot
= btrfs_root_last_snapshot(&root
->root_item
);
1813 for (i
= 0; i
< *level
; i
++) {
1814 free_extent_buffer(path
->nodes
[i
]);
1815 path
->nodes
[i
] = NULL
;
1818 for (i
= *level
; i
< BTRFS_MAX_LEVEL
&& path
->nodes
[i
]; i
++) {
1819 eb
= path
->nodes
[i
];
1820 nritems
= btrfs_header_nritems(eb
);
1821 while (path
->slots
[i
] + 1 < nritems
) {
1823 if (btrfs_node_ptr_generation(eb
, path
->slots
[i
]) <=
1830 free_extent_buffer(path
->nodes
[i
]);
1831 path
->nodes
[i
] = NULL
;
1837 * walk down reloc tree to find relocated block of lowest level
1839 static noinline_for_stack
1840 int walk_down_reloc_tree(struct btrfs_root
*root
, struct btrfs_path
*path
,
1843 struct extent_buffer
*eb
= NULL
;
1851 last_snapshot
= btrfs_root_last_snapshot(&root
->root_item
);
1853 for (i
= *level
; i
> 0; i
--) {
1854 eb
= path
->nodes
[i
];
1855 nritems
= btrfs_header_nritems(eb
);
1856 while (path
->slots
[i
] < nritems
) {
1857 ptr_gen
= btrfs_node_ptr_generation(eb
, path
->slots
[i
]);
1858 if (ptr_gen
> last_snapshot
)
1862 if (path
->slots
[i
] >= nritems
) {
1873 bytenr
= btrfs_node_blockptr(eb
, path
->slots
[i
]);
1874 blocksize
= btrfs_level_size(root
, i
- 1);
1875 eb
= read_tree_block(root
, bytenr
, blocksize
, ptr_gen
);
1876 BUG_ON(btrfs_header_level(eb
) != i
- 1);
1877 path
->nodes
[i
- 1] = eb
;
1878 path
->slots
[i
- 1] = 0;
1884 * invalidate extent cache for file extents whose key in range of
1885 * [min_key, max_key)
1887 static int invalidate_extent_cache(struct btrfs_root
*root
,
1888 struct btrfs_key
*min_key
,
1889 struct btrfs_key
*max_key
)
1891 struct inode
*inode
= NULL
;
1895 objectid
= min_key
->objectid
;
1900 if (objectid
> max_key
->objectid
)
1903 inode
= find_next_inode(root
, objectid
);
1907 if (inode
->i_ino
> max_key
->objectid
) {
1912 objectid
= inode
->i_ino
+ 1;
1913 if (!S_ISREG(inode
->i_mode
))
1916 if (unlikely(min_key
->objectid
== inode
->i_ino
)) {
1917 if (min_key
->type
> BTRFS_EXTENT_DATA_KEY
)
1919 if (min_key
->type
< BTRFS_EXTENT_DATA_KEY
)
1922 start
= min_key
->offset
;
1923 WARN_ON(!IS_ALIGNED(start
, root
->sectorsize
));
1929 if (unlikely(max_key
->objectid
== inode
->i_ino
)) {
1930 if (max_key
->type
< BTRFS_EXTENT_DATA_KEY
)
1932 if (max_key
->type
> BTRFS_EXTENT_DATA_KEY
) {
1935 if (max_key
->offset
== 0)
1937 end
= max_key
->offset
;
1938 WARN_ON(!IS_ALIGNED(end
, root
->sectorsize
));
1945 /* the lock_extent waits for readpage to complete */
1946 lock_extent(&BTRFS_I(inode
)->io_tree
, start
, end
, GFP_NOFS
);
1947 btrfs_drop_extent_cache(inode
, start
, end
, 1);
1948 unlock_extent(&BTRFS_I(inode
)->io_tree
, start
, end
, GFP_NOFS
);
1953 static int find_next_key(struct btrfs_path
*path
, int level
,
1954 struct btrfs_key
*key
)
1957 while (level
< BTRFS_MAX_LEVEL
) {
1958 if (!path
->nodes
[level
])
1960 if (path
->slots
[level
] + 1 <
1961 btrfs_header_nritems(path
->nodes
[level
])) {
1962 btrfs_node_key_to_cpu(path
->nodes
[level
], key
,
1963 path
->slots
[level
] + 1);
1972 * merge the relocated tree blocks in reloc tree with corresponding
1975 static noinline_for_stack
int merge_reloc_root(struct reloc_control
*rc
,
1976 struct btrfs_root
*root
)
1978 LIST_HEAD(inode_list
);
1979 struct btrfs_key key
;
1980 struct btrfs_key next_key
;
1981 struct btrfs_trans_handle
*trans
;
1982 struct btrfs_root
*reloc_root
;
1983 struct btrfs_root_item
*root_item
;
1984 struct btrfs_path
*path
;
1985 struct extent_buffer
*leaf
;
1994 path
= btrfs_alloc_path();
1998 reloc_root
= root
->reloc_root
;
1999 root_item
= &reloc_root
->root_item
;
2001 if (btrfs_disk_key_objectid(&root_item
->drop_progress
) == 0) {
2002 level
= btrfs_root_level(root_item
);
2003 extent_buffer_get(reloc_root
->node
);
2004 path
->nodes
[level
] = reloc_root
->node
;
2005 path
->slots
[level
] = 0;
2007 btrfs_disk_key_to_cpu(&key
, &root_item
->drop_progress
);
2009 level
= root_item
->drop_level
;
2011 path
->lowest_level
= level
;
2012 ret
= btrfs_search_slot(NULL
, reloc_root
, &key
, path
, 0, 0);
2013 path
->lowest_level
= 0;
2015 btrfs_free_path(path
);
2019 btrfs_node_key_to_cpu(path
->nodes
[level
], &next_key
,
2020 path
->slots
[level
]);
2021 WARN_ON(memcmp(&key
, &next_key
, sizeof(key
)));
2023 btrfs_unlock_up_safe(path
, 0);
2026 min_reserved
= root
->nodesize
* (BTRFS_MAX_LEVEL
- 1) * 2;
2027 memset(&next_key
, 0, sizeof(next_key
));
2030 trans
= btrfs_start_transaction(root
, 0);
2031 trans
->block_rsv
= rc
->block_rsv
;
2033 ret
= btrfs_block_rsv_check(trans
, root
, rc
->block_rsv
,
2036 BUG_ON(ret
!= -EAGAIN
);
2037 ret
= btrfs_commit_transaction(trans
, root
);
2045 ret
= walk_down_reloc_tree(reloc_root
, path
, &level
);
2053 if (!find_next_key(path
, level
, &key
) &&
2054 btrfs_comp_cpu_keys(&next_key
, &key
) >= 0) {
2057 ret
= replace_path(trans
, root
, reloc_root
, path
,
2058 &next_key
, level
, max_level
);
2067 btrfs_node_key_to_cpu(path
->nodes
[level
], &key
,
2068 path
->slots
[level
]);
2072 ret
= walk_up_reloc_tree(reloc_root
, path
, &level
);
2078 * save the merging progress in the drop_progress.
2079 * this is OK since root refs == 1 in this case.
2081 btrfs_node_key(path
->nodes
[level
], &root_item
->drop_progress
,
2082 path
->slots
[level
]);
2083 root_item
->drop_level
= level
;
2085 nr
= trans
->blocks_used
;
2086 btrfs_end_transaction_throttle(trans
, root
);
2088 btrfs_btree_balance_dirty(root
, nr
);
2090 if (replaced
&& rc
->stage
== UPDATE_DATA_PTRS
)
2091 invalidate_extent_cache(root
, &key
, &next_key
);
2095 * handle the case only one block in the fs tree need to be
2096 * relocated and the block is tree root.
2098 leaf
= btrfs_lock_root_node(root
);
2099 ret
= btrfs_cow_block(trans
, root
, leaf
, NULL
, 0, &leaf
);
2100 btrfs_tree_unlock(leaf
);
2101 free_extent_buffer(leaf
);
2105 btrfs_free_path(path
);
2108 memset(&root_item
->drop_progress
, 0,
2109 sizeof(root_item
->drop_progress
));
2110 root_item
->drop_level
= 0;
2111 btrfs_set_root_refs(root_item
, 0);
2112 btrfs_update_reloc_root(trans
, root
);
2115 nr
= trans
->blocks_used
;
2116 btrfs_end_transaction_throttle(trans
, root
);
2118 btrfs_btree_balance_dirty(root
, nr
);
2120 if (replaced
&& rc
->stage
== UPDATE_DATA_PTRS
)
2121 invalidate_extent_cache(root
, &key
, &next_key
);
2126 static noinline_for_stack
2127 int prepare_to_merge(struct reloc_control
*rc
, int err
)
2129 struct btrfs_root
*root
= rc
->extent_root
;
2130 struct btrfs_root
*reloc_root
;
2131 struct btrfs_trans_handle
*trans
;
2132 LIST_HEAD(reloc_roots
);
2137 mutex_lock(&root
->fs_info
->trans_mutex
);
2138 rc
->merging_rsv_size
+= root
->nodesize
* (BTRFS_MAX_LEVEL
- 1) * 2;
2139 rc
->merging_rsv_size
+= rc
->nodes_relocated
* 2;
2140 mutex_unlock(&root
->fs_info
->trans_mutex
);
2143 num_bytes
= rc
->merging_rsv_size
;
2144 ret
= btrfs_block_rsv_add(NULL
, root
, rc
->block_rsv
,
2145 num_bytes
, &retries
);
2150 trans
= btrfs_join_transaction(rc
->extent_root
, 1);
2153 if (num_bytes
!= rc
->merging_rsv_size
) {
2154 btrfs_end_transaction(trans
, rc
->extent_root
);
2155 btrfs_block_rsv_release(rc
->extent_root
,
2156 rc
->block_rsv
, num_bytes
);
2162 rc
->merge_reloc_tree
= 1;
2164 while (!list_empty(&rc
->reloc_roots
)) {
2165 reloc_root
= list_entry(rc
->reloc_roots
.next
,
2166 struct btrfs_root
, root_list
);
2167 list_del_init(&reloc_root
->root_list
);
2169 root
= read_fs_root(reloc_root
->fs_info
,
2170 reloc_root
->root_key
.offset
);
2171 BUG_ON(IS_ERR(root
));
2172 BUG_ON(root
->reloc_root
!= reloc_root
);
2175 * set reference count to 1, so btrfs_recover_relocation
2176 * knows it should resumes merging
2179 btrfs_set_root_refs(&reloc_root
->root_item
, 1);
2180 btrfs_update_reloc_root(trans
, root
);
2182 list_add(&reloc_root
->root_list
, &reloc_roots
);
2185 list_splice(&reloc_roots
, &rc
->reloc_roots
);
2188 btrfs_commit_transaction(trans
, rc
->extent_root
);
2190 btrfs_end_transaction(trans
, rc
->extent_root
);
2194 static noinline_for_stack
2195 int merge_reloc_roots(struct reloc_control
*rc
)
2197 struct btrfs_root
*root
;
2198 struct btrfs_root
*reloc_root
;
2199 LIST_HEAD(reloc_roots
);
2203 root
= rc
->extent_root
;
2204 mutex_lock(&root
->fs_info
->trans_mutex
);
2205 list_splice_init(&rc
->reloc_roots
, &reloc_roots
);
2206 mutex_unlock(&root
->fs_info
->trans_mutex
);
2208 while (!list_empty(&reloc_roots
)) {
2210 reloc_root
= list_entry(reloc_roots
.next
,
2211 struct btrfs_root
, root_list
);
2213 if (btrfs_root_refs(&reloc_root
->root_item
) > 0) {
2214 root
= read_fs_root(reloc_root
->fs_info
,
2215 reloc_root
->root_key
.offset
);
2216 BUG_ON(IS_ERR(root
));
2217 BUG_ON(root
->reloc_root
!= reloc_root
);
2219 ret
= merge_reloc_root(rc
, root
);
2222 list_del_init(&reloc_root
->root_list
);
2224 btrfs_drop_snapshot(reloc_root
, rc
->block_rsv
, 0);
2231 BUG_ON(!RB_EMPTY_ROOT(&rc
->reloc_root_tree
.rb_root
));
2235 static void free_block_list(struct rb_root
*blocks
)
2237 struct tree_block
*block
;
2238 struct rb_node
*rb_node
;
2239 while ((rb_node
= rb_first(blocks
))) {
2240 block
= rb_entry(rb_node
, struct tree_block
, rb_node
);
2241 rb_erase(rb_node
, blocks
);
2246 static int record_reloc_root_in_trans(struct btrfs_trans_handle
*trans
,
2247 struct btrfs_root
*reloc_root
)
2249 struct btrfs_root
*root
;
2251 if (reloc_root
->last_trans
== trans
->transid
)
2254 root
= read_fs_root(reloc_root
->fs_info
, reloc_root
->root_key
.offset
);
2255 BUG_ON(IS_ERR(root
));
2256 BUG_ON(root
->reloc_root
!= reloc_root
);
2258 return btrfs_record_root_in_trans(trans
, root
);
2261 static noinline_for_stack
2262 struct btrfs_root
*select_reloc_root(struct btrfs_trans_handle
*trans
,
2263 struct reloc_control
*rc
,
2264 struct backref_node
*node
,
2265 struct backref_edge
*edges
[], int *nr
)
2267 struct backref_node
*next
;
2268 struct btrfs_root
*root
;
2274 next
= walk_up_backref(next
, edges
, &index
);
2277 BUG_ON(!root
->ref_cows
);
2279 if (root
->root_key
.objectid
== BTRFS_TREE_RELOC_OBJECTID
) {
2280 record_reloc_root_in_trans(trans
, root
);
2284 btrfs_record_root_in_trans(trans
, root
);
2285 root
= root
->reloc_root
;
2287 if (next
->new_bytenr
!= root
->node
->start
) {
2288 BUG_ON(next
->new_bytenr
);
2289 BUG_ON(!list_empty(&next
->list
));
2290 next
->new_bytenr
= root
->node
->start
;
2292 list_add_tail(&next
->list
,
2293 &rc
->backref_cache
.changed
);
2294 __mark_block_processed(rc
, next
);
2300 next
= walk_down_backref(edges
, &index
);
2301 if (!next
|| next
->level
<= node
->level
)
2309 /* setup backref node path for btrfs_reloc_cow_block */
2311 rc
->backref_cache
.path
[next
->level
] = next
;
2314 next
= edges
[index
]->node
[UPPER
];
2320 * select a tree root for relocation. return NULL if the block
2321 * is reference counted. we should use do_relocation() in this
2322 * case. return a tree root pointer if the block isn't reference
2323 * counted. return -ENOENT if the block is root of reloc tree.
2325 static noinline_for_stack
2326 struct btrfs_root
*select_one_root(struct btrfs_trans_handle
*trans
,
2327 struct backref_node
*node
)
2329 struct backref_node
*next
;
2330 struct btrfs_root
*root
;
2331 struct btrfs_root
*fs_root
= NULL
;
2332 struct backref_edge
*edges
[BTRFS_MAX_LEVEL
- 1];
2338 next
= walk_up_backref(next
, edges
, &index
);
2342 /* no other choice for non-refernce counted tree */
2343 if (!root
->ref_cows
)
2346 if (root
->root_key
.objectid
!= BTRFS_TREE_RELOC_OBJECTID
)
2352 next
= walk_down_backref(edges
, &index
);
2353 if (!next
|| next
->level
<= node
->level
)
2358 return ERR_PTR(-ENOENT
);
2362 static noinline_for_stack
2363 u64
calcu_metadata_size(struct reloc_control
*rc
,
2364 struct backref_node
*node
, int reserve
)
2366 struct backref_node
*next
= node
;
2367 struct backref_edge
*edge
;
2368 struct backref_edge
*edges
[BTRFS_MAX_LEVEL
- 1];
2372 BUG_ON(reserve
&& node
->processed
);
2377 if (next
->processed
&& (reserve
|| next
!= node
))
2380 num_bytes
+= btrfs_level_size(rc
->extent_root
,
2383 if (list_empty(&next
->upper
))
2386 edge
= list_entry(next
->upper
.next
,
2387 struct backref_edge
, list
[LOWER
]);
2388 edges
[index
++] = edge
;
2389 next
= edge
->node
[UPPER
];
2391 next
= walk_down_backref(edges
, &index
);
2396 static int reserve_metadata_space(struct btrfs_trans_handle
*trans
,
2397 struct reloc_control
*rc
,
2398 struct backref_node
*node
)
2400 struct btrfs_root
*root
= rc
->extent_root
;
2404 num_bytes
= calcu_metadata_size(rc
, node
, 1) * 2;
2406 trans
->block_rsv
= rc
->block_rsv
;
2407 ret
= btrfs_block_rsv_add(trans
, root
, rc
->block_rsv
, num_bytes
,
2408 &rc
->block_rsv_retries
);
2411 rc
->commit_transaction
= 1;
2415 rc
->block_rsv_retries
= 0;
2419 static void release_metadata_space(struct reloc_control
*rc
,
2420 struct backref_node
*node
)
2422 u64 num_bytes
= calcu_metadata_size(rc
, node
, 0) * 2;
2423 btrfs_block_rsv_release(rc
->extent_root
, rc
->block_rsv
, num_bytes
);
2427 * relocate a block tree, and then update pointers in upper level
2428 * blocks that reference the block to point to the new location.
2430 * if called by link_to_upper, the block has already been relocated.
2431 * in that case this function just updates pointers.
2433 static int do_relocation(struct btrfs_trans_handle
*trans
,
2434 struct reloc_control
*rc
,
2435 struct backref_node
*node
,
2436 struct btrfs_key
*key
,
2437 struct btrfs_path
*path
, int lowest
)
2439 struct backref_node
*upper
;
2440 struct backref_edge
*edge
;
2441 struct backref_edge
*edges
[BTRFS_MAX_LEVEL
- 1];
2442 struct btrfs_root
*root
;
2443 struct extent_buffer
*eb
;
2452 BUG_ON(lowest
&& node
->eb
);
2454 path
->lowest_level
= node
->level
+ 1;
2455 rc
->backref_cache
.path
[node
->level
] = node
;
2456 list_for_each_entry(edge
, &node
->upper
, list
[LOWER
]) {
2459 upper
= edge
->node
[UPPER
];
2460 root
= select_reloc_root(trans
, rc
, upper
, edges
, &nr
);
2463 if (upper
->eb
&& !upper
->locked
) {
2465 ret
= btrfs_bin_search(upper
->eb
, key
,
2466 upper
->level
, &slot
);
2468 bytenr
= btrfs_node_blockptr(upper
->eb
, slot
);
2469 if (node
->eb
->start
== bytenr
)
2472 drop_node_buffer(upper
);
2476 ret
= btrfs_search_slot(trans
, root
, key
, path
, 0, 1);
2484 upper
->eb
= path
->nodes
[upper
->level
];
2485 path
->nodes
[upper
->level
] = NULL
;
2487 BUG_ON(upper
->eb
!= path
->nodes
[upper
->level
]);
2491 path
->locks
[upper
->level
] = 0;
2493 slot
= path
->slots
[upper
->level
];
2494 btrfs_release_path(NULL
, path
);
2496 ret
= btrfs_bin_search(upper
->eb
, key
, upper
->level
,
2501 bytenr
= btrfs_node_blockptr(upper
->eb
, slot
);
2503 BUG_ON(bytenr
!= node
->bytenr
);
2505 if (node
->eb
->start
== bytenr
)
2509 blocksize
= btrfs_level_size(root
, node
->level
);
2510 generation
= btrfs_node_ptr_generation(upper
->eb
, slot
);
2511 eb
= read_tree_block(root
, bytenr
, blocksize
, generation
);
2512 btrfs_tree_lock(eb
);
2513 btrfs_set_lock_blocking(eb
);
2516 ret
= btrfs_cow_block(trans
, root
, eb
, upper
->eb
,
2518 btrfs_tree_unlock(eb
);
2519 free_extent_buffer(eb
);
2524 BUG_ON(node
->eb
!= eb
);
2526 btrfs_set_node_blockptr(upper
->eb
, slot
,
2528 btrfs_set_node_ptr_generation(upper
->eb
, slot
,
2530 btrfs_mark_buffer_dirty(upper
->eb
);
2532 ret
= btrfs_inc_extent_ref(trans
, root
,
2533 node
->eb
->start
, blocksize
,
2535 btrfs_header_owner(upper
->eb
),
2539 ret
= btrfs_drop_subtree(trans
, root
, eb
, upper
->eb
);
2543 if (!upper
->pending
)
2544 drop_node_buffer(upper
);
2546 unlock_node_buffer(upper
);
2551 if (!err
&& node
->pending
) {
2552 drop_node_buffer(node
);
2553 list_move_tail(&node
->list
, &rc
->backref_cache
.changed
);
2557 path
->lowest_level
= 0;
2558 BUG_ON(err
== -ENOSPC
);
2562 static int link_to_upper(struct btrfs_trans_handle
*trans
,
2563 struct reloc_control
*rc
,
2564 struct backref_node
*node
,
2565 struct btrfs_path
*path
)
2567 struct btrfs_key key
;
2569 btrfs_node_key_to_cpu(node
->eb
, &key
, 0);
2570 return do_relocation(trans
, rc
, node
, &key
, path
, 0);
2573 static int finish_pending_nodes(struct btrfs_trans_handle
*trans
,
2574 struct reloc_control
*rc
,
2575 struct btrfs_path
*path
, int err
)
2578 struct backref_cache
*cache
= &rc
->backref_cache
;
2579 struct backref_node
*node
;
2583 for (level
= 0; level
< BTRFS_MAX_LEVEL
; level
++) {
2584 while (!list_empty(&cache
->pending
[level
])) {
2585 node
= list_entry(cache
->pending
[level
].next
,
2586 struct backref_node
, list
);
2587 list_move_tail(&node
->list
, &list
);
2588 BUG_ON(!node
->pending
);
2591 ret
= link_to_upper(trans
, rc
, node
, path
);
2596 list_splice_init(&list
, &cache
->pending
[level
]);
2601 static void mark_block_processed(struct reloc_control
*rc
,
2602 u64 bytenr
, u32 blocksize
)
2604 set_extent_bits(&rc
->processed_blocks
, bytenr
, bytenr
+ blocksize
- 1,
2605 EXTENT_DIRTY
, GFP_NOFS
);
2608 static void __mark_block_processed(struct reloc_control
*rc
,
2609 struct backref_node
*node
)
2612 if (node
->level
== 0 ||
2613 in_block_group(node
->bytenr
, rc
->block_group
)) {
2614 blocksize
= btrfs_level_size(rc
->extent_root
, node
->level
);
2615 mark_block_processed(rc
, node
->bytenr
, blocksize
);
2617 node
->processed
= 1;
2621 * mark a block and all blocks directly/indirectly reference the block
2624 static void update_processed_blocks(struct reloc_control
*rc
,
2625 struct backref_node
*node
)
2627 struct backref_node
*next
= node
;
2628 struct backref_edge
*edge
;
2629 struct backref_edge
*edges
[BTRFS_MAX_LEVEL
- 1];
2635 if (next
->processed
)
2638 __mark_block_processed(rc
, next
);
2640 if (list_empty(&next
->upper
))
2643 edge
= list_entry(next
->upper
.next
,
2644 struct backref_edge
, list
[LOWER
]);
2645 edges
[index
++] = edge
;
2646 next
= edge
->node
[UPPER
];
2648 next
= walk_down_backref(edges
, &index
);
2652 static int tree_block_processed(u64 bytenr
, u32 blocksize
,
2653 struct reloc_control
*rc
)
2655 if (test_range_bit(&rc
->processed_blocks
, bytenr
,
2656 bytenr
+ blocksize
- 1, EXTENT_DIRTY
, 1, NULL
))
2661 static int get_tree_block_key(struct reloc_control
*rc
,
2662 struct tree_block
*block
)
2664 struct extent_buffer
*eb
;
2666 BUG_ON(block
->key_ready
);
2667 eb
= read_tree_block(rc
->extent_root
, block
->bytenr
,
2668 block
->key
.objectid
, block
->key
.offset
);
2669 WARN_ON(btrfs_header_level(eb
) != block
->level
);
2670 if (block
->level
== 0)
2671 btrfs_item_key_to_cpu(eb
, &block
->key
, 0);
2673 btrfs_node_key_to_cpu(eb
, &block
->key
, 0);
2674 free_extent_buffer(eb
);
2675 block
->key_ready
= 1;
2679 static int reada_tree_block(struct reloc_control
*rc
,
2680 struct tree_block
*block
)
2682 BUG_ON(block
->key_ready
);
2683 readahead_tree_block(rc
->extent_root
, block
->bytenr
,
2684 block
->key
.objectid
, block
->key
.offset
);
2689 * helper function to relocate a tree block
2691 static int relocate_tree_block(struct btrfs_trans_handle
*trans
,
2692 struct reloc_control
*rc
,
2693 struct backref_node
*node
,
2694 struct btrfs_key
*key
,
2695 struct btrfs_path
*path
)
2697 struct btrfs_root
*root
;
2704 BUG_ON(node
->processed
);
2705 root
= select_one_root(trans
, node
);
2706 if (root
== ERR_PTR(-ENOENT
)) {
2707 update_processed_blocks(rc
, node
);
2711 if (!root
|| root
->ref_cows
) {
2712 ret
= reserve_metadata_space(trans
, rc
, node
);
2719 if (root
->ref_cows
) {
2720 BUG_ON(node
->new_bytenr
);
2721 BUG_ON(!list_empty(&node
->list
));
2722 btrfs_record_root_in_trans(trans
, root
);
2723 root
= root
->reloc_root
;
2724 node
->new_bytenr
= root
->node
->start
;
2726 list_add_tail(&node
->list
, &rc
->backref_cache
.changed
);
2728 path
->lowest_level
= node
->level
;
2729 ret
= btrfs_search_slot(trans
, root
, key
, path
, 0, 1);
2730 btrfs_release_path(root
, path
);
2735 update_processed_blocks(rc
, node
);
2737 ret
= do_relocation(trans
, rc
, node
, key
, path
, 1);
2740 if (ret
|| node
->level
== 0 || node
->cowonly
) {
2742 release_metadata_space(rc
, node
);
2743 remove_backref_node(&rc
->backref_cache
, node
);
2749 * relocate a list of blocks
2751 static noinline_for_stack
2752 int relocate_tree_blocks(struct btrfs_trans_handle
*trans
,
2753 struct reloc_control
*rc
, struct rb_root
*blocks
)
2755 struct backref_node
*node
;
2756 struct btrfs_path
*path
;
2757 struct tree_block
*block
;
2758 struct rb_node
*rb_node
;
2762 path
= btrfs_alloc_path();
2766 rb_node
= rb_first(blocks
);
2768 block
= rb_entry(rb_node
, struct tree_block
, rb_node
);
2769 if (!block
->key_ready
)
2770 reada_tree_block(rc
, block
);
2771 rb_node
= rb_next(rb_node
);
2774 rb_node
= rb_first(blocks
);
2776 block
= rb_entry(rb_node
, struct tree_block
, rb_node
);
2777 if (!block
->key_ready
)
2778 get_tree_block_key(rc
, block
);
2779 rb_node
= rb_next(rb_node
);
2782 rb_node
= rb_first(blocks
);
2784 block
= rb_entry(rb_node
, struct tree_block
, rb_node
);
2786 node
= build_backref_tree(rc
, &block
->key
,
2787 block
->level
, block
->bytenr
);
2789 err
= PTR_ERR(node
);
2793 ret
= relocate_tree_block(trans
, rc
, node
, &block
->key
,
2796 if (ret
!= -EAGAIN
|| rb_node
== rb_first(blocks
))
2800 rb_node
= rb_next(rb_node
);
2803 free_block_list(blocks
);
2804 err
= finish_pending_nodes(trans
, rc
, path
, err
);
2806 btrfs_free_path(path
);
2810 static noinline_for_stack
2811 int prealloc_file_extent_cluster(struct inode
*inode
,
2812 struct file_extent_cluster
*cluster
)
2817 u64 offset
= BTRFS_I(inode
)->index_cnt
;
2822 BUG_ON(cluster
->start
!= cluster
->boundary
[0]);
2823 mutex_lock(&inode
->i_mutex
);
2825 ret
= btrfs_check_data_free_space(inode
, cluster
->end
+
2826 1 - cluster
->start
);
2830 while (nr
< cluster
->nr
) {
2831 start
= cluster
->boundary
[nr
] - offset
;
2832 if (nr
+ 1 < cluster
->nr
)
2833 end
= cluster
->boundary
[nr
+ 1] - 1 - offset
;
2835 end
= cluster
->end
- offset
;
2837 lock_extent(&BTRFS_I(inode
)->io_tree
, start
, end
, GFP_NOFS
);
2838 num_bytes
= end
+ 1 - start
;
2839 ret
= btrfs_prealloc_file_range(inode
, 0, start
,
2840 num_bytes
, num_bytes
,
2841 end
+ 1, &alloc_hint
);
2842 unlock_extent(&BTRFS_I(inode
)->io_tree
, start
, end
, GFP_NOFS
);
2847 btrfs_free_reserved_data_space(inode
, cluster
->end
+
2848 1 - cluster
->start
);
2850 mutex_unlock(&inode
->i_mutex
);
2854 static noinline_for_stack
2855 int setup_extent_mapping(struct inode
*inode
, u64 start
, u64 end
,
2858 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2859 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
2860 struct extent_map
*em
;
2863 em
= alloc_extent_map(GFP_NOFS
);
2868 em
->len
= end
+ 1 - start
;
2869 em
->block_len
= em
->len
;
2870 em
->block_start
= block_start
;
2871 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
2872 set_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
2874 lock_extent(&BTRFS_I(inode
)->io_tree
, start
, end
, GFP_NOFS
);
2876 write_lock(&em_tree
->lock
);
2877 ret
= add_extent_mapping(em_tree
, em
);
2878 write_unlock(&em_tree
->lock
);
2879 if (ret
!= -EEXIST
) {
2880 free_extent_map(em
);
2883 btrfs_drop_extent_cache(inode
, start
, end
, 0);
2885 unlock_extent(&BTRFS_I(inode
)->io_tree
, start
, end
, GFP_NOFS
);
2889 static int relocate_file_extent_cluster(struct inode
*inode
,
2890 struct file_extent_cluster
*cluster
)
2894 u64 offset
= BTRFS_I(inode
)->index_cnt
;
2895 unsigned long index
;
2896 unsigned long last_index
;
2898 struct file_ra_state
*ra
;
2905 ra
= kzalloc(sizeof(*ra
), GFP_NOFS
);
2909 ret
= prealloc_file_extent_cluster(inode
, cluster
);
2913 file_ra_state_init(ra
, inode
->i_mapping
);
2915 ret
= setup_extent_mapping(inode
, cluster
->start
- offset
,
2916 cluster
->end
- offset
, cluster
->start
);
2920 index
= (cluster
->start
- offset
) >> PAGE_CACHE_SHIFT
;
2921 last_index
= (cluster
->end
- offset
) >> PAGE_CACHE_SHIFT
;
2922 while (index
<= last_index
) {
2923 ret
= btrfs_delalloc_reserve_metadata(inode
, PAGE_CACHE_SIZE
);
2927 page
= find_lock_page(inode
->i_mapping
, index
);
2929 page_cache_sync_readahead(inode
->i_mapping
,
2931 last_index
+ 1 - index
);
2932 page
= grab_cache_page(inode
->i_mapping
, index
);
2934 btrfs_delalloc_release_metadata(inode
,
2941 if (PageReadahead(page
)) {
2942 page_cache_async_readahead(inode
->i_mapping
,
2943 ra
, NULL
, page
, index
,
2944 last_index
+ 1 - index
);
2947 if (!PageUptodate(page
)) {
2948 btrfs_readpage(NULL
, page
);
2950 if (!PageUptodate(page
)) {
2952 page_cache_release(page
);
2953 btrfs_delalloc_release_metadata(inode
,
2960 page_start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
2961 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
2963 lock_extent(&BTRFS_I(inode
)->io_tree
,
2964 page_start
, page_end
, GFP_NOFS
);
2966 set_page_extent_mapped(page
);
2968 if (nr
< cluster
->nr
&&
2969 page_start
+ offset
== cluster
->boundary
[nr
]) {
2970 set_extent_bits(&BTRFS_I(inode
)->io_tree
,
2971 page_start
, page_end
,
2972 EXTENT_BOUNDARY
, GFP_NOFS
);
2976 btrfs_set_extent_delalloc(inode
, page_start
, page_end
, NULL
);
2977 set_page_dirty(page
);
2979 unlock_extent(&BTRFS_I(inode
)->io_tree
,
2980 page_start
, page_end
, GFP_NOFS
);
2982 page_cache_release(page
);
2985 balance_dirty_pages_ratelimited(inode
->i_mapping
);
2986 btrfs_throttle(BTRFS_I(inode
)->root
);
2988 WARN_ON(nr
!= cluster
->nr
);
2994 static noinline_for_stack
2995 int relocate_data_extent(struct inode
*inode
, struct btrfs_key
*extent_key
,
2996 struct file_extent_cluster
*cluster
)
3000 if (cluster
->nr
> 0 && extent_key
->objectid
!= cluster
->end
+ 1) {
3001 ret
= relocate_file_extent_cluster(inode
, cluster
);
3008 cluster
->start
= extent_key
->objectid
;
3010 BUG_ON(cluster
->nr
>= MAX_EXTENTS
);
3011 cluster
->end
= extent_key
->objectid
+ extent_key
->offset
- 1;
3012 cluster
->boundary
[cluster
->nr
] = extent_key
->objectid
;
3015 if (cluster
->nr
>= MAX_EXTENTS
) {
3016 ret
= relocate_file_extent_cluster(inode
, cluster
);
3024 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3025 static int get_ref_objectid_v0(struct reloc_control
*rc
,
3026 struct btrfs_path
*path
,
3027 struct btrfs_key
*extent_key
,
3028 u64
*ref_objectid
, int *path_change
)
3030 struct btrfs_key key
;
3031 struct extent_buffer
*leaf
;
3032 struct btrfs_extent_ref_v0
*ref0
;
3036 leaf
= path
->nodes
[0];
3037 slot
= path
->slots
[0];
3039 if (slot
>= btrfs_header_nritems(leaf
)) {
3040 ret
= btrfs_next_leaf(rc
->extent_root
, path
);
3044 leaf
= path
->nodes
[0];
3045 slot
= path
->slots
[0];
3049 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
3050 if (key
.objectid
!= extent_key
->objectid
)
3053 if (key
.type
!= BTRFS_EXTENT_REF_V0_KEY
) {
3057 ref0
= btrfs_item_ptr(leaf
, slot
,
3058 struct btrfs_extent_ref_v0
);
3059 *ref_objectid
= btrfs_ref_objectid_v0(leaf
, ref0
);
3067 * helper to add a tree block to the list.
3068 * the major work is getting the generation and level of the block
3070 static int add_tree_block(struct reloc_control
*rc
,
3071 struct btrfs_key
*extent_key
,
3072 struct btrfs_path
*path
,
3073 struct rb_root
*blocks
)
3075 struct extent_buffer
*eb
;
3076 struct btrfs_extent_item
*ei
;
3077 struct btrfs_tree_block_info
*bi
;
3078 struct tree_block
*block
;
3079 struct rb_node
*rb_node
;
3084 eb
= path
->nodes
[0];
3085 item_size
= btrfs_item_size_nr(eb
, path
->slots
[0]);
3087 if (item_size
>= sizeof(*ei
) + sizeof(*bi
)) {
3088 ei
= btrfs_item_ptr(eb
, path
->slots
[0],
3089 struct btrfs_extent_item
);
3090 bi
= (struct btrfs_tree_block_info
*)(ei
+ 1);
3091 generation
= btrfs_extent_generation(eb
, ei
);
3092 level
= btrfs_tree_block_level(eb
, bi
);
3094 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3098 BUG_ON(item_size
!= sizeof(struct btrfs_extent_item_v0
));
3099 ret
= get_ref_objectid_v0(rc
, path
, extent_key
,
3101 BUG_ON(ref_owner
>= BTRFS_MAX_LEVEL
);
3102 level
= (int)ref_owner
;
3103 /* FIXME: get real generation */
3110 btrfs_release_path(rc
->extent_root
, path
);
3112 BUG_ON(level
== -1);
3114 block
= kmalloc(sizeof(*block
), GFP_NOFS
);
3118 block
->bytenr
= extent_key
->objectid
;
3119 block
->key
.objectid
= extent_key
->offset
;
3120 block
->key
.offset
= generation
;
3121 block
->level
= level
;
3122 block
->key_ready
= 0;
3124 rb_node
= tree_insert(blocks
, block
->bytenr
, &block
->rb_node
);
3131 * helper to add tree blocks for backref of type BTRFS_SHARED_DATA_REF_KEY
3133 static int __add_tree_block(struct reloc_control
*rc
,
3134 u64 bytenr
, u32 blocksize
,
3135 struct rb_root
*blocks
)
3137 struct btrfs_path
*path
;
3138 struct btrfs_key key
;
3141 if (tree_block_processed(bytenr
, blocksize
, rc
))
3144 if (tree_search(blocks
, bytenr
))
3147 path
= btrfs_alloc_path();
3151 key
.objectid
= bytenr
;
3152 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
3153 key
.offset
= blocksize
;
3155 path
->search_commit_root
= 1;
3156 path
->skip_locking
= 1;
3157 ret
= btrfs_search_slot(NULL
, rc
->extent_root
, &key
, path
, 0, 0);
3162 btrfs_item_key_to_cpu(path
->nodes
[0], &key
, path
->slots
[0]);
3163 ret
= add_tree_block(rc
, &key
, path
, blocks
);
3165 btrfs_free_path(path
);
3170 * helper to check if the block use full backrefs for pointers in it
3172 static int block_use_full_backref(struct reloc_control
*rc
,
3173 struct extent_buffer
*eb
)
3178 if (btrfs_header_flag(eb
, BTRFS_HEADER_FLAG_RELOC
) ||
3179 btrfs_header_backref_rev(eb
) < BTRFS_MIXED_BACKREF_REV
)
3182 ret
= btrfs_lookup_extent_info(NULL
, rc
->extent_root
,
3183 eb
->start
, eb
->len
, NULL
, &flags
);
3186 if (flags
& BTRFS_BLOCK_FLAG_FULL_BACKREF
)
3194 * helper to add tree blocks for backref of type BTRFS_EXTENT_DATA_REF_KEY
3195 * this function scans fs tree to find blocks reference the data extent
3197 static int find_data_references(struct reloc_control
*rc
,
3198 struct btrfs_key
*extent_key
,
3199 struct extent_buffer
*leaf
,
3200 struct btrfs_extent_data_ref
*ref
,
3201 struct rb_root
*blocks
)
3203 struct btrfs_path
*path
;
3204 struct tree_block
*block
;
3205 struct btrfs_root
*root
;
3206 struct btrfs_file_extent_item
*fi
;
3207 struct rb_node
*rb_node
;
3208 struct btrfs_key key
;
3219 path
= btrfs_alloc_path();
3223 ref_root
= btrfs_extent_data_ref_root(leaf
, ref
);
3224 ref_objectid
= btrfs_extent_data_ref_objectid(leaf
, ref
);
3225 ref_offset
= btrfs_extent_data_ref_offset(leaf
, ref
);
3226 ref_count
= btrfs_extent_data_ref_count(leaf
, ref
);
3228 root
= read_fs_root(rc
->extent_root
->fs_info
, ref_root
);
3230 err
= PTR_ERR(root
);
3234 key
.objectid
= ref_objectid
;
3235 key
.offset
= ref_offset
;
3236 key
.type
= BTRFS_EXTENT_DATA_KEY
;
3238 path
->search_commit_root
= 1;
3239 path
->skip_locking
= 1;
3240 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
3246 leaf
= path
->nodes
[0];
3247 nritems
= btrfs_header_nritems(leaf
);
3249 * the references in tree blocks that use full backrefs
3250 * are not counted in
3252 if (block_use_full_backref(rc
, leaf
))
3256 rb_node
= tree_search(blocks
, leaf
->start
);
3261 path
->slots
[0] = nritems
;
3264 while (ref_count
> 0) {
3265 while (path
->slots
[0] >= nritems
) {
3266 ret
= btrfs_next_leaf(root
, path
);
3276 leaf
= path
->nodes
[0];
3277 nritems
= btrfs_header_nritems(leaf
);
3280 if (block_use_full_backref(rc
, leaf
))
3284 rb_node
= tree_search(blocks
, leaf
->start
);
3289 path
->slots
[0] = nritems
;
3293 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
3294 if (key
.objectid
!= ref_objectid
||
3295 key
.type
!= BTRFS_EXTENT_DATA_KEY
) {
3300 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
3301 struct btrfs_file_extent_item
);
3303 if (btrfs_file_extent_type(leaf
, fi
) ==
3304 BTRFS_FILE_EXTENT_INLINE
)
3307 if (btrfs_file_extent_disk_bytenr(leaf
, fi
) !=
3308 extent_key
->objectid
)
3311 key
.offset
-= btrfs_file_extent_offset(leaf
, fi
);
3312 if (key
.offset
!= ref_offset
)
3320 if (!tree_block_processed(leaf
->start
, leaf
->len
, rc
)) {
3321 block
= kmalloc(sizeof(*block
), GFP_NOFS
);
3326 block
->bytenr
= leaf
->start
;
3327 btrfs_item_key_to_cpu(leaf
, &block
->key
, 0);
3329 block
->key_ready
= 1;
3330 rb_node
= tree_insert(blocks
, block
->bytenr
,
3337 path
->slots
[0] = nritems
;
3343 btrfs_free_path(path
);
3348 * hepler to find all tree blocks that reference a given data extent
3350 static noinline_for_stack
3351 int add_data_references(struct reloc_control
*rc
,
3352 struct btrfs_key
*extent_key
,
3353 struct btrfs_path
*path
,
3354 struct rb_root
*blocks
)
3356 struct btrfs_key key
;
3357 struct extent_buffer
*eb
;
3358 struct btrfs_extent_data_ref
*dref
;
3359 struct btrfs_extent_inline_ref
*iref
;
3362 u32 blocksize
= btrfs_level_size(rc
->extent_root
, 0);
3366 eb
= path
->nodes
[0];
3367 ptr
= btrfs_item_ptr_offset(eb
, path
->slots
[0]);
3368 end
= ptr
+ btrfs_item_size_nr(eb
, path
->slots
[0]);
3369 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3370 if (ptr
+ sizeof(struct btrfs_extent_item_v0
) == end
)
3374 ptr
+= sizeof(struct btrfs_extent_item
);
3377 iref
= (struct btrfs_extent_inline_ref
*)ptr
;
3378 key
.type
= btrfs_extent_inline_ref_type(eb
, iref
);
3379 if (key
.type
== BTRFS_SHARED_DATA_REF_KEY
) {
3380 key
.offset
= btrfs_extent_inline_ref_offset(eb
, iref
);
3381 ret
= __add_tree_block(rc
, key
.offset
, blocksize
,
3383 } else if (key
.type
== BTRFS_EXTENT_DATA_REF_KEY
) {
3384 dref
= (struct btrfs_extent_data_ref
*)(&iref
->offset
);
3385 ret
= find_data_references(rc
, extent_key
,
3390 ptr
+= btrfs_extent_inline_ref_size(key
.type
);
3396 eb
= path
->nodes
[0];
3397 if (path
->slots
[0] >= btrfs_header_nritems(eb
)) {
3398 ret
= btrfs_next_leaf(rc
->extent_root
, path
);
3405 eb
= path
->nodes
[0];
3408 btrfs_item_key_to_cpu(eb
, &key
, path
->slots
[0]);
3409 if (key
.objectid
!= extent_key
->objectid
)
3412 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3413 if (key
.type
== BTRFS_SHARED_DATA_REF_KEY
||
3414 key
.type
== BTRFS_EXTENT_REF_V0_KEY
) {
3416 BUG_ON(key
.type
== BTRFS_EXTENT_REF_V0_KEY
);
3417 if (key
.type
== BTRFS_SHARED_DATA_REF_KEY
) {
3419 ret
= __add_tree_block(rc
, key
.offset
, blocksize
,
3421 } else if (key
.type
== BTRFS_EXTENT_DATA_REF_KEY
) {
3422 dref
= btrfs_item_ptr(eb
, path
->slots
[0],
3423 struct btrfs_extent_data_ref
);
3424 ret
= find_data_references(rc
, extent_key
,
3435 btrfs_release_path(rc
->extent_root
, path
);
3437 free_block_list(blocks
);
3442 * hepler to find next unprocessed extent
3444 static noinline_for_stack
3445 int find_next_extent(struct btrfs_trans_handle
*trans
,
3446 struct reloc_control
*rc
, struct btrfs_path
*path
,
3447 struct btrfs_key
*extent_key
)
3449 struct btrfs_key key
;
3450 struct extent_buffer
*leaf
;
3451 u64 start
, end
, last
;
3454 last
= rc
->block_group
->key
.objectid
+ rc
->block_group
->key
.offset
;
3457 if (rc
->search_start
>= last
) {
3462 key
.objectid
= rc
->search_start
;
3463 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
3466 path
->search_commit_root
= 1;
3467 path
->skip_locking
= 1;
3468 ret
= btrfs_search_slot(NULL
, rc
->extent_root
, &key
, path
,
3473 leaf
= path
->nodes
[0];
3474 if (path
->slots
[0] >= btrfs_header_nritems(leaf
)) {
3475 ret
= btrfs_next_leaf(rc
->extent_root
, path
);
3478 leaf
= path
->nodes
[0];
3481 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
3482 if (key
.objectid
>= last
) {
3487 if (key
.type
!= BTRFS_EXTENT_ITEM_KEY
||
3488 key
.objectid
+ key
.offset
<= rc
->search_start
) {
3493 ret
= find_first_extent_bit(&rc
->processed_blocks
,
3494 key
.objectid
, &start
, &end
,
3497 if (ret
== 0 && start
<= key
.objectid
) {
3498 btrfs_release_path(rc
->extent_root
, path
);
3499 rc
->search_start
= end
+ 1;
3501 rc
->search_start
= key
.objectid
+ key
.offset
;
3502 memcpy(extent_key
, &key
, sizeof(key
));
3506 btrfs_release_path(rc
->extent_root
, path
);
3510 static void set_reloc_control(struct reloc_control
*rc
)
3512 struct btrfs_fs_info
*fs_info
= rc
->extent_root
->fs_info
;
3513 mutex_lock(&fs_info
->trans_mutex
);
3514 fs_info
->reloc_ctl
= rc
;
3515 mutex_unlock(&fs_info
->trans_mutex
);
3518 static void unset_reloc_control(struct reloc_control
*rc
)
3520 struct btrfs_fs_info
*fs_info
= rc
->extent_root
->fs_info
;
3521 mutex_lock(&fs_info
->trans_mutex
);
3522 fs_info
->reloc_ctl
= NULL
;
3523 mutex_unlock(&fs_info
->trans_mutex
);
3526 static int check_extent_flags(u64 flags
)
3528 if ((flags
& BTRFS_EXTENT_FLAG_DATA
) &&
3529 (flags
& BTRFS_EXTENT_FLAG_TREE_BLOCK
))
3531 if (!(flags
& BTRFS_EXTENT_FLAG_DATA
) &&
3532 !(flags
& BTRFS_EXTENT_FLAG_TREE_BLOCK
))
3534 if ((flags
& BTRFS_EXTENT_FLAG_DATA
) &&
3535 (flags
& BTRFS_BLOCK_FLAG_FULL_BACKREF
))
3540 static noinline_for_stack
3541 int prepare_to_relocate(struct reloc_control
*rc
)
3543 struct btrfs_trans_handle
*trans
;
3546 rc
->block_rsv
= btrfs_alloc_block_rsv(rc
->extent_root
);
3551 * reserve some space for creating reloc trees.
3552 * btrfs_init_reloc_root will use them when there
3553 * is no reservation in transaction handle.
3555 ret
= btrfs_block_rsv_add(NULL
, rc
->extent_root
, rc
->block_rsv
,
3556 rc
->extent_root
->nodesize
* 256,
3557 &rc
->block_rsv_retries
);
3561 rc
->block_rsv
->refill_used
= 1;
3562 btrfs_add_durable_block_rsv(rc
->extent_root
->fs_info
, rc
->block_rsv
);
3564 memset(&rc
->cluster
, 0, sizeof(rc
->cluster
));
3565 rc
->search_start
= rc
->block_group
->key
.objectid
;
3566 rc
->extents_found
= 0;
3567 rc
->nodes_relocated
= 0;
3568 rc
->merging_rsv_size
= 0;
3569 rc
->block_rsv_retries
= 0;
3571 rc
->create_reloc_tree
= 1;
3572 set_reloc_control(rc
);
3574 trans
= btrfs_join_transaction(rc
->extent_root
, 1);
3575 btrfs_commit_transaction(trans
, rc
->extent_root
);
3579 static noinline_for_stack
int relocate_block_group(struct reloc_control
*rc
)
3581 struct rb_root blocks
= RB_ROOT
;
3582 struct btrfs_key key
;
3583 struct btrfs_trans_handle
*trans
= NULL
;
3584 struct btrfs_path
*path
;
3585 struct btrfs_extent_item
*ei
;
3592 path
= btrfs_alloc_path();
3596 ret
= prepare_to_relocate(rc
);
3603 trans
= btrfs_start_transaction(rc
->extent_root
, 0);
3605 if (update_backref_cache(trans
, &rc
->backref_cache
)) {
3606 btrfs_end_transaction(trans
, rc
->extent_root
);
3610 ret
= find_next_extent(trans
, rc
, path
, &key
);
3616 rc
->extents_found
++;
3618 ei
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
3619 struct btrfs_extent_item
);
3620 item_size
= btrfs_item_size_nr(path
->nodes
[0], path
->slots
[0]);
3621 if (item_size
>= sizeof(*ei
)) {
3622 flags
= btrfs_extent_flags(path
->nodes
[0], ei
);
3623 ret
= check_extent_flags(flags
);
3627 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3629 int path_change
= 0;
3632 sizeof(struct btrfs_extent_item_v0
));
3633 ret
= get_ref_objectid_v0(rc
, path
, &key
, &ref_owner
,
3635 if (ref_owner
< BTRFS_FIRST_FREE_OBJECTID
)
3636 flags
= BTRFS_EXTENT_FLAG_TREE_BLOCK
;
3638 flags
= BTRFS_EXTENT_FLAG_DATA
;
3641 btrfs_release_path(rc
->extent_root
, path
);
3643 path
->search_commit_root
= 1;
3644 path
->skip_locking
= 1;
3645 ret
= btrfs_search_slot(NULL
, rc
->extent_root
,
3658 if (flags
& BTRFS_EXTENT_FLAG_TREE_BLOCK
) {
3659 ret
= add_tree_block(rc
, &key
, path
, &blocks
);
3660 } else if (rc
->stage
== UPDATE_DATA_PTRS
&&
3661 (flags
& BTRFS_EXTENT_FLAG_DATA
)) {
3662 ret
= add_data_references(rc
, &key
, path
, &blocks
);
3664 btrfs_release_path(rc
->extent_root
, path
);
3672 if (!RB_EMPTY_ROOT(&blocks
)) {
3673 ret
= relocate_tree_blocks(trans
, rc
, &blocks
);
3675 if (ret
!= -EAGAIN
) {
3679 rc
->extents_found
--;
3680 rc
->search_start
= key
.objectid
;
3684 ret
= btrfs_block_rsv_check(trans
, rc
->extent_root
,
3685 rc
->block_rsv
, 0, 5);
3687 if (ret
!= -EAGAIN
) {
3692 rc
->commit_transaction
= 1;
3695 if (rc
->commit_transaction
) {
3696 rc
->commit_transaction
= 0;
3697 ret
= btrfs_commit_transaction(trans
, rc
->extent_root
);
3700 nr
= trans
->blocks_used
;
3701 btrfs_end_transaction_throttle(trans
, rc
->extent_root
);
3702 btrfs_btree_balance_dirty(rc
->extent_root
, nr
);
3706 if (rc
->stage
== MOVE_DATA_EXTENTS
&&
3707 (flags
& BTRFS_EXTENT_FLAG_DATA
)) {
3708 rc
->found_file_extent
= 1;
3709 ret
= relocate_data_extent(rc
->data_inode
,
3710 &key
, &rc
->cluster
);
3718 btrfs_release_path(rc
->extent_root
, path
);
3719 clear_extent_bits(&rc
->processed_blocks
, 0, (u64
)-1, EXTENT_DIRTY
,
3723 nr
= trans
->blocks_used
;
3724 btrfs_end_transaction_throttle(trans
, rc
->extent_root
);
3725 btrfs_btree_balance_dirty(rc
->extent_root
, nr
);
3729 ret
= relocate_file_extent_cluster(rc
->data_inode
,
3735 rc
->create_reloc_tree
= 0;
3736 set_reloc_control(rc
);
3738 backref_cache_cleanup(&rc
->backref_cache
);
3739 btrfs_block_rsv_release(rc
->extent_root
, rc
->block_rsv
, (u64
)-1);
3741 err
= prepare_to_merge(rc
, err
);
3743 merge_reloc_roots(rc
);
3745 rc
->merge_reloc_tree
= 0;
3746 unset_reloc_control(rc
);
3747 btrfs_block_rsv_release(rc
->extent_root
, rc
->block_rsv
, (u64
)-1);
3749 /* get rid of pinned extents */
3750 trans
= btrfs_join_transaction(rc
->extent_root
, 1);
3751 btrfs_commit_transaction(trans
, rc
->extent_root
);
3753 btrfs_free_block_rsv(rc
->extent_root
, rc
->block_rsv
);
3754 btrfs_free_path(path
);
3758 static int __insert_orphan_inode(struct btrfs_trans_handle
*trans
,
3759 struct btrfs_root
*root
, u64 objectid
)
3761 struct btrfs_path
*path
;
3762 struct btrfs_inode_item
*item
;
3763 struct extent_buffer
*leaf
;
3766 path
= btrfs_alloc_path();
3770 ret
= btrfs_insert_empty_inode(trans
, root
, path
, objectid
);
3774 leaf
= path
->nodes
[0];
3775 item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_inode_item
);
3776 memset_extent_buffer(leaf
, 0, (unsigned long)item
, sizeof(*item
));
3777 btrfs_set_inode_generation(leaf
, item
, 1);
3778 btrfs_set_inode_size(leaf
, item
, 0);
3779 btrfs_set_inode_mode(leaf
, item
, S_IFREG
| 0600);
3780 btrfs_set_inode_flags(leaf
, item
, BTRFS_INODE_NOCOMPRESS
|
3781 BTRFS_INODE_PREALLOC
);
3782 btrfs_mark_buffer_dirty(leaf
);
3783 btrfs_release_path(root
, path
);
3785 btrfs_free_path(path
);
3790 * helper to create inode for data relocation.
3791 * the inode is in data relocation tree and its link count is 0
3793 static noinline_for_stack
3794 struct inode
*create_reloc_inode(struct btrfs_fs_info
*fs_info
,
3795 struct btrfs_block_group_cache
*group
)
3797 struct inode
*inode
= NULL
;
3798 struct btrfs_trans_handle
*trans
;
3799 struct btrfs_root
*root
;
3800 struct btrfs_key key
;
3802 u64 objectid
= BTRFS_FIRST_FREE_OBJECTID
;
3805 root
= read_fs_root(fs_info
, BTRFS_DATA_RELOC_TREE_OBJECTID
);
3807 return ERR_CAST(root
);
3809 trans
= btrfs_start_transaction(root
, 6);
3811 return ERR_CAST(trans
);
3813 err
= btrfs_find_free_objectid(trans
, root
, objectid
, &objectid
);
3817 err
= __insert_orphan_inode(trans
, root
, objectid
);
3820 key
.objectid
= objectid
;
3821 key
.type
= BTRFS_INODE_ITEM_KEY
;
3823 inode
= btrfs_iget(root
->fs_info
->sb
, &key
, root
, NULL
);
3824 BUG_ON(IS_ERR(inode
) || is_bad_inode(inode
));
3825 BTRFS_I(inode
)->index_cnt
= group
->key
.objectid
;
3827 err
= btrfs_orphan_add(trans
, inode
);
3829 nr
= trans
->blocks_used
;
3830 btrfs_end_transaction(trans
, root
);
3831 btrfs_btree_balance_dirty(root
, nr
);
3835 inode
= ERR_PTR(err
);
3840 static struct reloc_control
*alloc_reloc_control(void)
3842 struct reloc_control
*rc
;
3844 rc
= kzalloc(sizeof(*rc
), GFP_NOFS
);
3848 INIT_LIST_HEAD(&rc
->reloc_roots
);
3849 backref_cache_init(&rc
->backref_cache
);
3850 mapping_tree_init(&rc
->reloc_root_tree
);
3851 extent_io_tree_init(&rc
->processed_blocks
, NULL
, GFP_NOFS
);
3856 * function to relocate all extents in a block group.
3858 int btrfs_relocate_block_group(struct btrfs_root
*extent_root
, u64 group_start
)
3860 struct btrfs_fs_info
*fs_info
= extent_root
->fs_info
;
3861 struct reloc_control
*rc
;
3866 rc
= alloc_reloc_control();
3870 rc
->extent_root
= extent_root
;
3872 rc
->block_group
= btrfs_lookup_block_group(fs_info
, group_start
);
3873 BUG_ON(!rc
->block_group
);
3875 if (!rc
->block_group
->ro
) {
3876 ret
= btrfs_set_block_group_ro(extent_root
, rc
->block_group
);
3884 rc
->data_inode
= create_reloc_inode(fs_info
, rc
->block_group
);
3885 if (IS_ERR(rc
->data_inode
)) {
3886 err
= PTR_ERR(rc
->data_inode
);
3887 rc
->data_inode
= NULL
;
3891 printk(KERN_INFO
"btrfs: relocating block group %llu flags %llu\n",
3892 (unsigned long long)rc
->block_group
->key
.objectid
,
3893 (unsigned long long)rc
->block_group
->flags
);
3895 btrfs_start_delalloc_inodes(fs_info
->tree_root
, 0);
3896 btrfs_wait_ordered_extents(fs_info
->tree_root
, 0, 0);
3899 mutex_lock(&fs_info
->cleaner_mutex
);
3901 btrfs_clean_old_snapshots(fs_info
->tree_root
);
3902 ret
= relocate_block_group(rc
);
3904 mutex_unlock(&fs_info
->cleaner_mutex
);
3910 if (rc
->extents_found
== 0)
3913 printk(KERN_INFO
"btrfs: found %llu extents\n",
3914 (unsigned long long)rc
->extents_found
);
3916 if (rc
->stage
== MOVE_DATA_EXTENTS
&& rc
->found_file_extent
) {
3917 btrfs_wait_ordered_range(rc
->data_inode
, 0, (u64
)-1);
3918 invalidate_mapping_pages(rc
->data_inode
->i_mapping
,
3920 rc
->stage
= UPDATE_DATA_PTRS
;
3924 filemap_write_and_wait_range(fs_info
->btree_inode
->i_mapping
,
3925 rc
->block_group
->key
.objectid
,
3926 rc
->block_group
->key
.objectid
+
3927 rc
->block_group
->key
.offset
- 1);
3929 WARN_ON(rc
->block_group
->pinned
> 0);
3930 WARN_ON(rc
->block_group
->reserved
> 0);
3931 WARN_ON(btrfs_block_group_used(&rc
->block_group
->item
) > 0);
3934 btrfs_set_block_group_rw(extent_root
, rc
->block_group
);
3935 iput(rc
->data_inode
);
3936 btrfs_put_block_group(rc
->block_group
);
3941 static noinline_for_stack
int mark_garbage_root(struct btrfs_root
*root
)
3943 struct btrfs_trans_handle
*trans
;
3946 trans
= btrfs_start_transaction(root
->fs_info
->tree_root
, 0);
3948 memset(&root
->root_item
.drop_progress
, 0,
3949 sizeof(root
->root_item
.drop_progress
));
3950 root
->root_item
.drop_level
= 0;
3951 btrfs_set_root_refs(&root
->root_item
, 0);
3952 ret
= btrfs_update_root(trans
, root
->fs_info
->tree_root
,
3953 &root
->root_key
, &root
->root_item
);
3956 ret
= btrfs_end_transaction(trans
, root
->fs_info
->tree_root
);
3962 * recover relocation interrupted by system crash.
3964 * this function resumes merging reloc trees with corresponding fs trees.
3965 * this is important for keeping the sharing of tree blocks
3967 int btrfs_recover_relocation(struct btrfs_root
*root
)
3969 LIST_HEAD(reloc_roots
);
3970 struct btrfs_key key
;
3971 struct btrfs_root
*fs_root
;
3972 struct btrfs_root
*reloc_root
;
3973 struct btrfs_path
*path
;
3974 struct extent_buffer
*leaf
;
3975 struct reloc_control
*rc
= NULL
;
3976 struct btrfs_trans_handle
*trans
;
3980 path
= btrfs_alloc_path();
3984 key
.objectid
= BTRFS_TREE_RELOC_OBJECTID
;
3985 key
.type
= BTRFS_ROOT_ITEM_KEY
;
3986 key
.offset
= (u64
)-1;
3989 ret
= btrfs_search_slot(NULL
, root
->fs_info
->tree_root
, &key
,
3996 if (path
->slots
[0] == 0)
4000 leaf
= path
->nodes
[0];
4001 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
4002 btrfs_release_path(root
->fs_info
->tree_root
, path
);
4004 if (key
.objectid
!= BTRFS_TREE_RELOC_OBJECTID
||
4005 key
.type
!= BTRFS_ROOT_ITEM_KEY
)
4008 reloc_root
= btrfs_read_fs_root_no_radix(root
, &key
);
4009 if (IS_ERR(reloc_root
)) {
4010 err
= PTR_ERR(reloc_root
);
4014 list_add(&reloc_root
->root_list
, &reloc_roots
);
4016 if (btrfs_root_refs(&reloc_root
->root_item
) > 0) {
4017 fs_root
= read_fs_root(root
->fs_info
,
4018 reloc_root
->root_key
.offset
);
4019 if (IS_ERR(fs_root
)) {
4020 ret
= PTR_ERR(fs_root
);
4021 if (ret
!= -ENOENT
) {
4025 mark_garbage_root(reloc_root
);
4029 if (key
.offset
== 0)
4034 btrfs_release_path(root
->fs_info
->tree_root
, path
);
4036 if (list_empty(&reloc_roots
))
4039 rc
= alloc_reloc_control();
4045 rc
->extent_root
= root
->fs_info
->extent_root
;
4047 set_reloc_control(rc
);
4049 trans
= btrfs_join_transaction(rc
->extent_root
, 1);
4051 rc
->merge_reloc_tree
= 1;
4053 while (!list_empty(&reloc_roots
)) {
4054 reloc_root
= list_entry(reloc_roots
.next
,
4055 struct btrfs_root
, root_list
);
4056 list_del(&reloc_root
->root_list
);
4058 if (btrfs_root_refs(&reloc_root
->root_item
) == 0) {
4059 list_add_tail(&reloc_root
->root_list
,
4064 fs_root
= read_fs_root(root
->fs_info
,
4065 reloc_root
->root_key
.offset
);
4066 BUG_ON(IS_ERR(fs_root
));
4068 __add_reloc_root(reloc_root
);
4069 fs_root
->reloc_root
= reloc_root
;
4072 btrfs_commit_transaction(trans
, rc
->extent_root
);
4074 merge_reloc_roots(rc
);
4076 unset_reloc_control(rc
);
4078 trans
= btrfs_join_transaction(rc
->extent_root
, 1);
4079 btrfs_commit_transaction(trans
, rc
->extent_root
);
4082 while (!list_empty(&reloc_roots
)) {
4083 reloc_root
= list_entry(reloc_roots
.next
,
4084 struct btrfs_root
, root_list
);
4085 list_del(&reloc_root
->root_list
);
4086 free_extent_buffer(reloc_root
->node
);
4087 free_extent_buffer(reloc_root
->commit_root
);
4090 btrfs_free_path(path
);
4093 /* cleanup orphan inode in data relocation tree */
4094 fs_root
= read_fs_root(root
->fs_info
,
4095 BTRFS_DATA_RELOC_TREE_OBJECTID
);
4096 if (IS_ERR(fs_root
))
4097 err
= PTR_ERR(fs_root
);
4099 btrfs_orphan_cleanup(fs_root
);
4105 * helper to add ordered checksum for data relocation.
4107 * cloning checksum properly handles the nodatasum extents.
4108 * it also saves CPU time to re-calculate the checksum.
4110 int btrfs_reloc_clone_csums(struct inode
*inode
, u64 file_pos
, u64 len
)
4112 struct btrfs_ordered_sum
*sums
;
4113 struct btrfs_sector_sum
*sector_sum
;
4114 struct btrfs_ordered_extent
*ordered
;
4115 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4121 ordered
= btrfs_lookup_ordered_extent(inode
, file_pos
);
4122 BUG_ON(ordered
->file_offset
!= file_pos
|| ordered
->len
!= len
);
4124 disk_bytenr
= file_pos
+ BTRFS_I(inode
)->index_cnt
;
4125 ret
= btrfs_lookup_csums_range(root
->fs_info
->csum_root
, disk_bytenr
,
4126 disk_bytenr
+ len
- 1, &list
);
4128 while (!list_empty(&list
)) {
4129 sums
= list_entry(list
.next
, struct btrfs_ordered_sum
, list
);
4130 list_del_init(&sums
->list
);
4132 sector_sum
= sums
->sums
;
4133 sums
->bytenr
= ordered
->start
;
4136 while (offset
< sums
->len
) {
4137 sector_sum
->bytenr
+= ordered
->start
- disk_bytenr
;
4139 offset
+= root
->sectorsize
;
4142 btrfs_add_ordered_sum(inode
, ordered
, sums
);
4144 btrfs_put_ordered_extent(ordered
);
4148 void btrfs_reloc_cow_block(struct btrfs_trans_handle
*trans
,
4149 struct btrfs_root
*root
, struct extent_buffer
*buf
,
4150 struct extent_buffer
*cow
)
4152 struct reloc_control
*rc
;
4153 struct backref_node
*node
;
4158 rc
= root
->fs_info
->reloc_ctl
;
4162 BUG_ON(rc
->stage
== UPDATE_DATA_PTRS
&&
4163 root
->root_key
.objectid
== BTRFS_DATA_RELOC_TREE_OBJECTID
);
4165 level
= btrfs_header_level(buf
);
4166 if (btrfs_header_generation(buf
) <=
4167 btrfs_root_last_snapshot(&root
->root_item
))
4170 if (root
->root_key
.objectid
== BTRFS_TREE_RELOC_OBJECTID
&&
4171 rc
->create_reloc_tree
) {
4172 WARN_ON(!first_cow
&& level
== 0);
4174 node
= rc
->backref_cache
.path
[level
];
4175 BUG_ON(node
->bytenr
!= buf
->start
&&
4176 node
->new_bytenr
!= buf
->start
);
4178 drop_node_buffer(node
);
4179 extent_buffer_get(cow
);
4181 node
->new_bytenr
= cow
->start
;
4183 if (!node
->pending
) {
4184 list_move_tail(&node
->list
,
4185 &rc
->backref_cache
.pending
[level
]);
4190 __mark_block_processed(rc
, node
);
4192 if (first_cow
&& level
> 0)
4193 rc
->nodes_relocated
+= buf
->len
;
4196 if (level
== 0 && first_cow
&& rc
->stage
== UPDATE_DATA_PTRS
) {
4197 ret
= replace_file_extents(trans
, rc
, root
, cow
);
4203 * called before creating snapshot. it calculates metadata reservation
4204 * requried for relocating tree blocks in the snapshot
4206 void btrfs_reloc_pre_snapshot(struct btrfs_trans_handle
*trans
,
4207 struct btrfs_pending_snapshot
*pending
,
4208 u64
*bytes_to_reserve
)
4210 struct btrfs_root
*root
;
4211 struct reloc_control
*rc
;
4213 root
= pending
->root
;
4214 if (!root
->reloc_root
)
4217 rc
= root
->fs_info
->reloc_ctl
;
4218 if (!rc
->merge_reloc_tree
)
4221 root
= root
->reloc_root
;
4222 BUG_ON(btrfs_root_refs(&root
->root_item
) == 0);
4224 * relocation is in the stage of merging trees. the space
4225 * used by merging a reloc tree is twice the size of
4226 * relocated tree nodes in the worst case. half for cowing
4227 * the reloc tree, half for cowing the fs tree. the space
4228 * used by cowing the reloc tree will be freed after the
4229 * tree is dropped. if we create snapshot, cowing the fs
4230 * tree may use more space than it frees. so we need
4231 * reserve extra space.
4233 *bytes_to_reserve
+= rc
->nodes_relocated
;
4237 * called after snapshot is created. migrate block reservation
4238 * and create reloc root for the newly created snapshot
4240 void btrfs_reloc_post_snapshot(struct btrfs_trans_handle
*trans
,
4241 struct btrfs_pending_snapshot
*pending
)
4243 struct btrfs_root
*root
= pending
->root
;
4244 struct btrfs_root
*reloc_root
;
4245 struct btrfs_root
*new_root
;
4246 struct reloc_control
*rc
;
4249 if (!root
->reloc_root
)
4252 rc
= root
->fs_info
->reloc_ctl
;
4253 rc
->merging_rsv_size
+= rc
->nodes_relocated
;
4255 if (rc
->merge_reloc_tree
) {
4256 ret
= btrfs_block_rsv_migrate(&pending
->block_rsv
,
4258 rc
->nodes_relocated
);
4262 new_root
= pending
->snap
;
4263 reloc_root
= create_reloc_root(trans
, root
->reloc_root
,
4264 new_root
->root_key
.objectid
);
4266 __add_reloc_root(reloc_root
);
4267 new_root
->reloc_root
= reloc_root
;
4269 if (rc
->create_reloc_tree
) {
4270 ret
= clone_backref_node(trans
, rc
, root
, reloc_root
);