xfs: force file creation to the data device for certain layout tests
[xfstests-dev.git] / tests / generic / 469
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 469
6 #
7 # Test that mmap read doesn't see non-zero data past EOF on truncate down.
8 #
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.
12 #
13 # Patch "xfs: truncate pagecache before writeback in xfs_setattr_size()" fixed
14 # the bug on XFS.
15 #
16 seq=`basename $0`
17 seqres=$RESULT_DIR/$seq
18 echo "QA output created by $seq"
19
20 here=`pwd`
21 tmp=/tmp/$$
22 file=$TEST_DIR/$seq.fsx
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 $file $tmp.*
30 }
31
32 # get standard environment, filters and checks
33 . ./common/rc
34 . ./common/filter
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_xfs_io_command "falloc" "-k"
43 _require_xfs_io_command "fpunch"
44 _require_xfs_io_command "fzero"
45
46 run_fsx()
47 {
48         $here/ltp/fsx $2 --replay-ops $1 $file 2>&1 | tee -a $seqres.full >$tmp.fsx
49         if [ ${PIPESTATUS[0]} -ne 0 ]; then
50                 cat $tmp.fsx
51                 exit 1
52         fi
53 }
54
55 # run fsx with and without fsync(2) after write to get more coverage
56 test_fsx()
57 {
58         echo "fsx --replay-ops ${1#*.}" | tee -a $seqres.full
59         run_fsx $1
60
61         echo "fsx -y --replay-ops ${1#*.}" | tee -a $seqres.full
62         run_fsx $1 -y
63 }
64
65 # simplified fsx operations that work on small & not blocksize-aligned offsets,
66 # so filesystems with small block size could reproduce too
67 cat >$tmp.fsxops.0 <<EOF
68 # create file with unwritten extent, KEEP_SIZE flag is required, otherwise page
69 # straddles new i_size in the writeback triggered by truncate, range [i_size,
70 # page_boundary] will be zeroed there, and bug won't be reproduced
71 fallocate 0x0 0x1000 0x0 keep_size
72
73 # overwrite the unwritten extents with non-zeros, but extent will stay in
74 # unwritten till I/O completion
75 write 0x0 0x1000 0x0
76
77 # truncate down the file, which should zero page cache beyong new EOF but a
78 # buggy kernel won't
79 truncate 0x0 0x10 0x1000
80
81 # unmap the file and invalidate the pagecache of the block
82 punch_hole 0x0 0x10 0x10
83
84 # mmap reads the whole block from disk, and fsx will check page range beyond
85 # EOF to make sure we only see zeros there
86 mapread 0x0 0x10 0x10
87 EOF
88
89 # to get a bit more test coverage, try other operation combinations too
90 # same as fsxops.0, but skip punch_hole to keep the pagecache before mapread
91 cat >$tmp.fsxops.1 <<EOF
92 fallocate 0x0 0x1000 0x0 keep_size
93 write 0x0 0x1000 0x0
94 truncate 0x0 0x10 0x1000
95 mapread 0x0 0x10 0x10
96 EOF
97
98 # same as fsxops.0, but fallocate without KEEP_SIZE flag
99 cat >$tmp.fsxops.2 <<EOF
100 fallocate 0x0 0x1000 0x0
101 write 0x0 0x1000 0x0
102 truncate 0x0 0x10 0x1000
103 punch_hole 0x0 0x10 0x10
104 mapread 0x0 0x10 0x10
105 EOF
106
107 # this is from the original fsxops when bug was first hit
108 cat >$tmp.fsxops.3 <<EOF
109 fallocate 0x35870 0xa790 0x0 keep_size
110 write 0x2aa50 0xc37f 0x0
111 truncate 0x0 0x36dcd 0x36dcf
112 zero_range 0x35849 0x1584 0x36dcd
113 mapread 0x361c8 0xc05 0x36dcd
114 EOF
115
116 for i in 0 1 2 3; do
117         test_fsx $tmp.fsxops.$i
118 done
119
120 # success, all done
121 status=0
122 exit