#! /bin/bash
# SPDX-License-Identifier: GPL-2.0
# Copyright (c) 2020 IBM Corporation. All Rights Reserved.
#
# FS QA Test No. ext4/046
#
# Test writes to falloc file with filesize > 4GB and make sure to verify
# the file checksum both before and after mount.
# This test is to check whether unwritten extents gets properly converted
# to written extent on a filesystem with bs < ps with dioread_nolock.
#
. ./common/preamble
_begin_fstest auto prealloc quick

# Import common functions.
. ./common/filter

_require_check_dmesg
_exclude_fs ext2
_exclude_fs ext3
_require_scratch
_require_xfs_io_command "falloc"
_require_scratch_size $((6 * 1024 * 1024)) #kB

_scratch_mkfs >> $seqres.full 2>&1
if ! _try_scratch_mount "-o dioread_nolock" >> $seqres.full 2>&1; then
	err_str="can't mount with dioread_nolock if block size != PAGE_SIZE"
	_check_dmesg_for ${err_str}
	if [ $? -eq 0 ]; then
		_notrun "mount failed, ext4 doesn't support bs < ps with dioread_nolock"
	else
		_fail "mount failed with dioread_nolock"
	fi
fi

# Get blksz
blksz=$(_get_file_block_size $SCRATCH_MNT)

testfile=$SCRATCH_MNT/testfile-$seq

# Fallocate testfile with size > 4G
fsize=$((5 * 1024 * 1024 * 1024))
$XFS_IO_PROG -f -c "falloc 0 $fsize" $testfile >> $seqres.full 2>&1

# First write at offset < 4G (at few alternative blks)
off=$((3 * 1024 * 1024 * 1024))
for i in 1 2 3 4; do
	$XFS_IO_PROG -f \
		-c "pwrite $off $blksz" \
		$testfile >> $seqres.full 2>&1
	off=$(($off + (2*$blksz)))
done

# Then write at offset > 4G (at few alternative blks) to check
# any 32bit overflow case in map.m_lblk
off=$((4 * 1024 * 1024 * 1024))
for i in 1 2 3 4; do
	$XFS_IO_PROG -f \
		-c "pwrite $off $blksz" \
		$testfile >> $seqres.full 2>&1
	off=$(($off + (2*$blksz)))
done

# ==== Pre-Remount ===
md5_pre=`md5sum $testfile | cut -d' ' -f1`
echo "Pre-Remount md5sum of $testfile = $md5_pre" >> $seqres.full

_scratch_cycle_mount

# ==== Post-Remount ===
md5_post=`md5sum $testfile | cut -d' ' -f1`
echo "Post-Remount md5sum of $testfile = $md5_post" >> $seqres.full
test $md5_pre != $md5_post && echo "md5sum mismatch"

# success, all done
echo "Silence is golden"
status=0
exit
