fstests: convert remaining tests to SPDX license tags
[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
44 run_fsx()
45 {
46         $here/ltp/fsx $2 --replay-ops $1 $file 2>&1 | tee -a $seqres.full >$tmp.fsx
47         if [ ${PIPESTATUS[0]} -ne 0 ]; then
48                 cat $tmp.fsx
49                 exit 1
50         fi
51 }
52
53 # run fsx with and without fsync(2) after write to get more coverage
54 test_fsx()
55 {
56         echo "fsx --replay-ops ${1#*.}" | tee -a $seqres.full
57         run_fsx $1
58
59         echo "fsx -y --replay-ops ${1#*.}" | tee -a $seqres.full
60         run_fsx $1 -y
61 }
62
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
70
71 # overwrite the unwritten extents with non-zeros, but extent will stay in
72 # unwritten till I/O completion
73 write 0x0 0x1000 0x0
74
75 # truncate down the file, which should zero page cache beyong new EOF but a
76 # buggy kernel won't
77 truncate 0x0 0x10 0x1000
78
79 # unmap the file and invalidate the pagecache of the block
80 punch_hole 0x0 0x10 0x10
81
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
84 mapread 0x0 0x10 0x10
85 EOF
86
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
91 write 0x0 0x1000 0x0
92 truncate 0x0 0x10 0x1000
93 mapread 0x0 0x10 0x10
94 EOF
95
96 # same as fsxops.0, but fallocate without KEEP_SIZE flag
97 cat >$tmp.fsxops.2 <<EOF
98 fallocate 0x0 0x1000 0x0
99 write 0x0 0x1000 0x0
100 truncate 0x0 0x10 0x1000
101 punch_hole 0x0 0x10 0x10
102 mapread 0x0 0x10 0x10
103 EOF
104
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
112 EOF
113
114 for i in 0 1 2 3; do
115         test_fsx $tmp.fsxops.$i
116 done
117
118 # success, all done
119 status=0
120 exit