Skip to content

Commit 2f96e40

Browse files
josefbacikkdave
authored andcommitted
btrfs: fix possible free space tree corruption with online conversion
While running btrfs/011 in a loop I would often ASSERT() while trying to add a new free space entry that already existed, or get an EEXIST while adding a new block to the extent tree, which is another indication of double allocation. This occurs because when we do the free space tree population, we create the new root and then populate the tree and commit the transaction. The problem is when you create a new root, the root node and commit root node are the same. During this initial transaction commit we will run all of the delayed refs that were paused during the free space tree generation, and thus begin to cache block groups. While caching block groups the caching thread will be reading from the main root for the free space tree, so as we make allocations we'll be changing the free space tree, which can cause us to add the same range twice which results in either the ASSERT(ret != -EEXIST); in __btrfs_add_free_space, or in a variety of different errors when running delayed refs because of a double allocation. Fix this by marking the fs_info as unsafe to load the free space tree, and fall back on the old slow method. We could be smarter than this, for example caching the block group while we're populating the free space tree, but since this is a serious problem I've opted for the simplest solution. CC: [email protected] # 4.9+ Fixes: a5ed918 ("Btrfs: implement the free space B-tree") Reviewed-by: Filipe Manana <[email protected]> Signed-off-by: Josef Bacik <[email protected]> Signed-off-by: David Sterba <[email protected]>
1 parent 34d1eb0 commit 2f96e40

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

fs/btrfs/block-group.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -673,7 +673,15 @@ static noinline void caching_thread(struct btrfs_work *work)
673673
wake_up(&caching_ctl->wait);
674674
}
675675

676-
if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE))
676+
/*
677+
* If we are in the transaction that populated the free space tree we
678+
* can't actually cache from the free space tree as our commit root and
679+
* real root are the same, so we could change the contents of the blocks
680+
* while caching. Instead do the slow caching in this case, and after
681+
* the transaction has committed we will be safe.
682+
*/
683+
if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE) &&
684+
!(test_bit(BTRFS_FS_FREE_SPACE_TREE_UNTRUSTED, &fs_info->flags)))
677685
ret = load_free_space_tree(caching_ctl);
678686
else
679687
ret = load_extent_tree_free(caching_ctl);

fs/btrfs/ctree.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,9 @@ enum {
564564

565565
/* Indicate that we need to cleanup space cache v1 */
566566
BTRFS_FS_CLEANUP_SPACE_CACHE_V1,
567+
568+
/* Indicate that we can't trust the free space tree for caching yet */
569+
BTRFS_FS_FREE_SPACE_TREE_UNTRUSTED,
567570
};
568571

569572
/*

fs/btrfs/free-space-tree.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1150,6 +1150,7 @@ int btrfs_create_free_space_tree(struct btrfs_fs_info *fs_info)
11501150
return PTR_ERR(trans);
11511151

11521152
set_bit(BTRFS_FS_CREATING_FREE_SPACE_TREE, &fs_info->flags);
1153+
set_bit(BTRFS_FS_FREE_SPACE_TREE_UNTRUSTED, &fs_info->flags);
11531154
free_space_root = btrfs_create_tree(trans,
11541155
BTRFS_FREE_SPACE_TREE_OBJECTID);
11551156
if (IS_ERR(free_space_root)) {
@@ -1171,11 +1172,18 @@ int btrfs_create_free_space_tree(struct btrfs_fs_info *fs_info)
11711172
btrfs_set_fs_compat_ro(fs_info, FREE_SPACE_TREE);
11721173
btrfs_set_fs_compat_ro(fs_info, FREE_SPACE_TREE_VALID);
11731174
clear_bit(BTRFS_FS_CREATING_FREE_SPACE_TREE, &fs_info->flags);
1175+
ret = btrfs_commit_transaction(trans);
11741176

1175-
return btrfs_commit_transaction(trans);
1177+
/*
1178+
* Now that we've committed the transaction any reading of our commit
1179+
* root will be safe, so we can cache from the free space tree now.
1180+
*/
1181+
clear_bit(BTRFS_FS_FREE_SPACE_TREE_UNTRUSTED, &fs_info->flags);
1182+
return ret;
11761183

11771184
abort:
11781185
clear_bit(BTRFS_FS_CREATING_FREE_SPACE_TREE, &fs_info->flags);
1186+
clear_bit(BTRFS_FS_FREE_SPACE_TREE_UNTRUSTED, &fs_info->flags);
11791187
btrfs_abort_transaction(trans, ret);
11801188
btrfs_end_transaction(trans);
11811189
return ret;

0 commit comments

Comments
 (0)