#! /bin/bash
# SPDX-License-Identifier: GPL-2.0
# Copyright (C) 2018 CTERA Networks. All Rights Reserved.
#
# FS QA Test No. 062
#
# Test file handle decode with multi lower layers on same fs
#
# This test is for a regression that was introduced in kernel v4.17 by
# commit 8b58924ad55c ("ovl: lookup in inode cache first when decoding
# lower file handle").
#
. ./common/preamble
_begin_fstest auto quick exportfs

# Override the default cleanup function.
_cleanup()
{
	cd /
	rm -f $tmp.*
	$UMOUNT_PROG $lowertestdir
}

# Import common functions.
. ./common/filter

# real QA test starts here

_supported_fs overlay
_require_scratch
_require_test_program "open_by_handle"
# We need to require all features together, because nfs_export cannot
# be enabled when index is disabled
_require_scratch_overlay_features index nfs_export

NUMFILES=1

# Create test dir and empty test files
create_test_files()
{
	local dir=$1

	mkdir -p $dir
	$here/src/open_by_handle -cwp $dir $NUMFILES
}

# Test encode/decode file handles on overlay mount
test_file_handles()
{
	local dir=$1

	$here/src/open_by_handle -rp $dir $NUMFILES
}

_scratch_mkfs

# All lower layers are on scratch partition
lower=$OVL_BASE_SCRATCH_MNT/$OVL_LOWER
lower2=$OVL_BASE_SCRATCH_MNT/$OVL_LOWER.2
lowertestdir=$lower2/testdir

create_test_files $lowertestdir

# bind mount to pin lower test dir dentry to dcache
$MOUNT_PROG --bind $lowertestdir $lowertestdir

# For non-upper overlay mount, nfs_export requires disabling redirect_dir.
$MOUNT_PROG -t overlay $OVL_BASE_SCRATCH_MNT $SCRATCH_MNT \
	-o ro,redirect_dir=nofollow,nfs_export=on,lowerdir=$lower:$lower2

# Decode an overlay directory file handle, whose underlying lower dir dentry
# is in dcache and connected and does not belong to the upper most lower layer.
# The test program drops caches, but $lowertestdir dcache in pinned, so only
# the overlay dentry is dropped.
test_file_handles $SCRATCH_MNT/testdir -rp

echo "Silence is golden"
status=0
exit
