common: kill _supported_os
[xfstests-dev.git] / tests / generic / 450
1 #! /bin/bash
2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (c) 2017 Red Hat, Inc.  All Rights Reserved.
4 #
5 # FS QA Test 450
6 #
7 # Test read around EOF. If the file offset is at or past the end of file,
8 # no bytes are read, and read() returns zero. There was a bug, when DIO
9 # read offset is just past the EOF a little, but in the same block with
10 # EOF, read returns different negative values.
11 #
12 # The following two kernel commits fixed this bug:
13 # 74cedf9b6c60 direct-io: Fix negative return from dio read beyond eof
14 # 2d4594acbf6d fix the regression from "direct-io: Fix negative return
15 # from dio read beyond eof"
16 #
17 seq=`basename $0`
18 seqres=$RESULT_DIR/$seq
19 echo "QA output created by $seq"
20
21 here=`pwd`
22 tmp=/tmp/$$
23 status=1        # failure is the default!
24 trap "_cleanup; exit \$status" 0 1 2 3 15
25
26 _cleanup()
27 {
28         cd /
29         rm -f $tmp.*
30         rm -f $tfile
31 }
32
33 # get standard environment, filters and checks
34 . ./common/rc
35
36 # remove previous $seqres.full before test
37 rm -f $seqres.full
38
39 # real QA test starts here
40 _supported_fs generic
41 _require_test
42 _require_odirect
43
44 tfile=$TEST_DIR/testfile_${seq}
45 ssize=`_min_dio_alignment $TEST_DEV`
46 bsize=`_get_block_size $TEST_DIR`
47
48 # let's focus on the specific bug that only happens when $ssize <= $bsize
49 if [ $ssize -gt $((bsize/4)) ]; then
50         _notrun "Only test on sector size < half of block size"
51 fi
52
53 rm -f $tfile 2>/dev/null
54
55 # check xfs_io pread result, especially for
56 # Param1: expected pread offset
57 # Param2: expected pread count
58 # Param3: expected pread return
59 #
60 # If any of above values are not as expected, the output keeps
61 # using the real value
62 check_xfs_io_read()
63 {
64         OFFSET=$1
65         COUNT=$2
66         RETURN=$3
67
68         $AWK_PROG -v ret="$RETURN" -v cnt="$COUNT" -v off="$OFFSET" '
69                 /read/{
70                         split($2, bytes, "/")
71
72                         retval=bytes[1]
73                         count=bytes[2]
74                         offset=$NF
75
76                         if(retval != ret || count != cnt || offset != off)
77                                 printf("expect [%s,%s,%s], got [%s,%s,%s]\n", \
78                                         off, cnt, ret, offset, count, retval)
79
80                         next
81                 }
82         '
83 }
84
85 # +-------------------------------------------------------+
86 # |           block           |           block           |
87 # +-------------------------------------------------------+
88 # | sect | sect | sect | sect | sect | sect | sect | sect |
89 #                                           |
90 #                                          EOF
91 # |<--------------- move EOF -------------->| xxxxxxxxxxx |
92 # [pread1]
93 #                             [           pread2          ]
94 #                                           [pread3]
95 #                                                  [pread4]  ...  [pread5]
96 #
97 # Run below steps with different $operation and $openflag
98 #
99 # 1) write 2 blocks (6 sectors) data to move EOF to the penultimate sector
100 # 2) read (pread1) the first sector within EOF
101 # 3) read (pread2) the second block contain EOF
102 # 4) read (pread3) a sector at (after) EOF
103 # 5) read (pread4) the last sector past EOF
104 # 6) read (pread5) at far away from EOF
105 #
106 asize=$((bsize * 2))
107 tsize=$((asize - ssize * 2))
108
109 read_test()
110 {
111         local of="$1"
112         local op="buffer read"
113
114         if [ "$of" != "" ]; then
115                 op="direct read"
116         fi
117
118         echo "$op the first sector within EOF"
119         $XFS_IO_PROG $of -c "pread 0 $ssize" $tfile | \
120                 check_xfs_io_read 0 "$ssize" "$ssize"
121
122         echo "$op the second block contains EOF"
123         $XFS_IO_PROG $of -c "pread $bsize $bsize" $tfile | \
124                 check_xfs_io_read "$bsize" "$bsize" "$((tsize - bsize))"
125
126         echo "$op a sector at (after) EOF"
127         $XFS_IO_PROG $of -c "pread $tsize $ssize" $tfile | \
128                 check_xfs_io_read "$tsize" "$ssize" "0"
129
130         echo "$op the last sector past EOF"
131         $XFS_IO_PROG $of -c "pread $((tsize + ssize)) $ssize" $tfile | \
132                 check_xfs_io_read "$((tsize + ssize))" "$ssize" "0"
133
134         echo "$op at far away from EOF"
135         $XFS_IO_PROG $of -c "pread $((bsize * 100)) $ssize" $tfile | \
136                 check_xfs_io_read "$((bsize * 100))" "$ssize" "0"
137 }
138
139 # Test buffer/direct I/O read
140 $XFS_IO_PROG -ft -c "pwrite 0 ${tsize}" -c "fsync" $tfile >>$seqres.full
141 for oflag in "" "-d"; do
142         read_test "$oflag"
143 done
144
145 # success, all done
146 status=0
147 exit