Skip to contentSkip to conversation

block: Fix non-shared storage migration

PATCH In review Fam Zheng 4 participants 14 messages Aug 23, 2017 Block layer

lore
Share & tools
lore

Fam Zheng

author
#0 lore raw

cover block: Fix non-shared storage migration

This fixes the issue reported as https://bugs.launchpad.net/bugs/1711602

Fam Zheng (3):
block-backend: Refactor inactivate check
block-backend: Allow more "can inactivate" cases
mirror: Mark target BB as "force allow inactivate"

Stefan Hajnoczi (1):
block: Update open_flags after ->inactivate() callback

 block.c                        |  7 +++----
 block/block-backend.c          | 30 +++++++++++++++++++++++++-----
 block/mirror.c                 | 14 ++++++++++++--
 include/sysemu/block-backend.h |  1 +
 4 files changed, 41 insertions(+), 11 deletions(-)
Signature · 1 line
2.13.5
author
#1 lore raw

PATCH 1/4 block-backend: Refactor inactivate check

The logic will be fixed (extended), move it to a separete function.

---
 block/block-backend.c | 19 ++++++++++++++-----
 1 file changed, 14 insertions(+), 5 deletions(-)
block/block-backend.c +14 -5
index e9798e897d..a3984d2bec 100644
@@ -192,6 +192,19 @@ static void blk_root_activate(BdrvChild *child, Error **errp)
     }
 }
 
+static bool blk_can_inactivate(BlockBackend *blk)
+{
+    /* Only inactivate BlockBackends for guest devices (which are inactive at
+     * this point because the VM is stopped) and unattached monitor-owned
+     * BlockBackends. If there is still any other user like a block job, then
+     * we simply can't inactivate the image. */
+    if (blk->dev || blk_name(blk)[0]) {
+        return true;
+    }
+
+    return false;
+}
+
 static int blk_root_inactivate(BdrvChild *child)
 {
     BlockBackend *blk = child->opaque;
@@ -200,11 +213,7 @@ static int blk_root_inactivate(BdrvChild *child)
         return 0;
     }
 
-    /* Only inactivate BlockBackends for guest devices (which are inactive at
-     * this point because the VM is stopped) and unattached monitor-owned
-     * BlockBackends. If there is still any other user like a block job, then
-     * we simply can't inactivate the image. */
-    if (!blk->dev && !blk_name(blk)[0]) {
+    if (!blk_can_inactivate(blk)) {
         return -EPERM;
     }
 
Signature · 1 line
2.13.5
#2 lore raw

Re: [Qemu-devel] [PATCH for-2.10 1/4] block-backend: Refactor inactivate check

Quoted 1 line · Fam Zheng

The logic will be fixed (extended), move it to a separete function.

s/separete/separate/

[can fix on pull request]

Quoted 5 lines
---
 block/block-backend.c | 19 ++++++++++++++-----
 1 file changed, 14 insertions(+), 5 deletions(-)
Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
author
#3 lore raw

PATCH 2/4 block-backend: Allow more "can inactivate" cases

In reply to Fam Zheng

These two conditions corresponds to mirror job's source and target, which need to be allowed as they are part of the non-shared storage migration workflow: failing to inactivate either will result in a failure during migration completion.

---
 block/block-backend.c          | 21 ++++++++++++++++-----
 include/sysemu/block-backend.h |  1 +
 2 files changed, 17 insertions(+), 5 deletions(-)
block/block-backend.c +16 -5
index a3984d2bec..48f5356657 100644
@@ -70,6 +70,7 @@ struct BlockBackend {
 
     int quiesce_counter;
     VMChangeStateEntry *vmsh;
+    bool force_allow_inactivate;
 };
 
 typedef struct BlockBackendAIOCB {
@@ -192,17 +193,27 @@ static void blk_root_activate(BdrvChild *child, Error **errp)
     }
 }
 
