Skip to content

Commit 39a2036

Browse files
kdaveasj
authored andcommitted
btrfs: lift uuid_mutex to callers of btrfs_scan_one_device
Prepartory work to fix race between mount and device scan. The callers will have to manage the critical section, eg. mount wants to scan and then call btrfs_open_devices without the ioctl scan walking in and modifying the fs devices in the meantime. Signed-off-by: David Sterba <[email protected]> Reviewed-by: Anand Jain <[email protected]>
1 parent e735e86 commit 39a2036

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

fs/btrfs/super.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -915,8 +915,10 @@ static int btrfs_parse_early_options(const char *options, fmode_t flags,
915915
error = -ENOMEM;
916916
goto out;
917917
}
918+
mutex_lock(&uuid_mutex);
918919
error = btrfs_scan_one_device(device_name,
919920
flags, holder, fs_devices);
921+
mutex_unlock(&uuid_mutex);
920922
kfree(device_name);
921923
if (error)
922924
goto out;
@@ -1537,7 +1539,9 @@ static struct dentry *btrfs_mount_root(struct file_system_type *fs_type,
15371539
return ERR_PTR(error);
15381540
}
15391541

1542+
mutex_lock(&uuid_mutex);
15401543
error = btrfs_scan_one_device(device_name, mode, fs_type, &fs_devices);
1544+
mutex_unlock(&uuid_mutex);
15411545
if (error)
15421546
goto error_sec_opts;
15431547

@@ -2232,15 +2236,21 @@ static long btrfs_control_ioctl(struct file *file, unsigned int cmd,
22322236

22332237
switch (cmd) {
22342238
case BTRFS_IOC_SCAN_DEV:
2239+
mutex_lock(&uuid_mutex);
22352240
ret = btrfs_scan_one_device(vol->name, FMODE_READ,
22362241
&btrfs_root_fs_type, &fs_devices);
2242+
mutex_unlock(&uuid_mutex);
22372243
break;
22382244
case BTRFS_IOC_DEVICES_READY:
2245+
mutex_lock(&uuid_mutex);
22392246
ret = btrfs_scan_one_device(vol->name, FMODE_READ,
22402247
&btrfs_root_fs_type, &fs_devices);
2241-
if (ret)
2248+
if (ret) {
2249+
mutex_unlock(&uuid_mutex);
22422250
break;
2251+
}
22432252
ret = !(fs_devices->num_devices == fs_devices->total_devices);
2253+
mutex_unlock(&uuid_mutex);
22442254
break;
22452255
case BTRFS_IOC_GET_SUPPORTED_FEATURES:
22462256
ret = btrfs_ioctl_get_supported_features((void __user*)arg);

fs/btrfs/volumes.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1226,6 +1226,8 @@ int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
12261226
int ret = 0;
12271227
u64 bytenr;
12281228

1229+
lockdep_assert_held(&uuid_mutex);
1230+
12291231
/*
12301232
* we would like to check all the supers, but that would make
12311233
* a btrfs mount succeed after a mkfs from a different FS.
@@ -1244,7 +1246,6 @@ int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
12441246
goto error_bdev_put;
12451247
}
12461248

1247-
mutex_lock(&uuid_mutex);
12481249
device = device_list_add(path, disk_super, &new_device_added);
12491250
if (IS_ERR(device)) {
12501251
ret = PTR_ERR(device);
@@ -1253,7 +1254,6 @@ int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
12531254
if (new_device_added)
12541255
btrfs_free_stale_devices(path, device);
12551256
}
1256-
mutex_unlock(&uuid_mutex);
12571257

12581258
btrfs_release_disk_super(page);
12591259

0 commit comments

Comments
 (0)