#! /bin/bash
# SPDX-License-Identifier: GPL-2.0-or-later
# Copyright (c) 2022 Oracle.  All Rights Reserved.
#
# FS QA Test No. 752
#
# Make sure that exchangerange won't touch a swap file.

. ./common/preamble
_begin_fstest auto quick fiexchange

# Override the default cleanup function.
_cleanup()
{
	cd /
	test -e "$dir/a" && swapoff $dir/a
	rm -r -f $tmp.* $dir
}

# Import common functions.
. ./common/filter

_require_xfs_io_command exchangerange
_require_test

dir=$TEST_DIR/test-$seq
mkdir -p $dir

# Set up a fragmented swapfile and a dummy donor file.
$XFS_IO_PROG -f -c 'pwrite -S 0x58 0 32m -b 1m' -c fsync $dir/a >> $seqres.full
$here/src/punch-alternating $dir/a
$XFS_IO_PROG -f -c 'pwrite -S 0x58 0 32m -b 1m' -c fsync $dir/a >> $seqres.full
$MKSWAP_PROG $dir/a >> $seqres.full

$XFS_IO_PROG -f -c 'pwrite -S 0x59 0 32m -b 1m' $dir/b >> $seqres.full

swapon $dir/a || _notrun 'failed to swapon'

# Now try to exchangerange.
$XFS_IO_PROG -c "exchangerange $dir/b" $dir/a

# success, all done
status=0
exit
