#! /bin/bash
# SPDX-License-Identifier: GPL-2.0
# Copyright (c) 2016, Oracle and/or its affiliates.  All Rights Reserved.
#
# FS QA Test No. 336
#
# Exercise metadump on realtime rmapbt preservation.
#
. ./common/preamble
_begin_fstest auto rmap realtime metadump prealloc

_cleanup()
{
	cd /
	rm -rf "$tmp".*
	_xfs_cleanup_verify_metadump
}

. ./common/filter
. ./common/metadump

_require_command "$XFS_MDRESTORE_PROG" "xfs_mdrestore"
_require_realtime
_require_xfs_scratch_rmapbt
_require_test_program "punch-alternating"
_require_xfs_io_command "falloc"
_xfs_setup_verify_metadump

rm -f "$seqres.full"

echo "Format and mount"
_scratch_mkfs | _filter_mkfs 2>$tmp.mkfs >/dev/null
. $tmp.mkfs
cat $tmp.mkfs > "$seqres.full" 2>&1
_scratch_mount
blksz="$(_get_file_block_size $SCRATCH_MNT)"

echo "Create a three-level rtrmapbt"
# inode core size is at least 176 bytes; btree block header is 64 bytes;
# rtrmap record is 24 bytes; and rtrmap key/pointer are 48 bytes.
i_core_size="$(_xfs_get_inode_core_bytes $SCRATCH_MNT)"
i_ptrs=$(( (isize - i_core_size) / 48 ))
bt_ptrs=$(( (blksz - 64) / 48 ))
bt_recs=$(( (blksz - 64) / 24 ))

blocks=$((i_ptrs * bt_ptrs * bt_recs))
_require_fs_space $SCRATCH_MNT $(( (2 * blocks * blksz) * 5 / 4096 ))
len=$((blocks * rtextsz))

echo "Create big file"
$XFS_IO_PROG -f -R -c "falloc 0 $len" -c "pwrite -S 0x68 -b 1048576 0 $len" $SCRATCH_MNT/f1 >> $seqres.full
$XFS_IO_PROG -f -R -c "falloc 0 $len" -c "pwrite -S 0x68 -b 1048576 0 $len" $SCRATCH_MNT/f2 >> $seqres.full

echo "Explode the rtrmapbt"
$here/src/punch-alternating $SCRATCH_MNT/f1 >> "$seqres.full"
$here/src/punch-alternating $SCRATCH_MNT/f2 >> "$seqres.full"
_scratch_unmount

echo "Test metadump"
_xfs_verify_metadumps '-a -o'

# success, all done
status=0
exit
