#! /bin/bash
# SPDX-License-Identifier: GPL-2.0
# Copyright (c) 2023 Western Digital Cooperation.  All Rights Reserved.
#
# FS QA Test 305
#
# Test on-disk layout of RAID Stripe Tree Metadata by writing 8k to a new file
# with a filesystem prepropulated, so that 4k of the write are written to the
# 1st stripe and 4k start a new stripe.
#
. ./common/preamble
_begin_fstest auto quick raid remount volume raid-stripe-tree

. ./common/filter
. ./common/filter.btrfs

_require_btrfs_command inspect-internal dump-tree
_require_btrfs_mkfs_feature "raid-stripe-tree"
_require_scratch_dev_pool 4
_require_btrfs_fs_feature "raid_stripe_tree"
_require_btrfs_fs_feature "free_space_tree"
_require_btrfs_free_space_tree
_require_btrfs_no_compress
_require_btrfs_support_sectorsize 4096

test_8k_new_stripe()
{
	local profile=$1
	local ndevs=$2

	_scratch_dev_pool_get $ndevs

	echo "==== Testing $profile ===="
	_scratch_pool_mkfs -s 4k -d $profile -m $profile -O raid-stripe-tree
	_scratch_mount

	# Fill the first stripe up to 64k - 4k
	$XFS_IO_PROG -fc "pwrite 0 60k" -c fsync "$SCRATCH_MNT/bar" | _filter_xfs_io

	# The actual 8k write
	$XFS_IO_PROG -fc "pwrite 0 8k" "$SCRATCH_MNT/foo" | _filter_xfs_io

	_scratch_cycle_mount
	md5sum "$SCRATCH_MNT/foo" | _filter_scratch

	_scratch_unmount

	$BTRFS_UTIL_PROG inspect-internal dump-tree -t raid_stripe $SCRATCH_DEV_POOL |\
		_filter_stripe_tree

	_scratch_dev_pool_put
}

echo "= Test 8k write to a new file so that 4k start a new stripe ="
test_8k_new_stripe raid0 2
test_8k_new_stripe raid1 2
test_8k_new_stripe raid10 4

# success, all done
status=0
exit
