#! /bin/bash
# SPDX-License-Identifier: GPL-2.0-or-later
# Copyright (c) 2020, Oracle and/or its affiliates.  All Rights Reserved.
#
# FS QA Test No. 521
#
# Tests xfs_growfs on the realtime volume to make sure none of it blows up.
# This is a regression test for the following patches:
#
# xfs: Set xfs_buf type flag when growing summary/bitmap files
# xfs: Set xfs_buf's b_ops member when zeroing bitmap/summary files
# xfs: fix realtime bitmap/summary file truncation when growing rt volume
# xfs: make xfs_growfs_rt update secondary superblocks
# xfs: annotate grabbing the realtime bitmap/summary locks in growfs
#
. ./common/preamble
_begin_fstest auto quick realtime growfs

. ./common/filter

_require_realtime
_require_scratch
_require_scratch_size $((400 * 1024))

echo "Format and mount 100m rt volume"
_scratch_mkfs -r size=100m > $seqres.full
_scratch_mount

# zoned file systems only support zone-size aligned RT device sizes
_require_xfs_scratch_non_zoned

testdir=$SCRATCH_MNT/test-$seq
mkdir $testdir

echo "Check rt volume stats"
_xfs_force_bdev realtime $testdir
$XFS_INFO_PROG $SCRATCH_MNT >> $seqres.full
before=$(stat -f -c '%b' $testdir)

echo "Create some files"
_pwrite_byte 0x61 0 1m $testdir/original >> $seqres.full

echo "Grow fs"
# growfs expects sizes in FSB units
fsbsize=$(_get_block_size $SCRATCH_MNT)
$XFS_GROWFS_PROG -R $((400 * 1024 * 1024 / fsbsize)) $SCRATCH_MNT 2>&1 | \
	  _filter_growfs >> $seqres.full
_scratch_cycle_mount

echo "Recheck 400m rt volume stats"
$XFS_INFO_PROG $SCRATCH_MNT >> $seqres.full
after=$(stat -f -c '%b' $testdir)
_within_tolerance "rt volume size" $after $((before * 4)) 5% -v

echo "Create more copies to make sure the bitmap really works"
cp -p $testdir/original $testdir/copy3

echo "Check filesystem"
_check_scratch_fs

_scratch_unmount

# success, all done
status=0
exit
