xfstests: introduce a common directory
[xfstests-dev.git] / tests / xfs / 194
1 #! /bin/bash
2 # FS QA Test No. 194
3 #
4 # Test mapping around/over holes for sub-page blocks
5 #
6 #-----------------------------------------------------------------------
7 # Copyright (c) 2008 Eric Sandeen.  All Rights Reserved.
8 #
9 # This program is free software; you can redistribute it and/or
10 # modify it under the terms of the GNU General Public License as
11 # published by the Free Software Foundation.
12 #
13 # This program is distributed in the hope that it would be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with this program; if not, write the Free Software Foundation,
20 # Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
21 #-----------------------------------------------------------------------
22 #
23
24 seq=`basename $0`
25 seqres=$RESULT_DIR/$seq
26 seqres=$RESULT_DIR/$seq
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 # only xfs supported due to use of xfs_bmap
46 _supported_fs xfs
47 _supported_os IRIX Linux
48
49 # real QA test starts here
50 rm -f $seqres.full
51
52 # For this test we use block size = 1/8 page size
53 pgsize=`$here/src/feature -s`
54 blksize=`expr $pgsize / 8`
55
56 # Filter out file mountpoint and physical location info
57 # Input:
58 #  EXT: FILE-OFFSET      BLOCK-RANGE      AG AG-OFFSET        TOTAL
59 #  0: [0..63]:         160..223          0 (160..223)          64
60 #  1: [64..127]:       hole                                    64
61 # Output:
62 #  SCRATCH_MNT/testfile4:       TYPE
63 #  EXT: TYPE    TOTAL
64 #  0:   blocks  1
65 #  1:   hole    1
66
67 _filter_bmap()
68 {
69     tee -a $seqres.full | \
70     sed "s#$SCRATCH_MNT#SCRATCH_MNT#g" | \
71     awk \
72         '$3 ~ /hole/     { print $1 "\t" $3 "\t" ($4 * 512) / blksize; next }
73          $1 ~ /^[0-9]/   { print $1 "\tblocks\t" ($6 * 512) / blksize; next }
74          $1 ~ /^SCRATCH/ { print $1; next }
75                          { print $1 "\tTYPE\t" $6 }' blksize=$blksize
76 }
77
78 # Filter out offsets, which vary by blocksize
79 _filter_od()
80 {
81     tee -a $seqres.full | \
82     sed -e "s/^[0-9A-Fa-f ]\{7,8\}//"
83 }
84
85 _require_scratch
86 unset MKFS_OPTIONS
87 unset XFS_MKFS_OPTIONS
88 _scratch_mkfs_xfs -b size=$blksize >/dev/null 2>&1
89 _scratch_mount
90
91 # 512b block / 4k page example:
92 #
93 #1) Write 1k of data (buffered):
94 #
95 # |1111|1111|
96 #
97 # 2) ftruncate back to 256 bytes:
98 #
99 # |1100|
100 #
101 # 3) ftruncate out to 4k: ("H" means hole (expected))
102 #
103 # |1100|HHHH|HHHH|HHHH|HHHH|HHHH|HHHH|HHHH|
104 #
105 # So we should have 1 block of data/0, 7 blocks of holes.
106 #
107 # 4) check what's there with a direct IO read
108 #
109 # In fact what I get is 1 block of data/0, 1 block of 0's, and 7 blocks of
110 # garbage:
111 #
112 # |1100|0000|GGGG|GGGG|GGGG|GGGG|GGGG|GGGG|
113 #
114 # The garbage is in fact stale data from the disk.
115 #
116 # Check that we don't get stale data and that the hole is a hole:
117
118 echo "== Test 1 =="
119 # Write, truncate in, truncate out
120 xfs_io \
121 -c "pwrite -S 0x11 -b `expr $pgsize / 2`  0 `expr $pgsize / 2`" \
122 -c "truncate `expr $blksize / 2`" \
123 -c "truncate $pgsize" \
124 -t -f $SCRATCH_MNT/testfile1 >> $seqres.full
125
126 # directio read of entire file
127 xfs_io \
128 -c "pread 0 $pgsize" \
129 -d $SCRATCH_MNT/testfile1 >> $seqres.full
130
131 xfs_bmap -v $SCRATCH_MNT/testfile1 | _filter_bmap
132 od -x $SCRATCH_MNT/testfile1 | _filter_od
133
134 # Similar but write another block to create block/hole/block/hole
135
136 echo "== Test 2 =="
137 # Write, truncate in, truncate out, write to middle
138 xfs_io \
139 -c "pwrite -S 0x11 -b `expr $pgsize / 2`  0 `expr $pgsize / 2`" \
140 -c "truncate `expr $blksize / 2`" \
141 -c "truncate $pgsize" \
142 -c "pwrite -S 0x22 -b $blksize `expr $blksize \* 4` $blksize" \
143 -t -f $SCRATCH_MNT/testfile2 >> $seqres.full
144
145 # directio read of entire file
146 xfs_io \
147 -c "pread 0 $pgsize" \
148 -d $SCRATCH_MNT/testfile2 >> $seqres.full
149
150 xfs_bmap -v $SCRATCH_MNT/testfile2 | _filter_bmap
151 od -x $SCRATCH_MNT/testfile2 | _filter_od
152
153 # 512 byte block / 4k page example:
154
155 # direct write 1 page (8 blocks) of "0x11" to 0x1000
156 # map read 1 block, 512 (0x200) at 0
157 # truncate to half a block, 256 (0x100)
158 # truncate to block+1, 513 (0x201)
159 # direct write "0x22" for 1 block at offset 2048 (0x800)
160
161 # |1111|1111|1111|1111|1111|1111|1111|1111|     Write 1's
162 # |MRMR|1111|1111|1111|1111|1111|1111|1111|     mapread
163 # |11--|                                        truncate down
164 # |1100|0---|                                   truncate up, block+1
165 # |    |    |HHHH|HHHH|2222|                    Write 2's (extending)
166
167 #           |uptodate?|
168 # |1100|0000|1111|1111|2222|----|----|----|     <- potential badness
169
170 # We're looking for this badness due to mapping over a hole:
171 # Exposes stale data from 0x400 (1024) through 0x800 (2048)
172
173 # 00000000  11 11 11 11 11 11 11 11  11 11 11 11 11 11 11 11  |................|
174 # *
175 # 00000100  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
176 # *
177 # 00000400  11 11 11 11 11 11 11 11  11 11 11 11 11 11 11 11  |................| <- BAD
178 # *
179 # 00000800  22 22 22 22 22 22 22 22  22 22 22 22 22 22 22 22  |""""""""""""""""|
180 # *
181 # 00000a00
182
183 # We *should* get:
184 # |1100|HHHH|HHHH|HHHH|2222|----|----|----|
185
186 echo "== Test 3 =="
187 xfs_io \
188 -c "pwrite -S 0x11 -b $pgsize 0 $pgsize" \
189 -c "mmap -r 0 $blksize" -c "mread 0 $blksize" -c "munmap" \
190 -c "truncate `expr $blksize / 2`" \
191 -c "truncate `expr $blksize + 1`" \
192 -c "pwrite -S 0x22 -b $blksize `expr $pgsize / 2` $blksize" \
193 -t -d -f $SCRATCH_MNT/testfile3 >> $seqres.full
194
195 xfs_bmap -v $SCRATCH_MNT/testfile3 | _filter_bmap
196 od -x $SCRATCH_MNT/testfile3 | _filter_od
197
198 # Now try the same thing but write a sector in the middle of that hole
199 # If things go badly stale data will be exposed either side.
200 # This is most interesting for block size > 512 (page size > 4096)
201
202 # We *should* get:
203 # |1100|HHHH|33HH|HHHH|2222|----|----|----|
204
205 echo "== Test 4 =="
206 xfs_io \
207 -c "pwrite -S 0x11 -b $pgsize 0 $pgsize" \
208 -c "mmap -r 0 $blksize" -c "mread 0 $blksize" -c "munmap" \
209 -c "truncate `expr $blksize / 2`" \
210 -c "truncate `expr $blksize + 1`" \
211 -c "pwrite -S 0x22 -b $blksize `expr $pgsize / 2` $blksize" \
212 -c "pwrite -S 0x33 -b 512 `expr $blksize \* 2` 512" \
213 -t -d -f $SCRATCH_MNT/testfile4 >> $seqres.full
214
215 xfs_bmap -v $SCRATCH_MNT/testfile4 | _filter_bmap
216 od -x $SCRATCH_MNT/testfile4 | _filter_od
217
218 # success, all done
219 status=0
220 exit