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>
26 #include "transaction.h"
29 #include "btrfs_inode.h"
30 #include "async-thread.h"
33 * backref_node, mapping_node and tree_block start with this
36 struct rb_node rb_node
;
41 * present a tree block in the backref cache
44 struct rb_node rb_node
;
46 /* objectid tree block owner */
48 /* list of upper level blocks reference this block */
49 struct list_head upper
;
50 /* list of child blocks in the cache */
51 struct list_head lower
;
52 /* NULL if this node is not tree root */
53 struct btrfs_root
*root
;
54 /* extent buffer got by COW the block */
55 struct extent_buffer
*eb
;
56 /* level of tree block */
58 /* 1 if the block is root of old snapshot */
59 unsigned int old_root
:1;
60 /* 1 if no child blocks in the cache */
61 unsigned int lowest
:1;
62 /* is the extent buffer locked */
63 unsigned int locked
:1;
64 /* has the block been processed */
65 unsigned int processed
:1;
66 /* have backrefs of this block been checked */
67 unsigned int checked
:1;
71 * present a block pointer in the backref cache
74 struct list_head list
[2];
75 struct backref_node
*node
[2];
82 struct backref_cache
{
83 /* red black tree of all backref nodes in the cache */
84 struct rb_root rb_root
;
85 /* list of backref nodes with no child block in the cache */
86 struct list_head pending
[BTRFS_MAX_LEVEL
];
91 * map address of tree root to tree
94 struct rb_node rb_node
;
100 struct rb_root rb_root
;
105 * present a tree block to process
108 struct rb_node rb_node
;
110 struct btrfs_key key
;
111 unsigned int level
:8;
112 unsigned int key_ready
:1;
116 #define INODEVEC_SIZE 16
119 struct list_head list
;
120 struct inode
*inode
[INODEVEC_SIZE
];
124 #define MAX_EXTENTS 128
126 struct file_extent_cluster
{
129 u64 boundary
[MAX_EXTENTS
];
133 struct reloc_control
{
134 /* block group to relocate */
135 struct btrfs_block_group_cache
*block_group
;
137 struct btrfs_root
*extent_root
;
138 /* inode for moving data */
139 struct inode
*data_inode
;
140 struct btrfs_workers workers
;
141 /* tree blocks have been processed */
142 struct extent_io_tree processed_blocks
;
143 /* map start of tree root to corresponding reloc tree */
144 struct mapping_tree reloc_root_tree
;
145 /* list of reloc trees */
146 struct list_head reloc_roots
;
151 int create_reloc_root
;
152 unsigned int found_file_extent
:1;
153 unsigned int found_old_snapshot
:1;
156 /* stages of data relocation */
157 #define MOVE_DATA_EXTENTS 0
158 #define UPDATE_DATA_PTRS 1
161 * merge reloc tree to corresponding fs tree in worker threads
164 struct btrfs_work work
;
165 struct reloc_control
*rc
;
166 struct btrfs_root
*root
;
167 struct completion
*done
;
168 atomic_t
*num_pending
;
171 static void mapping_tree_init(struct mapping_tree
*tree
)
173 tree
->rb_root
.rb_node
= NULL
;
174 spin_lock_init(&tree
->lock
);
177 static void backref_cache_init(struct backref_cache
*cache
)
180 cache
->rb_root
.rb_node
= NULL
;
181 for (i
= 0; i
< BTRFS_MAX_LEVEL
; i
++)
182 INIT_LIST_HEAD(&cache
->pending
[i
]);
183 spin_lock_init(&cache
->lock
);
186 static void backref_node_init(struct backref_node
*node
)
188 memset(node
, 0, sizeof(*node
));
189 INIT_LIST_HEAD(&node
->upper
);
190 INIT_LIST_HEAD(&node
->lower
);
191 RB_CLEAR_NODE(&node
->rb_node
);
194 static struct rb_node
*tree_insert(struct rb_root
*root
, u64 bytenr
,
195 struct rb_node
*node
)
197 struct rb_node
**p
= &root
->rb_node
;
198 struct rb_node
*parent
= NULL
;
199 struct tree_entry
*entry
;
203 entry
= rb_entry(parent
, struct tree_entry
, rb_node
);
205 if (bytenr
< entry
->bytenr
)
207 else if (bytenr
> entry
->bytenr
)
213 rb_link_node(node
, parent
, p
);
214 rb_insert_color(node
, root
);
218 static struct rb_node
*tree_search(struct rb_root
*root
, u64 bytenr
)
220 struct rb_node
*n
= root
->rb_node
;
221 struct tree_entry
*entry
;
224 entry
= rb_entry(n
, struct tree_entry
, rb_node
);
226 if (bytenr
< entry
->bytenr
)
228 else if (bytenr
> entry
->bytenr
)
237 * walk up backref nodes until reach node presents tree root
239 static struct backref_node
*walk_up_backref(struct backref_node
*node
,
240 struct backref_edge
*edges
[],
243 struct backref_edge
*edge
;
246 while (!list_empty(&node
->upper
)) {
247 edge
= list_entry(node
->upper
.next
,
248 struct backref_edge
, list
[LOWER
]);
250 node
= edge
->node
[UPPER
];
257 * walk down backref nodes to find start of next reference path
259 static struct backref_node
*walk_down_backref(struct backref_edge
*edges
[],
262 struct backref_edge
*edge
;
263 struct backref_node
*lower
;
267 edge
= edges
[idx
- 1];
268 lower
= edge
->node
[LOWER
];
269 if (list_is_last(&edge
->list
[LOWER
], &lower
->upper
)) {
273 edge
= list_entry(edge
->list
[LOWER
].next
,
274 struct backref_edge
, list
[LOWER
]);
275 edges
[idx
- 1] = edge
;
277 return edge
->node
[UPPER
];
283 static void drop_node_buffer(struct backref_node
*node
)
287 btrfs_tree_unlock(node
->eb
);
290 free_extent_buffer(node
->eb
);
295 static void drop_backref_node(struct backref_cache
*tree
,
296 struct backref_node
*node
)
298 BUG_ON(!node
->lowest
);
299 BUG_ON(!list_empty(&node
->upper
));
301 drop_node_buffer(node
);
302 list_del(&node
->lower
);
304 rb_erase(&node
->rb_node
, &tree
->rb_root
);
309 * remove a backref node from the backref cache
311 static void remove_backref_node(struct backref_cache
*cache
,
312 struct backref_node
*node
)
314 struct backref_node
*upper
;
315 struct backref_edge
*edge
;
320 BUG_ON(!node
->lowest
);
321 while (!list_empty(&node
->upper
)) {
322 edge
= list_entry(node
->upper
.next
, struct backref_edge
,
324 upper
= edge
->node
[UPPER
];
325 list_del(&edge
->list
[LOWER
]);
326 list_del(&edge
->list
[UPPER
]);
329 * add the node to pending list if no other
330 * child block cached.
332 if (list_empty(&upper
->lower
)) {
333 list_add_tail(&upper
->lower
,
334 &cache
->pending
[upper
->level
]);
338 drop_backref_node(cache
, node
);
342 * find reloc tree by address of tree root
344 static struct btrfs_root
*find_reloc_root(struct reloc_control
*rc
,
347 struct rb_node
*rb_node
;
348 struct mapping_node
*node
;
349 struct btrfs_root
*root
= NULL
;
351 spin_lock(&rc
->reloc_root_tree
.lock
);
352 rb_node
= tree_search(&rc
->reloc_root_tree
.rb_root
, bytenr
);
354 node
= rb_entry(rb_node
, struct mapping_node
, rb_node
);
355 root
= (struct btrfs_root
*)node
->data
;
357 spin_unlock(&rc
->reloc_root_tree
.lock
);
361 static int is_cowonly_root(u64 root_objectid
)
363 if (root_objectid
== BTRFS_ROOT_TREE_OBJECTID
||
364 root_objectid
== BTRFS_EXTENT_TREE_OBJECTID
||
365 root_objectid
== BTRFS_CHUNK_TREE_OBJECTID
||
366 root_objectid
== BTRFS_DEV_TREE_OBJECTID
||
367 root_objectid
== BTRFS_TREE_LOG_OBJECTID
||
368 root_objectid
== BTRFS_CSUM_TREE_OBJECTID
)
373 static struct btrfs_root
*read_fs_root(struct btrfs_fs_info
*fs_info
,
376 struct btrfs_key key
;
378 key
.objectid
= root_objectid
;
379 key
.type
= BTRFS_ROOT_ITEM_KEY
;
380 if (is_cowonly_root(root_objectid
))
383 key
.offset
= (u64
)-1;
385 return btrfs_read_fs_root_no_name(fs_info
, &key
);
388 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
389 static noinline_for_stack
390 struct btrfs_root
*find_tree_root(struct reloc_control
*rc
,
391 struct extent_buffer
*leaf
,
392 struct btrfs_extent_ref_v0
*ref0
)
394 struct btrfs_root
*root
;
395 u64 root_objectid
= btrfs_ref_root_v0(leaf
, ref0
);
396 u64 generation
= btrfs_ref_generation_v0(leaf
, ref0
);
398 BUG_ON(root_objectid
== BTRFS_TREE_RELOC_OBJECTID
);
400 root
= read_fs_root(rc
->extent_root
->fs_info
, root_objectid
);
401 BUG_ON(IS_ERR(root
));
403 if (root
->ref_cows
&&
404 generation
!= btrfs_root_generation(&root
->root_item
))
411 static noinline_for_stack
412 int find_inline_backref(struct extent_buffer
*leaf
, int slot
,
413 unsigned long *ptr
, unsigned long *end
)
415 struct btrfs_extent_item
*ei
;
416 struct btrfs_tree_block_info
*bi
;
419 item_size
= btrfs_item_size_nr(leaf
, slot
);
420 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
421 if (item_size
< sizeof(*ei
)) {
422 WARN_ON(item_size
!= sizeof(struct btrfs_extent_item_v0
));
426 ei
= btrfs_item_ptr(leaf
, slot
, struct btrfs_extent_item
);
427 WARN_ON(!(btrfs_extent_flags(leaf
, ei
) &
428 BTRFS_EXTENT_FLAG_TREE_BLOCK
));
430 if (item_size
<= sizeof(*ei
) + sizeof(*bi
)) {
431 WARN_ON(item_size
< sizeof(*ei
) + sizeof(*bi
));
435 bi
= (struct btrfs_tree_block_info
*)(ei
+ 1);
436 *ptr
= (unsigned long)(bi
+ 1);
437 *end
= (unsigned long)ei
+ item_size
;
442 * build backref tree for a given tree block. root of the backref tree
443 * corresponds the tree block, leaves of the backref tree correspond
444 * roots of b-trees that reference the tree block.
446 * the basic idea of this function is check backrefs of a given block
447 * to find upper level blocks that refernece the block, and then check
448 * bakcrefs of these upper level blocks recursively. the recursion stop
449 * when tree root is reached or backrefs for the block is cached.
451 * NOTE: if we find backrefs for a block are cached, we know backrefs
452 * for all upper level blocks that directly/indirectly reference the
453 * block are also cached.
455 static struct backref_node
*build_backref_tree(struct reloc_control
*rc
,
456 struct backref_cache
*cache
,
457 struct btrfs_key
*node_key
,
458 int level
, u64 bytenr
)
460 struct btrfs_path
*path1
;
461 struct btrfs_path
*path2
;
462 struct extent_buffer
*eb
;
463 struct btrfs_root
*root
;
464 struct backref_node
*cur
;
465 struct backref_node
*upper
;
466 struct backref_node
*lower
;
467 struct backref_node
*node
= NULL
;
468 struct backref_node
*exist
= NULL
;
469 struct backref_edge
*edge
;
470 struct rb_node
*rb_node
;
471 struct btrfs_key key
;
478 path1
= btrfs_alloc_path();
479 path2
= btrfs_alloc_path();
480 if (!path1
|| !path2
) {
485 node
= kmalloc(sizeof(*node
), GFP_NOFS
);
491 backref_node_init(node
);
492 node
->bytenr
= bytenr
;
500 key
.objectid
= cur
->bytenr
;
501 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
502 key
.offset
= (u64
)-1;
504 path1
->search_commit_root
= 1;
505 path1
->skip_locking
= 1;
506 ret
= btrfs_search_slot(NULL
, rc
->extent_root
, &key
, path1
,
512 BUG_ON(!ret
|| !path1
->slots
[0]);
516 WARN_ON(cur
->checked
);
517 if (!list_empty(&cur
->upper
)) {
519 * the backref was added previously when processsing
520 * backref of type BTRFS_TREE_BLOCK_REF_KEY
522 BUG_ON(!list_is_singular(&cur
->upper
));
523 edge
= list_entry(cur
->upper
.next
, struct backref_edge
,
525 BUG_ON(!list_empty(&edge
->list
[UPPER
]));
526 exist
= edge
->node
[UPPER
];
528 * add the upper level block to pending list if we need
532 list_add_tail(&edge
->list
[UPPER
], &list
);
539 eb
= path1
->nodes
[0];
542 if (path1
->slots
[0] >= btrfs_header_nritems(eb
)) {
543 ret
= btrfs_next_leaf(rc
->extent_root
, path1
);
550 eb
= path1
->nodes
[0];
553 btrfs_item_key_to_cpu(eb
, &key
, path1
->slots
[0]);
554 if (key
.objectid
!= cur
->bytenr
) {
559 if (key
.type
== BTRFS_EXTENT_ITEM_KEY
) {
560 ret
= find_inline_backref(eb
, path1
->slots
[0],
568 /* update key for inline back ref */
569 struct btrfs_extent_inline_ref
*iref
;
570 iref
= (struct btrfs_extent_inline_ref
*)ptr
;
571 key
.type
= btrfs_extent_inline_ref_type(eb
, iref
);
572 key
.offset
= btrfs_extent_inline_ref_offset(eb
, iref
);
573 WARN_ON(key
.type
!= BTRFS_TREE_BLOCK_REF_KEY
&&
574 key
.type
!= BTRFS_SHARED_BLOCK_REF_KEY
);
578 ((key
.type
== BTRFS_TREE_BLOCK_REF_KEY
&&
579 exist
->owner
== key
.offset
) ||
580 (key
.type
== BTRFS_SHARED_BLOCK_REF_KEY
&&
581 exist
->bytenr
== key
.offset
))) {
586 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
587 if (key
.type
== BTRFS_SHARED_BLOCK_REF_KEY
||
588 key
.type
== BTRFS_EXTENT_REF_V0_KEY
) {
589 if (key
.objectid
== key
.offset
&&
590 key
.type
== BTRFS_EXTENT_REF_V0_KEY
) {
591 struct btrfs_extent_ref_v0
*ref0
;
592 ref0
= btrfs_item_ptr(eb
, path1
->slots
[0],
593 struct btrfs_extent_ref_v0
);
594 root
= find_tree_root(rc
, eb
, ref0
);
602 BUG_ON(key
.type
== BTRFS_EXTENT_REF_V0_KEY
);
603 if (key
.type
== BTRFS_SHARED_BLOCK_REF_KEY
) {
605 if (key
.objectid
== key
.offset
) {
607 * only root blocks of reloc trees use
608 * backref of this type.
610 root
= find_reloc_root(rc
, cur
->bytenr
);
616 edge
= kzalloc(sizeof(*edge
), GFP_NOFS
);
621 rb_node
= tree_search(&cache
->rb_root
, key
.offset
);
623 upper
= kmalloc(sizeof(*upper
), GFP_NOFS
);
629 backref_node_init(upper
);
630 upper
->bytenr
= key
.offset
;
632 upper
->level
= cur
->level
+ 1;
634 * backrefs for the upper level block isn't
635 * cached, add the block to pending list
637 list_add_tail(&edge
->list
[UPPER
], &list
);
639 upper
= rb_entry(rb_node
, struct backref_node
,
641 INIT_LIST_HEAD(&edge
->list
[UPPER
]);
643 list_add(&edge
->list
[LOWER
], &cur
->upper
);
644 edge
->node
[UPPER
] = upper
;
645 edge
->node
[LOWER
] = cur
;
648 } else if (key
.type
!= BTRFS_TREE_BLOCK_REF_KEY
) {
652 /* key.type == BTRFS_TREE_BLOCK_REF_KEY */
653 root
= read_fs_root(rc
->extent_root
->fs_info
, key
.offset
);
659 if (btrfs_root_level(&root
->root_item
) == cur
->level
) {
661 BUG_ON(btrfs_root_bytenr(&root
->root_item
) !=
667 level
= cur
->level
+ 1;
670 * searching the tree to find upper level blocks
671 * reference the block.
673 path2
->search_commit_root
= 1;
674 path2
->skip_locking
= 1;
675 path2
->lowest_level
= level
;
676 ret
= btrfs_search_slot(NULL
, root
, node_key
, path2
, 0, 0);
677 path2
->lowest_level
= 0;
682 if (ret
> 0 && path2
->slots
[level
] > 0)
683 path2
->slots
[level
]--;
685 eb
= path2
->nodes
[level
];
686 WARN_ON(btrfs_node_blockptr(eb
, path2
->slots
[level
]) !=
690 for (; level
< BTRFS_MAX_LEVEL
; level
++) {
691 if (!path2
->nodes
[level
]) {
692 BUG_ON(btrfs_root_bytenr(&root
->root_item
) !=
698 edge
= kzalloc(sizeof(*edge
), GFP_NOFS
);
704 eb
= path2
->nodes
[level
];
705 rb_node
= tree_search(&cache
->rb_root
, eb
->start
);
707 upper
= kmalloc(sizeof(*upper
), GFP_NOFS
);
713 backref_node_init(upper
);
714 upper
->bytenr
= eb
->start
;
715 upper
->owner
= btrfs_header_owner(eb
);
716 upper
->level
= lower
->level
+ 1;
719 * if we know the block isn't shared
720 * we can void checking its backrefs.
722 if (btrfs_block_can_be_shared(root
, eb
))
728 * add the block to pending list if we
729 * need check its backrefs. only block
730 * at 'cur->level + 1' is added to the
731 * tail of pending list. this guarantees
732 * we check backrefs from lower level
733 * blocks to upper level blocks.
735 if (!upper
->checked
&&
736 level
== cur
->level
+ 1) {
737 list_add_tail(&edge
->list
[UPPER
],
740 INIT_LIST_HEAD(&edge
->list
[UPPER
]);
742 upper
= rb_entry(rb_node
, struct backref_node
,
744 BUG_ON(!upper
->checked
);
745 INIT_LIST_HEAD(&edge
->list
[UPPER
]);
747 list_add_tail(&edge
->list
[LOWER
], &lower
->upper
);
748 edge
->node
[UPPER
] = upper
;
749 edge
->node
[LOWER
] = lower
;
756 btrfs_release_path(root
, path2
);
759 ptr
+= btrfs_extent_inline_ref_size(key
.type
);
769 btrfs_release_path(rc
->extent_root
, path1
);
774 /* the pending list isn't empty, take the first block to process */
775 if (!list_empty(&list
)) {
776 edge
= list_entry(list
.next
, struct backref_edge
, list
[UPPER
]);
777 list_del_init(&edge
->list
[UPPER
]);
778 cur
= edge
->node
[UPPER
];
783 * everything goes well, connect backref nodes and insert backref nodes
786 BUG_ON(!node
->checked
);
787 rb_node
= tree_insert(&cache
->rb_root
, node
->bytenr
, &node
->rb_node
);
790 list_for_each_entry(edge
, &node
->upper
, list
[LOWER
])
791 list_add_tail(&edge
->list
[UPPER
], &list
);
793 while (!list_empty(&list
)) {
794 edge
= list_entry(list
.next
, struct backref_edge
, list
[UPPER
]);
795 list_del_init(&edge
->list
[UPPER
]);
796 upper
= edge
->node
[UPPER
];
798 if (!RB_EMPTY_NODE(&upper
->rb_node
)) {
800 list_del_init(&upper
->lower
);
804 list_add_tail(&edge
->list
[UPPER
], &upper
->lower
);
808 BUG_ON(!upper
->checked
);
809 rb_node
= tree_insert(&cache
->rb_root
, upper
->bytenr
,
813 list_add_tail(&edge
->list
[UPPER
], &upper
->lower
);
815 list_for_each_entry(edge
, &upper
->upper
, list
[LOWER
])
816 list_add_tail(&edge
->list
[UPPER
], &list
);
819 btrfs_free_path(path1
);
820 btrfs_free_path(path2
);
822 INIT_LIST_HEAD(&list
);
825 if (RB_EMPTY_NODE(&upper
->rb_node
)) {
826 list_splice_tail(&upper
->upper
, &list
);
830 if (list_empty(&list
))
833 edge
= list_entry(list
.next
, struct backref_edge
,
835 upper
= edge
->node
[UPPER
];
844 * helper to add 'address of tree root -> reloc tree' mapping
846 static int __add_reloc_root(struct btrfs_root
*root
)
848 struct rb_node
*rb_node
;
849 struct mapping_node
*node
;
850 struct reloc_control
*rc
= root
->fs_info
->reloc_ctl
;
852 node
= kmalloc(sizeof(*node
), GFP_NOFS
);
855 node
->bytenr
= root
->node
->start
;
858 spin_lock(&rc
->reloc_root_tree
.lock
);
859 rb_node
= tree_insert(&rc
->reloc_root_tree
.rb_root
,
860 node
->bytenr
, &node
->rb_node
);
861 spin_unlock(&rc
->reloc_root_tree
.lock
);
864 list_add_tail(&root
->root_list
, &rc
->reloc_roots
);
869 * helper to update/delete the 'address of tree root -> reloc tree'
872 static int __update_reloc_root(struct btrfs_root
*root
, int del
)
874 struct rb_node
*rb_node
;
875 struct mapping_node
*node
= NULL
;
876 struct reloc_control
*rc
= root
->fs_info
->reloc_ctl
;
878 spin_lock(&rc
->reloc_root_tree
.lock
);
879 rb_node
= tree_search(&rc
->reloc_root_tree
.rb_root
,
880 root
->commit_root
->start
);
882 node
= rb_entry(rb_node
, struct mapping_node
, rb_node
);
883 rb_erase(&node
->rb_node
, &rc
->reloc_root_tree
.rb_root
);
885 spin_unlock(&rc
->reloc_root_tree
.lock
);
887 BUG_ON((struct btrfs_root
*)node
->data
!= root
);
890 spin_lock(&rc
->reloc_root_tree
.lock
);
891 node
->bytenr
= root
->node
->start
;
892 rb_node
= tree_insert(&rc
->reloc_root_tree
.rb_root
,
893 node
->bytenr
, &node
->rb_node
);
894 spin_unlock(&rc
->reloc_root_tree
.lock
);
897 list_del_init(&root
->root_list
);
904 * create reloc tree for a given fs tree. reloc tree is just a
905 * snapshot of the fs tree with special root objectid.
907 int btrfs_init_reloc_root(struct btrfs_trans_handle
*trans
,
908 struct btrfs_root
*root
)
910 struct btrfs_root
*reloc_root
;
911 struct extent_buffer
*eb
;
912 struct btrfs_root_item
*root_item
;
913 struct btrfs_key root_key
;
916 if (root
->reloc_root
) {
917 reloc_root
= root
->reloc_root
;
918 reloc_root
->last_trans
= trans
->transid
;
922 if (!root
->fs_info
->reloc_ctl
||
923 !root
->fs_info
->reloc_ctl
->create_reloc_root
||
924 root
->root_key
.objectid
== BTRFS_TREE_RELOC_OBJECTID
)
927 root_item
= kmalloc(sizeof(*root_item
), GFP_NOFS
);
930 root_key
.objectid
= BTRFS_TREE_RELOC_OBJECTID
;
931 root_key
.type
= BTRFS_ROOT_ITEM_KEY
;
932 root_key
.offset
= root
->root_key
.objectid
;
934 ret
= btrfs_copy_root(trans
, root
, root
->commit_root
, &eb
,
935 BTRFS_TREE_RELOC_OBJECTID
);
938 btrfs_set_root_last_snapshot(&root
->root_item
, trans
->transid
- 1);
939 memcpy(root_item
, &root
->root_item
, sizeof(*root_item
));
940 btrfs_set_root_refs(root_item
, 1);
941 btrfs_set_root_bytenr(root_item
, eb
->start
);
942 btrfs_set_root_level(root_item
, btrfs_header_level(eb
));
943 btrfs_set_root_generation(root_item
, trans
->transid
);
944 memset(&root_item
->drop_progress
, 0, sizeof(struct btrfs_disk_key
));
945 root_item
->drop_level
= 0;
947 btrfs_tree_unlock(eb
);
948 free_extent_buffer(eb
);
950 ret
= btrfs_insert_root(trans
, root
->fs_info
->tree_root
,
951 &root_key
, root_item
);
955 reloc_root
= btrfs_read_fs_root_no_radix(root
->fs_info
->tree_root
,
957 BUG_ON(IS_ERR(reloc_root
));
958 reloc_root
->last_trans
= trans
->transid
;
960 __add_reloc_root(reloc_root
);
961 root
->reloc_root
= reloc_root
;
966 * update root item of reloc tree
968 int btrfs_update_reloc_root(struct btrfs_trans_handle
*trans
,
969 struct btrfs_root
*root
)
971 struct btrfs_root
*reloc_root
;
972 struct btrfs_root_item
*root_item
;
976 if (!root
->reloc_root
)
979 reloc_root
= root
->reloc_root
;
980 root_item
= &reloc_root
->root_item
;
982 if (btrfs_root_refs(root_item
) == 0) {
983 root
->reloc_root
= NULL
;
987 __update_reloc_root(reloc_root
, del
);
989 if (reloc_root
->commit_root
!= reloc_root
->node
) {
990 btrfs_set_root_node(root_item
, reloc_root
->node
);
991 free_extent_buffer(reloc_root
->commit_root
);
992 reloc_root
->commit_root
= btrfs_root_node(reloc_root
);
995 ret
= btrfs_update_root(trans
, root
->fs_info
->tree_root
,
996 &reloc_root
->root_key
, root_item
);
1002 * helper to find first cached inode with inode number >= objectid
1005 static struct inode
*find_next_inode(struct btrfs_root
*root
, u64 objectid
)
1007 struct rb_node
*node
;
1008 struct rb_node
*prev
;
1009 struct btrfs_inode
*entry
;
1010 struct inode
*inode
;
1012 spin_lock(&root
->inode_lock
);
1014 node
= root
->inode_tree
.rb_node
;
1018 entry
= rb_entry(node
, struct btrfs_inode
, rb_node
);
1020 if (objectid
< entry
->vfs_inode
.i_ino
)
1021 node
= node
->rb_left
;
1022 else if (objectid
> entry
->vfs_inode
.i_ino
)
1023 node
= node
->rb_right
;
1029 entry
= rb_entry(prev
, struct btrfs_inode
, rb_node
);
1030 if (objectid
<= entry
->vfs_inode
.i_ino
) {
1034 prev
= rb_next(prev
);
1038 entry
= rb_entry(node
, struct btrfs_inode
, rb_node
);
1039 inode
= igrab(&entry
->vfs_inode
);
1041 spin_unlock(&root
->inode_lock
);
1045 objectid
= entry
->vfs_inode
.i_ino
+ 1;
1046 if (cond_resched_lock(&root
->inode_lock
))
1049 node
= rb_next(node
);
1051 spin_unlock(&root
->inode_lock
);
1055 static int in_block_group(u64 bytenr
,
1056 struct btrfs_block_group_cache
*block_group
)
1058 if (bytenr
>= block_group
->key
.objectid
&&
1059 bytenr
< block_group
->key
.objectid
+ block_group
->key
.offset
)
1065 * get new location of data
1067 static int get_new_location(struct inode
*reloc_inode
, u64
*new_bytenr
,
1068 u64 bytenr
, u64 num_bytes
)
1070 struct btrfs_root
*root
= BTRFS_I(reloc_inode
)->root
;
1071 struct btrfs_path
*path
;
1072 struct btrfs_file_extent_item
*fi
;
1073 struct extent_buffer
*leaf
;
1076 path
= btrfs_alloc_path();
1080 bytenr
-= BTRFS_I(reloc_inode
)->index_cnt
;
1081 ret
= btrfs_lookup_file_extent(NULL
, root
, path
, reloc_inode
->i_ino
,
1090 leaf
= path
->nodes
[0];
1091 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
1092 struct btrfs_file_extent_item
);
1094 BUG_ON(btrfs_file_extent_offset(leaf
, fi
) ||
1095 btrfs_file_extent_compression(leaf
, fi
) ||
1096 btrfs_file_extent_encryption(leaf
, fi
) ||
1097 btrfs_file_extent_other_encoding(leaf
, fi
));
1099 if (num_bytes
!= btrfs_file_extent_disk_num_bytes(leaf
, fi
)) {
1105 *new_bytenr
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
1108 btrfs_free_path(path
);
1113 * update file extent items in the tree leaf to point to
1114 * the new locations.
1116 static int replace_file_extents(struct btrfs_trans_handle
*trans
,
1117 struct reloc_control
*rc
,
1118 struct btrfs_root
*root
,
1119 struct extent_buffer
*leaf
,
1120 struct list_head
*inode_list
)
1122 struct btrfs_key key
;
1123 struct btrfs_file_extent_item
*fi
;
1124 struct inode
*inode
= NULL
;
1125 struct inodevec
*ivec
= NULL
;
1137 if (rc
->stage
!= UPDATE_DATA_PTRS
)
1140 /* reloc trees always use full backref */
1141 if (root
->root_key
.objectid
== BTRFS_TREE_RELOC_OBJECTID
)
1142 parent
= leaf
->start
;
1146 nritems
= btrfs_header_nritems(leaf
);
1147 for (i
= 0; i
< nritems
; i
++) {
1149 btrfs_item_key_to_cpu(leaf
, &key
, i
);
1150 if (key
.type
!= BTRFS_EXTENT_DATA_KEY
)
1152 fi
= btrfs_item_ptr(leaf
, i
, struct btrfs_file_extent_item
);
1153 if (btrfs_file_extent_type(leaf
, fi
) ==
1154 BTRFS_FILE_EXTENT_INLINE
)
1156 bytenr
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
1157 num_bytes
= btrfs_file_extent_disk_num_bytes(leaf
, fi
);
1160 if (!in_block_group(bytenr
, rc
->block_group
))
1164 * if we are modifying block in fs tree, wait for readpage
1165 * to complete and drop the extent cache
1167 if (root
->root_key
.objectid
!= BTRFS_TREE_RELOC_OBJECTID
) {
1168 if (!ivec
|| ivec
->nr
== INODEVEC_SIZE
) {
1169 ivec
= kmalloc(sizeof(*ivec
), GFP_NOFS
);
1172 list_add_tail(&ivec
->list
, inode_list
);
1175 inode
= find_next_inode(root
, key
.objectid
);
1177 ivec
->inode
[ivec
->nr
++] = inode
;
1179 } else if (inode
&& inode
->i_ino
< key
.objectid
) {
1180 inode
= find_next_inode(root
, key
.objectid
);
1182 ivec
->inode
[ivec
->nr
++] = inode
;
1184 if (inode
&& inode
->i_ino
== key
.objectid
) {
1186 btrfs_file_extent_num_bytes(leaf
, fi
);
1187 WARN_ON(!IS_ALIGNED(key
.offset
,
1189 WARN_ON(!IS_ALIGNED(end
, root
->sectorsize
));
1191 ret
= try_lock_extent(&BTRFS_I(inode
)->io_tree
,
1197 btrfs_drop_extent_cache(inode
, key
.offset
, end
,
1199 unlock_extent(&BTRFS_I(inode
)->io_tree
,
1200 key
.offset
, end
, GFP_NOFS
);
1204 ret
= get_new_location(rc
->data_inode
, &new_bytenr
,
1210 btrfs_set_file_extent_disk_bytenr(leaf
, fi
, new_bytenr
);
1213 key
.offset
-= btrfs_file_extent_offset(leaf
, fi
);
1214 ret
= btrfs_inc_extent_ref(trans
, root
, new_bytenr
,
1216 btrfs_header_owner(leaf
),
1217 key
.objectid
, key
.offset
);
1220 ret
= btrfs_free_extent(trans
, root
, bytenr
, num_bytes
,
1221 parent
, btrfs_header_owner(leaf
),
1222 key
.objectid
, key
.offset
);
1226 btrfs_mark_buffer_dirty(leaf
);
1230 static noinline_for_stack
1231 int memcmp_node_keys(struct extent_buffer
*eb
, int slot
,
1232 struct btrfs_path
*path
, int level
)
1234 struct btrfs_disk_key key1
;
1235 struct btrfs_disk_key key2
;
1236 btrfs_node_key(eb
, &key1
, slot
);
1237 btrfs_node_key(path
->nodes
[level
], &key2
, path
->slots
[level
]);
1238 return memcmp(&key1
, &key2
, sizeof(key1
));
1242 * try to replace tree blocks in fs tree with the new blocks
1243 * in reloc tree. tree blocks haven't been modified since the
1244 * reloc tree was create can be replaced.
1246 * if a block was replaced, level of the block + 1 is returned.
1247 * if no block got replaced, 0 is returned. if there are other
1248 * errors, a negative error number is returned.
1250 static int replace_path(struct btrfs_trans_handle
*trans
,
1251 struct btrfs_root
*dest
, struct btrfs_root
*src
,
1252 struct btrfs_path
*path
, struct btrfs_key
*next_key
,
1253 struct extent_buffer
**leaf
,
1254 int lowest_level
, int max_level
)
1256 struct extent_buffer
*eb
;
1257 struct extent_buffer
*parent
;
1258 struct btrfs_key key
;
1269 BUG_ON(src
->root_key
.objectid
!= BTRFS_TREE_RELOC_OBJECTID
);
1270 BUG_ON(dest
->root_key
.objectid
== BTRFS_TREE_RELOC_OBJECTID
);
1271 BUG_ON(lowest_level
> 1 && leaf
);
1273 last_snapshot
= btrfs_root_last_snapshot(&src
->root_item
);
1275 slot
= path
->slots
[lowest_level
];
1276 btrfs_node_key_to_cpu(path
->nodes
[lowest_level
], &key
, slot
);
1278 eb
= btrfs_lock_root_node(dest
);
1279 btrfs_set_lock_blocking(eb
);
1280 level
= btrfs_header_level(eb
);
1282 if (level
< lowest_level
) {
1283 btrfs_tree_unlock(eb
);
1284 free_extent_buffer(eb
);
1288 ret
= btrfs_cow_block(trans
, dest
, eb
, NULL
, 0, &eb
);
1290 btrfs_set_lock_blocking(eb
);
1293 next_key
->objectid
= (u64
)-1;
1294 next_key
->type
= (u8
)-1;
1295 next_key
->offset
= (u64
)-1;
1300 level
= btrfs_header_level(parent
);
1301 BUG_ON(level
< lowest_level
);
1303 ret
= btrfs_bin_search(parent
, &key
, level
, &slot
);
1304 if (ret
&& slot
> 0)
1307 if (next_key
&& slot
+ 1 < btrfs_header_nritems(parent
))
1308 btrfs_node_key_to_cpu(parent
, next_key
, slot
+ 1);
1310 old_bytenr
= btrfs_node_blockptr(parent
, slot
);
1311 blocksize
= btrfs_level_size(dest
, level
- 1);
1312 old_ptr_gen
= btrfs_node_ptr_generation(parent
, slot
);
1314 if (level
<= max_level
) {
1315 eb
= path
->nodes
[level
];
1316 new_bytenr
= btrfs_node_blockptr(eb
,
1317 path
->slots
[level
]);
1318 new_ptr_gen
= btrfs_node_ptr_generation(eb
,
1319 path
->slots
[level
]);
1325 if (new_bytenr
> 0 && new_bytenr
== old_bytenr
) {
1331 if (new_bytenr
== 0 || old_ptr_gen
> last_snapshot
||
1332 memcmp_node_keys(parent
, slot
, path
, level
)) {
1333 if (level
<= lowest_level
&& !leaf
) {
1338 eb
= read_tree_block(dest
, old_bytenr
, blocksize
,
1340 btrfs_tree_lock(eb
);
1341 ret
= btrfs_cow_block(trans
, dest
, eb
, parent
,
1344 btrfs_set_lock_blocking(eb
);
1346 if (level
<= lowest_level
) {
1352 btrfs_tree_unlock(parent
);
1353 free_extent_buffer(parent
);
1359 btrfs_node_key_to_cpu(path
->nodes
[level
], &key
,
1360 path
->slots
[level
]);
1361 btrfs_release_path(src
, path
);
1363 path
->lowest_level
= level
;
1364 ret
= btrfs_search_slot(trans
, src
, &key
, path
, 0, 1);
1365 path
->lowest_level
= 0;
1369 * swap blocks in fs tree and reloc tree.
1371 btrfs_set_node_blockptr(parent
, slot
, new_bytenr
);
1372 btrfs_set_node_ptr_generation(parent
, slot
, new_ptr_gen
);
1373 btrfs_mark_buffer_dirty(parent
);
1375 btrfs_set_node_blockptr(path
->nodes
[level
],
1376 path
->slots
[level
], old_bytenr
);
1377 btrfs_set_node_ptr_generation(path
->nodes
[level
],
1378 path
->slots
[level
], old_ptr_gen
);
1379 btrfs_mark_buffer_dirty(path
->nodes
[level
]);
1381 ret
= btrfs_inc_extent_ref(trans
, src
, old_bytenr
, blocksize
,
1382 path
->nodes
[level
]->start
,
1383 src
->root_key
.objectid
, level
- 1, 0);
1385 ret
= btrfs_inc_extent_ref(trans
, dest
, new_bytenr
, blocksize
,
1386 0, dest
->root_key
.objectid
, level
- 1,
1390 ret
= btrfs_free_extent(trans
, src
, new_bytenr
, blocksize
,
1391 path
->nodes
[level
]->start
,
1392 src
->root_key
.objectid
, level
- 1, 0);
1395 ret
= btrfs_free_extent(trans
, dest
, old_bytenr
, blocksize
,
1396 0, dest
->root_key
.objectid
, level
- 1,
1400 btrfs_unlock_up_safe(path
, 0);
1405 btrfs_tree_unlock(parent
);
1406 free_extent_buffer(parent
);
1411 * helper to find next relocated block in reloc tree
1413 static noinline_for_stack
1414 int walk_up_reloc_tree(struct btrfs_root
*root
, struct btrfs_path
*path
,
1417 struct extent_buffer
*eb
;
1422 last_snapshot
= btrfs_root_last_snapshot(&root
->root_item
);
1424 for (i
= 0; i
< *level
; i
++) {
1425 free_extent_buffer(path
->nodes
[i
]);
1426 path
->nodes
[i
] = NULL
;
1429 for (i
= *level
; i
< BTRFS_MAX_LEVEL
&& path
->nodes
[i
]; i
++) {
1430 eb
= path
->nodes
[i
];
1431 nritems
= btrfs_header_nritems(eb
);
1432 while (path
->slots
[i
] + 1 < nritems
) {
1434 if (btrfs_node_ptr_generation(eb
, path
->slots
[i
]) <=
1441 free_extent_buffer(path
->nodes
[i
]);
1442 path
->nodes
[i
] = NULL
;
1448 * walk down reloc tree to find relocated block of lowest level
1450 static noinline_for_stack
1451 int walk_down_reloc_tree(struct btrfs_root
*root
, struct btrfs_path
*path
,
1454 struct extent_buffer
*eb
= NULL
;
1462 last_snapshot
= btrfs_root_last_snapshot(&root
->root_item
);
1464 for (i
= *level
; i
> 0; i
--) {
1465 eb
= path
->nodes
[i
];
1466 nritems
= btrfs_header_nritems(eb
);
1467 while (path
->slots
[i
] < nritems
) {
1468 ptr_gen
= btrfs_node_ptr_generation(eb
, path
->slots
[i
]);
1469 if (ptr_gen
> last_snapshot
)
1473 if (path
->slots
[i
] >= nritems
) {
1484 bytenr
= btrfs_node_blockptr(eb
, path
->slots
[i
]);
1485 blocksize
= btrfs_level_size(root
, i
- 1);
1486 eb
= read_tree_block(root
, bytenr
, blocksize
, ptr_gen
);
1487 BUG_ON(btrfs_header_level(eb
) != i
- 1);
1488 path
->nodes
[i
- 1] = eb
;
1489 path
->slots
[i
- 1] = 0;
1495 * invalidate extent cache for file extents whose key in range of
1496 * [min_key, max_key)
1498 static int invalidate_extent_cache(struct btrfs_root
*root
,
1499 struct btrfs_key
*min_key
,
1500 struct btrfs_key
*max_key
)
1502 struct inode
*inode
= NULL
;
1506 objectid
= min_key
->objectid
;
1511 if (objectid
> max_key
->objectid
)
1514 inode
= find_next_inode(root
, objectid
);
1518 if (inode
->i_ino
> max_key
->objectid
) {
1523 objectid
= inode
->i_ino
+ 1;
1524 if (!S_ISREG(inode
->i_mode
))
1527 if (unlikely(min_key
->objectid
== inode
->i_ino
)) {
1528 if (min_key
->type
> BTRFS_EXTENT_DATA_KEY
)
1530 if (min_key
->type
< BTRFS_EXTENT_DATA_KEY
)
1533 start
= min_key
->offset
;
1534 WARN_ON(!IS_ALIGNED(start
, root
->sectorsize
));
1540 if (unlikely(max_key
->objectid
== inode
->i_ino
)) {
1541 if (max_key
->type
< BTRFS_EXTENT_DATA_KEY
)
1543 if (max_key
->type
> BTRFS_EXTENT_DATA_KEY
) {
1546 if (max_key
->offset
== 0)
1548 end
= max_key
->offset
;
1549 WARN_ON(!IS_ALIGNED(end
, root
->sectorsize
));
1556 /* the lock_extent waits for readpage to complete */
1557 lock_extent(&BTRFS_I(inode
)->io_tree
, start
, end
, GFP_NOFS
);
1558 btrfs_drop_extent_cache(inode
, start
, end
, 1);
1559 unlock_extent(&BTRFS_I(inode
)->io_tree
, start
, end
, GFP_NOFS
);
1564 static int find_next_key(struct btrfs_path
*path
, int level
,
1565 struct btrfs_key
*key
)
1568 while (level
< BTRFS_MAX_LEVEL
) {
1569 if (!path
->nodes
[level
])
1571 if (path
->slots
[level
] + 1 <
1572 btrfs_header_nritems(path
->nodes
[level
])) {
1573 btrfs_node_key_to_cpu(path
->nodes
[level
], key
,
1574 path
->slots
[level
] + 1);
1583 * merge the relocated tree blocks in reloc tree with corresponding
1586 static noinline_for_stack
int merge_reloc_root(struct reloc_control
*rc
,
1587 struct btrfs_root
*root
)
1589 LIST_HEAD(inode_list
);
1590 struct btrfs_key key
;
1591 struct btrfs_key next_key
;
1592 struct btrfs_trans_handle
*trans
;
1593 struct btrfs_root
*reloc_root
;
1594 struct btrfs_root_item
*root_item
;
1595 struct btrfs_path
*path
;
1596 struct extent_buffer
*leaf
= NULL
;
1604 path
= btrfs_alloc_path();
1608 reloc_root
= root
->reloc_root
;
1609 root_item
= &reloc_root
->root_item
;
1611 if (btrfs_disk_key_objectid(&root_item
->drop_progress
) == 0) {
1612 level
= btrfs_root_level(root_item
);
1613 extent_buffer_get(reloc_root
->node
);
1614 path
->nodes
[level
] = reloc_root
->node
;
1615 path
->slots
[level
] = 0;
1617 btrfs_disk_key_to_cpu(&key
, &root_item
->drop_progress
);
1619 level
= root_item
->drop_level
;
1621 path
->lowest_level
= level
;
1622 ret
= btrfs_search_slot(NULL
, reloc_root
, &key
, path
, 0, 0);
1623 path
->lowest_level
= 0;
1625 btrfs_free_path(path
);
1629 btrfs_node_key_to_cpu(path
->nodes
[level
], &next_key
,
1630 path
->slots
[level
]);
1631 WARN_ON(memcmp(&key
, &next_key
, sizeof(key
)));
1633 btrfs_unlock_up_safe(path
, 0);
1636 if (level
== 0 && rc
->stage
== UPDATE_DATA_PTRS
) {
1637 trans
= btrfs_start_transaction(root
, 1);
1639 leaf
= path
->nodes
[0];
1640 btrfs_item_key_to_cpu(leaf
, &key
, 0);
1641 btrfs_release_path(reloc_root
, path
);
1643 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 1);
1649 leaf
= path
->nodes
[0];
1650 btrfs_unlock_up_safe(path
, 1);
1651 ret
= replace_file_extents(trans
, rc
, root
, leaf
,
1658 memset(&next_key
, 0, sizeof(next_key
));
1663 trans
= btrfs_start_transaction(root
, 1);
1666 ret
= walk_down_reloc_tree(reloc_root
, path
, &level
);
1674 if (!find_next_key(path
, level
, &key
) &&
1675 btrfs_comp_cpu_keys(&next_key
, &key
) >= 0) {
1677 } else if (level
== 1 && rc
->stage
== UPDATE_DATA_PTRS
) {
1678 ret
= replace_path(trans
, root
, reloc_root
,
1679 path
, &next_key
, &leaf
,
1682 ret
= replace_path(trans
, root
, reloc_root
,
1683 path
, &next_key
, NULL
,
1693 btrfs_node_key_to_cpu(path
->nodes
[level
], &key
,
1694 path
->slots
[level
]);
1698 * no block got replaced, try replacing file extents
1700 btrfs_item_key_to_cpu(leaf
, &key
, 0);
1701 ret
= replace_file_extents(trans
, rc
, root
, leaf
,
1703 btrfs_tree_unlock(leaf
);
1704 free_extent_buffer(leaf
);
1708 ret
= walk_up_reloc_tree(reloc_root
, path
, &level
);
1714 * save the merging progress in the drop_progress.
1715 * this is OK since root refs == 1 in this case.
1717 btrfs_node_key(path
->nodes
[level
], &root_item
->drop_progress
,
1718 path
->slots
[level
]);
1719 root_item
->drop_level
= level
;
1721 nr
= trans
->blocks_used
;
1722 btrfs_end_transaction(trans
, root
);
1724 btrfs_btree_balance_dirty(root
, nr
);
1726 if (replaced
&& rc
->stage
== UPDATE_DATA_PTRS
)
1727 invalidate_extent_cache(root
, &key
, &next_key
);
1731 * handle the case only one block in the fs tree need to be
1732 * relocated and the block is tree root.
1734 leaf
= btrfs_lock_root_node(root
);
1735 ret
= btrfs_cow_block(trans
, root
, leaf
, NULL
, 0, &leaf
);
1736 btrfs_tree_unlock(leaf
);
1737 free_extent_buffer(leaf
);
1741 btrfs_free_path(path
);
1744 memset(&root_item
->drop_progress
, 0,
1745 sizeof(root_item
->drop_progress
));
1746 root_item
->drop_level
= 0;
1747 btrfs_set_root_refs(root_item
, 0);
1750 nr
= trans
->blocks_used
;
1751 btrfs_end_transaction(trans
, root
);
1753 btrfs_btree_balance_dirty(root
, nr
);
1756 * put inodes while we aren't holding the tree locks
1758 while (!list_empty(&inode_list
)) {
1759 struct inodevec
*ivec
;
1760 ivec
= list_entry(inode_list
.next
, struct inodevec
, list
);
1761 list_del(&ivec
->list
);
1762 while (ivec
->nr
> 0) {
1764 iput(ivec
->inode
[ivec
->nr
]);
1769 if (replaced
&& rc
->stage
== UPDATE_DATA_PTRS
)
1770 invalidate_extent_cache(root
, &key
, &next_key
);
1776 * callback for the work threads.
1777 * this function merges reloc tree with corresponding fs tree,
1778 * and then drops the reloc tree.
1780 static void merge_func(struct btrfs_work
*work
)
1782 struct btrfs_trans_handle
*trans
;
1783 struct btrfs_root
*root
;
1784 struct btrfs_root
*reloc_root
;
1785 struct async_merge
*async
;
1787 async
= container_of(work
, struct async_merge
, work
);
1788 reloc_root
= async
->root
;
1790 if (btrfs_root_refs(&reloc_root
->root_item
) > 0) {
1791 root
= read_fs_root(reloc_root
->fs_info
,
1792 reloc_root
->root_key
.offset
);
1793 BUG_ON(IS_ERR(root
));
1794 BUG_ON(root
->reloc_root
!= reloc_root
);
1796 merge_reloc_root(async
->rc
, root
);
1798 trans
= btrfs_start_transaction(root
, 1);
1799 btrfs_update_reloc_root(trans
, root
);
1800 btrfs_end_transaction(trans
, root
);
1803 btrfs_drop_snapshot(reloc_root
, 0);
1805 if (atomic_dec_and_test(async
->num_pending
))
1806 complete(async
->done
);
1811 static int merge_reloc_roots(struct reloc_control
*rc
)
1813 struct async_merge
*async
;
1814 struct btrfs_root
*root
;
1815 struct completion done
;
1816 atomic_t num_pending
;
1818 init_completion(&done
);
1819 atomic_set(&num_pending
, 1);
1821 while (!list_empty(&rc
->reloc_roots
)) {
1822 root
= list_entry(rc
->reloc_roots
.next
,
1823 struct btrfs_root
, root_list
);
1824 list_del_init(&root
->root_list
);
1826 async
= kmalloc(sizeof(*async
), GFP_NOFS
);
1828 async
->work
.func
= merge_func
;
1829 async
->work
.flags
= 0;
1832 async
->done
= &done
;
1833 async
->num_pending
= &num_pending
;
1834 atomic_inc(&num_pending
);
1835 btrfs_queue_worker(&rc
->workers
, &async
->work
);
1838 if (!atomic_dec_and_test(&num_pending
))
1839 wait_for_completion(&done
);
1841 BUG_ON(!RB_EMPTY_ROOT(&rc
->reloc_root_tree
.rb_root
));
1845 static void free_block_list(struct rb_root
*blocks
)
1847 struct tree_block
*block
;
1848 struct rb_node
*rb_node
;
1849 while ((rb_node
= rb_first(blocks
))) {
1850 block
= rb_entry(rb_node
, struct tree_block
, rb_node
);
1851 rb_erase(rb_node
, blocks
);
1856 static int record_reloc_root_in_trans(struct btrfs_trans_handle
*trans
,
1857 struct btrfs_root
*reloc_root
)
1859 struct btrfs_root
*root
;
1861 if (reloc_root
->last_trans
== trans
->transid
)
1864 root
= read_fs_root(reloc_root
->fs_info
, reloc_root
->root_key
.offset
);
1865 BUG_ON(IS_ERR(root
));
1866 BUG_ON(root
->reloc_root
!= reloc_root
);
1868 return btrfs_record_root_in_trans(trans
, root
);
1872 * select one tree from trees that references the block.
1873 * for blocks in refernce counted trees, we preper reloc tree.
1874 * if no reloc tree found and reloc_only is true, NULL is returned.
1876 static struct btrfs_root
*__select_one_root(struct btrfs_trans_handle
*trans
,
1877 struct backref_node
*node
,
1878 struct backref_edge
*edges
[],
1879 int *nr
, int reloc_only
)
1881 struct backref_node
*next
;
1882 struct btrfs_root
*root
;
1890 next
= walk_up_backref(next
, edges
, &index
);
1893 BUG_ON(!node
->old_root
);
1897 /* no other choice for non-refernce counted tree */
1898 if (!root
->ref_cows
) {
1903 if (root
->root_key
.objectid
== BTRFS_TREE_RELOC_OBJECTID
) {
1904 record_reloc_root_in_trans(trans
, root
);
1909 btrfs_record_root_in_trans(trans
, root
);
1913 if (reloc_only
|| next
!= node
) {
1914 if (!root
->reloc_root
)
1915 btrfs_record_root_in_trans(trans
, root
);
1916 root
= root
->reloc_root
;
1918 * if the reloc tree was created in current
1919 * transation, there is no node in backref tree
1920 * corresponds to the root of the reloc tree.
1922 if (btrfs_root_last_snapshot(&root
->root_item
) ==
1928 next
= walk_down_backref(edges
, &index
);
1929 if (!next
|| next
->level
<= node
->level
)
1933 if (!root
&& !loop
&& !reloc_only
) {
1946 static noinline_for_stack
1947 struct btrfs_root
*select_one_root(struct btrfs_trans_handle
*trans
,
1948 struct backref_node
*node
)
1950 struct backref_edge
*edges
[BTRFS_MAX_LEVEL
- 1];
1952 return __select_one_root(trans
, node
, edges
, &nr
, 0);
1955 static noinline_for_stack
1956 struct btrfs_root
*select_reloc_root(struct btrfs_trans_handle
*trans
,
1957 struct backref_node
*node
,
1958 struct backref_edge
*edges
[], int *nr
)
1960 return __select_one_root(trans
, node
, edges
, nr
, 1);
1963 static void grab_path_buffers(struct btrfs_path
*path
,
1964 struct backref_node
*node
,
1965 struct backref_edge
*edges
[], int nr
)
1969 drop_node_buffer(node
);
1970 node
->eb
= path
->nodes
[node
->level
];
1972 if (path
->locks
[node
->level
])
1974 path
->nodes
[node
->level
] = NULL
;
1975 path
->locks
[node
->level
] = 0;
1980 edges
[i
]->blockptr
= node
->eb
->start
;
1981 node
= edges
[i
]->node
[UPPER
];
1987 * relocate a block tree, and then update pointers in upper level
1988 * blocks that reference the block to point to the new location.
1990 * if called by link_to_upper, the block has already been relocated.
1991 * in that case this function just updates pointers.
1993 static int do_relocation(struct btrfs_trans_handle
*trans
,
1994 struct backref_node
*node
,
1995 struct btrfs_key
*key
,
1996 struct btrfs_path
*path
, int lowest
)
1998 struct backref_node
*upper
;
1999 struct backref_edge
*edge
;
2000 struct backref_edge
*edges
[BTRFS_MAX_LEVEL
- 1];
2001 struct btrfs_root
*root
;
2002 struct extent_buffer
*eb
;
2011 BUG_ON(lowest
&& node
->eb
);
2013 path
->lowest_level
= node
->level
+ 1;
2014 list_for_each_entry(edge
, &node
->upper
, list
[LOWER
]) {
2016 if (node
->eb
&& node
->eb
->start
== edge
->blockptr
)
2019 upper
= edge
->node
[UPPER
];
2020 root
= select_reloc_root(trans
, upper
, edges
, &nr
);
2024 if (upper
->eb
&& !upper
->locked
)
2025 drop_node_buffer(upper
);
2028 ret
= btrfs_search_slot(trans
, root
, key
, path
, 0, 1);
2035 slot
= path
->slots
[upper
->level
];
2037 btrfs_unlock_up_safe(path
, upper
->level
+ 1);
2038 grab_path_buffers(path
, upper
, edges
, nr
);
2040 btrfs_release_path(NULL
, path
);
2042 ret
= btrfs_bin_search(upper
->eb
, key
, upper
->level
,
2047 bytenr
= btrfs_node_blockptr(upper
->eb
, slot
);
2049 if (node
->eb
->start
== bytenr
) {
2050 btrfs_tree_unlock(upper
->eb
);
2055 BUG_ON(node
->bytenr
!= bytenr
);
2058 blocksize
= btrfs_level_size(root
, node
->level
);
2059 generation
= btrfs_node_ptr_generation(upper
->eb
, slot
);
2060 eb
= read_tree_block(root
, bytenr
, blocksize
, generation
);
2061 btrfs_tree_lock(eb
);
2062 btrfs_set_lock_blocking(eb
);
2065 ret
= btrfs_cow_block(trans
, root
, eb
, upper
->eb
,
2071 btrfs_set_lock_blocking(eb
);
2075 btrfs_set_node_blockptr(upper
->eb
, slot
,
2077 btrfs_set_node_ptr_generation(upper
->eb
, slot
,
2079 btrfs_mark_buffer_dirty(upper
->eb
);
2081 ret
= btrfs_inc_extent_ref(trans
, root
,
2082 node
->eb
->start
, blocksize
,
2084 btrfs_header_owner(upper
->eb
),
2088 ret
= btrfs_drop_subtree(trans
, root
, eb
, upper
->eb
);
2092 btrfs_tree_unlock(upper
->eb
);
2096 path
->lowest_level
= 0;
2100 static int link_to_upper(struct btrfs_trans_handle
*trans
,
2101 struct backref_node
*node
,
2102 struct btrfs_path
*path
)
2104 struct btrfs_key key
;
2105 if (!node
->eb
|| list_empty(&node
->upper
))
2108 btrfs_node_key_to_cpu(node
->eb
, &key
, 0);
2109 return do_relocation(trans
, node
, &key
, path
, 0);
2112 static int finish_pending_nodes(struct btrfs_trans_handle
*trans
,
2113 struct backref_cache
*cache
,
2114 struct btrfs_path
*path
)
2116 struct backref_node
*node
;
2121 for (level
= 0; level
< BTRFS_MAX_LEVEL
; level
++) {
2122 while (!list_empty(&cache
->pending
[level
])) {
2123 node
= list_entry(cache
->pending
[level
].next
,
2124 struct backref_node
, lower
);
2125 BUG_ON(node
->level
!= level
);
2127 ret
= link_to_upper(trans
, node
, path
);
2131 * this remove the node from the pending list and
2132 * may add some other nodes to the level + 1
2135 remove_backref_node(cache
, node
);
2138 BUG_ON(!RB_EMPTY_ROOT(&cache
->rb_root
));
2142 static void mark_block_processed(struct reloc_control
*rc
,
2143 struct backref_node
*node
)
2146 if (node
->level
== 0 ||
2147 in_block_group(node
->bytenr
, rc
->block_group
)) {
2148 blocksize
= btrfs_level_size(rc
->extent_root
, node
->level
);
2149 set_extent_bits(&rc
->processed_blocks
, node
->bytenr
,
2150 node
->bytenr
+ blocksize
- 1, EXTENT_DIRTY
,
2153 node
->processed
= 1;
2157 * mark a block and all blocks directly/indirectly reference the block
2160 static void update_processed_blocks(struct reloc_control
*rc
,
2161 struct backref_node
*node
)
2163 struct backref_node
*next
= node
;
2164 struct backref_edge
*edge
;
2165 struct backref_edge
*edges
[BTRFS_MAX_LEVEL
- 1];
2171 if (next
->processed
)
2174 mark_block_processed(rc
, next
);
2176 if (list_empty(&next
->upper
))
2179 edge
= list_entry(next
->upper
.next
,
2180 struct backref_edge
, list
[LOWER
]);
2181 edges
[index
++] = edge
;
2182 next
= edge
->node
[UPPER
];
2184 next
= walk_down_backref(edges
, &index
);
2188 static int tree_block_processed(u64 bytenr
, u32 blocksize
,
2189 struct reloc_control
*rc
)
2191 if (test_range_bit(&rc
->processed_blocks
, bytenr
,
2192 bytenr
+ blocksize
- 1, EXTENT_DIRTY
, 1, NULL
))
2198 * check if there are any file extent pointers in the leaf point to
2199 * data require processing
2201 static int check_file_extents(struct reloc_control
*rc
,
2202 u64 bytenr
, u32 blocksize
, u64 ptr_gen
)
2204 struct btrfs_key found_key
;
2205 struct btrfs_file_extent_item
*fi
;
2206 struct extent_buffer
*leaf
;
2211 leaf
= read_tree_block(rc
->extent_root
, bytenr
, blocksize
, ptr_gen
);
2213 nritems
= btrfs_header_nritems(leaf
);
2214 for (i
= 0; i
< nritems
; i
++) {
2216 btrfs_item_key_to_cpu(leaf
, &found_key
, i
);
2217 if (found_key
.type
!= BTRFS_EXTENT_DATA_KEY
)
2219 fi
= btrfs_item_ptr(leaf
, i
, struct btrfs_file_extent_item
);
2220 if (btrfs_file_extent_type(leaf
, fi
) ==
2221 BTRFS_FILE_EXTENT_INLINE
)
2223 bytenr
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
2226 if (in_block_group(bytenr
, rc
->block_group
)) {
2231 free_extent_buffer(leaf
);
2236 * scan child blocks of a given block to find blocks require processing
2238 static int add_child_blocks(struct btrfs_trans_handle
*trans
,
2239 struct reloc_control
*rc
,
2240 struct backref_node
*node
,
2241 struct rb_root
*blocks
)
2243 struct tree_block
*block
;
2244 struct rb_node
*rb_node
;
2252 nritems
= btrfs_header_nritems(node
->eb
);
2253 blocksize
= btrfs_level_size(rc
->extent_root
, node
->level
- 1);
2254 for (i
= 0; i
< nritems
; i
++) {
2256 bytenr
= btrfs_node_blockptr(node
->eb
, i
);
2257 ptr_gen
= btrfs_node_ptr_generation(node
->eb
, i
);
2258 if (ptr_gen
== trans
->transid
)
2260 if (!in_block_group(bytenr
, rc
->block_group
) &&
2261 (node
->level
> 1 || rc
->stage
== MOVE_DATA_EXTENTS
))
2263 if (tree_block_processed(bytenr
, blocksize
, rc
))
2266 readahead_tree_block(rc
->extent_root
,
2267 bytenr
, blocksize
, ptr_gen
);
2270 for (i
= 0; i
< nritems
; i
++) {
2272 bytenr
= btrfs_node_blockptr(node
->eb
, i
);
2273 ptr_gen
= btrfs_node_ptr_generation(node
->eb
, i
);
2274 if (ptr_gen
== trans
->transid
)
2276 if (!in_block_group(bytenr
, rc
->block_group
) &&
2277 (node
->level
> 1 || rc
->stage
== MOVE_DATA_EXTENTS
))
2279 if (tree_block_processed(bytenr
, blocksize
, rc
))
2281 if (!in_block_group(bytenr
, rc
->block_group
) &&
2282 !check_file_extents(rc
, bytenr
, blocksize
, ptr_gen
))
2285 block
= kmalloc(sizeof(*block
), GFP_NOFS
);
2290 block
->bytenr
= bytenr
;
2291 btrfs_node_key_to_cpu(node
->eb
, &block
->key
, i
);
2292 block
->level
= node
->level
- 1;
2293 block
->key_ready
= 1;
2294 rb_node
= tree_insert(blocks
, block
->bytenr
, &block
->rb_node
);
2298 free_block_list(blocks
);
2303 * find adjacent blocks require processing
2305 static noinline_for_stack
2306 int add_adjacent_blocks(struct btrfs_trans_handle
*trans
,
2307 struct reloc_control
*rc
,
2308 struct backref_cache
*cache
,
2309 struct rb_root
*blocks
, int level
,
2310 struct backref_node
**upper
)
2312 struct backref_node
*node
;
2315 WARN_ON(!list_empty(&cache
->pending
[level
]));
2317 if (list_empty(&cache
->pending
[level
+ 1]))
2320 node
= list_entry(cache
->pending
[level
+ 1].next
,
2321 struct backref_node
, lower
);
2323 ret
= add_child_blocks(trans
, rc
, node
, blocks
);
2329 static int get_tree_block_key(struct reloc_control
*rc
,
2330 struct tree_block
*block
)
2332 struct extent_buffer
*eb
;
2334 BUG_ON(block
->key_ready
);
2335 eb
= read_tree_block(rc
->extent_root
, block
->bytenr
,
2336 block
->key
.objectid
, block
->key
.offset
);
2337 WARN_ON(btrfs_header_level(eb
) != block
->level
);
2338 if (block
->level
== 0)
2339 btrfs_item_key_to_cpu(eb
, &block
->key
, 0);
2341 btrfs_node_key_to_cpu(eb
, &block
->key
, 0);
2342 free_extent_buffer(eb
);
2343 block
->key_ready
= 1;
2347 static int reada_tree_block(struct reloc_control
*rc
,
2348 struct tree_block
*block
)
2350 BUG_ON(block
->key_ready
);
2351 readahead_tree_block(rc
->extent_root
, block
->bytenr
,
2352 block
->key
.objectid
, block
->key
.offset
);
2357 * helper function to relocate a tree block
2359 static int relocate_tree_block(struct btrfs_trans_handle
*trans
,
2360 struct reloc_control
*rc
,
2361 struct backref_node
*node
,
2362 struct btrfs_key
*key
,
2363 struct btrfs_path
*path
)
2365 struct btrfs_root
*root
;
2368 root
= select_one_root(trans
, node
);
2369 if (unlikely(!root
)) {
2370 rc
->found_old_snapshot
= 1;
2371 update_processed_blocks(rc
, node
);
2375 if (root
->root_key
.objectid
== BTRFS_TREE_RELOC_OBJECTID
) {
2376 ret
= do_relocation(trans
, node
, key
, path
, 1);
2379 if (node
->level
== 0 && rc
->stage
== UPDATE_DATA_PTRS
) {
2380 ret
= replace_file_extents(trans
, rc
, root
,
2385 drop_node_buffer(node
);
2386 } else if (!root
->ref_cows
) {
2387 path
->lowest_level
= node
->level
;
2388 ret
= btrfs_search_slot(trans
, root
, key
, path
, 0, 1);
2389 btrfs_release_path(root
, path
);
2392 } else if (root
!= node
->root
) {
2393 WARN_ON(node
->level
> 0 || rc
->stage
!= UPDATE_DATA_PTRS
);
2396 update_processed_blocks(rc
, node
);
2399 drop_node_buffer(node
);
2404 * relocate a list of blocks
2406 static noinline_for_stack
2407 int relocate_tree_blocks(struct btrfs_trans_handle
*trans
,
2408 struct reloc_control
*rc
, struct rb_root
*blocks
)
2410 struct backref_cache
*cache
;
2411 struct backref_node
*node
;
2412 struct btrfs_path
*path
;
2413 struct tree_block
*block
;
2414 struct rb_node
*rb_node
;
2419 path
= btrfs_alloc_path();
2423 cache
= kmalloc(sizeof(*cache
), GFP_NOFS
);
2425 btrfs_free_path(path
);
2429 backref_cache_init(cache
);
2431 rb_node
= rb_first(blocks
);
2433 block
= rb_entry(rb_node
, struct tree_block
, rb_node
);
2435 level
= block
->level
;
2437 BUG_ON(level
!= block
->level
);
2438 if (!block
->key_ready
)
2439 reada_tree_block(rc
, block
);
2440 rb_node
= rb_next(rb_node
);
2443 rb_node
= rb_first(blocks
);
2445 block
= rb_entry(rb_node
, struct tree_block
, rb_node
);
2446 if (!block
->key_ready
)
2447 get_tree_block_key(rc
, block
);
2448 rb_node
= rb_next(rb_node
);
2451 rb_node
= rb_first(blocks
);
2453 block
= rb_entry(rb_node
, struct tree_block
, rb_node
);
2455 node
= build_backref_tree(rc
, cache
, &block
->key
,
2456 block
->level
, block
->bytenr
);
2458 err
= PTR_ERR(node
);
2462 ret
= relocate_tree_block(trans
, rc
, node
, &block
->key
,
2468 remove_backref_node(cache
, node
);
2469 rb_node
= rb_next(rb_node
);
2475 free_block_list(blocks
);
2478 * now backrefs of some upper level tree blocks have been cached,
2479 * try relocating blocks referenced by these upper level blocks.
2482 struct backref_node
*upper
= NULL
;
2483 if (trans
->transaction
->in_commit
||
2484 trans
->transaction
->delayed_refs
.flushing
)
2487 ret
= add_adjacent_blocks(trans
, rc
, cache
, blocks
, level
,
2494 rb_node
= rb_first(blocks
);
2496 block
= rb_entry(rb_node
, struct tree_block
, rb_node
);
2497 if (trans
->transaction
->in_commit
||
2498 trans
->transaction
->delayed_refs
.flushing
)
2500 BUG_ON(!block
->key_ready
);
2501 node
= build_backref_tree(rc
, cache
, &block
->key
,
2502 level
, block
->bytenr
);
2504 err
= PTR_ERR(node
);
2508 ret
= relocate_tree_block(trans
, rc
, node
,
2514 remove_backref_node(cache
, node
);
2515 rb_node
= rb_next(rb_node
);
2517 free_block_list(blocks
);
2520 ret
= link_to_upper(trans
, upper
, path
);
2525 remove_backref_node(cache
, upper
);
2529 free_block_list(blocks
);
2531 ret
= finish_pending_nodes(trans
, cache
, path
);
2536 btrfs_free_path(path
);
2540 static noinline_for_stack
2541 int setup_extent_mapping(struct inode
*inode
, u64 start
, u64 end
,
2544 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2545 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
2546 struct extent_map
*em
;
2549 em
= alloc_extent_map(GFP_NOFS
);
2554 em
->len
= end
+ 1 - start
;
2555 em
->block_len
= em
->len
;
2556 em
->block_start
= block_start
;
2557 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
2558 set_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
2560 lock_extent(&BTRFS_I(inode
)->io_tree
, start
, end
, GFP_NOFS
);
2562 write_lock(&em_tree
->lock
);
2563 ret
= add_extent_mapping(em_tree
, em
);
2564 write_unlock(&em_tree
->lock
);
2565 if (ret
!= -EEXIST
) {
2566 free_extent_map(em
);
2569 btrfs_drop_extent_cache(inode
, start
, end
, 0);
2571 unlock_extent(&BTRFS_I(inode
)->io_tree
, start
, end
, GFP_NOFS
);
2575 static int relocate_file_extent_cluster(struct inode
*inode
,
2576 struct file_extent_cluster
*cluster
)
2580 u64 offset
= BTRFS_I(inode
)->index_cnt
;
2581 unsigned long index
;
2582 unsigned long last_index
;
2583 unsigned int dirty_page
= 0;
2585 struct file_ra_state
*ra
;
2592 ra
= kzalloc(sizeof(*ra
), GFP_NOFS
);
2596 index
= (cluster
->start
- offset
) >> PAGE_CACHE_SHIFT
;
2597 last_index
= (cluster
->end
- offset
) >> PAGE_CACHE_SHIFT
;
2599 mutex_lock(&inode
->i_mutex
);
2601 i_size_write(inode
, cluster
->end
+ 1 - offset
);
2602 ret
= setup_extent_mapping(inode
, cluster
->start
- offset
,
2603 cluster
->end
- offset
, cluster
->start
);
2607 file_ra_state_init(ra
, inode
->i_mapping
);
2609 WARN_ON(cluster
->start
!= cluster
->boundary
[0]);
2610 while (index
<= last_index
) {
2611 page
= find_lock_page(inode
->i_mapping
, index
);
2613 page_cache_sync_readahead(inode
->i_mapping
,
2615 last_index
+ 1 - index
);
2616 page
= grab_cache_page(inode
->i_mapping
, index
);
2623 if (PageReadahead(page
)) {
2624 page_cache_async_readahead(inode
->i_mapping
,
2625 ra
, NULL
, page
, index
,
2626 last_index
+ 1 - index
);
2629 if (!PageUptodate(page
)) {
2630 btrfs_readpage(NULL
, page
);
2632 if (!PageUptodate(page
)) {
2634 page_cache_release(page
);
2640 page_start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
2641 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
2643 lock_extent(&BTRFS_I(inode
)->io_tree
,
2644 page_start
, page_end
, GFP_NOFS
);
2646 set_page_extent_mapped(page
);
2648 if (nr
< cluster
->nr
&&
2649 page_start
+ offset
== cluster
->boundary
[nr
]) {
2650 set_extent_bits(&BTRFS_I(inode
)->io_tree
,
2651 page_start
, page_end
,
2652 EXTENT_BOUNDARY
, GFP_NOFS
);
2655 btrfs_set_extent_delalloc(inode
, page_start
, page_end
);
2657 set_page_dirty(page
);
2660 unlock_extent(&BTRFS_I(inode
)->io_tree
,
2661 page_start
, page_end
, GFP_NOFS
);
2663 page_cache_release(page
);
2666 if (nr
< cluster
->nr
&&
2667 page_end
+ 1 + offset
== cluster
->boundary
[nr
]) {
2668 balance_dirty_pages_ratelimited_nr(inode
->i_mapping
,
2674 balance_dirty_pages_ratelimited_nr(inode
->i_mapping
,
2677 WARN_ON(nr
!= cluster
->nr
);
2679 mutex_unlock(&inode
->i_mutex
);
2684 static noinline_for_stack
2685 int relocate_data_extent(struct inode
*inode
, struct btrfs_key
*extent_key
,
2686 struct file_extent_cluster
*cluster
)
2690 if (cluster
->nr
> 0 && extent_key
->objectid
!= cluster
->end
+ 1) {
2691 ret
= relocate_file_extent_cluster(inode
, cluster
);
2698 cluster
->start
= extent_key
->objectid
;
2700 BUG_ON(cluster
->nr
>= MAX_EXTENTS
);
2701 cluster
->end
= extent_key
->objectid
+ extent_key
->offset
- 1;
2702 cluster
->boundary
[cluster
->nr
] = extent_key
->objectid
;
2705 if (cluster
->nr
>= MAX_EXTENTS
) {
2706 ret
= relocate_file_extent_cluster(inode
, cluster
);
2714 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2715 static int get_ref_objectid_v0(struct reloc_control
*rc
,
2716 struct btrfs_path
*path
,
2717 struct btrfs_key
*extent_key
,
2718 u64
*ref_objectid
, int *path_change
)
2720 struct btrfs_key key
;
2721 struct extent_buffer
*leaf
;
2722 struct btrfs_extent_ref_v0
*ref0
;
2726 leaf
= path
->nodes
[0];
2727 slot
= path
->slots
[0];
2729 if (slot
>= btrfs_header_nritems(leaf
)) {
2730 ret
= btrfs_next_leaf(rc
->extent_root
, path
);
2734 leaf
= path
->nodes
[0];
2735 slot
= path
->slots
[0];
2739 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
2740 if (key
.objectid
!= extent_key
->objectid
)
2743 if (key
.type
!= BTRFS_EXTENT_REF_V0_KEY
) {
2747 ref0
= btrfs_item_ptr(leaf
, slot
,
2748 struct btrfs_extent_ref_v0
);
2749 *ref_objectid
= btrfs_ref_objectid_v0(leaf
, ref0
);
2757 * helper to add a tree block to the list.
2758 * the major work is getting the generation and level of the block
2760 static int add_tree_block(struct reloc_control
*rc
,
2761 struct btrfs_key
*extent_key
,
2762 struct btrfs_path
*path
,
2763 struct rb_root
*blocks
)
2765 struct extent_buffer
*eb
;
2766 struct btrfs_extent_item
*ei
;
2767 struct btrfs_tree_block_info
*bi
;
2768 struct tree_block
*block
;
2769 struct rb_node
*rb_node
;
2774 eb
= path
->nodes
[0];
2775 item_size
= btrfs_item_size_nr(eb
, path
->slots
[0]);
2777 if (item_size
>= sizeof(*ei
) + sizeof(*bi
)) {
2778 ei
= btrfs_item_ptr(eb
, path
->slots
[0],
2779 struct btrfs_extent_item
);
2780 bi
= (struct btrfs_tree_block_info
*)(ei
+ 1);
2781 generation
= btrfs_extent_generation(eb
, ei
);
2782 level
= btrfs_tree_block_level(eb
, bi
);
2784 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2788 BUG_ON(item_size
!= sizeof(struct btrfs_extent_item_v0
));
2789 ret
= get_ref_objectid_v0(rc
, path
, extent_key
,
2791 BUG_ON(ref_owner
>= BTRFS_MAX_LEVEL
);
2792 level
= (int)ref_owner
;
2793 /* FIXME: get real generation */
2800 btrfs_release_path(rc
->extent_root
, path
);
2802 BUG_ON(level
== -1);
2804 block
= kmalloc(sizeof(*block
), GFP_NOFS
);
2808 block
->bytenr
= extent_key
->objectid
;
2809 block
->key
.objectid
= extent_key
->offset
;
2810 block
->key
.offset
= generation
;
2811 block
->level
= level
;
2812 block
->key_ready
= 0;
2814 rb_node
= tree_insert(blocks
, block
->bytenr
, &block
->rb_node
);
2821 * helper to add tree blocks for backref of type BTRFS_SHARED_DATA_REF_KEY
2823 static int __add_tree_block(struct reloc_control
*rc
,
2824 u64 bytenr
, u32 blocksize
,
2825 struct rb_root
*blocks
)
2827 struct btrfs_path
*path
;
2828 struct btrfs_key key
;
2831 if (tree_block_processed(bytenr
, blocksize
, rc
))
2834 if (tree_search(blocks
, bytenr
))
2837 path
= btrfs_alloc_path();
2841 key
.objectid
= bytenr
;
2842 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
2843 key
.offset
= blocksize
;
2845 path
->search_commit_root
= 1;
2846 path
->skip_locking
= 1;
2847 ret
= btrfs_search_slot(NULL
, rc
->extent_root
, &key
, path
, 0, 0);
2852 btrfs_item_key_to_cpu(path
->nodes
[0], &key
, path
->slots
[0]);
2853 ret
= add_tree_block(rc
, &key
, path
, blocks
);
2855 btrfs_free_path(path
);
2860 * helper to check if the block use full backrefs for pointers in it
2862 static int block_use_full_backref(struct reloc_control
*rc
,
2863 struct extent_buffer
*eb
)
2865 struct btrfs_path
*path
;
2866 struct btrfs_extent_item
*ei
;
2867 struct btrfs_key key
;
2871 if (btrfs_header_flag(eb
, BTRFS_HEADER_FLAG_RELOC
) ||
2872 btrfs_header_backref_rev(eb
) < BTRFS_MIXED_BACKREF_REV
)
2875 path
= btrfs_alloc_path();
2878 key
.objectid
= eb
->start
;
2879 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
2880 key
.offset
= eb
->len
;
2882 path
->search_commit_root
= 1;
2883 path
->skip_locking
= 1;
2884 ret
= btrfs_search_slot(NULL
, rc
->extent_root
,
2888 ei
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
2889 struct btrfs_extent_item
);
2890 flags
= btrfs_extent_flags(path
->nodes
[0], ei
);
2891 BUG_ON(!(flags
& BTRFS_EXTENT_FLAG_TREE_BLOCK
));
2892 if (flags
& BTRFS_BLOCK_FLAG_FULL_BACKREF
)
2896 btrfs_free_path(path
);
2901 * helper to add tree blocks for backref of type BTRFS_EXTENT_DATA_REF_KEY
2902 * this function scans fs tree to find blocks reference the data extent
2904 static int find_data_references(struct reloc_control
*rc
,
2905 struct btrfs_key
*extent_key
,
2906 struct extent_buffer
*leaf
,
2907 struct btrfs_extent_data_ref
*ref
,
2908 struct rb_root
*blocks
)
2910 struct btrfs_path
*path
;
2911 struct tree_block
*block
;
2912 struct btrfs_root
*root
;
2913 struct btrfs_file_extent_item
*fi
;
2914 struct rb_node
*rb_node
;
2915 struct btrfs_key key
;
2926 path
= btrfs_alloc_path();
2930 ref_root
= btrfs_extent_data_ref_root(leaf
, ref
);
2931 ref_objectid
= btrfs_extent_data_ref_objectid(leaf
, ref
);
2932 ref_offset
= btrfs_extent_data_ref_offset(leaf
, ref
);
2933 ref_count
= btrfs_extent_data_ref_count(leaf
, ref
);
2935 root
= read_fs_root(rc
->extent_root
->fs_info
, ref_root
);
2937 err
= PTR_ERR(root
);
2941 key
.objectid
= ref_objectid
;
2942 key
.offset
= ref_offset
;
2943 key
.type
= BTRFS_EXTENT_DATA_KEY
;
2945 path
->search_commit_root
= 1;
2946 path
->skip_locking
= 1;
2947 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2953 leaf
= path
->nodes
[0];
2954 nritems
= btrfs_header_nritems(leaf
);
2956 * the references in tree blocks that use full backrefs
2957 * are not counted in
2959 if (block_use_full_backref(rc
, leaf
))
2963 rb_node
= tree_search(blocks
, leaf
->start
);
2968 path
->slots
[0] = nritems
;
2971 while (ref_count
> 0) {
2972 while (path
->slots
[0] >= nritems
) {
2973 ret
= btrfs_next_leaf(root
, path
);
2983 leaf
= path
->nodes
[0];
2984 nritems
= btrfs_header_nritems(leaf
);
2987 if (block_use_full_backref(rc
, leaf
))
2991 rb_node
= tree_search(blocks
, leaf
->start
);
2996 path
->slots
[0] = nritems
;
3000 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
3001 if (key
.objectid
!= ref_objectid
||
3002 key
.type
!= BTRFS_EXTENT_DATA_KEY
) {
3007 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
3008 struct btrfs_file_extent_item
);
3010 if (btrfs_file_extent_type(leaf
, fi
) ==
3011 BTRFS_FILE_EXTENT_INLINE
)
3014 if (btrfs_file_extent_disk_bytenr(leaf
, fi
) !=
3015 extent_key
->objectid
)
3018 key
.offset
-= btrfs_file_extent_offset(leaf
, fi
);
3019 if (key
.offset
!= ref_offset
)
3027 if (!tree_block_processed(leaf
->start
, leaf
->len
, rc
)) {
3028 block
= kmalloc(sizeof(*block
), GFP_NOFS
);
3033 block
->bytenr
= leaf
->start
;
3034 btrfs_item_key_to_cpu(leaf
, &block
->key
, 0);
3036 block
->key_ready
= 1;
3037 rb_node
= tree_insert(blocks
, block
->bytenr
,
3044 path
->slots
[0] = nritems
;
3050 btrfs_free_path(path
);
3055 * hepler to find all tree blocks that reference a given data extent
3057 static noinline_for_stack
3058 int add_data_references(struct reloc_control
*rc
,
3059 struct btrfs_key
*extent_key
,
3060 struct btrfs_path
*path
,
3061 struct rb_root
*blocks
)
3063 struct btrfs_key key
;
3064 struct extent_buffer
*eb
;
3065 struct btrfs_extent_data_ref
*dref
;
3066 struct btrfs_extent_inline_ref
*iref
;
3073 ret
= get_new_location(rc
->data_inode
, NULL
, extent_key
->objectid
,
3074 extent_key
->offset
);
3077 /* the relocated data is fragmented */
3078 rc
->extents_skipped
++;
3079 btrfs_release_path(rc
->extent_root
, path
);
3083 blocksize
= btrfs_level_size(rc
->extent_root
, 0);
3085 eb
= path
->nodes
[0];
3086 ptr
= btrfs_item_ptr_offset(eb
, path
->slots
[0]);
3087 end
= ptr
+ btrfs_item_size_nr(eb
, path
->slots
[0]);
3088 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3089 if (ptr
+ sizeof(struct btrfs_extent_item_v0
) == end
)
3093 ptr
+= sizeof(struct btrfs_extent_item
);
3096 iref
= (struct btrfs_extent_inline_ref
*)ptr
;
3097 key
.type
= btrfs_extent_inline_ref_type(eb
, iref
);
3098 if (key
.type
== BTRFS_SHARED_DATA_REF_KEY
) {
3099 key
.offset
= btrfs_extent_inline_ref_offset(eb
, iref
);
3100 ret
= __add_tree_block(rc
, key
.offset
, blocksize
,
3102 } else if (key
.type
== BTRFS_EXTENT_DATA_REF_KEY
) {
3103 dref
= (struct btrfs_extent_data_ref
*)(&iref
->offset
);
3104 ret
= find_data_references(rc
, extent_key
,
3109 ptr
+= btrfs_extent_inline_ref_size(key
.type
);
3115 eb
= path
->nodes
[0];
3116 if (path
->slots
[0] >= btrfs_header_nritems(eb
)) {
3117 ret
= btrfs_next_leaf(rc
->extent_root
, path
);
3124 eb
= path
->nodes
[0];
3127 btrfs_item_key_to_cpu(eb
, &key
, path
->slots
[0]);
3128 if (key
.objectid
!= extent_key
->objectid
)
3131 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3132 if (key
.type
== BTRFS_SHARED_DATA_REF_KEY
||
3133 key
.type
== BTRFS_EXTENT_REF_V0_KEY
) {
3135 BUG_ON(key
.type
== BTRFS_EXTENT_REF_V0_KEY
);
3136 if (key
.type
== BTRFS_SHARED_DATA_REF_KEY
) {
3138 ret
= __add_tree_block(rc
, key
.offset
, blocksize
,
3140 } else if (key
.type
== BTRFS_EXTENT_DATA_REF_KEY
) {
3141 dref
= btrfs_item_ptr(eb
, path
->slots
[0],
3142 struct btrfs_extent_data_ref
);
3143 ret
= find_data_references(rc
, extent_key
,
3154 btrfs_release_path(rc
->extent_root
, path
);
3156 free_block_list(blocks
);
3161 * hepler to find next unprocessed extent
3163 static noinline_for_stack
3164 int find_next_extent(struct btrfs_trans_handle
*trans
,
3165 struct reloc_control
*rc
, struct btrfs_path
*path
)
3167 struct btrfs_key key
;
3168 struct extent_buffer
*leaf
;
3169 u64 start
, end
, last
;
3172 last
= rc
->block_group
->key
.objectid
+ rc
->block_group
->key
.offset
;
3175 if (rc
->search_start
>= last
) {
3180 key
.objectid
= rc
->search_start
;
3181 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
3184 path
->search_commit_root
= 1;
3185 path
->skip_locking
= 1;
3186 ret
= btrfs_search_slot(NULL
, rc
->extent_root
, &key
, path
,
3191 leaf
= path
->nodes
[0];
3192 if (path
->slots
[0] >= btrfs_header_nritems(leaf
)) {
3193 ret
= btrfs_next_leaf(rc
->extent_root
, path
);
3196 leaf
= path
->nodes
[0];
3199 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
3200 if (key
.objectid
>= last
) {
3205 if (key
.type
!= BTRFS_EXTENT_ITEM_KEY
||
3206 key
.objectid
+ key
.offset
<= rc
->search_start
) {
3211 ret
= find_first_extent_bit(&rc
->processed_blocks
,
3212 key
.objectid
, &start
, &end
,
3215 if (ret
== 0 && start
<= key
.objectid
) {
3216 btrfs_release_path(rc
->extent_root
, path
);
3217 rc
->search_start
= end
+ 1;
3219 rc
->search_start
= key
.objectid
+ key
.offset
;
3223 btrfs_release_path(rc
->extent_root
, path
);
3227 static void set_reloc_control(struct reloc_control
*rc
)
3229 struct btrfs_fs_info
*fs_info
= rc
->extent_root
->fs_info
;
3230 mutex_lock(&fs_info
->trans_mutex
);
3231 fs_info
->reloc_ctl
= rc
;
3232 mutex_unlock(&fs_info
->trans_mutex
);
3235 static void unset_reloc_control(struct reloc_control
*rc
)
3237 struct btrfs_fs_info
*fs_info
= rc
->extent_root
->fs_info
;
3238 mutex_lock(&fs_info
->trans_mutex
);
3239 fs_info
->reloc_ctl
= NULL
;
3240 mutex_unlock(&fs_info
->trans_mutex
);
3243 static int check_extent_flags(u64 flags
)
3245 if ((flags
& BTRFS_EXTENT_FLAG_DATA
) &&
3246 (flags
& BTRFS_EXTENT_FLAG_TREE_BLOCK
))
3248 if (!(flags
& BTRFS_EXTENT_FLAG_DATA
) &&
3249 !(flags
& BTRFS_EXTENT_FLAG_TREE_BLOCK
))
3251 if ((flags
& BTRFS_EXTENT_FLAG_DATA
) &&
3252 (flags
& BTRFS_BLOCK_FLAG_FULL_BACKREF
))
3258 static noinline_for_stack
int relocate_block_group(struct reloc_control
*rc
)
3260 struct rb_root blocks
= RB_ROOT
;
3261 struct btrfs_key key
;
3262 struct file_extent_cluster
*cluster
;
3263 struct btrfs_trans_handle
*trans
= NULL
;
3264 struct btrfs_path
*path
;
3265 struct btrfs_extent_item
*ei
;
3272 cluster
= kzalloc(sizeof(*cluster
), GFP_NOFS
);
3276 path
= btrfs_alloc_path();
3280 rc
->extents_found
= 0;
3281 rc
->extents_skipped
= 0;
3283 rc
->search_start
= rc
->block_group
->key
.objectid
;
3284 clear_extent_bits(&rc
->processed_blocks
, 0, (u64
)-1, EXTENT_DIRTY
,
3287 rc
->create_reloc_root
= 1;
3288 set_reloc_control(rc
);
3290 trans
= btrfs_start_transaction(rc
->extent_root
, 1);
3291 btrfs_commit_transaction(trans
, rc
->extent_root
);
3294 trans
= btrfs_start_transaction(rc
->extent_root
, 1);
3296 ret
= find_next_extent(trans
, rc
, path
);
3302 rc
->extents_found
++;
3304 ei
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
3305 struct btrfs_extent_item
);
3306 btrfs_item_key_to_cpu(path
->nodes
[0], &key
, path
->slots
[0]);
3307 item_size
= btrfs_item_size_nr(path
->nodes
[0],
3309 if (item_size
>= sizeof(*ei
)) {
3310 flags
= btrfs_extent_flags(path
->nodes
[0], ei
);
3311 ret
= check_extent_flags(flags
);
3315 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3317 int path_change
= 0;
3320 sizeof(struct btrfs_extent_item_v0
));
3321 ret
= get_ref_objectid_v0(rc
, path
, &key
, &ref_owner
,
3323 if (ref_owner
< BTRFS_FIRST_FREE_OBJECTID
)
3324 flags
= BTRFS_EXTENT_FLAG_TREE_BLOCK
;
3326 flags
= BTRFS_EXTENT_FLAG_DATA
;
3329 btrfs_release_path(rc
->extent_root
, path
);
3331 path
->search_commit_root
= 1;
3332 path
->skip_locking
= 1;
3333 ret
= btrfs_search_slot(NULL
, rc
->extent_root
,
3346 if (flags
& BTRFS_EXTENT_FLAG_TREE_BLOCK
) {
3347 ret
= add_tree_block(rc
, &key
, path
, &blocks
);
3348 } else if (rc
->stage
== UPDATE_DATA_PTRS
&&
3349 (flags
& BTRFS_EXTENT_FLAG_DATA
)) {
3350 ret
= add_data_references(rc
, &key
, path
, &blocks
);
3352 btrfs_release_path(rc
->extent_root
, path
);
3360 if (!RB_EMPTY_ROOT(&blocks
)) {
3361 ret
= relocate_tree_blocks(trans
, rc
, &blocks
);
3368 nr
= trans
->blocks_used
;
3369 btrfs_end_transaction(trans
, rc
->extent_root
);
3371 btrfs_btree_balance_dirty(rc
->extent_root
, nr
);
3373 if (rc
->stage
== MOVE_DATA_EXTENTS
&&
3374 (flags
& BTRFS_EXTENT_FLAG_DATA
)) {
3375 rc
->found_file_extent
= 1;
3376 ret
= relocate_data_extent(rc
->data_inode
,
3384 btrfs_free_path(path
);
3387 nr
= trans
->blocks_used
;
3388 btrfs_end_transaction(trans
, rc
->extent_root
);
3389 btrfs_btree_balance_dirty(rc
->extent_root
, nr
);
3393 ret
= relocate_file_extent_cluster(rc
->data_inode
, cluster
);
3400 rc
->create_reloc_root
= 0;
3403 if (rc
->extents_found
> 0) {
3404 trans
= btrfs_start_transaction(rc
->extent_root
, 1);
3405 btrfs_commit_transaction(trans
, rc
->extent_root
);
3408 merge_reloc_roots(rc
);
3410 unset_reloc_control(rc
);
3412 /* get rid of pinned extents */
3413 trans
= btrfs_start_transaction(rc
->extent_root
, 1);
3414 btrfs_commit_transaction(trans
, rc
->extent_root
);
3419 static int __insert_orphan_inode(struct btrfs_trans_handle
*trans
,
3420 struct btrfs_root
*root
, u64 objectid
)
3422 struct btrfs_path
*path
;
3423 struct btrfs_inode_item
*item
;
3424 struct extent_buffer
*leaf
;
3427 path
= btrfs_alloc_path();
3431 ret
= btrfs_insert_empty_inode(trans
, root
, path
, objectid
);
3435 leaf
= path
->nodes
[0];
3436 item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_inode_item
);
3437 memset_extent_buffer(leaf
, 0, (unsigned long)item
, sizeof(*item
));
3438 btrfs_set_inode_generation(leaf
, item
, 1);
3439 btrfs_set_inode_size(leaf
, item
, 0);
3440 btrfs_set_inode_mode(leaf
, item
, S_IFREG
| 0600);
3441 btrfs_set_inode_flags(leaf
, item
, BTRFS_INODE_NOCOMPRESS
);
3442 btrfs_mark_buffer_dirty(leaf
);
3443 btrfs_release_path(root
, path
);
3445 btrfs_free_path(path
);
3450 * helper to create inode for data relocation.
3451 * the inode is in data relocation tree and its link count is 0
3453 static struct inode
*create_reloc_inode(struct btrfs_fs_info
*fs_info
,
3454 struct btrfs_block_group_cache
*group
)
3456 struct inode
*inode
= NULL
;
3457 struct btrfs_trans_handle
*trans
;
3458 struct btrfs_root
*root
;
3459 struct btrfs_key key
;
3461 u64 objectid
= BTRFS_FIRST_FREE_OBJECTID
;
3464 root
= read_fs_root(fs_info
, BTRFS_DATA_RELOC_TREE_OBJECTID
);
3466 return ERR_CAST(root
);
3468 trans
= btrfs_start_transaction(root
, 1);
3471 err
= btrfs_find_free_objectid(trans
, root
, objectid
, &objectid
);
3475 err
= __insert_orphan_inode(trans
, root
, objectid
);
3478 key
.objectid
= objectid
;
3479 key
.type
= BTRFS_INODE_ITEM_KEY
;
3481 inode
= btrfs_iget(root
->fs_info
->sb
, &key
, root
);
3482 BUG_ON(IS_ERR(inode
) || is_bad_inode(inode
));
3483 BTRFS_I(inode
)->index_cnt
= group
->key
.objectid
;
3485 err
= btrfs_orphan_add(trans
, inode
);
3487 nr
= trans
->blocks_used
;
3488 btrfs_end_transaction(trans
, root
);
3490 btrfs_btree_balance_dirty(root
, nr
);
3494 inode
= ERR_PTR(err
);
3500 * function to relocate all extents in a block group.
3502 int btrfs_relocate_block_group(struct btrfs_root
*extent_root
, u64 group_start
)
3504 struct btrfs_fs_info
*fs_info
= extent_root
->fs_info
;
3505 struct reloc_control
*rc
;
3509 rc
= kzalloc(sizeof(*rc
), GFP_NOFS
);
3513 mapping_tree_init(&rc
->reloc_root_tree
);
3514 extent_io_tree_init(&rc
->processed_blocks
, NULL
, GFP_NOFS
);
3515 INIT_LIST_HEAD(&rc
->reloc_roots
);
3517 rc
->block_group
= btrfs_lookup_block_group(fs_info
, group_start
);
3518 BUG_ON(!rc
->block_group
);
3520 btrfs_init_workers(&rc
->workers
, "relocate",
3521 fs_info
->thread_pool_size
);
3523 rc
->extent_root
= extent_root
;
3524 btrfs_prepare_block_group_relocation(extent_root
, rc
->block_group
);
3526 rc
->data_inode
= create_reloc_inode(fs_info
, rc
->block_group
);
3527 if (IS_ERR(rc
->data_inode
)) {
3528 err
= PTR_ERR(rc
->data_inode
);
3529 rc
->data_inode
= NULL
;
3533 printk(KERN_INFO
"btrfs: relocating block group %llu flags %llu\n",
3534 (unsigned long long)rc
->block_group
->key
.objectid
,
3535 (unsigned long long)rc
->block_group
->flags
);
3537 btrfs_start_delalloc_inodes(fs_info
->tree_root
);
3538 btrfs_wait_ordered_extents(fs_info
->tree_root
, 0);
3541 rc
->extents_found
= 0;
3542 rc
->extents_skipped
= 0;
3544 mutex_lock(&fs_info
->cleaner_mutex
);
3546 btrfs_clean_old_snapshots(fs_info
->tree_root
);
3547 ret
= relocate_block_group(rc
);
3549 mutex_unlock(&fs_info
->cleaner_mutex
);
3555 if (rc
->extents_found
== 0)
3558 printk(KERN_INFO
"btrfs: found %llu extents\n",
3559 (unsigned long long)rc
->extents_found
);
3561 if (rc
->stage
== MOVE_DATA_EXTENTS
&& rc
->found_file_extent
) {
3562 btrfs_wait_ordered_range(rc
->data_inode
, 0, (u64
)-1);
3563 invalidate_mapping_pages(rc
->data_inode
->i_mapping
,
3565 rc
->stage
= UPDATE_DATA_PTRS
;
3566 } else if (rc
->stage
== UPDATE_DATA_PTRS
&&
3567 rc
->extents_skipped
>= rc
->extents_found
) {
3568 iput(rc
->data_inode
);
3569 rc
->data_inode
= create_reloc_inode(fs_info
,
3571 if (IS_ERR(rc
->data_inode
)) {
3572 err
= PTR_ERR(rc
->data_inode
);
3573 rc
->data_inode
= NULL
;
3576 rc
->stage
= MOVE_DATA_EXTENTS
;
3577 rc
->found_file_extent
= 0;
3581 filemap_write_and_wait_range(fs_info
->btree_inode
->i_mapping
,
3582 rc
->block_group
->key
.objectid
,
3583 rc
->block_group
->key
.objectid
+
3584 rc
->block_group
->key
.offset
- 1);
3586 WARN_ON(rc
->block_group
->pinned
> 0);
3587 WARN_ON(rc
->block_group
->reserved
> 0);
3588 WARN_ON(btrfs_block_group_used(&rc
->block_group
->item
) > 0);
3590 iput(rc
->data_inode
);
3591 btrfs_stop_workers(&rc
->workers
);
3592 btrfs_put_block_group(rc
->block_group
);
3597 static noinline_for_stack
int mark_garbage_root(struct btrfs_root
*root
)
3599 struct btrfs_trans_handle
*trans
;
3602 trans
= btrfs_start_transaction(root
->fs_info
->tree_root
, 1);
3604 memset(&root
->root_item
.drop_progress
, 0,
3605 sizeof(root
->root_item
.drop_progress
));
3606 root
->root_item
.drop_level
= 0;
3607 btrfs_set_root_refs(&root
->root_item
, 0);
3608 ret
= btrfs_update_root(trans
, root
->fs_info
->tree_root
,
3609 &root
->root_key
, &root
->root_item
);
3612 ret
= btrfs_end_transaction(trans
, root
->fs_info
->tree_root
);
3618 * recover relocation interrupted by system crash.
3620 * this function resumes merging reloc trees with corresponding fs trees.
3621 * this is important for keeping the sharing of tree blocks
3623 int btrfs_recover_relocation(struct btrfs_root
*root
)
3625 LIST_HEAD(reloc_roots
);
3626 struct btrfs_key key
;
3627 struct btrfs_root
*fs_root
;
3628 struct btrfs_root
*reloc_root
;
3629 struct btrfs_path
*path
;
3630 struct extent_buffer
*leaf
;
3631 struct reloc_control
*rc
= NULL
;
3632 struct btrfs_trans_handle
*trans
;
3636 path
= btrfs_alloc_path();
3640 key
.objectid
= BTRFS_TREE_RELOC_OBJECTID
;
3641 key
.type
= BTRFS_ROOT_ITEM_KEY
;
3642 key
.offset
= (u64
)-1;
3645 ret
= btrfs_search_slot(NULL
, root
->fs_info
->tree_root
, &key
,
3652 if (path
->slots
[0] == 0)
3656 leaf
= path
->nodes
[0];
3657 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
3658 btrfs_release_path(root
->fs_info
->tree_root
, path
);
3660 if (key
.objectid
!= BTRFS_TREE_RELOC_OBJECTID
||
3661 key
.type
!= BTRFS_ROOT_ITEM_KEY
)
3664 reloc_root
= btrfs_read_fs_root_no_radix(root
, &key
);
3665 if (IS_ERR(reloc_root
)) {
3666 err
= PTR_ERR(reloc_root
);
3670 list_add(&reloc_root
->root_list
, &reloc_roots
);
3672 if (btrfs_root_refs(&reloc_root
->root_item
) > 0) {
3673 fs_root
= read_fs_root(root
->fs_info
,
3674 reloc_root
->root_key
.offset
);
3675 if (IS_ERR(fs_root
)) {
3676 ret
= PTR_ERR(fs_root
);
3677 if (ret
!= -ENOENT
) {
3681 mark_garbage_root(reloc_root
);
3685 if (key
.offset
== 0)
3690 btrfs_release_path(root
->fs_info
->tree_root
, path
);
3692 if (list_empty(&reloc_roots
))
3695 rc
= kzalloc(sizeof(*rc
), GFP_NOFS
);
3701 mapping_tree_init(&rc
->reloc_root_tree
);
3702 INIT_LIST_HEAD(&rc
->reloc_roots
);
3703 btrfs_init_workers(&rc
->workers
, "relocate",
3704 root
->fs_info
->thread_pool_size
);
3705 rc
->extent_root
= root
->fs_info
->extent_root
;
3707 set_reloc_control(rc
);
3709 while (!list_empty(&reloc_roots
)) {
3710 reloc_root
= list_entry(reloc_roots
.next
,
3711 struct btrfs_root
, root_list
);
3712 list_del(&reloc_root
->root_list
);
3714 if (btrfs_root_refs(&reloc_root
->root_item
) == 0) {
3715 list_add_tail(&reloc_root
->root_list
,
3720 fs_root
= read_fs_root(root
->fs_info
,
3721 reloc_root
->root_key
.offset
);
3722 BUG_ON(IS_ERR(fs_root
));
3724 __add_reloc_root(reloc_root
);
3725 fs_root
->reloc_root
= reloc_root
;
3728 trans
= btrfs_start_transaction(rc
->extent_root
, 1);
3729 btrfs_commit_transaction(trans
, rc
->extent_root
);
3731 merge_reloc_roots(rc
);
3733 unset_reloc_control(rc
);
3735 trans
= btrfs_start_transaction(rc
->extent_root
, 1);
3736 btrfs_commit_transaction(trans
, rc
->extent_root
);
3739 btrfs_stop_workers(&rc
->workers
);
3742 while (!list_empty(&reloc_roots
)) {
3743 reloc_root
= list_entry(reloc_roots
.next
,
3744 struct btrfs_root
, root_list
);
3745 list_del(&reloc_root
->root_list
);
3746 free_extent_buffer(reloc_root
->node
);
3747 free_extent_buffer(reloc_root
->commit_root
);
3750 btrfs_free_path(path
);
3753 /* cleanup orphan inode in data relocation tree */
3754 fs_root
= read_fs_root(root
->fs_info
,
3755 BTRFS_DATA_RELOC_TREE_OBJECTID
);
3756 if (IS_ERR(fs_root
))
3757 err
= PTR_ERR(fs_root
);
3763 * helper to add ordered checksum for data relocation.
3765 * cloning checksum properly handles the nodatasum extents.
3766 * it also saves CPU time to re-calculate the checksum.
3768 int btrfs_reloc_clone_csums(struct inode
*inode
, u64 file_pos
, u64 len
)
3770 struct btrfs_ordered_sum
*sums
;
3771 struct btrfs_sector_sum
*sector_sum
;
3772 struct btrfs_ordered_extent
*ordered
;
3773 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3779 ordered
= btrfs_lookup_ordered_extent(inode
, file_pos
);
3780 BUG_ON(ordered
->file_offset
!= file_pos
|| ordered
->len
!= len
);
3782 disk_bytenr
= file_pos
+ BTRFS_I(inode
)->index_cnt
;
3783 ret
= btrfs_lookup_csums_range(root
->fs_info
->csum_root
, disk_bytenr
,
3784 disk_bytenr
+ len
- 1, &list
);
3786 while (!list_empty(&list
)) {
3787 sums
= list_entry(list
.next
, struct btrfs_ordered_sum
, list
);
3788 list_del_init(&sums
->list
);
3790 sector_sum
= sums
->sums
;
3791 sums
->bytenr
= ordered
->start
;
3794 while (offset
< sums
->len
) {
3795 sector_sum
->bytenr
+= ordered
->start
- disk_bytenr
;
3797 offset
+= root
->sectorsize
;
3800 btrfs_add_ordered_sum(inode
, ordered
, sums
);
3802 btrfs_put_ordered_extent(ordered
);