111a82d14SPhilippe Mathieu-Daudé#!/usr/bin/env bash 20e8a3714SMax Reitz# 30e8a3714SMax Reitz# Test cases for qcow2 refcount table growth 40e8a3714SMax Reitz# 50e8a3714SMax Reitz# Copyright (C) 2015 Red Hat, Inc. 60e8a3714SMax Reitz# 70e8a3714SMax Reitz# This program is free software; you can redistribute it and/or modify 80e8a3714SMax Reitz# it under the terms of the GNU General Public License as published by 90e8a3714SMax Reitz# the Free Software Foundation; either version 2 of the License, or 100e8a3714SMax Reitz# (at your option) any later version. 110e8a3714SMax Reitz# 120e8a3714SMax Reitz# This program is distributed in the hope that it will be useful, 130e8a3714SMax Reitz# but WITHOUT ANY WARRANTY; without even the implied warranty of 140e8a3714SMax Reitz# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 150e8a3714SMax Reitz# GNU General Public License for more details. 160e8a3714SMax Reitz# 170e8a3714SMax Reitz# You should have received a copy of the GNU General Public License 180e8a3714SMax Reitz# along with this program. If not, see <http://www.gnu.org/licenses/>. 190e8a3714SMax Reitz# 200e8a3714SMax Reitz 210e8a3714SMax Reitz# creator 220e8a3714SMax Reitzowner=mreitz@redhat.com 230e8a3714SMax Reitz 240e8a3714SMax Reitzseq="$(basename $0)" 250e8a3714SMax Reitzecho "QA output created by $seq" 260e8a3714SMax Reitz 270e8a3714SMax Reitzstatus=1 # failure is the default! 280e8a3714SMax Reitz 290e8a3714SMax Reitz_cleanup() 300e8a3714SMax Reitz{ 310e8a3714SMax Reitz _cleanup_test_img 320e8a3714SMax Reitz} 330e8a3714SMax Reitztrap "_cleanup; exit \$status" 0 1 2 3 15 340e8a3714SMax Reitz 350e8a3714SMax Reitz# get standard environment, filters and checks 360e8a3714SMax Reitz. ./common.rc 370e8a3714SMax Reitz. ./common.filter 380e8a3714SMax Reitz 390e8a3714SMax Reitz_supported_fmt qcow2 400e8a3714SMax Reitz_supported_proto file 410e8a3714SMax Reitz_supported_os Linux 42*3be2024aSMax Reitz# Refcount structures are used much differently with external data 43*3be2024aSMax Reitz# files 44*3be2024aSMax Reitz_unsupported_imgopts data_file 450e8a3714SMax Reitz 460e8a3714SMax Reitzecho 470e8a3714SMax Reitzecho '=== New refcount structures may not conflict with existing structures ===' 480e8a3714SMax Reitz 490e8a3714SMax Reitzecho 500e8a3714SMax Reitzecho '--- Test 1 ---' 510e8a3714SMax Reitzecho 520e8a3714SMax Reitz 530e8a3714SMax Reitz# Preallocation speeds up the write operation, but preallocating everything will 540e8a3714SMax Reitz# destroy the purpose of the write; so preallocate one KB less than what would 550e8a3714SMax Reitz# cause a reftable growth... 56407fb56aSMax Reitz_make_test_img -o 'preallocation=metadata,cluster_size=1k' 64512K 570e8a3714SMax Reitz# ...and make the image the desired size afterwards. 580e8a3714SMax Reitz$QEMU_IMG resize "$TEST_IMG" 65M 590e8a3714SMax Reitz 600e8a3714SMax Reitz# The first write results in a growth of the refcount table during an allocation 610e8a3714SMax Reitz# which has precisely the required size so that the new refcount block allocated 620e8a3714SMax Reitz# in alloc_refcount_block() is right after cluster_index; this did lead to a 630e8a3714SMax Reitz# different refcount block being written to disk (a zeroed cluster) than what is 640e8a3714SMax Reitz# cached (a refblock with one entry having a refcount of 1), and the second 650e8a3714SMax Reitz# write would then result in that cached cluster being marked dirty and then 660e8a3714SMax Reitz# in it being written to disk. 670e8a3714SMax Reitz# This should not happen, the new refcount structures may not conflict with 680e8a3714SMax Reitz# new_block. 690e8a3714SMax Reitz# (Note that for some reason, 'write 63M 1K' does not trigger the problem) 700e8a3714SMax Reitz$QEMU_IO -c 'write 62M 1025K' -c 'write 64M 1M' "$TEST_IMG" | _filter_qemu_io 710e8a3714SMax Reitz 720e8a3714SMax Reitz_check_test_img 730e8a3714SMax Reitz 740e8a3714SMax Reitz 750e8a3714SMax Reitzecho 760e8a3714SMax Reitzecho '--- Test 2 ---' 770e8a3714SMax Reitzecho 780e8a3714SMax Reitz 79407fb56aSMax Reitz_make_test_img -o 'preallocation=metadata,cluster_size=1k' 64513K 800e8a3714SMax Reitz# This results in an L1 table growth which in turn results in some clusters at 810e8a3714SMax Reitz# the start of the image becoming free 820e8a3714SMax Reitz$QEMU_IMG resize "$TEST_IMG" 65M 830e8a3714SMax Reitz 840e8a3714SMax Reitz# This write results in a refcount table growth; but the refblock allocated 850e8a3714SMax Reitz# immediately before that (new_block) takes cluster index 4 (which is now free) 860e8a3714SMax Reitz# and is thus not self-describing (in contrast to test 1, where new_block was 870e8a3714SMax Reitz# self-describing). The refcount table growth algorithm then used to place the 880e8a3714SMax Reitz# new refcount structures at cluster index 65536 (which is the same as the 890e8a3714SMax Reitz# cluster_index parameter in this case), allocating a new refcount block for 900e8a3714SMax Reitz# that cluster while new_block already existed, leaking new_block. 910e8a3714SMax Reitz# Therefore, the new refcount structures may not be put at cluster_index 920e8a3714SMax Reitz# (because new_block already describes that cluster, and the new structures try 930e8a3714SMax Reitz# to be self-describing). 940e8a3714SMax Reitz$QEMU_IO -c 'write 63M 130K' "$TEST_IMG" | _filter_qemu_io 950e8a3714SMax Reitz 960e8a3714SMax Reitz_check_test_img 970e8a3714SMax Reitz 98abf754feSAlberto Garciaecho 99abf754feSAlberto Garciaecho '=== Allocating a new refcount block must not leave holes in the image ===' 100abf754feSAlberto Garciaecho 101abf754feSAlberto Garcia 102407fb56aSMax Reitz_make_test_img -o 'cluster_size=512,refcount_bits=16' 1M 103abf754feSAlberto Garcia 104abf754feSAlberto Garcia# This results in an image with 256 used clusters: the qcow2 header, 105abf754feSAlberto Garcia# the refcount table, one refcount block, the L1 table, four L2 tables 106abf754feSAlberto Garcia# and 248 data clusters 107abf754feSAlberto Garcia$QEMU_IO -c 'write 0 124k' "$TEST_IMG" | _filter_qemu_io 108abf754feSAlberto Garcia 109abf754feSAlberto Garcia# 256 clusters of 512 bytes each give us a 128K image 110abf754feSAlberto Garciastat -c "size=%s (expected 131072)" $TEST_IMG 111abf754feSAlberto Garcia 112abf754feSAlberto Garcia# All 256 entries of the refcount block are used, so writing a new 113abf754feSAlberto Garcia# data cluster also allocates a new refcount block 114abf754feSAlberto Garcia$QEMU_IO -c 'write 124k 512' "$TEST_IMG" | _filter_qemu_io 115abf754feSAlberto Garcia 116abf754feSAlberto Garcia# Two more clusters, the image size should be 129K now 117abf754feSAlberto Garciastat -c "size=%s (expected 132096)" $TEST_IMG 1180e8a3714SMax Reitz 1190e8a3714SMax Reitz# success, all done 1200e8a3714SMax Reitzecho 1210e8a3714SMax Reitzecho '*** done' 1220e8a3714SMax Reitzrm -f $seq.full 1230e8a3714SMax Reitzstatus=0 124