Skip to contentSkip to conversation

fix bug in btrfs_init_new_device()

PATCH Superseded Anand Jain 3 participants 7 messages Sep 26, 2017 – Sep 27, 2017 Btrfs file system

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Anand Jain

author
#0 lore raw

v3 cover fix bug in btrfs_init_new_device()

1/2 fixes a bug which failed to reset writable when sprouting failed 2/2 fixes BUG_ON in btrfs_init_new_device()

Anand Jain (2):
btrfs: undo writable when sprouting fails
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
#1 lore raw

PATCH v3 1/2 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.

---
 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 v3 1/2] btrfs: undo writable when sprouting fails

Quoted 14 lines · Anand Jain

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.

---
  fs/btrfs/volumes.c | 2 ++
  1 file changed, 2 insertions(+)
fs/btrfs/volumes.c +0 -0
index 0e8f16c305df..9d64700cc9b6 100644

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

Quoted 5 lines
fs/btrfs/volumes.c +2 -0
@@ -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;

Still the same question.

---

	if (seeding_dev) {
		mutex_unlock(&uuid_mutex);
		up_write(&sb->s_umount);

		if (ret) /* transaction commit */
			return ret;

		ret = btrfs_relocate_sys_chunks(fs_info);
		if (ret < 0)
			btrfs_handle_fs_error(fs_info, ret,
				    "Failed to relocate sys chunks after device initialization. This 
can be fixed using the \"btrfs balance\" command.");
		trans = btrfs_attach_transaction(root);
		if (IS_ERR(trans)) {
			if (PTR_ERR(trans) == -ENOENT)
				return 0;
			return PTR_ERR(trans);
		}

---
In the above btrfs_attch_transaction() error handler, which just returned without going to error_trans tags, did we misseed the s_flag revert thing?

Thanks,
Qu

Quoted 4 lines
fs/btrfs/volumes.c +0 -0
  	btrfs_end_transaction(trans);
  	rcu_string_free(device->name);
  	btrfs_sysfs_rm_device_link(fs_info->fs_devices, device);
#3 lore raw

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

Quoted 44 lines · Qu Wenruo
Quoted 14 lines · Anand Jain

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.

---
  fs/btrfs/volumes.c | 2 ++
  1 file changed, 2 insertions(+)
fs/btrfs/volumes.c +0 -0
index 0e8f16c305df..9d64700cc9b6 100644

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

Quoted 5 lines
fs/btrfs/volumes.c +2 -0
@@ -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;

Still the same question.

---

	if (seeding_dev) {
		mutex_unlock(&uuid_mutex);
		up_write(&sb->s_umount);

		if (ret) /* transaction commit */
			return ret;

		ret = btrfs_relocate_sys_chunks(fs_info);
		if (ret < 0)
			btrfs_handle_fs_error(fs_info, ret,
				    "Failed to relocate sys chunks after device initialization. This 
can be fixed using the \"btrfs balance\" command.");
		trans = btrfs_attach_transaction(root);
		if (IS_ERR(trans)) {
			if (PTR_ERR(trans) == -ENOENT)
				return 0;
			return PTR_ERR(trans);
		}
Quoted 4 lines

---
In the above btrfs_attch_transaction() error handler, which just returned without going to error_trans tags, did we misseed the s_flag revert thing?

I think so. Also I think "sb->s_flags |= MS_RDONLY;" can be moved to the 'if' after the error: label, where we already check the 'seeding_dev' condition.

author
#4 lore raw

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

Quoted 50 lines · David Sterba
Quoted 44 lines · Qu Wenruo
Quoted 14 lines · Anand Jain

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.

---
   fs/btrfs/volumes.c | 2 ++
   1 file changed, 2 insertions(+)
fs/btrfs/volumes.c +0 -0
index 0e8f16c305df..9d64700cc9b6 100644

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

Quoted 5 lines
fs/btrfs/volumes.c +2 -0
@@ -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;

Still the same question.

---

	if (seeding_dev) {
		mutex_unlock(&uuid_mutex);
		up_write(&sb->s_umount);

		if (ret) /* transaction commit */
			return ret;

		ret = btrfs_relocate_sys_chunks(fs_info);
		if (ret < 0)
			btrfs_handle_fs_error(fs_info, ret,
				    "Failed to relocate sys chunks after device initialization. This
can be fixed using the \"btrfs balance\" command.");
		trans = btrfs_attach_transaction(root);
		if (IS_ERR(trans)) {
			if (PTR_ERR(trans) == -ENOENT)
				return 0;
			return PTR_ERR(trans);
		}
Quoted 4 lines

---
In the above btrfs_attch_transaction() error handler, which just returned without going to error_trans tags, did we misseed the s_flag revert thing?

Quoted 3 lines

I think so. Also I think "sb->s_flags |= MS_RDONLY;" can be moved to the 'if' after the error: label, where we already check the 'seeding_dev' condition.

Qu, David,

Thanks. I have fixed that in a new patch as its wasn't about fixing
the BUG_ON().

Further, there is another bug that, in the original code we failed to
undo the btrfs_prepare_sprout() part. I am attempting to fix that, its bit complicated. For now, fix for Qu found bug is sent to the ML.

-Anand

author
#5 lore raw

PATCH v3 2/2 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 and going through the error path.

---
 fs/btrfs/volumes.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)
fs/btrfs/volumes.c +9 -5
index 9d64700cc9b6..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;
@@ -2445,14 +2448,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 +2464,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 +2503,13 @@ int btrfs_init_new_device(struct btrfs_fs_info *fs_info, const char *device_path
 	update_dev_time(device_path);
 	return ret;
 
+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);
 	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
Reviewed-by on v3 2/2
#6 lore raw

Re: [PATCH v3 2/2] btrfs: fix BUG_ON in btrfs_init_new_device()

Quoted 5 lines · Anand Jain

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

Thanks,
Qu

Quoted 62 lines
---
  fs/btrfs/volumes.c | 14 +++++++++-----
  1 file changed, 9 insertions(+), 5 deletions(-)
fs/btrfs/volumes.c +9 -5
index 9d64700cc9b6..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;
@@ -2445,14 +2448,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 +2464,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 +2503,13 @@ int btrfs_init_new_device(struct btrfs_fs_info *fs_info, const char *device_path
  	update_dev_time(device_path);
  	return ret;
 
+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);
  	rcu_string_free(device->name);
-	btrfs_sysfs_rm_device_link(fs_info->fs_devices, device);
  	kfree(device);
  error:
  	blkdev_put(bdev, FMODE_EXCL);