virtio-ccw-input: fix description
[qemu/ar7.git] / tests / qemu-iotests / 271
blobe242b28b586682b2fdfb28d3b2e085adab29582e
1 #!/usr/bin/env bash
3 # Test qcow2 images with extended L2 entries
5 # Copyright (C) 2019-2020 Igalia, S.L.
6 # Author: Alberto Garcia <berto@igalia.com>
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 2 of the License, or
11 # (at your option) any later version.
13 # This program is distributed in the hope that it will 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.
18 # You should have received a copy of the GNU General Public License
19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
22 # creator
23 owner=berto@igalia.com
25 seq="$(basename $0)"
26 echo "QA output created by $seq"
28 here="$PWD"
29 status=1 # failure is the default!
31 _cleanup()
33 _cleanup_test_img
34 rm -f "$TEST_IMG.raw"
36 trap "_cleanup; exit \$status" 0 1 2 3 15
38 # get standard environment, filters and checks
39 . ./common.rc
40 . ./common.filter
42 _supported_fmt qcow2
43 _supported_proto file nfs
44 _supported_os Linux
45 _unsupported_imgopts extended_l2 compat=0.10 cluster_size data_file refcount_bits=1[^0-9]
47 l2_offset=$((0x40000))
49 _verify_img()
51 $QEMU_IMG compare "$TEST_IMG" "$TEST_IMG.raw" | grep -v 'Images are identical'
52 $QEMU_IMG check "$TEST_IMG" | _filter_qemu_img_check | \
53 grep -v 'No errors were found on the image'
56 # Compare the bitmap of an extended L2 entry against an expected value
57 _verify_l2_bitmap()
59 entry_no="$1" # L2 entry number, starting from 0
60 expected_alloc="$alloc" # Space-separated list of allocated subcluster indexes
61 expected_zero="$zero" # Space-separated list of zero subcluster indexes
63 offset=$(($l2_offset + $entry_no * 16))
64 entry=$(peek_file_be "$TEST_IMG" $offset 8)
65 offset=$(($offset + 8))
66 bitmap=$(peek_file_be "$TEST_IMG" $offset 8)
68 expected_bitmap=0
69 for bit in $expected_alloc; do
70 expected_bitmap=$(($expected_bitmap | (1 << $bit)))
71 done
72 for bit in $expected_zero; do
73 expected_bitmap=$(($expected_bitmap | (1 << (32 + $bit))))
74 done
75 printf -v expected_bitmap "%u" $expected_bitmap # Convert to unsigned
77 printf "L2 entry #%d: 0x%016x %016x\n" "$entry_no" "$entry" "$bitmap"
78 if [ "$bitmap" != "$expected_bitmap" ]; then
79 printf "ERROR: expecting bitmap 0x%016x\n" "$expected_bitmap"
83 # This should be called as _run_test c=XXX sc=XXX off=XXX len=XXX cmd=XXX
84 # c: cluster number (0 if unset)
85 # sc: subcluster number inside cluster @c (0 if unset)
86 # off: offset inside subcluster @sc, in kilobytes (0 if unset)
87 # len: request length, passed directly to qemu-io (e.g: 256, 4k, 1M, ...)
88 # cmd: the command to pass to qemu-io, must be one of
89 # write -> write
90 # zero -> write -z
91 # unmap -> write -z -u
92 # compress -> write -c
93 # discard -> discard
94 _run_test()
96 unset c sc off len cmd
97 for var in "$@"; do eval "$var"; done
98 case "${cmd:-write}" in
99 zero)
100 cmd="write -q -z";;
101 unmap)
102 cmd="write -q -z -u";;
103 compress)
104 pat=$((${pat:-0} + 1))
105 cmd="write -q -c -P ${pat}";;
106 write)
107 pat=$((${pat:-0} + 1))
108 cmd="write -q -P ${pat}";;
109 discard)
110 cmd="discard -q";;
112 echo "Unknown option $cmd"
113 exit 1;;
114 esac
115 c="${c:-0}"
116 sc="${sc:-0}"
117 off="${off:-0}"
118 offset="$(($c * 64 + $sc * 2 + $off))"
119 [ "$offset" != 0 ] && offset="${offset}k"
120 cmd="$cmd ${offset} ${len}"
121 raw_cmd=$(echo $cmd | sed s/-c//) # Raw images don't support -c
122 echo $cmd | sed 's/-P [0-9][0-9]\?/-P PATTERN/'
123 $QEMU_IO -c "$cmd" "$TEST_IMG" | _filter_qemu_io
124 $QEMU_IO -c "$raw_cmd" -f raw "$TEST_IMG.raw" | _filter_qemu_io
125 _verify_img
126 _verify_l2_bitmap "$c"
129 _reset_img()
131 size="$1"
132 $QEMU_IMG create -f raw "$TEST_IMG.raw" "$size" | _filter_img_create
133 if [ "$use_backing_file" = "yes" ]; then
134 $QEMU_IMG create -f raw "$TEST_IMG.base" "$size" | _filter_img_create
135 $QEMU_IO -c "write -q -P 0xFF 0 $size" -f raw "$TEST_IMG.base" | _filter_qemu_io
136 $QEMU_IO -c "write -q -P 0xFF 0 $size" -f raw "$TEST_IMG.raw" | _filter_qemu_io
137 _make_test_img -o extended_l2=on -F raw -b "$TEST_IMG.base" "$size"
138 else
139 _make_test_img -o extended_l2=on "$size"
143 ############################################################
144 ############################################################
145 ############################################################
147 # Test that writing to an image with subclusters produces the expected
148 # results, in images with and without backing files
149 for use_backing_file in yes no; do
150 echo
151 echo "### Standard write tests (backing file: $use_backing_file) ###"
152 echo
153 _reset_img 1M
154 ### Write subcluster #0 (beginning of subcluster) ###
155 alloc="0"; zero=""
156 _run_test sc=0 len=1k
158 ### Write subcluster #1 (middle of subcluster) ###
159 alloc="0 1"; zero=""
160 _run_test sc=1 off=1 len=512
162 ### Write subcluster #2 (end of subcluster) ###
163 alloc="0 1 2"; zero=""
164 _run_test sc=2 off=1 len=1k
166 ### Write subcluster #3 (full subcluster) ###
167 alloc="0 1 2 3"; zero=""
168 _run_test sc=3 len=2k
170 ### Write subclusters #4-6 (full subclusters) ###
171 alloc="$(seq 0 6)"; zero=""
172 _run_test sc=4 len=6k
174 ### Write subclusters #7-9 (partial subclusters) ###
175 alloc="$(seq 0 9)"; zero=""
176 _run_test sc=7 off=1 len=4k
178 ### Write subcluster #16 (partial subcluster) ###
179 alloc="$(seq 0 9) 16"; zero=""
180 _run_test sc=16 len=1k
182 ### Write subcluster #31-#33 (cluster overlap) ###
183 alloc="$(seq 0 9) 16 31"; zero=""
184 _run_test sc=31 off=1 len=4k
185 alloc="0 1" ; zero=""
186 _verify_l2_bitmap 1
188 ### Zero subcluster #1
189 alloc="0 $(seq 2 9) 16 31"; zero="1"
190 _run_test sc=1 len=2k cmd=zero
192 ### Zero cluster #0
193 alloc=""; zero="$(seq 0 31)"
194 _run_test sc=0 len=64k cmd=zero
196 ### Fill cluster #0 with data
197 alloc="$(seq 0 31)"; zero=""
198 _run_test sc=0 len=64k
200 ### Zero and unmap half of cluster #0 (this won't unmap it)
201 alloc="$(seq 16 31)"; zero="$(seq 0 15)"
202 _run_test sc=0 len=32k cmd=unmap
204 ### Zero and unmap cluster #0
205 alloc=""; zero="$(seq 0 31)"
206 _run_test sc=0 len=64k cmd=unmap
208 ### Write subcluster #1 (middle of subcluster)
209 alloc="1"; zero="0 $(seq 2 31)"
210 _run_test sc=1 off=1 len=512
212 ### Fill cluster #0 with data
213 alloc="$(seq 0 31)"; zero=""
214 _run_test sc=0 len=64k
216 ### Discard cluster #0
217 alloc=""; zero="$(seq 0 31)"
218 _run_test sc=0 len=64k cmd=discard
220 ### Write compressed data to cluster #0
221 alloc=""; zero=""
222 _run_test sc=0 len=64k cmd=compress
224 ### Write subcluster #1 (middle of subcluster)
225 alloc="$(seq 0 31)"; zero=""
226 _run_test sc=1 off=1 len=512
227 done
229 ############################################################
230 ############################################################
231 ############################################################
233 # calculate_l2_meta() checks if none of the clusters affected by a
234 # write operation need COW or changes to their L2 metadata and simply
235 # returns when they don't. This is a test for that optimization.
236 # Here clusters #0-#3 are overwritten but only #1 and #2 need changes.
237 echo
238 echo '### Overwriting several clusters without COW ###'
239 echo
240 use_backing_file="no" _reset_img 1M
241 # Write cluster #0, subclusters #12-#31
242 alloc="$(seq 12 31)"; zero=""
243 _run_test sc=12 len=40k
245 # Write cluster #1, subcluster #13
246 alloc="13"; zero=""
247 _run_test c=1 sc=13 len=2k
249 # Zeroize cluster #2, subcluster #14
250 alloc="14"; zero=""
251 _run_test c=2 sc=14 len=2k
252 alloc=""; zero="14"
253 _run_test c=2 sc=14 len=2k cmd=zero
255 # Write cluster #3, subclusters #0-#16
256 alloc="$(seq 0 16)"; zero=""
257 _run_test c=3 sc=0 len=34k
259 # Write from cluster #0, subcluster #12 to cluster #3, subcluster #11
260 alloc="$(seq 12 31)"; zero=""
261 _run_test sc=12 len=192k
262 alloc="$(seq 0 31)"; zero=""
263 _verify_l2_bitmap 1
264 _verify_l2_bitmap 2
266 alloc="$(seq 0 16)"; zero=""
267 _verify_l2_bitmap 3
269 ############################################################
270 ############################################################
271 ############################################################
273 # Test different patterns of writing zeroes
274 for use_backing_file in yes no; do
275 echo
276 echo "### Writing zeroes 1: unallocated clusters (backing file: $use_backing_file) ###"
277 echo
278 # Note that the image size is not a multiple of the cluster size
279 _reset_img 2083k
281 # Cluster-aligned request from clusters #0 to #2
282 alloc=""; zero="$(seq 0 31)"
283 _run_test c=0 sc=0 len=192k cmd=zero
284 _verify_l2_bitmap 1
285 _verify_l2_bitmap 2
287 # Subcluster-aligned request from clusters #3 to #5
288 alloc=""; zero="$(seq 16 31)"
289 _run_test c=3 sc=16 len=128k cmd=zero
290 alloc=""; zero="$(seq 0 31)"
291 _verify_l2_bitmap 4
292 alloc=""; zero="$(seq 0 15)"
293 _verify_l2_bitmap 5
295 # Unaligned request from clusters #6 to #8
296 if [ "$use_backing_file" = "yes" ]; then
297 alloc="15"; zero="$(seq 16 31)" # copy-on-write happening here
298 else
299 alloc=""; zero="$(seq 15 31)"
301 _run_test c=6 sc=15 off=1 len=128k cmd=zero
302 alloc=""; zero="$(seq 0 31)"
303 _verify_l2_bitmap 7
304 if [ "$use_backing_file" = "yes" ]; then
305 alloc="15"; zero="$(seq 0 14)" # copy-on-write happening here
306 else
307 alloc=""; zero="$(seq 0 15)"
309 _verify_l2_bitmap 8
311 echo
312 echo "### Writing zeroes 2: allocated clusters (backing file: $use_backing_file) ###"
313 echo
314 alloc="$(seq 0 31)"; zero=""
315 _run_test c=9 sc=0 len=576k
316 _verify_l2_bitmap 10
317 _verify_l2_bitmap 11
318 _verify_l2_bitmap 12
319 _verify_l2_bitmap 13
320 _verify_l2_bitmap 14
321 _verify_l2_bitmap 15
322 _verify_l2_bitmap 16
323 _verify_l2_bitmap 17
325 # Cluster-aligned request from clusters #9 to #11
326 alloc=""; zero="$(seq 0 31)"
327 _run_test c=9 sc=0 len=192k cmd=zero
328 _verify_l2_bitmap 10
329 _verify_l2_bitmap 11
331 # Subcluster-aligned request from clusters #12 to #14
332 alloc="$(seq 0 15)"; zero="$(seq 16 31)"
333 _run_test c=12 sc=16 len=128k cmd=zero
334 alloc=""; zero="$(seq 0 31)"
335 _verify_l2_bitmap 13
336 alloc="$(seq 16 31)"; zero="$(seq 0 15)"
337 _verify_l2_bitmap 14
339 # Unaligned request from clusters #15 to #17
340 alloc="$(seq 0 15)"; zero="$(seq 16 31)"
341 _run_test c=15 sc=15 off=1 len=128k cmd=zero
342 alloc=""; zero="$(seq 0 31)"
343 _verify_l2_bitmap 16
344 alloc="$(seq 15 31)"; zero="$(seq 0 14)"
345 _verify_l2_bitmap 17
347 echo
348 echo "### Writing zeroes 3: compressed clusters (backing file: $use_backing_file) ###"
349 echo
350 alloc=""; zero=""
351 for c in $(seq 18 28); do
352 _run_test c=$c sc=0 len=64k cmd=compress
353 done
355 # Cluster-aligned request from clusters #18 to #20
356 alloc=""; zero="$(seq 0 31)"
357 _run_test c=18 sc=0 len=192k cmd=zero
358 _verify_l2_bitmap 19
359 _verify_l2_bitmap 20
361 # Subcluster-aligned request from clusters #21 to #23.
362 # We cannot partially zero a compressed cluster so the code
363 # returns -ENOTSUP, which means copy-on-write of the compressed
364 # data and fill the rest with actual zeroes on disk.
365 # TODO: cluster #22 should use the 'all zeroes' bits.
366 alloc="$(seq 0 31)"; zero=""
367 _run_test c=21 sc=16 len=128k cmd=zero
368 _verify_l2_bitmap 22
369 _verify_l2_bitmap 23
371 # Unaligned request from clusters #24 to #26
372 # In this case QEMU internally sends a 1k request followed by a
373 # subcluster-aligned 128k request. The first request decompresses
374 # cluster #24, but that's not enough to perform the second request
375 # efficiently because it partially writes to cluster #26 (which is
376 # compressed) so we hit the same problem as before.
377 alloc="$(seq 0 31)"; zero=""
378 _run_test c=24 sc=15 off=1 len=129k cmd=zero
379 _verify_l2_bitmap 25
380 _verify_l2_bitmap 26
382 # Unaligned request from clusters #27 to #29
383 # Similar to the previous case, but this time the tail of the
384 # request does not correspond to a compressed cluster, so it can
385 # be zeroed efficiently.
386 # Note that the very last subcluster is partially written, so if
387 # there's a backing file we need to perform cow.
388 alloc="$(seq 0 15)"; zero="$(seq 16 31)"
389 _run_test c=27 sc=15 off=1 len=128k cmd=zero
390 alloc=""; zero="$(seq 0 31)"
391 _verify_l2_bitmap 28
392 if [ "$use_backing_file" = "yes" ]; then
393 alloc="15"; zero="$(seq 0 14)" # copy-on-write happening here
394 else
395 alloc=""; zero="$(seq 0 15)"
397 _verify_l2_bitmap 29
399 echo
400 echo "### Writing zeroes 4: other tests (backing file: $use_backing_file) ###"
401 echo
402 # Unaligned request in the middle of cluster #30.
403 # If there's a backing file we need to allocate and do
404 # copy-on-write on the partially zeroed subclusters.
405 # If not we can set the 'all zeroes' bit on them.
406 if [ "$use_backing_file" = "yes" ]; then
407 alloc="15 19"; zero="$(seq 16 18)" # copy-on-write happening here
408 else
409 alloc=""; zero="$(seq 15 19)"
411 _run_test c=30 sc=15 off=1 len=8k cmd=zero
413 # Fill the last cluster with zeroes, up to the end of the image
414 # (the image size is not a multiple of the cluster or subcluster size).
415 alloc=""; zero="$(seq 0 17)"
416 _run_test c=32 sc=0 len=35k cmd=zero
417 done
419 ############################################################
420 ############################################################
421 ############################################################
423 # Zero + unmap
424 for use_backing_file in yes no; do
425 echo
426 echo "### Zero + unmap 1: allocated clusters (backing file: $use_backing_file) ###"
427 echo
428 # Note that the image size is not a multiple of the cluster size
429 _reset_img 2083k
430 alloc="$(seq 0 31)"; zero=""
431 _run_test c=9 sc=0 len=576k
432 _verify_l2_bitmap 10
433 _verify_l2_bitmap 11
434 _verify_l2_bitmap 12
435 _verify_l2_bitmap 13
436 _verify_l2_bitmap 14
437 _verify_l2_bitmap 15
438 _verify_l2_bitmap 16
439 _verify_l2_bitmap 17
441 # Cluster-aligned request from clusters #9 to #11
442 alloc=""; zero="$(seq 0 31)"
443 _run_test c=9 sc=0 len=192k cmd=unmap
444 _verify_l2_bitmap 10
445 _verify_l2_bitmap 11
447 # Subcluster-aligned request from clusters #12 to #14
448 alloc="$(seq 0 15)"; zero="$(seq 16 31)"
449 _run_test c=12 sc=16 len=128k cmd=unmap
450 alloc=""; zero="$(seq 0 31)"
451 _verify_l2_bitmap 13
452 alloc="$(seq 16 31)"; zero="$(seq 0 15)"
453 _verify_l2_bitmap 14
455 # Unaligned request from clusters #15 to #17
456 alloc="$(seq 0 15)"; zero="$(seq 16 31)"
457 _run_test c=15 sc=15 off=1 len=128k cmd=unmap
458 alloc=""; zero="$(seq 0 31)"
459 _verify_l2_bitmap 16
460 alloc="$(seq 15 31)"; zero="$(seq 0 14)"
461 _verify_l2_bitmap 17
463 echo
464 echo "### Zero + unmap 2: compressed clusters (backing file: $use_backing_file) ###"
465 echo
466 alloc=""; zero=""
467 for c in $(seq 18 28); do
468 _run_test c=$c sc=0 len=64k cmd=compress
469 done
471 # Cluster-aligned request from clusters #18 to #20
472 alloc=""; zero="$(seq 0 31)"
473 _run_test c=18 sc=0 len=192k cmd=unmap
474 _verify_l2_bitmap 19
475 _verify_l2_bitmap 20
477 # Subcluster-aligned request from clusters #21 to #23.
478 # We cannot partially zero a compressed cluster so the code
479 # returns -ENOTSUP, which means copy-on-write of the compressed
480 # data and fill the rest with actual zeroes on disk.
481 # TODO: cluster #22 should use the 'all zeroes' bits.
482 alloc="$(seq 0 31)"; zero=""
483 _run_test c=21 sc=16 len=128k cmd=unmap
484 _verify_l2_bitmap 22
485 _verify_l2_bitmap 23
487 # Unaligned request from clusters #24 to #26
488 # In this case QEMU internally sends a 1k request followed by a
489 # subcluster-aligned 128k request. The first request decompresses
490 # cluster #24, but that's not enough to perform the second request
491 # efficiently because it partially writes to cluster #26 (which is
492 # compressed) so we hit the same problem as before.
493 alloc="$(seq 0 31)"; zero=""
494 _run_test c=24 sc=15 off=1 len=129k cmd=unmap
495 _verify_l2_bitmap 25
496 _verify_l2_bitmap 26
498 # Unaligned request from clusters #27 to #29
499 # Similar to the previous case, but this time the tail of the
500 # request does not correspond to a compressed cluster, so it can
501 # be zeroed efficiently.
502 # Note that the very last subcluster is partially written, so if
503 # there's a backing file we need to perform cow.
504 alloc="$(seq 0 15)"; zero="$(seq 16 31)"
505 _run_test c=27 sc=15 off=1 len=128k cmd=unmap
506 alloc=""; zero="$(seq 0 31)"
507 _verify_l2_bitmap 28
508 if [ "$use_backing_file" = "yes" ]; then
509 alloc="15"; zero="$(seq 0 14)" # copy-on-write happening here
510 else
511 alloc=""; zero="$(seq 0 15)"
513 _verify_l2_bitmap 29
514 done
516 ############################################################
517 ############################################################
518 ############################################################
520 # Test qcow2_cluster_discard() with full and normal discards
521 for use_backing_file in yes no; do
522 echo
523 echo "### Discarding clusters with non-zero bitmaps (backing file: $use_backing_file) ###"
524 echo
525 if [ "$use_backing_file" = "yes" ]; then
526 _make_test_img -o extended_l2=on -F raw -b "$TEST_IMG.base" 1M
527 else
528 _make_test_img -o extended_l2=on 1M
530 # Write clusters #0-#2 and then discard them
531 $QEMU_IO -c 'write -q 0 128k' "$TEST_IMG"
532 $QEMU_IO -c 'discard -q 0 128k' "$TEST_IMG"
533 # 'qemu-io discard' doesn't do a full discard, it zeroizes the
534 # cluster, so both clusters have all zero bits set now
535 alloc=""; zero="$(seq 0 31)"
536 _verify_l2_bitmap 0
537 _verify_l2_bitmap 1
538 # Now mark the 2nd half of the subclusters from cluster #0 as unallocated
539 poke_file "$TEST_IMG" $(($l2_offset+8)) "\x00\x00"
540 # Discard cluster #0 again to see how the zero bits have changed
541 $QEMU_IO -c 'discard -q 0 64k' "$TEST_IMG"
542 # And do a full discard of cluster #1 by shrinking and growing the image
543 $QEMU_IMG resize --shrink "$TEST_IMG" 64k
544 $QEMU_IMG resize "$TEST_IMG" 1M
545 # A normal discard sets all 'zero' bits only if the image has a
546 # backing file, otherwise it won't touch them.
547 if [ "$use_backing_file" = "yes" ]; then
548 alloc=""; zero="$(seq 0 31)"
549 else
550 alloc=""; zero="$(seq 0 15)"
552 _verify_l2_bitmap 0
553 # A full discard should clear the L2 entry completely. However
554 # when growing an image with a backing file the new clusters are
555 # zeroized to hide the stale data from the backing file
556 if [ "$use_backing_file" = "yes" ]; then
557 alloc=""; zero="$(seq 0 31)"
558 else
559 alloc=""; zero=""
561 _verify_l2_bitmap 1
562 done
564 ############################################################
565 ############################################################
566 ############################################################
568 # Test that corrupted L2 entries are detected in both read and write
569 # operations
570 for corruption_test_cmd in read write; do
571 echo
572 echo "### Corrupted L2 entries - $corruption_test_cmd test (allocated) ###"
573 echo
574 echo "# 'cluster is zero' bit set on the standard cluster descriptor"
575 echo
576 # We actually don't consider this a corrupted image.
577 # The 'cluster is zero' bit is unused in extended L2 entries so
578 # QEMU ignores it.
579 # TODO: maybe treat the image as corrupted and make qemu-img check fix it?
580 _make_test_img -o extended_l2=on 1M
581 $QEMU_IO -c 'write -q -P 0x11 0 2k' "$TEST_IMG"
582 poke_file "$TEST_IMG" $(($l2_offset+7)) "\x01"
583 alloc="0"; zero=""
584 _verify_l2_bitmap 0
585 $QEMU_IO -c "$corruption_test_cmd -q -P 0x11 0 1k" "$TEST_IMG"
586 if [ "$corruption_test_cmd" = "write" ]; then
587 alloc="0"; zero=""
589 _verify_l2_bitmap 0
591 echo
592 echo "# Both 'subcluster is zero' and 'subcluster is allocated' bits set"
593 echo
594 _make_test_img -o extended_l2=on 1M
595 # Write from the middle of cluster #0 to the middle of cluster #2
596 $QEMU_IO -c 'write -q 32k 128k' "$TEST_IMG"
597 # Corrupt the L2 entry from cluster #1
598 poke_file_be "$TEST_IMG" $(($l2_offset+24)) 4 1
599 alloc="$(seq 0 31)"; zero="0"
600 _verify_l2_bitmap 1
601 $QEMU_IO -c "$corruption_test_cmd 0 192k" "$TEST_IMG"
603 echo
604 echo "### Corrupted L2 entries - $corruption_test_cmd test (unallocated) ###"
605 echo
606 echo "# 'cluster is zero' bit set on the standard cluster descriptor"
607 echo
608 # We actually don't consider this a corrupted image.
609 # The 'cluster is zero' bit is unused in extended L2 entries so
610 # QEMU ignores it.
611 # TODO: maybe treat the image as corrupted and make qemu-img check fix it?
612 _make_test_img -o extended_l2=on 1M
613 # We want to modify the (empty) L2 entry from cluster #0,
614 # but we write to #4 in order to initialize the L2 table first
615 $QEMU_IO -c 'write -q 256k 1k' "$TEST_IMG"
616 poke_file "$TEST_IMG" $(($l2_offset+7)) "\x01"
617 alloc=""; zero=""
618 _verify_l2_bitmap 0
619 $QEMU_IO -c "$corruption_test_cmd -q 0 1k" "$TEST_IMG"
620 if [ "$corruption_test_cmd" = "write" ]; then
621 alloc="0"; zero=""
623 _verify_l2_bitmap 0
625 echo
626 echo "# 'subcluster is allocated' bit set"
627 echo
628 _make_test_img -o extended_l2=on 1M
629 # We want to corrupt the (empty) L2 entry from cluster #0,
630 # but we write to #4 in order to initialize the L2 table first
631 $QEMU_IO -c 'write -q 256k 1k' "$TEST_IMG"
632 poke_file "$TEST_IMG" $(($l2_offset+15)) "\x01"
633 alloc="0"; zero=""
634 _verify_l2_bitmap 0
635 $QEMU_IO -c "$corruption_test_cmd 0 1k" "$TEST_IMG"
637 echo
638 echo "# Both 'subcluster is zero' and 'subcluster is allocated' bits set"
639 echo
640 _make_test_img -o extended_l2=on 1M
641 # We want to corrupt the (empty) L2 entry from cluster #1,
642 # but we write to #4 in order to initialize the L2 table first
643 $QEMU_IO -c 'write -q 256k 1k' "$TEST_IMG"
644 # Corrupt the L2 entry from cluster #1
645 poke_file_be "$TEST_IMG" $(($l2_offset+24)) 8 $(((1 << 32) | 1))
646 alloc="0"; zero="0"
647 _verify_l2_bitmap 1
648 $QEMU_IO -c "$corruption_test_cmd 0 192k" "$TEST_IMG"
650 echo
651 echo "### Compressed cluster with subcluster bitmap != 0 - $corruption_test_cmd test ###"
652 echo
653 # We actually don't consider this a corrupted image.
654 # The bitmap in compressed clusters is unused so QEMU should just ignore it.
655 _make_test_img -o extended_l2=on 1M
656 $QEMU_IO -c 'write -q -P 11 -c 0 64k' "$TEST_IMG"
657 # Change the L2 bitmap to allocate subcluster #31 and zeroize subcluster #0
658 poke_file "$TEST_IMG" $(($l2_offset+11)) "\x01\x80"
659 alloc="31"; zero="0"
660 _verify_l2_bitmap 0
661 $QEMU_IO -c "$corruption_test_cmd -P 11 0 64k" "$TEST_IMG" | _filter_qemu_io
662 # Writing allocates a new uncompressed cluster so we get a new bitmap
663 if [ "$corruption_test_cmd" = "write" ]; then
664 alloc="$(seq 0 31)"; zero=""
666 _verify_l2_bitmap 0
667 done
669 ############################################################
670 ############################################################
671 ############################################################
673 echo
674 echo "### Detect and repair unaligned clusters ###"
675 echo
676 # Create a backing file and fill it with data
677 $QEMU_IMG create -f raw "$TEST_IMG.base" 128k | _filter_img_create
678 $QEMU_IO -c "write -q -P 0xff 0 128k" -f raw "$TEST_IMG.base" | _filter_qemu_io
680 echo "# Corrupted L2 entry, allocated subcluster #"
681 # Create a new image, allocate a cluster and write some data to it
682 _make_test_img -o extended_l2=on -F raw -b "$TEST_IMG.base"
683 $QEMU_IO -c 'write -q -P 1 4k 2k' "$TEST_IMG"
684 # Corrupt the L2 entry by making the offset unaligned
685 poke_file "$TEST_IMG" "$(($l2_offset+6))" "\x02"
686 # This cannot be repaired, qemu-img check will fail to fix it
687 _check_test_img -r all
688 # Attempting to read the image will still show that it's corrupted
689 $QEMU_IO -c 'read -q 0 2k' "$TEST_IMG"
691 echo "# Corrupted L2 entry, no allocated subclusters #"
692 # Create a new image, allocate a cluster and zeroize subcluster #2
693 _make_test_img -o extended_l2=on -F raw -b "$TEST_IMG.base"
694 $QEMU_IO -c 'write -q -P 1 4k 2k' "$TEST_IMG"
695 $QEMU_IO -c 'write -q -z 4k 2k' "$TEST_IMG"
696 # Corrupt the L2 entry by making the offset unaligned
697 poke_file "$TEST_IMG" "$(($l2_offset+6))" "\x02"
698 # This time none of the subclusters are allocated so we can repair the image
699 _check_test_img -r all
700 # And the data can be read normally
701 $QEMU_IO -c 'read -q -P 0xff 0 4k' "$TEST_IMG"
702 $QEMU_IO -c 'read -q -P 0x00 4k 2k' "$TEST_IMG"
703 $QEMU_IO -c 'read -q -P 0xff 6k 122k' "$TEST_IMG"
705 ############################################################
706 ############################################################
707 ############################################################
709 echo
710 echo "### Image creation options ###"
711 echo
712 echo "# cluster_size < 16k"
713 _make_test_img -o extended_l2=on,cluster_size=8k 1M
715 echo "# backing file and preallocation=metadata"
716 # For preallocation with backing files, create a backing file first
717 $QEMU_IMG create -f raw "$TEST_IMG.base" 1M | _filter_img_create
718 $QEMU_IO -c "write -q -P 0xff 0 1M" -f raw "$TEST_IMG.base" | _filter_qemu_io
720 _make_test_img -o extended_l2=on,preallocation=metadata -F raw -b "$TEST_IMG.base" 512k
721 $QEMU_IMG resize "$TEST_IMG" 1M
722 $QEMU_IO -c 'read -P 0xff 0 512k' "$TEST_IMG" | _filter_qemu_io
723 $QEMU_IO -c 'read -P 0x00 512k 512k' "$TEST_IMG" | _filter_qemu_io
724 $QEMU_IMG map "$TEST_IMG" | _filter_testdir
726 echo "# backing file and preallocation=falloc"
727 _make_test_img -o extended_l2=on,preallocation=falloc -F raw -b "$TEST_IMG.base" 512k
728 $QEMU_IMG resize "$TEST_IMG" 1M
729 $QEMU_IO -c 'read -P 0xff 0 512k' "$TEST_IMG" | _filter_qemu_io
730 $QEMU_IO -c 'read -P 0x00 512k 512k' "$TEST_IMG" | _filter_qemu_io
731 $QEMU_IMG map "$TEST_IMG" | _filter_testdir
733 echo "# backing file and preallocation=full"
734 _make_test_img -o extended_l2=on,preallocation=full -F raw -b "$TEST_IMG.base" 512k
735 $QEMU_IMG resize "$TEST_IMG" 1M
736 $QEMU_IO -c 'read -P 0xff 0 512k' "$TEST_IMG" | _filter_qemu_io
737 $QEMU_IO -c 'read -P 0x00 512k 512k' "$TEST_IMG" | _filter_qemu_io
738 $QEMU_IMG map "$TEST_IMG" | _filter_testdir
740 echo
741 echo "### Image resizing with preallocation and backing files ###"
742 echo
743 # In this case the new subclusters must have the 'all zeroes' bit set
744 echo "# resize --preallocation=metadata"
745 _make_test_img -o extended_l2=on -F raw -b "$TEST_IMG.base" 503k
746 $QEMU_IMG resize --preallocation=metadata "$TEST_IMG" 1013k
747 $QEMU_IO -c 'read -P 0xff 0 503k' "$TEST_IMG" | _filter_qemu_io
748 $QEMU_IO -c 'read -P 0x00 503k 510k' "$TEST_IMG" | _filter_qemu_io
750 # In this case and the next one the new subclusters must be allocated
751 echo "# resize --preallocation=falloc"
752 _make_test_img -o extended_l2=on -F raw -b "$TEST_IMG.base" 503k
753 $QEMU_IMG resize --preallocation=falloc "$TEST_IMG" 1013k
754 $QEMU_IO -c 'read -P 0xff 0 503k' "$TEST_IMG" | _filter_qemu_io
755 $QEMU_IO -c 'read -P 0x00 503k 510k' "$TEST_IMG" | _filter_qemu_io
757 echo "# resize --preallocation=full"
758 _make_test_img -o extended_l2=on -F raw -b "$TEST_IMG.base" 503k
759 $QEMU_IMG resize --preallocation=full "$TEST_IMG" 1013k
760 $QEMU_IO -c 'read -P 0xff 0 503k' "$TEST_IMG" | _filter_qemu_io
761 $QEMU_IO -c 'read -P 0x00 503k 510k' "$TEST_IMG" | _filter_qemu_io
763 echo
764 echo "### Image resizing with preallocation without backing files ###"
765 echo
766 # In this case the new subclusters must have the 'all zeroes' bit set
767 echo "# resize --preallocation=metadata"
768 _make_test_img -o extended_l2=on 503k
769 $QEMU_IO -c 'write -P 0xff 0 503k' "$TEST_IMG" | _filter_qemu_io
770 $QEMU_IMG resize --preallocation=metadata "$TEST_IMG" 1013k
771 $QEMU_IO -c 'read -P 0xff 0 503k' "$TEST_IMG" | _filter_qemu_io
772 $QEMU_IO -c 'read -P 0x00 503k 510k' "$TEST_IMG" | _filter_qemu_io
774 # In this case and the next one the new subclusters must be allocated
775 echo "# resize --preallocation=falloc"
776 _make_test_img -o extended_l2=on 503k
777 $QEMU_IO -c 'write -P 0xff 0 503k' "$TEST_IMG" | _filter_qemu_io
778 $QEMU_IMG resize --preallocation=falloc "$TEST_IMG" 1013k
779 $QEMU_IO -c 'read -P 0xff 0 503k' "$TEST_IMG" | _filter_qemu_io
780 $QEMU_IO -c 'read -P 0x00 503k 510k' "$TEST_IMG" | _filter_qemu_io
782 echo "# resize --preallocation=full"
783 _make_test_img -o extended_l2=on 503k
784 $QEMU_IO -c 'write -P 0xff 0 503k' "$TEST_IMG" | _filter_qemu_io
785 $QEMU_IMG resize --preallocation=full "$TEST_IMG" 1013k
786 $QEMU_IO -c 'read -P 0xff 0 503k' "$TEST_IMG" | _filter_qemu_io
787 $QEMU_IO -c 'read -P 0x00 503k 510k' "$TEST_IMG" | _filter_qemu_io
789 echo
790 echo "### qemu-img measure ###"
791 echo
792 echo "# 512MB, extended_l2=off" # This needs one L2 table
793 $QEMU_IMG measure --size 512M -O qcow2 -o extended_l2=off
794 echo "# 512MB, extended_l2=on" # This needs two L2 tables
795 $QEMU_IMG measure --size 512M -O qcow2 -o extended_l2=on
797 echo "# 16K clusters, 64GB, extended_l2=off" # This needs one full L1 table cluster
798 $QEMU_IMG measure --size 64G -O qcow2 -o cluster_size=16k,extended_l2=off
799 echo "# 16K clusters, 64GB, extended_l2=on" # This needs two full L2 table clusters
800 $QEMU_IMG measure --size 64G -O qcow2 -o cluster_size=16k,extended_l2=on
802 echo "# 8k clusters" # This should fail
803 $QEMU_IMG measure --size 1M -O qcow2 -o cluster_size=8k,extended_l2=on
805 echo "# 1024 TB" # Maximum allowed size with extended_l2=on and 64K clusters
806 $QEMU_IMG measure --size 1024T -O qcow2 -o extended_l2=on
807 echo "# 1025 TB" # This should fail
808 $QEMU_IMG measure --size 1025T -O qcow2 -o extended_l2=on
810 echo
811 echo "### qemu-img amend ###"
812 echo
813 _make_test_img -o extended_l2=on 1M
814 $QEMU_IMG amend -o extended_l2=off "$TEST_IMG" && echo "Unexpected pass"
816 _make_test_img -o extended_l2=off 1M
817 $QEMU_IMG amend -o extended_l2=on "$TEST_IMG" && echo "Unexpected pass"
819 echo
820 echo "### Test copy-on-write on an image with snapshots ###"
821 echo
822 _make_test_img -o extended_l2=on 1M
824 # For each cluster from #0 to #9 this loop zeroes subcluster #7
825 # and allocates subclusters #13 and #18.
826 alloc="13 18"; zero="7"
827 for c in $(seq 0 9); do
828 $QEMU_IO -c "write -q -z $((64*$c+14))k 2k" \
829 -c "write -q -P $((0xd0+$c)) $((64*$c+26))k 2k" \
830 -c "write -q -P $((0xe0+$c)) $((64*$c+36))k 2k" "$TEST_IMG"
831 _verify_l2_bitmap "$c"
832 done
834 # Create a snapshot and set l2_offset to the new L2 table
835 $QEMU_IMG snapshot -c snap1 "$TEST_IMG"
836 l2_offset=$((0x110000))
838 # Write different patterns to each one of the clusters
839 # in order to see how copy-on-write behaves in each case.
840 $QEMU_IO -c "write -q -P 0xf0 $((64*0+30))k 1k" \
841 -c "write -q -P 0xf1 $((64*1+20))k 1k" \
842 -c "write -q -P 0xf2 $((64*2+40))k 1k" \
843 -c "write -q -P 0xf3 $((64*3+26))k 1k" \
844 -c "write -q -P 0xf4 $((64*4+14))k 1k" \
845 -c "write -q -P 0xf5 $((64*5+1))k 1k" \
846 -c "write -q -z $((64*6+30))k 3k" \
847 -c "write -q -z $((64*7+26))k 2k" \
848 -c "write -q -z $((64*8+26))k 1k" \
849 -c "write -q -z $((64*9+12))k 1k" \
850 "$TEST_IMG"
851 alloc="$(seq 13 18)"; zero="7" _verify_l2_bitmap 0
852 alloc="$(seq 10 18)"; zero="7" _verify_l2_bitmap 1
853 alloc="$(seq 13 20)"; zero="7" _verify_l2_bitmap 2
854 alloc="$(seq 13 18)"; zero="7" _verify_l2_bitmap 3
855 alloc="$(seq 7 18)"; zero="" _verify_l2_bitmap 4
856 alloc="$(seq 0 18)"; zero="" _verify_l2_bitmap 5
857 alloc="13 18"; zero="7 15 16" _verify_l2_bitmap 6
858 alloc="18"; zero="7 13" _verify_l2_bitmap 7
859 alloc="$(seq 13 18)"; zero="7" _verify_l2_bitmap 8
860 alloc="13 18"; zero="6 7" _verify_l2_bitmap 9
862 echo
863 echo "### Test concurrent requests ###"
864 echo
866 _concurrent_io()
868 # Allocate three subclusters in the same cluster.
869 # This works because handle_dependencies() checks whether the requests
870 # allocate the same cluster, even if the COW regions don't overlap (in
871 # this case they don't).
872 cat <<EOF
873 open -o driver=$IMGFMT blkdebug::$TEST_IMG
874 break write_aio A
875 aio_write -P 10 30k 2k
876 wait_break A
877 aio_write -P 11 20k 2k
878 aio_write -P 12 40k 2k
879 resume A
880 aio_flush
884 _concurrent_verify()
886 cat <<EOF
887 open -o driver=$IMGFMT $TEST_IMG
888 read -q -P 10 30k 2k
889 read -q -P 11 20k 2k
890 read -q -P 12 40k 2k
894 _make_test_img -o extended_l2=on 1M
895 _concurrent_io | $QEMU_IO | _filter_qemu_io
896 _concurrent_verify | $QEMU_IO | _filter_qemu_io
898 # success, all done
899 echo "*** done"
900 rm -f $seq.full
901 status=0