#! /bin/bash
# FS QA Test No. btrfs/046
#
# Regression test for the btrfs incremental send feature, where the kernel
# would incorrectly consider a range of a file as a hole and send a stream
# of 0 bytes to the destination (send stream) that would overwrite the
# corresponding file region.
#
# This issue is fixed by the following linux kernel btrfs patch:
#
#   Btrfs: send, fix data corruption due to incorrect hole detection
#
#-----------------------------------------------------------------------
# Copyright (c) 2014 Filipe Manana.  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"

tmp=`mktemp -d`
status=1	# failure is the default!
trap "_cleanup; exit \$status" 0 1 2 3 15

send_files_dir=$TEST_DIR/btrfs-test-$seq

_cleanup()
{
	rm -fr $send_files_dir
	rm -fr $tmp
}

# get standard environment, filters and checks
. ./common/rc
. ./common/filter

# real QA test starts here
_supported_fs btrfs
_supported_os Linux
_require_test
_require_scratch
_require_fssum

rm -f $seqres.full
rm -fr $send_files_dir
mkdir $send_files_dir

_scratch_mkfs >/dev/null 2>&1
_scratch_mount

$XFS_IO_PROG -f -c "falloc -k 0 268435456" $SCRATCH_MNT/foo

_run_btrfs_util_prog subvolume snapshot -r $SCRATCH_MNT $SCRATCH_MNT/mysnap0

$XFS_IO_PROG -c "pwrite -S 0x01 -b 9216 16190218 9216" $SCRATCH_MNT/foo \
	| _filter_xfs_io
$XFS_IO_PROG -c "pwrite -S 0x02 -b 1121 198720104 1121" $SCRATCH_MNT/foo \
	| _filter_xfs_io
$XFS_IO_PROG -c "pwrite -S 0x05 -b 9216 107887439 9216" $SCRATCH_MNT/foo \
	| _filter_xfs_io
$XFS_IO_PROG -c "pwrite -S 0x06 -b 9216 225520207 9216" $SCRATCH_MNT/foo \
	| _filter_xfs_io
$XFS_IO_PROG -c "pwrite -S 0x07 -b 67584 102138300 67584" $SCRATCH_MNT/foo \
	| _filter_xfs_io
$XFS_IO_PROG -c "pwrite -S 0x08 -b 7000 94897484 7000" $SCRATCH_MNT/foo \
	| _filter_xfs_io
$XFS_IO_PROG -c "pwrite -S 0x09 -b 113664 245083212 113664" $SCRATCH_MNT/foo \
	| _filter_xfs_io
$XFS_IO_PROG -c "pwrite -S 0x10 -b 123 17937788 123" $SCRATCH_MNT/foo \
	| _filter_xfs_io
$XFS_IO_PROG -c "pwrite -S 0x11 -b 39936 229573311 39936" $SCRATCH_MNT/foo \
	| _filter_xfs_io
$XFS_IO_PROG -c "pwrite -S 0x12 -b 67584 174792222 67584" $SCRATCH_MNT/foo \
	| _filter_xfs_io
$XFS_IO_PROG -c "pwrite -S 0x13 -b 9216 249253213 9216" $SCRATCH_MNT/foo \
	| _filter_xfs_io
$XFS_IO_PROG -c "pwrite -S 0x16 -b 67584 150046083 67584" $SCRATCH_MNT/foo \
	| _filter_xfs_io
$XFS_IO_PROG -c "pwrite -S 0x17 -b 39936 118246040 39936" $SCRATCH_MNT/foo \
	| _filter_xfs_io
$XFS_IO_PROG -c "pwrite -S 0x18 -b 67584 215965442 67584" $SCRATCH_MNT/foo \
	| _filter_xfs_io
$XFS_IO_PROG -c "pwrite -S 0x19 -b 33792 97096725 33792" $SCRATCH_MNT/foo \
	| _filter_xfs_io
