*/
#include "qemu/osdep.h"
+#include "qemu/module.h"
#include "qemu/option.h"
#include "block/nbd.h"
#include "block/blockjob.h"
#include "block/block_backup.h"
#include "sysemu/block-backend.h"
#include "qapi/error.h"
+#include "qapi/qmp/qdict.h"
#include "replication.h"
typedef enum {
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 orig_hidden_flags;
- int orig_secondary_flags;
+ bool orig_hidden_read_only;
+ bool orig_secondary_read_only;
int error;
} BDRVReplicationState;
replication_stop(s->rs, false, NULL);
}
if (s->stage == BLOCK_REPLICATION_FAILOVER) {
- job_cancel_sync(&s->active_disk->bs->job->job);
+ job_cancel_sync(&s->commit_job->job);
}
if (s->mode == REPLICATION_MODE_SECONDARY) {
QEMUIOVector *qiov)
{
BDRVReplicationState *s = bs->opaque;
- BdrvChild *child = s->secondary_disk;
- BlockJob *job = NULL;
- CowRequest req;
int ret;
if (s->mode == REPLICATION_MODE_PRIMARY) {
return ret;
}
- if (child && child->bs) {
- job = child->bs->job;
- }
-
- if (job) {
- uint64_t remaining_bytes = remaining_sectors * BDRV_SECTOR_SIZE;
-
- backup_wait_for_overlapping_requests(child->bs->job,
- sector_num * BDRV_SECTOR_SIZE,
- remaining_bytes);
- backup_cow_request_begin(&req, child->bs->job,
- sector_num * BDRV_SECTOR_SIZE,
- remaining_bytes);
- ret = bdrv_co_preadv(bs->file, sector_num * BDRV_SECTOR_SIZE,
- remaining_bytes, qiov, 0);
- backup_cow_request_end(&req);
- goto out;
- }
-
ret = bdrv_co_preadv(bs->file, sector_num * BDRV_SECTOR_SIZE,
remaining_sectors * BDRV_SECTOR_SIZE, qiov, 0);
-out:
+
return replication_return_value(s, ret);
}
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;
}
}
+/* This function is supposed to be called twice:
+ * first with writable = true, then with writable = false.
+ * The first call puts s->hidden_disk and s->secondary_disk in
+ * r/w mode, and the second puts them back in their original state.
+ */
static void reopen_backing_file(BlockDriverState *bs, bool writable,
Error **errp)
{
BDRVReplicationState *s = bs->opaque;
BlockReopenQueue *reopen_queue = NULL;
- int orig_hidden_flags, orig_secondary_flags;
- int new_hidden_flags, new_secondary_flags;
Error *local_err = NULL;
if (writable) {
- orig_hidden_flags = s->orig_hidden_flags =
- bdrv_get_flags(s->hidden_disk->bs);
- new_hidden_flags = (orig_hidden_flags | BDRV_O_RDWR) &
- ~BDRV_O_INACTIVE;
- orig_secondary_flags = s->orig_secondary_flags =
- bdrv_get_flags(s->secondary_disk->bs);
- new_secondary_flags = (orig_secondary_flags | BDRV_O_RDWR) &
- ~BDRV_O_INACTIVE;
- } else {
- orig_hidden_flags = (s->orig_hidden_flags | BDRV_O_RDWR) &
- ~BDRV_O_INACTIVE;
- new_hidden_flags = s->orig_hidden_flags;
- orig_secondary_flags = (s->orig_secondary_flags | BDRV_O_RDWR) &
- ~BDRV_O_INACTIVE;
- new_secondary_flags = s->orig_secondary_flags;
+ s->orig_hidden_read_only = bdrv_is_read_only(s->hidden_disk->bs);
+ s->orig_secondary_read_only = bdrv_is_read_only(s->secondary_disk->bs);
}
bdrv_subtree_drained_begin(s->hidden_disk->bs);
bdrv_subtree_drained_begin(s->secondary_disk->bs);
- if (orig_hidden_flags != new_hidden_flags) {
- reopen_queue = bdrv_reopen_queue(reopen_queue, s->hidden_disk->bs, NULL,
- new_hidden_flags);
+ if (s->orig_hidden_read_only) {
+ QDict *opts = qdict_new();
+ qdict_put_bool(opts, BDRV_OPT_READ_ONLY, !writable);
+ reopen_queue = bdrv_reopen_queue(reopen_queue, s->hidden_disk->bs,
+ opts, true);
}
- if (!(orig_secondary_flags & BDRV_O_RDWR)) {
+ if (s->orig_secondary_read_only) {
+ QDict *opts = qdict_new();
+ qdict_put_bool(opts, BDRV_OPT_READ_ONLY, !writable);
reopen_queue = bdrv_reopen_queue(reopen_queue, s->secondary_disk->bs,
- NULL, new_secondary_flags);
+ opts, true);
}
if (reopen_queue) {
- bdrv_reopen_multiple(bdrv_get_aio_context(bs),
- reopen_queue, &local_err);
+ bdrv_reopen_multiple(reopen_queue, &local_err);
error_propagate(errp, local_err);
}
int64_t active_length, hidden_length, disk_length;
AioContext *aio_context;
Error *local_err = NULL;
- BlockJob *job;
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,
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;
if (ret == 0) {
s->stage = BLOCK_REPLICATION_DONE;
- /* refresh top bs's filename */
- bdrv_refresh_filename(bs);
s->active_disk = NULL;
s->secondary_disk = NULL;
s->hidden_disk = NULL;
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;
aio_context_release(aio_context);
}
-BlockDriver bdrv_replication = {
+static const char *const replication_strong_runtime_opts[] = {
+ REPLICATION_MODE,
+ REPLICATION_TOP_ID,
+
+ NULL
+};
+
+static BlockDriver bdrv_replication = {
.format_name = "replication",
.instance_size = sizeof(BDRVReplicationState),
.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,
};
static void bdrv_replication_init(void)