#! /bin/bash
# SPDX-License-Identifier: GPL-2.0-or-later
# Copyright (c) 2022 Oracle.  All Rights Reserved.
#
# FS QA Test No. 724
#
# Test scatter-gather atomic file writes.  We create a temporary file, write
# sparsely to it, then use FILE_SWAP_RANGE_SKIP_FILE1_HOLES flag to swap
# atomicallly only the ranges that we wrote.

. ./common/preamble
_begin_fstest auto quick fiexchange swapext

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

# Import common functions.
. ./common/filter

# real QA test starts here
_require_xfs_io_command swapext '-v vfs -a'
_require_scratch

_scratch_mkfs >> $seqres.full
_scratch_mount
_require_congruent_file_oplen $SCRATCH_MNT 65536

# Create original file
_pwrite_byte 0x58 0 1m $SCRATCH_MNT/a >> $seqres.full

# Create the donor file
$XFS_IO_PROG -f -c 'truncate 1m' $SCRATCH_MNT/b
_pwrite_byte 0x59 64k 64k $SCRATCH_MNT/b >> $seqres.full
_pwrite_byte 0x57 768k 64k $SCRATCH_MNT/b >> $seqres.full

md5sum $SCRATCH_MNT/a | _filter_scratch
md5sum $SCRATCH_MNT/b | _filter_scratch

# Test swapext.  -h means skip holes in /b, and -e means operate to EOF
echo swap | tee -a $seqres.full
$XFS_IO_PROG -c "swapext -v vfs -f -u -h -e -a $SCRATCH_MNT/b" $SCRATCH_MNT/a
_scratch_cycle_mount

md5sum $SCRATCH_MNT/a | _filter_scratch
md5sum $SCRATCH_MNT/b | _filter_scratch

# success, all done
status=0
exit