+void blk_set_force_allow_inactivate(BlockBackend *blk)
+{
+    blk->force_allow_inactivate = true;
+}
+
 static bool blk_can_inactivate(BlockBackend *blk)
 {
-    /* Only inactivate BlockBackends for guest devices (which are inactive at
-     * this point because the VM is stopped) and unattached monitor-owned
-     * BlockBackends. If there is still any other user like a block job, then
-     * we simply can't inactivate the image. */
+    /* If it is a guest device, inactivate is ok. */
     if (blk->dev || blk_name(blk)[0]) {
         return true;
     }
 
-    return false;
+    /* Inactivating means no more write to the image can be done, even if it's
+     * guest invisible change. For block job BBs that satisfy this, we can just
+     * allow it.  This is the case for mirror job source, which is required by
+     * libvirt non-shared block migration. */
+    if (!(blk->perm & (BLK_PERM_WRITE | BLK_PERM_WRITE_UNCHANGED))) {
+        return true;
+    }
+
+    return blk->force_allow_inactivate;
 }
 
 static int blk_root_inactivate(BdrvChild *child)
include/sysemu/block-backend.h +1 -0
index 4a3730596b..aadc733daf 100644
@@ -241,5 +241,6 @@ void blk_set_io_limits(BlockBackend *blk, ThrottleConfig *cfg);
 void blk_io_limits_disable(BlockBackend *blk);
 void blk_io_limits_enable(BlockBackend *blk, const char *group);
 void blk_io_limits_update_group(BlockBackend *blk, const char *group);
+void blk_set_force_allow_inactivate(BlockBackend *blk);
 
 #endif
Signature · 1 line
2.13.5
#4 lore raw

Re: [Qemu-devel] [PATCH for-2.10 2/4] block-backend: Allow more "can inactivate" cases

Quoted 1 line · Fam Zheng

These two conditions corresponds to mirror job's source and target,

s/corresponds to/correspond to a/

[can touch up on pull request]

block/block-backend.c static void blk_root_activate(BdrvChild *child, Error **errp) · line 208

Quoted 13 lines

which need to be allowed as they are part of the non-shared storage migration workflow: failing to inactivate either will result in a failure during migration completion.

---
 block/block-backend.c          | 21 ++++++++++++++++-----
 include/sysemu/block-backend.h |  1 +
 2 files changed, 17 insertions(+), 5 deletions(-)
block/block-backend.c +1 -1
-    return false;
+    /* Inactivating means no more write to the image can be done, even if it's

s/write/writes/

Quoted 1 line

+ * guest invisible change. For block job BBs that satisfy this, we can just

reads awkwardly. Maybe 'even if it's changes invisible to the guest'? But I can leave your wording if I don't get confirmation.

Quoted 8 lines
block/block-backend.c +7 -0
+     * allow it.  This is the case for mirror job source, which is required by
+     * libvirt non-shared block migration. */
+    if (!(blk->perm & (BLK_PERM_WRITE | BLK_PERM_WRITE_UNCHANGED))) {
+        return true;
+    }
+
+    return blk->force_allow_inactivate;
 }
Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
author
#5 lore raw

Re: [Qemu-devel] [PATCH for-2.10 2/4] block-backend: Allow more "can inactivate" cases

Quoted 21 lines · Eric Blake
Quoted 1 line · Fam Zheng

These two conditions corresponds to mirror job's source and target,

s/corresponds to/correspond to a/

[can touch up on pull request]

block/block-backend.c static void blk_root_activate(BdrvChild *child, Error **errp) · line 208

Quoted 13 lines

which need to be allowed as they are part of the non-shared storage migration workflow: failing to inactivate either will result in a failure during migration completion.

---
 block/block-backend.c          | 21 ++++++++++++++++-----
 include/sysemu/block-backend.h |  1 +
 2 files changed, 17 insertions(+), 5 deletions(-)
block/block-backend.c +1 -1
-    return false;
+    /* Inactivating means no more write to the image can be done, even if it's
Quoted 6 lines

s/write/writes/

Quoted 1 line

+ * guest invisible change. For block job BBs that satisfy this, we can just

reads awkwardly. Maybe 'even if it's changes invisible to the guest'? But I can leave your wording if I don't get confirmation.

Not sure I understand the grammar of "it's changes" but yours must be better. Feel free to update it. Thanks,

Fam

Quoted 15 lines
Quoted 9 lines
block/block-backend.c +7 -0
+     * allow it.  This is the case for mirror job source, which is required by
+     * libvirt non-shared block migration. */
+    if (!(blk->perm & (BLK_PERM_WRITE | BLK_PERM_WRITE_UNCHANGED))) {
+        return true;
+    }
+
+    return blk->force_allow_inactivate;
 }
Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
#6 lore raw

