#!/bin/bash
# FS QA Test No. 430
#
# Tests vfs_copy_file_range():
#   - Copy a file
#   - Copy beginning of original to new file
#   - Copy middle of original to a new file
#   - Copy end of original to new file
#   - Copy middle of original to a new file, creating a hole
#-----------------------------------------------------------------------
# Copyright (c) 2017 Netapp, Inc. All rights reserved.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it would be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write the Free Software Foundation,
# Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#-----------------------------------------------------------------------
#

seq=`basename $0`
seqres=$RESULT_DIR/$seq
echo "QA output created by $seq"

here=`pwd`
tmp=/tmp/$$
status=1    # failure is the default!
trap "_cleanup; exit \$status" 0 1 2 3 15

_cleanup()
{
	cd /
	rm -rf $tmp.*
}

# get standard environment
. common/rc
. common/filter

# real QA test starts here
_supported_fs generic
_supported_os Linux

_require_xfs_io_command "copy_range"
_require_test

testdir=$TEST_DIR/test-$seq
rm -rf $testdir
mkdir $testdir
rm -f $seqres.full

echo "Create the original file and then copy"
$XFS_IO_PROG -f -c 'pwrite -S 0x61 0    1000' $testdir/file >> $seqres.full 2>&1
$XFS_IO_PROG -f -c 'pwrite -S 0x62 1000 1000' $testdir/file >> $seqres.full 2>&1
$XFS_IO_PROG -f -c 'pwrite -S 0x63 2000 1000' $testdir/file >> $seqres.full 2>&1
$XFS_IO_PROG -f -c 'pwrite -S 0x64 3000 1000' $testdir/file >> $seqres.full 2>&1
$XFS_IO_PROG -f -c 'pwrite -S 0x65 4000 1000' $testdir/file >> $seqres.full 2>&1
$XFS_IO_PROG -f -c "copy_range $testdir/file" "$testdir/copy"
cmp $testdir/file $testdir/copy
echo "Original md5sums:"
md5sum $testdir/{file,copy} | _filter_test_dir

echo "Copy beginning of original file"
$XFS_IO_PROG -f -c "copy_range -l 1000 $testdir/file" "$testdir/beginning"
cmp -n 1000 $testdir/file $testdir/beginning
echo "md5sums after copying beginning:"
md5sum $testdir/{file,beginning} | _filter_test_dir

echo "Copy middle of original file"
$XFS_IO_PROG -f -c "copy_range -s 1000 -l 3000 $testdir/file" "$testdir/middle"
cmp -n 3000 $testdir/file $testdir/middle 1000
echo "md5sums after copying middle:"
md5sum $testdir/{file,middle} | _filter_test_dir

echo "Copy end of original file"
$XFS_IO_PROG -f -c "copy_range -s 4000 -l 1000 $testdir/file" "$testdir/end"
cmp -n 1000 $testdir/file $testdir/end 4000
echo "md5sums after copying end:"
md5sum $testdir/{file,end} | _filter_test_dir

echo "Copy beyond end of original file"
$XFS_IO_PROG -f -c "copy_range -s 4000 -l 2000 $testdir/file" "$testdir/beyond"
cmp -n 1000 $testdir/file $testdir/end 4000
echo "md5sums after copying beyond:"
md5sum $testdir/{file,beyond} | _filter_test_dir

echo "Copy creates hole in target file"
$XFS_IO_PROG -f -c "copy_range -s 1000 -l 3000 -d 1000 $testdir/file" "$testdir/hole"
cmp -n 3000 $testdir/file $testdir/hole 1000 1000
echo "md5sums after creating hole:"
md5sum $testdir/{file,hole} | _filter_test_dir

#success, all done
status=0
exit
