[v2] btrfs-progs: Don't BUG_ON() when write_dev_supers() fails
diff mbox series

Message ID 20190411095742.13482-1-wqu@suse.com
State New
Headers show
Series
  • [v2] btrfs-progs: Don't BUG_ON() when write_dev_supers() fails
Related show

Commit Message

WenRuo Qu April 11, 2019, 9:57 a.m. UTC
[BUG]
Since commit "btrfs-progs: disk-io: Flush to ensure super block write is
FUA" mkfs-tests/017 will fail like:

  ====== RUN MUSTFAIL /home/adam/btrfs-progs/mkfs.btrfs -K -f /dev/mapper/btrfs-progs-thin-vol
  ERROR: failed to write super block for devid 1: flush error: Input/output error
  disk-io.c:1810: write_all_supers: BUG_ON `ret` triggered, value -5
  /home/adam/btrfs-progs/mkfs.btrfs(+0x1e5c1)[0x557a2c83e5c1]
  /home/adam/btrfs-progs/mkfs.btrfs(+0x1e65f)[0x557a2c83e65f]
  /home/adam/btrfs-progs/mkfs.btrfs(write_all_supers+0x1ce)[0x557a2c843a8a]
  /home/adam/btrfs-progs/mkfs.btrfs(write_ctree_super+0x12d)[0x557a2c843be2]
  /home/adam/btrfs-progs/mkfs.btrfs(btrfs_commit_transaction+0x250)[0x557a2c887c56]
  /home/adam/btrfs-progs/mkfs.btrfs(+0xc0b1)[0x557a2c82c0b1]
  /home/adam/btrfs-progs/mkfs.btrfs(main+0x1049)[0x557a2c82e929]
  /usr/lib/libc.so.6(__libc_start_main+0xf3)[0x7f6689e99223]
  /home/adam/btrfs-progs/mkfs.btrfs(_start+0x2e)[0x557a2c82b86e]
  failed (expected): /home/adam/btrfs-progs/mkfs.btrfs -K -f /dev/mapper/btrfs-progs-thin-vol

[CAUSE]
Just one BUG_ON() in write_all_supers().

[FIX]
Just remove the BUG_ON(). Callers of write_all_supers() are already
checking the return value.

Also since write_all_supers() can return error, make write_ctree_super()
callers, btrfs_commit_transaction() and close_ctree_fs_info() to
handle the error correctly.

Signed-off-by: Qu Wenruo <wqu@suse.com>
---
Changelog:
v2:
- Make write_ctree_super() callers to handle the error properly
---
 disk-io.c     | 16 ++++++++++------
 transaction.c |  2 +-
 2 files changed, 11 insertions(+), 7 deletions(-)

Comments

Nikolay Borisov April 11, 2019, 10:53 a.m. UTC | #1
On 11.04.19 г. 12:57 ч., Qu Wenruo wrote:
> [BUG]
> Since commit "btrfs-progs: disk-io: Flush to ensure super block write is
> FUA" mkfs-tests/017 will fail like:
> 
>   ====== RUN MUSTFAIL /home/adam/btrfs-progs/mkfs.btrfs -K -f /dev/mapper/btrfs-progs-thin-vol
>   ERROR: failed to write super block for devid 1: flush error: Input/output error
>   disk-io.c:1810: write_all_supers: BUG_ON `ret` triggered, value -5
>   /home/adam/btrfs-progs/mkfs.btrfs(+0x1e5c1)[0x557a2c83e5c1]
>   /home/adam/btrfs-progs/mkfs.btrfs(+0x1e65f)[0x557a2c83e65f]
>   /home/adam/btrfs-progs/mkfs.btrfs(write_all_supers+0x1ce)[0x557a2c843a8a]
>   /home/adam/btrfs-progs/mkfs.btrfs(write_ctree_super+0x12d)[0x557a2c843be2]
>   /home/adam/btrfs-progs/mkfs.btrfs(btrfs_commit_transaction+0x250)[0x557a2c887c56]
>   /home/adam/btrfs-progs/mkfs.btrfs(+0xc0b1)[0x557a2c82c0b1]
>   /home/adam/btrfs-progs/mkfs.btrfs(main+0x1049)[0x557a2c82e929]
>   /usr/lib/libc.so.6(__libc_start_main+0xf3)[0x7f6689e99223]
>   /home/adam/btrfs-progs/mkfs.btrfs(_start+0x2e)[0x557a2c82b86e]
>   failed (expected): /home/adam/btrfs-progs/mkfs.btrfs -K -f /dev/mapper/btrfs-progs-thin-vol
> 
> [CAUSE]
> Just one BUG_ON() in write_all_supers().
> 
> [FIX]
> Just remove the BUG_ON(). Callers of write_all_supers() are already
> checking the return value.
> 
> Also since write_all_supers() can return error, make write_ctree_super()
> callers, btrfs_commit_transaction() and close_ctree_fs_info() to
> handle the error correctly.
> 
> Signed-off-by: Qu Wenruo <wqu@suse.com>
> ---
> Changelog:
> v2:
> - Make write_ctree_super() callers to handle the error properly
> ---
>  disk-io.c     | 16 ++++++++++------
>  transaction.c |  2 +-
>  2 files changed, 11 insertions(+), 7 deletions(-)
> 
> diff --git a/disk-io.c b/disk-io.c
> index 10550151c5bb..a3aa8b29f2a3 100644
> --- a/disk-io.c
> +++ b/disk-io.c
> @@ -1807,7 +1807,8 @@ int write_all_supers(struct btrfs_fs_info *fs_info)
>  		btrfs_set_super_flags(sb, flags | BTRFS_HEADER_FLAG_WRITTEN);
>  
>  		ret = write_dev_supers(fs_info, sb, dev);
> -		BUG_ON(ret);
> +		if (ret < 0)
> +			return ret;
>  	}
>  	return 0;
>  }
> @@ -1863,17 +1864,20 @@ int close_ctree_fs_info(struct btrfs_fs_info *fs_info)
>  		BUG_ON(ret);
>  		ret = __commit_transaction(trans, root);
>  		BUG_ON(ret);
> -		write_ctree_super(trans);
> +		ret = write_ctree_super(trans);
>  		kfree(trans);

