xfs: test handling of invalid inode modes
[xfstests-dev.git] / tests / xfs / 348
1 #! /bin/bash
2 # FSQA Test No. 348
3 #
4 # Test handling of invalid inode modes
5 #
6 # Set all possible file type values for different types of files
7 # and verify that xfs_repair detects the correct errors.
8 #
9 #-----------------------------------------------------------------------
10 #
11 # Copyright (C) 2016 CTERA Networks. All Rights Reserved.
12 # Author: Amir Goldstein <amir73il@gmail.com>
13 #
14 # This program is free software; you can redistribute it and/or
15 # modify it under the terms of the GNU General Public License as
16 # published by the Free Software Foundation.
17 #
18 # This program is distributed in the hope that it would be useful,
19 # but WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 # GNU General Public License for more details.
22 #
23 # You should have received a copy of the GNU General Public License
24 # along with this program; if not, write the Free Software Foundation,
25 # Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
26 #-----------------------------------------------------------------------
27 #
28
29 seq=`basename $0`
30 seqres=$RESULT_DIR/$seq
31 echo "QA output created by $seq"
32
33 tmp=/tmp/$$
34 status=1        # failure is the default!
35 trap "_cleanup; exit \$status" 0 1 2 3 15
36
37 _cleanup()
38 {
39         rm -f $tmp.*
40 }
41
42 # get standard environment, filters and checks
43 . ./common/rc
44 . ./common/filter
45 . ./common/repair
46
47 # real QA test starts here
48 _supported_fs xfs
49 _supported_os Linux
50 _require_scratch
51
52 rm -f $seqres.full
53
54 _scratch_mkfs >>$seqres.full 2>&1
55
56 _scratch_mount
57
58 # Create our test files.
59 testdir=$SCRATCH_MNT/test
60 mkdir -p $testdir
61 mkdir $testdir/DIR
62 echo 123 > $testdir/DATA
63 touch $testdir/EMPTY
64 ln -s $testdir/DATA $testdir/SYMLINK
65 mknod $testdir/CHRDEV c 1 1
66 mknod $testdir/BLKDEV b 1 1
67 mknod $testdir/FIFO p
68
69 xfs_info $SCRATCH_MNT | grep -q "ftype=1" && FTYPE_FEATURE=1
70
71 # Record test dir inode for xfs_repair filter
72 inode_filter=$tmp.sed
73 rm -f $inode_filter
74 pino=$(ls -id $testdir | awk '{print $1}')
75 echo "s/inode $pino/PARENT_INO/" >> $inode_filter
76 echo "s/directory $pino/directory PARENT_INO/" >> $inode_filter
77
78 inodes=""
79 # Record inode numbers for xfs_db commands and xfs_repair filter
80 for f in DIR DATA EMPTY SYMLINK CHRDEV BLKDEV FIFO; do
81         ino=$(ls -id $testdir/$f | awk '{print $1}')
82         inodes="$inodes $ino"
83         echo "s/inode $ino/${f}_INO/" >> $inode_filter
84 done
85
86 _scratch_unmount
87
88 # Possible mode file type values (mode & S_IFMT) >> 12
89 dtypes="0 1 2 3 4 5 6 7 10 11 12 13 14 15 16 17"
90 echo "===== Find inode by file type:"
91 for dt in $dtypes; do
92         # Set all our test files to dt value
93         for ino in $inodes; do
94                 _scratch_xfs_db -c "inode $ino" -c "print core.mode" | \
95                         grep -q "0${dt}0...$" && \
96                         (echo "dt=$dt => inode $ino" | sed -f $inode_filter)
97         done
98 done
99 for dt in $dtypes; do
100         echo
101         echo "===== Setting dt=$dt to all files:"
102         # Set all our test files to dt value
103         for ino in $inodes; do
104                 _scratch_xfs_db -x -c "inode $ino" -c "write core.mode 0${dt}0644"
105         done
106         # Repair should detect the inconsistencies
107         # For invalid dt values, all files would have been junked.
108         # For valid dt values, one test file is expected to be valid.
109         # The rest would either have wrong format or non matching dir ftype.
110         _scratch_xfs_repair -n 2>&1  | tee -a $seqres.full | \
111                 _filter_repair | grep "^would have junked" | sed -f $inode_filter | sort -u
112         # If ftype feature is enabled, when setting file type to one of the
113         # special types (i.e. FIFO(1), CHRDEV(2),BLKDEV(6),SOCKET(14)),
114         # xfs_repair is expected to detect ftype mismatch error. Otherewise,
115         # xfs_repair is not expected to detect ftype mismatch error.
116         if [ "$FTYPE_FEATURE" = 1 ] && (echo ':1:2:6:14:' | grep -q ":$dt:"); then
117                 _scratch_xfs_repair -n 2>&1 | grep -q "^would fix ftype mismatch" || \
118                         echo "xfs_repair should fix ftype mismatch"
119         else
120                 _scratch_xfs_repair -n 2>&1 | grep -q -v "^would fix ftype mismatch" || \
121                         echo "xfs_repair should not fix ftype mismatch"
122         fi
123 done
124
125 # Repair should detect and junk all test files
126 _scratch_xfs_repair 2>&1 >> $seqres.full 2>&1 || _fail "xfs_repair should not fail"
127
128 status=0
129 exit