Skip to content

Commit 0dd1ff5

Browse files
committed
btrfs: fix device_list_add() missing device_list_mutex()
When the device is added or if the device pointer is retrieved for writing, make sure that device_list_mutex is held. Also make sure it is held when we check fs_devices::opened and fs_devices::total_devices is updated. Signed-off-by: Anand Jain <[email protected]>
1 parent 622f0a7 commit 0dd1ff5

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

fs/btrfs/volumes.c

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -762,36 +762,40 @@ static noinline struct btrfs_device *device_list_add(const char *path,
762762
if (IS_ERR(fs_devices))
763763
return ERR_CAST(fs_devices);
764764

765+
mutex_lock(&fs_devices->device_list_mutex);
765766
list_add(&fs_devices->fs_list, &fs_uuids);
766767

767768
device = NULL;
768769
} else {
770+
mutex_lock(&fs_devices->device_list_mutex);
769771
device = find_device(fs_devices, devid,
770772
disk_super->dev_item.uuid);
771773
}
772774

773775
if (!device) {
774-
if (fs_devices->opened)
776+
if (fs_devices->opened) {
777+
mutex_unlock(&fs_devices->device_list_mutex);
775778
return ERR_PTR(-EBUSY);
779+
}
776780

777781
device = btrfs_alloc_device(NULL, &devid,
778782
disk_super->dev_item.uuid);
779783
if (IS_ERR(device)) {
784+
mutex_unlock(&fs_devices->device_list_mutex);
780785
/* we can safely leave the fs_devices entry around */
781786
return device;
782787
}
783788

784789
name = rcu_string_strdup(path, GFP_NOFS);
785790
if (!name) {
786791
btrfs_free_device(device);
792+
mutex_unlock(&fs_devices->device_list_mutex);
787793
return ERR_PTR(-ENOMEM);
788794
}
789795
rcu_assign_pointer(device->name, name);
790796

791-
mutex_lock(&fs_devices->device_list_mutex);
792797
list_add_rcu(&device->dev_list, &fs_devices->devices);
793798
fs_devices->num_devices++;
794-
mutex_unlock(&fs_devices->device_list_mutex);
795799

796800
device->fs_devices = fs_devices;
797801
*new_device_added = true;
@@ -838,12 +842,15 @@ static noinline struct btrfs_device *device_list_add(const char *path,
838842
* with larger generation number or the last-in if
839843
* generation are equal.
840844
*/
845+
mutex_unlock(&fs_devices->device_list_mutex);
841846
return ERR_PTR(-EEXIST);
842847
}
843848

844849
name = rcu_string_strdup(path, GFP_NOFS);
845-
if (!name)
850+
if (!name) {
851+
mutex_unlock(&fs_devices->device_list_mutex);
846852
return ERR_PTR(-ENOMEM);
853+
}
847854
rcu_string_free(device->name);
848855
rcu_assign_pointer(device->name, name);
849856
if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state)) {
@@ -863,6 +870,7 @@ static noinline struct btrfs_device *device_list_add(const char *path,
863870

864871
fs_devices->total_devices = btrfs_super_num_devices(disk_super);
865872

873+
mutex_unlock(&fs_devices->device_list_mutex);
866874
return device;
867875
}
868876

0 commit comments

Comments
 (0)