populate: add _require_populate_commands to check for tools
[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 echo "QA output created by $seq"
27
28 here=`pwd`
29 tmp=/tmp/$$
30 status=1        # failure is the default!
31 trap "_cleanup; exit \$status" 0 1 2 3 15
32
33 _cleanup()
34 {
35     cd /
36     rm -f $tmp.*
37 }
38
39 # get standard environment, filters and checks
40 . ./common/rc
41 . ./common/filter
42
43 # only xfs supported due to use of xfs_bmap
44 _supported_fs xfs
45 _supported_os IRIX Linux
46
47 # real QA test starts here
48 rm -f $seqres.full
49
50 # For this test we use block size = 1/8 page size
51 pgsize=`$here/src/feature -s`
52 blksize=`expr $pgsize / 8`
53 secsize=`_min_dio_alignment $SCRATCH_DEV`
54
55 if [ $secsize -gt $blksize ];then
56         _notrun "sector size($secsize) too large for platform page size($pgsize)"
57 fi
58
59 # Filter out file mountpoint and physical location info
60 # Input:
61 #  EXT: FILE-OFFSET      BLOCK-RANGE      AG AG-OFFSET        TOTAL
62 #  0: [0..63]:         160..223          0 (160..223)          64
63 #  1: [64..127]:       hole                                    64
64 # Output:
65 #  SCRATCH_MNT/testfile4:       TYPE
66 #  EXT: TYPE    TOTAL
67 #  0:   blocks  1
68 #  1:   hole    1
69
70 _filter_bmap()
71 {
72     tee -a $seqres.full | \
73     sed "s#$SCRATCH_MNT#SCRATCH_MNT#g" | \
74     awk \
75         '$3 ~ /hole/     { print $1 "\t" $3 "\t" ($4 * 512) / blksize; next }
76          $1 ~ /^[0-9]/   { print $1 "\tblocks\t" ($6 * 512) / blksize; next }
77          $1 ~ /^SCRATCH/ { print $1; next }
78                          { print $1 "\tTYPE\t" $6 }' blksize=$blksize
79 }
80
81 # Filter out offsets, which vary by blocksize
82 _filter_od()
83 {
84     tee -a $seqres.full | \
85     sed -e "s/^[0-9A-Fa-f ]\{7,8\}//"
86 }
87
88 _require_scratch
89 unset MKFS_OPTIONS
90 unset XFS_MKFS_OPTIONS
91
92 # we need 512 byte block size, so crc's are turned off
93 _scratch_mkfs_xfs -m crc=0 -b size=$blksize >/dev/null 2>&1
94 _scratch_mount
95
96 # 512b block / 4k page example:
97 #
98 #1) Write 1k of data (buffered):
99 #
100 # |1111|1111|
101 #
102 # 2) ftruncate back to 256 bytes:
103 #
104 # |1100|
105 #
106 # 3) ftruncate out to 4k: ("H" means hole (expected))
107 #
108 # |1100|HHHH|HHHH|HHHH|HHHH|HHHH|HHHH|HHHH|
109 #
110 # So we should have 1 block of data/0, 7 blocks of holes.
111 #
112 # 4) check what's there with a direct IO read
113 #
114 # In fact what I get is 1 block of data/0, 1 block of 0's, and 7 blocks of
115 # garbage:
116 #
117 # |1100|0000|GGGG|GGGG|GGGG|GGGG|GGGG|GGGG|
118 #
119 # The garbage is in fact stale data from the disk.
120 #
121 # Check that we don't get stale data and that the hole is a hole:
122
123 echo "== Test 1 =="
124 # Write, truncate in, truncate out
125 xfs_io \
126 -c "pwrite -S 0x11 -b `expr $pgsize / 2`  0 `expr $pgsize / 2`" \
127 -c "truncate `expr $blksize / 2`" \
128 -c "truncate $pgsize" \
129 -t -f $SCRATCH_MNT/testfile1 >> $seqres.full
130
131 # directio read of entire file
132 xfs_io \
133 -c "pread 0 $pgsize" \
134 -d $SCRATCH_MNT/testfile1 >> $seqres.full
135
136 xfs_bmap -v $SCRATCH_MNT/testfile1 | _filter_bmap
137 od -x $SCRATCH_MNT/testfile1 | _filter_od
138
139 # Similar but write another block to create block/hole/block/hole
140
141 echo "== Test 2 =="
142 # Write, truncate in, truncate out, write to middle
143 xfs_io \
144 -c "pwrite -S 0x11 -b `expr $pgsize / 2`  0 `expr $pgsize / 2`" \
145 -c "truncate `expr $blksize / 2`" \
146 -c "truncate $pgsize" \
147 -c "pwrite -S 0x22 -b $blksize `expr $blksize \* 4` $blksize" \
148 -t -f $SCRATCH_MNT/testfile2 >> $seqres.full
149
150 # directio read of entire file
151 xfs_io \
152 -c "pread 0 $pgsize" \
153 -d $SCRATCH_MNT/testfile2 >> $seqres.full
154
155 xfs_bmap -v $SCRATCH_MNT/testfile2 | _filter_bmap
156 od -x $SCRATCH_MNT/testfile2 | _filter_od
157
158 # 512 byte block / 4k page example:
159
160 # direct write 1 page (8 blocks) of "0x11" to 0x1000
161 # map read 1 block, 512 (0x200) at 0
162 # truncate to half a block, 256 (0x100)
163 # truncate to block+1, 513 (0x201)
164 # direct write "0x22" for 1 block at offset 2048 (0x800)
165
166 # |1111|1111|1111|1111|1111|1111|1111|1111|     Write 1's
167 # |MRMR|1111|1111|1111|1111|1111|1111|1111|     mapread
168 # |11--|                                        truncate down
169 # |1100|0---|                                   truncate up, block+1
170 # |    |    |HHHH|HHHH|2222|                    Write 2's (extending)
171
172 #           |uptodate?|
173 # |1100|0000|1111|1111|2222|----|----|----|     <- potential badness
174
175 # We're looking for this badness due to mapping over a hole:
176 # Exposes stale data from 0x400 (1024) through 0x800 (2048)
177
178 # 00000000  11 11 11 11 11 11 11 11  11 11 11 11 11 11 11 11  |................|
179 # *
180 # 00000100  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
181 # *
182 # 00000400  11 11 11 11 11 11 11 11  11 11 11 11 11 11 11 11  |................| <- BAD
183 # *
184 # 00000800  22 22 22 22 22 22 22 22  22 22 22 22 22 22 22 22  |""""""""""""""""|
185 # *
186 # 00000a00
187
188 # We *should* get:
189 # |1100|HHHH|HHHH|HHHH|2222|----|----|----|
190
191 echo "== Test 3 =="
192 xfs_io \
193 -c "pwrite -S 0x11 -b $pgsize 0 $pgsize" \
194 -c "mmap -r 0 $blksize" -c "mread 0 $blksize" -c "munmap" \
195 -c "truncate `expr $blksize / 2`" \
196 -c "truncate `expr $blksize + 1`" \
197 -c "pwrite -S 0x22 -b $blksize `expr $pgsize / 2` $blksize" \
198 -t -d -f $SCRATCH_MNT/testfile3 >> $seqres.full
199
200 xfs_bmap -v $SCRATCH_MNT/testfile3 | _filter_bmap
201 od -x $SCRATCH_MNT/testfile3 | _filter_od
202
203 # Now try the same thing but write a sector in the middle of that hole
204 # If things go badly stale data will be exposed either side.
205 # This is most interesting for block size > 512 (page size > 4096)
206
207 # We *should* get:
208 # |1100|HHHH|33HH|HHHH|2222|----|----|----|
209
210 echo "== Test 4 =="
211 xfs_io \
212 -c "pwrite -S 0x11 -b $pgsize 0 $pgsize" \
213 -c "mmap -r 0 $blksize" -c "mread 0 $blksize" -c "munmap" \
214 -c "truncate `expr $blksize / 2`" \
215 -c "truncate `expr $blksize + 1`" \
216 -c "pwrite -S 0x22 -b $blksize `expr $pgsize / 2` $blksize" \
217 -c "pwrite -S 0x33 -b $secsize `expr $blksize \* 2` $secsize" \
218 -t -d -f $SCRATCH_MNT/testfile4 >> $seqres.full
219
220 xfs_bmap -v $SCRATCH_MNT/testfile4 | _filter_bmap
221 od -x $SCRATCH_MNT/testfile4 | _filter_od
222
223 # success, all done
224 status=0
225 exit