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