1*11a82d14SPhilippe 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 420e8a3714SMax Reitz 430e8a3714SMax Reitzecho 440e8a3714SMax Reitzecho '=== New refcount structures may not conflict with existing structures ===' 450e8a3714SMax Reitz 460e8a3714SMax Reitzecho 470e8a3714SMax Reitzecho '--- Test 1 ---' 480e8a3714SMax Reitzecho 490e8a3714SMax Reitz 500e8a3714SMax Reitz# Preallocation speeds up the write operation, but preallocating everything will 510e8a3714SMax Reitz# destroy the purpose of the write; so preallocate one KB less than what would 520e8a3714SMax Reitz# cause a reftable growth... 530e8a3714SMax ReitzIMGOPTS='preallocation=metadata,cluster_size=1k' _make_test_img 64512K 540e8a3714SMax Reitz# ...and make the image the desired size afterwards. 550e8a3714SMax Reitz$QEMU_IMG resize "$TEST_IMG" 65M 560e8a3714SMax Reitz 570e8a3714SMax Reitz# The first write results in a growth of the refcount table during an allocation 580e8a3714SMax Reitz# which has precisely the required size so that the new refcount block allocated 590e8a3714SMax Reitz# in alloc_refcount_block() is right after cluster_index; this did lead to a 600e8a3714SMax Reitz# different refcount block being written to disk (a zeroed cluster) than what is 610e8a3714SMax Reitz# cached (a refblock with one entry having a refcount of 1), and the second 620e8a3714SMax Reitz# write would then result in that cached cluster being marked dirty and then 630e8a3714SMax Reitz# in it being written to disk. 640e8a3714SMax Reitz# This should not happen, the new refcount structures may not conflict with 650e8a3714SMax Reitz# new_block. 660e8a3714SMax Reitz# (Note that for some reason, 'write 63M 1K' does not trigger the problem) 670e8a3714SMax Reitz$QEMU_IO -c 'write 62M 1025K' -c 'write 64M 1M' "$TEST_IMG" | _filter_qemu_io 680e8a3714SMax Reitz 690e8a3714SMax Reitz_check_test_img 700e8a3714SMax Reitz 710e8a3714SMax Reitz 720e8a3714SMax Reitzecho 730e8a3714SMax Reitzecho '--- Test 2 ---' 740e8a3714SMax Reitzecho 750e8a3714SMax Reitz 760e8a3714SMax ReitzIMGOPTS='preallocation=metadata,cluster_size=1k' _make_test_img 64513K 770e8a3714SMax Reitz# This results in an L1 table growth which in turn results in some clusters at 780e8a3714SMax Reitz# the start of the image becoming free 790e8a3714SMax Reitz$QEMU_IMG resize "$TEST_IMG" 65M 800e8a3714SMax Reitz 810e8a3714SMax Reitz# This write results in a refcount table growth; but the refblock allocated 820e8a3714SMax Reitz# immediately before that (new_block) takes cluster index 4 (which is now free) 830e8a3714SMax Reitz# and is thus not self-describing (in contrast to test 1, where new_block was 840e8a3714SMax Reitz# self-describing). The refcount table growth algorithm then used to place the 850e8a3714SMax Reitz# new refcount structures at cluster index 65536 (which is the same as the 860e8a3714SMax Reitz# cluster_index parameter in this case), allocating a new refcount block for 870e8a3714SMax Reitz# that cluster while new_block already existed, leaking new_block. 880e8a3714SMax Reitz# Therefore, the new refcount structures may not be put at cluster_index 890e8a3714SMax Reitz# (because new_block already describes that cluster, and the new structures try 900e8a3714SMax Reitz# to be self-describing). 910e8a3714SMax Reitz$QEMU_IO -c 'write 63M 130K' "$TEST_IMG" | _filter_qemu_io 920e8a3714SMax Reitz 930e8a3714SMax Reitz_check_test_img 940e8a3714SMax Reitz 95abf754feSAlberto Garciaecho 96abf754feSAlberto Garciaecho '=== Allocating a new refcount block must not leave holes in the image ===' 97abf754feSAlberto Garciaecho 98abf754feSAlberto Garcia 99abf754feSAlberto GarciaIMGOPTS='cluster_size=512,refcount_bits=16' _make_test_img 1M 100abf754feSAlberto Garcia 101abf754feSAlberto Garcia# This results in an image with 256 used clusters: the qcow2 header, 102abf754feSAlberto Garcia# the refcount table, one refcount block, the L1 table, four L2 tables 103abf754feSAlberto Garcia# and 248 data clusters 104abf754feSAlberto Garcia$QEMU_IO -c 'write 0 124k' "$TEST_IMG" | _filter_qemu_io 105abf754feSAlberto Garcia 106abf754feSAlberto Garcia# 256 clusters of 512 bytes each give us a 128K image 107abf754feSAlberto Garciastat -c "size=%s (expected 131072)" $TEST_IMG 108abf754feSAlberto Garcia 109abf754feSAlberto Garcia# All 256 entries of the refcount block are used, so writing a new 110abf754feSAlberto Garcia# data cluster also allocates a new refcount block 111abf754feSAlberto Garcia$QEMU_IO -c 'write 124k 512' "$TEST_IMG" | _filter_qemu_io 112abf754feSAlberto Garcia 113abf754feSAlberto Garcia# Two more clusters, the image size should be 129K now 114abf754feSAlberto Garciastat -c "size=%s (expected 132096)" $TEST_IMG 1150e8a3714SMax Reitz 1160e8a3714SMax Reitz# success, all done 1170e8a3714SMax Reitzecho 1180e8a3714SMax Reitzecho '*** done' 1190e8a3714SMax Reitzrm -f $seq.full 1200e8a3714SMax Reitzstatus=0 121