diff mbox

[v3,2/6] replication: add shared-disk and shared-disk-id options

Message ID 1484884080-28836-3-git-send-email-zhang.zhanghailiang@huawei.com
State New
Headers show

Commit Message

Zhanghailiang Jan. 20, 2017, 3:47 a.m. UTC
We use these two options to identify which disk is
shared

Cc: Eric Blake <eblake@redhat.com>
Signed-off-by: zhanghailiang <zhang.zhanghailiang@huawei.com>
Signed-off-by: Wen Congyang <wency@cn.fujitsu.com>
Signed-off-by: Zhang Chen <zhangchen.fnst@cn.fujitsu.com>
---
v3:
- Move g_free(s->shared_disk_id) to the common fail process place (Stefan)
- Fix comments for these two options
---
 block/replication.c  | 37 +++++++++++++++++++++++++++++++++++++
 qapi/block-core.json | 10 +++++++++-
 2 files changed, 46 insertions(+), 1 deletion(-)

Comments

Stefan Hajnoczi Feb. 27, 2017, 5:10 p.m. UTC | #1
On Fri, Jan 20, 2017 at 11:47:56AM +0800, zhanghailiang wrote:
> @@ -119,12 +136,31 @@ static int replication_open(BlockDriverState *bs, QDict *options,
>                     "The option mode's value should be primary or secondary");
>          goto fail;
>      }
> +    s->is_shared_disk = qemu_opt_get_bool(opts, REPLICATION_SHARED_DISK,
> +                                          false);
> +    if (s->is_shared_disk && (s->mode == REPLICATION_MODE_PRIMARY)) {
> +        shared_disk_id = qemu_opt_get(opts, REPLICATION_SHARED_DISK_ID);
> +        if (!shared_disk_id) {
> +            error_setg(&local_err, "Missing shared disk blk option");
> +            goto fail;
> +        }
> +        s->shared_disk_id = g_strdup(shared_disk_id);
> +        blk = blk_by_name(s->shared_disk_id);
> +        if (!blk) {
> +            error_setg(&local_err, "There is no %s block", s->shared_disk_id);
> +            goto fail;
> +        }
> +        /* We can't access root member of BlockBackend directly */
> +        tmp_bs = blk_bs(blk);
> +        s->primary_disk = QLIST_FIRST(&tmp_bs->parents);

Why is this necessary?

We already have the BlockBackend for the primary disk.  I'm not sure why
the BdrvChild is needed.

Stefan
Zhanghailiang March 1, 2017, 3:53 a.m. UTC | #2
On 2017/2/28 1:10, Stefan Hajnoczi wrote:
> On Fri, Jan 20, 2017 at 11:47:56AM +0800, zhanghailiang wrote:
>> @@ -119,12 +136,31 @@ static int replication_open(BlockDriverState *bs, QDict *options,
>>                      "The option mode's value should be primary or secondary");
>>           goto fail;
>>       }
>> +    s->is_shared_disk = qemu_opt_get_bool(opts, REPLICATION_SHARED_DISK,
>> +                                          false);
>> +    if (s->is_shared_disk && (s->mode == REPLICATION_MODE_PRIMARY)) {
>> +        shared_disk_id = qemu_opt_get(opts, REPLICATION_SHARED_DISK_ID);
>> +        if (!shared_disk_id) {
>> +            error_setg(&local_err, "Missing shared disk blk option");
>> +            goto fail;
>> +        }
>> +        s->shared_disk_id = g_strdup(shared_disk_id);
>> +        blk = blk_by_name(s->shared_disk_id);
>> +        if (!blk) {
>> +            error_setg(&local_err, "There is no %s block", s->shared_disk_id);
>> +            goto fail;
>> +        }
>> +        /* We can't access root member of BlockBackend directly */
>> +        tmp_bs = blk_bs(blk);
>> +        s->primary_disk = QLIST_FIRST(&tmp_bs->parents);
>
> Why is this necessary?
>
> We already have the BlockBackend for the primary disk.  I'm not sure why
> the BdrvChild is needed.
>

Er, the helper backup_job_create() needs the BlockDriverState for the primary disk,
besides, we want to make it same with 'secondary_disk' in struct BDRVReplicationState.

Thanks.
Hailiang

> Stefan
>
Stefan Hajnoczi March 1, 2017, 10:21 a.m. UTC | #3
On Wed, Mar 01, 2017 at 11:53:14AM +0800, Hailiang Zhang wrote:
> On 2017/2/28 1:10, Stefan Hajnoczi wrote:
> > On Fri, Jan 20, 2017 at 11:47:56AM +0800, zhanghailiang wrote:
> > > @@ -119,12 +136,31 @@ static int replication_open(BlockDriverState *bs, QDict *options,
> > >                      "The option mode's value should be primary or secondary");
> > >           goto fail;
> > >       }
> > > +    s->is_shared_disk = qemu_opt_get_bool(opts, REPLICATION_SHARED_DISK,
> > > +                                          false);
> > > +    if (s->is_shared_disk && (s->mode == REPLICATION_MODE_PRIMARY)) {
> > > +        shared_disk_id = qemu_opt_get(opts, REPLICATION_SHARED_DISK_ID);
> > > +        if (!shared_disk_id) {
> > > +            error_setg(&local_err, "Missing shared disk blk option");
> > > +            goto fail;
> > > +        }
> > > +        s->shared_disk_id = g_strdup(shared_disk_id);
> > > +        blk = blk_by_name(s->shared_disk_id);
> > > +        if (!blk) {
> > > +            error_setg(&local_err, "There is no %s block", s->shared_disk_id);
> > > +            goto fail;
> > > +        }
> > > +        /* We can't access root member of BlockBackend directly */
> > > +        tmp_bs = blk_bs(blk);
> > > +        s->primary_disk = QLIST_FIRST(&tmp_bs->parents);
> > 
> > Why is this necessary?
> > 
> > We already have the BlockBackend for the primary disk.  I'm not sure why
> > the BdrvChild is needed.
> > 
> 
> Er, the helper backup_job_create() needs the BlockDriverState for the primary disk,

You can get the BDS any time using blk_bs().

> besides, we want to make it same with 'secondary_disk' in struct BDRVReplicationState.

Block drivers are allowed to access BDS->parents so it's not necessarily
a problem that the code does this.  But it's rare and usually means
there is a non-standard relationship between BDS nodes.

It wasn't obvious from the code why you are going through the trouble of
getting a BdrvChild instead of using the BlockBackend (the normal way of
accessing a drive).

Perhaps a comment would help:

/* We have a BlockBackend for the primary disk but use BdrvChild for
 * consistency - active_disk, secondary_disk, etc are also BdrvChild.
 */

Stefan
diff mbox

Patch

diff --git a/block/replication.c b/block/replication.c
index 729dd12..b96a3e5 100644
--- a/block/replication.c
+++ b/block/replication.c
@@ -25,9 +25,12 @@ 
 typedef struct BDRVReplicationState {
     ReplicationMode mode;
     int replication_state;
+    bool is_shared_disk;
+    char *shared_disk_id;
     BdrvChild *active_disk;
     BdrvChild *hidden_disk;
     BdrvChild *secondary_disk;
+    BdrvChild *primary_disk;
     char *top_id;
     ReplicationState *rs;
     Error *blocker;
@@ -53,6 +56,9 @@  static void replication_stop(ReplicationState *rs, bool failover,
 
 #define REPLICATION_MODE        "mode"
 #define REPLICATION_TOP_ID      "top-id"
+#define REPLICATION_SHARED_DISK "shared-disk"
+#define REPLICATION_SHARED_DISK_ID "shared-disk-id"
+
 static QemuOptsList replication_runtime_opts = {
     .name = "replication",
     .head = QTAILQ_HEAD_INITIALIZER(replication_runtime_opts.head),
@@ -65,6 +71,14 @@  static QemuOptsList replication_runtime_opts = {
             .name = REPLICATION_TOP_ID,
             .type = QEMU_OPT_STRING,
         },
+        {
+            .name = REPLICATION_SHARED_DISK_ID,
+            .type = QEMU_OPT_STRING,
+        },
+        {
+            .name = REPLICATION_SHARED_DISK,
+            .type = QEMU_OPT_BOOL,
+        },
         { /* end of list */ }
     },
 };
@@ -85,6 +99,9 @@  static int replication_open(BlockDriverState *bs, QDict *options,
     QemuOpts *opts = NULL;
     const char *mode;
     const char *top_id;
+    const char *shared_disk_id;
+    BlockBackend *blk;
+    BlockDriverState *tmp_bs;
 
     ret = -EINVAL;
     opts = qemu_opts_create(&replication_runtime_opts, NULL, 0, &error_abort);
@@ -119,12 +136,31 @@  static int replication_open(BlockDriverState *bs, QDict *options,
                    "The option mode's value should be primary or secondary");
         goto fail;
     }
+    s->is_shared_disk = qemu_opt_get_bool(opts, REPLICATION_SHARED_DISK,
+                                          false);
+    if (s->is_shared_disk && (s->mode == REPLICATION_MODE_PRIMARY)) {
+        shared_disk_id = qemu_opt_get(opts, REPLICATION_SHARED_DISK_ID);
+        if (!shared_disk_id) {
+            error_setg(&local_err, "Missing shared disk blk option");
+            goto fail;
+        }
+        s->shared_disk_id = g_strdup(shared_disk_id);
+        blk = blk_by_name(s->shared_disk_id);
+        if (!blk) {
+            error_setg(&local_err, "There is no %s block", s->shared_disk_id);
+            goto fail;
+        }
+        /* We can't access root member of BlockBackend directly */
+        tmp_bs = blk_bs(blk);
+        s->primary_disk = QLIST_FIRST(&tmp_bs->parents);
+    }
 
     s->rs = replication_new(bs, &replication_ops);
 
     ret = 0;
 
 fail:
+    g_free(s->shared_disk_id);
     qemu_opts_del(opts);
     error_propagate(errp, local_err);
 
@@ -135,6 +171,7 @@  static void replication_close(BlockDriverState *bs)
 {
     BDRVReplicationState *s = bs->opaque;
 
+    g_free(s->shared_disk_id);
     if (s->replication_state == BLOCK_REPLICATION_RUNNING) {
         replication_stop(s->rs, false, NULL);
     }
diff --git a/qapi/block-core.json b/qapi/block-core.json
index 1b3e6eb..e9fecda 100644
--- a/qapi/block-core.json
+++ b/qapi/block-core.json
@@ -2624,12 +2624,20 @@ 
 #          node who owns the replication node chain. Must not be given in
 #          primary mode.
 #
+# @shared-disk-id: #optional id of shared disk while is replication mode,
+#                  if @shared-disk is true, this option is required (Since: 2.9)
+#
+# @shared-disk: #optional (The default is false) to indicate whether or not
+#               a disk is shared by primary VM and secondary VM. (Since: 2.9)
+#
 # Since: 2.8
 ##
 { 'struct': 'BlockdevOptionsReplication',
   'base': 'BlockdevOptionsGenericFormat',
   'data': { 'mode': 'ReplicationMode',
-            '*top-id': 'str' } }
+            '*top-id': 'str',
+            '*shared-disk-id': 'str',
+            '*shared-disk': 'bool' } }
 
 ##
 # @NFSTransport: