1*0e8a3714SMax Reitz#!/bin/bash 2*0e8a3714SMax Reitz# 3*0e8a3714SMax Reitz# Test cases for qcow2 refcount table growth 4*0e8a3714SMax Reitz# 5*0e8a3714SMax Reitz# Copyright (C) 2015 Red Hat, Inc. 6*0e8a3714SMax Reitz# 7*0e8a3714SMax Reitz# This program is free software; you can redistribute it and/or modify 8*0e8a3714SMax Reitz# it under the terms of the GNU General Public License as published by 9*0e8a3714SMax Reitz# the Free Software Foundation; either version 2 of the License, or 10*0e8a3714SMax Reitz# (at your option) any later version. 11*0e8a3714SMax Reitz# 12*0e8a3714SMax Reitz# This program is distributed in the hope that it will be useful, 13*0e8a3714SMax Reitz# but WITHOUT ANY WARRANTY; without even the implied warranty of 14*0e8a3714SMax Reitz# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15*0e8a3714SMax Reitz# GNU General Public License for more details. 16*0e8a3714SMax Reitz# 17*0e8a3714SMax Reitz# You should have received a copy of the GNU General Public License 18*0e8a3714SMax Reitz# along with this program. If not, see <http://www.gnu.org/licenses/>. 19*0e8a3714SMax Reitz# 20*0e8a3714SMax Reitz 21*0e8a3714SMax Reitz# creator 22*0e8a3714SMax Reitzowner=mreitz@redhat.com 23*0e8a3714SMax Reitz 24*0e8a3714SMax Reitzseq="$(basename $0)" 25*0e8a3714SMax Reitzecho "QA output created by $seq" 26*0e8a3714SMax Reitz 27*0e8a3714SMax Reitzhere="$PWD" 28*0e8a3714SMax Reitztmp=/tmp/$$ 29*0e8a3714SMax Reitzstatus=1 # failure is the default! 30*0e8a3714SMax Reitz 31*0e8a3714SMax Reitz_cleanup() 32*0e8a3714SMax Reitz{ 33*0e8a3714SMax Reitz _cleanup_test_img 34*0e8a3714SMax Reitz} 35*0e8a3714SMax Reitztrap "_cleanup; exit \$status" 0 1 2 3 15 36*0e8a3714SMax Reitz 37*0e8a3714SMax Reitz# get standard environment, filters and checks 38*0e8a3714SMax Reitz. ./common.rc 39*0e8a3714SMax Reitz. ./common.filter 40*0e8a3714SMax Reitz 41*0e8a3714SMax Reitz_supported_fmt qcow2 42*0e8a3714SMax Reitz_supported_proto file 43*0e8a3714SMax Reitz_supported_os Linux 44*0e8a3714SMax Reitz 45*0e8a3714SMax Reitzecho 46*0e8a3714SMax Reitzecho '=== New refcount structures may not conflict with existing structures ===' 47*0e8a3714SMax Reitz 48*0e8a3714SMax Reitzecho 49*0e8a3714SMax Reitzecho '--- Test 1 ---' 50*0e8a3714SMax Reitzecho 51*0e8a3714SMax Reitz 52*0e8a3714SMax Reitz# Preallocation speeds up the write operation, but preallocating everything will 53*0e8a3714SMax Reitz# destroy the purpose of the write; so preallocate one KB less than what would 54*0e8a3714SMax Reitz# cause a reftable growth... 55*0e8a3714SMax ReitzIMGOPTS='preallocation=metadata,cluster_size=1k' _make_test_img 64512K 56*0e8a3714SMax Reitz# ...and make the image the desired size afterwards. 57*0e8a3714SMax Reitz$QEMU_IMG resize "$TEST_IMG" 65M 58*0e8a3714SMax Reitz 59*0e8a3714SMax Reitz# The first write results in a growth of the refcount table during an allocation 60*0e8a3714SMax Reitz# which has precisely the required size so that the new refcount block allocated 61*0e8a3714SMax Reitz# in alloc_refcount_block() is right after cluster_index; this did lead to a 62*0e8a3714SMax Reitz# different refcount block being written to disk (a zeroed cluster) than what is 63*0e8a3714SMax Reitz# cached (a refblock with one entry having a refcount of 1), and the second 64*0e8a3714SMax Reitz# write would then result in that cached cluster being marked dirty and then 65*0e8a3714SMax Reitz# in it being written to disk. 66*0e8a3714SMax Reitz# This should not happen, the new refcount structures may not conflict with 67*0e8a3714SMax Reitz# new_block. 68*0e8a3714SMax Reitz# (Note that for some reason, 'write 63M 1K' does not trigger the problem) 69*0e8a3714SMax Reitz$QEMU_IO -c 'write 62M 1025K' -c 'write 64M 1M' "$TEST_IMG" | _filter_qemu_io 70*0e8a3714SMax Reitz 71*0e8a3714SMax Reitz_check_test_img 72*0e8a3714SMax Reitz 73*0e8a3714SMax Reitz 74*0e8a3714SMax Reitzecho 75*0e8a3714SMax Reitzecho '--- Test 2 ---' 76*0e8a3714SMax Reitzecho 77*0e8a3714SMax Reitz 78*0e8a3714SMax ReitzIMGOPTS='preallocation=metadata,cluster_size=1k' _make_test_img 64513K 79*0e8a3714SMax Reitz# This results in an L1 table growth which in turn results in some clusters at 80*0e8a3714SMax Reitz# the start of the image becoming free 81*0e8a3714SMax Reitz$QEMU_IMG resize "$TEST_IMG" 65M 82*0e8a3714SMax Reitz 83*0e8a3714SMax Reitz# This write results in a refcount table growth; but the refblock allocated 84*0e8a3714SMax Reitz# immediately before that (new_block) takes cluster index 4 (which is now free) 85*0e8a3714SMax Reitz# and is thus not self-describing (in contrast to test 1, where new_block was 86*0e8a3714SMax Reitz# self-describing). The refcount table growth algorithm then used to place the 87*0e8a3714SMax Reitz# new refcount structures at cluster index 65536 (which is the same as the 88*0e8a3714SMax Reitz# cluster_index parameter in this case), allocating a new refcount block for 89*0e8a3714SMax Reitz# that cluster while new_block already existed, leaking new_block. 90*0e8a3714SMax Reitz# Therefore, the new refcount structures may not be put at cluster_index 91*0e8a3714SMax Reitz# (because new_block already describes that cluster, and the new structures try 92*0e8a3714SMax Reitz# to be self-describing). 93*0e8a3714SMax Reitz$QEMU_IO -c 'write 63M 130K' "$TEST_IMG" | _filter_qemu_io 94*0e8a3714SMax Reitz 95*0e8a3714SMax Reitz_check_test_img 96*0e8a3714SMax Reitz 97*0e8a3714SMax Reitz 98*0e8a3714SMax Reitz# success, all done 99*0e8a3714SMax Reitzecho 100*0e8a3714SMax Reitzecho '*** done' 101*0e8a3714SMax Reitzrm -f $seq.full 102*0e8a3714SMax Reitzstatus=0 103