#! /bin/bash
# SPDX-License-Identifier: GPL-2.0
# Copyright (c) 2006 Silicon Graphics, Inc.  All Rights Reserved.
#
# FSQA Test No. 307
#
# Check data integrity during defrag compacting
#
. ./common/preamble
_begin_fstest auto ioctl rw defrag prealloc

# Import common functions.
. ./common/filter
. ./common/defrag
# Disable all sync operations to get higher load
FSSTRESS_AVOID="$FSSTRESS_AVOID -ffsync=0 -fsync=0 -ffdatasync=0"
_workout()
{
	echo ""
	echo "Run fsstress"
	out=$SCRATCH_MNT/fsstress.$$
	args=`_scale_fsstress_args -p4 -n500 -f setattr=1 -d $out`
	echo "fsstress $args" >> $seqres.full
	_run_fsstress $args

	find $out -type f > $out.list
	cat $out.list | xargs  md5sum > $out.md5sum

	# Get the total of stat.st_size of each file to determine the
	# size of donor file. We use st_size here instead of blocks used
	# because thats what e4compat.c checks against.
	bytes=$(xargs -r stat -c %s < $out.list | awk '{s+=$1} END{print s}')
	echo "Total bytes used: $bytes" >> $seqres.full

	echo "Allocate donor file"
	$XFS_IO_PROG -c "falloc 0 $bytes" -f $SCRATCH_MNT/donor | _filter_xfs_io

	echo "Perform compacting"
	cat $out.list | run_check $here/src/e4compact \
		-i -v -f $SCRATCH_MNT/donor  >> $seqres.full 2>&1

	echo "Check data"
	run_check md5sum -c $out.md5sum
}

_exclude_fs ext2
_exclude_fs ext3

_require_scratch
_require_defrag
_require_xfs_io_command "falloc"

_scratch_mkfs >> $seqres.full 2>&1
_scratch_mount

_workout
status=0
exit
