Skip to content

Commit d2311e6

Browse files
adam900710kdave
authored andcommitted
btrfs: relocation: Delay reloc tree deletion after merge_reloc_roots
Relocation code will drop btrfs_root::reloc_root as soon as merge_reloc_root() finishes. However later qgroup code will need to access btrfs_root::reloc_root after merge_reloc_root() for delayed subtree rescan. So alter the timming of resetting btrfs_root:::reloc_root, make it happens after transaction commit. With this patch, we will introduce a new btrfs_root::state, BTRFS_ROOT_DEAD_RELOC_TREE, to info part of btrfs_root::reloc_tree user that although btrfs_root::reloc_tree is still non-NULL, but still it's not used any more. The lifespan of btrfs_root::reloc tree will become: Old behavior | New ------------------------------------------------------------------------ btrfs_init_reloc_root() --- | btrfs_init_reloc_root() --- set reloc_root | | set reloc_root | | | | | | | merge_reloc_root() | | merge_reloc_root() | |- btrfs_update_reloc_root() --- | |- btrfs_update_reloc_root() -+- clear btrfs_root::reloc_root | set ROOT_DEAD_RELOC_TREE | | record root into dirty | | roots rbtree | | | | reloc_block_group() Or | | btrfs_recover_relocation() | | | After transaction commit | | |- clean_dirty_subvols() --- | clear btrfs_root::reloc_root During ROOT_DEAD_RELOC_TREE set lifespan, the only user of btrfs_root::reloc_tree should be qgroup. Since reloc root needs a longer life-span, this patch will also delay btrfs_drop_snapshot() call. Now btrfs_drop_snapshot() is called in clean_dirty_subvols(). This patch will increase the size of btrfs_root by 16 bytes. Signed-off-by: Qu Wenruo <[email protected]> Signed-off-by: David Sterba <[email protected]>
1 parent 119e80d commit d2311e6

File tree

3 files changed

+84
-17
lines changed

3 files changed

+84
-17
lines changed

fs/btrfs/ctree.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1199,6 +1199,13 @@ enum {
11991199
BTRFS_ROOT_MULTI_LOG_TASKS,
12001200
BTRFS_ROOT_DIRTY,
12011201
BTRFS_ROOT_DELETING,
1202+
1203+
/*
1204+
* Reloc tree is orphan, only kept here for qgroup delayed subtree scan
1205+
*
1206+
* Set for the subvolume tree owning the reloc tree.
1207+
*/
1208+
BTRFS_ROOT_DEAD_RELOC_TREE,
12021209
};
12031210

