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

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

# Modify as appropriate
_require_scratch
_require_xfs_sysfs debug/larp
_require_xfs_io_error_injection "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
$
: back in the root
testfolder2 d--755 3 1
: done
$
EOF

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

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

testfolder1="testfolder1"
testfolder2="testfolder2"
file4="file4"
file5="file5"

echo ""

# Create files
touch $SCRATCH_MNT/$testfolder1/$file4
_xfs_verify_parent "$testfolder1" "$file4" "$testfolder1/$file4"

# Inject error
_scratch_inject_error "larp"

# Move files
mv $SCRATCH_MNT/$testfolder1/$file4 $SCRATCH_MNT/$testfolder2/$file5 2>&1 \
	| _filter_scratch

# FS should be shut down, touch will fail
touch $SCRATCH_MNT/$testfolder2/$file5 2>&1 | _filter_scratch

# Remount to replay log
_scratch_remount_dump_log >> $seqres.full

# FS should be online, touch should succeed
touch $SCRATCH_MNT/$testfolder2/$file5

# Check files again
_xfs_verify_parent "$testfolder2" "$file5" "$testfolder2/$file5"

# success, all done
status=0
exit
