generic: Add tests for inode timestamp policy
[xfstests-dev.git] / tests / generic / 402
1 #! /bin/bash
2 # FS QA Test 402
3 #
4 # Tests to verify policy for filesystem timestamps for
5 # supported ranges:
6 # 1. Verify filesystem rw mount according to sysctl
7 # timestamp_supported.
8 # 2. Verify timestamp clamping for timestamps beyond max
9 # timestamp supported.
10 #
11 # Exit status 1: either or both tests above fail.
12 # Exit status 0: both the above tests pass.
13 #
14 #-----------------------------------------------------------------------
15 # Copyright (c) 2016 Deepa Dinamani.  All Rights Reserved.
16 #
17 # This program is free software; you can redistribute it and/or
18 # modify it under the terms of the GNU General Public License as
19 # published by the Free Software Foundation.
20 #
21 # This program is distributed in the hope that it would be useful,
22 # but WITHOUT ANY WARRANTY; without even the implied warranty of
23 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24 # GNU General Public License for more details.
25 #
26 # You should have received a copy of the GNU General Public License
27 # along with this program; if not, write the Free Software Foundation,
28 # Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
29 #-----------------------------------------------------------------------
30 #
31
32 seq=`basename $0`
33 seqres=$RESULT_DIR/$seq
34 echo "QA output created by $seq"
35
36 here=`pwd`
37 tmp=/tmp/$$
38 status=1        # failure is the default!
39 trap "exit \$status" 0 1 2 3 15
40
41 # Get standard environment, filters and checks.
42 . ./common/rc
43 . ./common/filter
44 . ./common/attr
45
46 # remove previous $seqres.full before test
47 rm -f $seqres.full
48
49 # Prerequisites for the test run.
50 _supported_fs generic
51 _supported_os Linux
52 _require_scratch
53 _require_xfs_io_command utimes
54
55 # Compare file timestamps obtained from stat
56 # with a given timestamp.
57 check_stat()
58 {
59         file=$1
60         timestamp=$2
61
62         stat_timestamp=`stat -c"%X;%Y" $file`
63
64         prev_timestamp="$timestamp;$timestamp"
65         if [ $prev_timestamp != $stat_timestamp ]; then
66                 echo "$prev_timestamp != $stat_timestamp" | tee -a $seqres.full
67         fi
68 }
69
70 run_test_individual()
71 {
72         file=$1
73         timestamp=$2
74         update_time=$3
75
76         # check if the time needs update
77         if [ $update_time -eq 1 ]; then
78                 echo "Updating file: $file to timestamp `date -d @$timestamp`"  >> $seqres.full
79                 $XFS_IO_PROG -f -c "utimes $timestamp 0 $timestamp 0" $file
80                 if [ $? -ne 0 ]; then
81                         echo "Failed to update times on $file" | tee -a $seqres.full
82                 fi
83         fi
84
85         tsclamp=$(($timestamp>$tsmax?$tsmax:$timestamp))
86         echo "Checking file: $file Updated timestamp is `date -d @$tsclamp`"  >> $seqres.full
87         check_stat $file $tsclamp
88 }
89
90 run_test()
91 {
92         update_time=$1
93
94         n=1
95
96         for TIME in "${TIMESTAMPS[@]}"; do
97                 run_test_individual ${SCRATCH_MNT}/test_$n $TIME $update_time
98                 ((n++))
99         done
100 }
101
102 _scratch_mkfs &>> $seqres.full 2>&1 || _fail "mkfs failed"
103 _require_y2038 $SCRATCH_DEV
104
105 read tsmin tsmax <<<$(_filesystem_timestamp_range $SCRATCH_DEV)
106 echo min supported timestamp $tsmin $(date --date=@$tsmin) >> $seqres.full
107 echo max supported timestamp $tsmax $(date --date=@$tsmax) >> $seqres.full
108
109 # Test timestamps array
110
111 declare -a TIMESTAMPS=(
112         $tsmin
113         0
114         $tsmax
115         $((tsmax+1))
116         4294967295
117         8589934591
118         34359738367
119 )
120
121 # Max timestamp is hardcoded to Mon Jan 18 19:14:07 PST 2038
122 sys_tsmax=2147483647
123 echo "max timestamp that needs to be supported by fs for rw mount is" \
124         "$((sys_tsmax+1)) $(date --date=@$((sys_tsmax+1)))" >> $seqres.full
125
126 read ts_check <<<$(cat /proc/sys/fs/fs-timestamp-check-on)
127
128 _scratch_mount
129 result=$?
130
131 if [ $ts_check -ne 0 ]; then
132         echo "sysctl filesystem timestamp check is on" >> $seqres.full
133         # check for mount failure if the minimum requirement for max timestamp
134         # supported is not met.
135         if [ $sys_tsmax -ge $tsmax ]; then
136                 if [ $result -eq 0 ]; then
137                         echo "mount test failed"  | tee -a $seqres.full
138                         exit
139                 fi
140         else
141                 if [ $result -ne 0 ]; then
142                         echo "failed to mount $SCRATCH_DEV"  | tee -a $seqres.full
143                         exit
144                 fi
145         fi
146 else
147         # if sysctl switch is off then mount should succeed always.
148         echo "sysctl filesystem timestamp check is off" >> $seqres.full
149         if [ $result -ne 0 ]; then
150                 echo "failed to mount $SCRATCH_DEV and timestamp check is off"  >> $seqres.full
151                 exit
152         fi
153 fi
154
155 # Begin test case 1
156 echo "In memory timestamps update test start" >> $seqres.full
157
158 # update time on the file
159 update_time=1
160
161 run_test $update_time
162
163 echo "In memory timestamps update complete" >> $seqres.full
164
165 echo "Unmounting and mounting scratch $SCRATCH_MNT" >> $seqres.full
166
167 # unmount and remount $SCRATCH_DEV
168 _scratch_cycle_mount
169
170 # Begin test case 2
171
172 n=1
173
174 # Do not update time on the file this time, just read from disk
175 update_time=0
176
177 echo "On disk timestamps update test start" >> $seqres.full
178
179 # Re-run test
180 run_test $update_time
181
182 echo "On disk timestamps update test complete" >> $seqres.full
183
184 echo "y2038 inode timestamp tests completed successfully"
185
186 # success, all done
187 status=0
188 exit