Skip to contentSkip to conversation

fix bug in btrfs_init_new_device()

PATCH Superseded Anand Jain 4 participants 18 messages Sep 25, 2017 – Sep 26, 2017 Btrfs file system

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Anand Jain

author
#0 lore raw

cover fix bug in btrfs_init_new_device()

1/3 fixes a bug which failed to reset writable when sprouting failed 2/3 is cleanup and preparatory to fix 3/3
3/3 fixes BUG_ON in btrfs_init_new_device()

Anand Jain (3):
btrfs: undo writable when sprouting fails
btrfs: cleanup btrfs_init_new_device()
btrfs: fix BUG_ON in btrfs_init_new_device()

 fs/btrfs/volumes.c | 27 +++++++++++++--------------
 1 file changed, 13 insertions(+), 14 deletions(-)
Signature · 1 line
2.13.1
author
#1 lore raw

PATCH 1/3 btrfs: undo writable when sprouting fails

---
 fs/btrfs/volumes.c | 2 ++
 1 file changed, 2 insertions(+)
fs/btrfs/volumes.c +2 -0
index 0e8f16c305df..9d64700cc9b6 100644
@@ -2501,6 +2501,8 @@ int btrfs_init_new_device(struct btrfs_fs_info *fs_info, const char *device_path
 	return ret;
 
 error_trans:
+	if (seeding_dev)
+		sb->s_flags |= MS_RDONLY;
 	btrfs_end_transaction(trans);
 	rcu_string_free(device->name);
 	btrfs_sysfs_rm_device_link(fs_info->fs_devices, device);
Signature · 1 line
2.13.1
#2 lore raw

Re: [PATCH 1/3] btrfs: undo writable when sprouting fails

Quoted 1 line · Anand Jain

The error_trans tag needs to revert the RDONLY flag without doubt.

But just a small question.

Just lines before error_trans tag, we're committing transaction and then relocating system chunks. In which we don't use goto error branch but returning directly.

Don't we need to to revert the RDONLY flag in that error handler?

Thanks,
Qu

Quoted 18 lines
---
  fs/btrfs/volumes.c | 2 ++
  1 file changed, 2 insertions(+)
fs/btrfs/volumes.c +2 -0
index 0e8f16c305df..9d64700cc9b6 100644
@@ -2501,6 +2501,8 @@ int btrfs_init_new_device(struct btrfs_fs_info *fs_info, const char *device_path
  	return ret;
 
  error_trans:
+	if (seeding_dev)
+		sb->s_flags |= MS_RDONLY;
  	btrfs_end_transaction(trans);
  	rcu_string_free(device->name);
  	btrfs_sysfs_rm_device_link(fs_info->fs_devices, device);
author
#4 lore raw

PATCH 2/3 btrfs: cleanup btrfs_init_new_device()

In reply to Anand Jain

Moves btrfs_abort_transaction() to the error goto and renames error_trans to error_sysfs. This is a preparatory patch to remove the BUG_ON() in btrfs_init_new_device().

---
 fs/btrfs/volumes.c | 23 +++++++++--------------
 1 file changed, 9 insertions(+), 14 deletions(-)
fs/btrfs/volumes.c +9 -14
index 9d64700cc9b6..5c34eea9d12d 100644
@@ -2443,26 +2443,20 @@ int btrfs_init_new_device(struct btrfs_fs_info *fs_info, const char *device_path
 		mutex_lock(&fs_info->chunk_mutex);
 		ret = init_first_rw_device(trans, fs_info);
 		mutex_unlock(&fs_info->chunk_mutex);
-		if (ret) {
-			btrfs_abort_transaction(trans, ret);
-			goto error_trans;
-		}
+		if (ret)
+			goto error_sysfs;
 	}
 
 	ret = btrfs_add_device(trans, fs_info, device);
-	if (ret) {
-		btrfs_abort_transaction(trans, ret);
-		goto error_trans;
-	}
+	if (ret)
+		goto error_sysfs;
 
 	if (seeding_dev) {
 		char fsid_buf[BTRFS_UUID_UNPARSED_SIZE];
 
 		ret = btrfs_finish_sprout(trans, fs_info);
-		if (ret) {
-			btrfs_abort_transaction(trans, ret);
-			goto error_trans;
-		}
+		if (ret)
+			goto error_sysfs;
 
 		/* Sprouting would change fsid of the mounted root,
 		 * so rename the fsid on the sysfs
@@ -2500,12 +2494,13 @@ int btrfs_init_new_device(struct btrfs_fs_info *fs_info, const char *device_path
 	update_dev_time(device_path);
 	return ret;
 
-error_trans:
+error_sysfs:
+	btrfs_sysfs_rm_device_link(fs_info->fs_devices, device);
 	if (seeding_dev)
 		sb->s_flags |= MS_RDONLY;
+	btrfs_abort_transaction(trans, ret);
 	btrfs_end_transaction(trans);
 	rcu_string_free(device->name);
-	btrfs_sysfs_rm_device_link(fs_info->fs_devices, device);
 	kfree(device);
 error:
 	blkdev_put(bdev, FMODE_EXCL);
Signature · 1 line
2.13.1
#5 lore raw

Re: [PATCH 2/3] btrfs: cleanup btrfs_init_new_device()

Quoted 47 lines · Anand Jain

Moves btrfs_abort_transaction() to the error goto and renames error_trans to error_sysfs. This is a preparatory patch to remove the BUG_ON() in btrfs_init_new_device().

---
 fs/btrfs/volumes.c | 23 +++++++++--------------
 1 file changed, 9 insertions(+), 14 deletions(-)
fs/btrfs/volumes.c +6 -12
index 9d64700cc9b6..5c34eea9d12d 100644
@@ -2443,26 +2443,20 @@ int btrfs_init_new_device(struct btrfs_fs_info *fs_info, const char *device_path
 		mutex_lock(&fs_info->chunk_mutex);
 		ret = init_first_rw_device(trans, fs_info);
 		mutex_unlock(&fs_info->chunk_mutex);
-		if (ret) {
-			btrfs_abort_transaction(trans, ret);
-			goto error_trans;
-		}
+		if (ret)
+			goto error_sysfs;
 	}
 
 	ret = btrfs_add_device(trans, fs_info, device);
-	if (ret) {
-		btrfs_abort_transaction(trans, ret);
-		goto error_trans;
-	}
+	if (ret)
+		goto error_sysfs;
 
 	if (seeding_dev) {
 		char fsid_buf[BTRFS_UUID_UNPARSED_SIZE];
 
 		ret = btrfs_finish_sprout(trans, fs_info);
-		if (ret) {
-			btrfs_abort_transaction(trans, ret);
-			goto error_trans;
-		}
+		if (ret)
+			goto error_sysfs;
 
 		/* Sprouting would change fsid of the mounted root,
 		 * so rename the fsid on the sysfs

fs/btrfs/volumes.c int btrfs_init_new_device(struct btrfs_fs_info *fs_info, const char *device_path · line 2498

Quoted 6 lines
fs/btrfs/volumes.c +2 -1
@@ -2500,12 +2494,13 @@ int btrfs_init_new_device(struct btrfs_fs_info *fs_info, const char *device_path
 	update_dev_time(device_path);
 	return ret;
 
-error_trans:
+error_sysfs:
+	btrfs_sysfs_rm_device_link(fs_info->fs_devices, device);

Any reason why you move btrfs_sysfs_rm_device_link?

fs/btrfs/volumes.c int btrfs_init_new_device(struct btrfs_fs_info *fs_info, const char *device_path · line 2506

Quoted 7 lines
fs/btrfs/volumes.c +0 -1
 	if (seeding_dev)
 		sb->s_flags |= MS_RDONL> +	btrfs_abort_transaction(trans, ret);
 	btrfs_end_transaction(trans);
 	rcu_string_free(device->name);
-	btrfs_sysfs_rm_device_link(fs_info->fs_devices, device);
 	kfree(device);>  error:
 	blkdev_put(bdev, FMODE_EXCL);

Also which branch is your code based on since in Linus' master the error handling (before your patch) looks a bit different. E.g. the MS_RDONL stuff look wrong.

error_trans:

        btrfs_end_transaction(trans);

        rcu_string_free(device->name);

        btrfs_sysfs_rm_device_link(fs_info->fs_devices, device);

        kfree(device);

error:

        blkdev_put(bdev, FMODE_EXCL);

        if (seeding_dev) {

                mutex_unlock(&uuid_mutex);

                up_write(&sb->s_umount);

        }

        return ret;
Quoted 1 line
#6 lore raw

Re: [PATCH 2/3] btrfs: cleanup btrfs_init_new_device()

Quoted 83 lines · Nikolay Borisov
Quoted 47 lines · Anand Jain

Moves btrfs_abort_transaction() to the error goto and renames error_trans to error_sysfs. This is a preparatory patch to remove the BUG_ON() in btrfs_init_new_device().

---
 fs/btrfs/volumes.c | 23 +++++++++--------------
 1 file changed, 9 insertions(+), 14 deletions(-)
fs/btrfs/volumes.c +6 -12
index 9d64700cc9b6..5c34eea9d12d 100644
@@ -2443,26 +2443,20 @@ int btrfs_init_new_device(struct btrfs_fs_info *fs_info, const char *device_path
 		mutex_lock(&fs_info->chunk_mutex);
 		ret = init_first_rw_device(trans, fs_info);
 		mutex_unlock(&fs_info->chunk_mutex);
-		if (ret) {
-			btrfs_abort_transaction(trans, ret);
-			goto error_trans;
-		}
+		if (ret)
+			goto error_sysfs;
 	}
 
 	ret = btrfs_add_device(trans, fs_info, device);
-	if (ret) {
-		btrfs_abort_transaction(trans, ret);
-		goto error_trans;
-	}
+	if (ret)
+		goto error_sysfs;
 
 	if (seeding_dev) {
 		char fsid_buf[BTRFS_UUID_UNPARSED_SIZE];
 
 		ret = btrfs_finish_sprout(trans, fs_info);
-		if (ret) {
-			btrfs_abort_transaction(trans, ret);
-			goto error_trans;
-		}
+		if (ret)
+			goto error_sysfs;
 
 		/* Sprouting would change fsid of the mounted root,
 		 * so rename the fsid on the sysfs

fs/btrfs/volumes.c int btrfs_init_new_device(struct btrfs_fs_info *fs_info, const char *device_path · line 2498

Quoted 6 lines
fs/btrfs/volumes.c +2 -1
@@ -2500,12 +2494,13 @@ int btrfs_init_new_device(struct btrfs_fs_info *fs_info, const char *device_path
 	update_dev_time(device_path);
 	return ret;
 
-error_trans:
+error_sysfs:
+	btrfs_sysfs_rm_device_link(fs_info->fs_devices, device);

Any reason why you move btrfs_sysfs_rm_device_link?

fs/btrfs/volumes.c int btrfs_init_new_device(struct btrfs_fs_info *fs_info, const char *device_path · line 2506

Quoted 7 lines
fs/btrfs/volumes.c +0 -1
 	if (seeding_dev)
 		sb->s_flags |= MS_RDONL> +	btrfs_abort_transaction(trans, ret);
 	btrfs_end_transaction(trans);
 	rcu_string_free(device->name);
-	btrfs_sysfs_rm_device_link(fs_info->fs_devices, device);
 	kfree(device);>  error:
 	blkdev_put(bdev, FMODE_EXCL);

Also which branch is your code based on since in Linus' master the error handling (before your patch) looks a bit different. E.g. the MS_RDONL stuff look wrong.

error_trans:

        btrfs_end_transaction(trans);

        rcu_string_free(device->name);

        btrfs_sysfs_rm_device_link(fs_info->fs_devices, device);

        kfree(device);

error:

Quoted 11 lines
        blkdev_put(bdev, FMODE_EXCL);

        if (seeding_dev) {

                mutex_unlock(&uuid_mutex);

                up_write(&sb->s_umount);

        }

        return ret;

Ok, turned out I hadn't received 1/3 so this answers this question. The sysfs rm stands though

Quoted 2 lines
Quoted 2 lines
author
#7 lore raw

Re: [PATCH 2/3] btrfs: cleanup btrfs_init_new_device()

Quoted 3 lines

The
sysfs rm stands though

This is a preparatory patch for 3/3, at BUG_ON it needs an exit
without the btrfs_sysfs_add_device_link() part.

-Anand

author
#8 lore raw

Re: [PATCH 2/3] btrfs: cleanup btrfs_init_new_device()

Quoted 6 lines · Anand Jain
Quoted 3 lines

The
sysfs rm stands though

This is a preparatory patch for 3/3, at BUG_ON it needs an exit
without the btrfs_sysfs_add_device_link() part.

typo s/btrfs_sysfs_add_device_link/btrfs_sysfs_rm_device_link/

Quoted 5 lines

-Anand

Signature · 3 lines
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#9 lore raw

Re: [PATCH 2/3] btrfs: cleanup btrfs_init_new_device()

In reply to Anand Jain
Quoted 6 lines · Anand Jain
Quoted 3 lines

The
sysfs rm stands though

 This is a preparatory patch for 3/3, at BUG_ON it needs an exit without  the btrfs_sysfs_add_device_link() part.

In this case I'd rather you have this change in 3/3, because on its own it doesn't really make sense and cannot easily be figured out if doing git blame.

Quoted 3 lines

-Anand

author
#10 lore raw

Re: [PATCH 2/3] btrfs: cleanup btrfs_init_new_device()

Quoted 10 lines
Quoted 6 lines
Quoted 3 lines

The
sysfs rm stands though

This is a preparatory patch for 3/3, at BUG_ON it needs an exit
without the btrfs_sysfs_add_device_link() part.

In this case I'd rather you have this change in 3/3, because on its own it doesn't really make sense and cannot easily be figured out if doing git blame.

Fixed in v3. Thanks for suggesting.

-Anand

#11 lore raw

Re: [PATCH 2/3] btrfs: cleanup btrfs_init_new_device()

In reply to Anand Jain
Quoted 47 lines · Anand Jain

Moves btrfs_abort_transaction() to the error goto and renames error_trans to error_sysfs. This is a preparatory patch to remove the BUG_ON() in btrfs_init_new_device().

---
  fs/btrfs/volumes.c | 23 +++++++++--------------
  1 file changed, 9 insertions(+), 14 deletions(-)
fs/btrfs/volumes.c +6 -12
index 9d64700cc9b6..5c34eea9d12d 100644
@@ -2443,26 +2443,20 @@ int btrfs_init_new_device(struct btrfs_fs_info *fs_info, const char *device_path
  		mutex_lock(&fs_info->chunk_mutex);
  		ret = init_first_rw_device(trans, fs_info);
  		mutex_unlock(&fs_info->chunk_mutex);
-		if (ret) {
-			btrfs_abort_transaction(trans, ret);
-			goto error_trans;
-		}
+		if (ret)
+			goto error_sysfs;
  	}
 
  	ret = btrfs_add_device(trans, fs_info, device);
-	if (ret) {
-		btrfs_abort_transaction(trans, ret);
-		goto error_trans;
-	}
+	if (ret)
+		goto error_sysfs;
 
  	if (seeding_dev) {
  		char fsid_buf[BTRFS_UUID_UNPARSED_SIZE];
 
  		ret = btrfs_finish_sprout(trans, fs_info);
-		if (ret) {
-			btrfs_abort_transaction(trans, ret);
-			goto error_trans;
-		}
+		if (ret)
+			goto error_sysfs;
 
  		/* Sprouting would change fsid of the mounted root,
  		 * so rename the fsid on the sysfs

fs/btrfs/volumes.c int btrfs_init_new_device(struct btrfs_fs_info *fs_info, const char *device_path · line 2502

Quoted 10 lines
fs/btrfs/volumes.c +3 -1
@@ -2500,12 +2494,13 @@ int btrfs_init_new_device(struct btrfs_fs_info *fs_info, const char *device_path
  	update_dev_time(device_path);
  	return ret;
 
-error_trans:
+error_sysfs:
+	btrfs_sysfs_rm_device_link(fs_info->fs_devices, device);
  	if (seeding_dev)
  		sb->s_flags |= MS_RDONLY;
+	btrfs_abort_transaction(trans, ret);
  	btrfs_end_transaction(trans);

Abort then end? Looks redundant at least.

fs/btrfs/volumes.c int btrfs_init_new_device(struct btrfs_fs_info *fs_info, const char *device_path · line 2508

Quoted 2 lines
fs/btrfs/volumes.c +0 -1
  	rcu_string_free(device->name);
-	btrfs_sysfs_rm_device_link(fs_info->fs_devices, device);

Any particular reason to move this line ahead?

I didn't see it has something to do with transaction, so just curious about the purpose.

Thanks,
Qu

Quoted 4 lines
fs/btrfs/volumes.c +0 -0
  	kfree(device);
  error:
  	blkdev_put(bdev, FMODE_EXCL);
#12 lore raw

Re: [PATCH 2/3] btrfs: cleanup btrfs_init_new_device()

In reply to Anand Jain
Quoted 3 lines · Anand Jain

Moves btrfs_abort_transaction() to the error goto and renames error_trans to error_sysfs. This is a preparatory patch to remove the BUG_ON() in btrfs_init_new_device().

Please keep all transaction abort exactly at the place where they happened and do not merge them to one. This pattern should be used everwhere and is important when debugging because we can pinpoint the line in the code from the syslog message and do not have to guess which way it got to the merged call.

This makes the binary large, looks like a code duplication in the code but for a good reason.

author
#13 lore raw

PATCH 3/3 btrfs: fix BUG_ON in btrfs_init_new_device()

In reply to Anand Jain

Here we will simply return the error to the caller. And handle the fail condition by calling the abort transaction through the error path.

---
 fs/btrfs/volumes.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
fs/btrfs/volumes.c +3 -1
index 5c34eea9d12d..93eaf314fe72 100644
@@ -2399,7 +2399,8 @@ int btrfs_init_new_device(struct btrfs_fs_info *fs_info, const char *device_path
 	if (seeding_dev) {
 		sb->s_flags &= ~MS_RDONLY;
 		ret = btrfs_prepare_sprout(fs_info);
-		BUG_ON(ret); /* -ENOMEM */
+		if (ret)
+			goto error_trans;
 	}
 
 	device->fs_devices = fs_info->fs_devices;
@@ -2496,6 +2497,7 @@ int btrfs_init_new_device(struct btrfs_fs_info *fs_info, const char *device_path
 
 error_sysfs:
 	btrfs_sysfs_rm_device_link(fs_info->fs_devices, device);
+error_trans:
 	if (seeding_dev)
 		sb->s_flags |= MS_RDONLY;
 	btrfs_abort_transaction(trans, ret);
Signature · 1 line
2.13.1
author
#14 lore raw

v2 cover fix bug in btrfs_init_new_device()

In reply to Anand Jain

1/3 fixes a bug which failed to reset writable when sprouting failed 2/3 is cleanup and preparatory to fix 3/3
3/3 fixes BUG_ON in btrfs_init_new_device()

v2:
add commit log to 1/3
do not consolidate btrfs_abort_transaction() in 2/3

Anand Jain (3):
btrfs: undo writable when sprouting fails
btrfs: cleanup btrfs_init_new_device()
btrfs: fix BUG_ON in btrfs_init_new_device()

 fs/btrfs/volumes.c | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)
Signature · 1 line
2.13.1
author
#15 lore raw

PATCH v2 1/3 btrfs: undo writable when sprouting fails

When new device is being added to seed FS, seed FS is marked writable, but when we fail to bring in the new device, we missed to undo the writable part. This patch fixes it.

---
v2: add commit log

 fs/btrfs/volumes.c | 2 ++
 1 file changed, 2 insertions(+)
fs/btrfs/volumes.c +2 -0
index 0e8f16c305df..9d64700cc9b6 100644
@@ -2501,6 +2501,8 @@ int btrfs_init_new_device(struct btrfs_fs_info *fs_info, const char *device_path
 	return ret;
 
 error_trans:
+	if (seeding_dev)
+		sb->s_flags |= MS_RDONLY;
 	btrfs_end_transaction(trans);
 	rcu_string_free(device->name);
 	btrfs_sysfs_rm_device_link(fs_info->fs_devices, device);
Signature · 1 line
2.13.1
author
#16 lore raw

PATCH v2 2/3 btrfs: cleanup btrfs_init_new_device()

In reply to Anand Jain

Moves btrfs_abort_transaction() to the error goto and renames error_trans to error_sysfs. This is a preparatory patch to remove the BUG_ON() in btrfs_init_new_device().

---
 fs/btrfs/volumes.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
fs/btrfs/volumes.c +5 -5
index 9d64700cc9b6..22eb81794375 100644
@@ -2445,14 +2445,14 @@ int btrfs_init_new_device(struct btrfs_fs_info *fs_info, const char *device_path
 		mutex_unlock(&fs_info->chunk_mutex);
 		if (ret) {
 			btrfs_abort_transaction(trans, ret);
-			goto error_trans;
+			goto error_sysfs;
 		}
 	}
 
 	ret = btrfs_add_device(trans, fs_info, device);
 	if (ret) {
 		btrfs_abort_transaction(trans, ret);
-		goto error_trans;
+		goto error_sysfs;
 	}
 
 	if (seeding_dev) {
@@ -2461,7 +2461,7 @@ int btrfs_init_new_device(struct btrfs_fs_info *fs_info, const char *device_path
 		ret = btrfs_finish_sprout(trans, fs_info);
 		if (ret) {
 			btrfs_abort_transaction(trans, ret);
-			goto error_trans;
+			goto error_sysfs;
 		}
 
 		/* Sprouting would change fsid of the mounted root,
@@ -2500,12 +2500,12 @@ int btrfs_init_new_device(struct btrfs_fs_info *fs_info, const char *device_path
 	update_dev_time(device_path);
 	return ret;
 
-error_trans:
+error_sysfs:
+	btrfs_sysfs_rm_device_link(fs_info->fs_devices, device);
 	if (seeding_dev)
 		sb->s_flags |= MS_RDONLY;
 	btrfs_end_transaction(trans);
 	rcu_string_free(device->name);
-	btrfs_sysfs_rm_device_link(fs_info->fs_devices, device);
 	kfree(device);
 error:
 	blkdev_put(bdev, FMODE_EXCL);
Signature · 1 line
2.13.1
author
#17 lore raw

PATCH v2 3/3 btrfs: fix BUG_ON in btrfs_init_new_device()

In reply to Anand Jain

Instead of BUG_ON return error to the caller. And handle the fail condition by calling the abort transaction through the error path.

---
v2: do not consolidate btrfs_abort_transaction()

 fs/btrfs/volumes.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
fs/btrfs/volumes.c +5 -1
index 22eb81794375..4cb575fbf643 100644
@@ -2399,7 +2399,10 @@ int btrfs_init_new_device(struct btrfs_fs_info *fs_info, const char *device_path
 	if (seeding_dev) {
 		sb->s_flags &= ~MS_RDONLY;
 		ret = btrfs_prepare_sprout(fs_info);
-		BUG_ON(ret); /* -ENOMEM */
+		if (ret) {
+			btrfs_abort_transaction(trans, ret);
+			goto error_trans;
+		}
 	}
 
 	device->fs_devices = fs_info->fs_devices;
@@ -2502,6 +2505,7 @@ int btrfs_init_new_device(struct btrfs_fs_info *fs_info, const char *device_path
 
 error_sysfs:
 	btrfs_sysfs_rm_device_link(fs_info->fs_devices, device);
+error_trans:
 	if (seeding_dev)
 		sb->s_flags |= MS_RDONLY;
 	btrfs_end_transaction(trans);
Signature · 1 line
2.13.1