]> git.hungrycats.org Git - linux/commitdiff
btrfs: unwind device add when sysfs registration fails
authorAdarsh Das <adarshdas950@gmail.com>
Wed, 7 Oct 2026 16:03:49 +0000 (21:33 +0530)
committerDavid Sterba <dsterba@suse.com>
Fri, 30 Oct 2026 20:36:16 +0000 (21:36 +0100)
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.

Check the return value and unwind when registration fails, without
aborting the filesystem for errnos like -EEXIST. If add failed before
anything was published in sysfs, tear down the in-memory device state only
and do not call btrfs_sysfs_remove_device(). When the block-device link
was created but devid kobject registration fails, drop the link inside
btrfs_sysfs_add_device() before returning the error.

Reported-by: syzbot+3bf3e110b2d406b8166c@syzkaller.appspotmail.com
Link: https://lore.kernel.org/all/6a791c94.01d0871a.3a0d52.0099.GAE@google.com
Tested-by: syzbot+3bf3e110b2d406b8166c@syzkaller.appspotmail.com
Assisted-by: LLM
Reviewed-by: Johannes Thumshirn <johannes.thumshirn@wdc.com>
Signed-off-by: Adarsh Das <adarshdas950@gmail.com>
Signed-off-by: David Sterba <dsterba@suse.com>
fs/btrfs/sysfs.c
fs/btrfs/volumes.c

index 3ac1fa608d3b5696c00316817978094615c3dba1..f508a2329181ded510f10f228ae410df91b682f7 100644 (file)
@@ -2049,6 +2049,9 @@ int btrfs_sysfs_add_device(struct btrfs_device *device)
        ret = kobject_init_and_add(&device->devid_kobj, &devid_ktype,
                                   devinfo_kobj, "%llu", device->devid);
        if (ret) {
+               if (device->bdev)
+                       sysfs_remove_link(devices_kobj,
+                                         bdev_kobj(device->bdev)->name);
                kobject_put(&device->devid_kobj);
                btrfs_warn(device->fs_info,
                           "devinfo init for devid %llu failed: %d",
index d3e6a9b429dd9de46f8aa2da86129e69d9ea4ad0..f1305f68b1f8ab248090fe26fe8f82d3bf59e914 100644 (file)
@@ -3097,7 +3097,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_unwind_device;
+       }
 
        mutex_unlock(&fs_devices->device_list_mutex);
 
@@ -3178,6 +3182,7 @@ int btrfs_init_new_device(struct btrfs_fs_info *fs_info, const char *device_path
 
 error_sysfs:
        btrfs_sysfs_remove_device(device);
+error_unwind_device:
        mutex_lock(&fs_info->fs_devices->device_list_mutex);
        if (seeding_dev)
                btrfs_assign_next_active_device(device, seed_devices->latest_dev);