fstests: move test group info to test files
[xfstests-dev.git] / tests / xfs / 348
1 #! /bin/bash
2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (C) 2016 CTERA Networks. All Rights Reserved.
4 #
5 # FSQA Test No. 348
6 #
7 # Test handling of invalid inode modes
8 #
9 # Set all possible file type values for different types of files
10 # and verify that xfs_repair detects the correct errors.
11 #
12 . ./common/preamble
13 _begin_fstest auto quick fuzzers repair
14
15 # Import common functions.
16 . ./common/filter
17 . ./common/repair
18
19 # real QA test starts here
20 _supported_fs xfs
21 _require_scratch
22
23 # This test will corrupt fs intentionally, so there will be WARNINGs
24 # in dmesg as expected
25 _disable_dmesg_check
26
27 _scratch_mkfs >>$seqres.full 2>&1
28
29 _scratch_mount
30
31 # Create our test files.
32 testdir=$SCRATCH_MNT/test
33 mkdir -p $testdir
34 mkdir $testdir/DIR
35 echo 123 > $testdir/DATA
36 touch $testdir/EMPTY
37 ln -s $testdir/DATA $testdir/SYMLINK
38 mknod $testdir/CHRDEV c 1 1
39 mknod $testdir/BLKDEV b 1 1
40 mknod $testdir/FIFO p
41
42 $XFS_INFO_PROG $SCRATCH_MNT | grep -q "ftype=1" && FTYPE_FEATURE=1
43
44 # Record test dir inode for xfs_repair filter
45 inode_filter=$tmp.sed
46 rm -f $inode_filter
47 pino=$(ls -id $testdir | awk '{print $1}')
48 echo "s/inode $pino/PARENT_INO/" >> $inode_filter
49 echo "s/directory $pino/directory PARENT_INO/" >> $inode_filter
50
51 inodes=""
52 # Record inode numbers for xfs_db commands and xfs_repair filter
53 for f in DIR DATA EMPTY SYMLINK CHRDEV BLKDEV FIFO; do
54         ino=$(ls -id $testdir/$f | awk '{print $1}')
55         inodes="$inodes $ino"
56         echo "s/inode $ino/${f}_INO/" >> $inode_filter
57 done
58
59 _scratch_unmount
60
61 # Possible mode file type values (mode & S_IFMT) >> 12
62 dtypes="0 1 2 3 4 5 6 7 10 11 12 13 14 15 16 17"
63 echo "===== Find inode by file type:"
64 for dt in $dtypes; do
65         # Set all our test files to dt value
66         for ino in $inodes; do
67                 _scratch_xfs_db -c "inode $ino" -c "print core.mode" | \
68                         grep -q "0${dt}0...$" && \
69                         (echo "dt=$dt => inode $ino" | sed -f $inode_filter)
70         done
71 done
72 for dt in $dtypes; do
73         echo
74         echo "===== Setting dt=$dt to all files:"
75         # Set all our test files to dt value
76         for ino in $inodes; do
77                 _scratch_xfs_db -x -c "inode $ino" -c "write core.mode 0${dt}0644"
78         done
79         # Repair should detect the inconsistencies
80         # For invalid dt values, all files would have been junked.
81         # For valid dt values, one test file is expected to be valid.
82         # The rest would either have wrong format or non matching dir ftype.
83         _scratch_xfs_repair -n 2>&1  | tee -a $seqres.full | \
84                 _filter_repair | grep "^would have junked" | sed -f $inode_filter | sort -u
85         # If ftype feature is enabled, when setting file type to one of the
86         # special types (i.e. FIFO(1), CHRDEV(2),BLKDEV(6),SOCKET(14)),
87         # xfs_repair is expected to detect ftype mismatch error. Otherewise,
88         # xfs_repair is not expected to detect ftype mismatch error.
89         if [ "$FTYPE_FEATURE" = 1 ] && (echo ':1:2:6:14:' | grep -q ":$dt:"); then
90                 _scratch_xfs_repair -n 2>&1 | grep -q "^would fix ftype mismatch" || \
91                         echo "xfs_repair should fix ftype mismatch"
92         else
93                 _scratch_xfs_repair -n 2>&1 | grep -q -v "^would fix ftype mismatch" || \
94                         echo "xfs_repair should not fix ftype mismatch"
95         fi
96
97         _scratch_mount
98         for file in DIR DATA EMPTY SYMLINK CHRDEV BLKDEV FIFO; do
99                 rm -f $tmp.stat.err
100                 ftype=$(stat --printf=%F $testdir/$file 2>$tmp.stat.err)
101                 if [ -s $tmp.stat.err ]; then
102                         cat $tmp.stat.err | _filter_stat
103                 else
104                         echo "stat: '$testdir/$file' is a $ftype"
105                         # Verify that readlink of a file posing as a symlink
106                         # and ls of a file posing as a directory does not blow up.
107                         # NOTE that ls DOES ASSERT with kernel 4.9 and XFS_DEBUG=y
108                         # on malformed directory
109                         if [ -d $testdir/$file ]; then
110                                 ls $testdir/$file &> /dev/null
111                         elif [ -h $testdir/$file ]; then
112                                 readlink $testdir/$file &> /dev/null
113                         fi
114                 fi | _filter_scratch
115         done
116         _scratch_unmount
117 done
118
119 # Repair should detect and junk all test files
120 _scratch_xfs_repair 2>&1 >> $seqres.full 2>&1 || _fail "xfs_repair should not fail"
121
122 status=0
123 exit