fstests: check for filesystem FS_IOC_FSSETXATTR support
[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 seq=`basename $0`
13 seqres=$RESULT_DIR/$seq
14 echo "QA output created by $seq"
15
16 tmp=/tmp/$$
17 status=1        # failure is the default!
18 trap "_cleanup; exit \$status" 0 1 2 3 15
19
20 _cleanup()
21 {
22         rm -f $tmp.*
23 }
24
25 # get standard environment, filters and checks
26 . ./common/rc
27 . ./common/filter
28 . ./common/repair
29
30 # real QA test starts here
31 _supported_fs xfs
32 _supported_os Linux
33 _require_scratch
34
35 # This test will corrupt fs intentionally, so there will be WARNINGs
36 # in dmesg as expected
37 _disable_dmesg_check
38
39 rm -f $seqres.full
40
41 _scratch_mkfs >>$seqres.full 2>&1
42
43 _scratch_mount
44
45 # Create our test files.
46 testdir=$SCRATCH_MNT/test
47 mkdir -p $testdir
48 mkdir $testdir/DIR
49 echo 123 > $testdir/DATA
50 touch $testdir/EMPTY
51 ln -s $testdir/DATA $testdir/SYMLINK
52 mknod $testdir/CHRDEV c 1 1
53 mknod $testdir/BLKDEV b 1 1
54 mknod $testdir/FIFO p
55
56 $XFS_INFO_PROG $SCRATCH_MNT | grep -q "ftype=1" && FTYPE_FEATURE=1
57
58 # Record test dir inode for xfs_repair filter
59 inode_filter=$tmp.sed
60 rm -f $inode_filter
61 pino=$(ls -id $testdir | awk '{print $1}')
62 echo "s/inode $pino/PARENT_INO/" >> $inode_filter
63 echo "s/directory $pino/directory PARENT_INO/" >> $inode_filter
64
65 inodes=""
66 # Record inode numbers for xfs_db commands and xfs_repair filter
67 for f in DIR DATA EMPTY SYMLINK CHRDEV BLKDEV FIFO; do
68         ino=$(ls -id $testdir/$f | awk '{print $1}')
69         inodes="$inodes $ino"
70         echo "s/inode $ino/${f}_INO/" >> $inode_filter
71 done
72
73 _scratch_unmount
74
75 # Possible mode file type values (mode & S_IFMT) >> 12
76 dtypes="0 1 2 3 4 5 6 7 10 11 12 13 14 15 16 17"
77 echo "===== Find inode by file type:"
78 for dt in $dtypes; do
79         # Set all our test files to dt value
80         for ino in $inodes; do
81                 _scratch_xfs_db -c "inode $ino" -c "print core.mode" | \
82                         grep -q "0${dt}0...$" && \
83                         (echo "dt=$dt => inode $ino" | sed -f $inode_filter)
84         done
85 done
86 for dt in $dtypes; do
87         echo
88         echo "===== Setting dt=$dt to all files:"
89         # Set all our test files to dt value
90         for ino in $inodes; do
91                 _scratch_xfs_db -x -c "inode $ino" -c "write core.mode 0${dt}0644"
92         done
93         # Repair should detect the inconsistencies
94         # For invalid dt values, all files would have been junked.
95         # For valid dt values, one test file is expected to be valid.
96         # The rest would either have wrong format or non matching dir ftype.
97         _scratch_xfs_repair -n 2>&1  | tee -a $seqres.full | \
98                 _filter_repair | grep "^would have junked" | sed -f $inode_filter | sort -u
99         # If ftype feature is enabled, when setting file type to one of the
100         # special types (i.e. FIFO(1), CHRDEV(2),BLKDEV(6),SOCKET(14)),
101         # xfs_repair is expected to detect ftype mismatch error. Otherewise,
102         # xfs_repair is not expected to detect ftype mismatch error.
103         if [ "$FTYPE_FEATURE" = 1 ] && (echo ':1:2:6:14:' | grep -q ":$dt:"); then
104                 _scratch_xfs_repair -n 2>&1 | grep -q "^would fix ftype mismatch" || \
105                         echo "xfs_repair should fix ftype mismatch"
106         else
107                 _scratch_xfs_repair -n 2>&1 | grep -q -v "^would fix ftype mismatch" || \
108                         echo "xfs_repair should not fix ftype mismatch"
109         fi
110
111         _scratch_mount
112         for file in DIR DATA EMPTY SYMLINK CHRDEV BLKDEV FIFO; do
113                 rm -f $tmp.stat.err
114                 ftype=$(stat --printf=%F $testdir/$file 2>$tmp.stat.err)
115                 if [ -s $tmp.stat.err ]; then
116                         cat $tmp.stat.err
117                 else
118                         echo "stat: '$testdir/$file' is a $ftype"
119                         # Verify that readlink of a file posing as a symlink
120                         # and ls of a file posing as a directory does not blow up.
121                         # NOTE that ls DOES ASSERT with kernel 4.9 and XFS_DEBUG=y
122                         # on malformed directory
123                         if [ -d $testdir/$file ]; then
124                                 ls $testdir/$file &> /dev/null
125                         elif [ -h $testdir/$file ]; then
126                                 readlink $testdir/$file &> /dev/null
127                         fi
128                 fi | _filter_scratch
129         done
130         _scratch_unmount
131 done
132
133 # Repair should detect and junk all test files
134 _scratch_xfs_repair 2>&1 >> $seqres.full 2>&1 || _fail "xfs_repair should not fail"
135
136 status=0
137 exit