ui/curses.c: Ensure we don't read off the end of curses2qemu array
[qemu/kevin.git] / tests / qemu-iotests / 112
blob28eb9aae93d9fd11b784bbfe6cb2b840964619b6
1 #!/bin/bash
3 # Test cases for different refcount_bits values
5 # Copyright (C) 2015 Red Hat, Inc.
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
21 # creator
22 owner=mreitz@redhat.com
24 seq="$(basename $0)"
25 echo "QA output created by $seq"
27 here="$PWD"
28 status=1 # failure is the default!
30 _cleanup()
32 _cleanup_test_img
34 trap "_cleanup; exit \$status" 0 1 2 3 15
36 # get standard environment, filters and checks
37 . ./common.rc
38 . ./common.filter
40 # This tests qcow2-specific low-level functionality
41 _supported_fmt qcow2
42 _supported_proto file
43 _supported_os Linux
44 # This test will set refcount_bits on its own which would conflict with the
45 # manual setting; compat will be overridden as well
46 _unsupported_imgopts refcount_bits 'compat=0.10'
48 function print_refcount_bits()
50 $QEMU_IMG info "$TEST_IMG" | sed -n '/refcount bits:/ s/^ *//p'
53 echo
54 echo '=== refcount_bits limits ==='
55 echo
57 # Must be positive (non-zero)
58 IMGOPTS="$IMGOPTS,refcount_bits=0" _make_test_img 64M
59 # Must be positive (non-negative)
60 IMGOPTS="$IMGOPTS,refcount_bits=-1" _make_test_img 64M
61 # May not exceed 64
62 IMGOPTS="$IMGOPTS,refcount_bits=128" _make_test_img 64M
63 # Must be a power of two
64 IMGOPTS="$IMGOPTS,refcount_bits=42" _make_test_img 64M
66 # 1 is the minimum
67 IMGOPTS="$IMGOPTS,refcount_bits=1" _make_test_img 64M
68 print_refcount_bits
70 # 64 is the maximum
71 IMGOPTS="$IMGOPTS,refcount_bits=64" _make_test_img 64M
72 print_refcount_bits
74 # 16 is the default
75 _make_test_img 64M
76 print_refcount_bits
78 echo
79 echo '=== refcount_bits and compat=0.10 ==='
80 echo
82 # Should work
83 IMGOPTS="$IMGOPTS,compat=0.10,refcount_bits=16" _make_test_img 64M
84 print_refcount_bits
86 # Should not work
87 IMGOPTS="$IMGOPTS,compat=0.10,refcount_bits=1" _make_test_img 64M
88 IMGOPTS="$IMGOPTS,compat=0.10,refcount_bits=64" _make_test_img 64M
91 echo
92 echo '=== Snapshot limit on refcount_bits=1 ==='
93 echo
95 IMGOPTS="$IMGOPTS,refcount_bits=1" _make_test_img 64M
96 print_refcount_bits
98 $QEMU_IO -c 'write 0 512' "$TEST_IMG" | _filter_qemu_io
100 # Should fail for now; in the future, this might be supported by automatically
101 # copying all clusters with overflowing refcount
102 $QEMU_IMG snapshot -c foo "$TEST_IMG"
104 # The new L1 table could/should be leaked
105 _check_test_img
107 echo
108 echo '=== Snapshot limit on refcount_bits=2 ==='
109 echo
111 IMGOPTS="$IMGOPTS,refcount_bits=2" _make_test_img 64M
112 print_refcount_bits
114 $QEMU_IO -c 'write 0 512' "$TEST_IMG" | _filter_qemu_io
116 # Should succeed
117 $QEMU_IMG snapshot -c foo "$TEST_IMG"
118 $QEMU_IMG snapshot -c bar "$TEST_IMG"
119 # Should fail (4th reference)
120 $QEMU_IMG snapshot -c baz "$TEST_IMG"
122 # The new L1 table could/should be leaked
123 _check_test_img
125 echo
126 echo '=== Compressed clusters with refcount_bits=1 ==='
127 echo
129 IMGOPTS="$IMGOPTS,refcount_bits=1" _make_test_img 64M
130 print_refcount_bits
132 # Both should fit into a single host cluster; instead of failing to increase the
133 # refcount of that cluster, qemu should just allocate a new cluster and make
134 # this operation succeed
135 $QEMU_IO -c 'write -P 0 -c 0 64k' \
136 -c 'write -P 1 -c 64k 64k' \
137 "$TEST_IMG" | _filter_qemu_io
139 _check_test_img
141 echo
142 echo '=== MSb set in 64 bit refcount ==='
143 echo
145 IMGOPTS="$IMGOPTS,refcount_bits=64" _make_test_img 64M
146 print_refcount_bits
148 $QEMU_IO -c 'write 0 512' "$TEST_IMG" | _filter_qemu_io
150 # Set the MSb in the refblock entry of the data cluster
151 poke_file "$TEST_IMG" $((0x20028)) "\x80\x00\x00\x00\x00\x00\x00\x00"
153 # Clear OFLAG_COPIED in the L2 entry of the data cluster
154 poke_file "$TEST_IMG" $((0x40000)) "\x00\x00\x00\x00\x00\x05\x00\x00"
156 # Try to write to that cluster (should work, even though the MSb is set)
157 $QEMU_IO -c 'write 0 512' "$TEST_IMG" | _filter_qemu_io
159 echo
160 echo '=== Snapshot on maximum 64 bit refcount value ==='
161 echo
163 IMGOPTS="$IMGOPTS,refcount_bits=64" _make_test_img 64M
164 print_refcount_bits
166 $QEMU_IO -c 'write 0 512' "$TEST_IMG" | _filter_qemu_io
168 # Set the refblock entry to the maximum value possible
169 poke_file "$TEST_IMG" $((0x20028)) "\xff\xff\xff\xff\xff\xff\xff\xff"
171 # Clear OFLAG_COPIED in the L2 entry of the data cluster
172 poke_file "$TEST_IMG" $((0x40000)) "\x00\x00\x00\x00\x00\x05\x00\x00"
174 # Try a snapshot (should correctly identify the overflow; may work in the future
175 # by falling back to COW)
176 $QEMU_IMG snapshot -c foo "$TEST_IMG"
178 # The new L1 table could/should be leaked; and obviously the data cluster is
179 # leaked (refcount=UINT64_MAX reference=1)
180 _check_test_img
182 echo
183 echo '=== Amend from refcount_bits=16 to refcount_bits=1 ==='
184 echo
186 _make_test_img 64M
187 print_refcount_bits
189 $QEMU_IO -c 'write 16M 32M' "$TEST_IMG" | _filter_qemu_io
190 $QEMU_IMG amend -o refcount_bits=1 "$TEST_IMG"
191 _check_test_img
192 print_refcount_bits
194 echo
195 echo '=== Amend from refcount_bits=1 to refcount_bits=64 ==='
196 echo
198 $QEMU_IMG amend -o refcount_bits=64 "$TEST_IMG"
199 _check_test_img
200 print_refcount_bits
202 echo
203 echo '=== Amend to compat=0.10 ==='
204 echo
206 # Should not work because refcount_bits needs to be 16 for compat=0.10
207 $QEMU_IMG amend -o compat=0.10 "$TEST_IMG"
208 print_refcount_bits
209 # Should work
210 $QEMU_IMG amend -o compat=0.10,refcount_bits=16 "$TEST_IMG"
211 _check_test_img
212 print_refcount_bits
214 # Get back to compat=1.1 and refcount_bits=16
215 $QEMU_IMG amend -o compat=1.1 "$TEST_IMG"
216 print_refcount_bits
217 # Should not work
218 $QEMU_IMG amend -o refcount_bits=32,compat=0.10 "$TEST_IMG"
219 print_refcount_bits
221 echo
222 echo '=== Amend with snapshot ==='
223 echo
225 $QEMU_IMG snapshot -c foo "$TEST_IMG"
226 # Just to have different refcounts across the image
227 $QEMU_IO -c 'write 0 16M' "$TEST_IMG" | _filter_qemu_io
229 # Should not work (may work in the future by first decreasing all refcounts so
230 # they fit into the target range by copying them)
231 $QEMU_IMG amend -o refcount_bits=1 "$TEST_IMG"
232 _check_test_img
233 print_refcount_bits
235 # Should work
236 $QEMU_IMG amend -o refcount_bits=2 "$TEST_IMG"
237 _check_test_img
238 print_refcount_bits
240 echo
241 echo '=== Testing too many references for check ==='
242 echo
244 IMGOPTS="$IMGOPTS,refcount_bits=1" _make_test_img 64M
245 print_refcount_bits
247 # This cluster should be created at 0x50000
248 $QEMU_IO -c 'write 0 64k' "$TEST_IMG" | _filter_qemu_io
249 # Now make the second L2 entry (the L2 table should be at 0x40000) point to that
250 # cluster, so we have two references
251 poke_file "$TEST_IMG" $((0x40008)) "\x80\x00\x00\x00\x00\x05\x00\x00"
253 # This should say "please use amend"
254 _check_test_img -r all
256 # So we do that
257 $QEMU_IMG amend -o refcount_bits=2 "$TEST_IMG"
258 print_refcount_bits
260 # And try again
261 _check_test_img -r all
263 echo
264 echo '=== Multiple walks necessary during amend ==='
265 echo
267 IMGOPTS="$IMGOPTS,refcount_bits=1,cluster_size=512" _make_test_img 64k
269 # Cluster 0 is the image header, clusters 1 to 4 are used by the L1 table, a
270 # single L2 table, the reftable and a single refblock. This creates 58 data
271 # clusters (actually, the L2 table is created here, too), so in total there are
272 # then 63 used clusters in the image. With a refcount width of 64, one refblock
273 # describes 64 clusters (512 bytes / 64 bits/entry = 64 entries), so this will
274 # make the first refblock in the amended image have exactly one free entry.
275 $QEMU_IO -c "write 0 $((58 * 512))" "$TEST_IMG" | _filter_qemu_io
277 # Now change the refcount width; since the first new refblock will have exactly
278 # one free entry, that entry will be used to store its own reference. No other
279 # refblocks are needed, so then the new reftable will be allocated; since the
280 # first new refblock is completely filled up, this will require a new refblock
281 # which is why the refcount width changing function will need to run through
282 # everything one more time until the allocations are stable.
283 # Having more walks than usual should be visible as regressing progress (from
284 # 66.67 % (2/3 walks) to 50.00 % (2/4 walks)).
285 $QEMU_IMG amend -o refcount_bits=64 -p "$TEST_IMG" | tr '\r' '\n' \
286 | grep -A 1 '66.67'
287 print_refcount_bits
289 _check_test_img
292 # success, all done
293 echo '*** done'
294 rm -f $seq.full
295 status=0