FAILED: patch "[PATCH] btrfs: drop recovered reloc root refs on recovery failure" failed to apply to 6.1-stable tree
Subsystems (2)
[email protected]
maintainerThe patch below does not apply to the 6.1-stable tree.
If someone wants it applied there, or to any other stable or longterm tree, then please email the backport, including the original git commit id to <[email protected]>.
To reproduce the conflict and resubmit, you may use the following commands:
git fetch https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/ linux-6.1.y
git checkout FETCH_HEAD
git cherry-pick -x 6d8ba4572922e336f0b59a80751b018e1e135164
# <resolve conflicts, build, test, etc.>
git commit -s
git send-email --to '<[email protected]>' --in-reply-to '2026090359-float-gamma-d28d@gregkh' --subject-prefix 'PATCH 6.1.y' 'HEAD^..'
Possible dependencies:
thanks,
greg k-h
------------------ original commit in Linus's tree ------------------
From 6d8ba4572922e336f0b59a80751b018e1e135164 Mon Sep 17 00:00:00 2001
From: Guanghui Yang <[email protected]>
Date: Sun, 12 Jul 2026 04:22:32 +0000
Subject: [PATCH] btrfs: drop recovered reloc root refs on recovery failure
During relocation recovery, each fs root gets a reference to its relocation root. If loading or adding a later root fails, or if the first transaction commit fails, btrfs_recover_relocation() jumps to out_unset before merge_reloc_roots() and clean_dirty_subvols().
put_reloc_control() drops the list-owned relocation root references, but it does not clear fs_root->reloc_root or drop the references owned by those pointers. Mount cleanup only drops them when BTRFS_FS_ERROR is set, so an error such as -ENOMEM while processing a later root can leave references behind.
Keep temporary references to the fs roots associated during recovery. On failure, clear their reloc_root pointers and drop the corresponding references. Once the first transaction commit succeeds, drop only the temporary fs root references and let the normal merge and cleanup paths handle the relocation roots.
Fault injection on a pending-relocation image confirmed the cleanup gap. With an injected first-commit failure, 25 fs roots had reloc_root set with fs_error=0. With this fix, the same failure path drops that count to 0 before mount fails.
fs/btrfs/relocation.c +27 -4
@@ -5593,6 +5593,24 @@ static noinline_for_stack int mark_garbage_root(struct btrfs_root *root)return ret; } +static void release_recovered_fs_roots (struct list_head *roots,bool drop_reloc_refs) +{ +struct btrfs_root *root; +struct btrfs_root *next; + +list_for_each_entry_safe (root, next, roots, reloc_dirty_list) { +list_del_init (&root->reloc_dirty_list); +if (drop_reloc_refs) { +struct btrfs_root *reloc_root = root->reloc_root; + +ASSERT (reloc_root); + root->reloc_root =NULL ; +btrfs_put_root (reloc_root); + } +btrfs_put_root (root); + } +} +/* * recover relocation interrupted by system crash. *
@@ -5602,6 +5620,7 @@ static noinline_for_stack int mark_garbage_root(struct btrfs_root *root)int btrfs_recover_relocation (struct btrfs_fs_info *fs_info) {LIST_HEAD (reloc_roots); +LIST_HEAD (recovered_roots);struct btrfs_key key;struct btrfs_root *fs_root;struct btrfs_root *reloc_root;
@@ -5718,7 +5737,7 @@ int btrfs_recover_relocation(struct btrfs_fs_info *fs_info) ret =PTR_ERR (fs_root);list_add_tail (&reloc_root->root_list, &reloc_roots);btrfs_end_transaction (trans); - goto out_unset; + goto out_drop_reloc_refs; } ret =__add_reloc_root (reloc_root, rc);
@@ -5727,15 +5746,17 @@ int btrfs_recover_relocation(struct btrfs_fs_info *fs_info)list_add_tail (&reloc_root->root_list, &reloc_roots);btrfs_put_root (fs_root);btrfs_end_transaction (trans); - goto out_unset; + goto out_drop_reloc_refs; } +ASSERT (list_empty (&fs_root->reloc_dirty_list)); fs_root->reloc_root =btrfs_grab_root (reloc_root); -btrfs_put_root (fs_root); +list_add_tail (&fs_root->reloc_dirty_list, &recovered_roots); } ret =btrfs_commit_transaction (trans);if (ret) - goto out_unset; + goto out_drop_reloc_refs; +release_recovered_fs_roots (&recovered_roots, false); ret =merge_reloc_roots (rc);if (ret)
@@ -5753,6 +5774,8 @@ int btrfs_recover_relocation(struct btrfs_fs_info *fs_info) ret2 =clean_dirty_subvols (rc);if (ret2 <0 && !ret) ret = ret2; +out_drop_reloc_refs: +release_recovered_fs_roots (&recovered_roots, true); out_unset:unset_reloc_control (rc);reloc_chunk_end (fs_info);
Sasha Levin
maintainerPATCH 1/4 btrfs: rename ret to err in btrfs_recover_relocation()
From: Anand Jain <[email protected]>
[ Upstream commit ba69f42af2a568f30904a11dc257eb892c5e8c0e ]
In the function btrfs_recover_relocation(), currently the variable 'err' carries the return value and 'ret' holds the intermediary return value. However, in some lines, we don't need this two-step approach; we can directly use 'err'. So, optimize them, which requires reinitializing 'err' to zero at two locations.
This is a preparatory patch to fix the code style by renaming 'err' to 'ret'.
Stable-dep-of: 6d8ba4572922 ("btrfs: drop recovered reloc root refs on recovery failure")
--- fs/btrfs/relocation.c | 28 +++++++++++----------------- 1 file changed, 11 insertions(+), 17 deletions(-)
fs/btrfs/relocation.c +11 -17
@@ -4234,17 +4234,16 @@ int btrfs_recover_relocation(struct btrfs_fs_info *fs_info) key.offset = (u64)-1 ;while (1 ) { - ret =btrfs_search_slot (NULL , fs_info->tree_root, &key, + err =btrfs_search_slot (NULL , fs_info->tree_root, &key, path,0 ,0 ); -if (ret <0 ) { - err = ret; +if (err <0 ) goto out; - } -if (ret >0 ) { +if (err >0 ) {if (path->slots[0 ] ==0 )break ; path->slots[0 ]--; } + err =0 ; leaf = path->nodes[0 ];btrfs_item_key_to_cpu (leaf, &key, path->slots[0 ]);btrfs_release_path (path);
@@ -4266,16 +4265,13 @@ int btrfs_recover_relocation(struct btrfs_fs_info *fs_info) fs_root =btrfs_get_fs_root (fs_info, reloc_root->root_key.offset, false);if (IS_ERR (fs_root)) { - ret =PTR_ERR (fs_root); -if (ret != -ENOENT ) { - err = ret; + err =PTR_ERR (fs_root); +if (err != -ENOENT ) goto out; - } - ret =mark_garbage_root (reloc_root); -if (ret <0 ) { - err = ret; + err =mark_garbage_root (reloc_root); +if (err <0 ) goto out; - } + err =0 ; }else {btrfs_put_root (fs_root); }
@@ -4297,11 +4293,9 @@ int btrfs_recover_relocation(struct btrfs_fs_info *fs_info) goto out; } - ret =reloc_chunk_start (fs_info); -if (ret <0 ) { - err = ret; + err =reloc_chunk_start (fs_info); +if (err <0 ) goto out_end; - } rc->extent_root =btrfs_extent_root (fs_info,0 );
Signature · 1 line
2.53.0
Sasha Levin
maintainerPATCH 2/4 btrfs: rename ret to ret2 in btrfs_recover_relocation()
From: Anand Jain <[email protected]>
[ Upstream commit bd0d9a619a870f8c6745d2b4f57508bc5106e282 ]
A preparatory patch to rename 'err' to 'ret', but ret is already used as an intermediary return value, so first rename 'ret' to 'ret2'.
Stable-dep-of: 6d8ba4572922 ("btrfs: drop recovered reloc root refs on recovery failure")
--- fs/btrfs/relocation.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
fs/btrfs/relocation.c +4 -4
@@ -4221,7 +4221,7 @@ int btrfs_recover_relocation(struct btrfs_fs_info *fs_info)struct extent_buffer *leaf;struct reloc_control *rc =NULL ;struct btrfs_trans_handle *trans; -int ret; +int ret2;int err =0 ; path =btrfs_alloc_path ();
@@ -4356,9 +4356,9 @@ int btrfs_recover_relocation(struct btrfs_fs_info *fs_info) } err =btrfs_commit_transaction (trans); out_clean: - ret =clean_dirty_subvols (rc); -if (ret <0 && !err) - err = ret; + ret2 =clean_dirty_subvols (rc); +if (ret2 <0 && !err) + err = ret2; out_unset:unset_reloc_control (rc);reloc_chunk_end (fs_info);
Signature · 1 line
2.53.0
Sasha Levin
maintainerPATCH 3/4 btrfs: rename err to ret in btrfs_recover_relocation()
In reply to Sasha LevinFrom: Anand Jain <[email protected]>
[ Upstream commit ced1b1bd214fab463f35438c63d9bc6b1a743cae ]
Fix coding style: rename the return variable to 'ret' in the function btrfs_recover_relocation instead of 'err'.
Stable-dep-of: 6d8ba4572922 ("btrfs: drop recovered reloc root refs on recovery failure")
--- fs/btrfs/relocation.c | 56 +++++++++++++++++++++---------------------- 1 file changed, 28 insertions(+), 28 deletions(-)
fs/btrfs/relocation.c +28 -28
@@ -4222,7 +4222,7 @@ int btrfs_recover_relocation(struct btrfs_fs_info *fs_info)struct reloc_control *rc =NULL ;struct btrfs_trans_handle *trans;int ret2; -int err =0 ; +int ret =0 ; path =btrfs_alloc_path ();if (!path)
@@ -4234,16 +4234,16 @@ int btrfs_recover_relocation(struct btrfs_fs_info *fs_info) key.offset = (u64)-1 ;while (1 ) { - err =btrfs_search_slot (NULL , fs_info->tree_root, &key, + ret =btrfs_search_slot (NULL , fs_info->tree_root, &key, path,0 ,0 ); -if (err <0 ) +if (ret <0 ) goto out; -if (err >0 ) { +if (ret >0 ) {if (path->slots[0 ] ==0 )break ; path->slots[0 ]--; } - err =0 ; + ret =0 ; leaf = path->nodes[0 ];btrfs_item_key_to_cpu (leaf, &key, path->slots[0 ]);btrfs_release_path (path);
@@ -4254,7 +4254,7 @@ int btrfs_recover_relocation(struct btrfs_fs_info *fs_info) reloc_root =btrfs_read_tree_root (fs_info->tree_root, &key);if (IS_ERR (reloc_root)) { - err =PTR_ERR (reloc_root); + ret =PTR_ERR (reloc_root); goto out; }
@@ -4265,13 +4265,13 @@ int btrfs_recover_relocation(struct btrfs_fs_info *fs_info) fs_root =btrfs_get_fs_root (fs_info, reloc_root->root_key.offset, false);if (IS_ERR (fs_root)) { - err =PTR_ERR (fs_root); -if (err != -ENOENT ) + ret =PTR_ERR (fs_root); +if (ret != -ENOENT ) goto out; - err =mark_garbage_root (reloc_root); -if (err <0 ) + ret =mark_garbage_root (reloc_root); +if (ret <0 ) goto out; - err =0 ; + ret =0 ; }else {btrfs_put_root (fs_root); }
@@ -4289,12 +4289,12 @@ int btrfs_recover_relocation(struct btrfs_fs_info *fs_info) rc =alloc_reloc_control (fs_info);if (!rc) { - err = -ENOMEM ; + ret = -ENOMEM ; goto out; } - err =reloc_chunk_start (fs_info); -if (err <0 ) + ret =reloc_chunk_start (fs_info); +if (ret <0 ) goto out_end; rc->extent_root =btrfs_extent_root (fs_info,0 );
@@ -4303,7 +4303,7 @@ int btrfs_recover_relocation(struct btrfs_fs_info *fs_info) trans =btrfs_join_transaction (rc->extent_root);if (IS_ERR (trans)) { - err =PTR_ERR (trans); + ret =PTR_ERR (trans); goto out_unset; }
@@ -4323,15 +4323,15 @@ int btrfs_recover_relocation(struct btrfs_fs_info *fs_info) fs_root =btrfs_get_fs_root (fs_info, reloc_root->root_key.offset, false);if (IS_ERR (fs_root)) { - err =PTR_ERR (fs_root); + ret =PTR_ERR (fs_root);list_add_tail (&reloc_root->root_list, &reloc_roots);btrfs_end_transaction (trans); goto out_unset; } - err =__add_reloc_root (reloc_root); -ASSERT (err != -EEXIST ); -if (err) { + ret =__add_reloc_root (reloc_root); +ASSERT (ret != -EEXIST ); +if (ret) {list_add_tail (&reloc_root->root_list, &reloc_roots);btrfs_put_root (fs_root);btrfs_end_transaction (trans);
@@ -4341,8 +4341,8 @@ int btrfs_recover_relocation(struct btrfs_fs_info *fs_info)btrfs_put_root (fs_root); } - err =btrfs_commit_transaction (trans); -if (err) + ret =btrfs_commit_transaction (trans); +if (ret) goto out_unset;merge_reloc_roots (rc);
@@ -4351,14 +4351,14 @@ int btrfs_recover_relocation(struct btrfs_fs_info *fs_info) trans =btrfs_join_transaction (rc->extent_root);if (IS_ERR (trans)) { - err =PTR_ERR (trans); + ret =PTR_ERR (trans); goto out_clean; } - err =btrfs_commit_transaction (trans); + ret =btrfs_commit_transaction (trans); out_clean: ret2 =clean_dirty_subvols (rc); -if (ret2 <0 && !err) - err = ret2; +if (ret2 <0 && !ret) + ret = ret2; out_unset:unset_reloc_control (rc);reloc_chunk_end (fs_info);
@@ -4369,14 +4369,14 @@ int btrfs_recover_relocation(struct btrfs_fs_info *fs_info)btrfs_free_path (path); -if (err ==0 ) { +if (ret ==0 ) {/* cleanup orphan inode in data relocation tree */ fs_root =btrfs_grab_root (fs_info->data_reloc_root);ASSERT (fs_root); - err =btrfs_orphan_cleanup (fs_root); + ret =btrfs_orphan_cleanup (fs_root);btrfs_put_root (fs_root); } -return err; +return ret; }/*
Signature · 1 line
2.53.0
Sasha Levin
maintainerPATCH 4/4 btrfs: drop recovered reloc root refs on recovery failure
In reply to Sasha LevinFrom: Guanghui Yang <[email protected]>
[ Upstream commit 6d8ba4572922e336f0b59a80751b018e1e135164 ]
During relocation recovery, each fs root gets a reference to its relocation root. If loading or adding a later root fails, or if the first transaction commit fails, btrfs_recover_relocation() jumps to out_unset before merge_reloc_roots() and clean_dirty_subvols().
put_reloc_control() drops the list-owned relocation root references, but it does not clear fs_root->reloc_root or drop the references owned by those pointers. Mount cleanup only drops them when BTRFS_FS_ERROR is set, so an error such as -ENOMEM while processing a later root can leave references behind.
Keep temporary references to the fs roots associated during recovery. On failure, clear their reloc_root pointers and drop the corresponding references. Once the first transaction commit succeeds, drop only the temporary fs root references and let the normal merge and cleanup paths handle the relocation roots.
Fault injection on a pending-relocation image confirmed the cleanup gap. With an injected first-commit failure, 25 fs roots had reloc_root set with fs_error=0. With this fix, the same failure path drops that count to 0 before mount fails.
--- fs/btrfs/relocation.c | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-)
fs/btrfs/relocation.c +27 -4
@@ -4205,6 +4205,24 @@ static noinline_for_stack int mark_garbage_root(struct btrfs_root *root)return ret; } +static void release_recovered_fs_roots (struct list_head *roots,bool drop_reloc_refs) +{ +struct btrfs_root *root; +struct btrfs_root *next; + +list_for_each_entry_safe (root, next, roots, reloc_dirty_list) { +list_del_init (&root->reloc_dirty_list); +if (drop_reloc_refs) { +struct btrfs_root *reloc_root = root->reloc_root; + +ASSERT (reloc_root); + root->reloc_root =NULL ; +btrfs_put_root (reloc_root); + } +btrfs_put_root (root); + } +} +/* * recover relocation interrupted by system crash. *
@@ -4214,6 +4232,7 @@ static noinline_for_stack int mark_garbage_root(struct btrfs_root *root)int btrfs_recover_relocation (struct btrfs_fs_info *fs_info) {LIST_HEAD (reloc_roots); +LIST_HEAD (recovered_roots);struct btrfs_key key;struct btrfs_root *fs_root;struct btrfs_root *reloc_root;
@@ -4326,7 +4345,7 @@ int btrfs_recover_relocation(struct btrfs_fs_info *fs_info) ret =PTR_ERR (fs_root);list_add_tail (&reloc_root->root_list, &reloc_roots);btrfs_end_transaction (trans); - goto out_unset; + goto out_drop_reloc_refs; } ret =__add_reloc_root (reloc_root);
@@ -4335,15 +4354,17 @@ int btrfs_recover_relocation(struct btrfs_fs_info *fs_info)list_add_tail (&reloc_root->root_list, &reloc_roots);btrfs_put_root (fs_root);btrfs_end_transaction (trans); - goto out_unset; + goto out_drop_reloc_refs; } +ASSERT (list_empty (&fs_root->reloc_dirty_list)); fs_root->reloc_root =btrfs_grab_root (reloc_root); -btrfs_put_root (fs_root); +list_add_tail (&fs_root->reloc_dirty_list, &recovered_roots); } ret =btrfs_commit_transaction (trans);if (ret) - goto out_unset; + goto out_drop_reloc_refs; +release_recovered_fs_roots (&recovered_roots, false);merge_reloc_roots (rc);
@@ -4359,6 +4380,8 @@ int btrfs_recover_relocation(struct btrfs_fs_info *fs_info) ret2 =clean_dirty_subvols (rc);if (ret2 <0 && !ret) ret = ret2; +out_drop_reloc_refs: +release_recovered_fs_roots (&recovered_roots, true); out_unset:unset_reloc_control (rc);reloc_chunk_end (fs_info);
Signature · 1 line
2.53.0