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 _begin_fstest auto quick punch zero
19 file=$TEST_DIR/$seq.fsx
21 # Override the default cleanup function.
28 # Import common functions.
31 # real QA test starts here
34 _require_xfs_io_command "falloc" "-k"
35 _require_xfs_io_command "fpunch"
36 _require_xfs_io_command "fzero"
40 $here/ltp/fsx $2 --replay-ops $1 $file 2>&1 | tee -a $seqres.full >$tmp.fsx
41 if [ ${PIPESTATUS[0]} -ne 0 ]; then
47 # run fsx with and without fsync(2) after write to get more coverage
50 echo "fsx --replay-ops ${1#*.}" | tee -a $seqres.full
53 echo "fsx -y --replay-ops ${1#*.}" | tee -a $seqres.full
57 # simplified fsx operations that work on small & not blocksize-aligned offsets,
58 # so filesystems with small block size could reproduce too
59 cat >$tmp.fsxops.0 <<EOF
60 # create file with unwritten extent, KEEP_SIZE flag is required, otherwise page
61 # straddles new i_size in the writeback triggered by truncate, range [i_size,
62 # page_boundary] will be zeroed there, and bug won't be reproduced
63 fallocate 0x0 0x1000 0x0 keep_size
65 # overwrite the unwritten extents with non-zeros, but extent will stay in
66 # unwritten till I/O completion
69 # truncate down the file, which should zero page cache beyong new EOF but a
71 truncate 0x0 0x10 0x1000
73 # unmap the file and invalidate the pagecache of the block
74 punch_hole 0x0 0x10 0x10
76 # mmap reads the whole block from disk, and fsx will check page range beyond
77 # EOF to make sure we only see zeros there
81 # to get a bit more test coverage, try other operation combinations too
82 # same as fsxops.0, but skip punch_hole to keep the pagecache before mapread
83 cat >$tmp.fsxops.1 <<EOF
84 fallocate 0x0 0x1000 0x0 keep_size
86 truncate 0x0 0x10 0x1000
90 # same as fsxops.0, but fallocate without KEEP_SIZE flag
91 cat >$tmp.fsxops.2 <<EOF
92 fallocate 0x0 0x1000 0x0
94 truncate 0x0 0x10 0x1000
95 punch_hole 0x0 0x10 0x10
99 # this is from the original fsxops when bug was first hit
100 cat >$tmp.fsxops.3 <<EOF
101 fallocate 0x35870 0xa790 0x0 keep_size
102 write 0x2aa50 0xc37f 0x0
103 truncate 0x0 0x36dcd 0x36dcf
104 zero_range 0x35849 0x1584 0x36dcd
105 mapread 0x361c8 0xc05 0x36dcd
109 test_fsx $tmp.fsxops.$i