#! /bin/bash
# SPDX-License-Identifier: GPL-2.0
# Copyright (c) 2024 Red Hat, Inc.  All Rights Reserved.
#
# FS QA Test No. 609
#
# Test XFS online growfs log recovery.
#
. ./common/preamble
_begin_fstest auto growfs stress shutdown log recoveryloop

# Import common functions.
. ./common/filter

_stress_scratch()
{
	procs=4
	nops=999999
	# -w ensures that the only ops are ones which cause write I/O
	FSSTRESS_ARGS=`_scale_fsstress_args -d $SCRATCH_MNT -w -p $procs \
	    -n $nops`
	_run_fsstress_bg $FSSTRESS_ARGS >> $seqres.full 2>&1
}

_require_scratch
_require_command "$XFS_GROWFS_PROG" xfs_growfs

_scratch_mkfs_xfs | _filter_mkfs >$seqres.full 2>$tmp.mkfs
. $tmp.mkfs	# extract blocksize and data size for scratch device

endsize=`expr 550 \* 1048576`	# stop after growing this big
[ `expr $endsize / $dbsize` -lt $dblocks ] || _notrun "Scratch device too small"

nags=4
size=`expr 125 \* 1048576`	# 120 megabytes initially
sizeb=`expr $size / $dbsize`	# in data blocks
logblks=$(_scratch_find_xfs_min_logblocks -dsize=${size} -dagcount=${nags})

_scratch_mkfs_xfs -lsize=${logblks}b -dsize=${size} -dagcount=${nags} \
	>> $seqres.full 2>&1 || _fail "mkfs failed"
_scratch_mount

# Grow the filesystem in random sized chunks while stressing and performing
# shutdown and recovery. The randomization is intended to create a mix of sub-ag
# and multi-ag grows.
while [ $size -le $endsize ]; do
	echo "*** stressing a ${sizeb} block filesystem" >> $seqres.full
	_stress_scratch
	incsize=$((RANDOM % 40 * 1048576))
	size=`expr $size + $incsize`
	sizeb=`expr $size / $dbsize`	# in data blocks
	echo "*** growing to a ${sizeb} block filesystem" >> $seqres.full
	$XFS_GROWFS_PROG -D ${sizeb} $SCRATCH_MNT >> $seqres.full

	sleep $((RANDOM % 3))
	_scratch_shutdown
	_kill_fsstress
	_scratch_cycle_mount
done > /dev/null 2>&1
wait	# stop for any remaining stress processes

echo Silence is golden.

status=0
exit
