#! /bin/bash
# FS QA Test No. 179
#
# See how well reflink handles overflowing reflink counts.
#
#-----------------------------------------------------------------------
# Copyright (c) 2016, Oracle and/or its affiliates.  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 -rf $tmp.*
}

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

# real QA test starts here
_supported_os Linux
_supported_fs xfs
_require_scratch_reflink
_require_scratch_nocheck
_require_cp_reflink
_require_test_program "punch-alternating"

rm -f $seqres.full

echo "Format and mount"
_scratch_mkfs -d agcount=1 > $seqres.full 2>&1
_scratch_mount >> $seqres.full 2>&1

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

blksz=65536

echo "Create original files"
_pwrite_byte 0x61 0 $blksz $testdir/file1 >> $seqres.full
_cp_reflink $testdir/file1 $testdir/file2 >> $seqres.full

echo "Change reference count"
_scratch_unmount
echo "set refcount to -4" >> $seqres.full
$XFS_DB_PROG -x -c 'agf 0' -c 'addr refcntroot' -c 'write recs[1].refcount 4294967292' $SCRATCH_DEV >> $seqres.full
echo "check refcount after setting to -4" >> $seqres.full
$XFS_DB_PROG -c 'agf 0' -c 'addr refcntroot' -c 'p recs[1]' $SCRATCH_DEV >> $seqres.full
_scratch_mount >> $seqres.full

echo "Reflink the overlinked file"
_cp_reflink $testdir/file1 $testdir/file3 >> $seqres.full
_cp_reflink $testdir/file1 $testdir/file4 >> $seqres.full
_cp_reflink $testdir/file1 $testdir/file5 >> $seqres.full
_cp_reflink $testdir/file1 $testdir/file6 >> $seqres.full
_cp_reflink $testdir/file1 $testdir/file7 >> $seqres.full

echo "Check scratch fs"
_scratch_unmount
echo "check refcount after reflinking 5 more times" >> $seqres.full
$XFS_DB_PROG -c 'agf 0' -c 'addr refcntroot' -c 'p recs[1]' $SCRATCH_DEV >> $seqres.full
_scratch_mount >> $seqres.full

echo "CoW a couple files"
_pwrite_byte 0x62 0 $blksz $testdir/file3 >> $seqres.full
_pwrite_byte 0x62 0 $blksz $testdir/file5 >> $seqres.full
_pwrite_byte 0x62 0 $blksz $testdir/file7 >> $seqres.full

echo "Check scratch fs"
_scratch_unmount
echo "check refcount after cowing 3 files" >> $seqres.full
$XFS_DB_PROG -c 'agf 0' -c 'addr refcntroot' -c 'p recs[1]' $SCRATCH_DEV >> $seqres.full
_scratch_mount >> $seqres.full

echo "Remove reflinked files"
rm -rf $testdir/file*

echo "Check scratch fs"
_scratch_unmount
echo "check refcount after removing all files" >> $seqres.full
$XFS_DB_PROG -c 'agf 0' -c 'addr refcntroot' -c 'p recs[1]' $SCRATCH_DEV >> $seqres.full
_scratch_xfs_repair -o force_geometry -n >> $seqres.full 2>&1
res=$?
if [ $res -eq 0 ]; then
	# If repair succeeds then format the device so that the post-test
	# check doesn't fail due to the single AG.
	_scratch_mkfs >> $seqres.full 2>&1
else
	_fail "xfs_repair fails"
fi

# success, all done
status=0
exit
