block: Make 'replication_state' an enum

BDRVReplicationState.replication_state is a name with a bit of
duplication, plus it could be an enum like BDRVReplicationState.mode,
which is more readable and also more straightforward in a debugger.

Rename it, and improve the type while at it.

Signed-off-by: Fam Zheng <famz@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Signed-off-by: Michael Tokarev <mjt@tls.msk.ru>
This commit is contained in:
Fam Zheng 2017-03-17 10:17:39 +08:00 committed by Michael Tokarev
parent ec45bbe5f1
commit 3c76c606da

View File

@ -22,9 +22,17 @@
#include "qapi/error.h" #include "qapi/error.h"
#include "replication.h" #include "replication.h"
typedef enum {
BLOCK_REPLICATION_NONE, /* block replication is not started */
BLOCK_REPLICATION_RUNNING, /* block replication is running */
BLOCK_REPLICATION_FAILOVER, /* failover is running in background */
BLOCK_REPLICATION_FAILOVER_FAILED, /* failover failed */
BLOCK_REPLICATION_DONE, /* block replication is done */
} ReplicationStage;
typedef struct BDRVReplicationState { typedef struct BDRVReplicationState {
ReplicationMode mode; ReplicationMode mode;
int replication_state; ReplicationStage stage;
BdrvChild *active_disk; BdrvChild *active_disk;
BdrvChild *hidden_disk; BdrvChild *hidden_disk;
BdrvChild *secondary_disk; BdrvChild *secondary_disk;
@ -36,14 +44,6 @@ typedef struct BDRVReplicationState {
int error; int error;
} BDRVReplicationState; } BDRVReplicationState;
enum {
BLOCK_REPLICATION_NONE, /* block replication is not started */
BLOCK_REPLICATION_RUNNING, /* block replication is running */
BLOCK_REPLICATION_FAILOVER, /* failover is running in background */
BLOCK_REPLICATION_FAILOVER_FAILED, /* failover failed */
BLOCK_REPLICATION_DONE, /* block replication is done */
};
static void replication_start(ReplicationState *rs, ReplicationMode mode, static void replication_start(ReplicationState *rs, ReplicationMode mode,
Error **errp); Error **errp);
static void replication_do_checkpoint(ReplicationState *rs, Error **errp); static void replication_do_checkpoint(ReplicationState *rs, Error **errp);
@ -141,10 +141,10 @@ static void replication_close(BlockDriverState *bs)
{ {
BDRVReplicationState *s = bs->opaque; BDRVReplicationState *s = bs->opaque;
if (s->replication_state == BLOCK_REPLICATION_RUNNING) { if (s->stage == BLOCK_REPLICATION_RUNNING) {
replication_stop(s->rs, false, NULL); replication_stop(s->rs, false, NULL);
} }
if (s->replication_state == BLOCK_REPLICATION_FAILOVER) { if (s->stage == BLOCK_REPLICATION_FAILOVER) {
block_job_cancel_sync(s->active_disk->bs->job); block_job_cancel_sync(s->active_disk->bs->job);
} }
@ -174,7 +174,7 @@ static int64_t replication_getlength(BlockDriverState *bs)
static int replication_get_io_status(BDRVReplicationState *s) static int replication_get_io_status(BDRVReplicationState *s)
{ {
switch (s->replication_state) { switch (s->stage) {
case BLOCK_REPLICATION_NONE: case BLOCK_REPLICATION_NONE:
return -EIO; return -EIO;
case BLOCK_REPLICATION_RUNNING: case BLOCK_REPLICATION_RUNNING:
@ -403,7 +403,7 @@ static void backup_job_completed(void *opaque, int ret)
BlockDriverState *bs = opaque; BlockDriverState *bs = opaque;
BDRVReplicationState *s = bs->opaque; BDRVReplicationState *s = bs->opaque;
if (s->replication_state != BLOCK_REPLICATION_FAILOVER) { if (s->stage != BLOCK_REPLICATION_FAILOVER) {
/* The backup job is cancelled unexpectedly */ /* The backup job is cancelled unexpectedly */
s->error = -EIO; s->error = -EIO;
} }
@ -445,7 +445,7 @@ static void replication_start(ReplicationState *rs, ReplicationMode mode,
aio_context_acquire(aio_context); aio_context_acquire(aio_context);
s = bs->opaque; s = bs->opaque;
if (s->replication_state != BLOCK_REPLICATION_NONE) { if (s->stage != BLOCK_REPLICATION_NONE) {
error_setg(errp, "Block replication is running or done"); error_setg(errp, "Block replication is running or done");
aio_context_release(aio_context); aio_context_release(aio_context);
return; return;
@ -545,7 +545,7 @@ static void replication_start(ReplicationState *rs, ReplicationMode mode,
abort(); abort();
} }
s->replication_state = BLOCK_REPLICATION_RUNNING; s->stage = BLOCK_REPLICATION_RUNNING;
if (s->mode == REPLICATION_MODE_SECONDARY) { if (s->mode == REPLICATION_MODE_SECONDARY) {
secondary_do_checkpoint(s, errp); secondary_do_checkpoint(s, errp);
@ -581,7 +581,7 @@ static void replication_get_error(ReplicationState *rs, Error **errp)
aio_context_acquire(aio_context); aio_context_acquire(aio_context);
s = bs->opaque; s = bs->opaque;
if (s->replication_state != BLOCK_REPLICATION_RUNNING) { if (s->stage != BLOCK_REPLICATION_RUNNING) {
error_setg(errp, "Block replication is not running"); error_setg(errp, "Block replication is not running");
aio_context_release(aio_context); aio_context_release(aio_context);
return; return;
@ -601,7 +601,7 @@ static void replication_done(void *opaque, int ret)
BDRVReplicationState *s = bs->opaque; BDRVReplicationState *s = bs->opaque;
if (ret == 0) { if (ret == 0) {
s->replication_state = BLOCK_REPLICATION_DONE; s->stage = BLOCK_REPLICATION_DONE;
/* refresh top bs's filename */ /* refresh top bs's filename */
bdrv_refresh_filename(bs); bdrv_refresh_filename(bs);
@ -610,7 +610,7 @@ static void replication_done(void *opaque, int ret)
s->hidden_disk = NULL; s->hidden_disk = NULL;
s->error = 0; s->error = 0;
} else { } else {
s->replication_state = BLOCK_REPLICATION_FAILOVER_FAILED; s->stage = BLOCK_REPLICATION_FAILOVER_FAILED;
s->error = -EIO; s->error = -EIO;
} }
} }
@ -625,7 +625,7 @@ static void replication_stop(ReplicationState *rs, bool failover, Error **errp)
aio_context_acquire(aio_context); aio_context_acquire(aio_context);
s = bs->opaque; s = bs->opaque;
if (s->replication_state != BLOCK_REPLICATION_RUNNING) { if (s->stage != BLOCK_REPLICATION_RUNNING) {
error_setg(errp, "Block replication is not running"); error_setg(errp, "Block replication is not running");
aio_context_release(aio_context); aio_context_release(aio_context);
return; return;
@ -633,7 +633,7 @@ static void replication_stop(ReplicationState *rs, bool failover, Error **errp)
switch (s->mode) { switch (s->mode) {
case REPLICATION_MODE_PRIMARY: case REPLICATION_MODE_PRIMARY:
s->replication_state = BLOCK_REPLICATION_DONE; s->stage = BLOCK_REPLICATION_DONE;
s->error = 0; s->error = 0;
break; break;
case REPLICATION_MODE_SECONDARY: case REPLICATION_MODE_SECONDARY:
@ -648,12 +648,12 @@ static void replication_stop(ReplicationState *rs, bool failover, Error **errp)
if (!failover) { if (!failover) {
secondary_do_checkpoint(s, errp); secondary_do_checkpoint(s, errp);
s->replication_state = BLOCK_REPLICATION_DONE; s->stage = BLOCK_REPLICATION_DONE;
aio_context_release(aio_context); aio_context_release(aio_context);
return; return;
} }
s->replication_state = BLOCK_REPLICATION_FAILOVER; s->stage = BLOCK_REPLICATION_FAILOVER;
commit_active_start(NULL, s->active_disk->bs, s->secondary_disk->bs, commit_active_start(NULL, s->active_disk->bs, s->secondary_disk->bs,
BLOCK_JOB_INTERNAL, 0, BLOCKDEV_ON_ERROR_REPORT, BLOCK_JOB_INTERNAL, 0, BLOCKDEV_ON_ERROR_REPORT,
NULL, replication_done, bs, true, errp); NULL, replication_done, bs, true, errp);