common/rc: Add _require_{chown,chmod}()
[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 if [ "$FSTYP" = "exfat" ]; then
41         # exfat's timestamp for access_time has double seconds granularity
42         access_delay=2.1
43 else
44         access_delay=1
45 fi
46
47 _stat() {
48         stat -c "%x;%y;%z" $1
49 }
50
51 _compare_stat_times() {
52         updated=$1      # 3 chars indicating if access, modify and
53                         # change times should be updated (Y) or not (N)
54         IFS=';' read -a first_stat <<< "$2"   # Convert first stat to array
55         IFS=';' read -a second_stat <<< "$3"  # Convert second stat to array
56         test_step=$4    # Will be printed to output stream in case of an
57                         # error, to make debugging easier
58         types=( access modify change )
59
60         for i in 0 1 2; do
61                 if [ "${first_stat[$i]}" == "${second_stat[$i]}" ]; then
62                         if [ "${updated:$i:1}" == "N" ]; then
63                                 continue;
64                         fi
65                         echo -n "ERROR: ${types[$i]} time has not been updated "
66                         echo $test_step
67                 elif [ "${updated:$i:1}" == "N" ]; then
68                         echo -n "ERROR: ${types[$i]} time has changed "
69                         echo $test_step
70                 fi
71         done
72 }
73
74 _scratch_mkfs >> $seqres.full 2>&1 || _fail "mkfs failed"
75 _scratch_mount "-o relatime"
76
77 if [ "$FSTYP" = "btrfs" ]; then
78         TPATH=$SCRATCH_MNT/sub1
79         $BTRFS_UTIL_PROG subvolume create $TPATH > $seqres.full
80 else
81         TPATH=$SCRATCH_MNT
82 fi
83
84 mkdir $TPATH/dir1
85 echo "aaa" > $TPATH/dir1/file1
86 file1_stat_before_first_access=`_stat $TPATH/dir1/file1`
87
88 # Accessing file1 the first time
89 sleep $access_delay
90 cat $TPATH/dir1/file1 > /dev/null
91 file1_stat_after_first_access=`_stat $TPATH/dir1/file1`
92 _compare_stat_times YNN "$file1_stat_before_first_access" \
93         "$file1_stat_after_first_access" "after accessing file1 first time"
94
95 # Accessing file1 a second time
96 sleep $access_delay
97 cat $TPATH/dir1/file1 > /dev/null
98 file1_stat_after_second_access=`_stat $TPATH/dir1/file1`
99 _compare_stat_times NNN "$file1_stat_after_first_access" \
100         "$file1_stat_after_second_access" "after accessing file1 second time"
101
102 # Mounting with nodiratime option
103 _scratch_cycle_mount "nodiratime"
104 file1_stat_after_remount=`_stat $TPATH/dir1/file1`
105 _compare_stat_times NNN "$file1_stat_after_second_access" \
106         "$file1_stat_after_remount" "for file1 after remount"
107
108 # Creating dir2 and file2, checking directory stats
109 mkdir $TPATH/dir2
110 dir2_stat_before_file_creation=`_stat $TPATH/dir2`
111 sleep 1
112 echo "bbb" > $TPATH/dir2/file2
113 dir2_stat_after_file_creation=`_stat $TPATH/dir2`
114 _compare_stat_times NYY "$dir2_stat_before_file_creation" \
115         "$dir2_stat_after_file_creation" "for dir2 after file creation"
116
117 # Accessing file2
118 file2_stat_before_first_access=`_stat $TPATH/dir2/file2`
119 sleep $access_delay
120 cat $TPATH/dir2/file2 > /dev/null
121 file2_stat_after_first_access=`_stat $TPATH/dir2/file2`
122 _compare_stat_times YNN "$file2_stat_before_first_access" \
123         "$file2_stat_after_first_access" "after accessing file2"
124 dir2_stat_after_file_access=`_stat $TPATH/dir2`
125 _compare_stat_times NNN "$dir2_stat_after_file_creation" \
126         "$dir2_stat_after_file_access" "for dir2 after file access"
127
128 # Mounting with noatime option, creating a file and accessing it
129 _scratch_cycle_mount "noatime"
130 echo "ccc" > $TPATH/dir2/file3
131 file3_stat_before_first_access=`_stat $TPATH/dir2/file3`
132 sleep 1
133 cat $TPATH/dir2/file3 > /dev/null
134 file3_stat_after_first_access=`_stat $TPATH/dir2/file3`
135 _compare_stat_times NNN "$file3_stat_before_first_access" \
136         "$file3_stat_after_first_access" "after accessing file3 first time"
137
138 # Checking that the modify and change times are still updated
139 file1_stat_before_modify=`_stat $TPATH/dir1/file1`
140 sleep 1
141 echo "xyz" > $TPATH/dir1/file1
142 file1_stat_after_modify=`_stat $TPATH/dir1/file1`
143 _compare_stat_times NYY "$file1_stat_before_modify" \
144         "$file1_stat_after_modify" "after modifying file1"
145
146 # exfat does not support last metadata change timestamp
147 if [ "$FSTYP" != "exfat" ]; then
148         sleep 1
149         mv $TPATH/dir1/file1 $TPATH/dir1/file1_renamed
150         file1_stat_after_change=`_stat $TPATH/dir1/file1_renamed`
151         _compare_stat_times NNY "$file1_stat_after_modify" \
152                 "$file1_stat_after_change" "after changing file1"
153 fi
154
155 # Mounting with strictatime option and
156 # accessing a previously created file twice
157 _scratch_cycle_mount "strictatime"
158 cat $TPATH/dir2/file3 > /dev/null
159 file3_stat_after_second_access=`_stat $TPATH/dir2/file3`
160 _compare_stat_times YNN "$file3_stat_after_first_access" \
161         "$file3_stat_after_second_access" "after accessing file3 second time"
162 sleep $access_delay
163 cat $TPATH/dir2/file3 > /dev/null
164 file3_stat_after_third_access=`_stat $TPATH/dir2/file3`
165 _compare_stat_times YNN "$file3_stat_after_second_access" \
166         "$file3_stat_after_third_access" "after accessing file3 third time"
167
168 # Btrfs only: Creating readonly snapshot. Access time should never
169 # be updated, even when the strictatime mount option is active
170 if [ "$FSTYP" = "btrfs" ]; then
171         SPATH=$SCRATCH_MNT/snap1
172         btrfs subvol snapshot -r $TPATH $SPATH >> $seqres.full
173         dir2_stat_readonly_before_access=`_stat $SPATH/dir2`
174         sleep 1
175         ls $SPATH/dir2 >> $seqres.full
176         cat $SPATH/dir2/file3 >> $seqres.full
177         dir2_stat_readonly_after_access=`_stat $SPATH/dir2`
178         _compare_stat_times NNN "$dir2_stat_readonly_before_access" \
179                 "$dir2_stat_readonly_after_access" "for dir in readonly subvol"
180         file3_stat_readonly_after_access=`_stat $SPATH/dir2/file3`
181         _compare_stat_times NNN "$file3_stat_after_third_access" \
182                 "$file3_stat_readonly_after_access" "for file in readonly subvol"
183 fi
184
185 # Mounting read-only. Access time should never be updated, despite the
186 # strictatime mount option.
187 sleep 1
188 dir2_stat_before_ro_mount=`_stat $TPATH/dir2`
189 file3_stat_before_ro_mount=`_stat $TPATH/dir2/file3`
190 _scratch_cycle_mount "ro,strictatime"
191 ls $TPATH/dir2 > /dev/null
192 cat $TPATH/dir2/file3 > /dev/null
193 dir2_stat_after_ro_mount=`_stat $TPATH/dir2`
194 _compare_stat_times NNN "$dir2_stat_before_ro_mount" \
195         "$dir2_stat_after_ro_mount" "for dir in read-only filesystem"
196 file3_stat_after_ro_mount=`_stat $TPATH/dir2/file3`
197 _compare_stat_times NNN "$file3_stat_before_ro_mount" \
198         "$file3_stat_after_ro_mount" "for file in read-only filesystem"
199
200 # success, all done
201 _scratch_unmount
202 echo "Silence is golden"
203 status=0
204 exit