generic: Add check for required keep_size/punch_hole/zero_range/collapse_range
[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 _supported_os Linux
42 _require_test
43 _require_xfs_io_command "falloc" "-k"
44 _require_xfs_io_command "fpunch"
45 _require_xfs_io_command "fzero"
46
47 run_fsx()
48 {
49         $here/ltp/fsx $2 --replay-ops $1 $file 2>&1 | tee -a $seqres.full >$tmp.fsx
50         if [ ${PIPESTATUS[0]} -ne 0 ]; then
51                 cat $tmp.fsx
52                 exit 1
53         fi
54 }
55
56 # run fsx with and without fsync(2) after write to get more coverage
57 test_fsx()
58 {
59         echo "fsx --replay-ops ${1#*.}" | tee -a $seqres.full
60         run_fsx $1
61
62         echo "fsx -y --replay-ops ${1#*.}" | tee -a $seqres.full
63         run_fsx $1 -y
64 }
65
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
73
74 # overwrite the unwritten extents with non-zeros, but extent will stay in
75 # unwritten till I/O completion
76 write 0x0 0x1000 0x0
77
78 # truncate down the file, which should zero page cache beyong new EOF but a
79 # buggy kernel won't
80 truncate 0x0 0x10 0x1000
81
82 # unmap the file and invalidate the pagecache of the block
83 punch_hole 0x0 0x10 0x10
84
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
87 mapread 0x0 0x10 0x10
88 EOF
89
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
94 write 0x0 0x1000 0x0
95 truncate 0x0 0x10 0x1000
96 mapread 0x0 0x10 0x10
97 EOF
98
99 # same as fsxops.0, but fallocate without KEEP_SIZE flag
100 cat >$tmp.fsxops.2 <<EOF
101 fallocate 0x0 0x1000 0x0
102 write 0x0 0x1000 0x0
103 truncate 0x0 0x10 0x1000
104 punch_hole 0x0 0x10 0x10
105 mapread 0x0 0x10 0x10
106 EOF
107
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
115 EOF
116
117 for i in 0 1 2 3; do
118         test_fsx $tmp.fsxops.$i
119 done
120
121 # success, all done
122 status=0
123 exit