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