#! /bin/bash
# SPDX-License-Identifier: GPL-2.0
# Copyright (C) 2018 CTERA Networks. All Rights Reserved.
#
# FS QA Test No. 477
#
# Check open by file handle after cycle mount.
#
# This test uses load and store of file handles from a temp file to test
# decoding file handles after cycle mount and after directory renames.
#
. ./common/preamble
_begin_fstest auto quick exportfs

# Import common functions.
. ./common/filter

# real QA test starts here

# Modify as appropriate.
_supported_fs generic
_require_test
# _require_exportfs already requires open_by_handle, but let's not count on it
_require_test_program "open_by_handle"
_require_exportfs

NUMFILES=10
testroot=$TEST_DIR/$seq-dir
testdir=$testroot/testdir

# Create test dir and test files, encode file handles and store to tmp file
create_test_files()
{
	rm -rf $testdir
	mkdir -p $testdir
	$here/src/open_by_handle -cwp -o $tmp.handles_file $testdir $NUMFILES
}

# Decode file handles loaded from tmp file
test_file_handles()
{
	local opt=$1
	local when=$2

	echo test_file_handles after $when
	$here/src/open_by_handle $opt -i $tmp.handles_file $TEST_DIR $NUMFILES
}

# Decode file handles of files/dir after cycle mount
create_test_files
_test_cycle_mount
test_file_handles -rp "cycle mount"

# Decode file handles of files/dir after rename of parent and cycle mount
create_test_files $testdir
rm -rf $testdir.renamed
mv $testdir $testdir.renamed/
_test_cycle_mount
test_file_handles -rp "rename parent"

# Decode file handles of files/dir after rename of grandparent and cycle mount
create_test_files $testdir
rm -rf $testroot.renamed
mv $testroot $testroot.renamed/
_test_cycle_mount
test_file_handles -rp "rename grandparent"

# Decode file handles of files/dir after move to new parent and cycle mount
create_test_files $testdir
rm -rf $testdir.new
mkdir -p $testdir.new
mv $testdir/* $testdir.new/
_test_cycle_mount
test_file_handles -r "move to new parent"

status=0
exit