$XFS_IO_PROG -c "pwrite -S 0x20 -b 125952 166300596 125952" $SCRATCH_MNT/foo \
	| _filter_xfs_io
$XFS_IO_PROG -c "pwrite -S 0x21 -b 123 1078957 123" $SCRATCH_MNT/foo \
	| _filter_xfs_io
$XFS_IO_PROG -c "pwrite -S 0x25 -b 9216 212044492 9216" $SCRATCH_MNT/foo \
	| _filter_xfs_io
$XFS_IO_PROG -c "pwrite -S 0x26 -b 7000 265037146 7000" $SCRATCH_MNT/foo \
	| _filter_xfs_io
$XFS_IO_PROG -c "pwrite -S 0x27 -b 42757 215922685 42757" $SCRATCH_MNT/foo \
	| _filter_xfs_io
$XFS_IO_PROG -c "pwrite -S 0x28 -b 7000 69865411 7000" $SCRATCH_MNT/foo \
	| _filter_xfs_io
$XFS_IO_PROG -c "pwrite -S 0x29 -b 67584 67948958 67584" $SCRATCH_MNT/foo \
	| _filter_xfs_io
$XFS_IO_PROG -c "pwrite -S 0x30 -b 39936 266967019 39936" $SCRATCH_MNT/foo \
	| _filter_xfs_io
$XFS_IO_PROG -c "pwrite -S 0x31 -b 1121 19582453 1121" $SCRATCH_MNT/foo \
	| _filter_xfs_io
$XFS_IO_PROG -c "pwrite -S 0x32 -b 17408 257710255 17408" $SCRATCH_MNT/foo \
	| _filter_xfs_io
$XFS_IO_PROG -c "pwrite -S 0x33 -b 39936 3895518 39936" $SCRATCH_MNT/foo \
	| _filter_xfs_io
$XFS_IO_PROG -c "pwrite -S 0x34 -b 125952 12045847 125952" $SCRATCH_MNT/foo \
	| _filter_xfs_io
$XFS_IO_PROG -c "pwrite -S 0x35 -b 17408 19156379 17408" $SCRATCH_MNT/foo \
	| _filter_xfs_io
$XFS_IO_PROG -c "pwrite -S 0x36 -b 39936 50160066 39936" $SCRATCH_MNT/foo \
	| _filter_xfs_io
$XFS_IO_PROG -c "pwrite -S 0x37 -b 113664 9549793 113664" $SCRATCH_MNT/foo \
	| _filter_xfs_io
$XFS_IO_PROG -c "pwrite -S 0x38 -b 105472 94391506 105472" $SCRATCH_MNT/foo \
	| _filter_xfs_io
$XFS_IO_PROG -c "pwrite -S 0x39 -b 23552 143632863 23552" $SCRATCH_MNT/foo \
	| _filter_xfs_io
$XFS_IO_PROG -c "pwrite -S 0x40 -b 39936 241283845 39936" $SCRATCH_MNT/foo \
	| _filter_xfs_io
$XFS_IO_PROG -c "pwrite -S 0x41 -b 113664 199937606 113664" $SCRATCH_MNT/foo \
	| _filter_xfs_io
$XFS_IO_PROG -c "pwrite -S 0x42 -b 67584 67380093 67584" $SCRATCH_MNT/foo \
	| _filter_xfs_io
$XFS_IO_PROG -c "pwrite -S 0x43 -b 67584 26793129 67584" $SCRATCH_MNT/foo \
	| _filter_xfs_io
$XFS_IO_PROG -c "pwrite -S 0x44 -b 39936 14421913 39936" $SCRATCH_MNT/foo \
	| _filter_xfs_io
$XFS_IO_PROG -c "pwrite -S 0x45 -b 123 253097405 123" $SCRATCH_MNT/foo \
	| _filter_xfs_io
$XFS_IO_PROG -c "pwrite -S 0x46 -b 1121 128233424 1121" $SCRATCH_MNT/foo \
	| _filter_xfs_io
