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
46 $here/ltp/fsx $2 --replay-ops $1 $file 2>&1 | tee -a $seqres.full >$tmp.fsx
47 if [ ${PIPESTATUS[0]} -ne 0 ]; then
53 # run fsx with and without fsync(2) after write to get more coverage
56 echo "fsx --replay-ops ${1#*.}" | tee -a $seqres.full
59 echo "fsx -y --replay-ops ${1#*.}" | tee -a $seqres.full
63 # simplified fsx operations that work on small & not blocksize-aligned offsets,
64 # so filesystems with small block size could reproduce too
65 cat >$tmp.fsxops.0 <<EOF
66 # create file with unwritten extent, KEEP_SIZE flag is required, otherwise page
67 # straddles new i_size in the writeback triggered by truncate, range [i_size,
68 # page_boundary] will be zeroed there, and bug won't be reproduced
69 fallocate 0x0 0x1000 0x0 keep_size
71 # overwrite the unwritten extents with non-zeros, but extent will stay in
72 # unwritten till I/O completion
75 # truncate down the file, which should zero page cache beyong new EOF but a
77 truncate 0x0 0x10 0x1000
79 # unmap the file and invalidate the pagecache of the block
80 punch_hole 0x0 0x10 0x10
82 # mmap reads the whole block from disk, and fsx will check page range beyond
83 # EOF to make sure we only see zeros there
87 # to get a bit more test coverage, try other operation combinations too
88 # same as fsxops.0, but skip punch_hole to keep the pagecache before mapread
89 cat >$tmp.fsxops.1 <<EOF
90 fallocate 0x0 0x1000 0x0 keep_size
92 truncate 0x0 0x10 0x1000
96 # same as fsxops.0, but fallocate without KEEP_SIZE flag
97 cat >$tmp.fsxops.2 <<EOF
98 fallocate 0x0 0x1000 0x0
100 truncate 0x0 0x10 0x1000
101 punch_hole 0x0 0x10 0x10
102 mapread 0x0 0x10 0x10
105 # this is from the original fsxops when bug was first hit
106 cat >$tmp.fsxops.3 <<EOF
107 fallocate 0x35870 0xa790 0x0 keep_size
108 write 0x2aa50 0xc37f 0x0
109 truncate 0x0 0x36dcd 0x36dcf
110 zero_range 0x35849 0x1584 0x36dcd
111 mapread 0x361c8 0xc05 0x36dcd
115 test_fsx $tmp.fsxops.$i