FSCK_OPT=-yf
touch $TMPFILE

stat -f $TMPFILE | grep "Type: tmpfs" > /dev/null
if [ $? == 0 ] ; then
	echo "skipped for tmpfs (no O_DIRECT support)"
	return 0
fi

echo "make the test image ..." > $test_name.log
$MKE2FS -q -F -o Linux -b 1024 -O mmp $TMPFILE 100 >> $test_name.log 2>&1
status=$?
if [ "$status" != 0 ] ; then
	echo "mke2fs -O mmp failed" >  $test_name.failed
	exit 1
fi

# create a corrupted image
mmp_block=`$DEBUGFS -R "dump_mmp" $TMPFILE 2>/dev/null | grep "MMP Block" | awk '{print $NF}'`
echo "mmp_block is: $mmp_block" >> $test_name.log
echo "corrupt the mmp ..." >> $test_name.log
dd conv=notrunc if=/dev/zero of=$TMPFILE bs=1k count=1 seek=$mmp_block >> $test_name.log 2>&1

echo "the first fsck ..." >> $test_name.log
$FSCK $FSCK_OPT $TMPFILE >> $test_name.log 2>&1
status=$?
if [ "$status" != 0 ] ; then
	echo "the second fsck ..." >> $test_name.log
	$FSCK $FSCK_OPT $TMPFILE >> $test_name.log 2>&1
	status=$?
	if  [ "$status" = 0 ] ; then
	echo "ok"
	touch $test_name.ok
	else
	echo "the second fsck with corrupted mmp failed" >  $test_name.failed
	fi
else
	echo "the first fsck with corrupted mmp failed" >  $test_name.failed
fi

rm -f $TMPFILE