$XFS_IO_PROG -c "pwrite -S 0x47 -b 105472 91577959 105472" $SCRATCH_MNT/foo \
	| _filter_xfs_io
$XFS_IO_PROG -c "pwrite -S 0x48 -b 1121 7245381 1121" $SCRATCH_MNT/foo \
	| _filter_xfs_io
$XFS_IO_PROG -c "pwrite -S 0x49 -b 113664 182414694 113664" $SCRATCH_MNT/foo \
	| _filter_xfs_io
$XFS_IO_PROG -c "pwrite -S 0x50 -b 9216 32750608 9216" $SCRATCH_MNT/foo \
	| _filter_xfs_io
$XFS_IO_PROG -c "pwrite -S 0x51 -b 67584 266546049 67584" $SCRATCH_MNT/foo \
	| _filter_xfs_io
$XFS_IO_PROG -c "pwrite -S 0x52 -b 67584 87969398 67584" $SCRATCH_MNT/foo \
	| _filter_xfs_io
$XFS_IO_PROG -c "pwrite -S 0x53 -b 9216 260848797 9216" $SCRATCH_MNT/foo \
	| _filter_xfs_io
$XFS_IO_PROG -c "pwrite -S 0x54 -b 39936 119461243 39936" $SCRATCH_MNT/foo \
	| _filter_xfs_io
$XFS_IO_PROG -c "pwrite -S 0x55 -b 7000 200178693 7000" $SCRATCH_MNT/foo \
	| _filter_xfs_io
$XFS_IO_PROG -c "pwrite -S 0x56 -b 9216 243316029 9216" $SCRATCH_MNT/foo \
	| _filter_xfs_io
$XFS_IO_PROG -c "pwrite -S 0x57 -b 7000 209658229 7000" $SCRATCH_MNT/foo \
	| _filter_xfs_io
$XFS_IO_PROG -c "pwrite -S 0x58 -b 101376 179745192 101376" $SCRATCH_MNT/foo \
	| _filter_xfs_io
$XFS_IO_PROG -c "pwrite -S 0x59 -b 9216 64012300 9216" $SCRATCH_MNT/foo \
	| _filter_xfs_io
$XFS_IO_PROG -c "pwrite -S 0x60 -b 125952 181705139 125952" $SCRATCH_MNT/foo \
	| _filter_xfs_io
$XFS_IO_PROG -c "pwrite -S 0x61 -b 23552 235737348 23552" $SCRATCH_MNT/foo \
	| _filter_xfs_io
$XFS_IO_PROG -c "pwrite -S 0x62 -b 113664 106021355 113664" $SCRATCH_MNT/foo \
	| _filter_xfs_io
$XFS_IO_PROG -c "pwrite -S 0x63 -b 67584 135753552 67584" $SCRATCH_MNT/foo \
	| _filter_xfs_io
$XFS_IO_PROG -c "pwrite -S 0x64 -b 23552 95730888 23552" $SCRATCH_MNT/foo \
	| _filter_xfs_io
$XFS_IO_PROG -c "pwrite -S 0x65 -b 11 17311415 11" $SCRATCH_MNT/foo \
	| _filter_xfs_io
$XFS_IO_PROG -c "pwrite -S 0x66 -b 33792 120695553 33792" $SCRATCH_MNT/foo \
	| _filter_xfs_io
$XFS_IO_PROG -c "pwrite -S 0x67 -b 9216 17164631 9216" $SCRATCH_MNT/foo \
	| _filter_xfs_io
$XFS_IO_PROG -c "pwrite -S 0x68 -b 9216 136065853 9216" $SCRATCH_MNT/foo \
	| _filter_xfs_io
$XFS_IO_PROG -c "pwrite -S 0x69 -b 67584 37752198 67584" $SCRATCH_MNT/foo \
	| _filter_xfs_io
$XFS_IO_PROG -c "pwrite -S 0x70 -b 101376 189717473 101376" $SCRATCH_MNT/foo \
	| _filter_xfs_io
