btrfs: reorder initialization before the mount locks uuid_mutex
authorDavid Sterba <dsterba@suse.com>
Tue, 19 Jun 2018 16:01:24 +0000 (18:01 +0200)
committerDavid Sterba <dsterba@suse.com>
Mon, 6 Aug 2018 11:12:48 +0000 (13:12 +0200)
In preparation to take a big lock, move resource initialization before
the critical section. It's not obvious from the diff, the desired order
is:

- initialize mount security options
- allocate temporary fs_info
- allocate superblock buffers

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

index de87b7b0367d719e1adb4ce91937b509e28ee7f9..d33fc1fc42858a48c8b94f837b37b0a33181d843 100644 (file)
@@ -1526,14 +1526,6 @@ static struct dentry *btrfs_mount_root(struct file_system_type *fs_type,
        if (!(flags & SB_RDONLY))
                mode |= FMODE_WRITE;
 
        if (!(flags & SB_RDONLY))
                mode |= FMODE_WRITE;
 
-       mutex_lock(&uuid_mutex);
-       error = btrfs_parse_early_options(data, mode, fs_type,
-                                         &fs_devices);
-       mutex_unlock(&uuid_mutex);
-       if (error) {
-               return ERR_PTR(error);
-       }
-
        security_init_mnt_opts(&new_sec_opts);
        if (data) {
                error = parse_security_options(data, &new_sec_opts);
        security_init_mnt_opts(&new_sec_opts);
        if (data) {
                error = parse_security_options(data, &new_sec_opts);
@@ -1541,12 +1533,6 @@ static struct dentry *btrfs_mount_root(struct file_system_type *fs_type,
                        return ERR_PTR(error);
        }
 
                        return ERR_PTR(error);
        }
 
-       mutex_lock(&uuid_mutex);
-       error = btrfs_scan_one_device(device_name, mode, fs_type, &fs_devices);
-       mutex_unlock(&uuid_mutex);
-       if (error)
-               goto error_sec_opts;
-
        /*
         * Setup a dummy root and fs_info for test/set super.  This is because
         * we don't actually fill this stuff out until open_ctree, but we need
        /*
         * Setup a dummy root and fs_info for test/set super.  This is because
         * we don't actually fill this stuff out until open_ctree, but we need
@@ -1559,8 +1545,6 @@ static struct dentry *btrfs_mount_root(struct file_system_type *fs_type,
                goto error_sec_opts;
        }
 
                goto error_sec_opts;
        }
 
-       fs_info->fs_devices = fs_devices;
-
        fs_info->super_copy = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_KERNEL);
        fs_info->super_for_commit = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_KERNEL);
        security_init_mnt_opts(&fs_info->security_opts);
        fs_info->super_copy = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_KERNEL);
        fs_info->super_for_commit = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_KERNEL);
        security_init_mnt_opts(&fs_info->security_opts);
@@ -1569,6 +1553,20 @@ static struct dentry *btrfs_mount_root(struct file_system_type *fs_type,
                goto error_fs_info;
        }
 
                goto error_fs_info;
        }
 
+       mutex_lock(&uuid_mutex);
+       error = btrfs_parse_early_options(data, mode, fs_type, &fs_devices);
+       mutex_unlock(&uuid_mutex);
+       if (error)
+               goto error_fs_info;
+
+       mutex_lock(&uuid_mutex);
+       error = btrfs_scan_one_device(device_name, mode, fs_type, &fs_devices);
+       mutex_unlock(&uuid_mutex);
+       if (error)
+               goto error_fs_info;
+
+       fs_info->fs_devices = fs_devices;
+
        mutex_lock(&uuid_mutex);
        error = btrfs_open_devices(fs_devices, mode, fs_type);
        mutex_unlock(&uuid_mutex);
        mutex_lock(&uuid_mutex);
        error = btrfs_open_devices(fs_devices, mode, fs_type);
        mutex_unlock(&uuid_mutex);