2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (c) 2017 Red Hat Inc., All Rights Reserved.
7 # Test that mmap read doesn't see non-zero data past EOF on truncate down.
9 # This is inspired by an XFS bug that truncate down fails to zero page cache
10 # beyond new EOF and causes stale data written to disk unexpectedly and a
11 # subsequent mmap reads and sees non-zeros post EOF.
13 # Patch "xfs: truncate pagecache before writeback in xfs_setattr_size()" fixed
17 seqres=$RESULT_DIR/$seq
18 echo "QA output created by $seq"
22 file=$TEST_DIR/$seq.fsx
23 status=1 # failure is the default!
24 trap "_cleanup; exit \$status" 0 1 2 3 15
32 # get standard environment, filters and checks
36 # remove previous $seqres.full before test
39 # real QA test starts here
43 _require_xfs_io_command "falloc" "-k"
44 _require_xfs_io_command "fpunch"
45 _require_xfs_io_command "fzero"
49 $here/ltp/fsx $2 --replay-ops $1 $file 2>&1 | tee -a $seqres.full >$tmp.fsx
50 if [ ${PIPESTATUS[0]} -ne 0 ]; then
56 # run fsx with and without fsync(2) after write to get more coverage
59 echo "fsx --replay-ops ${1#*.}" | tee -a $seqres.full
62 echo "fsx -y --replay-ops ${1#*.}" | tee -a $seqres.full
66 # simplified fsx operations that work on small & not blocksize-aligned offsets,
67 # so filesystems with small block size could reproduce too
68 cat >$tmp.fsxops.0 <<EOF
69 # create file with unwritten extent, KEEP_SIZE flag is required, otherwise page
70 # straddles new i_size in the writeback triggered by truncate, range [i_size,
71 # page_boundary] will be zeroed there, and bug won't be reproduced
72 fallocate 0x0 0x1000 0x0 keep_size
74 # overwrite the unwritten extents with non-zeros, but extent will stay in
75 # unwritten till I/O completion
78 # truncate down the file, which should zero page cache beyong new EOF but a
80 truncate 0x0 0x10 0x1000
82 # unmap the file and invalidate the pagecache of the block
83 punch_hole 0x0 0x10 0x10
85 # mmap reads the whole block from disk, and fsx will check page range beyond
86 # EOF to make sure we only see zeros there
90 # to get a bit more test coverage, try other operation combinations too
91 # same as fsxops.0, but skip punch_hole to keep the pagecache before mapread
92 cat >$tmp.fsxops.1 <<EOF
93 fallocate 0x0 0x1000 0x0 keep_size
95 truncate 0x0 0x10 0x1000
99 # same as fsxops.0, but fallocate without KEEP_SIZE flag
100 cat >$tmp.fsxops.2 <<EOF
101 fallocate 0x0 0x1000 0x0
103 truncate 0x0 0x10 0x1000
104 punch_hole 0x0 0x10 0x10
105 mapread 0x0 0x10 0x10
108 # this is from the original fsxops when bug was first hit
109 cat >$tmp.fsxops.3 <<EOF
110 fallocate 0x35870 0xa790 0x0 keep_size
111 write 0x2aa50 0xc37f 0x0
112 truncate 0x0 0x36dcd 0x36dcf
113 zero_range 0x35849 0x1584 0x36dcd
114 mapread 0x361c8 0xc05 0x36dcd
118 test_fsx $tmp.fsxops.$i