2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (c) 2017 Red Hat, Inc. All Rights Reserved.
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.
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"
18 seqres=$RESULT_DIR/$seq
19 echo "QA output created by $seq"
23 status=1 # failure is the default!
24 trap "_cleanup; exit \$status" 0 1 2 3 15
33 # get standard environment, filters and checks
36 # remove previous $seqres.full before test
39 # real QA test starts here
45 tfile=$TEST_DIR/testfile_${seq}
46 ssize=`_min_dio_alignment $TEST_DEV`
47 bsize=`_get_block_size $TEST_DIR`
49 # let's focus on the specific bug that only happens when $ssize <= $bsize
50 if [ $ssize -gt $((bsize/4)) ]; then
51 _notrun "Only test on sector size < half of block size"
54 rm -f $tfile 2>/dev/null
56 # check xfs_io pread result, especially for
57 # Param1: expected pread offset
58 # Param2: expected pread count
59 # Param3: expected pread return
61 # If any of above values are not as expected, the output keeps
62 # using the real value
69 $AWK_PROG -v ret="$RETURN" -v cnt="$COUNT" -v off="$OFFSET" '
77 if(retval != ret || count != cnt || offset != off)
78 printf("expect [%s,%s,%s], got [%s,%s,%s]\n", \
79 off, cnt, ret, offset, count, retval)
86 # +-------------------------------------------------------+
88 # +-------------------------------------------------------+
89 # | sect | sect | sect | sect | sect | sect | sect | sect |
92 # |<--------------- move EOF -------------->| xxxxxxxxxxx |
96 # [pread4] ... [pread5]
98 # Run below steps with different $operation and $openflag
100 # 1) write 2 blocks (6 sectors) data to move EOF to the penultimate sector
101 # 2) read (pread1) the first sector within EOF
102 # 3) read (pread2) the second block contain EOF
103 # 4) read (pread3) a sector at (after) EOF
104 # 5) read (pread4) the last sector past EOF
105 # 6) read (pread5) at far away from EOF
108 tsize=$((asize - ssize * 2))
113 local op="buffer read"
115 if [ "$of" != "" ]; then
119 echo "$op the first sector within EOF"
120 $XFS_IO_PROG $of -c "pread 0 $ssize" $tfile | \
121 check_xfs_io_read 0 "$ssize" "$ssize"
123 echo "$op the second block contains EOF"
124 $XFS_IO_PROG $of -c "pread $bsize $bsize" $tfile | \
125 check_xfs_io_read "$bsize" "$bsize" "$((tsize - bsize))"
127 echo "$op a sector at (after) EOF"
128 $XFS_IO_PROG $of -c "pread $tsize $ssize" $tfile | \
129 check_xfs_io_read "$tsize" "$ssize" "0"
131 echo "$op the last sector past EOF"
132 $XFS_IO_PROG $of -c "pread $((tsize + ssize)) $ssize" $tfile | \
133 check_xfs_io_read "$((tsize + ssize))" "$ssize" "0"
135 echo "$op at far away from EOF"
136 $XFS_IO_PROG $of -c "pread $((bsize * 100)) $ssize" $tfile | \
137 check_xfs_io_read "$((bsize * 100))" "$ssize" "0"
140 # Test buffer/direct I/O read
141 $XFS_IO_PROG -ft -c "pwrite 0 ${tsize}" -c "fsync" $tfile >>$seqres.full
142 for oflag in "" "-d"; do