btrfs: use device_list_mutex when removing stale devices
authorAnand Jain <anand.jain@oracle.com>
Tue, 29 May 2018 09:23:20 +0000 (17:23 +0800)
committerDavid Sterba <dsterba@suse.com>
Mon, 6 Aug 2018 11:12:47 +0000 (13:12 +0200)
btrfs_free_stale_devices() finds a stale (not opened) device matching
path in the fs_uuid list. We are already under uuid_mutex so when we
check for each fs_devices, hold the device_list_mutex too.

Signed-off-by: Anand Jain <anand.jain@oracle.com>
Reviewed-by: David Sterba <dsterba@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
fs/btrfs/volumes.c

index f4ce081bbe462be0195d499da82517891473a01d..6a8f1e2c116f662655c67badc388064b543680e7 100644 (file)
@@ -637,8 +637,11 @@ static void btrfs_free_stale_devices(const char *path,
        struct btrfs_device *device, *tmp_device;
 
        list_for_each_entry_safe(fs_devices, tmp_fs_devices, &fs_uuids, fs_list) {
-               if (fs_devices->opened)
+               mutex_lock(&fs_devices->device_list_mutex);
+               if (fs_devices->opened) {
+                       mutex_unlock(&fs_devices->device_list_mutex);
                        continue;
+               }
 
                list_for_each_entry_safe(device, tmp_device,
                                         &fs_devices->devices, dev_list) {
@@ -658,16 +661,18 @@ static void btrfs_free_stale_devices(const char *path,
                                continue;
 
                        /* delete the stale device */
-                       if (fs_devices->num_devices == 1) {
-                               btrfs_sysfs_remove_fsid(fs_devices);
-                               list_del(&fs_devices->fs_list);
-                               free_fs_devices(fs_devices);
+                       fs_devices->num_devices--;
+                       list_del(&device->dev_list);
+                       btrfs_free_device(device);
+
+                       if (fs_devices->num_devices == 0)
                                break;
-                       } else {
-                               fs_devices->num_devices--;
-                               list_del(&device->dev_list);
-                               btrfs_free_device(device);
-                       }
+               }
+               mutex_unlock(&fs_devices->device_list_mutex);
+               if (fs_devices->num_devices == 0) {
+                       btrfs_sysfs_remove_fsid(fs_devices);
+                       list_del(&fs_devices->fs_list);
+                       free_fs_devices(fs_devices);
                }
        }
 }