[2/2] btrfs-progs: tests/convert: Add test case to make sure we won't allocate dev extents beyond device boundary
diff mbox series

Message ID 20200624115527.855816-2-wqu@suse.com
State New
Headers show
Series
  • [1/2] btrfs-progs: convert: Ensure the data chunks size never exceed device size
Related show

Commit Message

Qu Wenruo June 24, 2020, 11:55 a.m. UTC
Add a test case to check if the converted fs has device extent beyond
boundary.

The disk layout of source ext4 fs needs some extents to make them
allocated at the very end of the fs.
The script is from the original reporter.

Also, since the existing convert tests always uses 512M as device size,
which is not suitable for this test case, make it to grab the existing
device size to co-operate with this test case.

Reported-by: Jiachen YANG <farseerfc@gmail.com>
Signed-off-by: Qu Wenruo <wqu@suse.com>
---
 tests/common.convert                         | 14 ++++++++-
 tests/convert-tests/017-fs-near-full/test.sh | 30 ++++++++++++++++++++
 2 files changed, 43 insertions(+), 1 deletion(-)
 create mode 100755 tests/convert-tests/017-fs-near-full/test.sh

Comments

David Sterba June 24, 2020, 3:49 p.m. UTC | #1
On Wed, Jun 24, 2020 at 07:55:27PM +0800, Qu Wenruo wrote:
> Add a test case to check if the converted fs has device extent beyond
> boundary.
> 
> The disk layout of source ext4 fs needs some extents to make them
> allocated at the very end of the fs.
> The script is from the original reporter.
> 
> Also, since the existing convert tests always uses 512M as device size,
> which is not suitable for this test case, make it to grab the existing
> device size to co-operate with this test case.
> 
> Reported-by: Jiachen YANG <farseerfc@gmail.com>
> Signed-off-by: Qu Wenruo <wqu@suse.com>
> ---
>  tests/common.convert                         | 14 ++++++++-
>  tests/convert-tests/017-fs-near-full/test.sh | 30 ++++++++++++++++++++
>  2 files changed, 43 insertions(+), 1 deletion(-)
>  create mode 100755 tests/convert-tests/017-fs-near-full/test.sh
> 
> diff --git a/tests/common.convert b/tests/common.convert
> index f24ceb0d6a64..0c918387758d 100644
> --- a/tests/common.convert
> +++ b/tests/common.convert
> @@ -53,6 +53,16 @@ convert_test_preamble() {
>  	echo "creating test image with: $@" >> "$RESULTS"
>  }
>  
> +get_test_file_size() {
> +	local path="$1"
> +	local ret
> +
> +	ret=$(ls -l "$path" | cut -f5 -d\ )

Parsing ls output is quite fragile, and we have 'stat --format=%s'.
David Sterba June 24, 2020, 3:51 p.m. UTC | #2
On Wed, Jun 24, 2020 at 07:55:27PM +0800, Qu Wenruo wrote:
> Add a test case to check if the converted fs has device extent beyond
> boundary.
> 
> The disk layout of source ext4 fs needs some extents to make them
> allocated at the very end of the fs.
> The script is from the original reporter.
> 
> Also, since the existing convert tests always uses 512M as device size,
> which is not suitable for this test case, make it to grab the existing
> device size to co-operate with this test case.
> 
> Reported-by: Jiachen YANG <farseerfc@gmail.com>
> Signed-off-by: Qu Wenruo <wqu@suse.com>
> ---
>  tests/common.convert                         | 14 ++++++++-
>  tests/convert-tests/017-fs-near-full/test.sh | 30 ++++++++++++++++++++
>  2 files changed, 43 insertions(+), 1 deletion(-)
>  create mode 100755 tests/convert-tests/017-fs-near-full/test.sh
> 
> diff --git a/tests/common.convert b/tests/common.convert
> index f24ceb0d6a64..0c918387758d 100644
> --- a/tests/common.convert
> +++ b/tests/common.convert
> @@ -53,6 +53,16 @@ convert_test_preamble() {
>  	echo "creating test image with: $@" >> "$RESULTS"
>  }
>  
> +get_test_file_size() {
> +	local path="$1"
> +	local ret
> +
> +	ret=$(ls -l "$path" | cut -f5 -d\ )
> +	if [ -z $ret ]; then
> +		ret=512M
> +	fi
> +	echo $ret
> +}
>  #  prepare TEST_DEV before conversion, create filesystem and mount it, image
>  #  size is 512MB
>  #  $1: type of the filesystem
> @@ -61,14 +71,16 @@ convert_test_prep_fs() {
>  	local fstype
>  	local force
>  	local mountopts
> +	local oldsize
>  
>  	fstype="$1"
>  	shift
> +	oldsize=$(get_test_file_size "$TEST_DEV")

The helper should be here, with a comment why the return size needs to
be 512M in some cases.

Patch
diff mbox series

diff --git a/tests/common.convert b/tests/common.convert
index f24ceb0d6a64..0c918387758d 100644
--- a/tests/common.convert
+++ b/tests/common.convert
@@ -53,6 +53,16 @@  convert_test_preamble() {
 	echo "creating test image with: $@" >> "$RESULTS"
 }
 
+get_test_file_size() {
+	local path="$1"
+	local ret
+
+	ret=$(ls -l "$path" | cut -f5 -d\ )
+	if [ -z $ret ]; then
+		ret=512M
+	fi
+	echo $ret
+}
 #  prepare TEST_DEV before conversion, create filesystem and mount it, image
 #  size is 512MB
 #  $1: type of the filesystem
@@ -61,14 +71,16 @@  convert_test_prep_fs() {
 	local fstype
 	local force
 	local mountopts
+	local oldsize
 
 	fstype="$1"
 	shift
+	oldsize=$(get_test_file_size "$TEST_DEV")
 	# TEST_DEV not removed as the file might have special permissions, eg.
 	# when test image is on NFS and would not be writable for root
 	run_check truncate -s 0 "$TEST_DEV"
 	# 256MB is the smallest acceptable btrfs image.
-	run_check truncate -s 512M "$TEST_DEV"
+	run_check truncate -s $oldsize "$TEST_DEV"
 	force=
 	mountopts=
 	case "$fstype" in
diff --git a/tests/convert-tests/017-fs-near-full/test.sh b/tests/convert-tests/017-fs-near-full/test.sh
new file mode 100755
index 000000000000..9459729ee87f
--- /dev/null
+++ b/tests/convert-tests/017-fs-near-full/test.sh
@@ -0,0 +1,30 @@ 
+#!/bin/bash
+# Check if btrfs-convert creates fs with dev extents boundary device boundary
+
+source "$TEST_TOP/common"
+source "$TEST_TOP/common.convert"
+
+setup_root_helper
+prepare_test_dev 1G
+check_prereq btrfs-convert
+check_global_prereq mke2fs
+check_global_prereq fallocate
+
+convert_test_prep_fs ext4 mke2fs -t ext4 -b 4096
+
+# Use up 800MiB first
+for i in $(seq 1 4); do
+	run_check $SUDO_HELPER fallocate -l 200M "$TEST_MNT/file$i"
+done
+
+# Then add 5MiB for above files. These 5 MiB will be allocated near the very
+# end of the fs, to confuse btrfs-convert
+for i in $(seq 1 4); do
+	run_check $SUDO_HELPER fallocate -l 205M "$TEST_MNT/file$i"
+done
+
+run_check_umount_test_dev
+
+# convert_test_do_convert() will call btrfs check, which should expose any
+# invalid inline extent with too large size
+convert_test_do_convert