#! /bin/bash
# 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.
#
#-----------------------------------------------------------------------
# Copyright (C) 2018 CTERA Networks. All Rights Reserved.
# Author: Amir Goldstein <amir73il@gmail.com>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it would be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write the Free Software Foundation,
# Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#-----------------------------------------------------------------------
#

seq=`basename $0`
seqres=$RESULT_DIR/$seq
echo "QA output created by $seq"

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

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

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

# real QA test starts here

# Modify as appropriate.
_supported_fs generic
_supported_os Linux
_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
