Commit 9c6d173e authored by Anand Jain's avatar Anand Jain Committed by David Sterba

btrfs: extend locked section when adding a new device in device_list_add

Make sure the device_list_lock is held the whole time:

* when the device is being looked up
* new device is initialized and put to the list
* the list counters are updated (fs_devices::opened, fs_devices::total_devices)
Signed-off-by: default avatarAnand Jain <anand.jain@oracle.com>
[ update changelog ]
Reviewed-by: default avatarDavid Sterba <dsterba@suse.com>
Signed-off-by: default avatarDavid Sterba <dsterba@suse.com>
parent 4306a974
...@@ -762,21 +762,26 @@ static noinline struct btrfs_device *device_list_add(const char *path, ...@@ -762,21 +762,26 @@ static noinline struct btrfs_device *device_list_add(const char *path,
if (IS_ERR(fs_devices)) if (IS_ERR(fs_devices))
return ERR_CAST(fs_devices); return ERR_CAST(fs_devices);
mutex_lock(&fs_devices->device_list_mutex);
list_add(&fs_devices->fs_list, &fs_uuids); list_add(&fs_devices->fs_list, &fs_uuids);
device = NULL; device = NULL;
} else { } else {
mutex_lock(&fs_devices->device_list_mutex);
device = find_device(fs_devices, devid, device = find_device(fs_devices, devid,
disk_super->dev_item.uuid); disk_super->dev_item.uuid);
} }
if (!device) { if (!device) {
if (fs_devices->opened) if (fs_devices->opened) {
mutex_unlock(&fs_devices->device_list_mutex);
return ERR_PTR(-EBUSY); return ERR_PTR(-EBUSY);
}
device = btrfs_alloc_device(NULL, &devid, device = btrfs_alloc_device(NULL, &devid,
disk_super->dev_item.uuid); disk_super->dev_item.uuid);
if (IS_ERR(device)) { if (IS_ERR(device)) {
mutex_unlock(&fs_devices->device_list_mutex);
/* we can safely leave the fs_devices entry around */ /* we can safely leave the fs_devices entry around */
return device; return device;
} }
...@@ -784,14 +789,13 @@ static noinline struct btrfs_device *device_list_add(const char *path, ...@@ -784,14 +789,13 @@ static noinline struct btrfs_device *device_list_add(const char *path,
name = rcu_string_strdup(path, GFP_NOFS); name = rcu_string_strdup(path, GFP_NOFS);
if (!name) { if (!name) {
btrfs_free_device(device); btrfs_free_device(device);
mutex_unlock(&fs_devices->device_list_mutex);
return ERR_PTR(-ENOMEM); return ERR_PTR(-ENOMEM);
} }
rcu_assign_pointer(device->name, name); rcu_assign_pointer(device->name, name);
mutex_lock(&fs_devices->device_list_mutex);
list_add_rcu(&device->dev_list, &fs_devices->devices); list_add_rcu(&device->dev_list, &fs_devices->devices);
fs_devices->num_devices++; fs_devices->num_devices++;
mutex_unlock(&fs_devices->device_list_mutex);
device->fs_devices = fs_devices; device->fs_devices = fs_devices;
*new_device_added = true; *new_device_added = true;
...@@ -838,12 +842,15 @@ static noinline struct btrfs_device *device_list_add(const char *path, ...@@ -838,12 +842,15 @@ static noinline struct btrfs_device *device_list_add(const char *path,
* with larger generation number or the last-in if * with larger generation number or the last-in if
* generation are equal. * generation are equal.
*/ */
mutex_unlock(&fs_devices->device_list_mutex);
return ERR_PTR(-EEXIST); return ERR_PTR(-EEXIST);
} }
name = rcu_string_strdup(path, GFP_NOFS); name = rcu_string_strdup(path, GFP_NOFS);
if (!name) if (!name) {
mutex_unlock(&fs_devices->device_list_mutex);
return ERR_PTR(-ENOMEM); return ERR_PTR(-ENOMEM);
}
rcu_string_free(device->name); rcu_string_free(device->name);
rcu_assign_pointer(device->name, name); rcu_assign_pointer(device->name, name);
if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state)) { 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, ...@@ -863,6 +870,7 @@ static noinline struct btrfs_device *device_list_add(const char *path,
fs_devices->total_devices = btrfs_super_num_devices(disk_super); fs_devices->total_devices = btrfs_super_num_devices(disk_super);
mutex_unlock(&fs_devices->device_list_mutex);
return device; return device;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment