#! /bin/bash
# SPDX-License-Identifier: GPL-2.0
# Copyright (c) 2025 Chao Yu.  All Rights Reserved.
#
# FS QA Test No. f2fs/022
#
# This is a regression test:
# 1. create foo & bar
# 2. write 8M data to foo
# 3. use inject.f2fs to inject i_nid[0] of foo w/ ino of bar
# 4. fpunch in foo w/ specified range
#
. ./common/preamble
_begin_fstest auto quick rw

. ./common/attr

_fixed_by_kernel_commit c18ecd99e0c7 \
	"f2fs: fix to do sanity check on node footer for non inode dnode"

_require_scratch_nocheck
_require_inject_f2fs_command node i_nid

# remove all mkfs options to avoid layout change of on-disk inode
export MKFS_OPTIONS=""

foo_path=$SCRATCH_MNT/foo
bar_path=$SCRATCH_MNT/bar

_scratch_mkfs >> $seqres.full
_scratch_mount

touch $foo_path
touch $bar_path
$XFS_IO_PROG $foo_path -c "pwrite 0 8M" >> $seqres.full
sync
foo_ino=`stat -c '%i' $foo_path`
bar_ino=`stat -c '%i' $bar_path`
_scratch_unmount

# inject foo inode to replace i_nid[0] w/ to bar ino
$F2FS_INJECT_PROG --node --mb i_nid --nid $foo_ino --idx 0 --val $bar_ino $SCRATCH_DEV >> $seqres.full || _fail "failed to inject"

_scratch_mount

# if CONFIG_F2FS_CHECK_FS is enabled, it will trigger a kernel panic,
# otherwise, it will enter a deadloop.
$XFS_IO_PROG $foo_path -c "fpunch 6984k 4k"
_scratch_unmount

status=0
exit
