#! /bin/bash
# SPDX-License-Identifier: GPL-2.0
# Copyright (c) 2025 IBM Corporation. All Rights Reserved.
#
# In ext4, even if an allocated range is physically and logically contiguous,
# it can still be split into 2 or more extents. This is because ext4 does not
# merge extents across leaf nodes. This is an issue for atomic writes since
# even for a continuous extent the map block could (in rare cases) return a
# shorter map, hence tearing the write. This test creates such a file and
# ensures that the atomic write handles this case correctly
#
. ./common/preamble
. ./common/atomicwrites
_begin_fstest auto atomicwrites

_require_scratch_write_atomic_multi_fsblock
_require_atomic_write_test_commands
_require_command "$DEBUGFS_PROG" debugfs

prep() {
	local bs=`_get_block_size $SCRATCH_MNT`
	local ex_hdr_bytes=12
	local ex_entry_bytes=12
	local entries_per_blk=$(( (bs - ex_hdr_bytes) / ex_entry_bytes ))

	# fill the extent tree leaf with bs len extents at alternate offsets.
	# The tree should look as follows
	#
	#                    +---------+---------+
	#                    | index 1 | index 2 |
	#                    +-----+---+-----+---+
	#                   +------+         +-----------+
	#                   |                            |
	#      +-------+-------+---+---------+     +-----+----+
	#      | ex 1  | ex 2  |   |  ex n   |     |  ex n+1  |
	#      | off:0 | off:2 |...| off:678 |     |  off:680 |
	#      | len:1 | len:1 |   |  len:1  |     |   len:1  |
	#      +-------+-------+---+---------+     +----------+
	#
	for i in $(seq 0 $entries_per_blk)
	do
		$XFS_IO_PROG -fc "pwrite -b $bs $((i * 2 * bs)) $bs" $testfile > /dev/null
	done
	sync $testfile

	echo >> $seqres.full
	echo "Create file with extents spanning 2 leaves. Extents:">> $seqres.full
	echo "...">> $seqres.full
	$DEBUGFS_PROG -R "ex `basename $testfile`" $SCRATCH_DEV |& tail >> $seqres.full

	# Now try to insert a new extent ex(new) between ex(n) and ex(n+1).
	# Since this is a new FS the allocator would find continuous blocks
	# such that ex(n) ex(new) ex(n+1) are physically(and logically)
	# contiguous. However, since we don't merge extents across leaf we will
	# end up with a tree as:
	#
	#                    +---------+---------+
	#                    | index 1 | index 2 |
	#                    +-----+---+-----+---+
	#                   +------+         +------------+
	#                   |                             |
	#      +-------+-------+---+---------+     +------+-----------+
	#      | ex 1  | ex 2  |   |  ex n   |     |  ex n+1 (merged) |
	#      | off:0 | off:2 |...| off:678 |     |      off:679     |
	#      | len:1 | len:1 |   |  len:1  |     |      len:2       |
	#      +-------+-------+---+---------+     +------------------+
	#
	echo >> $seqres.full
	torn_ex_offset=$((((entries_per_blk * 2) - 1) * bs))
	$XFS_IO_PROG -c "pwrite $torn_ex_offset $bs" $testfile >> /dev/null
	sync $testfile

	echo >> $seqres.full
	echo "Perform 1 block write at $torn_ex_offset to create torn extent. Extents:">> $seqres.full
	echo "...">> $seqres.full
	$DEBUGFS_PROG -R "ex `basename $testfile`" $SCRATCH_DEV |& tail >> $seqres.full

	_scratch_cycle_mount
}

_scratch_mkfs >> $seqres.full
_scratch_mount >> $seqres.full

testfile=$SCRATCH_MNT/testfile
touch $testfile
awu_max=$(_get_atomic_write_unit_max $testfile)

echo >> $seqres.full
echo "# Prepping the file" >> $seqres.full
prep

torn_aw_offset=$((torn_ex_offset - (torn_ex_offset % awu_max)))

echo >> $seqres.full
echo "# Performing atomic IO on the torn extent range. Command: " >> $seqres.full
echo $XFS_IO_PROG -c "open -fsd $testfile" -c "pwrite -S 0x61 -DA -V1 -b $awu_max $torn_aw_offset $awu_max" >> $seqres.full
$XFS_IO_PROG -c "open -fsd $testfile" -c "pwrite -S 0x61 -DA -V1 -b $awu_max $torn_aw_offset $awu_max" >> $seqres.full

echo >> $seqres.full
echo "Extent state after atomic write:">> $seqres.full
echo "...">> $seqres.full
$DEBUGFS_PROG -R "ex `basename $testfile`" $SCRATCH_DEV |& tail >> $seqres.full

echo >> $seqres.full
echo "# Checking data integrity" >> $seqres.full

# create a dummy file with expected data
$XFS_IO_PROG -fc "pwrite -S 0x61 -b $awu_max 0 $awu_max" $testfile.exp >> /dev/null
expected_data=$(od -An -t x1 -j 0 -N $awu_max $testfile.exp)

# We ensure that the data after atomic writes should match the expected data
actual_data=$(od -An -t x1 -j $torn_aw_offset -N $awu_max $testfile)
if [[ "$actual_data" != "$expected_data" ]]
then
	echo "Checksum match failed at off: $torn_aw_offset size: $awu_max"
	echo
	echo "Expected: "
	echo "$expected_data"
	echo
	echo "Actual contents: "
	echo "$actual_data"

	_fail
fi

echo -n "Data verification at offset $torn_aw_offset succeeded!" >> $seqres.full
echo "Silence is golden"
status=0
exit
