#! /bin/bash
# FS QA Test 099
#
# Check for qgroup reserved space leaks caused by re-writing dirty ranges
# This bug has been present in btrfs qgroup for a long time
#
#-----------------------------------------------------------------------
# Copyright (c) 2015 Fujitsu. All Rights Reserved.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it would be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write the Free Software Foundation,
# Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#-----------------------------------------------------------------------
#

seq=`basename $0`
seqres=$RESULT_DIR/$seq
echo "QA output created by $seq"

here=`pwd`
tmp=/tmp/$$
status=1	# failure is the default!
trap "_cleanup; exit \$status" 0 1 2 3 15

_cleanup()
{
	cd /
	rm -f $tmp.*
}

# get standard environment, filters and checks
. ./common/rc
. ./common/filter

# real QA test starts here

_supported_fs btrfs
_supported_os Linux
_require_scratch

# Use big blocksize to ensure there is still enough space left for metadata
# space reserve.
BLOCKSIZE=$(( 2 * 1024 * 1024 )) # 2M block size
FILESIZE=$(( 128 * 1024 * 1024 )) # 128M file size

rm -f $seqres.full

_scratch_mkfs >> $seqres.full 2>&1
_scratch_mount
_require_fs_space $SCRATCH_MNT $(($FILESIZE * 2 / 1024))

_run_btrfs_util_prog quota enable $SCRATCH_MNT
_run_btrfs_util_prog qgroup limit $FILESIZE 5 $SCRATCH_MNT

# loop 5 times without sync to ensure reserved space leak will happen
for i in `seq 1 5`; do
	# Use 1/4 of the file size, to ensure even commit is trigger by
	# dirty page threshold or commit interval, we should still be
	# able to continue write
	$XFS_IO_PROG -f -c "pwrite -b $BLOCKSIZE 0 $(($FILESIZE / 4))" \
		$SCRATCH_MNT/foo | _filter_xfs_io
done

# Sync to make sure all the dirty pages are written to disk, which should
# free all the reserved space
sync

# remove the file and sync, to ensure all quota space freed
rm $SCRATCH_MNT/foo
sync

# We should be able to write $FILESIZE - $BLOCKSIZE data now
$XFS_IO_PROG -f -c "pwrite -b $BLOCKSIZE 0 $(($FILESIZE - $BLOCKSIZE))" \
	$SCRATCH_MNT/foo | _filter_xfs_io

# success, all done
status=0
exit
