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