#! /bin/bash
# FS QA Test No. 106
#
# Exercise basic xfs_quota functionality (user/group/project quota)
# Use of "sync" mount option here is an attempt to get deterministic
# allocator behaviour.
#
#-----------------------------------------------------------------------
# Copyright (c) 2005 Silicon Graphics, Inc.  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
#
#-----------------------------------------------------------------------
#
# creator
owner=nathans@sgi.com

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

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

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

_supported_fs xfs
_supported_os Linux #IRIX
_require_scratch
_require_xfs_quota

# real QA test starts here
_scratch_mkfs_xfs | _filter_mkfs 2>$tmp.mkfs
cat $tmp.mkfs >$seq.full
. $tmp.mkfs

# setup a default run
if [ -z "$MOUNT_OPTIONS" ]; then
	export MOUNT_OPTIONS="-o pquota,sync"
else
	export MOUNT_OPTIONS="$MOUNT_OPTIONS -o sync"
fi

_qmount
_require_prjquota $SCRATCH_DEV

# initial populate
target=$SCRATCH_MNT/target
$FSSTRESS_PROG -s 0xdeed -m8 -w -p4 -n1000 $FSSTRESS_AVOID -d $target
$FSSTRESS_PROG -s 0xdeed -m8 -z -p4 -n1000 -fsetxattr=500 -fchown=500 -d $target

# also use space, to be able to go over/under limits easily
uid=255
gid=254
prid=253
rm -f $SCRATCH_MNT/resv
$XFS_IO_PROG -fc "resvsp 0 200m" -c "chproj $prid" $SCRATCH_MNT/resv
chown $uid $SCRATCH_MNT/resv
chgrp $gid $SCRATCH_MNT/resv

_qmount

filter_xfs_quota()
{
	perl -ne "
s,$SCRATCH_MNT,[SCR_MNT],;
s,$SCRATCH_DEV,[SCR_DEV],;
s/Inode: \#\d+ \(0 blocks, 0 extents\)/Inode: #[INO] (0 blocks, 0 extents)/;
s/Inode: \#\d+ \(\d+ blocks, \d+ extents\)/Inode: #[INO] (X blocks, Y extents)/;
	print;"
}

test_quot()
{
	echo "checking quot command (type=$type)"  # not deterministic on blks
	xfs_quota -x -c "quot -n -$type" $SCRATCH_MNT >>$seq.full 2>&1
}

test_report()
{
	echo "checking report command (type=$type)"
	xfs_quota -x -c "report -h -$type -U 256" $SCRATCH_MNT
}

test_limit1()
{
	echo "checking limit command, pass 1 (type=$type)"
	xfs_quota -x -c "limit -$type bsoft=100m bhard=100m ihard=2 $id" \
		$SCRATCH_MNT
	xfs_quota -x -c "limit -$type isoft=1 rtbsoft=100m rtbhard=110m $id"\
		$SCRATCH_MNT
	sleep 2		# let the timer day transition happen
	xfs_quota -x -c "quota -$type -b -hnv $id" $SCRATCH_MNT
	xfs_quota -x -c "quota -$type -i -hnv $id" $SCRATCH_MNT
	#xfs_quota -x -c "quota -$type -r -hnv $id" $SCRATCH_MNT
}

test_limit2()
{
	# push limits up high, so that timers are cleared, etc. (for later)
	echo "checking limit command, pass2 (type=$type)"
	xfs_quota -x -c "limit -$type bsoft=300m bhard=400m ihard=8800 $id" \
		$SCRATCH_MNT
	xfs_quota -x -c "limit -$type isoft=8000 rtbsoft=310m rtbhard=410m $id"\
		$SCRATCH_MNT
	xfs_quota -x -c "quota -$type -b -hnv $id" $SCRATCH_MNT
	xfs_quota -x -c "quota -$type -i -hnv $id" $SCRATCH_MNT
	#xfs_quota -x -c "quota -$type -r -hnv $id" $SCRATCH_MNT
}

test_warn()
{
	echo "checking warn command (type=$type)"
	xfs_quota -x -c "warn -$type -b 4 $id" $SCRATCH_MNT
	xfs_quota -x -c "warn -$type -i 3 $id" $SCRATCH_MNT
	#xfs_quota -x -c "warn -$type -r 2 $id" $SCRATCH_MNT
	xfs_quota -x -c "quota -$type -b -hnv $id" $SCRATCH_MNT
	xfs_quota -x -c "quota -$type -i -hnv $id" $SCRATCH_MNT
	#xfs_quota -x -c "quota -$type -r -hnv $id" $SCRATCH_MNT
}

test_timer()
{
	echo "checking timer command (type=$type)"
	xfs_quota -x -c "timer -$type -b 3days" $SCRATCH_MNT
	xfs_quota -x -c "timer -$type -i 2days" $SCRATCH_MNT
	#xfs_quota -x -c "timer -$type -r 1day" $SCRATCH_MNT
}

test_state()
{
	echo "checking state command (type=$type)"
	xfs_quota -x -c "state -$type" $SCRATCH_MNT
	# not yet working properly?
	#echo "checking disable command (type=$type)"
	#xfs_quota -x -c "disable -$type -v" $SCRATCH_MNT
	#echo "checking enable command (type=$type)"
	#xfs_quota -x -c "enable -$type -v" $SCRATCH_MNT
	#echo "checking off command (type=$type)"
	#xfs_quota -x -c "off -$type -v" $SCRATCH_MNT
	#echo "checking remove command (type=$type)"
	#xfs_quota -x -c "remove -$type -v" $SCRATCH_MNT
}

test_backup()
{
	echo "checking dump command (type=$type)"
	rm -f $tmp.backup
	xfs_quota -x -c "dump -$type -f $tmp.backup -U 256" $SCRATCH_MNT
	cat $tmp.backup

	echo "changing limits (type=$type)"
	xfs_quota -x -c "limit -$type isoft=1000 ihard=1100 $id" $SCRATCH_MNT
	xfs_quota -x -c "quota -$type -i -hnv $id" $SCRATCH_MNT

	echo "checking restore command (type=$type)"
	xfs_quota -x -c "restore -$type -f $tmp.backup" $SCRATCH_MNT
	xfs_quota -x -c "quota -$type -i -hnv $id" $SCRATCH_MNT
	rm -f $tmp.backup
}

test_xfs_quota()
{
	test_quot	; echo
	test_report	; echo
	test_timer	; echo
	test_limit1	; echo
	test_warn	; echo
	test_limit2	; echo
	test_backup	; echo
	test_state	; echo
	echo		; echo
}

# real QA test starts here
export MOUNT_OPTIONS="-ouquota,sync"
_qmount
type=u
id=$uid
test_xfs_quota | filter_xfs_quota

export MOUNT_OPTIONS="-ogquota,sync"
_qmount
type=g
id=$gid
test_xfs_quota | filter_xfs_quota

export MOUNT_OPTIONS="-opquota,sync"
_qmount
type=p
id=$prid
test_xfs_quota | filter_xfs_quota

umount $SCRATCH_DEV
status=0
exit
