#!/bin/sh

# This test require specially hacked fsx and fstorture binaries from here:
# https://github.com/thedrgreenthumb/fstools

#
# Test for flex_bg/meta_bg features.
# The large test drive is required to test reading and writing flexible groups
# in case of flex_bg.
#
# Run only fstorture, which will allocate directories in different groups.
# The fsx is does not required in this case.

TEST_DEV="/dev/md0" # The md0 size is not less then 10G.
TEST_MP="/mnt"
BIN_PATH="/root/Tests"

MY_PATH=${PWD}

run_fstorture() # ${1} => TEST_DIR_PATH
{
	if [ ! -f ${BIN_PATH}/fstorture ]; then
		echo "Can not find fstorture binary in ${PWD}"
		exit 1
	fi

	cp ${BIN_PATH}/fstorture ${TEST_MP}/fstorture

	mkdir ${1}/root0 ${1}/root1 
	cd ${1} && ./fstorture root0 root1 4 -c 1000
	if [ "${?}" -ne "0" ]; then exit 1; fi
}

run_fsx() # ${1} => TEST_DIR_PATH
{
	if [ ! -f ${BIN_PATH}/fsx ]; then
		echo "Can not find fsx binary in ${PWD}"
		exit 1
	fi

	cp ${BIN_PATH}/fsx ${1}/fsx

	cd ${1} && ./fsx -S 0 -l 1234567 -N 10000 ./TEST_FILE0
	if [ "${?}" -ne "0" ]; then exit 1; fi
}

run_ea_fsx() # ${1} => TEST_DIR_PATH
{
	if [ ! -f ${BIN_PATH}/fsx ]; then
		echo "Can not find fsx binary in ${PWD}"
		exit 1
	fi

	cp ${BIN_PATH}/fsx ${1}/fsx

	cd ${1} && truncate -s 1M ./TEST_FILE_EA && ./fsx -S 0 -e 989 -N 10000 ./TEST_FILE_EA
	if [ "${?}" -ne "0" ]; then exit 1; fi
}

run_test() # ${1} => block size ${2} => features list
{
	echo "Run test with bs=${1} and features ${2} ..."

	mkfs.ext3 -b ${1} ${2} -F ${TEST_DEV}
	dumpe2fs ${TEST_DEV} | grep 'Filesystem features'

	mount -t ext2fs ${TEST_DEV} ${TEST_MP}

	# run_fsx ${TEST_MP}
	# run_ea_fsx ${TEST_MP}
	run_fstorture ${TEST_MP}

	cd ${MY_PATH}

	sleep 5

	umount ${TEST_DEV}
	if [ "$?" -ne "0" ]
	then 
		echo "Can not unmount..."
		exit 1
	fi

	e2fsck -f -n ${TEST_DEV}
	if [ "$?" -ne "0" ]
	then 
		echo "e2fsck fail"
		exit 1
	fi
}

# main()

echo "Tests for clean ext3..."
FEATURES=""
run_test "1024" "${FEATURES}"
run_test "2048" "${FEATURES}"
run_test "4096" "${FEATURES}"
# run_test "64k"  "${FEATURES}"

echo "Tests for clean ext3 with flex/meta_bg..."
FEATURES="-O metadata_csum"
run_test "1024" "${FEATURES}"
run_test "2048" "${FEATURES}"
run_test "4096" "${FEATURES}"
# run_test "64k"  "${FEATURES}"

FEATURES="-O huge_file -O dir_nlink -O uninit_bg"
echo "Tests for flex/meta_bg with features: ${FEATURES} ..."
run_test "1024" "${FEATURES}"
run_test "2048" "${FEATURES}"
run_test "4096" "${FEATURES}"
# run_test "64k"  "${FEATURES}"

FEATURES="-O huge_file -O dir_nlink -O uninit_bg -O ^resize_inode -O flex_bg"
echo "Tests for flex/meta_bg with features: ${FEATURES} ..."
run_test "1024" "${FEATURES}"
run_test "2048" "${FEATURES}"
run_test "4096" "${FEATURES}"
# run_test "64k"  "${FEATURES}"

FEATURES="-O huge_file -O dir_nlink -O uninit_bg -O ^resize_inode -O meta_bg"
echo "Tests for flex/meta_bg with features: ${FEATURES} ..."
run_test "1024" "${FEATURES}"
run_test "2048" "${FEATURES}"
run_test "4096" "${FEATURES}"
# run_test "64k"  "${FEATURES}"

FEATURES="-O huge_file -O dir_nlink -O uninit_bg -O ^resize_inode -O meta_bg -O flex_bg"
echo "Tests for flex/meta_bg with features: ${FEATURES} ..."
run_test "1024" "${FEATURES}"
run_test "2048" "${FEATURES}"
run_test "4096" "${FEATURES}"
# run_test "64k"  "${FEATURES}"

FEATURES="-O huge_file -O dir_nlink -O metadata_csum"
echo "Tests for flex/meta_bg with features: ${FEATURES} ..."
run_test "1024" "${FEATURES}"
run_test "2048" "${FEATURES}"
run_test "4096" "${FEATURES}"
# run_test "64k"  "${FEATURES}"

