#! /bin/bash
# SPDX-License-Identifier: GPL-2.0-or-later
# Copyright (c) 2022 Oracle.  All Rights Reserved.
#
# FS QA Test No. 714
#
# Test exchangerange between ranges of two different files, when one of the files
# is shared.

. ./common/preamble
_begin_fstest auto quick fiexchange

# Override the default cleanup function.
_cleanup()
{
	cd /
	rm -r -f $tmp.* $dir
}

# Import common functions.
. ./common/filter
. ./common/reflink

_require_xfs_io_command exchangerange
_require_xfs_io_command "falloc"
_require_test_reflink

filesnap() {
	echo "$1"
	if [ "$2" != "$3" ]; then
		md5sum $2 $3 | _filter_test_dir
	else
		md5sum $2 | _filter_test_dir
	fi
}

test_exchangerange_once() {
	filesnap "$1: before exchangerange" $dir/$3 $dir/$4
	$XFS_IO_PROG -c "exchangerange $2 $dir/$3" $dir/$4
	filesnap "$1: after exchangerange" $dir/$3 $dir/$4
	_test_cycle_mount
	filesnap "$1: after cycling mount" $dir/$3 $dir/$4
	echo
}

test_exchangerange_two() {
	# exchangerange the same range of two files
	test_exchangerange_once "$*: samerange" \
		"-s $((blksz * 3)) -d $((blksz * 3)) -l $((blksz * 5))" b a

	# exchangerange different ranges of two files
	test_exchangerange_once "$*: diffrange" \
		"-s $((blksz * 37)) -d $((blksz * 47)) -l $((blksz * 7))" b a

	# exchangerange overlapping ranges of two files
	test_exchangerange_once "$*: overlap" \
		"-s $((blksz * 17)) -d $((blksz * 23)) -l $((blksz * 7))" b a

	# Now let's overwrite a entirely to make sure COW works
	echo "overwrite A and B entirely"
	md5sum $dir/sharea | _filter_test_dir
	$XFS_IO_PROG -c "pwrite -S 0x60 0 $((blksz * nrblks))" $dir/a >> $seqres.full
	$XFS_IO_PROG -c "pwrite -S 0x60 0 $((blksz * nrblks))" $dir/b >> $seqres.full
	md5sum $dir/sharea | _filter_test_dir
	_test_cycle_mount
	md5sum $dir/sharea | _filter_test_dir
	echo
}

dir=$TEST_DIR/test-$seq
mkdir -p $dir
blksz=65536
_require_congruent_file_oplen $TEST_DIR $blksz
nrblks=57

# Set up some simple files for a first test.
rm -f $dir/a $dir/b $dir/sharea
_pwrite_byte 0x58 0 $((blksz * nrblks)) $dir/a >> $seqres.full
_pwrite_byte 0x59 0 $((blksz * nrblks)) $dir/b >> $seqres.full
_cp_reflink $dir/a $dir/sharea
test_exchangerange_two "simple"

# Set up some crazy rainbow files
rm -f $dir/a $dir/b $dir/sharea
_weave_file_rainbow $blksz $nrblks $dir/a >> $seqres.full
_weave_file_rainbow $blksz $nrblks $dir/b >> $seqres.full
_cp_reflink $dir/a $dir/sharea
test_exchangerange_two "rainbow"

# Now set up a simple file for testing within the same file
rm -f $dir/c $dir/sharec
$XFS_IO_PROG -f -c "pwrite -S 0x58 0 $((blksz * nrblks))" \
	-c "pwrite -S 0x59 $((blksz * nrblks)) $((blksz * nrblks))" \
	$dir/c >> $seqres.full
_cp_reflink $dir/c $dir/sharec

# exchangerange the same offset into the 'X' and 'Y' regions of the file
test_exchangerange_once "single: sameXandY" \
	"-s $((blksz * 3)) -d $((blksz * (nrblks + 3))) -l $((blksz * 5))" c c

# exchangerange the same offset into the 'X' and 'Y' regions of the file
test_exchangerange_once "single: overlap" \
	"-s $((blksz * 13)) -d $((blksz * 17)) -l $((blksz * 5))" c c

# Now let's overwrite a entirely to make sure COW works
echo "overwrite C entirely"
md5sum $dir/sharec | _filter_test_dir
$XFS_IO_PROG -c "pwrite -S 0x60 0 $((blksz * nrblks))" $dir/c >> $seqres.full
md5sum $dir/sharec | _filter_test_dir
_test_cycle_mount
md5sum $dir/sharec | _filter_test_dir

# success, all done
status=0
exit
