]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/commitdiff
btrfs: reset device back to allocation state when removing
authorJohannes Thumshirn <jthumshirn@suse.de>
Wed, 4 Dec 2019 13:36:39 +0000 (14:36 +0100)
committerDavid Sterba <dsterba@suse.com>
Mon, 20 Jan 2020 15:40:53 +0000 (16:40 +0100)
When closing a device, btrfs_close_one_device() first allocates a new
device, copies the device to close's name, replaces it in the dev_list
with the copy and then finally frees it.

This involves two memory allocation, which can potentially fail. As this
code path is tricky to unwind, the allocation failures where handled by
BUG_ON()s.

But this copying isn't strictly needed, all that is needed is resetting
the device in question to it's state it had after the allocation.

Signed-off-by: Johannes Thumshirn <jthumshirn@suse.de>
Reviewed-by: David Sterba <dsterba@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
fs/btrfs/volumes.c

index ac7b483754c48481b8ece2e0a4d12c72ab757e7c..86990b7a60edb3834d3f773edc07c7a870bcbe9a 100644 (file)
@@ -1066,8 +1066,6 @@ static void btrfs_close_bdev(struct btrfs_device *device)
 static void btrfs_close_one_device(struct btrfs_device *device)
 {
        struct btrfs_fs_devices *fs_devices = device->fs_devices;
-       struct btrfs_device *new_device;
-       struct rcu_string *name;
 
        if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) &&
            device->devid != BTRFS_DEV_REPLACE_DEVID) {
@@ -1079,25 +1077,22 @@ static void btrfs_close_one_device(struct btrfs_device *device)
                fs_devices->missing_devices--;
 
        btrfs_close_bdev(device);
-       if (device->bdev)
+       if (device->bdev) {
                fs_devices->open_devices--;
-
-       new_device = btrfs_alloc_device(NULL, &device->devid,
-                                       device->uuid);
-       BUG_ON(IS_ERR(new_device)); /* -ENOMEM */
-
-       /* Safe because we are under uuid_mutex */
-       if (device->name) {
-               name = rcu_string_strdup(device->name->str, GFP_NOFS);
-               BUG_ON(!name); /* -ENOMEM */
-               rcu_assign_pointer(new_device->name, name);
+               device->bdev = NULL;
        }
+       clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
 
-       list_replace_rcu(&device->dev_list, &new_device->dev_list);
-       new_device->fs_devices = device->fs_devices;
+       device->fs_info = NULL;
+       atomic_set(&device->dev_stats_ccnt, 0);
+       extent_io_tree_release(&device->alloc_state);
 
-       synchronize_rcu();
-       btrfs_free_device(device);
+       /* Verify the device is back in a pristine state  */
+       ASSERT(!test_bit(BTRFS_DEV_STATE_FLUSH_SENT, &device->dev_state));
+       ASSERT(!test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state));
+       ASSERT(list_empty(&device->dev_alloc_list));
+       ASSERT(list_empty(&device->post_commit_list));
+       ASSERT(atomic_read(&device->reada_in_flight) == 0);
 }
 
 static int close_fs_devices(struct btrfs_fs_devices *fs_devices)