This is a regression test to check whether fsck can handle corrupted
nlinks correctly, it uses inject.f2fs to inject nlinks w/ wrong value,
and expects fsck.f2fs can detect such corruption and do the repair.

Cc: Jaegeuk Kim <jaeg...@kernel.org>
Signed-off-by: Chao Yu <c...@kernel.org>
---
v2:
- make _scratch_mkfs() to format f2fs image forcely
- adjust _check_scratch_fs() to allow specified parameter of f2fs
- add _fixed_by_git_commit
- add _require_command inject.f2fs
- simply return value handling
 common/rc          |   9 ++-
 tests/f2fs/009     | 133 +++++++++++++++++++++++++++++++++++++++++++++
 tests/f2fs/009.out |   2 +
 3 files changed, 143 insertions(+), 1 deletion(-)
 create mode 100755 tests/f2fs/009
 create mode 100644 tests/f2fs/009.out

diff --git a/common/rc b/common/rc
index bf24da4e..e156f3af 100644
--- a/common/rc
+++ b/common/rc
@@ -993,7 +993,7 @@ _scratch_mkfs()
                mkfs_filter="grep -v -e ^Warning: -e \"^mke2fs \""
                ;;
        f2fs)
-               mkfs_cmd="$MKFS_F2FS_PROG"
+               mkfs_cmd="$MKFS_F2FS_PROG -f"
                mkfs_filter="cat"
                ;;
        ocfs2)
@@ -3682,6 +3682,13 @@ _check_scratch_fs()
     ubifs)
        # there is no fsck program for ubifs yet
        ;;
+    f2fs)
+        if [ "$FSCK_OPTIONS" == "--dry-run" ]; then
+            fsck -t $FSTYP $device -- $FSCK_OPTIONS >> $seqres.full 2>&1
+        else
+            _check_generic_filesystem $device
+        fi
+        ;;
     *)
        _check_generic_filesystem $device
        ;;
diff --git a/tests/f2fs/009 b/tests/f2fs/009
new file mode 100755
index 00000000..d14df968
--- /dev/null
+++ b/tests/f2fs/009
@@ -0,0 +1,133 @@
+#! /bin/bash
+# SPDX-License-Identifier: GPL-2.0
+# Copyright (c) 2025 Chao Yu.  All Rights Reserved.
+#
+# FS QA Test No. f2fs/009
+#
+# This is a regression test to check whether fsck can handle corrupted
+# nlinks correctly, it uses inject.f2fs to inject nlinks w/ wrong value,
+# and expects fsck.f2fs can detect such corruption and do the repair.
+#
+. ./common/preamble
+_begin_fstest auto quick
+
+_require_scratch
+_require_command "$F2FS_INJECT_PROG" inject.f2fs
+
+_fixed_by_git_commit f2fs-tools 958cd6e
+
+filename=$SCRATCH_MNT/foo
+hardlink=$SCRATCH_MNT/bar
+
+for ((i=0;i<14;i++)) do
+       echo "round: " $i >> $seqres.full
+
+       _scratch_mkfs "-f" >> $seqres.full
+       _scratch_mount >> $seqres.full
+
+       if [ $i == 0 ]; then
+               touch $filename
+               ino=`stat -c '%i' $filename`
+               nlink=0
+       elif [ $i == 1 ]; then
+               mkdir $filename
+               ino=`stat -c '%i' $filename`
+               nlink=1
+       elif [ $i == 2 ]; then
+               mknod $filename c 9 0
+               ino=`stat -c '%i' $filename`
+               nlink=0
+       elif [ $i == 3 ]; then
+               mknod $filename b 8 0
+               ino=`stat -c '%i' $filename`
+               nlink=0
+       elif [ $i == 4 ]; then
+               mkfifo $filename
+               ino=`stat -c '%i' $filename`
+               nlink=0
+       elif [ $i == 5 ]; then
+               socket -s $filename >> $seqres.full 2>&1 &
+               pid=$!
+               sleep 2
+               ino=`stat -c '%i' $filename`
+               kill $pid >> $seqres.full 2>&1
+               nlink=0
+       elif [ $i == 6 ]; then
+               ln -s $SCRATCH_MNT/empty $filename
+               ino=`stat -c '%i' $filename`
+               nlink=0
+       elif [ $i == 7 ]; then
+               # orphan inode
+               touch $filename
+               ino=`stat -c '%i' $filename`
+               $F2FS_IO_PROG write 1 0 1 zero atomic_commit $filename 5000 >> 
$seqres.full 2>&1 &
+               stat $filename >> $seqres.full
+               rm $filename
+               $F2FS_IO_PROG shutdown 1 $SCRATCH_MNT/ >> $seqres.full
+               sleep 6
+               nlink=1
+       elif [ $i == 8 ]; then
+               # hardlink on file
+               touch $filename
+               ino=`stat -c '%i' $filename`
+               ln $filename $hardlink
+               nlink=0
+       elif [ $i == 9 ]; then
+               # hardlink on charactor
+               mknod $filename c 9 0
+               ino=`stat -c '%i' $filename`
+               ln $filename $hardlink
+               nlink=0
+       elif [ $i == 10 ]; then
+               # hardlink on blockdev
+               mknod $filename b 8 0
+               ino=`stat -c '%i' $filename`
+               ln $filename $hardlink
+               nlink=0
+       elif [ $i == 11 ]; then
+               # hardlink on pipe
+               mkfifo $filename
+               ino=`stat -c '%i' $filename`
+               ln $filename $hardlink
+               nlink=0
+       elif [ $i == 12 ]; then
+               # hardlink on socket
+               socket -s $filename >> $seqres.full 2>&1 &
+               pid=$!
+               sleep 2
+               ino=`stat -c '%i' $filename`
+               kill $pid >> $seqres.full 2>&1
+               ln $filename $hardlink
+               nlink=0
+       elif [ $i == 13 ]; then
+               # hardlink on symlink
+               ln -s $SCRATCH_MNT/empty $filename
+               ino=`stat -c '%i' $filename`
+               ln $filename $hardlink
+               nlink=0
+       fi
+
+       if [ $i != 7 ]; then
+               stat $SCRATCH_MNT/* >> $seqres.full
+       fi
+       echo "ino:"$ino >> $seqres.full
+       echo "nlink:"$nlink >> $seqres.full
+
+       _scratch_unmount
+
+       $F2FS_INJECT_PROG --node --mb i_links --nid $ino --val $nlink 
$SCRATCH_DEV \
+               >> $seqres.full || _fail "fail to inject"
+
+       export FSCK_OPTIONS="--dry-run"
+       _check_scratch_fs >>$seqres.full 2>&1 && _fail "can't find corruption"
+       _repair_scratch_fs >> $seqres.full
+       _check_scratch_fs
+
+       _scratch_mount
+       _scratch_unmount
+done
+
+echo "Silence is golden"
+
+status=0
+exit
diff --git a/tests/f2fs/009.out b/tests/f2fs/009.out
new file mode 100644
index 00000000..7e977155
--- /dev/null
+++ b/tests/f2fs/009.out
@@ -0,0 +1,2 @@
+QA output created by 009
+Silence is golden
-- 
2.48.1



_______________________________________________
Linux-f2fs-devel mailing list
Linux-f2fs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

Reply via email to