2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (C) 2016 CTERA Networks. All Rights Reserved.
7 # Test handling of invalid inode modes
9 # Set all possible file type values for different types of files
10 # and verify that xfs_repair detects the correct errors.
13 seqres=$RESULT_DIR/$seq
14 echo "QA output created by $seq"
17 status=1 # failure is the default!
18 trap "_cleanup; exit \$status" 0 1 2 3 15
25 # get standard environment, filters and checks
30 # real QA test starts here
34 # This test will corrupt fs intentionally, so there will be WARNINGs
35 # in dmesg as expected
40 _scratch_mkfs >>$seqres.full 2>&1
44 # Create our test files.
45 testdir=$SCRATCH_MNT/test
48 echo 123 > $testdir/DATA
50 ln -s $testdir/DATA $testdir/SYMLINK
51 mknod $testdir/CHRDEV c 1 1
52 mknod $testdir/BLKDEV b 1 1
55 $XFS_INFO_PROG $SCRATCH_MNT | grep -q "ftype=1" && FTYPE_FEATURE=1
57 # Record test dir inode for xfs_repair filter
60 pino=$(ls -id $testdir | awk '{print $1}')
61 echo "s/inode $pino/PARENT_INO/" >> $inode_filter
62 echo "s/directory $pino/directory PARENT_INO/" >> $inode_filter
65 # Record inode numbers for xfs_db commands and xfs_repair filter
66 for f in DIR DATA EMPTY SYMLINK CHRDEV BLKDEV FIFO; do
67 ino=$(ls -id $testdir/$f | awk '{print $1}')
69 echo "s/inode $ino/${f}_INO/" >> $inode_filter
74 # Possible mode file type values (mode & S_IFMT) >> 12
75 dtypes="0 1 2 3 4 5 6 7 10 11 12 13 14 15 16 17"
76 echo "===== Find inode by file type:"
78 # Set all our test files to dt value
79 for ino in $inodes; do
80 _scratch_xfs_db -c "inode $ino" -c "print core.mode" | \
81 grep -q "0${dt}0...$" && \
82 (echo "dt=$dt => inode $ino" | sed -f $inode_filter)
87 echo "===== Setting dt=$dt to all files:"
88 # Set all our test files to dt value
89 for ino in $inodes; do
90 _scratch_xfs_db -x -c "inode $ino" -c "write core.mode 0${dt}0644"
92 # Repair should detect the inconsistencies
93 # For invalid dt values, all files would have been junked.
94 # For valid dt values, one test file is expected to be valid.
95 # The rest would either have wrong format or non matching dir ftype.
96 _scratch_xfs_repair -n 2>&1 | tee -a $seqres.full | \
97 _filter_repair | grep "^would have junked" | sed -f $inode_filter | sort -u
98 # If ftype feature is enabled, when setting file type to one of the
99 # special types (i.e. FIFO(1), CHRDEV(2),BLKDEV(6),SOCKET(14)),
100 # xfs_repair is expected to detect ftype mismatch error. Otherewise,
101 # xfs_repair is not expected to detect ftype mismatch error.
102 if [ "$FTYPE_FEATURE" = 1 ] && (echo ':1:2:6:14:' | grep -q ":$dt:"); then
103 _scratch_xfs_repair -n 2>&1 | grep -q "^would fix ftype mismatch" || \
104 echo "xfs_repair should fix ftype mismatch"
106 _scratch_xfs_repair -n 2>&1 | grep -q -v "^would fix ftype mismatch" || \
107 echo "xfs_repair should not fix ftype mismatch"
111 for file in DIR DATA EMPTY SYMLINK CHRDEV BLKDEV FIFO; do
113 ftype=$(stat --printf=%F $testdir/$file 2>$tmp.stat.err)
114 if [ -s $tmp.stat.err ]; then
115 cat $tmp.stat.err | _filter_stat
117 echo "stat: '$testdir/$file' is a $ftype"
118 # Verify that readlink of a file posing as a symlink
119 # and ls of a file posing as a directory does not blow up.
120 # NOTE that ls DOES ASSERT with kernel 4.9 and XFS_DEBUG=y
121 # on malformed directory
122 if [ -d $testdir/$file ]; then
123 ls $testdir/$file &> /dev/null
124 elif [ -h $testdir/$file ]; then
125 readlink $testdir/$file &> /dev/null
132 # Repair should detect and junk all test files
133 _scratch_xfs_repair 2>&1 >> $seqres.full 2>&1 || _fail "xfs_repair should not fail"