aboutsummaryrefslogtreecommitdiff
path: root/tests/ubifs_tools-tests/fsck_tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests/ubifs_tools-tests/fsck_tests')
-rwxr-xr-xtests/ubifs_tools-tests/fsck_tests/authentication_refuse.sh.in66
-rwxr-xr-xtests/ubifs_tools-tests/fsck_tests/cycle_corrupted_fsck_fault_inject.sh.in225
-rwxr-xr-xtests/ubifs_tools-tests/fsck_tests/cycle_mount_fsck_check.sh.in154
-rwxr-xr-xtests/ubifs_tools-tests/fsck_tests/cycle_powercut_mount_fsck.sh.in148
-rwxr-xr-xtests/ubifs_tools-tests/fsck_tests/fsck_bad_image.sh.in355
-rwxr-xr-xtests/ubifs_tools-tests/fsck_tests/powercut_fsck_mount.sh.in148
-rwxr-xr-xtests/ubifs_tools-tests/fsck_tests/random_corrupted_fsck.sh.in206
7 files changed, 1302 insertions, 0 deletions
diff --git a/tests/ubifs_tools-tests/fsck_tests/authentication_refuse.sh.in b/tests/ubifs_tools-tests/fsck_tests/authentication_refuse.sh.in
new file mode 100755
index 0000000..268a7de
--- /dev/null
+++ b/tests/ubifs_tools-tests/fsck_tests/authentication_refuse.sh.in
@@ -0,0 +1,66 @@
+#!/bin/sh
+# Copyright (c), 2024, Huawei Technologies Co, Ltd.
+# Author: Zhihao Cheng <chengzhihao1@huawei.com>
+#
+# Test Description:
+# Refuse checking authenticated UBIFS image
+# Running time: 10s
+
+TESTBINDIR=@TESTBINDIR@
+source $TESTBINDIR/common.sh
+
+ID="0xec,0xa1,0x00,0x15" # 128M 128KB 2KB 512-sub-page
+
+function run_test()
+{
+ echo "Do authentication_refused test"
+
+ modprobe nandsim id_bytes=$ID
+ mtdnum="$(find_mtd_device "$nandsim_patt")"
+ flash_eraseall /dev/mtd$mtdnum
+
+ modprobe ubi mtd="$mtdnum,2048" || fatal "modprobe ubi fail"
+ ubimkvol -N vol_test -m -n 0 /dev/ubi$UBI_NUM || fatal "mkvol fail"
+ modprobe ubifs || fatal "modprobe ubifs fail"
+
+ mount_ubifs $DEV $MNT "authentication" || fatal "mount ubifs failed"
+ fsstress -d $MNT/fsstress -l0 -p4 -n10000 &
+ sleep $((RANDOM % 5))
+
+ ps -e | grep -w fsstress > /dev/null 2>&1
+ while [ $? -eq 0 ]
+ do
+ killall -9 fsstress > /dev/null 2>&1
+ sleep 1
+ ps -e | grep -w fsstress > /dev/null 2>&1
+ done
+
+ while true
+ do
+ res=`mount | grep "$MNT"`
+ if [[ "$res" == "" ]]
+ then
+ break;
+ fi
+ umount $MNT
+ sleep 0.1
+ done
+
+ fsck.ubifs -a $DEV # 'fsck.ubifs $DEV' is fine too.
+ res=$?
+ if [[ $res == $FSCK_OK ]]
+ then
+ fatal "fsck should not be success!"
+ fi
+
+ modprobe -r ubifs
+ modprobe -r ubi
+ modprobe -r nandsim
+}
+
+start_t=$(date +%s)
+run_test
+end_t=$(date +%s)
+time_cost=$(( end_t - start_t ))
+echo "Success, cost $time_cost seconds"
+exit 0
diff --git a/tests/ubifs_tools-tests/fsck_tests/cycle_corrupted_fsck_fault_inject.sh.in b/tests/ubifs_tools-tests/fsck_tests/cycle_corrupted_fsck_fault_inject.sh.in
new file mode 100755
index 0000000..2073fec
--- /dev/null
+++ b/tests/ubifs_tools-tests/fsck_tests/cycle_corrupted_fsck_fault_inject.sh.in
@@ -0,0 +1,225 @@
+#!/bin/sh
+# Copyright (c), 2024, Huawei Technologies Co, Ltd.
+# Author: Zhihao Cheng <chengzhihao1@huawei.com>
+#
+# Test Description:
+# For many kinds of flash, do following things
+# 1. mount UBIFS
+# 2. fsstress && unmount
+# 3. inject corruption into UBIFS image randomly
+# 3. fsck UBIFS && inject kinds of errors(memory, io)
+# 4. check UBIFS mounting result
+# Running time: 15min
+
+TESTBINDIR=@TESTBINDIR@
+source $TESTBINDIR/common.sh
+
+function run_test()
+{
+ local simulator="$1";
+ local size="$2";
+ local peb_size="$3";
+ local page_size="$4";
+ local encryption=$5;
+
+ echo "======================================================================"
+ printf "%s" "$simulator: ${size}MiB PEB size ${peb_size}KiB"
+ if [ "$simulator" = "nandsim" ]; then
+ printf " %s" "page size ${page_size}Bytes"
+ fi
+ printf " $encryption\n"
+
+ if [ "$simulator" = "nandsim" ]; then
+ $TESTBINDIR/load_nandsim.sh "$size" "$peb_size" "$page_size" || echo "cannot load nandsim";
+ mtdnum="$(find_mtd_device "$nandsim_patt")"
+ elif [ "$simulator" = "mtdram" ]; then
+ load_mtdram "$size" "$peb_size" || echo "cannot load mtdram"
+ mtdnum="$(find_mtd_device "$mtdram_patt")"
+ else
+ fatal "$simulator is not supported"
+ fi
+
+ flash_eraseall /dev/mtd$mtdnum
+ modprobe ubi mtd="$mtdnum,$page_size" || fatal "modprobe ubi fail"
+ ubimkvol -N vol_test -m -n 0 /dev/ubi$UBI_NUM || fatal "mkvol fail"
+ modprobe ubifs || fatal "modprobe ubifs fail"
+ mount_ubifs $DEV $MNT || fatal "mount ubifs fail"
+ if [[ "$encryption" == "encrypted" ]]; then
+ encryption_gen_key
+ encryption_set_key $MNT
+ fi
+
+ fsstress -d $MNT -l0 -p4 -n10000 &
+
+ sleep $((RANDOM % 20))
+
+ ps -e | grep -w fsstress > /dev/null 2>&1
+ while [ $? -eq 0 ]
+ do
+ killall -9 fsstress > /dev/null 2>&1
+ sleep 1
+ ps -e | grep -w fsstress > /dev/null 2>&1
+ done
+
+ while true
+ do
+ res=`mount | grep "$MNT"`
+ if [[ "$res" == "" ]]
+ then
+ break;
+ fi
+ umount $MNT
+ sleep 0.1
+ done
+
+ # inject corruption
+ times=$((RANDOM % 10))
+ let times=$times+10
+ i=0
+ tot_peb=`cat /sys/class/ubi/ubi$UBI_NUM/total_eraseblocks`;
+
+ modprobe -r ubifs
+ modprobe -r ubi # Stop wear-leveling & erasing worker
+ while [[ $i -lt $times ]]
+ do
+ let i=$i+1;
+ peb=$((RANDOM % $tot_peb));
+ pg=`expr $peb_size \* 1024`;
+ peb_off=`expr $pg \* $peb`
+ pages=`expr $pg / $page_size`;
+ pg=`expr $pages - 2`;
+ pg=$((RANDOM % $pg));
+ pg_off=`expr $pg + 2`;
+ pg_start=`expr $pages \* $peb`;
+ pg=`expr $pg_start + $pg_off`;
+ vid_pg=`expr $pg_start + 1`;
+ dd if=/dev/mtd$mtdnum of=$TMP_FILE bs=$page_size skip=$vid_pg count=1 2>/dev/null;
+ content=`cat $TMP_FILE | grep UBI!`; # vid header magic
+ if [[ "$content" == "" ]]; then
+ # Skip free PEB, otherwise LEB data could be overwritten in UBIFS
+ continue;
+ fi
+ if [[ $((RANDOM % 2)) == 0 ]]; then
+ # Corrupts 1 page
+ dd if=/dev/urandom of=/dev/mtd$mtdnum bs=$page_size seek=$pg count=1;
+ else
+ # Erase 1 LEB, TNC points to an unmapped area
+ flash_erase /dev/mtd$mtdnum $peb_off 1
+ fi
+ done
+ rm -f $TMP_FILE 2>/dev/null
+ sync
+
+ skip=0
+ modprobe ubi mtd="$mtdnum,$page_size"
+ ret=$?
+ if [[ $ret != 0 ]]
+ then
+ skip=1
+ echo "UBI layout volume is corrupted, skip"
+ fi
+
+ if [[ $skip == 0 ]]; then
+ modprobe ubifs || fatal "modprobe ubifs2 fail"
+ dmesg -c > /dev/null
+
+ round=0
+ while [[ $round -lt 50 ]];
+ do
+ let round=$round+1
+ inject_mem=0
+
+ fsck.ubifs -yb $DEV 2>&1 > $LOG_FILE &
+ pid=$!
+ if [[ $((RANDOM % 2)) == 0 ]]; then
+ inject_mem_err $pid
+ inject_mem=1
+ fi
+ inject_io_err
+ wait $pid
+ cat $LOG_FILE
+ if [[ $inject_mem == 1 ]]; then
+ cancel_mem_err
+ fi
+ cancel_io_err
+
+ # UBI could become ro-mode, reload it
+ modprobe -r ubifs
+ modprobe -r ubi
+ modprobe ubi mtd="$mtdnum,$page_size" || fatal "modprobe ubi2 fail"
+ modprobe ubifs || fatal "modprobe ubifs3 fail"
+ done
+
+ fsck.ubifs -yb $DEV 2>&1 > $LOG_FILE
+ res=$?
+ cat $LOG_FILE
+ let "ret=$res&~$FSCK_NONDESTRUCT"
+ if [[ $ret != $FSCK_OK ]]
+ then
+ # Skip superblock error
+ log=`cat $LOG_FILE | grep "bad node at LEB 0:"`
+ if [[ "$log" != "" ]]
+ then
+ skip=1
+ echo "SB is corrupted, skip fsck & mounting"
+ else
+ fatal "fsck fail $res"
+ fi
+ fi
+
+ if [[ $skip == 0 ]]; then
+ enable_chkfs
+
+ mount_ubifs $DEV $MNT "noauthentication" "noatime"
+ res=$?
+ if [[ $res != 0 ]]
+ then
+ fatal "mount fail $res"
+ fi
+
+ if [[ "$encryption" == "encrypted" ]]; then
+ # Ignore the encrypting error, root dir could be
+ # corrupted, the new root dir cannot be
+ # encrypted because it is not empty.
+ encryption_set_key $MNT 1
+ fi
+
+ du -sh $MNT > /dev/null # Make sure all files are accessible
+ ret=$?
+ if [[ $ret != 0 ]]; then
+ fatal "Cannot access all files"
+ fi
+ # check_err_msg is not suggested in this testcase, because
+ # ubi_io_read(triggered by wear_leveling_worker -> ubi_eba_copy_leb)
+ # could print stack if ecc uncorrectable errors are detected.
+
+ umount $MNT
+ res=$?
+ if [[ $res != 0 ]]
+ then
+ fatal "unmount fail $res"
+ fi
+ fi
+
+ modprobe -r ubifs
+ modprobe -r ubi
+ fi
+ modprobe -r $simulator
+
+ echo "----------------------------------------------------------------------"
+}
+
+check_fsstress
+start_t=$(date +%s)
+echo "Do corrruption+cycle_fsck_fault_injection test in kinds of flashes"
+for simulator in "mtdram" "nandsim"; do
+ for encryption in "encrypted" "noencrypted"; do
+ run_test "$simulator" "16" "16" "512" $encryption
+ run_test "$simulator" "256" "128" "2048" $encryption
+ run_test "$simulator" "1024" "512" "2048" $encryption
+ done
+done
+end_t=$(date +%s)
+time_cost=$(( end_t - start_t ))
+echo "Success, cost $time_cost seconds"
+exit 0
diff --git a/tests/ubifs_tools-tests/fsck_tests/cycle_mount_fsck_check.sh.in b/tests/ubifs_tools-tests/fsck_tests/cycle_mount_fsck_check.sh.in
new file mode 100755
index 0000000..c9972e2
--- /dev/null
+++ b/tests/ubifs_tools-tests/fsck_tests/cycle_mount_fsck_check.sh.in
@@ -0,0 +1,154 @@
+#!/bin/sh
+# Copyright (c), 2024, Huawei Technologies Co, Ltd.
+# Author: Zhihao Cheng <chengzhihao1@huawei.com>
+#
+# Test Description:
+# Do many cycles of mount/fsstress/umount/fsck/mount, check whether the
+# filesystem content before fsck and after fsck are consistent.
+# Running time: 10h
+
+TESTBINDIR=@TESTBINDIR@
+source $TESTBINDIR/common.sh
+
+ID="0xec,0xa1,0x00,0x15" # 128M 128KB 2KB 512-sub-page
+
+function run_test()
+{
+ encryption=$1
+ modprobe nandsim id_bytes=$ID
+ mtdnum="$(find_mtd_device "$nandsim_patt")"
+ flash_eraseall /dev/mtd$mtdnum
+
+ dmesg -c > /dev/null
+
+ modprobe ubi mtd="$mtdnum,2048" || fatal "modprobe ubi fail"
+ ubimkvol -N vol_test -m -n 0 /dev/ubi$UBI_NUM || fatal "mkvol fail"
+ modprobe ubifs || fatal "modprobe ubifs fail"
+
+ echo "Do cycle mount+umount+fsck+check_fs_content test ($encryption)"
+
+ if [[ "$encryption" == "encrypted" ]]; then
+ encryption_gen_key
+ fi
+
+ round=0
+ while [[ $round -lt 20 ]]
+ do
+ echo "---------------------- ROUND $round ----------------------"
+ let round=$round+1
+
+ mount_ubifs $DEV $MNT "noauthentication" "noatime" || fatal "mount ubifs fail"
+ if [[ "$encryption" == "encrypted" ]]; then
+ encryption_set_key $MNT
+ fi
+
+ per=`df -Th | grep ubifs | awk '{print $6}'`;
+ if [[ ${per%?} -gt 95 ]]; then
+ # Used > 95%
+ echo "Clean files"
+ rm -rf $MNT/*
+ check_err_msg
+ fi
+
+ fsstress -d $MNT -l0 -p4 -n10000 &
+
+ sleep $((RANDOM % 30))
+
+ ps -e | grep -w fsstress > /dev/null 2>&1
+ while [ $? -eq 0 ]
+ do
+ killall -9 fsstress > /dev/null 2>&1
+ sleep 1
+ ps -e | grep -w fsstress > /dev/null 2>&1
+ done
+
+ per=`df -Th | grep ubifs | awk '{print $6}'`;
+ if [[ ${per%?} -gt 95 ]]; then
+ dmesg -c > /dev/null # The ENOSPC error messages may exist
+ else
+ check_err_msg # Make sure new operations are okay after fsck
+ fi
+ sync
+
+ # Record filesystem information
+ rm -f $TMP_FILE 2>/dev/null
+ read_dir $MNT "md5sum"
+
+ while true
+ do
+ res=`mount | grep "$MNT"`
+ if [[ "$res" == "" ]]
+ then
+ break;
+ fi
+ umount $MNT
+ sleep 0.1
+ done
+
+ fsck.ubifs -a $DEV 2>&1 > $LOG_FILE
+ res=$?
+ cat $LOG_FILE
+ if [[ $res != $FSCK_OK ]]
+ then
+ # The lpt nodes could be parsed incorrectly because the lpt disk
+ # layout is too simple. See details in
+ # https://lore.kernel.org/linux-mtd/97ca7fe4-4ad4-edd1-e97a-1d540aeabe2d@huawei.com/
+ log=`cat $LOG_FILE | grep "dbg_check_ltab_lnum: invalid empty space in LEB"`
+ if [[ "$log" == "" ]]; then
+ fatal "fsck fail $res"
+ fi
+ if [[ $res != $FSCK_NONDESTRUCT ]]; then
+ fatal "fsck fail $res"
+ fi
+ fi
+
+ enable_chkfs
+
+ mount_ubifs $DEV $MNT "noauthentication" "noatime"
+ res=$?
+ if [[ $res != 0 ]]
+ then
+ fatal "mount fail $res"
+ fi
+
+ if [[ "$encryption" == "encrypted" ]]; then
+ encryption_set_key $MNT
+ fi
+
+ du -sh $MNT > /dev/null # Ensure that all files are accessible
+ ret=$?
+ if [[ $ret != 0 ]]; then
+ fatal "Cannot access all files"
+ fi
+ check_err_msg
+
+ # Check filesystem information
+ parse_dir "md5sum"
+ rm -f $TMP_FILE 2>/dev/null
+
+ umount $MNT
+ res=$?
+ if [[ $res != 0 ]]
+ then
+ fatal "unmount fail $res"
+ fi
+
+ check_err_msg
+
+ disable_chkfs
+ done
+
+ modprobe -r ubifs
+ modprobe -r ubi
+ modprobe -r nandsim
+}
+
+check_fsstress
+start_t=$(date +%s)
+for encryption in "encrypted" "noencrypted"; do
+ run_test $encryption
+done
+end_t=$(date +%s)
+time_cost=$(( end_t - start_t ))
+echo "Success, cost $time_cost seconds"
+exit 0
diff --git a/tests/ubifs_tools-tests/fsck_tests/cycle_powercut_mount_fsck.sh.in b/tests/ubifs_tools-tests/fsck_tests/cycle_powercut_mount_fsck.sh.in
new file mode 100755
index 0000000..6af1b54
--- /dev/null
+++ b/tests/ubifs_tools-tests/fsck_tests/cycle_powercut_mount_fsck.sh.in
@@ -0,0 +1,148 @@
+#!/bin/sh
+# Copyright (c), 2024, Huawei Technologies Co, Ltd.
+# Author: Zhihao Cheng <chengzhihao1@huawei.com>
+#
+# Test Description:
+# Do many cycles of mount/fsstress/powercut/umount/fsck/mount, check whether
+# mount is successful.
+# Running time: 9h
+
+TESTBINDIR=@TESTBINDIR@
+source $TESTBINDIR/common.sh
+
+ID="0x20,0xa7,0x00,0x26" # 4G 256KB 4KB 2KB-sub-page
+
+function run_test()
+{
+ local encryption=$1
+
+ echo "Do cycle mount+powercut+fsck+umount($encryption) test"
+ modprobe nandsim id_bytes=$ID
+ mtdnum="$(find_mtd_device "$nandsim_patt")"
+ flash_eraseall /dev/mtd$mtdnum
+
+ dmesg -c > /dev/null
+
+ modprobe ubi mtd="$mtdnum,4096" || fatal "modprobe ubi fail"
+ ubimkvol -N vol_test -m -n 0 /dev/ubi$UBI_NUM || fatal "mkvol fail"
+ modprobe ubifs || fatal "modprobe ubifs fail"
+
+ if [[ "$encryption" == "encrypted" ]]; then
+ encryption_gen_key
+ fi
+
+ round=0
+ while [[ $round -lt 60 ]]
+ do
+ echo "---------------------- ROUND $round ----------------------"
+ let round=$round+1
+
+ mount_ubifs $DEV $MNT || fatal "mount ubifs fail"
+ if [[ "$encryption" == "encrypted" ]]; then
+ encryption_set_key $MNT
+ fi
+
+ if [[ $(($round % 30)) == 0 ]]
+ then
+ echo "Clean files"
+ rm -rf $MNT/*
+ check_err_msg
+ fi
+
+ fsstress -d $MNT -l0 -p4 -n10000 &
+ sleep $((RANDOM % 30))
+
+ per=`df -Th | grep ubifs | awk '{print $6}'`;
+ if [[ ${per%?} -gt 95 ]]; then
+ dmesg -c > /dev/null # The ENOSPC error messages may exist
+ else
+ check_err_msg # Make sure new operations are okay after fsck
+ fi
+ powercut
+
+ ps -e | grep -w fsstress > /dev/null 2>&1
+ while [ $? -eq 0 ]
+ do
+ killall -9 fsstress > /dev/null 2>&1
+ sleep 1
+ ps -e | grep -w fsstress > /dev/null 2>&1
+ done
+
+ while true
+ do
+ res=`mount | grep "$MNT"`
+ if [[ "$res" == "" ]]
+ then
+ break;
+ fi
+ umount $MNT
+ sleep 0.1
+ done
+
+ fsck.ubifs -a $DEV 2>&1 > $LOG_FILE
+ res=$?
+ cat $LOG_FILE
+ if [[ $res != $FSCK_OK ]]
+ then
+ # Powercut during layout_leb_in_gaps may change index
+ # LEBs without updating LPT.
+ idx_log=`cat $LOG_FILE | grep "Inconsistent properties" | grep "is_idx 1"`
+ # The lpt nodes could be parsed incorrectly because the lpt disk
+ # layout is too simple. See details in
+ # https://lore.kernel.org/linux-mtd/97ca7fe4-4ad4-edd1-e97a-1d540aeabe2d@huawei.com/
+ lpt_log=`cat $LOG_FILE | grep "dbg_check_ltab_lnum: invalid empty space in LEB"`
+ if [[ "$idx_log" == "" ]] && [[ "$lpt_log" == "" ]]; then
+ fatal "fsck fail $res"
+ fi
+ if [[ $res != $FSCK_NONDESTRUCT ]]; then
+ fatal "fsck fail $res"
+ fi
+ fi
+
+ dmesg -c > /dev/null # powercut could reproduce error messages
+
+ enable_chkfs
+
+ mount_ubifs $DEV $MNT "noauthentication" "noatime"
+ res=$?
+ if [[ $res != 0 ]]
+ then
+ fatal "mount fail $res"
+ fi
+
+ if [[ "$encryption" == "encrypted" ]]; then
+ encryption_set_key $MNT
+ fi
+
+ du -sh $MNT > /dev/null # Make sure all files are accessible
+ ret=$?
+ if [[ $ret != 0 ]]; then
+ fatal "Cannot access all files"
+ fi
+ check_err_msg
+
+ umount $MNT
+ res=$?
+ if [[ $res != 0 ]]
+ then
+ fatal "unmount fail $res"
+ fi
+
+ check_err_msg
+
+ disable_chkfs
+ done
+
+ modprobe -r ubifs
+ modprobe -r ubi
+ modprobe -r nandsim
+}
+
+check_fsstress
+start_t=$(date +%s)
+run_test "encrypted"
+run_test "noencrypted"
+end_t=$(date +%s)
+time_cost=$(( end_t - start_t ))
+echo "Success, cost $time_cost seconds"
+exit 0
diff --git a/tests/ubifs_tools-tests/fsck_tests/fsck_bad_image.sh.in b/tests/ubifs_tools-tests/fsck_tests/fsck_bad_image.sh.in
new file mode 100755
index 0000000..2ba9f78
--- /dev/null
+++ b/tests/ubifs_tools-tests/fsck_tests/fsck_bad_image.sh.in
@@ -0,0 +1,355 @@
+#!/bin/sh
+# Copyright (c), 2024, Huawei Technologies Co, Ltd.
+# Author: Zhihao Cheng <chengzhihao1@huawei.com>
+#
+# Test Description: Tests whether all inconsistent UBIFS images can be fixed
+# as expected.
+# Origin UBIFS image content:
+# /
+# ├── corrupt_file (xattr - user.corrupt:123, 2K data)
+# ├── dir
+# │   ├── block_dev
+# │   ├── char_dev
+# │   ├── dir
+# │   └── file (content: '123')
+# ├── hardl_corrupt_file => corrupt_file
+# └── softl_corrupt_file -> corrupt_file
+#
+# Running time: 2min
+
+TESTBINDIR=@TESTBINDIR@
+source $TESTBINDIR/common.sh
+
+CORRUPT_FILE=corrupt_file
+CORRUPT_FILE_INUM=INO_65
+XATTR_NAME="user.corrupt"
+XATTR_VAL=123
+CORRUPT_FILE_MD5=7d2f953e91033c743ab6a801d5ee6e15
+SOFT_LINK_FILE=softl_corrupt_file
+HARD_LINK_FILE=hardl_corrupt_file
+DIR=dir
+BLOCK_DEV=block_dev
+CHAR_DEV=char_dev
+FILE=file
+FILE_INUM=INO_72
+FILE_MD5=ba1f2511fc30423bdbb183fe33f3dd0f
+LOST_FOUND="lost+found"
+
+function fsck_image()
+{
+ local img_type=$1;
+ local img=$2;
+ local fsck_mode=$3;
+ local file_exist=$4;
+ local file_nochange=$5
+ local file_xattr_exist=$6;
+ local hard_link_exist=$7;
+ local hard_link_no_change=$8;
+ local hard_link_xattr_exist=$9;
+ local soft_link_exist=${10};
+ local dir_exist=${11};
+ local dir_file_no_change=${12};
+ local lost_found=${13};
+
+ echo "======================================================================"
+ echo "fsck $img_type, success_fsck_mode:$fsck_mode file_exist:$file_exist file_nochange:$file_nochange file_xattr_exist:$file_xattr_exist hard_link_exist:$hard_link_exist hard_link_no_change:$hard_link_no_change:hard_link_xattr_exist $hard_link_xattr_exist:soft_link_exist:$soft_link_exist dir_exist:$dir_exist $lost_found"
+
+ load_mtdram 1 16 || echo "cannot load mtdram"
+ mtdnum="$(find_mtd_device "$mtdram_patt")"
+
+ dmesg -c > /dev/null
+ gzip -f -k -d $TESTBINDIR/${img}.gz || fatal "gzip failed"
+ flash_eraseall /dev/mtd$mtdnum
+ dd if=$TESTBINDIR/$img of=/dev/mtd$mtdnum bs=1M
+ rm -f $TESTBINDIR/$img
+ modprobe ubi mtd="$mtdnum,0" || fatal "modprobe ubi fail"
+ modprobe ubifs || fatal "modprobe ubifs fail"
+
+ fsck.ubifs -a $DEV
+ let "ret=$?&~$FSCK_NONDESTRUCT"
+ if [[ $ret != $FSCK_OK ]]; then
+ if [[ "$fsck_mode" == "safe" ]]; then
+ fatal "The image should be fixed in $fsck_mode mode, but it fails"
+ fi
+
+ fsck.ubifs -y $DEV
+ let "ret=$?&~$FSCK_NONDESTRUCT"
+ if [[ $ret != $FSCK_OK ]]; then
+ if [[ "$fsck_mode" == "danger_default" ]]; then
+ fatal "The image should be fixed in $fsck_mode mode, but it fails"
+ fi
+
+ fsck.ubifs -yb $DEV
+ let "ret=$?&~$FSCK_NONDESTRUCT"
+ if [[ $ret != $FSCK_OK ]]; then
+ if [[ "$fsck_mode" == "danger_rebuild" ]]; then
+ fatal "The image should be fixed in $fsck_mode mode, but it fails"
+ fi
+
+ echo "fsck failed is expected, skip"
+
+ modprobe -r ubifs
+ modprobe -r ubi
+ modprobe -r mtdram
+ echo "----------------------------------------------------------------------"
+
+ return;
+ elif [[ "$fsck_mode" != "danger_rebuild" ]]; then
+ fatal "The image should not be fixed in $fsck_mode mode, but it succeeds"
+ fi
+ elif [[ "$fsck_mode" != "danger_default" ]]; then
+ fatal "The image should not be fixed in $fsck_mode mode, but it succeeds"
+ fi
+ elif [[ "$fsck_mode" != "safe" ]]; then
+ fatal "The image should not be fixed in $fsck_mode mode, but it succeeds"
+ fi
+
+ enable_chkfs
+
+ mount_ubifs $DEV $MNT
+ ret=$?
+ if [[ $ret != 0 ]]; then
+ fatal "mount failed $ret"
+ fi
+
+ du -sh $MNT > /dev/null # Make sure all files are accessible
+ ret=$?
+ if [[ $ret != 0 ]]; then
+ fatal "cannot access all files $ret"
+ fi
+
+ if [[ $file_exist == 1 ]]; then
+ if ! [ -f $MNT/$CORRUPT_FILE ]; then
+ fatal "$MNT/$CORRUPT_FILE is lost"
+ fi
+ else
+ if [ -f $MNT/$CORRUPT_FILE ]; then
+ fatal "$MNT/$CORRUPT_FILE should not exist"
+ fi
+ fi
+
+ md5_after=`md5sum $MNT/$CORRUPT_FILE 2>/dev/null | awk '{print $1}'`
+ if [[ $file_nochange == 1 ]]; then
+ if [[ $CORRUPT_FILE_MD5 != $md5_after ]]; then
+ fatal "content changed for $MNT/$CORRUPT_FILE"
+ fi
+ else
+ if [[ $CORRUPT_FILE_MD5 == $md5_after ]]; then
+ fatal "content not changed for $MNT/$CORRUPT_FILE"
+ fi
+ fi
+
+ xattr=`getfattr -n $XATTR_NAME $MNT/$CORRUPT_FILE 2>/dev/null | grep $XATTR_NAME | awk -F '=' '{ print $2 }'`
+ if [[ $file_xattr_exist == 1 ]]; then
+ if ! [[ "$xattr" =~ "$XATTR_VAL" ]]; then
+ fatal "wrong xattr $xattr for $MNT/$CORRUPT_DENT_NAME"
+ fi
+ else
+ if [[ "$xattr" =~ "$XATTR_VAL" ]]; then
+ fatal "xattr $xattr for $MNT/$CORRUPT_DENT_NAME should not exist"
+ fi
+ fi
+
+ if [[ $hard_link_exist == 1 ]]; then
+ if ! [ -f $MNT/$HARD_LINK_FILE ]; then
+ fatal "$MNT/$HARD_LINK_FILE should is lost"
+ fi
+ else
+ if [ -f $MNT/$HARD_LINK_FILE ]; then
+ fatal "$MNT/$HARD_LINK_FILE should not exist"
+ fi
+ fi
+
+ md5_after=`md5sum $MNT/$HARD_LINK_FILE 2>/dev/null | awk '{print $1}'`
+ if [[ $hard_link_no_change == 1 ]]; then
+ if [[ $CORRUPT_FILE_MD5 != $md5_after ]]; then
+ fatal "content changed for $MNT/$HARD_LINK_FILE"
+ fi
+ else
+ if [[ $CORRUPT_FILE_MD5 == $md5_after ]]; then
+ fatal "content not changed for $MNT/$HARD_LINK_FILE"
+ fi
+ fi
+
+ xattr=`getfattr -n $XATTR_NAME $MNT/$HARD_LINK_FILE 2>/dev/null | grep $XATTR_NAME | awk -F '=' '{ print $2 }'`
+ if [[ $hard_link_xattr_exist == 1 ]]; then
+ if ! [[ "$xattr" =~ "$XATTR_VAL" ]]; then
+ fatal "wrong xattr $xattr for $MNT/$HARD_LINK_FILE"
+ fi
+ else
+ if [[ "$xattr" =~ "$XATTR_VAL" ]]; then
+ fatal "xattr $xattr for $MNT/$HARD_LINK_FILE should not exist"
+ fi
+ fi
+
+ link=`stat -c %N $MNT/$SOFT_LINK_FILE 2>/dev/null | grep $SOFT_LINK_FILE | grep $CORRUPT_FILE`
+ if [[ $soft_link_exist == 1 ]]; then
+ if [[ "$link" == "" ]]; then
+ fatal "$MNT/$SOFT_LINK_FILE is lost"
+ fi
+ else
+ if [[ "$link" != "" ]]; then
+ fatal "$MNT/$SOFT_LINK_FILE should not exist"
+ fi
+ fi
+
+ if [[ $dir_exist == 1 ]]; then
+ if ! [ -d $MNT/$DIR ]; then
+ fatal "$MNT/$DIR is lost"
+ fi
+ if ! [ -d $MNT/$DIR/$DIR ]; then
+ fatal "$MNT/$DIR/$DIR is lost"
+ fi
+ if ! [ -f $MNT/$DIR/$FILE ]; then
+ fatal "$MNT/$DIR/$FILE is lost"
+ fi
+ f_md5=`md5sum $MNT/$DIR/$FILE 2>/dev/null | awk '{print $1}'`
+ if [[ $dir_file_no_change == 1 ]]; then
+ if [[ $FILE_MD5 != $f_md5 ]]; then
+ fatal "content changed for $MNT/$DIR/$FILE"
+ fi
+ else
+ if [[ $FILE_MD5 == $f_md5 ]]; then
+ fatal "content not changed for $MNT/$DIR/$FILE"
+ fi
+ fi
+ if ! [ -b $MNT/$DIR/$BLOCK_DEV ]; then
+ fatal "$MNT/$DIR/$BLOCK_DEV is lost"
+ fi
+ major=`stat -c %t $MNT/$DIR/$BLOCK_DEV`
+ minor=`stat -c %T $MNT/$DIR/$BLOCK_DEV`
+ if [[ $major != 1 ]] || [[ $minor != 2 ]]; then
+ echo "major/minor changed for $MNT/$DIR/$BLOCK_DEV"
+ fi
+ if ! [ -c $MNT/$DIR/$CHAR_DEV ]; then
+ fatal "$MNT/$DIR/$CHAR_DEV is lost"
+ fi
+ major=`stat -c %t $MNT/$DIR/$CHAR_DEV`
+ minor=`stat -c %T $MNT/$DIR/$CHAR_DEV`
+ if [[ $major != 0 ]] || [[ $minor != 1 ]]; then
+ echo "major/minor changed for $MNT/$DIR/$CHAR_DEV"
+ fi
+ else
+ if [ -d $MNT/$DIR ]; then
+ fatal "$MNT/$DIR should not exist"
+ fi
+ fi
+
+ if [[ "$lost_found" == "no lost+found" ]]; then
+ if [ -d $MNT/$LOST_FOUND ]; then
+ fatal "$MNT/$LOST_FOUND should not exist"
+ fi
+ elif [[ "$lost_found" == "lost+found is regular" ]]; then
+ if ! [ -f $MNT/$LOST_FOUND ]; then
+ fatal "$MNT/$LOST_FOUND is not regular file"
+ fi
+ else
+ if ! [ -d $MNT/$LOST_FOUND ]; then
+ fatal "$MNT/$LOST_FOUND is lost"
+ fi
+
+ if ! [ -f $MNT/$LOST_FOUND/${FILE_INUM}_0 ]; then
+ fatal "$MNT/$LOST_FOUND/${FILE_INUM}_0 is lost"
+ fi
+ if [[ "$lost_found" == "lost+found has one" ]]; then
+ f_md5=`md5sum $MNT/$LOST_FOUND/${FILE_INUM}_0 2>/dev/null | awk '{print $1}'`
+ if [[ $FILE_MD5 != $f_md5 ]]; then
+ fatal "content changed for $MNT/$LOST_FOUND/${FILE_INUM}_0"
+ fi
+ elif [[ "$lost_found" == "lost+found has two" ]]; then
+ f_md5=`md5sum $MNT/$LOST_FOUND/${CORRUPT_FILE_INUM}_0 2>/dev/null | awk '{print $1}'`
+ if [[ $CORRUPT_FILE_MD5 != $f_md5 ]]; then
+ fatal "content changed for $MNT/$LOST_FOUND/${CORRUPT_FILE_INUM}_0"
+ fi
+ f_md5=`md5sum $MNT/$LOST_FOUND/${FILE_INUM}_0 2>/dev/null | awk '{print $1}'`
+ if [[ $FILE_MD5 != $f_md5 ]]; then
+ fatal "content changed for $MNT/$LOST_FOUND/${FILE_INUM}_0"
+ fi
+ else
+ if ! [ -f $MNT/$LOST_FOUND/${FILE_INUM}_1 ]; then
+ fatal "$MNT/$LOST_FOUND/${FILE_INUM}_1 is lost"
+ fi
+ f_md5=`md5sum $MNT/$LOST_FOUND/${FILE_INUM}_1 2>/dev/null | awk '{print $1}'`
+ if [[ $FILE_MD5 != $f_md5 ]]; then
+ fatal "content changed for $MNT/$LOST_FOUND/${FILE_INUM}_1"
+ fi
+ fi
+ fi
+
+ umount $MNT
+ res=$?
+ if [[ $res != 0 ]]
+ then
+ fatal "unmount fail $res"
+ fi
+
+ disable_chkfs
+
+ check_err_msg
+
+ modprobe -r ubifs
+ modprobe -r ubi
+ modprobe -r mtdram
+
+ echo "----------------------------------------------------------------------"
+}
+
+start_t=$(date +%s)
+echo "Do inconsistent UBIFS images fscking test"
+fsck_image "good image" good "safe" 1 1 1 1 1 1 1 1 1 "no lost+found"
+fsck_image "bad sb fanout image" sb_fanout "none" 1 1 1 1 1 1 1 1 1 "no lost+found"
+fsck_image "bad sb fmt_version image" sb_fmt_version "none" 1 1 1 1 1 1 1 1 1 "no lost+found"
+fsck_image "bad sb leb_size image" sb_leb_size "none" 1 1 1 1 1 1 1 1 1 "no lost+found"
+fsck_image "bad sb log_lebs image" sb_log_lebs "none" 1 1 1 1 1 1 1 1 1 "no lost+found"
+fsck_image "bad sb min_io_size image" sb_min_io_size "none" 1 1 1 1 1 1 1 1 1 "no lost+found"
+fsck_image "bad master highest_inum image" master_highest_inum "danger_rebuild" 1 1 1 1 1 1 1 1 1 "no lost+found"
+fsck_image "bad master lpt image" master_lpt "danger_rebuild" 1 1 1 1 1 1 1 1 1 "no lost+found"
+fsck_image "bad master tnc image" master_tnc "danger_rebuild" 1 1 1 1 1 1 1 1 1 "no lost+found"
+fsck_image "bad master total_dead image" master_total_dead "safe" 1 1 1 1 1 1 1 1 1 "no lost+found"
+fsck_image "bad master total_dirty image" master_total_dirty "safe" 1 1 1 1 1 1 1 1 1 "no lost+found"
+fsck_image "bad master total_free image" master_total_free "safe" 1 1 1 1 1 1 1 1 1 "no lost+found"
+fsck_image "corrupted journal log area image" journal_log "danger_rebuild" 1 1 1 1 1 1 1 1 1 "no lost+found"
+fsck_image "corrupted journal bud area image" journal_bud "danger_default" 1 0 1 1 0 1 1 1 0 "no lost+found"
+fsck_image "bad orphan node image" orphan_node "danger_default" 0 0 0 0 0 0 1 1 1 "no lost+found"
+fsck_image "bad lpt dirty image" lpt_dirty "safe" 1 1 1 1 1 1 1 1 1 "no lost+found"
+fsck_image "bad lpt lpt_flags image" lpt_flags "safe" 1 1 1 1 1 1 1 1 1 "no lost+found"
+fsck_image "bad lpt free image" lpt_free "safe" 1 1 1 1 1 1 1 1 1 "no lost+found"
+fsck_image "bad lpt pos image" lpt_pos "safe" 1 1 1 1 1 1 1 1 1 "no lost+found"
+fsck_image "bad lprops table dirty image" ltab_dirty "safe" 1 1 1 1 1 1 1 1 1 "no lost+found"
+fsck_image "bad lprops table free image" ltab_free "safe" 1 1 1 1 1 1 1 1 1 "no lost+found"
+fsck_image "bad index size image" index_size "safe" 1 1 1 1 1 1 1 1 1 "no lost+found"
+fsck_image "bad tnc lv0 key image" tnc_lv0_key "danger_rebuild" 1 1 1 1 1 1 1 1 1 "no lost+found"
+fsck_image "bad tnc lv0 len image" tnc_lv0_len "danger_rebuild" 1 1 1 1 1 1 1 1 1 "no lost+found"
+fsck_image "bad tnc lv0 pos image" tnc_lv0_pos "danger_rebuild" 1 1 1 1 1 1 1 1 1 "no lost+found"
+fsck_image "bad tnc non-leaf key image" tnc_noleaf_key "danger_rebuild" 1 1 1 1 1 1 1 1 1 "no lost+found"
+fsck_image "bad tnc non-leaf len image" tnc_noleaf_len "danger_rebuild" 1 1 1 1 1 1 1 1 1 "no lost+found"
+fsck_image "bad tnc non-leaf pos image" tnc_noleaf_pos "danger_rebuild" 1 1 1 1 1 1 1 1 1 "no lost+found"
+fsck_image "corrupted leb for file data image" corrupted_data_leb "danger_default" 1 0 1 1 0 1 1 1 1 "no lost+found"
+fsck_image "corrupted leb for TNC image" corrupted_idx_leb "danger_rebuild" 1 1 1 1 1 1 1 1 1 "no lost+found"
+fsck_image "bad inode data image" inode_data "danger_default" 1 0 1 1 0 1 1 1 1 "no lost+found"
+fsck_image "bad inode mode image" inode_mode "danger_default" 0 0 0 0 0 0 1 1 1 "no lost+found"
+fsck_image "bad inode nlink image" inode_nlink "safe" 1 1 1 1 1 1 1 1 1 "no lost+found"
+fsck_image "bad inode size image" inode_size "safe" 1 1 1 1 1 1 1 1 1 "no lost+found"
+fsck_image "bad inode xattr_cnt image" inode_xcnt "safe" 1 1 1 1 1 1 1 1 1 "no lost+found"
+fsck_image "bad softlink inode mode image" soft_link_inode_mode "danger_default" 1 1 1 1 1 1 0 1 1 "no lost+found"
+fsck_image "bad softlink inode data_len image" soft_link_data_len "danger_default" 1 1 1 1 1 1 0 1 1 "no lost+found"
+fsck_image "bad dentry key image" dentry_key "danger_default" 0 0 0 1 1 1 1 1 1 "no lost+found"
+fsck_image "bad dentry nlen image" dentry_nlen "danger_default" 0 0 0 1 1 1 1 1 1 "no lost+found"
+fsck_image "bad dentry type image" dentry_type "danger_default" 0 0 0 1 1 1 1 1 1 "no lost+found"
+fsck_image "bad xattr inode flags image" xinode_flags "danger_default" 1 1 0 1 1 0 1 1 1 "no lost+found"
+fsck_image "bad xattr inode key image" xinode_key "danger_default" 1 1 0 1 1 0 1 1 1 "no lost+found"
+fsck_image "bad xattr inode mode image" xinode_mode "danger_default" 1 1 0 1 1 0 1 1 1 "no lost+found"
+fsck_image "bad xattr dentry key image" xentry_key "danger_default" 1 1 0 1 1 0 1 1 1 "no lost+found"
+fsck_image "bad xattr dentry nlen image" xentry_nlen "danger_default" 1 1 0 1 1 0 1 1 1 "no lost+found"
+fsck_image "bad xattr dentry type image" xentry_type "danger_default" 1 1 0 1 1 0 1 1 1 "no lost+found"
+fsck_image "bad xattr host image" xent_host "danger_default" 0 0 0 0 0 0 1 1 1 "no lost+found"
+fsck_image "dir has too many dentry image" dir_many_dentry "danger_default" 1 1 1 0 0 0 1 1 1 "no lost+found"
+fsck_image "bad dir image" dir_lost "danger_default" 1 1 1 1 1 1 1 0 1 "lost+found has one"
+fsck_image "bad dir and duplicated file name in lost+found image" dir_lost_duplicated "danger_default" 1 1 1 1 1 1 1 0 1 "lost+found has duplicated files"
+fsck_image "bad dir and lost+found image" dir_lost_not_recover "danger_default" 1 1 1 1 1 1 1 0 1 "lost+found is regular"
+fsck_image "bad root dir image" root_dir "danger_default" 0 0 0 0 0 0 0 0 1 "lost+found has two"
+fsck_image "empty TNC image" empty_tnc "danger_rebuild" 0 0 0 0 0 0 0 0 1 "no lost+found"
+end_t=$(date +%s)
+time_cost=$(( end_t - start_t ))
+echo "Success, cost $time_cost seconds"
+exit 0
diff --git a/tests/ubifs_tools-tests/fsck_tests/powercut_fsck_mount.sh.in b/tests/ubifs_tools-tests/fsck_tests/powercut_fsck_mount.sh.in
new file mode 100755
index 0000000..ac7128b
--- /dev/null
+++ b/tests/ubifs_tools-tests/fsck_tests/powercut_fsck_mount.sh.in
@@ -0,0 +1,148 @@
+#!/bin/sh
+# Copyright (c), 2024, Huawei Technologies Co, Ltd.
+# Author: Zhihao Cheng <chengzhihao1@huawei.com>
+#
+# Test Description:
+# For many kinds of flash, do following things
+# 1. mount UBIFS
+# 2. fsstress & powercut & unmount
+# 3. fsck UBIFS
+# 4. check UBIFS mounting result
+# Running time: 1h
+
+TESTBINDIR=@TESTBINDIR@
+source $TESTBINDIR/common.sh
+
+function run_test()
+{
+ local simulator="$1";
+ local size="$2";
+ local peb_size="$3";
+ local page_size="$4";
+ local encryption=$5;
+
+ echo "======================================================================"
+ printf "%s" "$simulator: ${size}MiB PEB size ${peb_size}KiB"
+ if [ "$simulator" = "nandsim" ]; then
+ printf " %s" "page size ${page_size}Bytes"
+ fi
+ printf " $encryption\n"
+
+ if [ "$simulator" = "nandsim" ]; then
+ $TESTBINDIR/load_nandsim.sh "$size" "$peb_size" "$page_size" || echo "cannot load nandsim";
+ mtdnum="$(find_mtd_device "$nandsim_patt")"
+ elif [ "$simulator" = "mtdram" ]; then
+ load_mtdram "$size" "$peb_size" || echo "cannot load mtdram"
+ mtdnum="$(find_mtd_device "$mtdram_patt")"
+ else
+ fatal "$simulator is not supported"
+ fi
+
+ flash_eraseall /dev/mtd$mtdnum
+ modprobe ubi mtd="$mtdnum,$page_size" || fatal "modprobe ubi fail"
+ ubimkvol -N vol_test -m -n 0 /dev/ubi$UBI_NUM || fatal "mkvol fail"
+ modprobe ubifs || fatal "modprobe ubifs fail"
+ mount_ubifs $DEV $MNT || fatal "mount ubifs fail"
+ if [[ "$encryption" == "encrypted" ]]; then
+ encryption_gen_key
+ encryption_set_key $MNT
+ fi
+
+ fsstress -d $MNT -l0 -p4 -n10000 &
+ sleep $((RANDOM % 120))
+ powercut
+
+ ps -e | grep -w fsstress > /dev/null 2>&1
+ while [ $? -eq 0 ]
+ do
+ killall -9 fsstress > /dev/null 2>&1
+ sleep 1
+ ps -e | grep -w fsstress > /dev/null 2>&1
+ done
+
+ while true
+ do
+ res=`mount | grep "$MNT"`
+ if [[ "$res" == "" ]]
+ then
+ break;
+ fi
+ umount $MNT
+ sleep 0.1
+ done
+
+ fsck.ubifs -a $DEV 2>&1 > $LOG_FILE
+ res=$?
+ cat $LOG_FILE
+ if [[ $res != $FSCK_OK ]]
+ then
+ # Powercut during layout_leb_in_gaps may change index LEBs
+ # without updating LPT.
+ idx_log=`cat $LOG_FILE | grep "Inconsistent properties" | grep "is_idx 1"`
+ # The lpt nodes could be parsed incorrectly because the lpt disk
+ # layout is too simple. See details in
+ # https://lore.kernel.org/linux-mtd/97ca7fe4-4ad4-edd1-e97a-1d540aeabe2d@huawei.com/
+ lpt_log=`cat $LOG_FILE | grep "dbg_check_ltab_lnum: invalid empty space in LEB"`
+ if [[ "$idx_log" == "" ]] && [[ "$lpt_log" == "" ]]; then
+ fatal "fsck fail $res"
+ fi
+ if [[ $res != $FSCK_NONDESTRUCT ]]; then
+ fatal "fsck fail $res"
+ fi
+ fi
+
+ dmesg -c > /dev/null # powercut could reproduce error messages
+
+ enable_chkfs
+
+ mount_ubifs $DEV $MNT "noauthentication" "noatime"
+ res=$?
+ if [[ $res != 0 ]]
+ then
+ fatal "mount fail $res"
+ fi
+
+ if [[ "$encryption" == "encrypted" ]]; then
+ encryption_set_key $MNT
+ fi
+
+ du -sh $MNT > /dev/null # Make sure all files are accessible
+ ret=$?
+ if [[ $ret != 0 ]]; then
+ fatal "Cannot access all files"
+ fi
+ check_err_msg
+
+ umount $MNT
+ res=$?
+ if [[ $res != 0 ]]
+ then
+ fatal "unmount fail $res"
+ fi
+
+ check_err_msg
+
+ modprobe -r ubifs
+ modprobe -r ubi
+ modprobe -r $simulator
+
+ echo "----------------------------------------------------------------------"
+}
+
+check_fsstress
+start_t=$(date +%s)
+echo "Do powercut+fsck+mount test in kinds of flashes"
+for simulator in "mtdram" "nandsim"; do
+ for encryption in "encrypted" "noencrypted"; do
+ run_test "$simulator" "16" "16" "512" $encryption
+ run_test "$simulator" "64" "16" "512" $encryption
+ run_test "$simulator" "128" "64" "2048" $encryption
+ run_test "$simulator" "256" "128" "2048" $encryption
+ run_test "$simulator" "512" "128" "2048" $encryption
+ run_test "$simulator" "1024" "512" "2048" $encryption
+ done
+done
+end_t=$(date +%s)
+time_cost=$(( end_t - start_t ))
+echo "Success, cost $time_cost seconds"
+exit 0
diff --git a/tests/ubifs_tools-tests/fsck_tests/random_corrupted_fsck.sh.in b/tests/ubifs_tools-tests/fsck_tests/random_corrupted_fsck.sh.in
new file mode 100755
index 0000000..64760c9
--- /dev/null
+++ b/tests/ubifs_tools-tests/fsck_tests/random_corrupted_fsck.sh.in
@@ -0,0 +1,206 @@
+#!/bin/sh
+# Copyright (c), 2024, Huawei Technologies Co, Ltd.
+# Author: Zhihao Cheng <chengzhihao1@huawei.com>
+#
+# Test Description:
+# For many kinds of flash, do following things
+# 1. mount UBIFS
+# 2. fsstress && unmount
+# 3. inject corruption into UBIFS image randomly
+# 3. fsck UBIFS
+# 4. check UBIFS mounting result
+# Running time: 1h
+
+TESTBINDIR=@TESTBINDIR@
+source $TESTBINDIR/common.sh
+
+function run_test()
+{
+ local simulator="$1";
+ local size="$2";
+ local peb_size="$3";
+ local page_size="$4";
+ local encryption=$5;
+
+ echo "======================================================================"
+ printf "%s" "$simulator: ${size}MiB PEB size ${peb_size}KiB"
+ if [ "$simulator" = "nandsim" ]; then
+ printf " %s" "page size ${page_size}Bytes"
+ fi
+ printf " $encryption\n"
+
+ if [ "$simulator" = "nandsim" ]; then
+ $TESTBINDIR/load_nandsim.sh "$size" "$peb_size" "$page_size" || echo "cannot load nandsim";
+ mtdnum="$(find_mtd_device "$nandsim_patt")"
+ elif [ "$simulator" = "mtdram" ]; then
+ load_mtdram "$size" "$peb_size" || echo "cannot load mtdram"
+ mtdnum="$(find_mtd_device "$mtdram_patt")"
+ else
+ fatal "$simulator is not supported"
+ fi
+
+ flash_eraseall /dev/mtd$mtdnum
+ modprobe ubi mtd="$mtdnum,$page_size" || fatal "modprobe ubi fail"
+ ubimkvol -N vol_test -m -n 0 /dev/ubi$UBI_NUM || fatal "mkvol fail"
+ modprobe ubifs || fatal "modprobe ubifs fail"
+ mount_ubifs $DEV $MNT || fatal "mount ubifs fail"
+ if [[ "$encryption" == "encrypted" ]]; then
+ encryption_gen_key
+ encryption_set_key $MNT
+ fi
+
+ fsstress -d $MNT -l0 -p4 -n10000 &
+
+ while true;
+ do
+ per=`df -Th | grep ubifs | awk '{print $6}'`;
+ if [[ ${per%?} -gt 95 ]]; then
+ # Used > 95%
+ break;
+ fi
+ done
+
+ ps -e | grep -w fsstress > /dev/null 2>&1
+ while [ $? -eq 0 ]
+ do
+ killall -9 fsstress > /dev/null 2>&1
+ sleep 1
+ ps -e | grep -w fsstress > /dev/null 2>&1
+ done
+
+ while true
+ do
+ res=`mount | grep "$MNT"`
+ if [[ "$res" == "" ]]
+ then
+ break;
+ fi
+ umount $MNT
+ sleep 0.1
+ done
+
+ # injection
+ times=$((RANDOM % 10))
+ let times=$times+10
+ i=0
+ tot_peb=`cat /sys/class/ubi/ubi$UBI_NUM/total_eraseblocks`;
+
+ modprobe -r ubifs
+ modprobe -r ubi # Stop wear-leveling & erasing worker
+ while [[ $i -lt $times ]]
+ do
+ let i=$i+1;
+ peb=$((RANDOM % $tot_peb));
+ pg=`expr $peb_size \* 1024`;
+ peb_off=`expr $pg \* $peb`
+ pages=`expr $pg / $page_size`;
+ pg=`expr $pages - 2`;
+ pg=$((RANDOM % $pg));
+ pg_off=`expr $pg + 2`;
+ pg_start=`expr $pages \* $peb`;
+ pg=`expr $pg_start + $pg_off`;
+ vid_pg=`expr $pg_start + 1`;
+ dd if=/dev/mtd$mtdnum of=$TMP_FILE bs=$page_size skip=$vid_pg count=1 2>/dev/null;
+ content=`cat $TMP_FILE | grep UBI!`; # vid header magic
+ if [[ "$content" == "" ]]; then
+ # Skip free PEB, otherwise data could be overwritten in UBIFS
+ continue;
+ fi
+ if [[ $((RANDOM % 2)) == 0 ]]; then
+ # Corrupts 1 page
+ dd if=/dev/urandom of=/dev/mtd$mtdnum bs=$page_size seek=$pg count=1;
+ else
+ # Erase 1 LEB, TNC points to an unmapped area
+ flash_erase /dev/mtd$mtdnum $peb_off 1
+ fi
+ done
+ rm -f $TMP_FILE 2>/dev/null
+ sync
+
+ skip=0
+ modprobe ubi mtd="$mtdnum,$page_size"
+ ret=$?
+ if [[ $ret != 0 ]]
+ then
+ skip=1
+ echo "UBI layout volume is corrupted, skip"
+ fi
+
+ if [[ $skip == 0 ]]; then
+ modprobe ubifs || fatal "modprobe ubifs2 fail"
+ dmesg -c > /dev/null
+ fsck.ubifs -yb $DEV 2>&1 > $LOG_FILE
+ res=$?
+ cat $LOG_FILE
+ let "ret=$res&~$FSCK_NONDESTRUCT"
+ if [[ $ret != $FSCK_OK ]]
+ then
+ # Skip superblock error
+ log=`cat $LOG_FILE | grep "bad node at LEB 0:"`
+ if [[ "$log" != "" ]]
+ then
+ skip=1
+ echo "SB is corrupted, skip fsck & mounting"
+ else
+ fatal "fsck fail $res"
+ fi
+ fi
+
+ if [[ $skip == 0 ]]; then
+ enable_chkfs
+
+ mount_ubifs $DEV $MNT "noauthentication" "noatime"
+ res=$?
+ if [[ $res != 0 ]]
+ then
+ fatal "mount fail $res"
+ fi
+
+ if [[ "$encryption" == "encrypted" ]]; then
+ # Ignore the encrypting error, root dir could be
+ # corrupted, the new root dir cannot be
+ # encrypted because it is not empty.
+ encryption_set_key $MNT 1
+ fi
+
+ du -sh $MNT > /dev/null # Make sure all files are accessible
+ ret=$?
+ if [[ $ret != 0 ]]; then
+ fatal "Cannot access all files"
+ fi
+ # check_err_msg is not suggested in this testcase, because
+ # ubi_io_read(triggered by wear_leveling_worker -> ubi_eba_copy_leb)
+ # could print stack if ecc uncorrectable errors are detected.
+
+ umount $MNT
+ res=$?
+ if [[ $res != 0 ]]
+ then
+ fatal "unmount fail $res"
+ fi
+ fi
+ modprobe -r ubifs
+ modprobe -r ubi
+ fi
+ modprobe -r $simulator
+
+ echo "----------------------------------------------------------------------"
+}
+
+check_fsstress
+start_t=$(date +%s)
+echo "Do random_corrruption+fsck+mount test in kinds of flashes"
+for simulator in "mtdram" "nandsim"; do
+ for encryption in "encrypted" "noencrypted"; do
+ run_test "$simulator" "16" "16" "512" $encryption
+ run_test "$simulator" "64" "16" "512" $encryption
+ run_test "$simulator" "128" "64" "2048" $encryption
+ run_test "$simulator" "256" "128" "2048" $encryption
+ run_test "$simulator" "512" "128" "2048" $encryption
+ run_test "$simulator" "1024" "512" "2048" $encryption
+ done
+done
+end_t=$(date +%s)
+time_cost=$(( end_t - start_t ))
+echo "Success, cost $time_cost seconds"
+exit 0