Skip to content

Commit 9d04716

Browse files
adam900710gregkh
authored andcommitted
btrfs: exit gracefully if reloc roots don't match
commit 05d7ce5 upstream. [BUG] Syzbot reported a crash that an ASSERT() got triggered inside prepare_to_merge(). [CAUSE] The root cause of the triggered ASSERT() is we can have a race between quota tree creation and relocation. This leads us to create a duplicated quota tree in the btrfs_read_fs_root() path, and since it's treated as fs tree, it would have ROOT_SHAREABLE flag, causing us to create a reloc tree for it. The bug itself is fixed by a dedicated patch for it, but this already taught us the ASSERT() is not something straightforward for developers. [ENHANCEMENT] Instead of using an ASSERT(), let's handle it gracefully and output extra info about the mismatch reloc roots to help debug. Also with the above ASSERT() removed, we can trigger ASSERT(0)s inside merge_reloc_roots() later. Also replace those ASSERT(0)s with WARN_ON()s. CC: [email protected] # 5.15+ Reported-by: [email protected] Reviewed-by: Filipe Manana <[email protected]> Signed-off-by: Qu Wenruo <[email protected]> Signed-off-by: David Sterba <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 7112abc commit 9d04716

File tree

1 file changed

+37
-8
lines changed

1 file changed

+37
-8
lines changed

fs/btrfs/relocation.c

+37-8
Original file line numberDiff line numberDiff line change
@@ -1902,7 +1902,39 @@ int prepare_to_merge(struct reloc_control *rc, int err)
19021902
err = PTR_ERR(root);
19031903
break;
19041904
}
1905-
ASSERT(root->reloc_root == reloc_root);
1905+
1906+
if (unlikely(root->reloc_root != reloc_root)) {
1907+
if (root->reloc_root) {
1908+
btrfs_err(fs_info,
1909+
"reloc tree mismatch, root %lld has reloc root key (%lld %u %llu) gen %llu, expect reloc root key (%lld %u %llu) gen %llu",
1910+
root->root_key.objectid,
1911+
root->reloc_root->root_key.objectid,
1912+
root->reloc_root->root_key.type,
1913+
root->reloc_root->root_key.offset,
1914+
btrfs_root_generation(
1915+
&root->reloc_root->root_item),
1916+
reloc_root->root_key.objectid,
1917+
reloc_root->root_key.type,
1918+
reloc_root->root_key.offset,
1919+
btrfs_root_generation(
1920+
&reloc_root->root_item));
1921+
} else {
1922+
btrfs_err(fs_info,
1923+
"reloc tree mismatch, root %lld has no reloc root, expect reloc root key (%lld %u %llu) gen %llu",
1924+
root->root_key.objectid,
1925+
reloc_root->root_key.objectid,
1926+
reloc_root->root_key.type,
1927+
reloc_root->root_key.offset,
1928+
btrfs_root_generation(
1929+
&reloc_root->root_item));
1930+
}
1931+
list_add(&reloc_root->root_list, &reloc_roots);
1932+
btrfs_put_root(root);
1933+
btrfs_abort_transaction(trans, -EUCLEAN);
1934+
if (!err)
1935+
err = -EUCLEAN;
1936+
break;
1937+
}
19061938

19071939
/*
19081940
* set reference count to 1, so btrfs_recover_relocation
@@ -1975,7 +2007,7 @@ void merge_reloc_roots(struct reloc_control *rc)
19752007
root = btrfs_get_fs_root(fs_info, reloc_root->root_key.offset,
19762008
false);
19772009
if (btrfs_root_refs(&reloc_root->root_item) > 0) {
1978-
if (IS_ERR(root)) {
2010+
if (WARN_ON(IS_ERR(root))) {
19792011
/*
19802012
* For recovery we read the fs roots on mount,
19812013
* and if we didn't find the root then we marked
@@ -1984,17 +2016,14 @@ void merge_reloc_roots(struct reloc_control *rc)
19842016
* memory. However there's no reason we can't
19852017
* handle the error properly here just in case.
19862018
*/
1987-
ASSERT(0);
19882019
ret = PTR_ERR(root);
19892020
goto out;
19902021
}
1991-
if (root->reloc_root != reloc_root) {
2022+
if (WARN_ON(root->reloc_root != reloc_root)) {
19922023
/*
1993-
* This is actually impossible without something
1994-
* going really wrong (like weird race condition
1995-
* or cosmic rays).
2024+
* This can happen if on-disk metadata has some
2025+
* corruption, e.g. bad reloc tree key offset.
19962026
*/
1997-
ASSERT(0);
19982027
ret = -EINVAL;
19992028
goto out;
20002029
}

0 commit comments

Comments
 (0)