#! /bin/bash
# SPDX-License-Identifier: GPL-2.0
# Copyright (c) 2022-2024 Oracle and/or its affiliates.  All Rights Reserved.
#
# FS QA Test No. 619
#
# multi link parent pointer test
#
. ./common/preamble
_begin_fstest auto quick parent

# get standard environment, filters and checks
. ./common/parent

# Modify as appropriate
_require_scratch
_require_xfs_sysfs debug/larp
_require_xfs_parent
_require_xfs_io_command "parent"


# Create a directory tree using a protofile and
# make sure all inodes created have parent pointers

protofile=$tmp.proto

cat >$protofile <<EOF
DUMMY1
0 0
: root directory
d--777 3 1
: a directory
testfolder1 d--755 3 1
file1 ---755 3 1 /dev/null
: done
$
EOF

_scratch_mkfs -f -n parent=1 -p $protofile >>$seqresres.full 2>&1 \
	|| _fail "mkfs failed"
_check_scratch_fs

_scratch_mount >>$seqres.full 2>&1 \
	|| _fail "mount failed"

testfolder1="testfolder1"
file1="file1"
file1_ln="file1_link"

echo ""
# Multi link parent pointer test
NLINKS=100
for (( j=0; j<$NLINKS; j++ )); do
	ln $SCRATCH_MNT/$testfolder1/$file1 $SCRATCH_MNT/$testfolder1/$file1_ln.$j
	_xfs_verify_parent "$testfolder1" "$file1_ln.$j" "$testfolder1/$file1"
	_xfs_verify_parent "$testfolder1" "$file1" "$testfolder1/$file1_ln.$j"
done
# Multi unlink parent pointer test
for (( j=$NLINKS-1; j<=0; j-- )); do
	ino="$(stat -c '%i' $SCRATCH_MNT/$testfolder1/$file1_ln.$j)"
	rm $SCRATCH_MNT/$testfolder1/$file1_ln.$j
	_xfs_verify_no_parent "$file1_ln.$j" "$ino" "$testfolder1/$file1"
done

# success, all done
status=0
exit
