From nobody Wed Nov 19 07:03:52 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1615392170; cv=none; d=zohomail.com; s=zohoarc; b=J0rxSpihVA32VEdF0d4oVxqrOyikbwkjKLaQMglg+tBeB4JA4WR17Vyphz8UxhvB86ok/3Yr+grk19cTFfDEfadiryRgZxdnQqhnVYMedd6KWN2H3JUdhPB0qMLG5QYelWDWgoZZjH8yIqOTKD8Sdzp8laHOPu6l+kv1xQs8e1c= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1615392170; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=D1ebnv9CH4pPF48/3fnpmkUosTdpCnKcTQplkRE32yA=; b=OEcEoaty/ya1SjeDsPl1dm9unAotxpyJTpc/3gm2Lk7RAfDqPOHnaEWv9ymsaFUsvUJTWc0K0UX6S3uGdsj1bubKAB0Xr5BtBgMP9k5cPHYQbRbDWvTX+YRm2xA5uar61aV0RMDXTPJblOMehTDlPYIUEPMhungUHepqJ25mLbI= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 161539216980588.9025899119697; Wed, 10 Mar 2021 08:02:49 -0800 (PST) Received: from localhost ([::1]:53462 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lK1IL-0004VO-Q3 for importer@patchew.org; Wed, 10 Mar 2021 11:02:45 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:34022) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lK1Ez-0003AV-Te for qemu-devel@nongnu.org; Wed, 10 Mar 2021 10:59:17 -0500 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:41904) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1lK1Ex-00023F-9N for qemu-devel@nongnu.org; Wed, 10 Mar 2021 10:59:17 -0500 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-539-szdzuHBVMTmeg-qdyC6VLg-1; Wed, 10 Mar 2021 10:59:12 -0500 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 5C878802690; Wed, 10 Mar 2021 15:59:11 +0000 (UTC) Received: from localhost (ovpn-115-159.ams2.redhat.com [10.36.115.159]) by smtp.corp.redhat.com (Postfix) with ESMTPS id C13256062F; Wed, 10 Mar 2021 15:59:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1615391954; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=D1ebnv9CH4pPF48/3fnpmkUosTdpCnKcTQplkRE32yA=; b=JP4UXPGXNstjupzV/0hSy3STrM/BFqsLaYPegGItyGxyUPf2LM6xSEppstN5v0A1rz5fid whpjlZjuAT4guhfwXR1L7UYBdHDObB+RK9iLIw19crrMyEbZcvQtze3bGOntQ0FHQpTqgp pXoZdM+78UtsqHiWJHmNf6lj7MxfD0U= X-MC-Unique: szdzuHBVMTmeg-qdyC6VLg-1 From: Max Reitz To: qemu-block@nongnu.org Subject: [PATCH 1/4] qcow2: Improve refcount structure rebuilding Date: Wed, 10 Mar 2021 16:59:03 +0100 Message-Id: <20210310155906.147478-2-mreitz@redhat.com> In-Reply-To: <20210310155906.147478-1-mreitz@redhat.com> References: <20210310155906.147478-1-mreitz@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=mreitz@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=216.205.24.124; envelope-from=mreitz@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -29 X-Spam_score: -3.0 X-Spam_bar: --- X-Spam_report: (-3.0 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.243, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , qemu-devel@nongnu.org, Max Reitz Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" When rebuilding the refcount structures (when qemu-img check -r found errors with refcount =3D 0, but reference count > 0), the new refcount table defaults to being put at the image file end[1]. There is no good reason for that except that it means we will not have to rewrite any refblocks we already wrote to disk. Changing the code to rewrite those refblocks is not too difficult, though, so let us do that. That is beneficial for images on block devices, where we cannot really write beyond the end of the image file. [1] Unless there is something allocated in the area pointed to by the last refblock, so we have to write that refblock. In that case, we try to put the reftable in there. Buglink: https://bugzilla.redhat.com/show_bug.cgi?id=3D1519071 Signed-off-by: Max Reitz --- block/qcow2-refcount.c | 126 ++++++++++++++++++++++------------------- 1 file changed, 67 insertions(+), 59 deletions(-) diff --git a/block/qcow2-refcount.c b/block/qcow2-refcount.c index 8e649b008e..162caeeb8e 100644 --- a/block/qcow2-refcount.c +++ b/block/qcow2-refcount.c @@ -2352,8 +2352,9 @@ static int rebuild_refcount_structure(BlockDriverStat= e *bs, int64_t *nb_clusters) { BDRVQcow2State *s =3D bs->opaque; - int64_t first_free_cluster =3D 0, reftable_offset =3D -1, cluster =3D = 0; + int64_t first_free_cluster =3D 0, reftable_offset =3D -1, cluster; int64_t refblock_offset, refblock_start, refblock_index; + int64_t first_cluster, end_cluster; uint32_t reftable_size =3D 0; uint64_t *on_disk_reftable =3D NULL; void *on_disk_refblock; @@ -2365,8 +2366,11 @@ static int rebuild_refcount_structure(BlockDriverSta= te *bs, =20 qcow2_cache_empty(bs, s->refcount_block_cache); =20 + first_cluster =3D 0; + end_cluster =3D *nb_clusters; + write_refblocks: - for (; cluster < *nb_clusters; cluster++) { + for (cluster =3D first_cluster; cluster < end_cluster; cluster++) { if (!s->get_refcount(*refcount_table, cluster)) { continue; } @@ -2374,65 +2378,68 @@ write_refblocks: refblock_index =3D cluster >> s->refcount_block_bits; refblock_start =3D refblock_index << s->refcount_block_bits; =20 - /* Don't allocate a cluster in a refblock already written to disk = */ - if (first_free_cluster < refblock_start) { - first_free_cluster =3D refblock_start; - } - refblock_offset =3D alloc_clusters_imrt(bs, 1, refcount_table, - nb_clusters, &first_free_clu= ster); - if (refblock_offset < 0) { - fprintf(stderr, "ERROR allocating refblock: %s\n", - strerror(-refblock_offset)); - res->check_errors++; - ret =3D refblock_offset; - goto fail; - } - - if (reftable_size <=3D refblock_index) { - uint32_t old_reftable_size =3D reftable_size; - uint64_t *new_on_disk_reftable; + if (reftable_size > refblock_index && + on_disk_reftable[refblock_index]) + { + refblock_offset =3D on_disk_reftable[refblock_index]; + } else { + int64_t refblock_cluster_index; =20 - reftable_size =3D ROUND_UP((refblock_index + 1) * REFTABLE_ENT= RY_SIZE, - s->cluster_size) / REFTABLE_ENTRY_SIZ= E; - new_on_disk_reftable =3D g_try_realloc(on_disk_reftable, - reftable_size * - REFTABLE_ENTRY_SIZE); - if (!new_on_disk_reftable) { + /* Don't allocate a cluster in a refblock already written to d= isk */ + if (first_free_cluster < refblock_start) { + first_free_cluster =3D refblock_start; + } + refblock_offset =3D alloc_clusters_imrt(bs, 1, refcount_table, + nb_clusters, + &first_free_cluster); + if (refblock_offset < 0) { + fprintf(stderr, "ERROR allocating refblock: %s\n", + strerror(-refblock_offset)); res->check_errors++; - ret =3D -ENOMEM; + ret =3D refblock_offset; goto fail; } - on_disk_reftable =3D new_on_disk_reftable; =20 - memset(on_disk_reftable + old_reftable_size, 0, - (reftable_size - old_reftable_size) * REFTABLE_ENTRY_SI= ZE); + refblock_cluster_index =3D refblock_offset / s->cluster_size; + if (refblock_cluster_index >=3D end_cluster) { + /* + * We must write the refblock that holds this refblock's + * refcount + */ + end_cluster =3D refblock_cluster_index + 1; + } =20 - /* The offset we have for the reftable is now no longer valid; - * this will leak that range, but we can easily fix that by ru= nning - * a leak-fixing check after this rebuild operation */ - reftable_offset =3D -1; - } else { - assert(on_disk_reftable); - } - on_disk_reftable[refblock_index] =3D refblock_offset; + if (reftable_size <=3D refblock_index) { + uint32_t old_reftable_size =3D reftable_size; + uint64_t *new_on_disk_reftable; + + reftable_size =3D + ROUND_UP((refblock_index + 1) * REFTABLE_ENTRY_SIZE, + s->cluster_size) / REFTABLE_ENTRY_SIZE; + new_on_disk_reftable =3D + g_try_realloc(on_disk_reftable, + reftable_size * REFTABLE_ENTRY_SIZE); + if (!new_on_disk_reftable) { + res->check_errors++; + ret =3D -ENOMEM; + goto fail; + } + on_disk_reftable =3D new_on_disk_reftable; =20 - /* If this is apparently the last refblock (for now), try to squee= ze the - * reftable in */ - if (refblock_index =3D=3D (*nb_clusters - 1) >> s->refcount_block_= bits && - reftable_offset < 0) - { - uint64_t reftable_clusters =3D size_to_clusters(s, reftable_si= ze * - REFTABLE_ENTRY_S= IZE); - reftable_offset =3D alloc_clusters_imrt(bs, reftable_clusters, - refcount_table, nb_clust= ers, - &first_free_cluster); - if (reftable_offset < 0) { - fprintf(stderr, "ERROR allocating reftable: %s\n", - strerror(-reftable_offset)); - res->check_errors++; - ret =3D reftable_offset; - goto fail; + memset(on_disk_reftable + old_reftable_size, 0, + (reftable_size - old_reftable_size) * + REFTABLE_ENTRY_SIZE); + + /* + * The offset we have for the reftable is now no longer va= lid; + * this will leak that range, but we can easily fix that by + * running a leak-fixing check after this rebuild operation + */ + reftable_offset =3D -1; + } else { + assert(on_disk_reftable); } + on_disk_reftable[refblock_index] =3D refblock_offset; } =20 ret =3D qcow2_pre_write_overlap_check(bs, 0, refblock_offset, @@ -2459,15 +2466,12 @@ write_refblocks: } =20 if (reftable_offset < 0) { - uint64_t post_refblock_start, reftable_clusters; + uint64_t reftable_clusters; =20 - post_refblock_start =3D ROUND_UP(*nb_clusters, s->refcount_block_s= ize); reftable_clusters =3D size_to_clusters(s, reftable_size * REFTABLE_ENTRY_SIZE); - /* Not pretty but simple */ - if (first_free_cluster < post_refblock_start) { - first_free_cluster =3D post_refblock_start; - } + + first_free_cluster =3D 0; reftable_offset =3D alloc_clusters_imrt(bs, reftable_clusters, refcount_table, nb_clusters, &first_free_cluster); @@ -2479,6 +2483,10 @@ write_refblocks: goto fail; } =20 + assert(offset_into_cluster(s, reftable_offset) =3D=3D 0); + first_cluster =3D reftable_offset / s->cluster_size; + end_cluster =3D first_cluster + reftable_clusters; + goto write_refblocks; } =20 --=20 2.29.2 From nobody Wed Nov 19 07:03:52 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1615392486; cv=none; d=zohomail.com; s=zohoarc; b=BAEqrB7NToOR+/64AzZukSDWKouFVTQ+sLHjdz59G5+NetbkDBImNUrkOaIjBDIR36FxoJ2N7XXF0CXlmtBOvf8WZijf48BLNLlhVUqomcCzEcAh705+EddQJJv/0LdN+M5FTVkrUlHQvi/i5uIJK9DG2GzkaEHhwb99w6ldOHc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1615392486; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=Bbw36JNdEnPmtaYTK3y+jnAxy9/kxpnUxf30UWWTNxY=; b=GTiF2fuPKb+Tev9tkPM/Uy4+PsCJopeVQ+Uqba60GzpbpN6NQOhnl2bOzID/0Jo0/In1BCFNykcfBjC6CScY0dwyGAxpoCcw+4zJHzWTgNA3FceGIYawsSUkSsikozTKjBy+vrWrlakqGcr1kAyzK0s0M2olbjxjf492+c13SU4= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1615392486284240.26453550864358; Wed, 10 Mar 2021 08:08:06 -0800 (PST) Received: from localhost ([::1]:33844 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lK1NU-0000by-DZ for importer@patchew.org; Wed, 10 Mar 2021 11:08:04 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:34060) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lK1F1-0003CB-0S for qemu-devel@nongnu.org; Wed, 10 Mar 2021 10:59:19 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:55179) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1lK1Ez-00023Y-5j for qemu-devel@nongnu.org; Wed, 10 Mar 2021 10:59:18 -0500 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-8-8-YZqb68P7WwjlHsGHvvGA-1; Wed, 10 Mar 2021 10:59:14 -0500 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 7D8B3108BD09; Wed, 10 Mar 2021 15:59:13 +0000 (UTC) Received: from localhost (ovpn-115-159.ams2.redhat.com [10.36.115.159]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 2061C5C1A1; Wed, 10 Mar 2021 15:59:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1615391956; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Bbw36JNdEnPmtaYTK3y+jnAxy9/kxpnUxf30UWWTNxY=; b=FDuLhEXIrvXvV3n5A8V02h6yYRnc0IH1INNOocyCqu4eFSnelU2tQ9NGJ3JsJTN2OB9JlO rWZndJaFQVinwwIEFikbDJHclzKnFlRHnAw1fncm5gjqCdmcyc8n4d17bJxAqQbYxKujHR Y2Vak2ZLQpd8NiSRSiYcNtzGqFbh624= X-MC-Unique: 8-YZqb68P7WwjlHsGHvvGA-1 From: Max Reitz To: qemu-block@nongnu.org Subject: [PATCH 2/4] iotests/common.qemu: Add _cleanup_single_qemu Date: Wed, 10 Mar 2021 16:59:04 +0100 Message-Id: <20210310155906.147478-3-mreitz@redhat.com> In-Reply-To: <20210310155906.147478-1-mreitz@redhat.com> References: <20210310155906.147478-1-mreitz@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=mreitz@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=mreitz@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -29 X-Spam_score: -3.0 X-Spam_bar: --- X-Spam_report: (-3.0 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.243, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , qemu-devel@nongnu.org, Max Reitz Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" _cleanup_qemu cleans up all qemu instances, which sometimes is not very useful. Pull out _cleanup_single_qemu, which does the same only for a single instance. Signed-off-by: Max Reitz --- tests/qemu-iotests/common.qemu | 55 +++++++++++++++++++++------------- 1 file changed, 34 insertions(+), 21 deletions(-) diff --git a/tests/qemu-iotests/common.qemu b/tests/qemu-iotests/common.qemu index 0fc52d20d7..56c1ea1bac 100644 --- a/tests/qemu-iotests/common.qemu +++ b/tests/qemu-iotests/common.qemu @@ -365,37 +365,50 @@ _launch_qemu() } =20 =20 -# Silently kills the QEMU process +# Silently kills all QEMU processes # # If $wait is set to anything other than the empty string, the process wil= l not # be killed but only waited for, and any output will be forwarded to stdou= t. If # $wait is empty, the process will be killed and all output will be suppre= ssed. +# +# To cleanup a single process, use _cleanup_single_qemu instead. _cleanup_qemu() { # QEMU_PID[], QEMU_IN[], QEMU_OUT[] all use same indices for i in "${!QEMU_OUT[@]}" do - local QEMU_PID - if [ -f "${QEMU_TEST_DIR}/qemu-${i}.pid" ]; then - read QEMU_PID < "${QEMU_TEST_DIR}/qemu-${i}.pid" - rm -f "${QEMU_TEST_DIR}/qemu-${i}.pid" - if [ -z "${wait}" ] && [ -n "${QEMU_PID}" ]; then - kill -KILL ${QEMU_PID} 2>/dev/null - fi - if [ -n "${QEMU_PID}" ]; then - wait ${QEMU_PID} 2>/dev/null # silent kill - fi - fi + _cleanup_single_qemu $i + done +} =20 - if [ -n "${wait}" ]; then - cat <&${QEMU_OUT[$i]} | _filter_testdir | _filter_qemu \ - | _filter_qemu_io | _filter_qmp | _filte= r_hmp +# The same as _cleanup_qemu, but for a single instance. +# +# Input parameters: +# $1: qemu handle +_cleanup_single_qemu() +{ + i=3D$1 + + local QEMU_PID + if [ -f "${QEMU_TEST_DIR}/qemu-${i}.pid" ]; then + read QEMU_PID < "${QEMU_TEST_DIR}/qemu-${i}.pid" + rm -f "${QEMU_TEST_DIR}/qemu-${i}.pid" + if [ -z "${wait}" ] && [ -n "${QEMU_PID}" ]; then + kill -KILL ${QEMU_PID} 2>/dev/null fi - rm -f "${QEMU_FIFO_IN}_${i}" "${QEMU_FIFO_OUT}_${i}" - eval "exec ${QEMU_IN[$i]}<&-" # close file descriptors - eval "exec ${QEMU_OUT[$i]}<&-" + if [ -n "${QEMU_PID}" ]; then + wait ${QEMU_PID} 2>/dev/null # silent kill + fi + fi =20 - unset QEMU_IN[$i] - unset QEMU_OUT[$i] - done + if [ -n "${wait}" ]; then + cat <&${QEMU_OUT[$i]} | _filter_testdir | _filter_qemu \ + | _filter_qemu_io | _filter_qmp | _filter_hmp + fi + rm -f "${QEMU_FIFO_IN}_${i}" "${QEMU_FIFO_OUT}_${i}" + eval "exec ${QEMU_IN[$i]}<&-" # close file descriptors + eval "exec ${QEMU_OUT[$i]}<&-" + + unset QEMU_IN[$i] + unset QEMU_OUT[$i] } --=20 2.29.2 From nobody Wed Nov 19 07:03:52 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1615392798; cv=none; d=zohomail.com; s=zohoarc; b=T+M2OVEJApn9r9NdAV8HVaHUp2g87ydLZ+rT3MhnRkqNjo4Ft6Km9x159RZ5RHCs9IX+osdEUe3fnpJsz59sVohrxUSysXoYWzZsL2YN1Yzoila2yhvgVX5MMKtagy8lVvcyo1vw/0SV98C61/6Smf0XwIui9M/dbgQAMTix9R4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1615392798; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=WE4WcvIFHhkJweCB6XjXXqbnzA+3tvmVRvG0rsUu4uc=; b=JBGJU5kJsBpCcgDCAfHCu7Ex4+Us+o0krfOPIRSXkr+NagIF/dzZ/ukWMHbvlJVF3cex/tgKS8UxQNcGFL4x3Aj7K+pG4S9yA32QqKoRVigQNapkqMk7EGsA9j76RIeay9afmq+cvMwCWk6wITlom/9Pwl+15gG270d6u6hsjdw= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1615392798461169.34281144189003; Wed, 10 Mar 2021 08:13:18 -0800 (PST) Received: from localhost ([::1]:50958 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lK1SU-00088P-KU for importer@patchew.org; Wed, 10 Mar 2021 11:13:14 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:34226) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lK1F7-0003Q0-74 for qemu-devel@nongnu.org; Wed, 10 Mar 2021 10:59:25 -0500 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:59036) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1lK1F3-00026b-9n for qemu-devel@nongnu.org; Wed, 10 Mar 2021 10:59:24 -0500 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-577-p3WAt37dMGe0Y-_lw0w-Kw-1; Wed, 10 Mar 2021 10:59:16 -0500 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id C019E80433A; Wed, 10 Mar 2021 15:59:15 +0000 (UTC) Received: from localhost (ovpn-115-159.ams2.redhat.com [10.36.115.159]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 61CD76A047; Wed, 10 Mar 2021 15:59:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1615391960; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=WE4WcvIFHhkJweCB6XjXXqbnzA+3tvmVRvG0rsUu4uc=; b=LLSx+4VVHqpGSfj8r9KbS3J1CXItZ2GEXiUGeku79MWoTlNqMIhXt4bDNJKths8qgUQL4f f4PLWmvx0rvqjzXxUzwqP6uwlqG7nSme8fcN2FL3sIN5RViaSglfzgvDiC02A5kf9rR3cH qbcj7VIjf7HbxF7HYi4qrmUvp/5oeyo= X-MC-Unique: p3WAt37dMGe0Y-_lw0w-Kw-1 From: Max Reitz To: qemu-block@nongnu.org Subject: [PATCH 3/4] iotests/common.qemu: Allow using the QSD Date: Wed, 10 Mar 2021 16:59:05 +0100 Message-Id: <20210310155906.147478-4-mreitz@redhat.com> In-Reply-To: <20210310155906.147478-1-mreitz@redhat.com> References: <20210310155906.147478-1-mreitz@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=mreitz@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=216.205.24.124; envelope-from=mreitz@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -29 X-Spam_score: -3.0 X-Spam_bar: --- X-Spam_report: (-3.0 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.243, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , qemu-devel@nongnu.org, Max Reitz Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" For block things, we often do not need to run all of qemu, so allow using the qemu-storage-daemon instead. Signed-off-by: Max Reitz --- tests/qemu-iotests/common.qemu | 53 +++++++++++++++++++++++++++------- 1 file changed, 43 insertions(+), 10 deletions(-) diff --git a/tests/qemu-iotests/common.qemu b/tests/qemu-iotests/common.qemu index 56c1ea1bac..8003ed3586 100644 --- a/tests/qemu-iotests/common.qemu +++ b/tests/qemu-iotests/common.qemu @@ -291,6 +291,8 @@ _wait_event() # $qmp_pretty: Set this variable to 'y' to enable QMP pretty printing. # $keep_stderr: Set this variable to 'y' to keep QEMU's stderr output on s= tderr. # If this variable is empty, stderr will be redirected to st= dout. +# $qsd: Set this variable to 'y' to use the QSD instead of QEMU. +# stdout and stderr are never redirected when using the QSD. # Returns: # $QEMU_HANDLE: set to a handle value to communicate with this QEMU instan= ce. # @@ -300,18 +302,31 @@ _launch_qemu() local fifo_out=3D local fifo_in=3D =20 + if [[ $qsd =3D 'y' ]]; then + mon_arg=3D'--monitor' + else + mon_arg=3D'-mon' + fi + if (shopt -s nocasematch; [[ "${qemu_comm_method}" =3D=3D "monitor" ]]) then - comm=3D"-monitor stdio" + comm=3D(--chardev stdio,id=3Dpipe + $mon_arg pipe,mode=3Dreadline) else local qemu_comm_method=3D"qmp" if [ "$qmp_pretty" =3D "y" ]; then - comm=3D"-monitor none -qmp-pretty stdio" + comm=3D(--chardev stdio,id=3Dpipe + $mon_arg pipe,mode=3Dcontrol,pretty=3Don) else - comm=3D"-monitor none -qmp stdio" + comm=3D(--chardev stdio,id=3Dpipe + $mon_arg pipe,mode=3Dcontrol,pretty=3Doff) fi fi =20 + if [[ $qsd !=3D 'y' ]]; then + comm=3D(-monitor none "${comm[@]}") + fi + fifo_out=3D${QEMU_FIFO_OUT}_${_QEMU_HANDLE} fifo_in=3D${QEMU_FIFO_IN}_${_QEMU_HANDLE} mkfifo "${fifo_out}" @@ -322,15 +337,23 @@ _launch_qemu() object_options=3D"--object secret,id=3Dkeysec0,data=3D$IMGKEYSECRE= T" fi =20 + if [[ $qsd =3D 'y' ]]; then + cmd=3D$QSD + args=3D() + else + cmd=3D$QEMU + args=3D(-nographic -serial none) + fi + args+=3D(${object_options} "${comm[@]}") + args+=3D("$@") + + # Just set both QEMU_NEED_PID and QSD_NEED_PID, it can't hurt. if [ -z "$keep_stderr" ]; then - QEMU_NEED_PID=3D'y'\ - ${QEMU} ${object_options} -nographic -serial none ${comm} "${@}" >= "${fifo_out}" \ - 2>&1 \ - <"${fifo_in}" & + QEMU_NEED_PID=3D'y' QSD_NEED_PID=3D'y' $cmd "${args[@]}" \ + >"$fifo_out" 2>&1 <"$fifo_in" & elif [ "$keep_stderr" =3D "y" ]; then - QEMU_NEED_PID=3D'y'\ - ${QEMU} ${object_options} -nographic -serial none ${comm} "${@}" >= "${fifo_out}" \ - <"${fifo_in}" & + QEMU_NEED_PID=3D'y' QSD_NEED_PID=3D'y' $cmd "${args[@]}" \ + >"$fifo_out" <"$fifo_in" & else exit 1 fi @@ -360,6 +383,16 @@ _launch_qemu() silent=3Dyes _timed_wait_for ${_QEMU_HANDLE} "^}" fi fi + + if [[ $qsd =3D 'y' ]]; then + # Wait for PID file, then move it to where qemu would put it + pidfile=3D"$QEMU_TEST_DIR/qemu-storage-daemon.pid" + while [[ ! -f $pidfile ]]; do + sleep 0.5 + done + mv "$pidfile" "$QEMU_TEST_DIR/qemu-${_QEMU_HANDLE}.pid" + fi + QEMU_HANDLE=3D${_QEMU_HANDLE} let _QEMU_HANDLE++ } --=20 2.29.2 From nobody Wed Nov 19 07:03:52 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1615392629; cv=none; d=zohomail.com; s=zohoarc; b=h9TlLTP3WUIcdEH0AZbIv/BUMpoXk6XC/3R2PQoF4wbSNQQNHjYFyvVgm4uRUoEczuTskB1ANbaVKeRKh0kJAcKg9kWdWbrvQyKy6NuU2lslqeg1YABLqZcqqNoZG5o00S2E5Se6JmWoy6e7bZQWumAJnUNobY9Q16L57St4w+s= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1615392629; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=ptiI8wlesc93LTtOvL0SEzMeXIKn4dDtcj6oF1KiQS4=; b=dQ6cS2+CsGp2/t7MpmeEYXRbD4HuIRQhW19DGzOUKQLJVOSw8QRRAajYjU/hEGBsm0D0ZWF/bGqh8fl3BdchgXDD2Wv1o0DX0FmPffYR9PiZxdjusHpd0Av/2YFPILaTiGVRl58/RADWImWUVXuxWUAM9qicXSzJLWJQDz02Cqc= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1615392629273920.9331255655635; Wed, 10 Mar 2021 08:10:29 -0800 (PST) Received: from localhost ([::1]:42466 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lK1Pm-0004b4-S8 for importer@patchew.org; Wed, 10 Mar 2021 11:10:27 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:34224) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lK1F6-0003PP-SK for qemu-devel@nongnu.org; Wed, 10 Mar 2021 10:59:24 -0500 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:38639) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1lK1F4-00027U-62 for qemu-devel@nongnu.org; Wed, 10 Mar 2021 10:59:24 -0500 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-409-WLCVOUf6N7GQmDseO1-2ww-1; Wed, 10 Mar 2021 10:59:19 -0500 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 2489B1005D4E; Wed, 10 Mar 2021 15:59:18 +0000 (UTC) Received: from localhost (ovpn-115-159.ams2.redhat.com [10.36.115.159]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 8A63F5B6BF; Wed, 10 Mar 2021 15:59:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1615391961; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ptiI8wlesc93LTtOvL0SEzMeXIKn4dDtcj6oF1KiQS4=; b=Ld2WakUomr40j0ahJBzOe2zIiSsai3hjc1nF0DXTAxZ9dH8h1h+q/IwmfwRtqi1AvhHLh1 Sqz3NKQlX82G0IkfZh099RH5Z5DoH+nECX6rloCgd6E8vCzXB5X252hwWJJAW+5g/RxfkG +67aiE0vRXvefs+KGA97A+O/WFv1TNA= X-MC-Unique: WLCVOUf6N7GQmDseO1-2ww-1 From: Max Reitz To: qemu-block@nongnu.org Subject: [PATCH 4/4] iotests/108: Test new refcount rebuild algorithm Date: Wed, 10 Mar 2021 16:59:06 +0100 Message-Id: <20210310155906.147478-5-mreitz@redhat.com> In-Reply-To: <20210310155906.147478-1-mreitz@redhat.com> References: <20210310155906.147478-1-mreitz@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=mreitz@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=216.205.24.124; envelope-from=mreitz@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -29 X-Spam_score: -3.0 X-Spam_bar: --- X-Spam_report: (-3.0 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.243, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , qemu-devel@nongnu.org, Max Reitz Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" One clear problem with how qcow2's refcount structure rebuild algorithm used to be before "qcow2: Improve refcount structure rebuilding" was that it is prone to failure for qcow2 images on block devices: There is generally unused space after the actual image, and if that exceeds what one refblock covers, the old algorithm would invariably write the reftable past the block device's end, which cannot work. The new algorithm does not have this problem. Test it with three tests: (1) Create an image with more empty space at the end than what one refblock covers, see whether rebuilding the refcount structures results in a change in the image file length. (It should not.) (2) Leave precisely enough space somewhere at the beginning of the image for the new reftable (and the refblock for that place), see whether the new algorithm puts the reftable there. (It should.) (3) Test the original problem: Create (something like) a block device with a fixed size, then create a qcow2 image in there, write some data, and then have qemu-img check rebuild the refcount structures. Before HEAD^, the reftable would have been written past the image file end, i.e. outside of what the block device provides, which cannot work. HEAD^ should have fixed that. ("Something like a block device" means a loop device if we can use one ("sudo -n losetup" works), or a FUSE block export with growable=3Dfalse otherwise.) Signed-off-by: Max Reitz --- tests/qemu-iotests/108 | 259 +++++++++++++++++++++++++++++++++++++ tests/qemu-iotests/108.out | 80 ++++++++++++ 2 files changed, 339 insertions(+) diff --git a/tests/qemu-iotests/108 b/tests/qemu-iotests/108 index 8eaef0b8bf..7b1aa08781 100755 --- a/tests/qemu-iotests/108 +++ b/tests/qemu-iotests/108 @@ -37,6 +37,7 @@ trap "_cleanup; exit \$status" 0 1 2 3 15 # get standard environment, filters and checks . ./common.rc . ./common.filter +. ./common.qemu =20 # This tests qcow2-specific low-level functionality _supported_fmt qcow2 @@ -47,6 +48,22 @@ _supported_os Linux # files _unsupported_imgopts 'refcount_bits=3D\([^1]\|.\([^6]\|$\)\)' data_file =20 +# This test either needs sudo -n losetup or FUSE exports to work +if sudo -n losetup &>/dev/null; then + loopdev=3Dtrue +else + loopdev=3Dfalse + + # QSD --export fuse will either yield "Parameter 'id' is missing" + # or "Invalid parameter 'fuse'", depending on whether there is + # FUSE support or not. + error=3D$($QSD --export fuse 2>&1) + if [[ $error =3D *"Invalid parameter 'fuse'" ]]; then + _notrun 'Passwordless sudo for losetup or FUSE support required, b= ut' \ + 'neither is available' + fi +fi + echo echo '=3D=3D=3D Repairing an image without any refcount table =3D=3D=3D' echo @@ -138,6 +155,248 @@ _make_test_img 64M poke_file "$TEST_IMG" $((0x10008)) "\xff\xff\xff\xff\xff\xff\x00\x00" _check_test_img -r all =20 +echo +echo '=3D=3D=3D Check rebuilt reftable location =3D=3D=3D' + +# In an earlier version of the refcount rebuild algorithm, the +# reftable was generally placed at the image end (unless something was +# allocated in the area covered by the refblock right before the image +# file end, then we would try to place the reftable in that refblock). +# This was later changed so the reftable would be placed in the +# earliest possible location. Test this. + +echo +echo '--- Does the image size increase? ---' +echo + +# First test: Just create some image, write some data to it, and +# resize it so there is free space at the end of the image (enough +# that it spans at least one full refblock, which for cluster_size=3D512 +# images, spans 128k). With the old algorithm, the reftable would +# have then been placed at the end of the image file, but with the new +# one, it will be put in that free space. +# We want to check whether the size of the image file increases due to +# rebuilding the refcount structures (it should not). + +_make_test_img -o 'cluster_size=3D512' 1M +# Write something +$QEMU_IO -c 'write 0 64k' "$TEST_IMG" | _filter_qemu_io + +# Add free space +file_len=3D$(stat -c '%s' "$TEST_IMG") +truncate -s $((file_len + 256 * 1024)) "$TEST_IMG" + +# Corrupt the image by saying the image header were not allocated +rt_offset=3D$(peek_file_be "$TEST_IMG" 48 8) +rb_offset=3D$(peek_file_be "$TEST_IMG" $rt_offset 8) +poke_file "$TEST_IMG" $rb_offset "\x00\x00" + +# Check whether rebuilding the refcount structures increases the image +# file size +file_len=3D$(stat -c '%s' "$TEST_IMG") +echo +# The only leaks there can be are the old refcount structures that are +# leaked during rebuilding, no need to clutter the output with them +_check_test_img -r all | grep -v '^Repairing cluster.*refcount=3D1 referen= ce=3D0' +echo +post_repair_file_len=3D$(stat -c '%s' "$TEST_IMG") + +if [[ $file_len -eq $post_repair_file_len ]]; then + echo 'OK: Image size did not change' +else + echo 'ERROR: Image size differs' \ + "($file_len before, $post_repair_file_len after)" +fi + +echo +echo '--- Will the reftable occupy a hole specifically left for it? ---' +echo + +# Note: With cluster_size=3D512, every refblock covers 128k. +# The reftable covers 8M per reftable cluster. + +# Create an image that requires two reftable clusters (just because +# this is more interesting than a single-clustered reftable). +_make_test_img -o 'cluster_size=3D512' 9M +$QEMU_IO -c 'write 0 8M' "$TEST_IMG" | _filter_qemu_io + +# Writing 8M will have resized the reftable. Unfortunately, doing so +# will leave holes in the file, so we need to fill them up so we can +# be sure the whole file is allocated. Do that by writing +# consecutively smaller chunks starting from 8 MB, until the file +# length increases even with a chunk size of 512. Then we must have +# filled all holes. +ofs=3D$((8 * 1024 * 1024)) +block_len=3D$((16 * 1024)) +while [[ $block_len -ge 512 ]]; do + file_len=3D$(stat -c '%s' "$TEST_IMG") + while [[ $(stat -c '%s' "$TEST_IMG") -eq $file_len ]]; do + # Do not include this in the reference output, it does not + # really matter which qemu-io calls we do here exactly + $QEMU_IO -c "write $ofs $block_len" "$TEST_IMG" >/dev/null + ofs=3D$((ofs + block_len)) + done + block_len=3D$((block_len / 2)) +done + +# Fill up to 9M (do not include this in the reference output either, +# $ofs is random for all we know) +$QEMU_IO -c "write $ofs $((9 * 1024 * 1024 - ofs))" "$TEST_IMG" >/dev/null + +# Make space as follows: +# - For the first refblock: Right at the beginning of the image (this +# refblock is placed in the first place possible), +# - For the reftable somewhere soon afterwards, still near the +# beginning of the image (i.e. covered by the first refblock); the +# reftable too is placed in the first place possible, but only after +# all refblocks have been placed) +# No space is needed for the other refblocks, because no refblock is +# put before the space it covers. In this test case, we do not mind +# if they are placed at the image file's end. + +# Before we make that space, we have to find out the host offset of +# the area that belonged to the two data clusters at guest offset 4k, +# because we expect the reftable to be placed there, and we will have +# to verify that it is. + +l1_offset=3D$(peek_file_be "$TEST_IMG" 40 8) +l2_offset=3D$(peek_file_be "$TEST_IMG" $l1_offset 8) +l2_offset=3D$((l2_offset & 0x00fffffffffffe00)) +data_4k_offset=3D$(peek_file_be "$TEST_IMG" \ + $((l2_offset + 4096 / 512 * 8)) 8) +data_4k_offset=3D$((data_4k_offset & 0x00fffffffffffe00)) + +$QEMU_IO -c "discard 0 512" -c "discard 4k 1k" "$TEST_IMG" | _filter_qemu_= io + +# Corrupt the image by saying the image header were not allocated +rt_offset=3D$(peek_file_be "$TEST_IMG" 48 8) +rb_offset=3D$(peek_file_be "$TEST_IMG" $rt_offset 8) +poke_file "$TEST_IMG" $rb_offset "\x00\x00" + +echo +# The only leaks there can be are the old refcount structures that are +# leaked during rebuilding, no need to clutter the output with them +_check_test_img -r all | grep -v '^Repairing cluster.*refcount=3D1 referen= ce=3D0' +echo + +# Check whether the reftable was put where we expected +rt_offset=3D$(peek_file_be "$TEST_IMG" 48 8) +if [[ $rt_offset -eq $data_4k_offset ]]; then + echo 'OK: Reftable is where we expect it' +else + echo "ERROR: Reftable is at $rt_offset, but was expected at $data_4k_o= ffset" +fi + +echo +echo '--- Rebuilding refcount structures on block devices ---' +echo + +# A block device cannot really grow, at least not during qemu-img +# check. As mentioned in the above cases, rebuilding the refcount +# structure may lead to new refcount structures being written after +# the end of the image, and in the past that happened even if there +# was more than sufficient space in the image. Such post-EOF writes +# will not work on block devices, so test that the new algorithm +# avoids it. + +# If we have passwordless sudo and losetup, we can use those to create +# a block device. Otherwise, we can resort to qemu's FUSE export to +# create a file that isn't growable, which effectively tests the same +# thing. + +_cleanup_test_img +truncate -s $((64 * 1024 * 1024)) "$TEST_IMG" + +if $loopdev; then + export_mp=3D$(sudo -n losetup --show -f "$TEST_IMG") + export_mp_driver=3Dhost_device + sudo -n chmod go+rw "$export_mp" +else + # Create non-growable FUSE export that is a bit like an empty + # block device + export_mp=3D"$TEST_DIR/fuse-export" + export_mp_driver=3Dfile + touch "$export_mp" + + qsd=3Dy _launch_qemu \ + --blockdev file,node-name=3Dexport-node,filename=3D"$TEST_IMG" \ + --export fuse,id=3Dfuse-export,node-name=3Dexport-node,mountpoint= =3D"$export_mp",writable=3Don,growable=3Doff + fuse_export_handle=3D$QEMU_HANDLE + + # Must not appear in the reference output (to stay equal to the + # losetup branch). + _send_qemu_cmd \ + $fuse_export_handle \ + '{ "execute": "qmp_capabilities" }' \ + 'return' \ + >/dev/null +fi + +# Now create a qcow2 image on the device -- unfortunately, qemu-img +# create force-creates the file, so we have to resort to the +# blockdev-create job. +qsd=3Dy _launch_qemu \ + --blockdev $export_mp_driver,node-name=3Dfile,filename=3D"$export_mp" + +_send_qemu_cmd \ + $QEMU_HANDLE \ + '{ "execute": "qmp_capabilities" }' \ + 'return' + +# Small cluster size again, so the image needs multiple refblocks +_send_qemu_cmd \ + $QEMU_HANDLE \ + '{ "execute": "blockdev-create", + "arguments": { + "job-id": "create", + "options": { + "driver": "qcow2", + "file": "file", + "size": '$((64 * 1024 * 1024))', + "cluster-size": 512 + } } }' \ + '"concluded"' + +_send_qemu_cmd \ + $QEMU_HANDLE \ + '{ "execute": "job-dismiss", "arguments": { "id": "create" } }' \ + 'return' + +_send_qemu_cmd \ + $QEMU_HANDLE \ + '{ "execute": "quit" }' \ + 'return' + +wait=3Dy _cleanup_single_qemu $QEMU_HANDLE +echo + +# Write some data +$QEMU_IO -c 'write 0 64k' "$export_mp" | _filter_qemu_io + +# Corrupt the image by saying the image header were not allocated +rt_offset=3D$(peek_file_be "$export_mp" 48 8) +rb_offset=3D$(peek_file_be "$export_mp" $rt_offset 8) +poke_file "$export_mp" $rb_offset "\x00\x00" + +echo +TEST_IMG=3D"$export_mp" _check_test_img -r all \ + | grep -v '^Repairing cluster.*refcount=3D1 reference=3D0' + +if $loopdev; then + sudo -n losetup -d "$export_mp" +else + # Again, must not appear in the reference output (to stay equal to + # the losetup branch). + _send_qemu_cmd \ + $fuse_export_handle \ + '{ "execute": "quit" }' \ + 'return' \ + >/dev/null + + wait=3Dy _cleanup_single_qemu $fuse_export_handle >/dev/null + rm -f "$export_mp" +fi + # success, all done echo '*** done' rm -f $seq.full diff --git a/tests/qemu-iotests/108.out b/tests/qemu-iotests/108.out index 75bab8dc84..a4c571df1a 100644 --- a/tests/qemu-iotests/108.out +++ b/tests/qemu-iotests/108.out @@ -105,6 +105,86 @@ The following inconsistencies were found and repaired: 0 leaked clusters 1 corruptions =20 +Double checking the fixed image now... +No errors were found on the image. + +=3D=3D=3D Check rebuilt reftable location =3D=3D=3D + +--- Does the image size increase? --- + +Formatting 'TEST_DIR/t.IMGFMT', fmt=3DIMGFMT size=3D1048576 +wrote 65536/65536 bytes at offset 0 +64 KiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec) + +ERROR cluster 0 refcount=3D0 reference=3D1 +Rebuilding refcount structure +The following inconsistencies were found and repaired: + + 0 leaked clusters + 1 corruptions + +Double checking the fixed image now... +No errors were found on the image. + +OK: Image size did not change + +--- Will the reftable occupy a hole specifically left for it? --- + +Formatting 'TEST_DIR/t.IMGFMT', fmt=3DIMGFMT size=3D9437184 +wrote 8388608/8388608 bytes at offset 0 +8 MiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec) +discard 512/512 bytes at offset 0 +512 bytes, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec) +discard 1024/1024 bytes at offset 4096 +1 KiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec) + +ERROR cluster 0 refcount=3D0 reference=3D1 +Rebuilding refcount structure +The following inconsistencies were found and repaired: + + 0 leaked clusters + 1 corruptions + +Double checking the fixed image now... +No errors were found on the image. + +OK: Reftable is where we expect it + +--- Rebuilding refcount structures on block devices --- + +{ "execute": "qmp_capabilities" } +{"return": {}} +{ "execute": "blockdev-create", + "arguments": { + "job-id": "create", + "options": { + "driver": "IMGFMT", + "file": "file", + "size": 67108864, + "cluster-size": 512 + } } } +{"timestamp": {"seconds": TIMESTAMP, "microseconds": TIMESTAMP}, "event"= : "JOB_STATUS_CHANGE", "data": {"status": "created", "id": "create"}} +{"timestamp": {"seconds": TIMESTAMP, "microseconds": TIMESTAMP}, "event"= : "JOB_STATUS_CHANGE", "data": {"status": "running", "id": "create"}} +{"return": {}} +{"timestamp": {"seconds": TIMESTAMP, "microseconds": TIMESTAMP}, "event"= : "JOB_STATUS_CHANGE", "data": {"status": "waiting", "id": "create"}} +{"timestamp": {"seconds": TIMESTAMP, "microseconds": TIMESTAMP}, "event"= : "JOB_STATUS_CHANGE", "data": {"status": "pending", "id": "create"}} +{"timestamp": {"seconds": TIMESTAMP, "microseconds": TIMESTAMP}, "event"= : "JOB_STATUS_CHANGE", "data": {"status": "concluded", "id": "create"}} +{ "execute": "job-dismiss", "arguments": { "id": "create" } } +{"timestamp": {"seconds": TIMESTAMP, "microseconds": TIMESTAMP}, "event"= : "JOB_STATUS_CHANGE", "data": {"status": "null", "id": "create"}} +{"return": {}} +{ "execute": "quit" } +{"return": {}} + +wrote 65536/65536 bytes at offset 0 +64 KiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec) + +ERROR cluster 0 refcount=3D0 reference=3D1 +Rebuilding refcount structure +The following inconsistencies were found and repaired: + + 0 leaked clusters + 1 corruptions + Double checking the fixed image now... No errors were found on the image. *** done --=20 2.29.2