Re: [Qemu-devel] [PATCH for-2.10 2/4] block-backend: Allow more "can inactivate" cases

Quoted 24 lines · Fam Zheng
Quoted 23 lines · Eric Blake
Quoted 1 line · Fam Zheng

These two conditions corresponds to mirror job's source and target,

s/corresponds to/correspond to a/

[can touch up on pull request]

block/block-backend.c static void blk_root_activate(BdrvChild *child, Error **errp) · line 208

Quoted 13 lines

which need to be allowed as they are part of the non-shared storage migration workflow: failing to inactivate either will result in a failure during migration completion.

---
 block/block-backend.c          | 21 ++++++++++++++++-----
 include/sysemu/block-backend.h |  1 +
 2 files changed, 17 insertions(+), 5 deletions(-)
block/block-backend.c +1 -1
-    return false;
+    /* Inactivating means no more write to the image can be done, even if it's

s/write/writes/

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

+ * guest invisible change. For block job BBs that satisfy this, we can just

reads awkwardly. Maybe 'even if it's changes invisible to the guest'? But I can leave your wording if I don't get confirmation.

Not sure I understand the grammar of "it's changes" but yours must be better. Feel free to update it. Thanks,

If it's confusing, we can still do better. I settled on:

no more writes to the image can be done, even if those writes would be changes invisible to the guest.

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
author
#7 lore raw

PATCH 3/4 mirror: Mark target BB as "force allow inactivate"

In reply to Fam Zheng
---
 block/mirror.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)
block/mirror.c +12 -2
index c9a6a3ca86..429751b9fe 100644
@@ -1134,6 +1134,7 @@ static void mirror_start_job(const char *job_id, BlockDriverState *bs,
                              const BlockJobDriver *driver,
                              bool is_none_mode, BlockDriverState *base,
                              bool auto_complete, const char *filter_node_name,
+                             bool is_mirror,
                              Error **errp)
 {
     MirrorBlockJob *s;
@@ -1222,6 +1223,15 @@ static void mirror_start_job(const char *job_id, BlockDriverState *bs,
     if (ret < 0) {
         goto fail;
     }
+    if (is_mirror) {
+        /* XXX: Mirror target could be a NBD server of target QEMU in the case
+         * of non-shared block migration. To allow migration completion, we
+         * have to allow "inactivate" of the target BB.  When that happens, we
+         * know the job is drained, and the vcpus are stopped, so no write
+         * operation will be performed. Block layer already has assertions to
+         * ensure that. */
+        blk_set_force_allow_inactivate(s->target);
+    }
 
     s->replaces = g_strdup(replaces);
     s->on_source_error = on_source_error;
@@ -1306,7 +1316,7 @@ void mirror_start(const char *job_id, BlockDriverState *bs,
                      speed, granularity, buf_size, backing_mode,
                      on_source_error, on_target_error, unmap, NULL, NULL,
                      &mirror_job_driver, is_none_mode, base, false,
-                     filter_node_name, errp);
+                     filter_node_name, true, errp);
 }
 
 void commit_active_start(const char *job_id, BlockDriverState *bs,
@@ -1329,7 +1339,7 @@ void commit_active_start(const char *job_id, BlockDriverState *bs,
                      MIRROR_LEAVE_BACKING_CHAIN,
                      on_error, on_error, true, cb, opaque,
                      &commit_active_job_driver, false, base, auto_complete,
-                     filter_node_name, &local_err);
+                     filter_node_name, false, &local_err);
     if (local_err) {
         error_propagate(errp, local_err);
         goto error_restore_flags;
Signature · 1 line
2.13.5
author
#8 lore raw

PATCH 4/4 block: Update open_flags after ->inactivate() callback

In reply to Fam Zheng

From: Stefan Hajnoczi <[email protected]>

In the ->inactivate() callbacks, permissions are updated, which typically involves a recursive check of the whole graph. Setting BDRV_O_INACTIVE right before doing that creates a state that bdrv_is_writable() returns false, which causes permission update failure.

Reorder them so the flag is updated after calling the function. Note that this doesn't break the assert in bdrv_child_cb_inactivate() because for any specific BDS, we still update its flags first before calling ->inactivate() on it one level deeper in the recursion.

---
 block.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
block.c +3 -4
index 3615a6809e..3308814bba 100644
@@ -4085,21 +4085,20 @@ static int bdrv_inactivate_recurse(BlockDriverState *bs,
         }
     }
 
