#! /bin/bash
# SPDX-License-Identifier: GPL-2.0
# Copyright (c) 2016 Fujitsu. All Rights Reserved.
#
# FS QA Test 353
#
# Check if fiemap ioctl returns correct SHARED flag on reflinked file
# before and after sync the fs
#
# Btrfs has a bug in checking shared extent, which can only handle metadata
# already committed to disk, but not delayed extent tree modification.
# This caused SHARED flag only occurs after sync.
#
. ./common/preamble
_begin_fstest auto quick clone fiemap

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


# Modify as appropriate.
_require_scratch_reflink
_require_xfs_io_command "fiemap"

_scratch_mkfs > /dev/null 2>&1
_scratch_mount

blocksize=$(_get_file_block_size $SCRATCH_MNT)

file1="$SCRATCH_MNT/file1"
file2="$SCRATCH_MNT/file2"
extmap1="$SCRATCH_MNT/extmap1"
extmap2="$SCRATCH_MNT/extmap2"

# write the initial file
_pwrite_byte 0xcdcdcdcd 0 $blocksize $file1 > /dev/null

# reflink initial file
_reflink_range $file1 0 $file2 0 $blocksize > /dev/null

# check their fiemap to make sure it's correct
$XFS_IO_PROG -c "fiemap -v" $file1 | _filter_fiemap_flags > $extmap1
$XFS_IO_PROG -c "fiemap -v" $file2 | _filter_fiemap_flags > $extmap2

cmp -s $extmap1 $extmap2 || echo "mismatched extent maps before sync"

# sync and recheck, to make sure the fiemap doesn't change just
# due to sync
_scratch_sync
$XFS_IO_PROG -c "fiemap -v" $file1 | _filter_fiemap_flags > $extmap1
$XFS_IO_PROG -c "fiemap -v" $file2 | _filter_fiemap_flags > $extmap2

cmp -s $extmap1 $extmap2 || echo "mismatched extent maps after sync"

echo "Silence is golden"

# success, all done
status=0
exit
