From nobody Wed Nov 5 17:32:56 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.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 1496766552202367.8918250596678; Tue, 6 Jun 2017 09:29:12 -0700 (PDT) Received: from localhost ([::1]:39268 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dIHLx-0001PU-Q9 for importer@patchew.org; Tue, 06 Jun 2017 12:29:09 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:44826) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dIHKK-0008K1-9y for qemu-devel@nongnu.org; Tue, 06 Jun 2017 12:27:29 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dIHKH-0002Rz-3y for qemu-devel@nongnu.org; Tue, 06 Jun 2017 12:27:28 -0400 Received: from mailhub.sw.ru ([195.214.232.25]:35456 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 1dIHKG-0002Ri-LA for qemu-devel@nongnu.org; Tue, 06 Jun 2017 12:27:25 -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 v56GQq4E001574; Tue, 6 Jun 2017 19:26:54 +0300 (MSK) From: Vladimir Sementsov-Ogievskiy To: qemu-devel@nongnu.org, qemu-block@nongnu.org Date: Tue, 6 Jun 2017 19:26:51 +0300 Message-Id: <20170606162652.112122-3-vsementsov@virtuozzo.com> X-Mailer: git-send-email 2.11.1 In-Reply-To: <20170606162652.112122-1-vsementsov@virtuozzo.com> References: <20170606162652.112122-1-vsementsov@virtuozzo.com> X-detected-operating-system: by eggs.gnu.org: OpenBSD 3.x [fuzzy] X-Received-From: 195.214.232.25 Subject: [Qemu-devel] [PATCH v3 2/3] qcow2: add .bdrv_get_format_alloc_stat 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" Realize .bdrv_get_format_alloc_stat interface. Signed-off-by: Vladimir Sementsov-Ogievskiy --- block/qcow2-refcount.c | 152 +++++++++++++++++++++++++++++++++++++++++++++= ++++ block/qcow2.c | 2 + block/qcow2.h | 2 + 3 files changed, 156 insertions(+) diff --git a/block/qcow2-refcount.c b/block/qcow2-refcount.c index 7c06061aae..fd6027f0c2 100644 --- a/block/qcow2-refcount.c +++ b/block/qcow2-refcount.c @@ -2931,3 +2931,155 @@ done: qemu_vfree(new_refblock); return ret; } + +typedef struct FormatAllocStatCo { + BlockDriverState *bs; + BlockFormatAllocInfo *bfai; + int64_t ret; +} FormatAllocStatCo; + +static void coroutine_fn qcow2_get_format_alloc_stat_entry(void *opaque) +{ + int ret =3D 0; + FormatAllocStatCo *nbco =3D opaque; + BlockDriverState *bs =3D nbco->bs; + BDRVQcow2State *s =3D bs->opaque; + BlockFormatAllocInfo *bfai =3D nbco->bfai; + int64_t cluster, file_sectors, sector; + int refcount_block_offset; + uint32_t i; + bool used =3D false, f_data =3D false, f_zero =3D false; + int dif, num =3D 0, f_num =3D 0; + + memset(bfai, 0, sizeof(*bfai)); + + file_sectors =3D bdrv_nb_sectors(bs->file->bs); + if (file_sectors < 0) { + nbco->ret =3D file_sectors; + return; + } + + qemu_co_mutex_lock(&s->lock); + + for (sector =3D 0; sector < file_sectors; sector +=3D dif) { + if (f_num =3D=3D 0) { + BlockDriverState *file; + ret =3D bdrv_get_block_status(bs->file->bs, sector, + file_sectors - sector, &f_num, &fi= le); + if (ret < 0) { + goto fail; + } + f_data =3D ret & BDRV_BLOCK_DATA; + f_zero =3D ret & BDRV_BLOCK_ZERO; + } + + if (num =3D=3D 0) { + uint64_t refcount; + assert(((sector << BDRV_SECTOR_BITS) & (s->cluster_size - 1)) = =3D=3D 0); + ret =3D qcow2_get_refcount( + bs, (sector << BDRV_SECTOR_BITS) >> s->cluster_bits, &refc= ount); + if (ret < 0) { + goto fail; + } + used =3D refcount > 0; + num =3D s->cluster_size >> BDRV_SECTOR_BITS; + } + + dif =3D MIN(f_num, MIN(num, file_sectors - sector)); + if (used) { + if (f_data) { + bfai->used_data +=3D dif; + } else if (f_zero) { + bfai->used_zero +=3D dif; + } else { + bfai->used_discarded +=3D dif; + } + } else { + if (f_data) { + bfai->unused_data +=3D dif; + } else if (f_zero) { + bfai->unused_zero +=3D dif; + } else { + bfai->unused_discarded +=3D dif; + } + } + f_num -=3D dif; + num -=3D dif; + } + + assert(f_num =3D=3D 0); + + if (used) { + bfai->used_overrun +=3D num; + } + + cluster =3D size_to_clusters(s, sector << BDRV_SECTOR_BITS); + refcount_block_offset =3D cluster & (s->refcount_block_size - 1); + for (i =3D cluster >> s->refcount_block_bits; + i <=3D s->max_refcount_table_index; i++) + { + int j; + + if (!(s->refcount_table[i] & REFT_OFFSET_MASK)) { + refcount_block_offset =3D 0; + continue; + } + + for (j =3D refcount_block_offset; j < s->refcount_block_size; j++)= { + uint64_t refcount; + cluster =3D (i << s->refcount_block_bits) + j; + + ret =3D qcow2_get_refcount(bs, cluster, &refcount); + if (ret < 0) { + goto fail; + } + if (refcount > 0) { + bfai->used_overrun++; + } + } + + refcount_block_offset =3D 0; + } + + qemu_co_mutex_unlock(&s->lock); + + bfai->used_data =3D bfai->used_data << BDRV_SECTOR_BITS; + bfai->used_zero =3D bfai->used_zero << BDRV_SECTOR_BITS; + bfai->used_discarded =3D bfai->used_discarded << BDRV_SECTOR_BITS; + bfai->used_overrun =3D bfai->used_overrun << BDRV_SECTOR_BITS; + + bfai->unused_data =3D bfai->unused_data << BDRV_SECTOR_BITS; + bfai->unused_zero =3D bfai->unused_zero << BDRV_SECTOR_BITS; + bfai->unused_discarded =3D bfai->unused_discarded << BDRV_SECTOR_BITS; + + nbco->ret =3D 0; + return; + +fail: + nbco->ret =3D ret; + qemu_co_mutex_unlock(&s->lock); +} + +/* qcow2_get_format_alloc_stat() + * Fills @bfai struct. In case of failure @bfai content is unpredicted. + */ +int qcow2_get_format_alloc_stat(BlockDriverState *bs, + BlockFormatAllocInfo *bfai) +{ + FormatAllocStatCo nbco =3D { + .bs =3D bs, + .bfai =3D bfai, + .ret =3D -EINPROGRESS + }; + + if (qemu_in_coroutine()) { + qcow2_get_format_alloc_stat_entry(&nbco); + } else { + Coroutine *co =3D + qemu_coroutine_create(qcow2_get_format_alloc_stat_entry, &nbco= ); + 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..0e26b548fd 100644 --- a/block/qcow2.c +++ b/block/qcow2.c @@ -3469,6 +3469,8 @@ BlockDriver bdrv_qcow2 =3D { =20 .bdrv_detach_aio_context =3D qcow2_detach_aio_context, .bdrv_attach_aio_context =3D qcow2_attach_aio_context, + + .bdrv_get_format_alloc_stat =3D qcow2_get_format_alloc_stat, }; =20 static void bdrv_qcow2_init(void) diff --git a/block/qcow2.h b/block/qcow2.h index 1801dc30dc..16d31a8624 100644 --- a/block/qcow2.h +++ b/block/qcow2.h @@ -598,4 +598,6 @@ int qcow2_cache_get_empty(BlockDriverState *bs, Qcow2Ca= che *c, uint64_t offset, void **table); void qcow2_cache_put(BlockDriverState *bs, Qcow2Cache *c, void **table); =20 +int qcow2_get_format_alloc_stat(BlockDriverState *bs, + BlockFormatAllocInfo *bfai); #endif --=20 2.11.1