Patchwork [RFC,V2,5/7] qcow2: Add qcow2_dedup_control.

login
register
mail settings
Submitter Benoît Canet
Date Feb. 6, 2013, 12:32 p.m.
Message ID <1360153946-9617-6-git-send-email-benoit@irqsave.net>
Download mbox | patch
Permalink /patch/218639/
State New
Headers show

Comments

Benoît Canet - Feb. 6, 2013, 12:32 p.m.
---
 block/qcow2-dedup.c |   31 +++++++++++++++++++++++++++++++
 block/qcow2.c       |    2 ++
 block/qcow2.h       |    1 +
 3 files changed, 34 insertions(+)
Eric Blake - Feb. 7, 2013, 4:06 p.m.
On 02/06/2013 05:32 AM, Benoît Canet wrote:
> ---
>  block/qcow2-dedup.c |   31 +++++++++++++++++++++++++++++++
>  block/qcow2.c       |    2 ++
>  block/qcow2.h       |    1 +
>  3 files changed, 34 insertions(+)
> 
> diff --git a/block/qcow2-dedup.c b/block/qcow2-dedup.c
> index bbbb1ac..66fb415 100644
> --- a/block/qcow2-dedup.c
> +++ b/block/qcow2-dedup.c
> @@ -1278,15 +1278,20 @@ static void qcow2_deduplicate_after_resuming(BlockDriverState *bs)
>      BDRVQcowState *s = bs->opaque;
>      uint64_t i;
>      bool processed;
> +    bool exit;

Naming local variables that shadow global functions is risky.  I don't
know if qemu has a policy on being clean under -Wshadow, but this would
fail such a policy.

Patch

diff --git a/block/qcow2-dedup.c b/block/qcow2-dedup.c
index bbbb1ac..66fb415 100644
--- a/block/qcow2-dedup.c
+++ b/block/qcow2-dedup.c
@@ -1278,15 +1278,20 @@  static void qcow2_deduplicate_after_resuming(BlockDriverState *bs)
     BDRVQcowState *s = bs->opaque;
     uint64_t i;
     bool processed;
+    bool exit;
 
     /* for each l2 entry */
     for (i = 0; i < s->l2_size * s->l1_size; i++) {
         qemu_co_mutex_lock(&s->lock);
         processed = qcow2_process_to_dedup_cluster(bs, i);
+        exit = s->dedup_status == DEDUP_STATUS_STOPPING;
         qemu_co_mutex_unlock(&s->lock);
         if (processed || !(i % s->l2_size)) {
             co_sleep_ns(rt_clock, s->dedup_co_delay);
         }
+        if (exit) {
+            return;
+        }
     }
 }
 
@@ -1448,3 +1453,29 @@  void qcow2_dedup_update_metrics(BlockDriverState *bs)
     s->dedup_metrics.ram_usage = nb_hashs * sizeof(GTreeNode_Copy) * 2;
     s->dedup_metrics.ram_usage += nb_hashs * sizeof(QCowHashNode);
 }
+
+void qcow2_dedup_control(BlockDriverState *bs, const bool enable)
+{
+    BDRVQcowState *s = bs->opaque;
+
+    /* must stop and not STARTED -> pass */
+    if (!enable && s->dedup_status != DEDUP_STATUS_STARTED) {
+        return;
+    }
+
+    /* must start and not stopped -> pass */
+    if (enable && s->dedup_status != DEDUP_STATUS_STOPPED) {
+        return;
+    }
+
+    if (enable) {
+        s->dedup_status = DEDUP_STATUS_STARTING;
+        s->dedup_resume_co = qemu_coroutine_create(qcow2_co_dedup_resume);
+        qemu_coroutine_enter(s->dedup_resume_co, bs);
+    } else {
+        s->dedup_status = DEDUP_STATUS_STOPPING;
+        /* must handle half processed write requests */
+        qcow2_dedup_reset(bs);
+        s->dedup_status = DEDUP_STATUS_STOPPED;
+    }
+}
diff --git a/block/qcow2.c b/block/qcow2.c
index 0ee5f4b..3549e87 100644
--- a/block/qcow2.c
+++ b/block/qcow2.c
@@ -2052,6 +2052,8 @@  static BlockDriver bdrv_qcow2 = {
 
     .bdrv_invalidate_cache      = qcow2_invalidate_cache,
 
+    .bdrv_dedup_control         = qcow2_dedup_control,
+
     .create_options = qcow2_create_options,
     .bdrv_check = qcow2_check,
 };
diff --git a/block/qcow2.h b/block/qcow2.h
index 191b272..316b0d3 100644
--- a/block/qcow2.h
+++ b/block/qcow2.h
@@ -525,5 +525,6 @@  bool qcow2_dedup_is_running(BlockDriverState *bs);
 int qcow2_dedup_init(BlockDriverState *bs);
 void qcow2_dedup_close(BlockDriverState *bs);
 void qcow2_dedup_update_metrics(BlockDriverState *bs);
+void qcow2_dedup_control(BlockDriverState *bs, const bool enable);
 
 #endif