From nobody Fri May 3 07:21:19 2024 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.zoho.com; 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; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1495102239592245.96877893850342; Thu, 18 May 2017 03:10:39 -0700 (PDT) Received: from localhost ([::1]:52806 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dBIOD-0003Bv-Mb for importer@patchew.org; Thu, 18 May 2017 06:10:37 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:40910) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dBINP-0002nO-RS for qemu-devel@nongnu.org; Thu, 18 May 2017 06:09:48 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dBINM-0005Z6-Jc for qemu-devel@nongnu.org; Thu, 18 May 2017 06:09:47 -0400 Received: from mailhub.sw.ru ([195.214.232.25]:47700 helo=relay.sw.ru) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1dBINM-0005Z2-8I for qemu-devel@nongnu.org; Thu, 18 May 2017 06:09:44 -0400 Received: from kvm.sw.ru (msk-vpn.virtuozzo.com [195.214.232.6]) by relay.sw.ru (8.13.4/8.13.4) with ESMTP id v4IA9PqR016118; Thu, 18 May 2017 13:09:26 +0300 (MSK) From: Vladimir Sementsov-Ogievskiy To: qemu-devel@nongnu.org, qemu-block@nongnu.org Date: Thu, 18 May 2017 13:09:25 +0300 Message-Id: <20170518100925.28682-1-vsementsov@virtuozzo.com> X-Mailer: git-send-email 2.11.1 X-detected-operating-system: by eggs.gnu.org: OpenBSD 3.x [fuzzy] X-Received-From: 195.214.232.25 Subject: [Qemu-devel] [PATCH] qcow2: add allocated-size to image specific info 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: kwolf@redhat.com, vsementsov@virtuozzo.com, armbru@redhat.com, mreitz@redhat.com, den@openvz.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Shows, how much data qcow2 allocates in underlying file. This should be helpful on non-sparse file systems, when qemu-img info "disk size" doesn't provide this information. Signed-off-by: Vladimir Sementsov-Ogievskiy --- Hi all. Here is an allocated-size feature for qemu-img info. block/qcow2-refcount.c | 60 ++++++++++++++++++++++++++++++++++++++++++++++= ++++ block/qcow2.c | 4 ++++ block/qcow2.h | 2 ++ qapi/block-core.json | 6 ++++- 4 files changed, 71 insertions(+), 1 deletion(-) diff --git a/block/qcow2-refcount.c b/block/qcow2-refcount.c index 7c06061aae..a2f7696a0a 100644 --- a/block/qcow2-refcount.c +++ b/block/qcow2-refcount.c @@ -2931,3 +2931,63 @@ done: qemu_vfree(new_refblock); return ret; } + +typedef struct NbAllocatedClustersCo { + BlockDriverState *bs; + int64_t ret; +} NbAllocatedClustersCo; + +static void coroutine_fn qcow2_nb_allocated_clusters_co_entry(void *opaque) +{ + NbAllocatedClustersCo *nbco =3D opaque; + BlockDriverState *bs =3D nbco->bs; + BDRVQcow2State *s =3D bs->opaque; + int64_t size, nb_clusters, nb_allocated =3D 0, i; + int ret =3D 0; + + size =3D bdrv_getlength(bs->file->bs); + if (size < 0) { + nbco->ret =3D size; + return; + } + + nb_clusters =3D size_to_clusters(s, size); + + qemu_co_mutex_lock(&s->lock); + + for (i =3D 0; i < nb_clusters; ++i) { + uint64_t refcount; + ret =3D qcow2_get_refcount(bs, i, &refcount); + if (ret < 0) { + nbco->ret =3D ret; + qemu_co_mutex_unlock(&s->lock); + return; + } + if (refcount > 0) { + nb_allocated++; + } + } + + qemu_co_mutex_unlock(&s->lock); + + nbco->ret =3D nb_allocated; +} + +int64_t qcow2_nb_allocated_clusters(BlockDriverState *bs) +{ + NbAllocatedClustersCo nbco =3D { + .bs =3D bs, + .ret =3D -EINPROGRESS + }; + + if (qemu_in_coroutine()) { + qcow2_nb_allocated_clusters_co_entry(&nbco); + } else { + Coroutine *co =3D + qemu_coroutine_create(qcow2_nb_allocated_clusters_co_entry, &n= bco); + qemu_coroutine_enter(co); + BDRV_POLL_WHILE(bs, nbco.ret =3D=3D -EINPROGRESS); + } + + return nbco.ret; +} diff --git a/block/qcow2.c b/block/qcow2.c index a8d61f0981..9a64b89ba2 100644 --- a/block/qcow2.c +++ b/block/qcow2.c @@ -2927,6 +2927,8 @@ static ImageInfoSpecific *qcow2_get_specific_info(Blo= ckDriverState *bs) .refcount_bits =3D s->refcount_bits, }; } else if (s->qcow_version =3D=3D 3) { + int64_t nb_allocated =3D qcow2_nb_allocated_clusters(bs); + *spec_info->u.qcow2.data =3D (ImageInfoSpecificQCow2){ .compat =3D g_strdup("1.1"), .lazy_refcounts =3D s->compatible_features & @@ -2936,6 +2938,8 @@ static ImageInfoSpecific *qcow2_get_specific_info(Blo= ckDriverState *bs) QCOW2_INCOMPAT_CORRUPT, .has_corrupt =3D true, .refcount_bits =3D s->refcount_bits, + .allocated_size =3D MAX(0, nb_allocated) << s->cluster_bit= s, + .has_allocated_size =3D nb_allocated >=3D 0, }; } else { /* if this assertion fails, this probably means a new version was diff --git a/block/qcow2.h b/block/qcow2.h index 1801dc30dc..a0f0bf9358 100644 --- a/block/qcow2.h +++ b/block/qcow2.h @@ -532,6 +532,8 @@ int qcow2_change_refcount_order(BlockDriverState *bs, i= nt refcount_order, BlockDriverAmendStatusCB *status_cb, void *cb_opaque, Error **errp); =20 +int64_t qcow2_nb_allocated_clusters(BlockDriverState *bs); + /* qcow2-cluster.c functions */ int qcow2_grow_l1_table(BlockDriverState *bs, uint64_t min_size, bool exact_size); diff --git a/qapi/block-core.json b/qapi/block-core.json index 6b974b952f..ddaffff830 100644 --- a/qapi/block-core.json +++ b/qapi/block-core.json @@ -44,6 +44,9 @@ # # @refcount-bits: width of a refcount entry in bits (since 2.3) # +# @allocated-size: number of allocated clusters (including metadata) +# if available; only valid for compat >=3D 1.1 (since v2.= 10) +# # Since: 1.7 ## { 'struct': 'ImageInfoSpecificQCow2', @@ -51,7 +54,8 @@ 'compat': 'str', '*lazy-refcounts': 'bool', '*corrupt': 'bool', - 'refcount-bits': 'int' + 'refcount-bits': 'int', + '*allocated-size': 'uint64' } } =20 ## --=20 2.11.1