12041211
/*
@@ -1311,6 +1318,14 @@ struct btrfs_root {
13111318
struct list_head ordered_root;
13121319
u64 nr_ordered_extents;
13131320

1321+
/*
1322+
* Not empty if this subvolume root has gone through tree block swap
1323+
* (relocation)
1324+
*
1325+
* Will be used by reloc_control::dirty_subvol_roots.
1326+
*/
1327+
struct list_head reloc_dirty_list;
1328+
13141329
/*
13151330
* Number of currently running SEND ioctls to prevent
13161331
* manipulation with the read-only status via SUBVOL_SETFLAGS

fs/btrfs/disk-io.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1176,6 +1176,7 @@ static void __setup_root(struct btrfs_root *root, struct btrfs_fs_info *fs_info,
11761176
INIT_LIST_HEAD(&root->delalloc_root);
11771177
INIT_LIST_HEAD(&root->ordered_extents);
11781178
INIT_LIST_HEAD(&root->ordered_root);
1179+
INIT_LIST_HEAD(&root->reloc_dirty_list);
11791180
INIT_LIST_HEAD(&root->logged_list[0]);
11801181
INIT_LIST_HEAD(&root->logged_list[1]);
11811182
spin_lock_init(&root->inode_lock);

fs/btrfs/relocation.c

Lines changed: 68 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,8 @@ struct reloc_control {
162162
struct mapping_tree reloc_root_tree;
163163
/* list of reloc trees */
164164
struct list_head reloc_roots;
165+
/* list of subvolume trees that get relocated */
166+
struct list_head dirty_subvol_roots;
165167
/* size of metadata reservation for merging reloc trees */
166168
u64 merging_rsv_size;
167169
/* size of relocated tree nodes */
@@ -1467,15 +1469,17 @@ int btrfs_update_reloc_root(struct btrfs_trans_handle *trans,
14671469
struct btrfs_root_item *root_item;
14681470
int ret;
14691471

1470-
if (!root->reloc_root)
1472+
if (test_bit(BTRFS_ROOT_DEAD_RELOC_TREE, &root->state) ||
1473+
!root->reloc_root)
14711474
goto out;
14721475

14731476
reloc_root = root->reloc_root;
14741477
root_item = &reloc_root->root_item;
14751478

1479+
/* root->reloc_root will stay until current relocation finished */
14761480
if (fs_info->reloc_ctl->merge_reloc_tree &&
14771481
btrfs_root_refs(root_item) == 0) {
1478-
root->reloc_root = NULL;
1482+
set_bit(BTRFS_ROOT_DEAD_RELOC_TREE, &root->state);
14791483
__del_reloc_root(reloc_root);
14801484
}
14811485

@@ -2120,6 +2124,58 @@ static int find_next_key(struct btrfs_path *path, int level,
21202124
return 1;
21212125
}
21222126

2127+
/*
2128+
* Insert current subvolume into reloc_control::dirty_subvol_roots
2129+
*/
2130+
static void insert_dirty_subvol(struct btrfs_trans_handle *trans,
2131+
struct reloc_control *rc,
2132+
struct btrfs_root *root)
2133+
{
2134+
struct btrfs_root *reloc_root = root->reloc_root;
2135+
struct btrfs_root_item *reloc_root_item;
2136+
2137+
/* @root must be a subvolume tree root with a valid reloc tree */
2138+
ASSERT(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
2139+
ASSERT(reloc_root);
2140+
2141+
reloc_root_item = &reloc_root->root_item;
2142+
memset(&reloc_root_item->drop_progress, 0,
2143+
sizeof(reloc_root_item->drop_progress));
2144+
reloc_root_item->drop_level = 0;
2145+
btrfs_set_root_refs(reloc_root_item, 0);
2146+
btrfs_update_reloc_root(trans, root);
2147+
2148+
if (list_empty(&root->reloc_dirty_list)) {
2149+
btrfs_grab_fs_root(root);
2150+
list_add_tail(&root->reloc_dirty_list, &rc->dirty_subvol_roots);
2151+
}
2152+
}
2153+
2154+
static int clean_dirty_subvols(struct reloc_control *rc)
2155+
{
2156+
struct btrfs_root *root;
2157+
struct btrfs_root *next;
2158+
int ret = 0;
2159+
2160+
list_for_each_entry_safe(root, next, &rc->dirty_subvol_roots,
2161+
reloc_dirty_list) {
2162+
struct btrfs_root *reloc_root = root->reloc_root;
2163+
2164+
clear_bit(BTRFS_ROOT_DEAD_RELOC_TREE, &root->state);
2165+
list_del_init(&root->reloc_dirty_list);
2166+
root->reloc_root = NULL;
2167+
if (reloc_root) {
2168+
int ret2;
2169+
2170+
ret2 = btrfs_drop_snapshot(reloc_root, NULL, 0, 1);
2171+
if (ret2 < 0 && !ret)
2172+
ret = ret2;
2173+
}
2174+
btrfs_put_fs_root(root);
2175+
}
2176+
return ret;
2177+
}
2178+
21232179
/*
21242180
* merge the relocated tree blocks in reloc tree with corresponding
21252181
* fs tree.
@@ -2258,13 +2314,8 @@ static noinline_for_stack int merge_reloc_root(struct reloc_control *rc,
22582314
out:
22592315
btrfs_free_path(path);
22602316

2261-
if (err == 0) {
2262-
memset(&root_item->drop_progress, 0,
2263-
sizeof(root_item->drop_progress));
2264-
root_item->drop_level = 0;
2265-
btrfs_set_root_refs(root_item, 0);
2266-
btrfs_update_reloc_root(trans, root);
2267-
}
2317+
if (err == 0)
2318+
insert_dirty_subvol(trans, rc, root);
22682319

22692320
if (trans)
22702321
btrfs_end_transaction_throttle(trans);
@@ -2409,14 +2460,6 @@ void merge_reloc_roots(struct reloc_control *rc)
24092460
} else {
24102461
list_del_init(&reloc_root->root_list);
24112462
}
2412-
2413-
ret = btrfs_drop_snapshot(reloc_root, rc->block_rsv, 0, 1);
2414-
if (ret < 0) {
2415-
if (list_empty(&reloc_root->root_list))
2416-
list_add_tail(&reloc_root->root_list,
2417-
&reloc_roots);
2418-
goto out;
2419-
}
24202463
}
24212464

24222465
if (found) {
@@ -4078,6 +4121,9 @@ static noinline_for_stack int relocate_block_group(struct reloc_control *rc)
40784121
goto out_free;
40794122
}
40804123
btrfs_commit_transaction(trans);
4124+
ret = clean_dirty_subvols(rc);
4125+
if (ret < 0 && !err)
4126+
err = ret;
40814127
out_free:
40824128
btrfs_free_block_rsv(fs_info, rc->block_rsv);
40834129
btrfs_free_path(path);
@@ -4172,6 +4218,7 @@ static struct reloc_control *alloc_reloc_control(void)
41724218
return NULL;
41734219

41744220
INIT_LIST_HEAD(&rc->reloc_roots);
4221+
INIT_LIST_HEAD(&rc->dirty_subvol_roots);
41754222
backref_cache_init(&rc->backref_cache);
41764223
mapping_tree_init(&rc->reloc_root_tree);
41774224
extent_io_tree_init(&rc->processed_blocks, NULL);
@@ -4467,6 +4514,10 @@ int btrfs_recover_relocation(struct btrfs_root *root)
44674514
goto out_free;
44684515
}
44694516
err = btrfs_commit_transaction(trans);
4517+
4518+
ret = clean_dirty_subvols(rc);
4519+
if (ret < 0 && !err)
4520+
err = ret;
44704521
out_free:
44714522
kfree(rc);
44724523
out:

0 commit comments

Comments
 (0)