From nobody Wed Oct 29 11:29:44 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 1525782534493150.83545352796966; Tue, 8 May 2018 05:28:54 -0700 (PDT) Received: from localhost ([::1]:50935 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fG1je-0005vC-BN for importer@patchew.org; Tue, 08 May 2018 08:28:50 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:40953) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fG1iT-0005JT-V2 for qemu-devel@nongnu.org; Tue, 08 May 2018 08:27:39 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fG1iS-0008I7-UX for qemu-devel@nongnu.org; Tue, 08 May 2018 08:27:37 -0400 Received: from mail-pg0-x241.google.com ([2607:f8b0:400e:c05::241]:41329) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1fG1iO-0008G0-3f; Tue, 08 May 2018 08:27:32 -0400 Received: by mail-pg0-x241.google.com with SMTP id m21-v6so21127530pgv.8; Tue, 08 May 2018 05:27:31 -0700 (PDT) Received: from VM_111_45_centos.localdomain ([119.28.55.16]) by smtp.gmail.com with ESMTPSA id c8sm45109950pfi.96.2018.05.08.05.27.29 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 08 May 2018 05:27:30 -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=hC3tdYnSWOMnc0zu26LBqJnc434e/LIfRYGuBHtT3j8=; b=dyksluvieA2giqHyTYhnzA+l/TU9JANnjzrTQFDbPDQdbt641nCUUfwwgEnIKnvyOI V2Zl/q4QzIDfjQ93oR1K2cudqZ/3g1a7EhCkjnnRQix6nz7W9U7v1wtvMo1U0rYpDkPG mRuIf4fTG75knEMCrpGqcbOB2ivn3FXfczocZ9cntgifi0k/pAQkt0daL7Lcd+oAQuLK 3ox1Z30Ql34mnuVMJCB0AFfpZg7xkVjaYQQ4QKFxkKAYWPPSURGMbe+iHvLh7NITeVEu OtJC8n49yXUglY7f1vkmiy84vgzKOprHfr7A6s1VZ16zGwXJbZajHTAB8w1FgvmL8kV4 mTiw== 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=hC3tdYnSWOMnc0zu26LBqJnc434e/LIfRYGuBHtT3j8=; b=ilsG7o2ceZH5z/m/vUu1sEGdNPsw+Yck/doGwgNcGqbfEZ2c9Q8RLVfQZ4kC0na7n+ XSIqUTQKNbsu/7SUyMOQ4+IuA9CRsRWloodIGV9mCo/ct9on4c+iVhVv4GsCAIem64+A wS4WNR0wxAS+q0J9seePu3GMpuohr9mrKZ+7j/4sReg9yzpjGUUJqg+Mxy/Sffi9fW1a WI7Oyya28REQ1bzCKxCT8WF6R/18g6WHPjPC/D5ZRizETuqVgRiBiCeY0oxiGqJVpDKt ZdZJgP9rE/sgISi3J+CW4M41MyNEXsxAhQjNAyLKUqtH8Ujt0hrZBy8IYIdqWqd8VNM9 j+Sw== X-Gm-Message-State: ALQs6tDBRcauz71SOBkbA73OkXNgtCFqmxpY0GbkGmFeq4PJDa7WEUVL jaXVEO9ivdie4VWigo0xnq0= X-Google-Smtp-Source: AB8JxZrSgDrC5Shaqs1DVpEdLrKly+xYWw2JR7tjjGXfF+3r/Nj7IxF70wlMqUVrXal1bdy86hh+EA== X-Received: by 2002:a63:6406:: with SMTP id y6-v6mr32666104pgb.205.1525782450993; Tue, 08 May 2018 05:27:30 -0700 (PDT) From: Ivan Ren X-Google-Original-From: Ivan Ren To: mreitz@redhat.com, kwolf@redhat.com Date: Tue, 8 May 2018 20:27:21 +0800 Message-Id: <1525782441-2224-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:c05::241 Subject: [Qemu-devel] [PATCH] 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 dirty data of block device. This patch add QCOW_OFLAG_ZERO for all preallocated l2 entry if the underlying device is a bare block device. Signed-off-by: Ivan Ren --- block/qcow2-cluster.c | 5 +++-- block/qcow2.c | 19 ++++++++++++++++--- block/qcow2.h | 3 ++- 3 files changed, 21 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..093735c 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; } @@ -2534,6 +2534,7 @@ static void coroutine_fn preallocate_co(void *opaque) uint64_t host_offset =3D 0; unsigned int cur_bytes; int ret; + struct stat st; QCowL2Meta *meta; =20 qemu_co_mutex_lock(&s->lock); @@ -2552,7 +2553,19 @@ 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); + /* Check the underlying device type. + * If the underlying device is a block device, we add + * QCOW_OFLAG_ZERO for all preallocated l2 entry to ignore dir= ty + * data on block device. + * If the underlying device can't be used with stat(return < 0= ), + * treat it as a regular file. + */ + if (stat(bs->filename, &st) < 0 || !S_ISBLK(st.st_mode)) { + ret =3D qcow2_alloc_cluster_link_l2(bs, meta, 0); + } else { + ret =3D qcow2_alloc_cluster_link_l2(bs, meta, QCOW_OFLAG_Z= ERO); + } + if (ret < 0) { qcow2_free_any_clusters(bs, meta->alloc_offset, meta->nb_clusters, QCOW2_DISCARD_N= EVER); @@ -3458,7 +3471,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