2 * Copyright (C) 2009 Oracle. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
19 #include <linux/sched.h>
20 #include <linux/pagemap.h>
21 #include <linux/writeback.h>
22 #include <linux/blkdev.h>
23 #include <linux/rbtree.h>
24 #include <linux/slab.h>
27 #include "transaction.h"
30 #include "btrfs_inode.h"
31 #include "async-thread.h"
32 #include "free-space-cache.h"
33 #include "inode-map.h"
36 * backref_node, mapping_node and tree_block start with this
39 struct rb_node rb_node
;
44 * present a tree block in the backref cache
47 struct rb_node rb_node
;
51 /* objectid of tree block owner, can be not uptodate */
53 /* link to pending, changed or detached list */
54 struct list_head list
;
55 /* list of upper level blocks reference this block */
56 struct list_head upper
;
57 /* list of child blocks in the cache */
58 struct list_head lower
;
59 /* NULL if this node is not tree root */
60 struct btrfs_root
*root
;
61 /* extent buffer got by COW the block */
62 struct extent_buffer
*eb
;
63 /* level of tree block */
65 /* is the block in non-reference counted tree */
66 unsigned int cowonly
:1;
67 /* 1 if no child node in the cache */
68 unsigned int lowest
:1;
69 /* is the extent buffer locked */
70 unsigned int locked
:1;
71 /* has the block been processed */
72 unsigned int processed
:1;
73 /* have backrefs of this block been checked */
74 unsigned int checked
:1;
76 * 1 if corresponding block has been cowed but some upper
77 * level block pointers may not point to the new location
79 unsigned int pending
:1;
81 * 1 if the backref node isn't connected to any other
84 unsigned int detached
:1;
88 * present a block pointer in the backref cache
91 struct list_head list
[2];
92 struct backref_node
*node
[2];
98 struct backref_cache
{
99 /* red black tree of all backref nodes in the cache */
100 struct rb_root rb_root
;
101 /* for passing backref nodes to btrfs_reloc_cow_block */
102 struct backref_node
*path
[BTRFS_MAX_LEVEL
];
104 * list of blocks that have been cowed but some block
105 * pointers in upper level blocks may not reflect the
108 struct list_head pending
[BTRFS_MAX_LEVEL
];
109 /* list of backref nodes with no child node */
110 struct list_head leaves
;
111 /* list of blocks that have been cowed in current transaction */
112 struct list_head changed
;
113 /* list of detached backref node. */
114 struct list_head detached
;
123 * map address of tree root to tree
125 struct mapping_node
{
126 struct rb_node rb_node
;
131 struct mapping_tree
{
132 struct rb_root rb_root
;
137 * present a tree block to process
140 struct rb_node rb_node
;
142 struct btrfs_key key
;
143 unsigned int level
:8;
144 unsigned int key_ready
:1;
147 #define MAX_EXTENTS 128
149 struct file_extent_cluster
{
152 u64 boundary
[MAX_EXTENTS
];
156 struct reloc_control
{
157 /* block group to relocate */
158 struct btrfs_block_group_cache
*block_group
;
160 struct btrfs_root
*extent_root
;
161 /* inode for moving data */
162 struct inode
*data_inode
;
164 struct btrfs_block_rsv
*block_rsv
;
166 struct backref_cache backref_cache
;
168 struct file_extent_cluster cluster
;
169 /* tree blocks have been processed */
170 struct extent_io_tree processed_blocks
;
171 /* map start of tree root to corresponding reloc tree */
172 struct mapping_tree reloc_root_tree
;
173 /* list of reloc trees */
174 struct list_head reloc_roots
;
175 /* size of metadata reservation for merging reloc trees */
176 u64 merging_rsv_size
;
177 /* size of relocated tree nodes */
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
)
329 static void backref_tree_panic(struct rb_node
*rb_node
, int errno
, u64 bytenr
)
332 struct btrfs_fs_info
*fs_info
= NULL
;
333 struct backref_node
*bnode
= rb_entry(rb_node
, struct backref_node
,
336 fs_info
= bnode
->root
->fs_info
;
337 btrfs_panic(fs_info
, errno
, "Inconsistency in backref cache "
338 "found at offset %llu\n", (unsigned long long)bytenr
);
342 * walk up backref nodes until reach node presents tree root
344 static struct backref_node
*walk_up_backref(struct backref_node
*node
,
345 struct backref_edge
*edges
[],
348 struct backref_edge
*edge
;
351 while (!list_empty(&node
->upper
)) {
352 edge
= list_entry(node
->upper
.next
,
353 struct backref_edge
, list
[LOWER
]);
355 node
= edge
->node
[UPPER
];
357 BUG_ON(node
->detached
);
363 * walk down backref nodes to find start of next reference path
365 static struct backref_node
*walk_down_backref(struct backref_edge
*edges
[],
368 struct backref_edge
*edge
;
369 struct backref_node
*lower
;
373 edge
= edges
[idx
- 1];
374 lower
= edge
->node
[LOWER
];
375 if (list_is_last(&edge
->list
[LOWER
], &lower
->upper
)) {
379 edge
= list_entry(edge
->list
[LOWER
].next
,
380 struct backref_edge
, list
[LOWER
]);
381 edges
[idx
- 1] = edge
;
383 return edge
->node
[UPPER
];
389 static void unlock_node_buffer(struct backref_node
*node
)
392 btrfs_tree_unlock(node
->eb
);
397 static void drop_node_buffer(struct backref_node
*node
)
400 unlock_node_buffer(node
);
401 free_extent_buffer(node
->eb
);
406 static void drop_backref_node(struct backref_cache
*tree
,
407 struct backref_node
*node
)
409 BUG_ON(!list_empty(&node
->upper
));
411 drop_node_buffer(node
);
412 list_del(&node
->list
);
413 list_del(&node
->lower
);
414 if (!RB_EMPTY_NODE(&node
->rb_node
))
415 rb_erase(&node
->rb_node
, &tree
->rb_root
);
416 free_backref_node(tree
, node
);
420 * remove a backref node from the backref cache
422 static void remove_backref_node(struct backref_cache
*cache
,
423 struct backref_node
*node
)
425 struct backref_node
*upper
;
426 struct backref_edge
*edge
;
431 BUG_ON(!node
->lowest
&& !node
->detached
);
432 while (!list_empty(&node
->upper
)) {
433 edge
= list_entry(node
->upper
.next
, struct backref_edge
,
435 upper
= edge
->node
[UPPER
];
436 list_del(&edge
->list
[LOWER
]);
437 list_del(&edge
->list
[UPPER
]);
438 free_backref_edge(cache
, edge
);
440 if (RB_EMPTY_NODE(&upper
->rb_node
)) {
441 BUG_ON(!list_empty(&node
->upper
));
442 drop_backref_node(cache
, node
);
448 * add the node to leaf node list if no other
449 * child block cached.
451 if (list_empty(&upper
->lower
)) {
452 list_add_tail(&upper
->lower
, &cache
->leaves
);
457 drop_backref_node(cache
, node
);
460 static void update_backref_node(struct backref_cache
*cache
,
461 struct backref_node
*node
, u64 bytenr
)
463 struct rb_node
*rb_node
;
464 rb_erase(&node
->rb_node
, &cache
->rb_root
);
465 node
->bytenr
= bytenr
;
466 rb_node
= tree_insert(&cache
->rb_root
, node
->bytenr
, &node
->rb_node
);
468 backref_tree_panic(rb_node
, -EEXIST
, bytenr
);
472 * update backref cache after a transaction commit
474 static int update_backref_cache(struct btrfs_trans_handle
*trans
,
475 struct backref_cache
*cache
)
477 struct backref_node
*node
;
480 if (cache
->last_trans
== 0) {
481 cache
->last_trans
= trans
->transid
;
485 if (cache
->last_trans
== trans
->transid
)
489 * detached nodes are used to avoid unnecessary backref
490 * lookup. transaction commit changes the extent tree.
491 * so the detached nodes are no longer useful.
493 while (!list_empty(&cache
->detached
)) {
494 node
= list_entry(cache
->detached
.next
,
495 struct backref_node
, list
);
496 remove_backref_node(cache
, node
);
499 while (!list_empty(&cache
->changed
)) {
500 node
= list_entry(cache
->changed
.next
,
501 struct backref_node
, list
);
502 list_del_init(&node
->list
);
503 BUG_ON(node
->pending
);
504 update_backref_node(cache
, node
, node
->new_bytenr
);
508 * some nodes can be left in the pending list if there were
509 * errors during processing the pending nodes.
511 for (level
= 0; level
< BTRFS_MAX_LEVEL
; level
++) {
512 list_for_each_entry(node
, &cache
->pending
[level
], list
) {
513 BUG_ON(!node
->pending
);
514 if (node
->bytenr
== node
->new_bytenr
)
516 update_backref_node(cache
, node
, node
->new_bytenr
);
520 cache
->last_trans
= 0;
525 static int should_ignore_root(struct btrfs_root
*root
)
527 struct btrfs_root
*reloc_root
;
532 reloc_root
= root
->reloc_root
;
536 if (btrfs_root_last_snapshot(&reloc_root
->root_item
) ==
537 root
->fs_info
->running_transaction
->transid
- 1)
540 * if there is reloc tree and it was created in previous
541 * transaction backref lookup can find the reloc tree,
542 * so backref node for the fs tree root is useless for
548 * find reloc tree by address of tree root
550 static struct btrfs_root
*find_reloc_root(struct reloc_control
*rc
,
553 struct rb_node
*rb_node
;
554 struct mapping_node
*node
;
555 struct btrfs_root
*root
= NULL
;
557 spin_lock(&rc
->reloc_root_tree
.lock
);
558 rb_node
= tree_search(&rc
->reloc_root_tree
.rb_root
, bytenr
);
560 node
= rb_entry(rb_node
, struct mapping_node
, rb_node
);
561 root
= (struct btrfs_root
*)node
->data
;
563 spin_unlock(&rc
->reloc_root_tree
.lock
);
567 static int is_cowonly_root(u64 root_objectid
)
569 if (root_objectid
== BTRFS_ROOT_TREE_OBJECTID
||
570 root_objectid
== BTRFS_EXTENT_TREE_OBJECTID
||
571 root_objectid
== BTRFS_CHUNK_TREE_OBJECTID
||
572 root_objectid
== BTRFS_DEV_TREE_OBJECTID
||
573 root_objectid
== BTRFS_TREE_LOG_OBJECTID
||
574 root_objectid
== BTRFS_CSUM_TREE_OBJECTID
)
579 static struct btrfs_root
*read_fs_root(struct btrfs_fs_info
*fs_info
,
582 struct btrfs_key key
;
584 key
.objectid
= root_objectid
;
585 key
.type
= BTRFS_ROOT_ITEM_KEY
;
586 if (is_cowonly_root(root_objectid
))
589 key
.offset
= (u64
)-1;
591 return btrfs_read_fs_root_no_name(fs_info
, &key
);
594 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
595 static noinline_for_stack
596 struct btrfs_root
*find_tree_root(struct reloc_control
*rc
,
597 struct extent_buffer
*leaf
,
598 struct btrfs_extent_ref_v0
*ref0
)
600 struct btrfs_root
*root
;
601 u64 root_objectid
= btrfs_ref_root_v0(leaf
, ref0
);
602 u64 generation
= btrfs_ref_generation_v0(leaf
, ref0
);
604 BUG_ON(root_objectid
== BTRFS_TREE_RELOC_OBJECTID
);
606 root
= read_fs_root(rc
->extent_root
->fs_info
, root_objectid
);
607 BUG_ON(IS_ERR(root
));
609 if (root
->ref_cows
&&
610 generation
!= btrfs_root_generation(&root
->root_item
))
617 static noinline_for_stack
618 int find_inline_backref(struct extent_buffer
*leaf
, int slot
,
619 unsigned long *ptr
, unsigned long *end
)
621 struct btrfs_key key
;
622 struct btrfs_extent_item
*ei
;
623 struct btrfs_tree_block_info
*bi
;
626 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
628 item_size
= btrfs_item_size_nr(leaf
, slot
);
629 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
630 if (item_size
< sizeof(*ei
)) {
631 WARN_ON(item_size
!= sizeof(struct btrfs_extent_item_v0
));
635 ei
= btrfs_item_ptr(leaf
, slot
, struct btrfs_extent_item
);
636 WARN_ON(!(btrfs_extent_flags(leaf
, ei
) &
637 BTRFS_EXTENT_FLAG_TREE_BLOCK
));
639 if (key
.type
== BTRFS_EXTENT_ITEM_KEY
&&
640 item_size
<= sizeof(*ei
) + sizeof(*bi
)) {
641 WARN_ON(item_size
< sizeof(*ei
) + sizeof(*bi
));
645 if (key
.type
== BTRFS_EXTENT_ITEM_KEY
) {
646 bi
= (struct btrfs_tree_block_info
*)(ei
+ 1);
647 *ptr
= (unsigned long)(bi
+ 1);
649 *ptr
= (unsigned long)(ei
+ 1);
651 *end
= (unsigned long)ei
+ item_size
;
656 * build backref tree for a given tree block. root of the backref tree
657 * corresponds the tree block, leaves of the backref tree correspond
658 * roots of b-trees that reference the tree block.
660 * the basic idea of this function is check backrefs of a given block
661 * to find upper level blocks that refernece the block, and then check
662 * bakcrefs of these upper level blocks recursively. the recursion stop
663 * when tree root is reached or backrefs for the block is cached.
665 * NOTE: if we find backrefs for a block are cached, we know backrefs
666 * for all upper level blocks that directly/indirectly reference the
667 * block are also cached.
669 static noinline_for_stack
670 struct backref_node
*build_backref_tree(struct reloc_control
*rc
,
671 struct btrfs_key
*node_key
,
672 int level
, u64 bytenr
)
674 struct backref_cache
*cache
= &rc
->backref_cache
;
675 struct btrfs_path
*path1
;
676 struct btrfs_path
*path2
;
677 struct extent_buffer
*eb
;
678 struct btrfs_root
*root
;
679 struct backref_node
*cur
;
680 struct backref_node
*upper
;
681 struct backref_node
*lower
;
682 struct backref_node
*node
= NULL
;
683 struct backref_node
*exist
= NULL
;
684 struct backref_edge
*edge
;
685 struct rb_node
*rb_node
;
686 struct btrfs_key key
;
694 bool need_check
= true;
696 path1
= btrfs_alloc_path();
697 path2
= btrfs_alloc_path();
698 if (!path1
|| !path2
) {
705 node
= alloc_backref_node(cache
);
711 node
->bytenr
= bytenr
;
718 key
.objectid
= cur
->bytenr
;
719 key
.type
= BTRFS_METADATA_ITEM_KEY
;
720 key
.offset
= (u64
)-1;
722 path1
->search_commit_root
= 1;
723 path1
->skip_locking
= 1;
724 ret
= btrfs_search_slot(NULL
, rc
->extent_root
, &key
, path1
,
730 BUG_ON(!ret
|| !path1
->slots
[0]);
734 WARN_ON(cur
->checked
);
735 if (!list_empty(&cur
->upper
)) {
737 * the backref was added previously when processing
738 * backref of type BTRFS_TREE_BLOCK_REF_KEY
740 BUG_ON(!list_is_singular(&cur
->upper
));
741 edge
= list_entry(cur
->upper
.next
, struct backref_edge
,
743 BUG_ON(!list_empty(&edge
->list
[UPPER
]));
744 exist
= edge
->node
[UPPER
];
746 * add the upper level block to pending list if we need
750 list_add_tail(&edge
->list
[UPPER
], &list
);
757 eb
= path1
->nodes
[0];
760 if (path1
->slots
[0] >= btrfs_header_nritems(eb
)) {
761 ret
= btrfs_next_leaf(rc
->extent_root
, path1
);
768 eb
= path1
->nodes
[0];
771 btrfs_item_key_to_cpu(eb
, &key
, path1
->slots
[0]);
772 if (key
.objectid
!= cur
->bytenr
) {
777 if (key
.type
== BTRFS_EXTENT_ITEM_KEY
||
778 key
.type
== BTRFS_METADATA_ITEM_KEY
) {
779 ret
= find_inline_backref(eb
, path1
->slots
[0],
787 /* update key for inline back ref */
788 struct btrfs_extent_inline_ref
*iref
;
789 iref
= (struct btrfs_extent_inline_ref
*)ptr
;
790 key
.type
= btrfs_extent_inline_ref_type(eb
, iref
);
791 key
.offset
= btrfs_extent_inline_ref_offset(eb
, iref
);
792 WARN_ON(key
.type
!= BTRFS_TREE_BLOCK_REF_KEY
&&
793 key
.type
!= BTRFS_SHARED_BLOCK_REF_KEY
);
797 ((key
.type
== BTRFS_TREE_BLOCK_REF_KEY
&&
798 exist
->owner
== key
.offset
) ||
799 (key
.type
== BTRFS_SHARED_BLOCK_REF_KEY
&&
800 exist
->bytenr
== key
.offset
))) {
805 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
806 if (key
.type
== BTRFS_SHARED_BLOCK_REF_KEY
||
807 key
.type
== BTRFS_EXTENT_REF_V0_KEY
) {
808 if (key
.type
== BTRFS_EXTENT_REF_V0_KEY
) {
809 struct btrfs_extent_ref_v0
*ref0
;
810 ref0
= btrfs_item_ptr(eb
, path1
->slots
[0],
811 struct btrfs_extent_ref_v0
);
812 if (key
.objectid
== key
.offset
) {
813 root
= find_tree_root(rc
, eb
, ref0
);
814 if (root
&& !should_ignore_root(root
))
817 list_add(&cur
->list
, &useless
);
820 if (is_cowonly_root(btrfs_ref_root_v0(eb
,
825 BUG_ON(key
.type
== BTRFS_EXTENT_REF_V0_KEY
);
826 if (key
.type
== BTRFS_SHARED_BLOCK_REF_KEY
) {
828 if (key
.objectid
== key
.offset
) {
830 * only root blocks of reloc trees use
831 * backref of this type.
833 root
= find_reloc_root(rc
, cur
->bytenr
);
839 edge
= alloc_backref_edge(cache
);
844 rb_node
= tree_search(&cache
->rb_root
, key
.offset
);
846 upper
= alloc_backref_node(cache
);
848 free_backref_edge(cache
, edge
);
852 upper
->bytenr
= key
.offset
;
853 upper
->level
= cur
->level
+ 1;
855 * backrefs for the upper level block isn't
856 * cached, add the block to pending list
858 list_add_tail(&edge
->list
[UPPER
], &list
);
860 upper
= rb_entry(rb_node
, struct backref_node
,
862 BUG_ON(!upper
->checked
);
863 INIT_LIST_HEAD(&edge
->list
[UPPER
]);
865 list_add_tail(&edge
->list
[LOWER
], &cur
->upper
);
866 edge
->node
[LOWER
] = cur
;
867 edge
->node
[UPPER
] = upper
;
870 } else if (key
.type
!= BTRFS_TREE_BLOCK_REF_KEY
) {
874 /* key.type == BTRFS_TREE_BLOCK_REF_KEY */
875 root
= read_fs_root(rc
->extent_root
->fs_info
, key
.offset
);
884 if (btrfs_root_level(&root
->root_item
) == cur
->level
) {
886 BUG_ON(btrfs_root_bytenr(&root
->root_item
) !=
888 if (should_ignore_root(root
))
889 list_add(&cur
->list
, &useless
);
895 level
= cur
->level
+ 1;
898 * searching the tree to find upper level blocks
899 * reference the block.
901 path2
->search_commit_root
= 1;
902 path2
->skip_locking
= 1;
903 path2
->lowest_level
= level
;
904 ret
= btrfs_search_slot(NULL
, root
, node_key
, path2
, 0, 0);
905 path2
->lowest_level
= 0;
910 if (ret
> 0 && path2
->slots
[level
] > 0)
911 path2
->slots
[level
]--;
913 eb
= path2
->nodes
[level
];
914 WARN_ON(btrfs_node_blockptr(eb
, path2
->slots
[level
]) !=
919 for (; level
< BTRFS_MAX_LEVEL
; level
++) {
920 if (!path2
->nodes
[level
]) {
921 BUG_ON(btrfs_root_bytenr(&root
->root_item
) !=
923 if (should_ignore_root(root
))
924 list_add(&lower
->list
, &useless
);
930 edge
= alloc_backref_edge(cache
);
936 eb
= path2
->nodes
[level
];
937 rb_node
= tree_search(&cache
->rb_root
, eb
->start
);
939 upper
= alloc_backref_node(cache
);
941 free_backref_edge(cache
, edge
);
945 upper
->bytenr
= eb
->start
;
946 upper
->owner
= btrfs_header_owner(eb
);
947 upper
->level
= lower
->level
+ 1;
952 * if we know the block isn't shared
953 * we can void checking its backrefs.
955 if (btrfs_block_can_be_shared(root
, eb
))
961 * add the block to pending list if we
962 * need check its backrefs, we only do this once
963 * while walking up a tree as we will catch
964 * anything else later on.
966 if (!upper
->checked
&& need_check
) {
968 list_add_tail(&edge
->list
[UPPER
],
973 INIT_LIST_HEAD(&edge
->list
[UPPER
]);
976 upper
= rb_entry(rb_node
, struct backref_node
,
978 BUG_ON(!upper
->checked
);
979 INIT_LIST_HEAD(&edge
->list
[UPPER
]);
981 upper
->owner
= btrfs_header_owner(eb
);
983 list_add_tail(&edge
->list
[LOWER
], &lower
->upper
);
984 edge
->node
[LOWER
] = lower
;
985 edge
->node
[UPPER
] = upper
;
992 btrfs_release_path(path2
);
995 ptr
+= btrfs_extent_inline_ref_size(key
.type
);
1005 btrfs_release_path(path1
);
1010 /* the pending list isn't empty, take the first block to process */
1011 if (!list_empty(&list
)) {
1012 edge
= list_entry(list
.next
, struct backref_edge
, list
[UPPER
]);
1013 list_del_init(&edge
->list
[UPPER
]);
1014 cur
= edge
->node
[UPPER
];
1019 * everything goes well, connect backref nodes and insert backref nodes
1022 BUG_ON(!node
->checked
);
1023 cowonly
= node
->cowonly
;
1025 rb_node
= tree_insert(&cache
->rb_root
, node
->bytenr
,
1028 backref_tree_panic(rb_node
, -EEXIST
, node
->bytenr
);
1029 list_add_tail(&node
->lower
, &cache
->leaves
);
1032 list_for_each_entry(edge
, &node
->upper
, list
[LOWER
])
1033 list_add_tail(&edge
->list
[UPPER
], &list
);
1035 while (!list_empty(&list
)) {
1036 edge
= list_entry(list
.next
, struct backref_edge
, list
[UPPER
]);
1037 list_del_init(&edge
->list
[UPPER
]);
1038 upper
= edge
->node
[UPPER
];
1039 if (upper
->detached
) {
1040 list_del(&edge
->list
[LOWER
]);
1041 lower
= edge
->node
[LOWER
];
1042 free_backref_edge(cache
, edge
);
1043 if (list_empty(&lower
->upper
))
1044 list_add(&lower
->list
, &useless
);
1048 if (!RB_EMPTY_NODE(&upper
->rb_node
)) {
1049 if (upper
->lowest
) {
1050 list_del_init(&upper
->lower
);
1054 list_add_tail(&edge
->list
[UPPER
], &upper
->lower
);
1058 BUG_ON(!upper
->checked
);
1059 BUG_ON(cowonly
!= upper
->cowonly
);
1061 rb_node
= tree_insert(&cache
->rb_root
, upper
->bytenr
,
1064 backref_tree_panic(rb_node
, -EEXIST
,
1068 list_add_tail(&edge
->list
[UPPER
], &upper
->lower
);
1070 list_for_each_entry(edge
, &upper
->upper
, list
[LOWER
])
1071 list_add_tail(&edge
->list
[UPPER
], &list
);
1074 * process useless backref nodes. backref nodes for tree leaves
1075 * are deleted from the cache. backref nodes for upper level
1076 * tree blocks are left in the cache to avoid unnecessary backref
1079 while (!list_empty(&useless
)) {
1080 upper
= list_entry(useless
.next
, struct backref_node
, list
);
1081 list_del_init(&upper
->list
);
1082 BUG_ON(!list_empty(&upper
->upper
));
1085 if (upper
->lowest
) {
1086 list_del_init(&upper
->lower
);
1089 while (!list_empty(&upper
->lower
)) {
1090 edge
= list_entry(upper
->lower
.next
,
1091 struct backref_edge
, list
[UPPER
]);
1092 list_del(&edge
->list
[UPPER
]);
1093 list_del(&edge
->list
[LOWER
]);
1094 lower
= edge
->node
[LOWER
];
1095 free_backref_edge(cache
, edge
);
1097 if (list_empty(&lower
->upper
))
1098 list_add(&lower
->list
, &useless
);
1100 __mark_block_processed(rc
, upper
);
1101 if (upper
->level
> 0) {
1102 list_add(&upper
->list
, &cache
->detached
);
1103 upper
->detached
= 1;
1105 rb_erase(&upper
->rb_node
, &cache
->rb_root
);
1106 free_backref_node(cache
, upper
);
1110 btrfs_free_path(path1
);
1111 btrfs_free_path(path2
);
1113 while (!list_empty(&useless
)) {
1114 lower
= list_entry(useless
.next
,
1115 struct backref_node
, upper
);
1116 list_del_init(&lower
->upper
);
1119 INIT_LIST_HEAD(&list
);
1121 if (RB_EMPTY_NODE(&upper
->rb_node
)) {
1122 list_splice_tail(&upper
->upper
, &list
);
1123 free_backref_node(cache
, upper
);
1126 if (list_empty(&list
))
1129 edge
= list_entry(list
.next
, struct backref_edge
,
1131 list_del(&edge
->list
[LOWER
]);
1132 upper
= edge
->node
[UPPER
];
1133 free_backref_edge(cache
, edge
);
1135 return ERR_PTR(err
);
1137 BUG_ON(node
&& node
->detached
);
1142 * helper to add backref node for the newly created snapshot.
1143 * the backref node is created by cloning backref node that
1144 * corresponds to root of source tree
1146 static int clone_backref_node(struct btrfs_trans_handle
*trans
,
1147 struct reloc_control
*rc
,
1148 struct btrfs_root
*src
,
1149 struct btrfs_root
*dest
)
1151 struct btrfs_root
*reloc_root
= src
->reloc_root
;
1152 struct backref_cache
*cache
= &rc
->backref_cache
;
1153 struct backref_node
*node
= NULL
;
1154 struct backref_node
*new_node
;
1155 struct backref_edge
*edge
;
1156 struct backref_edge
*new_edge
;
1157 struct rb_node
*rb_node
;
1159 if (cache
->last_trans
> 0)
1160 update_backref_cache(trans
, cache
);
1162 rb_node
= tree_search(&cache
->rb_root
, src
->commit_root
->start
);
1164 node
= rb_entry(rb_node
, struct backref_node
, rb_node
);
1168 BUG_ON(node
->new_bytenr
!= reloc_root
->node
->start
);
1172 rb_node
= tree_search(&cache
->rb_root
,
1173 reloc_root
->commit_root
->start
);
1175 node
= rb_entry(rb_node
, struct backref_node
,
1177 BUG_ON(node
->detached
);
1184 new_node
= alloc_backref_node(cache
);
1188 new_node
->bytenr
= dest
->node
->start
;
1189 new_node
->level
= node
->level
;
1190 new_node
->lowest
= node
->lowest
;
1191 new_node
->checked
= 1;
1192 new_node
->root
= dest
;
1194 if (!node
->lowest
) {
1195 list_for_each_entry(edge
, &node
->lower
, list
[UPPER
]) {
1196 new_edge
= alloc_backref_edge(cache
);
1200 new_edge
->node
[UPPER
] = new_node
;
1201 new_edge
->node
[LOWER
] = edge
->node
[LOWER
];
1202 list_add_tail(&new_edge
->list
[UPPER
],
1206 list_add_tail(&new_node
->lower
, &cache
->leaves
);
1209 rb_node
= tree_insert(&cache
->rb_root
, new_node
->bytenr
,
1210 &new_node
->rb_node
);
1212 backref_tree_panic(rb_node
, -EEXIST
, new_node
->bytenr
);
1214 if (!new_node
->lowest
) {
1215 list_for_each_entry(new_edge
, &new_node
->lower
, list
[UPPER
]) {
1216 list_add_tail(&new_edge
->list
[LOWER
],
1217 &new_edge
->node
[LOWER
]->upper
);
1222 while (!list_empty(&new_node
->lower
)) {
1223 new_edge
= list_entry(new_node
->lower
.next
,
1224 struct backref_edge
, list
[UPPER
]);
1225 list_del(&new_edge
->list
[UPPER
]);
1226 free_backref_edge(cache
, new_edge
);
1228 free_backref_node(cache
, new_node
);
1233 * helper to add 'address of tree root -> reloc tree' mapping
1235 static int __must_check
__add_reloc_root(struct btrfs_root
*root
)
1237 struct rb_node
*rb_node
;
1238 struct mapping_node
*node
;
1239 struct reloc_control
*rc
= root
->fs_info
->reloc_ctl
;
1241 node
= kmalloc(sizeof(*node
), GFP_NOFS
);
1245 node
->bytenr
= root
->node
->start
;
1248 spin_lock(&rc
->reloc_root_tree
.lock
);
1249 rb_node
= tree_insert(&rc
->reloc_root_tree
.rb_root
,
1250 node
->bytenr
, &node
->rb_node
);
1251 spin_unlock(&rc
->reloc_root_tree
.lock
);
1253 btrfs_panic(root
->fs_info
, -EEXIST
, "Duplicate root found "
1254 "for start=%llu while inserting into relocation "
1255 "tree\n", node
->bytenr
);
1260 list_add_tail(&root
->root_list
, &rc
->reloc_roots
);
1265 * helper to update/delete the 'address of tree root -> reloc tree'
1268 static int __update_reloc_root(struct btrfs_root
*root
, int del
)
1270 struct rb_node
*rb_node
;
1271 struct mapping_node
*node
= NULL
;
1272 struct reloc_control
*rc
= root
->fs_info
->reloc_ctl
;
1274 spin_lock(&rc
->reloc_root_tree
.lock
);
1275 rb_node
= tree_search(&rc
->reloc_root_tree
.rb_root
,
1276 root
->commit_root
->start
);
1278 node
= rb_entry(rb_node
, struct mapping_node
, rb_node
);
1279 rb_erase(&node
->rb_node
, &rc
->reloc_root_tree
.rb_root
);
1281 spin_unlock(&rc
->reloc_root_tree
.lock
);
1285 BUG_ON((struct btrfs_root
*)node
->data
!= root
);
1288 spin_lock(&rc
->reloc_root_tree
.lock
);
1289 node
->bytenr
= root
->node
->start
;
1290 rb_node
= tree_insert(&rc
->reloc_root_tree
.rb_root
,
1291 node
->bytenr
, &node
->rb_node
);
1292 spin_unlock(&rc
->reloc_root_tree
.lock
);
1294 backref_tree_panic(rb_node
, -EEXIST
, node
->bytenr
);
1296 spin_lock(&root
->fs_info
->trans_lock
);
1297 list_del_init(&root
->root_list
);
1298 spin_unlock(&root
->fs_info
->trans_lock
);
1304 static struct btrfs_root
*create_reloc_root(struct btrfs_trans_handle
*trans
,
1305 struct btrfs_root
*root
, u64 objectid
)
1307 struct btrfs_root
*reloc_root
;
1308 struct extent_buffer
*eb
;
1309 struct btrfs_root_item
*root_item
;
1310 struct btrfs_key root_key
;
1313 root_item
= kmalloc(sizeof(*root_item
), GFP_NOFS
);
1316 root_key
.objectid
= BTRFS_TREE_RELOC_OBJECTID
;
1317 root_key
.type
= BTRFS_ROOT_ITEM_KEY
;
1318 root_key
.offset
= objectid
;
1320 if (root
->root_key
.objectid
== objectid
) {
1321 /* called by btrfs_init_reloc_root */
1322 ret
= btrfs_copy_root(trans
, root
, root
->commit_root
, &eb
,
1323 BTRFS_TREE_RELOC_OBJECTID
);
1326 btrfs_set_root_last_snapshot(&root
->root_item
,
1327 trans
->transid
- 1);
1330 * called by btrfs_reloc_post_snapshot_hook.
1331 * the source tree is a reloc tree, all tree blocks
1332 * modified after it was created have RELOC flag
1333 * set in their headers. so it's OK to not update
1334 * the 'last_snapshot'.
1336 ret
= btrfs_copy_root(trans
, root
, root
->node
, &eb
,
1337 BTRFS_TREE_RELOC_OBJECTID
);
1341 memcpy(root_item
, &root
->root_item
, sizeof(*root_item
));
1342 btrfs_set_root_bytenr(root_item
, eb
->start
);
1343 btrfs_set_root_level(root_item
, btrfs_header_level(eb
));
1344 btrfs_set_root_generation(root_item
, trans
->transid
);
1346 if (root
->root_key
.objectid
== objectid
) {
1347 btrfs_set_root_refs(root_item
, 0);
1348 memset(&root_item
->drop_progress
, 0,
1349 sizeof(struct btrfs_disk_key
));
1350 root_item
->drop_level
= 0;
1353 btrfs_tree_unlock(eb
);
1354 free_extent_buffer(eb
);
1356 ret
= btrfs_insert_root(trans
, root
->fs_info
->tree_root
,
1357 &root_key
, root_item
);
1361 reloc_root
= btrfs_read_fs_root_no_radix(root
->fs_info
->tree_root
,
1363 BUG_ON(IS_ERR(reloc_root
));
1364 reloc_root
->last_trans
= trans
->transid
;
1369 * create reloc tree for a given fs tree. reloc tree is just a
1370 * snapshot of the fs tree with special root objectid.
1372 int btrfs_init_reloc_root(struct btrfs_trans_handle
*trans
,
1373 struct btrfs_root
*root
)
1375 struct btrfs_root
*reloc_root
;
1376 struct reloc_control
*rc
= root
->fs_info
->reloc_ctl
;
1380 if (root
->reloc_root
) {
1381 reloc_root
= root
->reloc_root
;
1382 reloc_root
->last_trans
= trans
->transid
;
1386 if (!rc
|| !rc
->create_reloc_tree
||
1387 root
->root_key
.objectid
== BTRFS_TREE_RELOC_OBJECTID
)
1390 if (!trans
->block_rsv
) {
1391 trans
->block_rsv
= rc
->block_rsv
;
1394 reloc_root
= create_reloc_root(trans
, root
, root
->root_key
.objectid
);
1396 trans
->block_rsv
= NULL
;
1398 ret
= __add_reloc_root(reloc_root
);
1400 root
->reloc_root
= reloc_root
;
1405 * update root item of reloc tree
1407 int btrfs_update_reloc_root(struct btrfs_trans_handle
*trans
,
1408 struct btrfs_root
*root
)
1410 struct btrfs_root
*reloc_root
;
1411 struct btrfs_root_item
*root_item
;
1415 if (!root
->reloc_root
)
1418 reloc_root
= root
->reloc_root
;
1419 root_item
= &reloc_root
->root_item
;
1421 if (root
->fs_info
->reloc_ctl
->merge_reloc_tree
&&
1422 btrfs_root_refs(root_item
) == 0) {
1423 root
->reloc_root
= NULL
;
1427 __update_reloc_root(reloc_root
, del
);
1429 if (reloc_root
->commit_root
!= reloc_root
->node
) {
1430 btrfs_set_root_node(root_item
, reloc_root
->node
);
1431 free_extent_buffer(reloc_root
->commit_root
);
1432 reloc_root
->commit_root
= btrfs_root_node(reloc_root
);
1435 ret
= btrfs_update_root(trans
, root
->fs_info
->tree_root
,
1436 &reloc_root
->root_key
, root_item
);
1444 * helper to find first cached inode with inode number >= objectid
1447 static struct inode
*find_next_inode(struct btrfs_root
*root
, u64 objectid
)
1449 struct rb_node
*node
;
1450 struct rb_node
*prev
;
1451 struct btrfs_inode
*entry
;
1452 struct inode
*inode
;
1454 spin_lock(&root
->inode_lock
);
1456 node
= root
->inode_tree
.rb_node
;
1460 entry
= rb_entry(node
, struct btrfs_inode
, rb_node
);
1462 if (objectid
< btrfs_ino(&entry
->vfs_inode
))
1463 node
= node
->rb_left
;
1464 else if (objectid
> btrfs_ino(&entry
->vfs_inode
))
1465 node
= node
->rb_right
;
1471 entry
= rb_entry(prev
, struct btrfs_inode
, rb_node
);
1472 if (objectid
<= btrfs_ino(&entry
->vfs_inode
)) {
1476 prev
= rb_next(prev
);
1480 entry
= rb_entry(node
, struct btrfs_inode
, rb_node
);
1481 inode
= igrab(&entry
->vfs_inode
);
1483 spin_unlock(&root
->inode_lock
);
1487 objectid
= btrfs_ino(&entry
->vfs_inode
) + 1;
1488 if (cond_resched_lock(&root
->inode_lock
))
1491 node
= rb_next(node
);
1493 spin_unlock(&root
->inode_lock
);
1497 static int in_block_group(u64 bytenr
,
1498 struct btrfs_block_group_cache
*block_group
)
1500 if (bytenr
>= block_group
->key
.objectid
&&
1501 bytenr
< block_group
->key
.objectid
+ block_group
->key
.offset
)
1507 * get new location of data
1509 static int get_new_location(struct inode
*reloc_inode
, u64
*new_bytenr
,
1510 u64 bytenr
, u64 num_bytes
)
1512 struct btrfs_root
*root
= BTRFS_I(reloc_inode
)->root
;
1513 struct btrfs_path
*path
;
1514 struct btrfs_file_extent_item
*fi
;
1515 struct extent_buffer
*leaf
;
1518 path
= btrfs_alloc_path();
1522 bytenr
-= BTRFS_I(reloc_inode
)->index_cnt
;
1523 ret
= btrfs_lookup_file_extent(NULL
, root
, path
, btrfs_ino(reloc_inode
),
1532 leaf
= path
->nodes
[0];
1533 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
1534 struct btrfs_file_extent_item
);
1536 BUG_ON(btrfs_file_extent_offset(leaf
, fi
) ||
1537 btrfs_file_extent_compression(leaf
, fi
) ||
1538 btrfs_file_extent_encryption(leaf
, fi
) ||
1539 btrfs_file_extent_other_encoding(leaf
, fi
));
1541 if (num_bytes
!= btrfs_file_extent_disk_num_bytes(leaf
, fi
)) {
1546 *new_bytenr
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
1549 btrfs_free_path(path
);
1554 * update file extent items in the tree leaf to point to
1555 * the new locations.
1557 static noinline_for_stack
1558 int replace_file_extents(struct btrfs_trans_handle
*trans
,
1559 struct reloc_control
*rc
,
1560 struct btrfs_root
*root
,
1561 struct extent_buffer
*leaf
)
1563 struct btrfs_key key
;
1564 struct btrfs_file_extent_item
*fi
;
1565 struct inode
*inode
= NULL
;
1577 if (rc
->stage
!= UPDATE_DATA_PTRS
)
1580 /* reloc trees always use full backref */
1581 if (root
->root_key
.objectid
== BTRFS_TREE_RELOC_OBJECTID
)
1582 parent
= leaf
->start
;
1586 nritems
= btrfs_header_nritems(leaf
);
1587 for (i
= 0; i
< nritems
; i
++) {
1589 btrfs_item_key_to_cpu(leaf
, &key
, i
);
1590 if (key
.type
!= BTRFS_EXTENT_DATA_KEY
)
1592 fi
= btrfs_item_ptr(leaf
, i
, struct btrfs_file_extent_item
);
1593 if (btrfs_file_extent_type(leaf
, fi
) ==
1594 BTRFS_FILE_EXTENT_INLINE
)
1596 bytenr
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
1597 num_bytes
= btrfs_file_extent_disk_num_bytes(leaf
, fi
);
1600 if (!in_block_group(bytenr
, rc
->block_group
))
1604 * if we are modifying block in fs tree, wait for readpage
1605 * to complete and drop the extent cache
1607 if (root
->root_key
.objectid
!= BTRFS_TREE_RELOC_OBJECTID
) {
1609 inode
= find_next_inode(root
, key
.objectid
);
1611 } else if (inode
&& btrfs_ino(inode
) < key
.objectid
) {
1612 btrfs_add_delayed_iput(inode
);
1613 inode
= find_next_inode(root
, key
.objectid
);
1615 if (inode
&& btrfs_ino(inode
) == key
.objectid
) {
1617 btrfs_file_extent_num_bytes(leaf
, fi
);
1618 WARN_ON(!IS_ALIGNED(key
.offset
,
1620 WARN_ON(!IS_ALIGNED(end
, root
->sectorsize
));
1622 ret
= try_lock_extent(&BTRFS_I(inode
)->io_tree
,
1627 btrfs_drop_extent_cache(inode
, key
.offset
, end
,
1629 unlock_extent(&BTRFS_I(inode
)->io_tree
,
1634 ret
= get_new_location(rc
->data_inode
, &new_bytenr
,
1642 btrfs_set_file_extent_disk_bytenr(leaf
, fi
, new_bytenr
);
1645 key
.offset
-= btrfs_file_extent_offset(leaf
, fi
);
1646 ret
= btrfs_inc_extent_ref(trans
, root
, new_bytenr
,
1648 btrfs_header_owner(leaf
),
1649 key
.objectid
, key
.offset
, 1);
1652 ret
= btrfs_free_extent(trans
, root
, bytenr
, num_bytes
,
1653 parent
, btrfs_header_owner(leaf
),
1654 key
.objectid
, key
.offset
, 1);
1658 btrfs_mark_buffer_dirty(leaf
);
1660 btrfs_add_delayed_iput(inode
);
1664 static noinline_for_stack
1665 int memcmp_node_keys(struct extent_buffer
*eb
, int slot
,
1666 struct btrfs_path
*path
, int level
)
1668 struct btrfs_disk_key key1
;
1669 struct btrfs_disk_key key2
;
1670 btrfs_node_key(eb
, &key1
, slot
);
1671 btrfs_node_key(path
->nodes
[level
], &key2
, path
->slots
[level
]);
1672 return memcmp(&key1
, &key2
, sizeof(key1
));
1676 * try to replace tree blocks in fs tree with the new blocks
1677 * in reloc tree. tree blocks haven't been modified since the
1678 * reloc tree was create can be replaced.
1680 * if a block was replaced, level of the block + 1 is returned.
1681 * if no block got replaced, 0 is returned. if there are other
1682 * errors, a negative error number is returned.
1684 static noinline_for_stack
1685 int replace_path(struct btrfs_trans_handle
*trans
,
1686 struct btrfs_root
*dest
, struct btrfs_root
*src
,
1687 struct btrfs_path
*path
, struct btrfs_key
*next_key
,
1688 int lowest_level
, int max_level
)
1690 struct extent_buffer
*eb
;
1691 struct extent_buffer
*parent
;
1692 struct btrfs_key key
;
1704 BUG_ON(src
->root_key
.objectid
!= BTRFS_TREE_RELOC_OBJECTID
);
1705 BUG_ON(dest
->root_key
.objectid
== BTRFS_TREE_RELOC_OBJECTID
);
1707 last_snapshot
= btrfs_root_last_snapshot(&src
->root_item
);
1709 slot
= path
->slots
[lowest_level
];
1710 btrfs_node_key_to_cpu(path
->nodes
[lowest_level
], &key
, slot
);
1712 eb
= btrfs_lock_root_node(dest
);
1713 btrfs_set_lock_blocking(eb
);
1714 level
= btrfs_header_level(eb
);
1716 if (level
< lowest_level
) {
1717 btrfs_tree_unlock(eb
);
1718 free_extent_buffer(eb
);
1723 ret
= btrfs_cow_block(trans
, dest
, eb
, NULL
, 0, &eb
);
1726 btrfs_set_lock_blocking(eb
);
1729 next_key
->objectid
= (u64
)-1;
1730 next_key
->type
= (u8
)-1;
1731 next_key
->offset
= (u64
)-1;
1736 level
= btrfs_header_level(parent
);
1737 BUG_ON(level
< lowest_level
);
1739 ret
= btrfs_bin_search(parent
, &key
, level
, &slot
);
1740 if (ret
&& slot
> 0)
1743 if (next_key
&& slot
+ 1 < btrfs_header_nritems(parent
))
1744 btrfs_node_key_to_cpu(parent
, next_key
, slot
+ 1);
1746 old_bytenr
= btrfs_node_blockptr(parent
, slot
);
1747 blocksize
= btrfs_level_size(dest
, level
- 1);
1748 old_ptr_gen
= btrfs_node_ptr_generation(parent
, slot
);
1750 if (level
<= max_level
) {
1751 eb
= path
->nodes
[level
];
1752 new_bytenr
= btrfs_node_blockptr(eb
,
1753 path
->slots
[level
]);
1754 new_ptr_gen
= btrfs_node_ptr_generation(eb
,
1755 path
->slots
[level
]);
1761 if (new_bytenr
> 0 && new_bytenr
== old_bytenr
) {
1767 if (new_bytenr
== 0 || old_ptr_gen
> last_snapshot
||
1768 memcmp_node_keys(parent
, slot
, path
, level
)) {
1769 if (level
<= lowest_level
) {
1774 eb
= read_tree_block(dest
, old_bytenr
, blocksize
,
1776 if (!eb
|| !extent_buffer_uptodate(eb
)) {
1777 ret
= (!eb
) ? -ENOMEM
: -EIO
;
1778 free_extent_buffer(eb
);
1781 btrfs_tree_lock(eb
);
1783 ret
= btrfs_cow_block(trans
, dest
, eb
, parent
,
1787 btrfs_set_lock_blocking(eb
);
1789 btrfs_tree_unlock(parent
);
1790 free_extent_buffer(parent
);
1797 btrfs_tree_unlock(parent
);
1798 free_extent_buffer(parent
);
1803 btrfs_node_key_to_cpu(path
->nodes
[level
], &key
,
1804 path
->slots
[level
]);
1805 btrfs_release_path(path
);
1807 path
->lowest_level
= level
;
1808 ret
= btrfs_search_slot(trans
, src
, &key
, path
, 0, 1);
1809 path
->lowest_level
= 0;
1813 * swap blocks in fs tree and reloc tree.
1815 btrfs_set_node_blockptr(parent
, slot
, new_bytenr
);
1816 btrfs_set_node_ptr_generation(parent
, slot
, new_ptr_gen
);
1817 btrfs_mark_buffer_dirty(parent
);
1819 btrfs_set_node_blockptr(path
->nodes
[level
],
1820 path
->slots
[level
], old_bytenr
);
1821 btrfs_set_node_ptr_generation(path
->nodes
[level
],
1822 path
->slots
[level
], old_ptr_gen
);
1823 btrfs_mark_buffer_dirty(path
->nodes
[level
]);
1825 ret
= btrfs_inc_extent_ref(trans
, src
, old_bytenr
, blocksize
,
1826 path
->nodes
[level
]->start
,
1827 src
->root_key
.objectid
, level
- 1, 0,
1830 ret
= btrfs_inc_extent_ref(trans
, dest
, new_bytenr
, blocksize
,
1831 0, dest
->root_key
.objectid
, level
- 1,
1835 ret
= btrfs_free_extent(trans
, src
, new_bytenr
, blocksize
,
1836 path
->nodes
[level
]->start
,
1837 src
->root_key
.objectid
, level
- 1, 0,
1841 ret
= btrfs_free_extent(trans
, dest
, old_bytenr
, blocksize
,
1842 0, dest
->root_key
.objectid
, level
- 1,
1846 btrfs_unlock_up_safe(path
, 0);
1851 btrfs_tree_unlock(parent
);
1852 free_extent_buffer(parent
);
1857 * helper to find next relocated block in reloc tree
1859 static noinline_for_stack
1860 int walk_up_reloc_tree(struct btrfs_root
*root
, struct btrfs_path
*path
,
1863 struct extent_buffer
*eb
;
1868 last_snapshot
= btrfs_root_last_snapshot(&root
->root_item
);
1870 for (i
= 0; i
< *level
; i
++) {
1871 free_extent_buffer(path
->nodes
[i
]);
1872 path
->nodes
[i
] = NULL
;
1875 for (i
= *level
; i
< BTRFS_MAX_LEVEL
&& path
->nodes
[i
]; i
++) {
1876 eb
= path
->nodes
[i
];
1877 nritems
= btrfs_header_nritems(eb
);
1878 while (path
->slots
[i
] + 1 < nritems
) {
1880 if (btrfs_node_ptr_generation(eb
, path
->slots
[i
]) <=
1887 free_extent_buffer(path
->nodes
[i
]);
1888 path
->nodes
[i
] = NULL
;
1894 * walk down reloc tree to find relocated block of lowest level
1896 static noinline_for_stack
1897 int walk_down_reloc_tree(struct btrfs_root
*root
, struct btrfs_path
*path
,
1900 struct extent_buffer
*eb
= NULL
;
1908 last_snapshot
= btrfs_root_last_snapshot(&root
->root_item
);
1910 for (i
= *level
; i
> 0; i
--) {
1911 eb
= path
->nodes
[i
];
1912 nritems
= btrfs_header_nritems(eb
);
1913 while (path
->slots
[i
] < nritems
) {
1914 ptr_gen
= btrfs_node_ptr_generation(eb
, path
->slots
[i
]);
1915 if (ptr_gen
> last_snapshot
)
1919 if (path
->slots
[i
] >= nritems
) {
1930 bytenr
= btrfs_node_blockptr(eb
, path
->slots
[i
]);
1931 blocksize
= btrfs_level_size(root
, i
- 1);
1932 eb
= read_tree_block(root
, bytenr
, blocksize
, ptr_gen
);
1933 if (!eb
|| !extent_buffer_uptodate(eb
)) {
1934 free_extent_buffer(eb
);
1937 BUG_ON(btrfs_header_level(eb
) != i
- 1);
1938 path
->nodes
[i
- 1] = eb
;
1939 path
->slots
[i
- 1] = 0;
1945 * invalidate extent cache for file extents whose key in range of
1946 * [min_key, max_key)
1948 static int invalidate_extent_cache(struct btrfs_root
*root
,
1949 struct btrfs_key
*min_key
,
1950 struct btrfs_key
*max_key
)
1952 struct inode
*inode
= NULL
;
1957 objectid
= min_key
->objectid
;
1962 if (objectid
> max_key
->objectid
)
1965 inode
= find_next_inode(root
, objectid
);
1968 ino
= btrfs_ino(inode
);
1970 if (ino
> max_key
->objectid
) {
1976 if (!S_ISREG(inode
->i_mode
))
1979 if (unlikely(min_key
->objectid
== ino
)) {
1980 if (min_key
->type
> BTRFS_EXTENT_DATA_KEY
)
1982 if (min_key
->type
< BTRFS_EXTENT_DATA_KEY
)
1985 start
= min_key
->offset
;
1986 WARN_ON(!IS_ALIGNED(start
, root
->sectorsize
));
1992 if (unlikely(max_key
->objectid
== ino
)) {
1993 if (max_key
->type
< BTRFS_EXTENT_DATA_KEY
)
1995 if (max_key
->type
> BTRFS_EXTENT_DATA_KEY
) {
1998 if (max_key
->offset
== 0)
2000 end
= max_key
->offset
;
2001 WARN_ON(!IS_ALIGNED(end
, root
->sectorsize
));
2008 /* the lock_extent waits for readpage to complete */
2009 lock_extent(&BTRFS_I(inode
)->io_tree
, start
, end
);
2010 btrfs_drop_extent_cache(inode
, start
, end
, 1);
2011 unlock_extent(&BTRFS_I(inode
)->io_tree
, start
, end
);
2016 static int find_next_key(struct btrfs_path
*path
, int level
,
2017 struct btrfs_key
*key
)
2020 while (level
< BTRFS_MAX_LEVEL
) {
2021 if (!path
->nodes
[level
])
2023 if (path
->slots
[level
] + 1 <
2024 btrfs_header_nritems(path
->nodes
[level
])) {
2025 btrfs_node_key_to_cpu(path
->nodes
[level
], key
,
2026 path
->slots
[level
] + 1);
2035 * merge the relocated tree blocks in reloc tree with corresponding
2038 static noinline_for_stack
int merge_reloc_root(struct reloc_control
*rc
,
2039 struct btrfs_root
*root
)
2041 LIST_HEAD(inode_list
);
2042 struct btrfs_key key
;
2043 struct btrfs_key next_key
;
2044 struct btrfs_trans_handle
*trans
;
2045 struct btrfs_root
*reloc_root
;
2046 struct btrfs_root_item
*root_item
;
2047 struct btrfs_path
*path
;
2048 struct extent_buffer
*leaf
;
2056 path
= btrfs_alloc_path();
2061 reloc_root
= root
->reloc_root
;
2062 root_item
= &reloc_root
->root_item
;
2064 if (btrfs_disk_key_objectid(&root_item
->drop_progress
) == 0) {
2065 level
= btrfs_root_level(root_item
);
2066 extent_buffer_get(reloc_root
->node
);
2067 path
->nodes
[level
] = reloc_root
->node
;
2068 path
->slots
[level
] = 0;
2070 btrfs_disk_key_to_cpu(&key
, &root_item
->drop_progress
);
2072 level
= root_item
->drop_level
;
2074 path
->lowest_level
= level
;
2075 ret
= btrfs_search_slot(NULL
, reloc_root
, &key
, path
, 0, 0);
2076 path
->lowest_level
= 0;
2078 btrfs_free_path(path
);
2082 btrfs_node_key_to_cpu(path
->nodes
[level
], &next_key
,
2083 path
->slots
[level
]);
2084 WARN_ON(memcmp(&key
, &next_key
, sizeof(key
)));
2086 btrfs_unlock_up_safe(path
, 0);
2089 min_reserved
= root
->nodesize
* (BTRFS_MAX_LEVEL
- 1) * 2;
2090 memset(&next_key
, 0, sizeof(next_key
));
2093 trans
= btrfs_start_transaction(root
, 0);
2094 BUG_ON(IS_ERR(trans
));
2095 trans
->block_rsv
= rc
->block_rsv
;
2097 ret
= btrfs_block_rsv_refill(root
, rc
->block_rsv
, min_reserved
,
2098 BTRFS_RESERVE_FLUSH_ALL
);
2100 BUG_ON(ret
!= -EAGAIN
);
2101 ret
= btrfs_commit_transaction(trans
, root
);
2109 ret
= walk_down_reloc_tree(reloc_root
, path
, &level
);
2117 if (!find_next_key(path
, level
, &key
) &&
2118 btrfs_comp_cpu_keys(&next_key
, &key
) >= 0) {
2121 ret
= replace_path(trans
, root
, reloc_root
, path
,
2122 &next_key
, level
, max_level
);
2131 btrfs_node_key_to_cpu(path
->nodes
[level
], &key
,
2132 path
->slots
[level
]);
2136 ret
= walk_up_reloc_tree(reloc_root
, path
, &level
);
2142 * save the merging progress in the drop_progress.
2143 * this is OK since root refs == 1 in this case.
2145 btrfs_node_key(path
->nodes
[level
], &root_item
->drop_progress
,
2146 path
->slots
[level
]);
2147 root_item
->drop_level
= level
;
2149 btrfs_end_transaction_throttle(trans
, root
);
2151 btrfs_btree_balance_dirty(root
);
2153 if (replaced
&& rc
->stage
== UPDATE_DATA_PTRS
)
2154 invalidate_extent_cache(root
, &key
, &next_key
);
2158 * handle the case only one block in the fs tree need to be
2159 * relocated and the block is tree root.
2161 leaf
= btrfs_lock_root_node(root
);
2162 ret
= btrfs_cow_block(trans
, root
, leaf
, NULL
, 0, &leaf
);
2163 btrfs_tree_unlock(leaf
);
2164 free_extent_buffer(leaf
);
2168 btrfs_free_path(path
);
2171 memset(&root_item
->drop_progress
, 0,
2172 sizeof(root_item
->drop_progress
));
2173 root_item
->drop_level
= 0;
2174 btrfs_set_root_refs(root_item
, 0);
2175 btrfs_update_reloc_root(trans
, root
);
2178 btrfs_end_transaction_throttle(trans
, root
);
2180 btrfs_btree_balance_dirty(root
);
2182 if (replaced
&& rc
->stage
== UPDATE_DATA_PTRS
)
2183 invalidate_extent_cache(root
, &key
, &next_key
);
2188 static noinline_for_stack
2189 int prepare_to_merge(struct reloc_control
*rc
, int err
)
2191 struct btrfs_root
*root
= rc
->extent_root
;
2192 struct btrfs_root
*reloc_root
;
2193 struct btrfs_trans_handle
*trans
;
2194 LIST_HEAD(reloc_roots
);
2198 mutex_lock(&root
->fs_info
->reloc_mutex
);
2199 rc
->merging_rsv_size
+= root
->nodesize
* (BTRFS_MAX_LEVEL
- 1) * 2;
2200 rc
->merging_rsv_size
+= rc
->nodes_relocated
* 2;
2201 mutex_unlock(&root
->fs_info
->reloc_mutex
);
2205 num_bytes
= rc
->merging_rsv_size
;
2206 ret
= btrfs_block_rsv_add(root
, rc
->block_rsv
, num_bytes
,
2207 BTRFS_RESERVE_FLUSH_ALL
);
2212 trans
= btrfs_join_transaction(rc
->extent_root
);
2213 if (IS_ERR(trans
)) {
2215 btrfs_block_rsv_release(rc
->extent_root
,
2216 rc
->block_rsv
, num_bytes
);
2217 return PTR_ERR(trans
);
2221 if (num_bytes
!= rc
->merging_rsv_size
) {
2222 btrfs_end_transaction(trans
, rc
->extent_root
);
2223 btrfs_block_rsv_release(rc
->extent_root
,
2224 rc
->block_rsv
, num_bytes
);
2229 rc
->merge_reloc_tree
= 1;
2231 while (!list_empty(&rc
->reloc_roots
)) {
2232 reloc_root
= list_entry(rc
->reloc_roots
.next
,
2233 struct btrfs_root
, root_list
);
2234 list_del_init(&reloc_root
->root_list
);
2236 root
= read_fs_root(reloc_root
->fs_info
,
2237 reloc_root
->root_key
.offset
);
2238 BUG_ON(IS_ERR(root
));
2239 BUG_ON(root
->reloc_root
!= reloc_root
);
2242 * set reference count to 1, so btrfs_recover_relocation
2243 * knows it should resumes merging
2246 btrfs_set_root_refs(&reloc_root
->root_item
, 1);
2247 btrfs_update_reloc_root(trans
, root
);
2249 list_add(&reloc_root
->root_list
, &reloc_roots
);
2252 list_splice(&reloc_roots
, &rc
->reloc_roots
);
2255 btrfs_commit_transaction(trans
, rc
->extent_root
);
2257 btrfs_end_transaction(trans
, rc
->extent_root
);
2261 static noinline_for_stack
2262 void free_reloc_roots(struct list_head
*list
)
2264 struct btrfs_root
*reloc_root
;
2266 while (!list_empty(list
)) {
2267 reloc_root
= list_entry(list
->next
, struct btrfs_root
,
2269 __update_reloc_root(reloc_root
, 1);
2270 free_extent_buffer(reloc_root
->node
);
2271 free_extent_buffer(reloc_root
->commit_root
);
2276 static noinline_for_stack
2277 int merge_reloc_roots(struct reloc_control
*rc
)
2279 struct btrfs_root
*root
;
2280 struct btrfs_root
*reloc_root
;
2281 LIST_HEAD(reloc_roots
);
2285 root
= rc
->extent_root
;
2288 * this serializes us with btrfs_record_root_in_transaction,
2289 * we have to make sure nobody is in the middle of
2290 * adding their roots to the list while we are
2293 mutex_lock(&root
->fs_info
->reloc_mutex
);
2294 list_splice_init(&rc
->reloc_roots
, &reloc_roots
);
2295 mutex_unlock(&root
->fs_info
->reloc_mutex
);
2297 while (!list_empty(&reloc_roots
)) {
2299 reloc_root
= list_entry(reloc_roots
.next
,
2300 struct btrfs_root
, root_list
);
2302 if (btrfs_root_refs(&reloc_root
->root_item
) > 0) {
2303 root
= read_fs_root(reloc_root
->fs_info
,
2304 reloc_root
->root_key
.offset
);
2305 BUG_ON(IS_ERR(root
));
2306 BUG_ON(root
->reloc_root
!= reloc_root
);
2308 ret
= merge_reloc_root(rc
, root
);
2312 list_del_init(&reloc_root
->root_list
);
2314 ret
= btrfs_drop_snapshot(reloc_root
, rc
->block_rsv
, 0, 1);
2316 if (list_empty(&reloc_root
->root_list
))
2317 list_add_tail(&reloc_root
->root_list
,
2329 btrfs_std_error(root
->fs_info
, ret
);
2330 if (!list_empty(&reloc_roots
))
2331 free_reloc_roots(&reloc_roots
);
2334 BUG_ON(!RB_EMPTY_ROOT(&rc
->reloc_root_tree
.rb_root
));
2338 static void free_block_list(struct rb_root
*blocks
)
2340 struct tree_block
*block
;
2341 struct rb_node
*rb_node
;
2342 while ((rb_node
= rb_first(blocks
))) {
2343 block
= rb_entry(rb_node
, struct tree_block
, rb_node
);
2344 rb_erase(rb_node
, blocks
);
2349 static int record_reloc_root_in_trans(struct btrfs_trans_handle
*trans
,
2350 struct btrfs_root
*reloc_root
)
2352 struct btrfs_root
*root
;
2354 if (reloc_root
->last_trans
== trans
->transid
)
2357 root
= read_fs_root(reloc_root
->fs_info
, reloc_root
->root_key
.offset
);
2358 BUG_ON(IS_ERR(root
));
2359 BUG_ON(root
->reloc_root
!= reloc_root
);
2361 return btrfs_record_root_in_trans(trans
, root
);
2364 static noinline_for_stack
2365 struct btrfs_root
*select_reloc_root(struct btrfs_trans_handle
*trans
,
2366 struct reloc_control
*rc
,
2367 struct backref_node
*node
,
2368 struct backref_edge
*edges
[], int *nr
)
2370 struct backref_node
*next
;
2371 struct btrfs_root
*root
;
2377 next
= walk_up_backref(next
, edges
, &index
);
2380 BUG_ON(!root
->ref_cows
);
2382 if (root
->root_key
.objectid
== BTRFS_TREE_RELOC_OBJECTID
) {
2383 record_reloc_root_in_trans(trans
, root
);
2387 btrfs_record_root_in_trans(trans
, root
);
2388 root
= root
->reloc_root
;
2390 if (next
->new_bytenr
!= root
->node
->start
) {
2391 BUG_ON(next
->new_bytenr
);
2392 BUG_ON(!list_empty(&next
->list
));
2393 next
->new_bytenr
= root
->node
->start
;
2395 list_add_tail(&next
->list
,
2396 &rc
->backref_cache
.changed
);
2397 __mark_block_processed(rc
, next
);
2403 next
= walk_down_backref(edges
, &index
);
2404 if (!next
|| next
->level
<= node
->level
)
2412 /* setup backref node path for btrfs_reloc_cow_block */
2414 rc
->backref_cache
.path
[next
->level
] = next
;
2417 next
= edges
[index
]->node
[UPPER
];
2423 * select a tree root for relocation. return NULL if the block
2424 * is reference counted. we should use do_relocation() in this
2425 * case. return a tree root pointer if the block isn't reference
2426 * counted. return -ENOENT if the block is root of reloc tree.
2428 static noinline_for_stack
2429 struct btrfs_root
*select_one_root(struct btrfs_trans_handle
*trans
,
2430 struct backref_node
*node
)
2432 struct backref_node
*next
;
2433 struct btrfs_root
*root
;
2434 struct btrfs_root
*fs_root
= NULL
;
2435 struct backref_edge
*edges
[BTRFS_MAX_LEVEL
- 1];
2441 next
= walk_up_backref(next
, edges
, &index
);
2445 /* no other choice for non-references counted tree */
2446 if (!root
->ref_cows
)
2449 if (root
->root_key
.objectid
!= BTRFS_TREE_RELOC_OBJECTID
)
2455 next
= walk_down_backref(edges
, &index
);
2456 if (!next
|| next
->level
<= node
->level
)
2461 return ERR_PTR(-ENOENT
);
2465 static noinline_for_stack
2466 u64
calcu_metadata_size(struct reloc_control
*rc
,
2467 struct backref_node
*node
, int reserve
)
2469 struct backref_node
*next
= node
;
2470 struct backref_edge
*edge
;
2471 struct backref_edge
*edges
[BTRFS_MAX_LEVEL
- 1];
2475 BUG_ON(reserve
&& node
->processed
);
2480 if (next
->processed
&& (reserve
|| next
!= node
))
2483 num_bytes
+= btrfs_level_size(rc
->extent_root
,
2486 if (list_empty(&next
->upper
))
2489 edge
= list_entry(next
->upper
.next
,
2490 struct backref_edge
, list
[LOWER
]);
2491 edges
[index
++] = edge
;
2492 next
= edge
->node
[UPPER
];
2494 next
= walk_down_backref(edges
, &index
);
2499 static int reserve_metadata_space(struct btrfs_trans_handle
*trans
,
2500 struct reloc_control
*rc
,
2501 struct backref_node
*node
)
2503 struct btrfs_root
*root
= rc
->extent_root
;
2507 num_bytes
= calcu_metadata_size(rc
, node
, 1) * 2;
2509 trans
->block_rsv
= rc
->block_rsv
;
2510 ret
= btrfs_block_rsv_add(root
, rc
->block_rsv
, num_bytes
,
2511 BTRFS_RESERVE_FLUSH_ALL
);
2514 rc
->commit_transaction
= 1;
2521 static void release_metadata_space(struct reloc_control
*rc
,
2522 struct backref_node
*node
)
2524 u64 num_bytes
= calcu_metadata_size(rc
, node
, 0) * 2;
2525 btrfs_block_rsv_release(rc
->extent_root
, rc
->block_rsv
, num_bytes
);
2529 * relocate a block tree, and then update pointers in upper level
2530 * blocks that reference the block to point to the new location.
2532 * if called by link_to_upper, the block has already been relocated.
2533 * in that case this function just updates pointers.
2535 static int do_relocation(struct btrfs_trans_handle
*trans
,
2536 struct reloc_control
*rc
,
2537 struct backref_node
*node
,
2538 struct btrfs_key
*key
,
2539 struct btrfs_path
*path
, int lowest
)
2541 struct backref_node
*upper
;
2542 struct backref_edge
*edge
;
2543 struct backref_edge
*edges
[BTRFS_MAX_LEVEL
- 1];
2544 struct btrfs_root
*root
;
2545 struct extent_buffer
*eb
;
2554 BUG_ON(lowest
&& node
->eb
);
2556 path
->lowest_level
= node
->level
+ 1;
2557 rc
->backref_cache
.path
[node
->level
] = node
;
2558 list_for_each_entry(edge
, &node
->upper
, list
[LOWER
]) {
2561 upper
= edge
->node
[UPPER
];
2562 root
= select_reloc_root(trans
, rc
, upper
, edges
, &nr
);
2565 if (upper
->eb
&& !upper
->locked
) {
2567 ret
= btrfs_bin_search(upper
->eb
, key
,
2568 upper
->level
, &slot
);
2570 bytenr
= btrfs_node_blockptr(upper
->eb
, slot
);
2571 if (node
->eb
->start
== bytenr
)
2574 drop_node_buffer(upper
);
2578 ret
= btrfs_search_slot(trans
, root
, key
, path
, 0, 1);
2586 upper
->eb
= path
->nodes
[upper
->level
];
2587 path
->nodes
[upper
->level
] = NULL
;
2589 BUG_ON(upper
->eb
!= path
->nodes
[upper
->level
]);
2593 path
->locks
[upper
->level
] = 0;
2595 slot
= path
->slots
[upper
->level
];
2596 btrfs_release_path(path
);
2598 ret
= btrfs_bin_search(upper
->eb
, key
, upper
->level
,
2603 bytenr
= btrfs_node_blockptr(upper
->eb
, slot
);
2605 BUG_ON(bytenr
!= node
->bytenr
);
2607 if (node
->eb
->start
== bytenr
)
2611 blocksize
= btrfs_level_size(root
, node
->level
);
2612 generation
= btrfs_node_ptr_generation(upper
->eb
, slot
);
2613 eb
= read_tree_block(root
, bytenr
, blocksize
, generation
);
2614 if (!eb
|| !extent_buffer_uptodate(eb
)) {
2615 free_extent_buffer(eb
);
2619 btrfs_tree_lock(eb
);
2620 btrfs_set_lock_blocking(eb
);
2623 ret
= btrfs_cow_block(trans
, root
, eb
, upper
->eb
,
2625 btrfs_tree_unlock(eb
);
2626 free_extent_buffer(eb
);
2631 BUG_ON(node
->eb
!= eb
);
2633 btrfs_set_node_blockptr(upper
->eb
, slot
,
2635 btrfs_set_node_ptr_generation(upper
->eb
, slot
,
2637 btrfs_mark_buffer_dirty(upper
->eb
);
2639 ret
= btrfs_inc_extent_ref(trans
, root
,
2640 node
->eb
->start
, blocksize
,
2642 btrfs_header_owner(upper
->eb
),
2646 ret
= btrfs_drop_subtree(trans
, root
, eb
, upper
->eb
);
2650 if (!upper
->pending
)
2651 drop_node_buffer(upper
);
2653 unlock_node_buffer(upper
);
2658 if (!err
&& node
->pending
) {
2659 drop_node_buffer(node
);
2660 list_move_tail(&node
->list
, &rc
->backref_cache
.changed
);
2664 path
->lowest_level
= 0;
2665 BUG_ON(err
== -ENOSPC
);
2669 static int link_to_upper(struct btrfs_trans_handle
*trans
,
2670 struct reloc_control
*rc
,
2671 struct backref_node
*node
,
2672 struct btrfs_path
*path
)
2674 struct btrfs_key key
;
2676 btrfs_node_key_to_cpu(node
->eb
, &key
, 0);
2677 return do_relocation(trans
, rc
, node
, &key
, path
, 0);
2680 static int finish_pending_nodes(struct btrfs_trans_handle
*trans
,
2681 struct reloc_control
*rc
,
2682 struct btrfs_path
*path
, int err
)
2685 struct backref_cache
*cache
= &rc
->backref_cache
;
2686 struct backref_node
*node
;
2690 for (level
= 0; level
< BTRFS_MAX_LEVEL
; level
++) {
2691 while (!list_empty(&cache
->pending
[level
])) {
2692 node
= list_entry(cache
->pending
[level
].next
,
2693 struct backref_node
, list
);
2694 list_move_tail(&node
->list
, &list
);
2695 BUG_ON(!node
->pending
);
2698 ret
= link_to_upper(trans
, rc
, node
, path
);
2703 list_splice_init(&list
, &cache
->pending
[level
]);
2708 static void mark_block_processed(struct reloc_control
*rc
,
2709 u64 bytenr
, u32 blocksize
)
2711 set_extent_bits(&rc
->processed_blocks
, bytenr
, bytenr
+ blocksize
- 1,
2712 EXTENT_DIRTY
, GFP_NOFS
);
2715 static void __mark_block_processed(struct reloc_control
*rc
,
2716 struct backref_node
*node
)
2719 if (node
->level
== 0 ||
2720 in_block_group(node
->bytenr
, rc
->block_group
)) {
2721 blocksize
= btrfs_level_size(rc
->extent_root
, node
->level
);
2722 mark_block_processed(rc
, node
->bytenr
, blocksize
);
2724 node
->processed
= 1;
2728 * mark a block and all blocks directly/indirectly reference the block
2731 static void update_processed_blocks(struct reloc_control
*rc
,
2732 struct backref_node
*node
)
2734 struct backref_node
*next
= node
;
2735 struct backref_edge
*edge
;
2736 struct backref_edge
*edges
[BTRFS_MAX_LEVEL
- 1];
2742 if (next
->processed
)
2745 __mark_block_processed(rc
, next
);
2747 if (list_empty(&next
->upper
))
2750 edge
= list_entry(next
->upper
.next
,
2751 struct backref_edge
, list
[LOWER
]);
2752 edges
[index
++] = edge
;
2753 next
= edge
->node
[UPPER
];
2755 next
= walk_down_backref(edges
, &index
);
2759 static int tree_block_processed(u64 bytenr
, u32 blocksize
,
2760 struct reloc_control
*rc
)
2762 if (test_range_bit(&rc
->processed_blocks
, bytenr
,
2763 bytenr
+ blocksize
- 1, EXTENT_DIRTY
, 1, NULL
))
2768 static int get_tree_block_key(struct reloc_control
*rc
,
2769 struct tree_block
*block
)
2771 struct extent_buffer
*eb
;
2773 BUG_ON(block
->key_ready
);
2774 eb
= read_tree_block(rc
->extent_root
, block
->bytenr
,
2775 block
->key
.objectid
, block
->key
.offset
);
2776 if (!eb
|| !extent_buffer_uptodate(eb
)) {
2777 free_extent_buffer(eb
);
2780 WARN_ON(btrfs_header_level(eb
) != block
->level
);
2781 if (block
->level
== 0)
2782 btrfs_item_key_to_cpu(eb
, &block
->key
, 0);
2784 btrfs_node_key_to_cpu(eb
, &block
->key
, 0);
2785 free_extent_buffer(eb
);
2786 block
->key_ready
= 1;
2790 static int reada_tree_block(struct reloc_control
*rc
,
2791 struct tree_block
*block
)
2793 BUG_ON(block
->key_ready
);
2794 if (block
->key
.type
== BTRFS_METADATA_ITEM_KEY
)
2795 readahead_tree_block(rc
->extent_root
, block
->bytenr
,
2796 block
->key
.objectid
,
2797 rc
->extent_root
->leafsize
);
2799 readahead_tree_block(rc
->extent_root
, block
->bytenr
,
2800 block
->key
.objectid
, block
->key
.offset
);
2805 * helper function to relocate a tree block
2807 static int relocate_tree_block(struct btrfs_trans_handle
*trans
,
2808 struct reloc_control
*rc
,
2809 struct backref_node
*node
,
2810 struct btrfs_key
*key
,
2811 struct btrfs_path
*path
)
2813 struct btrfs_root
*root
;
2820 BUG_ON(node
->processed
);
2821 root
= select_one_root(trans
, node
);
2822 if (root
== ERR_PTR(-ENOENT
)) {
2823 update_processed_blocks(rc
, node
);
2827 if (!root
|| root
->ref_cows
) {
2828 ret
= reserve_metadata_space(trans
, rc
, node
);
2835 if (root
->ref_cows
) {
2836 BUG_ON(node
->new_bytenr
);
2837 BUG_ON(!list_empty(&node
->list
));
2838 btrfs_record_root_in_trans(trans
, root
);
2839 root
= root
->reloc_root
;
2840 node
->new_bytenr
= root
->node
->start
;
2842 list_add_tail(&node
->list
, &rc
->backref_cache
.changed
);
2844 path
->lowest_level
= node
->level
;
2845 ret
= btrfs_search_slot(trans
, root
, key
, path
, 0, 1);
2846 btrfs_release_path(path
);
2851 update_processed_blocks(rc
, node
);
2853 ret
= do_relocation(trans
, rc
, node
, key
, path
, 1);
2856 if (ret
|| node
->level
== 0 || node
->cowonly
) {
2858 release_metadata_space(rc
, node
);
2859 remove_backref_node(&rc
->backref_cache
, node
);
2865 * relocate a list of blocks
2867 static noinline_for_stack
2868 int relocate_tree_blocks(struct btrfs_trans_handle
*trans
,
2869 struct reloc_control
*rc
, struct rb_root
*blocks
)
2871 struct backref_node
*node
;
2872 struct btrfs_path
*path
;
2873 struct tree_block
*block
;
2874 struct rb_node
*rb_node
;
2878 path
= btrfs_alloc_path();
2881 goto out_free_blocks
;
2884 rb_node
= rb_first(blocks
);
2886 block
= rb_entry(rb_node
, struct tree_block
, rb_node
);
2887 if (!block
->key_ready
)
2888 reada_tree_block(rc
, block
);
2889 rb_node
= rb_next(rb_node
);
2892 rb_node
= rb_first(blocks
);
2894 block
= rb_entry(rb_node
, struct tree_block
, rb_node
);
2895 if (!block
->key_ready
) {
2896 err
= get_tree_block_key(rc
, block
);
2900 rb_node
= rb_next(rb_node
);
2903 rb_node
= rb_first(blocks
);
2905 block
= rb_entry(rb_node
, struct tree_block
, rb_node
);
2907 node
= build_backref_tree(rc
, &block
->key
,
2908 block
->level
, block
->bytenr
);
2910 err
= PTR_ERR(node
);
2914 ret
= relocate_tree_block(trans
, rc
, node
, &block
->key
,
2917 if (ret
!= -EAGAIN
|| rb_node
== rb_first(blocks
))
2921 rb_node
= rb_next(rb_node
);
2924 err
= finish_pending_nodes(trans
, rc
, path
, err
);
2927 btrfs_free_path(path
);
2929 free_block_list(blocks
);
2933 static noinline_for_stack
2934 int prealloc_file_extent_cluster(struct inode
*inode
,
2935 struct file_extent_cluster
*cluster
)
2940 u64 offset
= BTRFS_I(inode
)->index_cnt
;
2945 BUG_ON(cluster
->start
!= cluster
->boundary
[0]);
2946 mutex_lock(&inode
->i_mutex
);
2948 ret
= btrfs_check_data_free_space(inode
, cluster
->end
+
2949 1 - cluster
->start
);
2953 while (nr
< cluster
->nr
) {
2954 start
= cluster
->boundary
[nr
] - offset
;
2955 if (nr
+ 1 < cluster
->nr
)
2956 end
= cluster
->boundary
[nr
+ 1] - 1 - offset
;
2958 end
= cluster
->end
- offset
;
2960 lock_extent(&BTRFS_I(inode
)->io_tree
, start
, end
);
2961 num_bytes
= end
+ 1 - start
;
2962 ret
= btrfs_prealloc_file_range(inode
, 0, start
,
2963 num_bytes
, num_bytes
,
2964 end
+ 1, &alloc_hint
);
2965 unlock_extent(&BTRFS_I(inode
)->io_tree
, start
, end
);
2970 btrfs_free_reserved_data_space(inode
, cluster
->end
+
2971 1 - cluster
->start
);
2973 mutex_unlock(&inode
->i_mutex
);
2977 static noinline_for_stack
2978 int setup_extent_mapping(struct inode
*inode
, u64 start
, u64 end
,
2981 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2982 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
2983 struct extent_map
*em
;
2986 em
= alloc_extent_map();
2991 em
->len
= end
+ 1 - start
;
2992 em
->block_len
= em
->len
;
2993 em
->block_start
= block_start
;
2994 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
2995 set_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
2997 lock_extent(&BTRFS_I(inode
)->io_tree
, start
, end
);
2999 write_lock(&em_tree
->lock
);
3000 ret
= add_extent_mapping(em_tree
, em
, 0);
3001 write_unlock(&em_tree
->lock
);
3002 if (ret
!= -EEXIST
) {
3003 free_extent_map(em
);
3006 btrfs_drop_extent_cache(inode
, start
, end
, 0);
3008 unlock_extent(&BTRFS_I(inode
)->io_tree
, start
, end
);
3012 static int relocate_file_extent_cluster(struct inode
*inode
,
3013 struct file_extent_cluster
*cluster
)
3017 u64 offset
= BTRFS_I(inode
)->index_cnt
;
3018 unsigned long index
;
3019 unsigned long last_index
;
3021 struct file_ra_state
*ra
;
3022 gfp_t mask
= btrfs_alloc_write_mask(inode
->i_mapping
);
3029 ra
= kzalloc(sizeof(*ra
), GFP_NOFS
);
3033 ret
= prealloc_file_extent_cluster(inode
, cluster
);
3037 file_ra_state_init(ra
, inode
->i_mapping
);
3039 ret
= setup_extent_mapping(inode
, cluster
->start
- offset
,
3040 cluster
->end
- offset
, cluster
->start
);
3044 index
= (cluster
->start
- offset
) >> PAGE_CACHE_SHIFT
;
3045 last_index
= (cluster
->end
- offset
) >> PAGE_CACHE_SHIFT
;
3046 while (index
<= last_index
) {
3047 ret
= btrfs_delalloc_reserve_metadata(inode
, PAGE_CACHE_SIZE
);
3051 page
= find_lock_page(inode
->i_mapping
, index
);
3053 page_cache_sync_readahead(inode
->i_mapping
,
3055 last_index
+ 1 - index
);
3056 page
= find_or_create_page(inode
->i_mapping
, index
,
3059 btrfs_delalloc_release_metadata(inode
,
3066 if (PageReadahead(page
)) {
3067 page_cache_async_readahead(inode
->i_mapping
,
3068 ra
, NULL
, page
, index
,
3069 last_index
+ 1 - index
);
3072 if (!PageUptodate(page
)) {
3073 btrfs_readpage(NULL
, page
);
3075 if (!PageUptodate(page
)) {
3077 page_cache_release(page
);
3078 btrfs_delalloc_release_metadata(inode
,
3085 page_start
= page_offset(page
);
3086 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
3088 lock_extent(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
);
3090 set_page_extent_mapped(page
);
3092 if (nr
< cluster
->nr
&&
3093 page_start
+ offset
== cluster
->boundary
[nr
]) {
3094 set_extent_bits(&BTRFS_I(inode
)->io_tree
,
3095 page_start
, page_end
,
3096 EXTENT_BOUNDARY
, GFP_NOFS
);
3100 btrfs_set_extent_delalloc(inode
, page_start
, page_end
, NULL
);
3101 set_page_dirty(page
);
3103 unlock_extent(&BTRFS_I(inode
)->io_tree
,
3104 page_start
, page_end
);
3106 page_cache_release(page
);
3109 balance_dirty_pages_ratelimited(inode
->i_mapping
);
3110 btrfs_throttle(BTRFS_I(inode
)->root
);
3112 WARN_ON(nr
!= cluster
->nr
);
3118 static noinline_for_stack
3119 int relocate_data_extent(struct inode
*inode
, struct btrfs_key
*extent_key
,
3120 struct file_extent_cluster
*cluster
)
3124 if (cluster
->nr
> 0 && extent_key
->objectid
!= cluster
->end
+ 1) {
3125 ret
= relocate_file_extent_cluster(inode
, cluster
);
3132 cluster
->start
= extent_key
->objectid
;
3134 BUG_ON(cluster
->nr
>= MAX_EXTENTS
);
3135 cluster
->end
= extent_key
->objectid
+ extent_key
->offset
- 1;
3136 cluster
->boundary
[cluster
->nr
] = extent_key
->objectid
;
3139 if (cluster
->nr
>= MAX_EXTENTS
) {
3140 ret
= relocate_file_extent_cluster(inode
, cluster
);
3148 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3149 static int get_ref_objectid_v0(struct reloc_control
*rc
,
3150 struct btrfs_path
*path
,
3151 struct btrfs_key
*extent_key
,
3152 u64
*ref_objectid
, int *path_change
)
3154 struct btrfs_key key
;
3155 struct extent_buffer
*leaf
;
3156 struct btrfs_extent_ref_v0
*ref0
;
3160 leaf
= path
->nodes
[0];
3161 slot
= path
->slots
[0];
3163 if (slot
>= btrfs_header_nritems(leaf
)) {
3164 ret
= btrfs_next_leaf(rc
->extent_root
, path
);
3168 leaf
= path
->nodes
[0];
3169 slot
= path
->slots
[0];
3173 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
3174 if (key
.objectid
!= extent_key
->objectid
)
3177 if (key
.type
!= BTRFS_EXTENT_REF_V0_KEY
) {
3181 ref0
= btrfs_item_ptr(leaf
, slot
,
3182 struct btrfs_extent_ref_v0
);
3183 *ref_objectid
= btrfs_ref_objectid_v0(leaf
, ref0
);
3191 * helper to add a tree block to the list.
3192 * the major work is getting the generation and level of the block
3194 static int add_tree_block(struct reloc_control
*rc
,
3195 struct btrfs_key
*extent_key
,
3196 struct btrfs_path
*path
,
3197 struct rb_root
*blocks
)
3199 struct extent_buffer
*eb
;
3200 struct btrfs_extent_item
*ei
;
3201 struct btrfs_tree_block_info
*bi
;
3202 struct tree_block
*block
;
3203 struct rb_node
*rb_node
;
3208 eb
= path
->nodes
[0];
3209 item_size
= btrfs_item_size_nr(eb
, path
->slots
[0]);
3211 if (extent_key
->type
== BTRFS_METADATA_ITEM_KEY
||
3212 item_size
>= sizeof(*ei
) + sizeof(*bi
)) {
3213 ei
= btrfs_item_ptr(eb
, path
->slots
[0],
3214 struct btrfs_extent_item
);
3215 if (extent_key
->type
== BTRFS_EXTENT_ITEM_KEY
) {
3216 bi
= (struct btrfs_tree_block_info
*)(ei
+ 1);
3217 level
= btrfs_tree_block_level(eb
, bi
);
3219 level
= (int)extent_key
->offset
;
3221 generation
= btrfs_extent_generation(eb
, ei
);
3223 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3227 BUG_ON(item_size
!= sizeof(struct btrfs_extent_item_v0
));
3228 ret
= get_ref_objectid_v0(rc
, path
, extent_key
,
3232 BUG_ON(ref_owner
>= BTRFS_MAX_LEVEL
);
3233 level
= (int)ref_owner
;
3234 /* FIXME: get real generation */
3241 btrfs_release_path(path
);
3243 BUG_ON(level
== -1);
3245 block
= kmalloc(sizeof(*block
), GFP_NOFS
);
3249 block
->bytenr
= extent_key
->objectid
;
3250 block
->key
.objectid
= rc
->extent_root
->leafsize
;
3251 block
->key
.offset
= generation
;
3252 block
->level
= level
;
3253 block
->key_ready
= 0;
3255 rb_node
= tree_insert(blocks
, block
->bytenr
, &block
->rb_node
);
3257 backref_tree_panic(rb_node
, -EEXIST
, block
->bytenr
);
3263 * helper to add tree blocks for backref of type BTRFS_SHARED_DATA_REF_KEY
3265 static int __add_tree_block(struct reloc_control
*rc
,
3266 u64 bytenr
, u32 blocksize
,
3267 struct rb_root
*blocks
)
3269 struct btrfs_path
*path
;
3270 struct btrfs_key key
;
3273 if (tree_block_processed(bytenr
, blocksize
, rc
))
3276 if (tree_search(blocks
, bytenr
))
3279 path
= btrfs_alloc_path();
3283 key
.objectid
= bytenr
;
3284 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
3285 key
.offset
= blocksize
;
3287 path
->search_commit_root
= 1;
3288 path
->skip_locking
= 1;
3289 ret
= btrfs_search_slot(NULL
, rc
->extent_root
, &key
, path
, 0, 0);
3293 btrfs_item_key_to_cpu(path
->nodes
[0], &key
, path
->slots
[0]);
3295 if (key
.objectid
== bytenr
&&
3296 key
.type
== BTRFS_METADATA_ITEM_KEY
)
3301 ret
= add_tree_block(rc
, &key
, path
, blocks
);
3303 btrfs_free_path(path
);
3308 * helper to check if the block use full backrefs for pointers in it
3310 static int block_use_full_backref(struct reloc_control
*rc
,
3311 struct extent_buffer
*eb
)
3316 if (btrfs_header_flag(eb
, BTRFS_HEADER_FLAG_RELOC
) ||
3317 btrfs_header_backref_rev(eb
) < BTRFS_MIXED_BACKREF_REV
)
3320 ret
= btrfs_lookup_extent_info(NULL
, rc
->extent_root
,
3321 eb
->start
, btrfs_header_level(eb
), 1,
3325 if (flags
& BTRFS_BLOCK_FLAG_FULL_BACKREF
)
3332 static int delete_block_group_cache(struct btrfs_fs_info
*fs_info
,
3333 struct inode
*inode
, u64 ino
)
3335 struct btrfs_key key
;
3336 struct btrfs_path
*path
;
3337 struct btrfs_root
*root
= fs_info
->tree_root
;
3338 struct btrfs_trans_handle
*trans
;
3345 key
.type
= BTRFS_INODE_ITEM_KEY
;
3348 inode
= btrfs_iget(fs_info
->sb
, &key
, root
, NULL
);
3349 if (IS_ERR(inode
) || is_bad_inode(inode
)) {
3356 ret
= btrfs_check_trunc_cache_free_space(root
,
3357 &fs_info
->global_block_rsv
);
3361 path
= btrfs_alloc_path();
3367 trans
= btrfs_join_transaction(root
);
3368 if (IS_ERR(trans
)) {
3369 btrfs_free_path(path
);
3370 ret
= PTR_ERR(trans
);
3374 ret
= btrfs_truncate_free_space_cache(root
, trans
, path
, inode
);
3376 btrfs_free_path(path
);
3377 btrfs_end_transaction(trans
, root
);
3378 btrfs_btree_balance_dirty(root
);
3385 * helper to add tree blocks for backref of type BTRFS_EXTENT_DATA_REF_KEY
3386 * this function scans fs tree to find blocks reference the data extent
3388 static int find_data_references(struct reloc_control
*rc
,
3389 struct btrfs_key
*extent_key
,
3390 struct extent_buffer
*leaf
,
3391 struct btrfs_extent_data_ref
*ref
,
3392 struct rb_root
*blocks
)
3394 struct btrfs_path
*path
;
3395 struct tree_block
*block
;
3396 struct btrfs_root
*root
;
3397 struct btrfs_file_extent_item
*fi
;
3398 struct rb_node
*rb_node
;
3399 struct btrfs_key key
;
3410 ref_root
= btrfs_extent_data_ref_root(leaf
, ref
);
3411 ref_objectid
= btrfs_extent_data_ref_objectid(leaf
, ref
);
3412 ref_offset
= btrfs_extent_data_ref_offset(leaf
, ref
);
3413 ref_count
= btrfs_extent_data_ref_count(leaf
, ref
);
3416 * This is an extent belonging to the free space cache, lets just delete
3417 * it and redo the search.
3419 if (ref_root
== BTRFS_ROOT_TREE_OBJECTID
) {
3420 ret
= delete_block_group_cache(rc
->extent_root
->fs_info
,
3421 NULL
, ref_objectid
);
3427 path
= btrfs_alloc_path();
3432 root
= read_fs_root(rc
->extent_root
->fs_info
, ref_root
);
3434 err
= PTR_ERR(root
);
3438 key
.objectid
= ref_objectid
;
3439 key
.type
= BTRFS_EXTENT_DATA_KEY
;
3440 if (ref_offset
> ((u64
)-1 << 32))
3443 key
.offset
= ref_offset
;
3445 path
->search_commit_root
= 1;
3446 path
->skip_locking
= 1;
3447 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
3453 leaf
= path
->nodes
[0];
3454 nritems
= btrfs_header_nritems(leaf
);
3456 * the references in tree blocks that use full backrefs
3457 * are not counted in
3459 if (block_use_full_backref(rc
, leaf
))
3463 rb_node
= tree_search(blocks
, leaf
->start
);
3468 path
->slots
[0] = nritems
;
3471 while (ref_count
> 0) {
3472 while (path
->slots
[0] >= nritems
) {
3473 ret
= btrfs_next_leaf(root
, path
);
3483 leaf
= path
->nodes
[0];
3484 nritems
= btrfs_header_nritems(leaf
);
3487 if (block_use_full_backref(rc
, leaf
))
3491 rb_node
= tree_search(blocks
, leaf
->start
);
3496 path
->slots
[0] = nritems
;
3500 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
3501 if (key
.objectid
!= ref_objectid
||
3502 key
.type
!= BTRFS_EXTENT_DATA_KEY
) {
3507 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
3508 struct btrfs_file_extent_item
);
3510 if (btrfs_file_extent_type(leaf
, fi
) ==
3511 BTRFS_FILE_EXTENT_INLINE
)
3514 if (btrfs_file_extent_disk_bytenr(leaf
, fi
) !=
3515 extent_key
->objectid
)
3518 key
.offset
-= btrfs_file_extent_offset(leaf
, fi
);
3519 if (key
.offset
!= ref_offset
)
3527 if (!tree_block_processed(leaf
->start
, leaf
->len
, rc
)) {
3528 block
= kmalloc(sizeof(*block
), GFP_NOFS
);
3533 block
->bytenr
= leaf
->start
;
3534 btrfs_item_key_to_cpu(leaf
, &block
->key
, 0);
3536 block
->key_ready
= 1;
3537 rb_node
= tree_insert(blocks
, block
->bytenr
,
3540 backref_tree_panic(rb_node
, -EEXIST
,
3546 path
->slots
[0] = nritems
;
3552 btrfs_free_path(path
);
3557 * helper to find all tree blocks that reference a given data extent
3559 static noinline_for_stack
3560 int add_data_references(struct reloc_control
*rc
,
3561 struct btrfs_key
*extent_key
,
3562 struct btrfs_path
*path
,
3563 struct rb_root
*blocks
)
3565 struct btrfs_key key
;
3566 struct extent_buffer
*eb
;
3567 struct btrfs_extent_data_ref
*dref
;
3568 struct btrfs_extent_inline_ref
*iref
;
3571 u32 blocksize
= btrfs_level_size(rc
->extent_root
, 0);
3575 eb
= path
->nodes
[0];
3576 ptr
= btrfs_item_ptr_offset(eb
, path
->slots
[0]);
3577 end
= ptr
+ btrfs_item_size_nr(eb
, path
->slots
[0]);
3578 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3579 if (ptr
+ sizeof(struct btrfs_extent_item_v0
) == end
)
3583 ptr
+= sizeof(struct btrfs_extent_item
);
3586 iref
= (struct btrfs_extent_inline_ref
*)ptr
;
3587 key
.type
= btrfs_extent_inline_ref_type(eb
, iref
);
3588 if (key
.type
== BTRFS_SHARED_DATA_REF_KEY
) {
3589 key
.offset
= btrfs_extent_inline_ref_offset(eb
, iref
);
3590 ret
= __add_tree_block(rc
, key
.offset
, blocksize
,
3592 } else if (key
.type
== BTRFS_EXTENT_DATA_REF_KEY
) {
3593 dref
= (struct btrfs_extent_data_ref
*)(&iref
->offset
);
3594 ret
= find_data_references(rc
, extent_key
,
3599 ptr
+= btrfs_extent_inline_ref_size(key
.type
);
3605 eb
= path
->nodes
[0];
3606 if (path
->slots
[0] >= btrfs_header_nritems(eb
)) {
3607 ret
= btrfs_next_leaf(rc
->extent_root
, path
);
3614 eb
= path
->nodes
[0];
3617 btrfs_item_key_to_cpu(eb
, &key
, path
->slots
[0]);
3618 if (key
.objectid
!= extent_key
->objectid
)
3621 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3622 if (key
.type
== BTRFS_SHARED_DATA_REF_KEY
||
3623 key
.type
== BTRFS_EXTENT_REF_V0_KEY
) {
3625 BUG_ON(key
.type
== BTRFS_EXTENT_REF_V0_KEY
);
3626 if (key
.type
== BTRFS_SHARED_DATA_REF_KEY
) {
3628 ret
= __add_tree_block(rc
, key
.offset
, blocksize
,
3630 } else if (key
.type
== BTRFS_EXTENT_DATA_REF_KEY
) {
3631 dref
= btrfs_item_ptr(eb
, path
->slots
[0],
3632 struct btrfs_extent_data_ref
);
3633 ret
= find_data_references(rc
, extent_key
,
3644 btrfs_release_path(path
);
3646 free_block_list(blocks
);
3651 * helper to find next unprocessed extent
3653 static noinline_for_stack
3654 int find_next_extent(struct btrfs_trans_handle
*trans
,
3655 struct reloc_control
*rc
, struct btrfs_path
*path
,
3656 struct btrfs_key
*extent_key
)
3658 struct btrfs_key key
;
3659 struct extent_buffer
*leaf
;
3660 u64 start
, end
, last
;
3663 last
= rc
->block_group
->key
.objectid
+ rc
->block_group
->key
.offset
;
3666 if (rc
->search_start
>= last
) {
3671 key
.objectid
= rc
->search_start
;
3672 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
3675 path
->search_commit_root
= 1;
3676 path
->skip_locking
= 1;
3677 ret
= btrfs_search_slot(NULL
, rc
->extent_root
, &key
, path
,
3682 leaf
= path
->nodes
[0];
3683 if (path
->slots
[0] >= btrfs_header_nritems(leaf
)) {
3684 ret
= btrfs_next_leaf(rc
->extent_root
, path
);
3687 leaf
= path
->nodes
[0];
3690 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
3691 if (key
.objectid
>= last
) {
3696 if (key
.type
!= BTRFS_EXTENT_ITEM_KEY
&&
3697 key
.type
!= BTRFS_METADATA_ITEM_KEY
) {
3702 if (key
.type
== BTRFS_EXTENT_ITEM_KEY
&&
3703 key
.objectid
+ key
.offset
<= rc
->search_start
) {
3708 if (key
.type
== BTRFS_METADATA_ITEM_KEY
&&
3709 key
.objectid
+ rc
->extent_root
->leafsize
<=
3715 ret
= find_first_extent_bit(&rc
->processed_blocks
,
3716 key
.objectid
, &start
, &end
,
3717 EXTENT_DIRTY
, NULL
);
3719 if (ret
== 0 && start
<= key
.objectid
) {
3720 btrfs_release_path(path
);
3721 rc
->search_start
= end
+ 1;
3723 if (key
.type
== BTRFS_EXTENT_ITEM_KEY
)
3724 rc
->search_start
= key
.objectid
+ key
.offset
;
3726 rc
->search_start
= key
.objectid
+
3727 rc
->extent_root
->leafsize
;
3728 memcpy(extent_key
, &key
, sizeof(key
));
3732 btrfs_release_path(path
);
3736 static void set_reloc_control(struct reloc_control
*rc
)
3738 struct btrfs_fs_info
*fs_info
= rc
->extent_root
->fs_info
;
3740 mutex_lock(&fs_info
->reloc_mutex
);
3741 fs_info
->reloc_ctl
= rc
;
3742 mutex_unlock(&fs_info
->reloc_mutex
);
3745 static void unset_reloc_control(struct reloc_control
*rc
)
3747 struct btrfs_fs_info
*fs_info
= rc
->extent_root
->fs_info
;
3749 mutex_lock(&fs_info
->reloc_mutex
);
3750 fs_info
->reloc_ctl
= NULL
;
3751 mutex_unlock(&fs_info
->reloc_mutex
);
3754 static int check_extent_flags(u64 flags
)
3756 if ((flags
& BTRFS_EXTENT_FLAG_DATA
) &&
3757 (flags
& BTRFS_EXTENT_FLAG_TREE_BLOCK
))
3759 if (!(flags
& BTRFS_EXTENT_FLAG_DATA
) &&
3760 !(flags
& BTRFS_EXTENT_FLAG_TREE_BLOCK
))
3762 if ((flags
& BTRFS_EXTENT_FLAG_DATA
) &&
3763 (flags
& BTRFS_BLOCK_FLAG_FULL_BACKREF
))
3768 static noinline_for_stack
3769 int prepare_to_relocate(struct reloc_control
*rc
)
3771 struct btrfs_trans_handle
*trans
;
3774 rc
->block_rsv
= btrfs_alloc_block_rsv(rc
->extent_root
,
3775 BTRFS_BLOCK_RSV_TEMP
);
3780 * reserve some space for creating reloc trees.
3781 * btrfs_init_reloc_root will use them when there
3782 * is no reservation in transaction handle.
3784 ret
= btrfs_block_rsv_add(rc
->extent_root
, rc
->block_rsv
,
3785 rc
->extent_root
->nodesize
* 256,
3786 BTRFS_RESERVE_FLUSH_ALL
);
3790 memset(&rc
->cluster
, 0, sizeof(rc
->cluster
));
3791 rc
->search_start
= rc
->block_group
->key
.objectid
;
3792 rc
->extents_found
= 0;
3793 rc
->nodes_relocated
= 0;
3794 rc
->merging_rsv_size
= 0;
3796 rc
->create_reloc_tree
= 1;
3797 set_reloc_control(rc
);
3799 trans
= btrfs_join_transaction(rc
->extent_root
);
3800 if (IS_ERR(trans
)) {
3801 unset_reloc_control(rc
);
3803 * extent tree is not a ref_cow tree and has no reloc_root to
3804 * cleanup. And callers are responsible to free the above
3807 return PTR_ERR(trans
);
3809 btrfs_commit_transaction(trans
, rc
->extent_root
);
3813 static noinline_for_stack
int relocate_block_group(struct reloc_control
*rc
)
3815 struct rb_root blocks
= RB_ROOT
;
3816 struct btrfs_key key
;
3817 struct btrfs_trans_handle
*trans
= NULL
;
3818 struct btrfs_path
*path
;
3819 struct btrfs_extent_item
*ei
;
3826 path
= btrfs_alloc_path();
3831 ret
= prepare_to_relocate(rc
);
3839 trans
= btrfs_start_transaction(rc
->extent_root
, 0);
3840 if (IS_ERR(trans
)) {
3841 err
= PTR_ERR(trans
);
3846 if (update_backref_cache(trans
, &rc
->backref_cache
)) {
3847 btrfs_end_transaction(trans
, rc
->extent_root
);
3851 ret
= find_next_extent(trans
, rc
, path
, &key
);
3857 rc
->extents_found
++;
3859 ei
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
3860 struct btrfs_extent_item
);
3861 item_size
= btrfs_item_size_nr(path
->nodes
[0], path
->slots
[0]);
3862 if (item_size
>= sizeof(*ei
)) {
3863 flags
= btrfs_extent_flags(path
->nodes
[0], ei
);
3864 ret
= check_extent_flags(flags
);
3868 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3870 int path_change
= 0;
3873 sizeof(struct btrfs_extent_item_v0
));
3874 ret
= get_ref_objectid_v0(rc
, path
, &key
, &ref_owner
,
3876 if (ref_owner
< BTRFS_FIRST_FREE_OBJECTID
)
3877 flags
= BTRFS_EXTENT_FLAG_TREE_BLOCK
;
3879 flags
= BTRFS_EXTENT_FLAG_DATA
;
3882 btrfs_release_path(path
);
3884 path
->search_commit_root
= 1;
3885 path
->skip_locking
= 1;
3886 ret
= btrfs_search_slot(NULL
, rc
->extent_root
,
3899 if (flags
& BTRFS_EXTENT_FLAG_TREE_BLOCK
) {
3900 ret
= add_tree_block(rc
, &key
, path
, &blocks
);
3901 } else if (rc
->stage
== UPDATE_DATA_PTRS
&&
3902 (flags
& BTRFS_EXTENT_FLAG_DATA
)) {
3903 ret
= add_data_references(rc
, &key
, path
, &blocks
);
3905 btrfs_release_path(path
);
3913 if (!RB_EMPTY_ROOT(&blocks
)) {
3914 ret
= relocate_tree_blocks(trans
, rc
, &blocks
);
3916 if (ret
!= -EAGAIN
) {
3920 rc
->extents_found
--;
3921 rc
->search_start
= key
.objectid
;
3925 ret
= btrfs_block_rsv_check(rc
->extent_root
, rc
->block_rsv
, 5);
3927 if (ret
!= -ENOSPC
) {
3932 rc
->commit_transaction
= 1;
3935 if (rc
->commit_transaction
) {
3936 rc
->commit_transaction
= 0;
3937 ret
= btrfs_commit_transaction(trans
, rc
->extent_root
);
3940 btrfs_end_transaction_throttle(trans
, rc
->extent_root
);
3941 btrfs_btree_balance_dirty(rc
->extent_root
);
3945 if (rc
->stage
== MOVE_DATA_EXTENTS
&&
3946 (flags
& BTRFS_EXTENT_FLAG_DATA
)) {
3947 rc
->found_file_extent
= 1;
3948 ret
= relocate_data_extent(rc
->data_inode
,
3949 &key
, &rc
->cluster
);
3956 if (trans
&& progress
&& err
== -ENOSPC
) {
3957 ret
= btrfs_force_chunk_alloc(trans
, rc
->extent_root
,
3958 rc
->block_group
->flags
);
3966 btrfs_release_path(path
);
3967 clear_extent_bits(&rc
->processed_blocks
, 0, (u64
)-1, EXTENT_DIRTY
,
3971 btrfs_end_transaction_throttle(trans
, rc
->extent_root
);
3972 btrfs_btree_balance_dirty(rc
->extent_root
);
3976 ret
= relocate_file_extent_cluster(rc
->data_inode
,
3982 rc
->create_reloc_tree
= 0;
3983 set_reloc_control(rc
);
3985 backref_cache_cleanup(&rc
->backref_cache
);
3986 btrfs_block_rsv_release(rc
->extent_root
, rc
->block_rsv
, (u64
)-1);
3988 err
= prepare_to_merge(rc
, err
);
3990 merge_reloc_roots(rc
);
3992 rc
->merge_reloc_tree
= 0;
3993 unset_reloc_control(rc
);
3994 btrfs_block_rsv_release(rc
->extent_root
, rc
->block_rsv
, (u64
)-1);
3996 /* get rid of pinned extents */
3997 trans
= btrfs_join_transaction(rc
->extent_root
);
3999 err
= PTR_ERR(trans
);
4001 btrfs_commit_transaction(trans
, rc
->extent_root
);
4003 btrfs_free_block_rsv(rc
->extent_root
, rc
->block_rsv
);
4004 btrfs_free_path(path
);
4008 static int __insert_orphan_inode(struct btrfs_trans_handle
*trans
,
4009 struct btrfs_root
*root
, u64 objectid
)
4011 struct btrfs_path
*path
;
4012 struct btrfs_inode_item
*item
;
4013 struct extent_buffer
*leaf
;
4016 path
= btrfs_alloc_path();
4020 ret
= btrfs_insert_empty_inode(trans
, root
, path
, objectid
);
4024 leaf
= path
->nodes
[0];
4025 item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_inode_item
);
4026 memset_extent_buffer(leaf
, 0, (unsigned long)item
, sizeof(*item
));
4027 btrfs_set_inode_generation(leaf
, item
, 1);
4028 btrfs_set_inode_size(leaf
, item
, 0);
4029 btrfs_set_inode_mode(leaf
, item
, S_IFREG
| 0600);
4030 btrfs_set_inode_flags(leaf
, item
, BTRFS_INODE_NOCOMPRESS
|
4031 BTRFS_INODE_PREALLOC
);
4032 btrfs_mark_buffer_dirty(leaf
);
4033 btrfs_release_path(path
);
4035 btrfs_free_path(path
);
4040 * helper to create inode for data relocation.
4041 * the inode is in data relocation tree and its link count is 0
4043 static noinline_for_stack
4044 struct inode
*create_reloc_inode(struct btrfs_fs_info
*fs_info
,
4045 struct btrfs_block_group_cache
*group
)
4047 struct inode
*inode
= NULL
;
4048 struct btrfs_trans_handle
*trans
;
4049 struct btrfs_root
*root
;
4050 struct btrfs_key key
;
4051 u64 objectid
= BTRFS_FIRST_FREE_OBJECTID
;
4054 root
= read_fs_root(fs_info
, BTRFS_DATA_RELOC_TREE_OBJECTID
);
4056 return ERR_CAST(root
);
4058 trans
= btrfs_start_transaction(root
, 6);
4060 return ERR_CAST(trans
);
4062 err
= btrfs_find_free_objectid(root
, &objectid
);
4066 err
= __insert_orphan_inode(trans
, root
, objectid
);
4069 key
.objectid
= objectid
;
4070 key
.type
= BTRFS_INODE_ITEM_KEY
;
4072 inode
= btrfs_iget(root
->fs_info
->sb
, &key
, root
, NULL
);
4073 BUG_ON(IS_ERR(inode
) || is_bad_inode(inode
));
4074 BTRFS_I(inode
)->index_cnt
= group
->key
.objectid
;
4076 err
= btrfs_orphan_add(trans
, inode
);
4078 btrfs_end_transaction(trans
, root
);
4079 btrfs_btree_balance_dirty(root
);
4083 inode
= ERR_PTR(err
);
4088 static struct reloc_control
*alloc_reloc_control(struct btrfs_fs_info
*fs_info
)
4090 struct reloc_control
*rc
;
4092 rc
= kzalloc(sizeof(*rc
), GFP_NOFS
);
4096 INIT_LIST_HEAD(&rc
->reloc_roots
);
4097 backref_cache_init(&rc
->backref_cache
);
4098 mapping_tree_init(&rc
->reloc_root_tree
);
4099 extent_io_tree_init(&rc
->processed_blocks
,
4100 fs_info
->btree_inode
->i_mapping
);
4105 * function to relocate all extents in a block group.
4107 int btrfs_relocate_block_group(struct btrfs_root
*extent_root
, u64 group_start
)
4109 struct btrfs_fs_info
*fs_info
= extent_root
->fs_info
;
4110 struct reloc_control
*rc
;
4111 struct inode
*inode
;
4112 struct btrfs_path
*path
;
4117 rc
= alloc_reloc_control(fs_info
);
4121 rc
->extent_root
= extent_root
;
4123 rc
->block_group
= btrfs_lookup_block_group(fs_info
, group_start
);
4124 BUG_ON(!rc
->block_group
);
4126 if (!rc
->block_group
->ro
) {
4127 ret
= btrfs_set_block_group_ro(extent_root
, rc
->block_group
);
4135 path
= btrfs_alloc_path();
4141 inode
= lookup_free_space_inode(fs_info
->tree_root
, rc
->block_group
,
4143 btrfs_free_path(path
);
4146 ret
= delete_block_group_cache(fs_info
, inode
, 0);
4148 ret
= PTR_ERR(inode
);
4150 if (ret
&& ret
!= -ENOENT
) {
4155 rc
->data_inode
= create_reloc_inode(fs_info
, rc
->block_group
);
4156 if (IS_ERR(rc
->data_inode
)) {
4157 err
= PTR_ERR(rc
->data_inode
);
4158 rc
->data_inode
= NULL
;
4162 printk(KERN_INFO
"btrfs: relocating block group %llu flags %llu\n",
4163 (unsigned long long)rc
->block_group
->key
.objectid
,
4164 (unsigned long long)rc
->block_group
->flags
);
4166 ret
= btrfs_start_delalloc_inodes(fs_info
->tree_root
, 0);
4171 btrfs_wait_ordered_extents(fs_info
->tree_root
, 0);
4174 mutex_lock(&fs_info
->cleaner_mutex
);
4175 ret
= relocate_block_group(rc
);
4176 mutex_unlock(&fs_info
->cleaner_mutex
);
4182 if (rc
->extents_found
== 0)
4185 printk(KERN_INFO
"btrfs: found %llu extents\n",
4186 (unsigned long long)rc
->extents_found
);
4188 if (rc
->stage
== MOVE_DATA_EXTENTS
&& rc
->found_file_extent
) {
4189 btrfs_wait_ordered_range(rc
->data_inode
, 0, (u64
)-1);
4190 invalidate_mapping_pages(rc
->data_inode
->i_mapping
,
4192 rc
->stage
= UPDATE_DATA_PTRS
;
4196 filemap_write_and_wait_range(fs_info
->btree_inode
->i_mapping
,
4197 rc
->block_group
->key
.objectid
,
4198 rc
->block_group
->key
.objectid
+
4199 rc
->block_group
->key
.offset
- 1);
4201 WARN_ON(rc
->block_group
->pinned
> 0);
4202 WARN_ON(rc
->block_group
->reserved
> 0);
4203 WARN_ON(btrfs_block_group_used(&rc
->block_group
->item
) > 0);
4206 btrfs_set_block_group_rw(extent_root
, rc
->block_group
);
4207 iput(rc
->data_inode
);
4208 btrfs_put_block_group(rc
->block_group
);
4213 static noinline_for_stack
int mark_garbage_root(struct btrfs_root
*root
)
4215 struct btrfs_trans_handle
*trans
;
4218 trans
= btrfs_start_transaction(root
->fs_info
->tree_root
, 0);
4220 return PTR_ERR(trans
);
4222 memset(&root
->root_item
.drop_progress
, 0,
4223 sizeof(root
->root_item
.drop_progress
));
4224 root
->root_item
.drop_level
= 0;
4225 btrfs_set_root_refs(&root
->root_item
, 0);
4226 ret
= btrfs_update_root(trans
, root
->fs_info
->tree_root
,
4227 &root
->root_key
, &root
->root_item
);
4229 err
= btrfs_end_transaction(trans
, root
->fs_info
->tree_root
);
4236 * recover relocation interrupted by system crash.
4238 * this function resumes merging reloc trees with corresponding fs trees.
4239 * this is important for keeping the sharing of tree blocks
4241 int btrfs_recover_relocation(struct btrfs_root
*root
)
4243 LIST_HEAD(reloc_roots
);
4244 struct btrfs_key key
;
4245 struct btrfs_root
*fs_root
;
4246 struct btrfs_root
*reloc_root
;
4247 struct btrfs_path
*path
;
4248 struct extent_buffer
*leaf
;
4249 struct reloc_control
*rc
= NULL
;
4250 struct btrfs_trans_handle
*trans
;
4254 path
= btrfs_alloc_path();
4259 key
.objectid
= BTRFS_TREE_RELOC_OBJECTID
;
4260 key
.type
= BTRFS_ROOT_ITEM_KEY
;
4261 key
.offset
= (u64
)-1;
4264 ret
= btrfs_search_slot(NULL
, root
->fs_info
->tree_root
, &key
,
4271 if (path
->slots
[0] == 0)
4275 leaf
= path
->nodes
[0];
4276 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
4277 btrfs_release_path(path
);
4279 if (key
.objectid
!= BTRFS_TREE_RELOC_OBJECTID
||
4280 key
.type
!= BTRFS_ROOT_ITEM_KEY
)
4283 reloc_root
= btrfs_read_fs_root_no_radix(root
, &key
);
4284 if (IS_ERR(reloc_root
)) {
4285 err
= PTR_ERR(reloc_root
);
4289 list_add(&reloc_root
->root_list
, &reloc_roots
);
4291 if (btrfs_root_refs(&reloc_root
->root_item
) > 0) {
4292 fs_root
= read_fs_root(root
->fs_info
,
4293 reloc_root
->root_key
.offset
);
4294 if (IS_ERR(fs_root
)) {
4295 ret
= PTR_ERR(fs_root
);
4296 if (ret
!= -ENOENT
) {
4300 ret
= mark_garbage_root(reloc_root
);
4308 if (key
.offset
== 0)
4313 btrfs_release_path(path
);
4315 if (list_empty(&reloc_roots
))
4318 rc
= alloc_reloc_control(root
->fs_info
);
4324 rc
->extent_root
= root
->fs_info
->extent_root
;
4326 set_reloc_control(rc
);
4328 trans
= btrfs_join_transaction(rc
->extent_root
);
4329 if (IS_ERR(trans
)) {
4330 unset_reloc_control(rc
);
4331 err
= PTR_ERR(trans
);
4335 rc
->merge_reloc_tree
= 1;
4337 while (!list_empty(&reloc_roots
)) {
4338 reloc_root
= list_entry(reloc_roots
.next
,
4339 struct btrfs_root
, root_list
);
4340 list_del(&reloc_root
->root_list
);
4342 if (btrfs_root_refs(&reloc_root
->root_item
) == 0) {
4343 list_add_tail(&reloc_root
->root_list
,
4348 fs_root
= read_fs_root(root
->fs_info
,
4349 reloc_root
->root_key
.offset
);
4350 if (IS_ERR(fs_root
)) {
4351 err
= PTR_ERR(fs_root
);
4355 err
= __add_reloc_root(reloc_root
);
4356 BUG_ON(err
< 0); /* -ENOMEM or logic error */
4357 fs_root
->reloc_root
= reloc_root
;
4360 err
= btrfs_commit_transaction(trans
, rc
->extent_root
);
4364 merge_reloc_roots(rc
);
4366 unset_reloc_control(rc
);
4368 trans
= btrfs_join_transaction(rc
->extent_root
);
4370 err
= PTR_ERR(trans
);
4372 err
= btrfs_commit_transaction(trans
, rc
->extent_root
);
4376 if (!list_empty(&reloc_roots
))
4377 free_reloc_roots(&reloc_roots
);
4379 btrfs_free_path(path
);
4382 /* cleanup orphan inode in data relocation tree */
4383 fs_root
= read_fs_root(root
->fs_info
,
4384 BTRFS_DATA_RELOC_TREE_OBJECTID
);
4385 if (IS_ERR(fs_root
))
4386 err
= PTR_ERR(fs_root
);
4388 err
= btrfs_orphan_cleanup(fs_root
);
4394 * helper to add ordered checksum for data relocation.
4396 * cloning checksum properly handles the nodatasum extents.
4397 * it also saves CPU time to re-calculate the checksum.
4399 int btrfs_reloc_clone_csums(struct inode
*inode
, u64 file_pos
, u64 len
)
4401 struct btrfs_ordered_sum
*sums
;
4402 struct btrfs_sector_sum
*sector_sum
;
4403 struct btrfs_ordered_extent
*ordered
;
4404 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4410 ordered
= btrfs_lookup_ordered_extent(inode
, file_pos
);
4411 BUG_ON(ordered
->file_offset
!= file_pos
|| ordered
->len
!= len
);
4413 disk_bytenr
= file_pos
+ BTRFS_I(inode
)->index_cnt
;
4414 ret
= btrfs_lookup_csums_range(root
->fs_info
->csum_root
, disk_bytenr
,
4415 disk_bytenr
+ len
- 1, &list
, 0);
4419 while (!list_empty(&list
)) {
4420 sums
= list_entry(list
.next
, struct btrfs_ordered_sum
, list
);
4421 list_del_init(&sums
->list
);
4423 sector_sum
= sums
->sums
;
4424 sums
->bytenr
= ordered
->start
;
4427 while (offset
< sums
->len
) {
4428 sector_sum
->bytenr
+= ordered
->start
- disk_bytenr
;
4430 offset
+= root
->sectorsize
;
4433 btrfs_add_ordered_sum(inode
, ordered
, sums
);
4436 btrfs_put_ordered_extent(ordered
);
4440 void btrfs_reloc_cow_block(struct btrfs_trans_handle
*trans
,
4441 struct btrfs_root
*root
, struct extent_buffer
*buf
,
4442 struct extent_buffer
*cow
)
4444 struct reloc_control
*rc
;
4445 struct backref_node
*node
;
4450 rc
= root
->fs_info
->reloc_ctl
;
4454 BUG_ON(rc
->stage
== UPDATE_DATA_PTRS
&&
4455 root
->root_key
.objectid
== BTRFS_DATA_RELOC_TREE_OBJECTID
);
4457 level
= btrfs_header_level(buf
);
4458 if (btrfs_header_generation(buf
) <=
4459 btrfs_root_last_snapshot(&root
->root_item
))
4462 if (root
->root_key
.objectid
== BTRFS_TREE_RELOC_OBJECTID
&&
4463 rc
->create_reloc_tree
) {
4464 WARN_ON(!first_cow
&& level
== 0);
4466 node
= rc
->backref_cache
.path
[level
];
4467 BUG_ON(node
->bytenr
!= buf
->start
&&
4468 node
->new_bytenr
!= buf
->start
);
4470 drop_node_buffer(node
);
4471 extent_buffer_get(cow
);
4473 node
->new_bytenr
= cow
->start
;
4475 if (!node
->pending
) {
4476 list_move_tail(&node
->list
,
4477 &rc
->backref_cache
.pending
[level
]);
4482 __mark_block_processed(rc
, node
);
4484 if (first_cow
&& level
> 0)
4485 rc
->nodes_relocated
+= buf
->len
;
4488 if (level
== 0 && first_cow
&& rc
->stage
== UPDATE_DATA_PTRS
) {
4489 ret
= replace_file_extents(trans
, rc
, root
, cow
);
4495 * called before creating snapshot. it calculates metadata reservation
4496 * requried for relocating tree blocks in the snapshot
4498 void btrfs_reloc_pre_snapshot(struct btrfs_trans_handle
*trans
,
4499 struct btrfs_pending_snapshot
*pending
,
4500 u64
*bytes_to_reserve
)
4502 struct btrfs_root
*root
;
4503 struct reloc_control
*rc
;
4505 root
= pending
->root
;
4506 if (!root
->reloc_root
)
4509 rc
= root
->fs_info
->reloc_ctl
;
4510 if (!rc
->merge_reloc_tree
)
4513 root
= root
->reloc_root
;
4514 BUG_ON(btrfs_root_refs(&root
->root_item
) == 0);
4516 * relocation is in the stage of merging trees. the space
4517 * used by merging a reloc tree is twice the size of
4518 * relocated tree nodes in the worst case. half for cowing
4519 * the reloc tree, half for cowing the fs tree. the space
4520 * used by cowing the reloc tree will be freed after the
4521 * tree is dropped. if we create snapshot, cowing the fs
4522 * tree may use more space than it frees. so we need
4523 * reserve extra space.
4525 *bytes_to_reserve
+= rc
->nodes_relocated
;
4529 * called after snapshot is created. migrate block reservation
4530 * and create reloc root for the newly created snapshot
4532 int btrfs_reloc_post_snapshot(struct btrfs_trans_handle
*trans
,
4533 struct btrfs_pending_snapshot
*pending
)
4535 struct btrfs_root
*root
= pending
->root
;
4536 struct btrfs_root
*reloc_root
;
4537 struct btrfs_root
*new_root
;
4538 struct reloc_control
*rc
;
4541 if (!root
->reloc_root
)
4544 rc
= root
->fs_info
->reloc_ctl
;
4545 rc
->merging_rsv_size
+= rc
->nodes_relocated
;
4547 if (rc
->merge_reloc_tree
) {
4548 ret
= btrfs_block_rsv_migrate(&pending
->block_rsv
,
4550 rc
->nodes_relocated
);
4555 new_root
= pending
->snap
;
4556 reloc_root
= create_reloc_root(trans
, root
->reloc_root
,
4557 new_root
->root_key
.objectid
);
4558 if (IS_ERR(reloc_root
))
4559 return PTR_ERR(reloc_root
);
4561 ret
= __add_reloc_root(reloc_root
);
4563 new_root
->reloc_root
= reloc_root
;
4565 if (rc
->create_reloc_tree
)
4566 ret
= clone_backref_node(trans
, rc
, root
, reloc_root
);