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