#! /bin/bash
# SPDX-License-Identifier: GPL-2.0
# Copyright (c) 2026 Chao Yu <chao@kernel.org>
#
# FS QA Test No. f2fs/025
#
# Check whether f2fs will encounter cp_error (Inconsistent segment type)
# when doing sanity check on type of segments inside large section during
# garbage collection.
#
. ./common/preamble
_begin_fstest auto quick

_fixed_by_kernel_commit 520760b9f915 \
	"f2fs: optimize representative type determination in GC"

. ./common/filter

_require_scratch
_require_scratch_shutdown
_require_xfs_io_command "pwrite"
_require_xfs_io_command "truncate"
_require_command "$F2FS_IO_PROG" f2fs_io
_require_fs_sysfs_attr $TEST_DEV gc_urgent
_require_check_dmesg

# Clear options to avoid interference from external configurations
export MKFS_OPTIONS=""
export MOUNT_OPTIONS=""

# Format with 96MB size and 2 segments per section
_scratch_mkfs_sized $((96 * 1024 * 1024)) "" "-s 2" >> $seqres.full 2>&1

# Mount with mode=lfs
_scratch_mount -o mode=lfs

# Create files to fill whole filesystem, then segment type will be changed to node type
for ((i=0;i<5120;i++)) do
	touch $SCRATCH_MNT/$i >> $seqres.full 2>&1
done
sync

# Remove all files to create free(empty) node segments
rm -f $SCRATCH_MNT/*
sync

# Allocate free space so that we have chance to reuse free(empty) node segments
$XFS_IO_PROG -f -c "pwrite -b 4k 0 1928k" $SCRATCH_MNT/file >> $seqres.full 2>&1
sync

$XFS_IO_PROG -c "truncate 0" $SCRATCH_MNT/file >> $seqres.full 2>&1
$XFS_IO_PROG -d -c "pwrite -b 4k 0 16M" $SCRATCH_MNT/file >> $seqres.full 2>&1
$XFS_IO_PROG -c "truncate 0" $SCRATCH_MNT/file >> $seqres.full 2>&1
$XFS_IO_PROG -d -c "pwrite -b 4k 0 16M" $SCRATCH_MNT/file >> $seqres.full 2>&1
$XFS_IO_PROG -c "truncate 0" $SCRATCH_MNT/file >> $seqres.full 2>&1
sync

$XFS_IO_PROG -d -c "pwrite -b 4k 0 8M" $SCRATCH_MNT/file >> $seqres.full 2>&1
$XFS_IO_PROG -c "truncate 0" $SCRATCH_MNT/file >> $seqres.full 2>&1
$XFS_IO_PROG -d -c "pwrite -b 4k 0 32K" $SCRATCH_MNT/file >> $seqres.full 2>&1
$XFS_IO_PROG -c "truncate 0" $SCRATCH_MNT/file >> $seqres.full 2>&1
$XFS_IO_PROG -d -c "pwrite -b 4k 0 2M" -c "fsync" $SCRATCH_MNT/file >> $seqres.full 2>&1

# Shutdown the filesystem without checkpoint
_scratch_shutdown

_scratch_unmount

_scratch_mount -o mode=lfs

# Run urgent_gc mode to trigger garbage collection
dev_name=$(_short_dev $SCRATCH_DEV)
$F2FS_IO_PROG gc_urgent $dev_name run 5 >> $seqres.full

_scratch_unmount

# Check whether the dmesg has the warning indicating the bug
_check_dmesg_for "F2FS-fs \($dev_name\): Inconsistent segment" && \
	_fail "F2FS-fs ($dev_name): Inconsistent segment type detected in dmesg!"

echo "Silence is golden"
_exit 0
