#! /bin/bash
# SPDX-License-Identifier: GPL-2.0
# Copyright (C) 2014 SUSE Linux Products GmbH. All Rights Reserved.
#
# FS QA Test No. 325
#
# Make some pages/extents of a file dirty, do a ranged fsync that covers
# only some of the dirty pages/extents, and then do a regular fsync (or
# another ranged fsync that covers the remaining dirty pages/extents).
# Verify after that all extents were persisted.
#
# This test is motivated by a btrfs issue where the first ranged fsync
# would prevent the following fsync from persisting the remaining dirty
# pages/extents. This was fixed by the following btrfs kernel patch:
#
#     Btrfs: fix fsync data loss after a ranged fsync
#
. ./common/preamble
_begin_fstest auto quick data log mmap

# Override the default cleanup function.
_cleanup()
{
	_cleanup_flakey
}

# Import common functions.
. ./common/filter
. ./common/dmflakey

_require_scratch
_require_dm_target flakey

_scratch_mkfs >> $seqres.full 2>&1
_require_metadata_journaling $SCRATCH_DEV
_init_flakey
_mount_flakey

# Create the file first.
$XFS_IO_PROG -f -c "pwrite -S 0xff 0 256K" $SCRATCH_MNT/foo | _filter_xfs_io

# Now sync the file data to disk using 'sync' and not an fsync. This is because
# in btrfs the first fsync clears the btrfs inode full fsync flag, which must
# be set when the first msync below happens in order to trigger the bug.
_scratch_sync

# Now update the first 4Kb and the last 4Kb of the file, using memory mapped IO
# because an msync(), since the linux kernel commit
# 7fc34a62ca4434a79c68e23e70ed26111b7a4cf8, invokes a ranged fsync.
#
# After those writes, msync a range covering the first 4Kb and then after
# perform a msync with a range covering the last 4Kb of the file.
# This second msync() used to be a no-op for that btrfs bug (and the first fsync
# didn't log the last 4Kb extent as expected too).
$XFS_IO_PROG \
	-c "mmap -w 0 256K"         \
	-c "mwrite -S 0xaa 0 4K"   \
	-c "mwrite -S 0xbb 252K 4K" \
	-c "msync -s 0K 64K"       \
	-c "msync -s 192K 64K"      \
	-c "munmap"                \
	$SCRATCH_MNT/foo | _filter_xfs_io

echo "File content before crash/reboot:"
od -t x1 $SCRATCH_MNT/foo

_flakey_drop_and_remount

echo "File content after crash/reboot and fs mount:"
od -t x1 $SCRATCH_MNT/foo

_unmount_flakey

status=0
exit
