#! /bin/bash
# SPDX-License-Identifier: GPL-2.0-or-later
# Copyright (c) 2026 Tencent.  All Rights Reserved.
#
# FS QA Test No. 671
#
# Make sure that a full-file exchangerange under FILE1_WRITTEN does not strip
# the reflink flag from a file that still owns shared extents.  When the
# donor file is fully sparse, every mapping pair is skipped, so no extents
# actually move; the target file must keep its reflink flag, and a later
# write must go through CoW instead of modifying the shared blocks in place.

. ./common/preamble
_begin_fstest auto quick fiexchange

# Import common functions.
. ./common/filter
. ./common/reflink

_require_xfs_io_command exchangerange
_require_scratch_reflink
_fixed_by_fs_commit xfs xxxxxxxxxxxx \
	"xfs: fix exchange-range reflink flag clearing issue with INO1_WRITTEN"

_scratch_mkfs >> $seqres.full
_scratch_mount

# Create the original file with a known pattern and clone it, so that both
# files share the same extents.
_pwrite_byte 0x41 0 1m $SCRATCH_MNT/orig >> $seqres.full
_reflink $SCRATCH_MNT/orig $SCRATCH_MNT/clone >> $seqres.full

# Create a fully sparse donor file of the same size.
$XFS_IO_PROG -f -c 'truncate 1m' $SCRATCH_MNT/donor

md5_before=$(md5sum $SCRATCH_MNT/orig | awk '{print $1}')

# Swap the clone against the sparse donor, claiming the donor is fully
# written (-w).  Every donor mapping is a hole, so all pairs are skipped
# and the clone keeps its shared extents in place.
$XFS_IO_PROG -c "exchangerange -f -w $SCRATCH_MNT/donor" $SCRATCH_MNT/clone \
	>> $seqres.full

# Directly confirm the inode flag state after the exchange: the clone
# must still be flagged reflink.  The donor may also carry the flag, which
# is the conservative outcome of refusing the flag exchange under
# FILE1_WRITTEN; the extra flag is harmless and can be dropped later by
# the regular reflink flag cleanup path.
_scratch_unmount
_scratch_xfs_db -c "path /clone" -c print -c "path /donor" -c print | \
	grep reflink | sed -e 's/^v[0-9]*/vX/g'
_scratch_mount

# Overwrite part of the clone.  With the reflink flag correctly retained,
# this must go through CoW and leave the shared blocks of orig untouched.
_pwrite_byte 0x42 0 64k $SCRATCH_MNT/clone >> $seqres.full
_scratch_cycle_mount

md5_after=$(md5sum $SCRATCH_MNT/orig | awk '{print $1}')

test "$md5_before" != "$md5_after" && \
	echo "orig changed: md5 $md5_before -> $md5_after (write hit shared blocks in place)"

# success, all done
_exit 0
