#! /bin/bash
# FSQA Test No. btrfs/057
#
# Quota rescan stress test, we run fsstress and quota rescan concurrently
#
#-----------------------------------------------------------------------
# Copyright (C) 2014 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

_cleanup()
{
	cd /
	rm -f $tmp.*
}
trap "_cleanup; exit \$status" 0 1 2 3 15

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

# real QA test starts here
_supported_fs btrfs
_supported_os Linux
_require_scratch

rm -f $seqres.full

# use small leaf size to get higher btree height.
run_check _scratch_mkfs "-b 1g --nodesize 4096"

# inode cache is saved in the FS tree itself for every
# individual FS tree,that affects the sizes reported by qgroup show
# so we need to explicitly turn it off to get consistent values.
run_check _scratch_mount "-o noinode_cache"

# -w ensures that the only ops are ones which cause write I/O
run_check $FSSTRESS_PROG -d $SCRATCH_MNT -w -p 5 -n 1000 \
		$FSSTRESS_AVOID >&/dev/null

_run_btrfs_util_prog subvolume snapshot $SCRATCH_MNT \
	$SCRATCH_MNT/snap1

run_check $FSSTRESS_PROG -d $SCRATCH_MNT/snap1 -w -p 5 -n 1000 \
       $FSSTRESS_AVOID >&/dev/null

_run_btrfs_util_prog quota enable $SCRATCH_MNT
_run_btrfs_util_prog quota rescan -w $SCRATCH_MNT

# ignore removing subvolume errors
rm -rf $SCRATCH_MNT/* >& /dev/null

_run_btrfs_util_prog filesystem sync $SCRATCH_MNT
units=`_btrfs_qgroup_units`
$BTRFS_UTIL_PROG qgroup show $units $SCRATCH_MNT | $SED_PROG -n '/[0-9]/p' \
	| $AWK_PROG '{print $2" "$3}'

status=0
exit
