#! /bin/bash
# SPDX-License-Identifier: GPL-2.0
# Copyright (c) 2020 Fujitsu.  All Rights Reserved.
#
# FS QA Test 608
# Toggling FS_XFLAG_DAX on an existing file can make S_DAX on the
# file change immediately when all applications close the file.
# It's a regression test for:
# 'commit 77573fa310d9 ("fs: Kill DCACHE_DONTCACHE dentry even if DCACHE_REFERENCED is set")'
#
# Write data into a file and then enable DAX on the file immediately,
# the written data which is still in the buffer should be synchronized
# to disk instead of discarded when the corresponding inode is evicted.
# It's a regression test for:
# 'commit 88149082bb8e ("fs: Handle I_DONTCACHE in iput_final() instead of generic_drop_inode()"'

. ./common/preamble
_begin_fstest auto attr quick dax

# Import common functions.
. ./common/filter

_require_scratch_dax_mountopt "dax=always"
_require_dax_iflag
_require_xfs_io_command "lsattr" "-v"
_require_xfs_io_command "statx" "-r"

test_enable_dax()
{
	local t_file=$SCRATCH_MNT/testfile

	rm -f $t_file
	touch $t_file
	_check_xflag $t_file 0
	_check_s_dax $t_file 0

	exec 3< $t_file

	$XFS_IO_PROG -c 'chattr +x' $t_file
	_check_xflag $t_file 1
	# One application is using test file and S_DAX
	# on the file is not changed immediately
	_check_s_dax $t_file 0

	exec 3<&-

	# No application is using test file and S_DAX
	# on the file is changed immediately
	_check_s_dax $t_file 1
}

test_disable_dax()
{
	local t_dir=$SCRATCH_MNT/testdir
	local t_file=$t_dir/testfile

	mkdir -p $t_dir
	$XFS_IO_PROG -c 'chattr +x' $t_dir
	rm -f $t_file
	touch $t_file
	_check_xflag $t_file 1
	_check_s_dax $t_file 1

	exec 3< $t_file

	$XFS_IO_PROG -c 'chattr -x' $t_file
	_check_xflag $t_file 0
	# One application is using test file and S_DAX
	# on the file is not changed immediately
	_check_s_dax $t_file 1

	exec 3<&-

	# No application is using test file and S_DAX
	# on the file is changed immediately
	_check_s_dax $t_file 0
}

test_buffered_data_lost()
{
	local t_file=$SCRATCH_MNT/datafile

	# Write data into a file
	echo "Buffered data" > $t_file

	# Then enable DAX on the file immediately
	$XFS_IO_PROG -c 'chattr +x' $t_file

	# Without commit 77573fa310d9, ensure inode can
	# be evicted by drop_caches
	echo 2 > /proc/sys/vm/drop_caches

	# The written data which is still in the buffer should not be lost
	grep -q "Buffered data" $t_file || echo "Buffered data is lost"

	rm -f $t_file
}

do_tests()
{
	local mount_option=$1

	_scratch_mount "$mount_option"

	# Make sure the root dir doesn't have FS_XFLAG_DAX set before we start.
	$XFS_IO_PROG -c "chattr -x" $SCRATCH_MNT &>> $seqres.full

	# Do test for commit 77573fa310d9
	test_enable_dax
	test_disable_dax

	# Do test for commit 88149082bb8e
	test_buffered_data_lost

	_scratch_unmount
}

_scratch_mkfs >> $seqres.full 2>&1

# Mount with dax option
do_tests "-o dax=inode"

# Mount without dax option
export MOUNT_OPTIONS=""
do_tests

# success, all done
echo "Silence is golden"
status=0
exit
