From nobody Wed Oct 29 11:38:24 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=gmail.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1526053179352213.91879116962775; Fri, 11 May 2018 08:39:39 -0700 (PDT) Received: from localhost ([::1]:51945 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fHA8w-0004uN-Ks for importer@patchew.org; Fri, 11 May 2018 11:39:38 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:48001) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fHA6P-0001wV-FN for qemu-devel@nongnu.org; Fri, 11 May 2018 11:37:07 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fHA6O-0005Xa-Fg for qemu-devel@nongnu.org; Fri, 11 May 2018 11:37:01 -0400 Received: from mail-pf0-x241.google.com ([2607:f8b0:400e:c00::241]:41386) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1fHA6I-0005MG-0q; Fri, 11 May 2018 11:36:54 -0400 Received: by mail-pf0-x241.google.com with SMTP id v63-v6so2897354pfk.8; Fri, 11 May 2018 08:36:53 -0700 (PDT) Received: from VM_111_45_centos.localdomain ([119.28.55.16]) by smtp.gmail.com with ESMTPSA id v186-v6sm6681271pfb.45.2018.05.11.08.36.50 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 11 May 2018 08:36:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=TH4+bUov8lniAsM9xCTwu74va3CKGLcHwpBNmUgaFh4=; b=iBbyXo02Xc1+4bxkeLLQfNVkK9aW2A1pmLkzAs+RYWNfkLy5rPu66pTBKBd6lLDLqV nmwrPHrcdZKXKl6sR4O7P/OVfa4vh8naDunn735WfbjUMqnZO5hIxMvbzt2A/0acVN0c V2gCxt2IUCOfr40YEyNC33r7Y2Q03uo1F25qeNgmhsV0AviUJhA5aQxUfDMEMWs7McE6 z0XhotdseCoHSeex2w/lCsVFzRdj0jFYo991b4QPkIOjTHvKAH5zAS+4oASzLjpTRiuE Atle+/kLQ0MJv9x4V9aBJRIwh2pFw3sQB4VL2qiSiDQ8hRdJOXbntCm1r9FSBv9u2723 IP/A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=TH4+bUov8lniAsM9xCTwu74va3CKGLcHwpBNmUgaFh4=; b=hDtzOTl0IQejJsq4hD6yD+sVOK4nItWtCrey6KdYHn4npqhIPwpwnAU0cBTrYzAbkA qFIljOsO++PC0bad1aZrSw5FIQFGud08A7gyMDucfsH8BEQI96YR4qCsti2dxBXWFbSc FeGFoSW0FlUK3nB0d1uQIcBKaY/YDzhiOmyhF25bQwWvkBOK3DV/F1w3jPbHIMluuOAf qLWICUX367xJYNVN9+Bd52uGF04jb2zvlZBIfDFinSD3S6Xu+Qe5CEzJ7q3vFkTCL/av UOljoS13VR+q8piq4HmcRaNQvQH8XiiQXiafAGMW+aRK9FRTFOZjCl/YHN9HNN++f4bq iBzw== X-Gm-Message-State: ALKqPwfljo3nOdLUYAUE6ifL+mgGdeiKzQTjNRMNq32bFnKPotZLpnar Ih940TYoLNWZK24HdFHatxQ= X-Google-Smtp-Source: AB8JxZqRvHGCp0TlfNJuyTOaK6bQDC8hALpsx0kr7aT4DkWmkZNuK4x6yja87mYCP7PFvN75H/NFBg== X-Received: by 2002:a63:a807:: with SMTP id o7-v6mr4848409pgf.153.1526053012693; Fri, 11 May 2018 08:36:52 -0700 (PDT) From: Ivan Ren X-Google-Original-From: Ivan Ren To: mreitz@redhat.com, kwolf@redhat.com Date: Fri, 11 May 2018 23:36:44 +0800 Message-Id: <1526053004-12268-1-git-send-email-ivanren@tencent.com> X-Mailer: git-send-email 1.8.3.1 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:400e:c00::241 Subject: [Qemu-devel] [PATCH v3] qcow2: fix preallocation with metadata on bare block device X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: qemu-devel@nongnu.org, qemu-block@nongnu.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Create a qcow2 directly on bare block device with "-o preallocation=3Dmetadata" option. When read this qcow2, it will return pre-existing data on block device. This patch add QCOW_OFLAG_ZERO flag (supported in qcow_version >=3D 3) for preallocated l2 entry to avoid this problem. Signed-off-by: Ivan Ren --- Changes in v2: - always pass QCOW_OFLAG_ZERO when preallocate metadta Changes in v3: - limit this feature only on qcow_version >=3D 3 --- block/qcow2-cluster.c | 5 +++-- block/qcow2.c | 12 +++++++++--- block/qcow2.h | 3 ++- 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/block/qcow2-cluster.c b/block/qcow2-cluster.c index 1aee726..b9e0ceb 100644 --- a/block/qcow2-cluster.c +++ b/block/qcow2-cluster.c @@ -919,7 +919,8 @@ fail: return ret; } =20 -int qcow2_alloc_cluster_link_l2(BlockDriverState *bs, QCowL2Meta *m) +int qcow2_alloc_cluster_link_l2(BlockDriverState *bs, QCowL2Meta *m, + uint64_t flags) { BDRVQcow2State *s =3D bs->opaque; int i, j =3D 0, l2_index, ret; @@ -969,7 +970,7 @@ int qcow2_alloc_cluster_link_l2(BlockDriverState *bs, Q= CowL2Meta *m) } =20 l2_slice[l2_index + i] =3D cpu_to_be64((cluster_offset + - (i << s->cluster_bits)) | QCOW_OFLAG_COPIED); + (i << s->cluster_bits)) | QCOW_OFLAG_COPIED | flags); } =20 =20 diff --git a/block/qcow2.c b/block/qcow2.c index 2f36e63..ee862b0 100644 --- a/block/qcow2.c +++ b/block/qcow2.c @@ -2044,7 +2044,7 @@ static coroutine_fn int qcow2_co_pwritev(BlockDriverS= tate *bs, uint64_t offset, while (l2meta !=3D NULL) { QCowL2Meta *next; =20 - ret =3D qcow2_alloc_cluster_link_l2(bs, l2meta); + ret =3D qcow2_alloc_cluster_link_l2(bs, l2meta, 0); if (ret < 0) { goto fail; } @@ -2552,7 +2552,13 @@ static void coroutine_fn preallocate_co(void *opaque) while (meta) { QCowL2Meta *next =3D meta->next; =20 - ret =3D qcow2_alloc_cluster_link_l2(bs, meta); + if (s->qcow_version >=3D 3) { + /* add QCOW_OFLAG_ZERO to avoid pre-existing data be read = */ + ret =3D qcow2_alloc_cluster_link_l2(bs, meta, QCOW_OFLAG_Z= ERO); + } else { + ret =3D qcow2_alloc_cluster_link_l2(bs, meta, 0); + } + if (ret < 0) { qcow2_free_any_clusters(bs, meta->alloc_offset, meta->nb_clusters, QCOW2_DISCARD_N= EVER); @@ -3458,7 +3464,7 @@ static int qcow2_truncate(BlockDriverState *bs, int64= _t offset, }; qemu_co_queue_init(&allocation.dependent_requests); =20 - ret =3D qcow2_alloc_cluster_link_l2(bs, &allocation); + ret =3D qcow2_alloc_cluster_link_l2(bs, &allocation, 0); if (ret < 0) { error_setg_errno(errp, -ret, "Failed to update L2 tables"); qcow2_free_clusters(bs, host_offset, diff --git a/block/qcow2.h b/block/qcow2.h index adf5c39..9a59602 100644 --- a/block/qcow2.h +++ b/block/qcow2.h @@ -617,7 +617,8 @@ uint64_t qcow2_alloc_compressed_cluster_offset(BlockDri= verState *bs, uint64_t offset, int compressed_size); =20 -int qcow2_alloc_cluster_link_l2(BlockDriverState *bs, QCowL2Meta *m); +int qcow2_alloc_cluster_link_l2(BlockDriverState *bs, QCowL2Meta *m, + uint64_t flags); int qcow2_cluster_discard(BlockDriverState *bs, uint64_t offset, uint64_t bytes, enum qcow2_discard_type type, bool full_discard); --=20 1.8.3.1