$XFS_IO_PROG -c "pwrite -S 0x71 -b 7000 227463698 7000" $SCRATCH_MNT/foo \
	| _filter_xfs_io
$XFS_IO_PROG -c "pwrite -S 0x72 -b 9216 12655137 9216" $SCRATCH_MNT/foo \
	| _filter_xfs_io
$XFS_IO_PROG -c "pwrite -S 0x73 -b 7000 7488866 7000" $SCRATCH_MNT/foo \
	| _filter_xfs_io
$XFS_IO_PROG -c "pwrite -S 0x74 -b 113664 87813649 113664" $SCRATCH_MNT/foo \
	| _filter_xfs_io
$XFS_IO_PROG -c "pwrite -S 0x75 -b 33792 25802183 33792" $SCRATCH_MNT/foo \
	| _filter_xfs_io
$XFS_IO_PROG -c "pwrite -S 0x76 -b 39936 93524024 39936" $SCRATCH_MNT/foo \
	| _filter_xfs_io
$XFS_IO_PROG -c "pwrite -S 0x77 -b 33792 113336388 33792" $SCRATCH_MNT/foo \
	| _filter_xfs_io
$XFS_IO_PROG -c "pwrite -S 0x78 -b 105472 184955320 105472" $SCRATCH_MNT/foo \
	| _filter_xfs_io
$XFS_IO_PROG -c "pwrite -S 0x79 -b 101376 225691598 101376" $SCRATCH_MNT/foo \
	| _filter_xfs_io
$XFS_IO_PROG -c "pwrite -S 0x80 -b 23552 77023155 23552" $SCRATCH_MNT/foo \
	| _filter_xfs_io
$XFS_IO_PROG -c "pwrite -S 0x81 -b 11 201888192 11" $SCRATCH_MNT/foo \
	| _filter_xfs_io
$XFS_IO_PROG -c "pwrite -S 0x82 -b 11 115332492 11" $SCRATCH_MNT/foo \
	| _filter_xfs_io
$XFS_IO_PROG -c "pwrite -S 0x83 -b 67584 230278015 67584" $SCRATCH_MNT/foo \
	| _filter_xfs_io
$XFS_IO_PROG -c "pwrite -S 0x84 -b 11 120589073 11" $SCRATCH_MNT/foo \
	| _filter_xfs_io
$XFS_IO_PROG -c "pwrite -S 0x85 -b 125952 202207819 125952" $SCRATCH_MNT/foo \
	| _filter_xfs_io
$XFS_IO_PROG -c "pwrite -S 0x86 -b 113664 86672080 113664" $SCRATCH_MNT/foo \
	| _filter_xfs_io
$XFS_IO_PROG -c "pwrite -S 0x87 -b 17408 208459603 17408" $SCRATCH_MNT/foo \
	| _filter_xfs_io
$XFS_IO_PROG -c "pwrite -S 0x88 -b 7000 73372211 7000" $SCRATCH_MNT/foo \
	| _filter_xfs_io
$XFS_IO_PROG -c "pwrite -S 0x89 -b 7000 42252122 7000" $SCRATCH_MNT/foo \
	| _filter_xfs_io
$XFS_IO_PROG -c "pwrite -S 0x90 -b 23552 46784881 23552" $SCRATCH_MNT/foo \
	| _filter_xfs_io
$XFS_IO_PROG -c "pwrite -S 0x91 -b 101376 63172351 101376" $SCRATCH_MNT/foo \
	| _filter_xfs_io
$XFS_IO_PROG -c "pwrite -S 0x92 -b 23552 59341931 23552" $SCRATCH_MNT/foo \
	| _filter_xfs_io
$XFS_IO_PROG -c "pwrite -S 0x93 -b 39936 239599283 39936" $SCRATCH_MNT/foo \
	| _filter_xfs_io
$XFS_IO_PROG -c "pwrite -S 0x94 -b 67584 175643105 67584" $SCRATCH_MNT/foo \
	| _filter_xfs_io