FEATURES="-O huge_file -O dir_nlink -O metadata_csum -O ^resize_inode -O meta_bg"
echo "Tests for flex/meta_bg with features: ${FEATURES} ..."
run_test "1024" "${FEATURES}"
run_test "2048" "${FEATURES}"
run_test "4096" "${FEATURES}"
# run_test "64k"  "${FEATURES}"

FEATURES="-O huge_file -O dir_nlink -O metadata_csum -O ^resize_inode -O flex_bg"
echo "Tests for flex/meta_bgm with features: ${FEATURES} ..."
run_test "1024" "${FEATURES}"
run_test "2048" "${FEATURES}"
run_test "4096" "${FEATURES}"
# run_test "64k"  "${FEATURES}"

FEATURES="-O huge_file -O dir_nlink -O metadata_csum -O ^resize_inode -O meta_bg -O flex_bg"
echo "Tests for flex/meta_bg with features: ${FEATURES} ..."
run_test "1024" "${FEATURES}"
run_test "2048" "${FEATURES}"
run_test "4096" "${FEATURES}"
# run_test "64k"  "${FEATURES}"

FEATURES="-O huge_file -O dir_nlink -O uninit_bg -O extents"
echo "Tests for flex/meta_bg with features: ${FEATURES} ..."
run_test "1024" "${FEATURES}"
run_test "2048" "${FEATURES}"
run_test "4096" "${FEATURES}"
# run_test "64k"  "${FEATURES}"

FEATURES="-O huge_file -O dir_nlink -O uninit_bg -O extents -O ^resize_inode -O meta_bg"
echo "Tests for flex/meta_bg with features: ${FEATURES} ..."
run_test "1024" "${FEATURES}"
run_test "2048" "${FEATURES}"
run_test "4096" "${FEATURES}"
# run_test "64k"  "${FEATURES}"

FEATURES="-O huge_file -O dir_nlink -O uninit_bg -O extents -O ^resize_inode -O flex_bg"
echo "Tests for flex/meta_bg with features: ${FEATURES} ..."
run_test "1024" "${FEATURES}"
run_test "2048" "${FEATURES}"
run_test "4096" "${FEATURES}"
# run_test "64k"  "${FEATURES}"

FEATURES="-O huge_file -O dir_nlink -O uninit_bg -O extents -O ^resize_inode -O flex_bg -O meta_bg"
echo "Tests for flex/meta_bg with features: ${FEATURES} ..."
run_test "1024" "${FEATURES}"
run_test "2048" "${FEATURES}"
run_test "4096" "${FEATURES}"
# run_test "64k"  "${FEATURES}"

FEATURES="-O huge_file -O dir_nlink -O uninit_bg -O extents -O metadata_csum"
echo "Tests for flex/meta_bg with features: ${FEATURES} ..."
run_test "1024" "${FEATURES}"
run_test "2048" "${FEATURES}"
run_test "4096" "${FEATURES}"
# run_test "64k"  "${FEATURES}"

FEATURES="-O huge_file -O dir_nlink -O extents -O 64bit"
echo "Tests for flex/meta_bg with features: ${FEATURES} ..."
run_test "1024" "${FEATURES}"
run_test "2048" "${FEATURES}"
run_test "4096" "${FEATURES}"
# run_test "64k"  "${FEATURES}"

FEATURES="-O huge_file -O dir_nlink -O extents -O 64bit -O metadata_csum"
echo "Tests for flex/meta_bg with features: ${FEATURES} ..."
run_test "1024" "${FEATURES}"
run_test "2048" "${FEATURES}"
run_test "4096" "${FEATURES}"
# run_test "64k"  "${FEATURES}"

FEATURES="-O huge_file -O dir_nlink -O extents -O 64bit -O metadata_csum -O ^resize_inode -O flex_bg"
echo "Tests for flex/meta_bg with features: ${FEATURES} ..."
run_test "1024" "${FEATURES}"
run_test "2048" "${FEATURES}"
run_test "4096" "${FEATURES}"
# run_test "64k"  "${FEATURES}"

FEATURES="-O huge_file -O dir_nlink -O extents -O 64bit -O metadata_csum -O ^resize_inode -O meta_bg"
echo "Tests for flex/meta_bg with features: ${FEATURES} ..."
run_test "1024" "${FEATURES}"
run_test "2048" "${FEATURES}"
run_test "4096" "${FEATURES}"
# run_test "64k"  "${FEATURES}"

FEATURES="-O huge_file -O dir_nlink -O extents -O 64bit -O metadata_csum -O ^resize_inode -O flex_bg -O meta_bg"
echo "Tests for flex/meta_bg with features: ${FEATURES} ..."
run_test "1024" "${FEATURES}"
run_test "2048" "${FEATURES}"
run_test "4096" "${FEATURES}"
# run_test "64k"  "${FEATURES}"


echo PASSED

