*/
#include "qemu/osdep.h"
+#include "qemu/module.h"
#include "qemu/option.h"
#include "block/nbd.h"
#include "block/blockjob.h"
ReplicationMode mode;
ReplicationStage stage;
BdrvChild *active_disk;
+ BlockJob *commit_job;
BdrvChild *hidden_disk;
BdrvChild *secondary_disk;
+ BlockJob *backup_job;
char *top_id;
ReplicationState *rs;
Error *blocker;
{
int ret;
BDRVReplicationState *s = bs->opaque;
- Error *local_err = NULL;
QemuOpts *opts = NULL;
const char *mode;
const char *top_id;
- bs->file = bdrv_open_child(NULL, options, "file", bs, &child_file,
+ bs->file = bdrv_open_child(NULL, options, "file", bs, &child_of_bds,
+ BDRV_CHILD_FILTERED | BDRV_CHILD_PRIMARY,
false, errp);
if (!bs->file) {
return -EINVAL;
ret = -EINVAL;
opts = qemu_opts_create(&replication_runtime_opts, NULL, 0, &error_abort);
- qemu_opts_absorb_qdict(opts, options, &local_err);
- if (local_err) {
+ if (!qemu_opts_absorb_qdict(opts, options, errp)) {
goto fail;
}
mode = qemu_opt_get(opts, REPLICATION_MODE);
if (!mode) {
- error_setg(&local_err, "Missing the option mode");
+ error_setg(errp, "Missing the option mode");
goto fail;
}
s->mode = REPLICATION_MODE_PRIMARY;
top_id = qemu_opt_get(opts, REPLICATION_TOP_ID);
if (top_id) {
- error_setg(&local_err, "The primary side does not support option top-id");
+ error_setg(errp,
+ "The primary side does not support option top-id");
goto fail;
}
} else if (!strcmp(mode, "secondary")) {
top_id = qemu_opt_get(opts, REPLICATION_TOP_ID);
s->top_id = g_strdup(top_id);
if (!s->top_id) {
- error_setg(&local_err, "Missing the option top-id");
+ error_setg(errp, "Missing the option top-id");
goto fail;
}
} else {
- error_setg(&local_err,
+ error_setg(errp,
"The option mode's value should be primary or secondary");
goto fail;
}
fail:
qemu_opts_del(opts);
- error_propagate(errp, local_err);
-
return ret;
}
static void replication_close(BlockDriverState *bs)
{
BDRVReplicationState *s = bs->opaque;
+ Job *commit_job;
if (s->stage == BLOCK_REPLICATION_RUNNING) {
replication_stop(s->rs, false, NULL);
}
if (s->stage == BLOCK_REPLICATION_FAILOVER) {
- job_cancel_sync(&s->active_disk->bs->job->job);
+ commit_job = &s->commit_job->job;
+ assert(commit_job->aio_context == qemu_get_current_aio_context());
+ job_cancel_sync(commit_job);
}
if (s->mode == REPLICATION_MODE_SECONDARY) {
}
static void replication_child_perm(BlockDriverState *bs, BdrvChild *c,
- const BdrvChildRole *role,
+ BdrvChildRole role,
BlockReopenQueue *reopen_queue,
uint64_t perm, uint64_t shared,
uint64_t *nperm, uint64_t *nshared)
if ((bs->open_flags & (BDRV_O_INACTIVE | BDRV_O_RDWR)) == BDRV_O_RDWR) {
*nperm |= BLK_PERM_WRITE;
}
- *nshared = BLK_PERM_CONSISTENT_READ \
- | BLK_PERM_WRITE \
+ *nshared = BLK_PERM_CONSISTENT_READ
+ | BLK_PERM_WRITE
| BLK_PERM_WRITE_UNCHANGED;
return;
}
while (remaining_sectors > 0) {
int64_t count;
- ret = bdrv_is_allocated_above(top->bs, base->bs,
+ ret = bdrv_is_allocated_above(top->bs, base->bs, false,
sector_num * BDRV_SECTOR_SIZE,
remaining_sectors * BDRV_SECTOR_SIZE,
&count);
return ret;
}
-static bool replication_recurse_is_first_non_filter(BlockDriverState *bs,
- BlockDriverState *candidate)
-{
- return bdrv_recurse_is_first_non_filter(bs->file->bs, candidate);
-}
-
static void secondary_do_checkpoint(BDRVReplicationState *s, Error **errp)
{
Error *local_err = NULL;
int ret;
- if (!s->secondary_disk->bs->job) {
+ if (!s->backup_job) {
error_setg(errp, "Backup job was cancelled unexpectedly");
return;
}
- backup_do_checkpoint(s->secondary_disk->bs->job, &local_err);
+ backup_do_checkpoint(s->backup_job, &local_err);
if (local_err) {
error_propagate(errp, local_err);
return;
return;
}
- ret = s->active_disk->bs->drv->bdrv_make_empty(s->active_disk->bs);
+ ret = bdrv_make_empty(s->active_disk, errp);
if (ret < 0) {
- error_setg(errp, "Cannot make active disk empty");
return;
}
return;
}
- ret = s->hidden_disk->bs->drv->bdrv_make_empty(s->hidden_disk->bs);
+ BlockBackend *blk = blk_new(qemu_get_current_aio_context(),
+ BLK_PERM_WRITE, BLK_PERM_ALL);
+ blk_insert_bs(blk, s->hidden_disk->bs, &local_err);
+ if (local_err) {
+ error_propagate(errp, local_err);
+ blk_unref(blk);
+ return;
+ }
+
+ ret = blk_make_empty(blk, errp);
+ blk_unref(blk);
if (ret < 0) {
- error_setg(errp, "Cannot make hidden disk empty");
return;
}
}
{
BDRVReplicationState *s = bs->opaque;
BlockReopenQueue *reopen_queue = NULL;
- Error *local_err = NULL;
if (writable) {
s->orig_hidden_read_only = bdrv_is_read_only(s->hidden_disk->bs);
}
if (reopen_queue) {
- bdrv_reopen_multiple(reopen_queue, &local_err);
- error_propagate(errp, local_err);
+ bdrv_reopen_multiple(reopen_queue, errp);
}
bdrv_subtree_drained_end(s->hidden_disk->bs);
BDRVReplicationState *s = bs->opaque;
BlockDriverState *top_bs;
+ s->backup_job = NULL;
+
top_bs = bdrv_lookup_bs(s->top_id, s->top_id, NULL);
if (!top_bs) {
return;
int64_t active_length, hidden_length, disk_length;
AioContext *aio_context;
Error *local_err = NULL;
- BlockJob *job;
+ BackupPerf perf = { .use_copy_range = true, .max_workers = 1 };
aio_context = bdrv_get_aio_context(bs);
aio_context_acquire(aio_context);
s = bs->opaque;
+ if (s->stage == BLOCK_REPLICATION_DONE ||
+ s->stage == BLOCK_REPLICATION_FAILOVER) {
+ /*
+ * This case happens when a secondary is promoted to primary.
+ * Ignore the request because the secondary side of replication
+ * doesn't have to do anything anymore.
+ */
+ aio_context_release(aio_context);
+ return;
+ }
+
if (s->stage != BLOCK_REPLICATION_NONE) {
error_setg(errp, "Block replication is running or done");
aio_context_release(aio_context);
bdrv_op_block_all(top_bs, s->blocker);
bdrv_op_unblock(top_bs, BLOCK_OP_TYPE_DATAPLANE, s->blocker);
- job = backup_job_create(NULL, s->secondary_disk->bs, s->hidden_disk->bs,
- 0, MIRROR_SYNC_MODE_NONE, NULL, false,
+ s->backup_job = backup_job_create(
+ NULL, s->secondary_disk->bs, s->hidden_disk->bs,
+ 0, MIRROR_SYNC_MODE_NONE, NULL, 0, false, NULL,
+ &perf,
BLOCKDEV_ON_ERROR_REPORT,
BLOCKDEV_ON_ERROR_REPORT, JOB_INTERNAL,
backup_job_completed, bs, NULL, &local_err);
aio_context_release(aio_context);
return;
}
- job_start(&job->job);
+ job_start(&s->backup_job->job);
break;
default:
aio_context_release(aio_context);
aio_context_acquire(aio_context);
s = bs->opaque;
+ if (s->stage == BLOCK_REPLICATION_DONE ||
+ s->stage == BLOCK_REPLICATION_FAILOVER) {
+ /*
+ * This case happens when a secondary was promoted to primary.
+ * Ignore the request because the secondary side of replication
+ * doesn't have to do anything anymore.
+ */
+ aio_context_release(aio_context);
+ return;
+ }
+
if (s->mode == REPLICATION_MODE_SECONDARY) {
secondary_do_checkpoint(s, errp);
}
aio_context_acquire(aio_context);
s = bs->opaque;
- if (s->stage != BLOCK_REPLICATION_RUNNING) {
+ if (s->stage == BLOCK_REPLICATION_NONE) {
error_setg(errp, "Block replication is not running");
aio_context_release(aio_context);
return;
aio_context_acquire(aio_context);
s = bs->opaque;
+ if (s->stage == BLOCK_REPLICATION_DONE ||
+ s->stage == BLOCK_REPLICATION_FAILOVER) {
+ /*
+ * This case happens when a secondary was promoted to primary.
+ * Ignore the request because the secondary side of replication
+ * doesn't have to do anything anymore.
+ */
+ aio_context_release(aio_context);
+ return;
+ }
+
if (s->stage != BLOCK_REPLICATION_RUNNING) {
error_setg(errp, "Block replication is not running");
aio_context_release(aio_context);
* before the BDS is closed, because we will access hidden
* disk, secondary disk in backup_job_completed().
*/
- if (s->secondary_disk->bs->job) {
- job_cancel_sync(&s->secondary_disk->bs->job->job);
+ if (s->backup_job) {
+ job_cancel_sync(&s->backup_job->job);
}
if (!failover) {
}
s->stage = BLOCK_REPLICATION_FAILOVER;
- commit_active_start(NULL, s->active_disk->bs, s->secondary_disk->bs,
+ s->commit_job = commit_active_start(
+ NULL, s->active_disk->bs, s->secondary_disk->bs,
JOB_INTERNAL, 0, BLOCKDEV_ON_ERROR_REPORT,
NULL, replication_done, bs, true, errp);
break;
.bdrv_co_writev = replication_co_writev,
.is_filter = true,
- .bdrv_recurse_is_first_non_filter = replication_recurse_is_first_non_filter,
.has_variable_length = true,
.strong_runtime_opts = replication_strong_runtime_opts,