#! /bin/bash
# SPDX-License-Identifier: GPL-2.0
# Copyright (C) 2016-2017 CTERA Networks. All Rights Reserved.
#
# FSQA Test No. 018
#
# Test hardlink breakage
#
# This simple test demonstrates a known issue with overlayfs:
# - file A and B are hardlinked in lower
# - modify A to trigger copy up
# - file A is no longer a hardlink of file B
#
seq=`basename $0`
seqres=$RESULT_DIR/$seq
echo "QA output created by $seq"

tmp=/tmp/$$
status=1	# failure is the default!
trap "_cleanup; exit \$status" 0 1 2 3 15

_cleanup()
{
	rm -f $tmp.*
}

# get standard environment, filters and checks
. ./common/rc
. ./common/filter

# real QA test starts here
_supported_fs overlay
_supported_os Linux
_require_scratch
_require_scratch_feature index
_require_test_program "t_dir_type"

rm -f $seqres.full

_scratch_mkfs >>$seqres.full 2>&1

# Create 2 hardlinked files in lower
lowerdir=$OVL_BASE_SCRATCH_MNT/$OVL_LOWER
mkdir -p $lowerdir
echo "zero" >> $lowerdir/foo
ln $lowerdir/foo $lowerdir/bar


# Record inode numbers in format <ino> <nlink>
function record_ino_nlink()
{
	ls -li $FILES | awk '{ print $1, $3, $10}' > $1
}

# Check inode numbers match recorded inode numbers
function check_ino_nlink()
{
	dir=$1
	before=$2
	after=$3

	record_ino_nlink $after

	# Test constant stat(2) st_ino/st_nlink -
	#   Compare before..after - expect silence
	# We use diff -u so out.bad will tell us which stage failed
	diff -u $before $after

	# Test constant readdir(3)/getdents(2) d_ino -
	#   Expect to find file by inode number
	cat $before | while read ino nlink f; do
		$here/src/t_dir_type $dir $ino | grep -q $(basename $f) || \
			echo "$(basename $f) not found by ino $ino (from $before)"
	done
}

# Enable overlay index feature to prevent breaking hardlinks on copy up
_scratch_mount -o index=on


rm -f $tmp.*

foo=$SCRATCH_MNT/foo
bar=$SCRATCH_MNT/bar

FILES="$foo $bar"

echo "== Before copy up =="
cat $FILES
record_ino_nlink $tmp.before

# Modify content of one of the hardlinks
# Intentionally modify the last hardlink in $FILES, so after mount cycle
# when reading the first file in $FILES, last file won't be in inode/dcache
echo "one" >> $bar

echo "== After write one =="
cat $FILES
check_ino_nlink $SCRATCH_MNT $tmp.before $tmp.after_one

# Verify that the hardlinks survive a mount cycle
_scratch_cycle_mount index=on

echo "== After mount cycle =="
cat $FILES
check_ino_nlink $SCRATCH_MNT $tmp.after_one $tmp.after_cycle

# Drop caches to get the copied up hardlink out of cache
echo 3 > /proc/sys/vm/drop_caches

# Modify content of the other hardlink
echo "two" >> $foo

echo "== After write two =="
cat $FILES
check_ino_nlink $SCRATCH_MNT $tmp.after_one $tmp.after_two

status=0
exit
