#! /bin/bash
# SPDX-License-Identifier: GPL-2.0
# Copyright (c) 2018 Oracle.  All Rights Reserved.
#
# FS QA Test No. 472
#
# Test various swapfile activation oddities.
#
. ./common/preamble
_begin_fstest auto quick swap

# Override the default cleanup function.
_cleanup()
{
	cd /
	swapoff $swapfile 2> /dev/null
	rm -f $tmp.*
}

# Import common functions.
. ./common/filter

# real QA test starts here
_supported_fs generic
_require_scratch_swapfile
_require_test_program mkswap
_require_test_program swapon

_scratch_mkfs >>$seqres.full 2>&1
_scratch_mount >>$seqres.full 2>&1

swapfile=$SCRATCH_MNT/swap
len=$((2 * 1048576))

swapfile_cycle() {
	local swapfile="$1"
	local len="$2"

	touch $swapfile
	# Swap files must be nocow on Btrfs.
	$CHATTR_PROG +C $swapfile >> $seqres.full 2>&1
	_pwrite_byte 0x58 0 $len $swapfile >> $seqres.full
	"$here/src/mkswap" $swapfile >> $seqres.full
	"$here/src/swapon" $swapfile 2>&1 | _filter_scratch
	swapoff $swapfile 2>> $seqres.full
	rm -f $swapfile
}

# Create a regular swap file
echo "regular swap" | tee -a $seqres.full
swapfile_cycle $swapfile $len

# Create a swap file with a little too much junk on the end
echo "too long swap" | tee -a $seqres.full
swapfile_cycle $swapfile $((len + 3))

# Create a ridiculously small swap file.  Each swap file must have at least
# two pages after the header page.
echo "tiny swap" | tee -a $seqres.full
swapfile_cycle $swapfile $(($(get_page_size) * 3))

status=0
exit