$XFS_IO_PROG -c "pwrite -S 0x97 -b 23552 105534880 23552" $SCRATCH_MNT/foo \
	| _filter_xfs_io
$XFS_IO_PROG -c "pwrite -S 0x98 -b 113664 8236844 113664" $SCRATCH_MNT/foo \
	| _filter_xfs_io
$XFS_IO_PROG -c "pwrite -S 0x99 -b 125952 144489686 125952" $SCRATCH_MNT/foo \
	| _filter_xfs_io
$XFS_IO_PROG -c "pwrite -S 0xa0 -b 7000 73273112 7000" $SCRATCH_MNT/foo \
	| _filter_xfs_io
$XFS_IO_PROG -c "pwrite -S 0xa1 -b 125952 194580243 125952" $SCRATCH_MNT/foo \
	| _filter_xfs_io
$XFS_IO_PROG -c "pwrite -S 0xa2 -b 123 56296779 123" $SCRATCH_MNT/foo \
	| _filter_xfs_io
$XFS_IO_PROG -c "pwrite -S 0xa3 -b 11 233066845 11" $SCRATCH_MNT/foo \
	| _filter_xfs_io
$XFS_IO_PROG -c "pwrite -S 0xa4 -b 39936 197727090 39936" $SCRATCH_MNT/foo \
	| _filter_xfs_io
$XFS_IO_PROG -c "pwrite -S 0xa5 -b 101376 53579812 101376" $SCRATCH_MNT/foo \
	| _filter_xfs_io
$XFS_IO_PROG -c "pwrite -S 0xa6 -b 9216 85669738 9216" $SCRATCH_MNT/foo \
	| _filter_xfs_io
$XFS_IO_PROG -c "pwrite -S 0xa7 -b 125952 21266322 125952" $SCRATCH_MNT/foo \
	| _filter_xfs_io
$XFS_IO_PROG -c "pwrite -S 0xa8 -b 23552 125726568 23552" $SCRATCH_MNT/foo \
	| _filter_xfs_io
$XFS_IO_PROG -c "pwrite -S 0xa9 -b 9216 18423680 9216" $SCRATCH_MNT/foo \
	| _filter_xfs_io
$XFS_IO_PROG -c "pwrite -S 0xb0 -b 1121 165901483 1121" $SCRATCH_MNT/foo \
	| _filter_xfs_io

_run_btrfs_util_prog subvolume snapshot -r $SCRATCH_MNT $SCRATCH_MNT/mysnap1

$XFS_IO_PROG -c "pwrite -S 0xff -b 10 16190218 10" $SCRATCH_MNT/foo \
	| _filter_xfs_io

_run_btrfs_util_prog subvolume snapshot -r $SCRATCH_MNT $SCRATCH_MNT/mysnap2

echo "File digests in the original filesystem:"
md5sum $SCRATCH_MNT/foo | _filter_scratch
md5sum $SCRATCH_MNT/mysnap0/foo | _filter_scratch
md5sum $SCRATCH_MNT/mysnap1/foo | _filter_scratch
md5sum $SCRATCH_MNT/mysnap2/foo | _filter_scratch

_run_btrfs_util_prog send $SCRATCH_MNT/mysnap1 -f $send_files_dir/1.snap
_run_btrfs_util_prog send -p $SCRATCH_MNT/mysnap1 $SCRATCH_MNT/mysnap2 \
	-f $send_files_dir/2.snap

_check_scratch_fs
_scratch_unmount
_scratch_mkfs >/dev/null 2>&1
_scratch_mount

_run_btrfs_util_prog receive $SCRATCH_MNT -f $send_files_dir/1.snap
_run_btrfs_util_prog receive $SCRATCH_MNT -f $send_files_dir/2.snap

echo "File digests in the replica filesystem:"
md5sum $SCRATCH_MNT/mysnap1/foo | _filter_scratch
md5sum $SCRATCH_MNT/mysnap2/foo | _filter_scratch

status=0
exit
