generic/395: remove workarounds for wrong error codes
[xfstests-dev.git] / tests / generic / 003
1 #! /bin/bash
2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (c) 2014, Oracle and/or its affiliates.  All Rights Reserved.
4 #
5 # FS QA Test No. generic/003
6 #
7 # Tests the noatime, relatime, strictatime and nodiratime mount options.
8 # There is an extra check for Btrfs to ensure that the access time is
9 # never updated on read-only subvolumes. (Regression test for bug fixed
10 # with commit 93fd63c2f001ca6797c6b15b696a484b165b4800)
11 #
12 seq=`basename $0`
13 seqres=$RESULT_DIR/$seq
14 echo "QA output created by $seq"
15
16 here=`pwd`
17 tmp=/tmp/$$
18 status=1        # failure is the default!
19 trap "_cleanup; exit \$status" 0 1 2 3 15
20
21 _cleanup()
22 {
23     cd /
24     rm -rf $tmp.*
25 }
26
27 # get standard environment, filters and checks
28 . ./common/rc
29 . ./common/filter
30
31 # real QA test starts here
32
33 _supported_fs generic
34 _require_scratch
35 _require_atime
36 _require_relatime
37
38 rm -f $seqres.full
39
40 _stat() {
41         stat -c "%x;%y;%z" $1
42 }
43
44 _compare_stat_times() {
45         updated=$1      # 3 chars indicating if access, modify and
46                         # change times should be updated (Y) or not (N)
47         IFS=';' read -a first_stat <<< "$2"   # Convert first stat to array
48         IFS=';' read -a second_stat <<< "$3"  # Convert second stat to array
49         test_step=$4    # Will be printed to output stream in case of an
50                         # error, to make debugging easier
51         types=( access modify change )
52
53         for i in 0 1 2; do
54                 if [ "${first_stat[$i]}" == "${second_stat[$i]}" ]; then
55                         if [ "${updated:$i:1}" == "N" ]; then
56                                 continue;
57                         fi
58                         echo -n "ERROR: ${types[$i]} time has not been updated "
59                         echo $test_step
60                 elif [ "${updated:$i:1}" == "N" ]; then
61                         echo -n "ERROR: ${types[$i]} time has changed "
62                         echo $test_step
63                 fi
64         done
65 }
66
67 _scratch_mkfs >> $seqres.full 2>&1 || _fail "mkfs failed"
68 _scratch_mount "-o relatime"
69
70 if [ "$FSTYP" = "btrfs" ]; then
71         TPATH=$SCRATCH_MNT/sub1
72         $BTRFS_UTIL_PROG subvolume create $TPATH > $seqres.full
73 else
74         TPATH=$SCRATCH_MNT
75 fi
76
77 mkdir $TPATH/dir1
78 echo "aaa" > $TPATH/dir1/file1
79 file1_stat_before_first_access=`_stat $TPATH/dir1/file1`
80
81 # Accessing file1 the first time
82 sleep 1
83 cat $TPATH/dir1/file1 > /dev/null
84 file1_stat_after_first_access=`_stat $TPATH/dir1/file1`
85 _compare_stat_times YNN "$file1_stat_before_first_access" \
86         "$file1_stat_after_first_access" "after accessing file1 first time"
87
88 # Accessing file1 a second time
89 sleep 1
90 cat $TPATH/dir1/file1 > /dev/null
91 file1_stat_after_second_access=`_stat $TPATH/dir1/file1`
92 _compare_stat_times NNN "$file1_stat_after_first_access" \
93         "$file1_stat_after_second_access" "after accessing file1 second time"
94
95 # Mounting with nodiratime option
96 _scratch_cycle_mount "nodiratime"
97 file1_stat_after_remount=`_stat $TPATH/dir1/file1`
98 _compare_stat_times NNN "$file1_stat_after_second_access" \
99         "$file1_stat_after_remount" "for file1 after remount"
100
101 # Creating dir2 and file2, checking directory stats
102 mkdir $TPATH/dir2
103 dir2_stat_before_file_creation=`_stat $TPATH/dir2`
104 sleep 1
105 echo "bbb" > $TPATH/dir2/file2
106 dir2_stat_after_file_creation=`_stat $TPATH/dir2`
107 _compare_stat_times NYY "$dir2_stat_before_file_creation" \
108         "$dir2_stat_after_file_creation" "for dir2 after file creation"
109
110 # Accessing file2
111 file2_stat_before_first_access=`_stat $TPATH/dir2/file2`
112 sleep 1
113 cat $TPATH/dir2/file2 > /dev/null
114 file2_stat_after_first_access=`_stat $TPATH/dir2/file2`
115 _compare_stat_times YNN "$file2_stat_before_first_access" \
116         "$file2_stat_after_first_access" "after accessing file2"
117 dir2_stat_after_file_access=`_stat $TPATH/dir2`
118 _compare_stat_times NNN "$dir2_stat_after_file_creation" \
119         "$dir2_stat_after_file_access" "for dir2 after file access"
120
121 # Mounting with noatime option, creating a file and accessing it
122 _scratch_cycle_mount "noatime"
123 echo "ccc" > $TPATH/dir2/file3
124 file3_stat_before_first_access=`_stat $TPATH/dir2/file3`
125 sleep 1
126 cat $TPATH/dir2/file3 > /dev/null
127 file3_stat_after_first_access=`_stat $TPATH/dir2/file3`
128 _compare_stat_times NNN "$file3_stat_before_first_access" \
129         "$file3_stat_after_first_access" "after accessing file3 first time"
130
131 # Checking that the modify and change times are still updated
132 file1_stat_before_modify=`_stat $TPATH/dir1/file1`
133 sleep 1
134 echo "xyz" > $TPATH/dir1/file1
135 file1_stat_after_modify=`_stat $TPATH/dir1/file1`
136 _compare_stat_times NYY "$file1_stat_before_modify" \
137         "$file1_stat_after_modify" "after modifying file1"
138 sleep 1
139 mv $TPATH/dir1/file1 $TPATH/dir1/file1_renamed
140 file1_stat_after_change=`_stat $TPATH/dir1/file1_renamed`
141 _compare_stat_times NNY "$file1_stat_after_modify" \
142         "$file1_stat_after_change" "after changing file1"
143
144 # Mounting with strictatime option and
145 # accessing a previously created file twice
146 _scratch_cycle_mount "strictatime"
147 cat $TPATH/dir2/file3 > /dev/null
148 file3_stat_after_second_access=`_stat $TPATH/dir2/file3`
149 _compare_stat_times YNN "$file3_stat_after_first_access" \
150         "$file3_stat_after_second_access" "after accessing file3 second time"
151 sleep 1
152 cat $TPATH/dir2/file3 > /dev/null
153 file3_stat_after_third_access=`_stat $TPATH/dir2/file3`
154 _compare_stat_times YNN "$file3_stat_after_second_access" \
155         "$file3_stat_after_third_access" "after accessing file3 third time"
156
157 # Btrfs only: Creating readonly snapshot. Access time should never
158 # be updated, even when the strictatime mount option is active
159 if [ "$FSTYP" = "btrfs" ]; then
160         SPATH=$SCRATCH_MNT/snap1
161         btrfs subvol snapshot -r $TPATH $SPATH >> $seqres.full
162         dir2_stat_readonly_before_access=`_stat $SPATH/dir2`
163         sleep 1
164         ls $SPATH/dir2 >> $seqres.full
165         cat $SPATH/dir2/file3 >> $seqres.full
166         dir2_stat_readonly_after_access=`_stat $SPATH/dir2`
167         _compare_stat_times NNN "$dir2_stat_readonly_before_access" \
168                 "$dir2_stat_readonly_after_access" "for dir in readonly subvol"
169         file3_stat_readonly_after_access=`_stat $SPATH/dir2/file3`
170         _compare_stat_times NNN "$file3_stat_after_third_access" \
171                 "$file3_stat_readonly_after_access" "for file in readonly subvol"
172 fi
173
174 # Mounting read-only. Access time should never be updated, despite the
175 # strictatime mount option.
176 sleep 1
177 dir2_stat_before_ro_mount=`_stat $TPATH/dir2`
178 file3_stat_before_ro_mount=`_stat $TPATH/dir2/file3`
179 _scratch_cycle_mount "ro,strictatime"
180 ls $TPATH/dir2 > /dev/null
181 cat $TPATH/dir2/file3 > /dev/null
182 dir2_stat_after_ro_mount=`_stat $TPATH/dir2`
183 _compare_stat_times NNN "$dir2_stat_before_ro_mount" \
184         "$dir2_stat_after_ro_mount" "for dir in read-only filesystem"
185 file3_stat_after_ro_mount=`_stat $TPATH/dir2/file3`
186 _compare_stat_times NNN "$file3_stat_before_ro_mount" \
187         "$file3_stat_after_ro_mount" "for file in read-only filesystem"
188
189 # success, all done
190 _scratch_unmount
191 echo "Silence is golden"
192 status=0
193 exit