[PATCH] btrfs: unwind device add when sysfs registration fails
From: Adarsh Das
Date: Sat Oct 03 2026 - 07:20:32 EST
btrfs_init_new_device() ignored the return value of
btrfs_sysfs_add_device(). When sysfs link creation failed (e.g. -EEXIST),
the add path continued, aborted the transaction with the same errno, and
tripped WARN_ON(btrfs_abort_should_print_stack()) while leaving the device
half-registered in memory.
Unwind through error_sysfs when sysfs registration fails, matching other
failure paths after the device lists are updated.
Reported-by: syzbot+3bf3e110b2d406b8166c@xxxxxxxxxxxxxxxxxxxxxxxxx
Closes: https://lore.kernel.org/all/6a791c94.01d0871a.3a0d52.0099.GAE@xxxxxxxxxx
Tested-by: syzbot+3bf3e110b2d406b8166c@xxxxxxxxxxxxxxxxxxxxxxxxx
Assisted-by: LLM
Signed-off-by: Adarsh Das <adarshdas950@xxxxxxxxx>
---
fs/btrfs/volumes.c | 6 +++++-
1 file changed, 5 insertions(+), 1 deletion(-)
diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c
index 85ea9c5d4536..1517ed3da22c 100644
--- a/fs/btrfs/volumes.c
+++ b/fs/btrfs/volumes.c
@@ -3066,7 +3066,11 @@ int btrfs_init_new_device(struct btrfs_fs_info *fs_info, const char *device_path
mutex_unlock(&fs_info->chunk_mutex);
/* Add sysfs device entry */
- btrfs_sysfs_add_device(device);
+ ret = btrfs_sysfs_add_device(device);
+ if (ret) {
+ mutex_unlock(&fs_devices->device_list_mutex);
+ goto error_sysfs;
+ }
mutex_unlock(&fs_devices->device_list_mutex);