-    if (setting_flag) {
+    if (setting_flag && !(bs->open_flags & BDRV_O_INACTIVE)) {
         uint64_t perm, shared_perm;
 
-        bs->open_flags |= BDRV_O_INACTIVE;
-
         QLIST_FOREACH(parent, &bs->parents, next_parent) {
             if (parent->role->inactivate) {
                 ret = parent->role->inactivate(parent);
                 if (ret < 0) {
-                    bs->open_flags &= ~BDRV_O_INACTIVE;
                     return ret;
                 }
             }
         }
 
+        bs->open_flags |= BDRV_O_INACTIVE;
+
         /* Update permissions, they may differ for inactive nodes */
         bdrv_get_cumulative_perm(bs, &perm, &shared_perm);
         bdrv_check_perm(bs, perm, shared_perm, NULL, &error_abort);
Signature · 1 line
2.13.5
#9 lore raw

Re: [Qemu-devel] [PATCH for-2.10 4/4] block: Update open_flags after ->inactivate() callback

Quoted 8 lines · Fam Zheng

From: Stefan Hajnoczi <[email protected]>

In the ->inactivate() callbacks, permissions are updated, which typically involves a recursive check of the whole graph. Setting BDRV_O_INACTIVE right before doing that creates a state that bdrv_is_writable() returns false, which causes permission update failure.

Quoted 6 lines

Reorder them so the flag is updated after calling the function. Note that this doesn't break the assert in bdrv_child_cb_inactivate() because for any specific BDS, we still update its flags first before calling ->inactivate() on it one level deeper in the recursion.

Reviewed-by on the v1 series
#10 lore raw
In reply to Fam Zheng
Quoted 7 lines · Fam Zheng

This fixes the issue reported as https://bugs.launchpad.net/bugs/1711602

Fam Zheng (3):
block-backend: Refactor inactivate check
block-backend: Allow more "can inactivate" cases
mirror: Mark target BB as "force allow inactivate"

Quoted 12 lines

Stefan Hajnoczi (1):
block: Update open_flags after ->inactivate() callback

 block.c                        |  7 +++----
 block/block-backend.c          | 30 +++++++++++++++++++++++++-----
 block/mirror.c                 | 14 ++++++++++++--
 include/sysemu/block-backend.h |  1 +
 4 files changed, 41 insertions(+), 11 deletions(-)
Signature · 1 line
2.13.5
#11 lore raw
In reply to Fam Zheng
Quoted 1 line · * Fam Zheng ([email protected]) wrote:

This fixes the issue reported as https://bugs.launchpad.net/bugs/1711602

Yep, that fixes it in my test setup (1 of 1 passes!)

Dave

Quoted 5 lines

Fam Zheng (3):
block-backend: Refactor inactivate check
block-backend: Allow more "can inactivate" cases
mirror: Mark target BB as "force allow inactivate"

Quoted 13 lines

Stefan Hajnoczi (1):
block: Update open_flags after ->inactivate() callback

 block.c                        |  7 +++----
 block/block-backend.c          | 30 +++++++++++++++++++++++++-----
 block/mirror.c                 | 14 ++++++++++++--
 include/sysemu/block-backend.h |  1 +
 4 files changed, 41 insertions(+), 11 deletions(-)
Signature · 1 line
2.13.5

--
Dr. David Alan Gilbert / [email protected] / Manchester, UK

#12 lore raw
Quoted 4 lines · Dr. David Alan Gilbert
Quoted 1 line · * Fam Zheng ([email protected]) wrote:

This fixes the issue reported as https://bugs.launchpad.net/bugs/1711602

Yep, that fixes it in my test setup (1 of 1 passes!)

As mentioned on IRC, I'll treat this as a Tested-by: tag on 4/4 in my pull request. Thanks for helping check things.

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
#13 lore raw
In reply to Fam Zheng
Quoted 9 lines · Fam Zheng

This fixes the issue reported as https://bugs.launchpad.net/bugs/1711602

Fam Zheng (3):
block-backend: Refactor inactivate check
block-backend: Allow more "can inactivate" cases
mirror: Mark target BB as "force allow inactivate"

Stefan Hajnoczi (1):
block: Update open_flags after ->inactivate() callback

Placing on my queue for a 2.10-rc4 pull request

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org