#! /bin/bash
# SPDX-License-Identifier: GPL-2.0-or-later
# Copyright (c) 2021 Oracle.  All Rights Reserved.
#
# FS QA Test No. 147
#
# Make sure we validate realtime extent size alignment for fallocate modes.
# This is a regression test for fe341eb151ec ("xfs: ensure that fpunch,
# fcollapse, and finsert operations are aligned to rt extent size")

. ./common/preamble
_begin_fstest auto quick rw realtime collapse insert unshare zero prealloc

# Import common functions.
. ./common/filter

_require_scratch
_require_realtime
_require_xfs_io_command "fcollapse"
_require_xfs_io_command "finsert"
_require_xfs_io_command "funshare"
_require_xfs_io_command "fzero"
_require_xfs_io_command "falloc"

# Format filesystem with a 256k realtime extent size
_scratch_mkfs -r extsize=256k > $seqres.full
_scratch_mount >> $seqres.full

blksz=$(_get_block_size $SCRATCH_MNT)
rextsize=$(_xfs_get_rtextsize "$SCRATCH_MNT")
rextblks=$((rextsize / blksz))

echo "blksz $blksz rextsize $rextsize rextblks $rextblks" >> $seqres.full

# Make sure the root directory has rtinherit set so our test file will too
_xfs_force_bdev realtime $SCRATCH_MNT

sz=$((rextsize * 100))
range="$((blksz * 3)) $blksz"

for verb in fpunch finsert fcollapse fzero funshare falloc; do
	echo "test $verb"
	$XFS_IO_PROG -f -c "falloc 0 $sz" "$SCRATCH_MNT/b"
	$XFS_IO_PROG -f -c "$verb $range" "$SCRATCH_MNT/b"
	rm -f "$SCRATCH_MNT/b"
	_scratch_cycle_mount
done

# success, all done
status=0
exit
