#! /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

# real QA test starts here

# Modify as appropriate.
_supported_fs generic
_require_scratch_reflink
_require_xfs_io_command "fiemap"

_scratch_mkfs > /dev/null 2>&1
_scratch_mount

blocksize=64k
file1="$SCRATCH_MNT/file1"
file2="$SCRATCH_MNT/file2"

# write the initial file
_pwrite_byte 0xcdcdcdcd 0 $blocksize $file1 | _filter_xfs_io

# reflink initial file
_reflink_range $file1 0 $file2 0 $blocksize | _filter_xfs_io

# check their fiemap to make sure it's correct
echo "before sync:"
echo "$file1" | _filter_scratch
$XFS_IO_PROG -c "fiemap -v" $file1 | _filter_fiemap_flags
echo "$file2" | _filter_scratch
$XFS_IO_PROG -c "fiemap -v" $file2 | _filter_fiemap_flags

# sync and recheck, to make sure the fiemap doesn't change just
# due to sync
sync
echo "after sync:"
echo "$file1" | _filter_scratch
$XFS_IO_PROG -c "fiemap -v" $file1 | _filter_fiemap_flags
echo "$file2" | _filter_scratch
$XFS_IO_PROG -c "fiemap -v" $file2 | _filter_fiemap_flags

# success, all done
status=0
exit
