qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
To: qemu-devel@nongnu.org, qemu-block@nongnu.org
Cc: eblake@redhat.com, armbru@redhat.com, mreitz@redhat.com,
	kwolf@redhat.com, vsementsov@virtuozzo.com, den@openvz.org
Subject: [Qemu-devel] [PATCH 3/7] qcow2: add .bdrv_get_format_allocated_size
Date: Thu, 25 May 2017 18:26:24 +0300	[thread overview]
Message-ID: <20170525152628.37628-4-vsementsov@virtuozzo.com> (raw)
In-Reply-To: <20170525152628.37628-1-vsementsov@virtuozzo.com>

Realize .bdrv_get_format_allocated_size interface.

Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
---
 block/qcow2-refcount.c | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++
 block/qcow2.c          |  2 ++
 block/qcow2.h          |  2 ++
 3 files changed, 65 insertions(+)

diff --git a/block/qcow2-refcount.c b/block/qcow2-refcount.c
index 7c06061aae..45adf6bd1d 100644
--- a/block/qcow2-refcount.c
+++ b/block/qcow2-refcount.c
@@ -2931,3 +2931,64 @@ done:
     qemu_vfree(new_refblock);
     return ret;
 }
+
+typedef struct NbAllocatedClustersCo {
+    BlockDriverState *bs;
+    int64_t ret;
+} NbAllocatedClustersCo;
+
+static void coroutine_fn qcow2_get_format_allocated_size_co_entry(void *opaque)
+{
+    NbAllocatedClustersCo *nbco = opaque;
+    BlockDriverState *bs = nbco->bs;
+    BDRVQcow2State *s = bs->opaque;
+    int64_t size, nb_clusters, nb_allocated = 0, i;
+    int ret = 0;
+
+    size = bdrv_getlength(bs->file->bs);
+    if (size < 0) {
+        nbco->ret = size;
+        return;
+    }
+
+    nb_clusters = size_to_clusters(s, size);
+
+    qemu_co_mutex_lock(&s->lock);
+
+    for (i = 0; i < nb_clusters; ++i) {
+        uint64_t refcount;
+        ret = qcow2_get_refcount(bs, i, &refcount);
+        if (ret < 0) {
+            nbco->ret = ret;
+            qemu_co_mutex_unlock(&s->lock);
+            return;
+        }
+        if (refcount > 0) {
+            nb_allocated++;
+        }
+    }
+
+    qemu_co_mutex_unlock(&s->lock);
+
+    nbco->ret = nb_allocated << s->cluster_bits;
+}
+
+int64_t qcow2_get_format_allocated_size(BlockDriverState *bs)
+{
+    NbAllocatedClustersCo nbco = {
+        .bs = bs,
+        .ret = -EINPROGRESS
+    };
+
+    if (qemu_in_coroutine()) {
+        qcow2_get_format_allocated_size_co_entry(&nbco);
+    } else {
+        Coroutine *co =
+            qemu_coroutine_create(qcow2_get_format_allocated_size_co_entry,
+                                  &nbco);
+        qemu_coroutine_enter(co);
+        BDRV_POLL_WHILE(bs, nbco.ret == -EINPROGRESS);
+    }
+
+    return nbco.ret;
+}
diff --git a/block/qcow2.c b/block/qcow2.c
index a8d61f0981..274f213d16 100644
--- a/block/qcow2.c
+++ b/block/qcow2.c
@@ -3469,6 +3469,8 @@ BlockDriver bdrv_qcow2 = {
 
     .bdrv_detach_aio_context  = qcow2_detach_aio_context,
     .bdrv_attach_aio_context  = qcow2_attach_aio_context,
+
+    .bdrv_get_format_allocated_size = qcow2_get_format_allocated_size,
 };
 
 static void bdrv_qcow2_init(void)
diff --git a/block/qcow2.h b/block/qcow2.h
index 1801dc30dc..8cc63d9e0e 100644
--- a/block/qcow2.h
+++ b/block/qcow2.h
@@ -532,6 +532,8 @@ int qcow2_change_refcount_order(BlockDriverState *bs, int refcount_order,
                                 BlockDriverAmendStatusCB *status_cb,
                                 void *cb_opaque, Error **errp);
 
+int64_t qcow2_get_format_allocated_size(BlockDriverState *bs);
+
 /* qcow2-cluster.c functions */
 int qcow2_grow_l1_table(BlockDriverState *bs, uint64_t min_size,
                         bool exact_size);
-- 
2.11.1

  parent reply	other threads:[~2017-05-25 15:26 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-05-25 15:26 [Qemu-devel] [PATCH 0/7] qemu-img check: unallocated size Vladimir Sementsov-Ogievskiy
2017-05-25 15:26 ` [Qemu-devel] [PATCH 1/7] block: fix comment for bdrv_get_allocated_file_size() Vladimir Sementsov-Ogievskiy
2017-05-25 16:32   ` Eric Blake
2017-05-25 16:34     ` Eric Blake
2017-05-25 17:03     ` Vladimir Sementsov-Ogievskiy
2017-05-25 17:46       ` Eric Blake
2017-05-25 17:56         ` Vladimir Sementsov-Ogievskiy
2017-05-25 15:26 ` [Qemu-devel] [PATCH 2/7] block: add bdrv_get_format_allocated_size format interface Vladimir Sementsov-Ogievskiy
2017-05-25 17:54   ` Eric Blake
2017-05-25 18:07     ` Vladimir Sementsov-Ogievskiy
2017-05-25 18:19       ` Vladimir Sementsov-Ogievskiy
2017-05-25 19:02         ` Eric Blake
2017-05-25 15:26 ` Vladimir Sementsov-Ogievskiy [this message]
2017-05-25 15:26 ` [Qemu-devel] [PATCH 4/7] common: make get_human_readable_size public Vladimir Sementsov-Ogievskiy
2017-05-25 17:57   ` Eric Blake
2017-05-25 15:26 ` [Qemu-devel] [PATCH 5/7] qemu-img check: add format unallocated size Vladimir Sementsov-Ogievskiy
2017-05-25 17:59   ` Eric Blake
2017-05-25 15:26 ` [Qemu-devel] [PATCH 6/7] qemu-img check: add file-size Vladimir Sementsov-Ogievskiy
2017-05-25 17:59   ` Eric Blake
2017-05-25 15:26 ` [Qemu-devel] [PATCH 7/7] block: rename _get_allocated_file_size() to _get_fs_allocated_size() Vladimir Sementsov-Ogievskiy
2017-05-25 15:34   ` Vladimir Sementsov-Ogievskiy
2017-05-25 18:01   ` Eric Blake

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20170525152628.37628-4-vsementsov@virtuozzo.com \
    --to=vsementsov@virtuozzo.com \
    --cc=armbru@redhat.com \
    --cc=den@openvz.org \
    --cc=eblake@redhat.com \
    --cc=kwolf@redhat.com \
    --cc=mreitz@redhat.com \
    --cc=qemu-block@nongnu.org \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).