8b7e6fba259d87536daf0552ba154aea394f03da
[xfstests-dev.git] / tests / generic / 030
1 #! /bin/bash
2 # FS QA Test No. generic/030
3 #
4 # Test mapped writes against remap+truncate down/up to ensure we get the data
5 # correctly written. This can expose data corruption bugs on filesystems where
6 # the block size is smaller than the page size.
7 #
8 #-----------------------------------------------------------------------
9 # Copyright (c) 2014 Red Hat, Inc.  All Rights Reserved.
10 #
11 # This program is free software; you can redistribute it and/or
12 # modify it under the terms of the GNU General Public License as
13 # published by the Free Software Foundation.
14 #
15 # This program is distributed in the hope that it would be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 # GNU General Public License for more details.
19 #
20 # You should have received a copy of the GNU General Public License
21 # along with this program; if not, write the Free Software Foundation,
22 # Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
23 #-----------------------------------------------------------------------
24 #
25
26 seq=`basename $0`
27 seqres=$RESULT_DIR/$seq
28 echo "QA output created by $seq"
29
30 here=`pwd`
31 tmp=/tmp/$$
32 status=1        # failure is the default!
33 trap "_cleanup; exit \$status" 0 1 2 3 15
34
35 _cleanup()
36 {
37     cd /
38     rm -f $tmp.*
39 }
40
41 # get standard environment, filters and checks
42 . ./common/rc
43 . ./common/filter
44
45 # real QA test starts here
46
47 # Modify as appropriate.
48 _supported_fs generic
49 _supported_os Linux
50 _require_scratch
51 _require_xfs_io_command "mremap"
52
53 testfile=$SCRATCH_MNT/testfile
54
55 _scratch_mkfs > /dev/null 2>&1
56 _scratch_mount
57
58 # first case is just truncate down/truncate up to check that the mapped
59 # write after the truncate up is correctly handled.
60 $XFS_IO_PROG -t -f \
61 -c "truncate 5017k"             `# truncate     |       |` \
62 -c "pwrite -S 0x58 0 5017k"     `# write        |X...XXX|` \
63 -c "mmap -rw 0 5017k"           `# mmap         |       |` \
64 -c "truncate 5020k"             `# truncate up  |                        |` \
65 -c "mremap -m 5020k"            `# mremap up    |                        |` \
66 -c "mwrite -S 0x57 5017k 3k"    `# mwrite       |        WWWWWWWWWWWWWWWW|` \
67 -c "mremap 5017k "              `# mremap dn    |       |` \
68 -c "truncate 5017k"             `# mremap dn    |       |` \
69 -c "truncate 5020k"             `# truncate up  |                        |` \
70 -c "mremap -m 5020k"            `# mremap up    |                        |` \
71 -c "mwrite -S 0x59 5017k 3k"    `# mwrite       |        YYYYYYYYYYYYYYYY|` \
72 -c "close"      \
73 $testfile | _filter_xfs_io
74
75 echo "==== Pre-Remount ==="
76 hexdump -C $testfile
77 _scratch_cycle_mount
78 echo "==== Post-Remount =="
79 hexdump -C $testfile
80
81 rm -f $testfile
82 sync
83
84 # second case is to do a mwrite between the truncate to a block on the
85 # same page we are truncating within the EOF. This checks that a mapped
86 # write between truncate down and truncate up a further mapped
87 # write to the same page into the new space doesn't result in data being lost.
88 $XFS_IO_PROG -t -f \
89 -c "truncate 5017k"             `# truncate     |       |` \
90 -c "pwrite -S 0x58 0 5017k"     `# write        |X...XXX|` \
91 -c "mmap -rw 0 5017k"           `# mmap         |       |` \
92 -c "truncate 5020k"             `# truncate up  |                        |` \
93 -c "mremap -m 5020k"            `# mremap up    |                        |` \
94 -c "mwrite -S 0x57 5017k 3k"    `# mwrite       |        WWWWWWWWWWWWWWWW|` \
95 -c "mremap 5017k "              `# mremap dn    |       |` \
96 -c "truncate 5017k"             `# mremap dn    |       |` \
97 -c "mwrite -S 0x5a 5016k 1k"    `# mwrite       |    ZZZ                 |` \
98 -c "truncate 5020k"             `# truncate up  |                        |` \
99 -c "mremap -m 5020k"            `# mremap up    |                        |` \
100 -c "mwrite -S 0x59 5017k 3k"    `# mwrite       |        YYYYYYYYYYYYYYYY|` \
101 -c "close"      \
102 $testfile | _filter_xfs_io
103
104 echo "==== Pre-Remount ==="
105 hexdump -C $testfile
106 _scratch_cycle_mount
107 echo "==== Post-Remount =="
108 hexdump -C $testfile
109
110 # third case is the same as the first, but this time on unaligned byte
111 # boundaries rather than block boundaries. This mimics the exact mmap write
112 # patterns of the application that exposed the bug in the first place, and
113 # so is somewhat more complex and has repeated operations in it. 
114 $XFS_IO_PROG -t -f \
115 -c "truncate 5136912" \
116 -c "pwrite -S 0x58 0 5136912" \
117 -c "mmap -rw 0 5136912" \
118 -c "mremap 5136912" \
119 -c "truncate 5136912" \
120 -c "truncate 5139720" \
121 -c "mremap -m 5139720" \
122 -c "mwrite -S 0 5136912 2808" \
123 -c "mwrite -S 0 5136912 2808" \
124 -c "mwrite -S 0 5136912 2808" \
125 -c "mremap 5136912 " \
126 -c "truncate 5136912" \
127 -c "truncate 5139720" \
128 -c "mremap -m 5139720" \
129 -c "mwrite -S 0 5136912 2808" \
130 -c "mwrite -S 0 5136912 2808" \
131 -c "mwrite -S 0x59 5136912 2808" \
132 -c "truncate 5140480" \
133 -c "mremap 5140480" \
134 -c "msync -s 0 5140480" \
135 -c "mremap 5139720" \
136 -c "munmap" \
137 -c "close" \
138 $testfile | _filter_xfs_io
139
140 echo "==== Pre-Remount ==="
141 hexdump -C $testfile
142 _scratch_cycle_mount
143 echo "==== Post-Remount =="
144 hexdump -C $testfile
145
146 status=0
147 exit
148