if (ret) {
     err = ret;
     goto skip_commit
}

>  	}
>  
>  	if (fs_info->finalize_on_close) {
>  		btrfs_set_super_magic(fs_info->super_copy, BTRFS_MAGIC);
>  		root->fs_info->finalize_on_close = 0;
> -		ret = write_all_supers(fs_info);
> -		if (ret)
> -			fprintf(stderr,
> -				"failed to write new super block err %d\n", ret);
> +		if (!ret) {
> +			ret = write_all_supers(fs_info);
> +			if (ret)
> +				fprintf(stderr,
> +				"failed to write new super block err %d\n",
> +					ret);
> +		}

And you won't have to touch this code at all.

>  	}
>  
>  skip_commit:
> diff --git a/transaction.c b/transaction.c
> index e756db332fec..3a63988b0969 100644
> --- a/transaction.c
> +++ b/transaction.c
> @@ -197,7 +197,7 @@ commit_tree:
>  	__commit_transaction(trans, root);
>  	if (ret < 0)
>  		goto out;
> -	write_ctree_super(trans);
> +	ret = write_ctree_super(trans);
>  	btrfs_finish_extent_commit(trans, fs_info->extent_root,
>  			           &fs_info->pinned_extents);
>  	kfree(trans);
>

Patch
diff mbox series

diff --git a/disk-io.c b/disk-io.c
index 10550151c5bb..a3aa8b29f2a3 100644
--- a/disk-io.c
+++ b/disk-io.c
@@ -1807,7 +1807,8 @@  int write_all_supers(struct btrfs_fs_info *fs_info)
 		btrfs_set_super_flags(sb, flags | BTRFS_HEADER_FLAG_WRITTEN);
 
 		ret = write_dev_supers(fs_info, sb, dev);
-		BUG_ON(ret);
+		if (ret < 0)
+			return ret;
 	}
 	return 0;
 }
@@ -1863,17 +1864,20 @@  int close_ctree_fs_info(struct btrfs_fs_info *fs_info)
 		BUG_ON(ret);
 		ret = __commit_transaction(trans, root);
 		BUG_ON(ret);
-		write_ctree_super(trans);
+		ret = write_ctree_super(trans);
 		kfree(trans);
 	}
 
 	if (fs_info->finalize_on_close) {
 		btrfs_set_super_magic(fs_info->super_copy, BTRFS_MAGIC);
 		root->fs_info->finalize_on_close = 0;
-		ret = write_all_supers(fs_info);
-		if (ret)
-			fprintf(stderr,
-				"failed to write new super block err %d\n", ret);
+		if (!ret) {
+			ret = write_all_supers(fs_info);
+			if (ret)
+				fprintf(stderr,
+				"failed to write new super block err %d\n",
+					ret);
+		}
 	}
 
 skip_commit:
diff --git a/transaction.c b/transaction.c
index e756db332fec..3a63988b0969 100644
--- a/transaction.c
+++ b/transaction.c
@@ -197,7 +197,7 @@  commit_tree:
 	__commit_transaction(trans, root);
 	if (ret < 0)
 		goto out;
-	write_ctree_super(trans);
+	ret = write_ctree_super(trans);
 	btrfs_finish_extent_commit(trans, fs_info->extent_root,
 			           &fs_info->pinned_extents);
 	kfree(trans);