Skip to contentSkip to conversation

remove BUG_ON()s in btrfs_close_one_device()

PATCH Superseded Johannes Thumshirn 1 participant 3 messages Nov 25, 2019 Btrfs file system

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v3 cover remove BUG_ON()s in btrfs_close_one_device()

This series attempts to remove the BUG_ON()s in btrfs_close_one_device(). Therefore some reorganization of btrfs_close_one_device() was needed, to avoid the memory allocation.

This series has passed fstests without any deviation from the baseline.

Changes to v2:
- Completly different approach to the origianl patchset, instead of handling eventual allocation failures.
- Dropped already merged patches for ' btrfs_fs_devices::rotating' and 'btrfs_fs_devices::seeding'
- Kept the 1st patch of the old series, as it's a nice cleanup

Changes to v1:
- Fixed the decremt of btrfs_fs_devices::seeding.
- In addition to this, I've added two patches changing btrfs_fs_devices::seeding and btrfs_fs_devices::rotating to bool, as they are in fact used as booleans.

Johannes Thumshirn (2):
btrfs: decrement number of open devices after closing the device not
before
btrfs: reset device back to allocation state when removing

 fs/btrfs/volumes.c | 37 ++++++++++++++++---------------------
 1 file changed, 16 insertions(+), 21 deletions(-)
Signature · 1 line
2.16.4
#1 lore raw

PATCH v3 1/2 btrfs: decrement number of open devices after closing the device not before

In btrfs_close_one_device we're decrementing the number of open devices before we're calling btrfs_close_bdev().

As there is no intermediate exit between these points in this function it is technically OK to do so, but it makes the code a bit harder to understand.

Move both operations closer together and move the decrement step after btrfs_close_bdev().

---
 fs/btrfs/volumes.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
fs/btrfs/volumes.c +2 -3
index d8e5560db285..2398b071bcf6 100644
@@ -1067,9 +1067,6 @@ static void btrfs_close_one_device(struct btrfs_device *device)
 	struct btrfs_device *new_device;
 	struct rcu_string *name;
 
-	if (device->bdev)
-		fs_devices->open_devices--;
-
 	if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) &&
 	    device->devid != BTRFS_DEV_REPLACE_DEVID) {
 		list_del_init(&device->dev_alloc_list);
@@ -1080,6 +1077,8 @@ static void btrfs_close_one_device(struct btrfs_device *device)
 		fs_devices->missing_devices--;
 
 	btrfs_close_bdev(device);
+	if (device->bdev)
+		fs_devices->open_devices--;
 
 	new_device = btrfs_alloc_device(NULL, &device->devid,
 					device->uuid);
Signature · 1 line
2.16.4
#2 lore raw

PATCH v3 2/2 btrfs: reset device back to allocation state when removing

In reply to Johannes Thumshirn

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.

---
 fs/btrfs/volumes.c | 34 +++++++++++++++-------------------
 1 file changed, 15 insertions(+), 19 deletions(-)
fs/btrfs/volumes.c +15 -19
index 2398b071bcf6..3d04fa4a0ba6 100644
@@ -1064,38 +1064,34 @@ 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) {
 		list_del_init(&device->dev_alloc_list);
 		fs_devices->rw_devices--;
 	}
+	clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
 
-	if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state))
+	if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state)) {
 		fs_devices->missing_devices--;
+		clear_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state);
+	}
 
+	clear_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
 	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;
 	}
 
-	list_replace_rcu(&device->dev_list, &new_device->dev_list);
-	new_device->fs_devices = device->fs_devices;
-
-	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);
+	ASSERT(atomic_read(&device->dev_stats_ccnt) == 0);
+	ASSERT(RB_EMPTY_ROOT(&device->alloc_state.state));
 }
 
 static int close_fs_devices(struct btrfs_fs_devices *fs_devices)
Signature · 1 line
2.16.4