xref: /openbmc/qemu/tests/qemu-iotests/121 (revision f7160f32)
1#!/usr/bin/env bash
2#
3# Test cases for qcow2 refcount table growth
4#
5# Copyright (C) 2015 Red Hat, Inc.
6#
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.
11#
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.
16#
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/>.
19#
20
21# creator
22owner=mreitz@redhat.com
23
24seq="$(basename $0)"
25echo "QA output created by $seq"
26
27status=1	# failure is the default!
28
29_cleanup()
30{
31	_cleanup_test_img
32}
33trap "_cleanup; exit \$status" 0 1 2 3 15
34
35# get standard environment, filters and checks
36. ./common.rc
37. ./common.filter
38
39_supported_fmt qcow2
40_supported_proto file
41_supported_os Linux
42# Refcount structures are used much differently with external data
43# files
44_unsupported_imgopts data_file
45
46echo
47echo '=== New refcount structures may not conflict with existing structures ==='
48
49echo
50echo '--- Test 1 ---'
51echo
52
53# Preallocation speeds up the write operation, but preallocating everything will
54# destroy the purpose of the write; so preallocate one KB less than what would
55# cause a reftable growth...
56_make_test_img -o 'preallocation=metadata,cluster_size=1k' 64512K
57# ...and make the image the desired size afterwards.
58$QEMU_IMG resize "$TEST_IMG" 65M
59
60# The first write results in a growth of the refcount table during an allocation
61# which has precisely the required size so that the new refcount block allocated
62# in alloc_refcount_block() is right after cluster_index; this did lead to a
63# different refcount block being written to disk (a zeroed cluster) than what is
64# cached (a refblock with one entry having a refcount of 1), and the second
65# write would then result in that cached cluster being marked dirty and then
66# in it being written to disk.
67# This should not happen, the new refcount structures may not conflict with
68# new_block.
69# (Note that for some reason, 'write 63M 1K' does not trigger the problem)
70$QEMU_IO -c 'write 62M 1025K' -c 'write 64M 1M' "$TEST_IMG" | _filter_qemu_io
71
72_check_test_img
73
74
75echo
76echo '--- Test 2 ---'
77echo
78
79_make_test_img -o 'preallocation=metadata,cluster_size=1k' 64513K
80# This results in an L1 table growth which in turn results in some clusters at
81# the start of the image becoming free
82$QEMU_IMG resize "$TEST_IMG" 65M
83
84# This write results in a refcount table growth; but the refblock allocated
85# immediately before that (new_block) takes cluster index 4 (which is now free)
86# and is thus not self-describing (in contrast to test 1, where new_block was
87# self-describing). The refcount table growth algorithm then used to place the
88# new refcount structures at cluster index 65536 (which is the same as the
89# cluster_index parameter in this case), allocating a new refcount block for
90# that cluster while new_block already existed, leaking new_block.
91# Therefore, the new refcount structures may not be put at cluster_index
92# (because new_block already describes that cluster, and the new structures try
93# to be self-describing).
94$QEMU_IO -c 'write 63M 130K' "$TEST_IMG" | _filter_qemu_io
95
96_check_test_img
97
98echo
99echo '=== Allocating a new refcount block must not leave holes in the image ==='
100echo
101
102_make_test_img -o 'cluster_size=512,refcount_bits=16' 1M
103
104# This results in an image with 256 used clusters: the qcow2 header,
105# the refcount table, one refcount block, the L1 table, four L2 tables
106# and 248 data clusters
107$QEMU_IO -c 'write 0 124k' "$TEST_IMG" | _filter_qemu_io
108
109# 256 clusters of 512 bytes each give us a 128K image
110stat -c "size=%s (expected 131072)" $TEST_IMG
111
112# All 256 entries of the refcount block are used, so writing a new
113# data cluster also allocates a new refcount block
114$QEMU_IO -c 'write 124k 512' "$TEST_IMG" | _filter_qemu_io
115
116# Two more clusters, the image size should be 129K now
117stat -c "size=%s (expected 132096)" $TEST_IMG
118
119# success, all done
120echo
121echo '*** done'
122rm -f $seq.full
123status=0
124