2012-10-18 18:49:23 +04:00
|
|
|
/*
|
|
|
|
* Image mirroring
|
|
|
|
*
|
|
|
|
* Copyright Red Hat, Inc. 2012
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Paolo Bonzini <pbonzini@redhat.com>
|
|
|
|
*
|
|
|
|
* This work is licensed under the terms of the GNU LGPL, version 2 or later.
|
|
|
|
* See the COPYING.LIB file in the top-level directory.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2016-01-18 21:01:42 +03:00
|
|
|
#include "qemu/osdep.h"
|
2017-03-09 13:49:16 +03:00
|
|
|
#include "qemu/cutils.h"
|
2012-10-18 18:49:23 +04:00
|
|
|
#include "trace.h"
|
2016-10-27 19:07:00 +03:00
|
|
|
#include "block/blockjob_int.h"
|
2012-12-17 21:19:44 +04:00
|
|
|
#include "block/block_int.h"
|
2015-10-19 18:53:22 +03:00
|
|
|
#include "sysemu/block-backend.h"
|
include/qemu/osdep.h: Don't include qapi/error.h
Commit 57cb38b included qapi/error.h into qemu/osdep.h to get the
Error typedef. Since then, we've moved to include qemu/osdep.h
everywhere. Its file comment explains: "To avoid getting into
possible circular include dependencies, this file should not include
any other QEMU headers, with the exceptions of config-host.h,
compiler.h, os-posix.h and os-win32.h, all of which are doing a
similar job to this file and are under similar constraints."
qapi/error.h doesn't do a similar job, and it doesn't adhere to
similar constraints: it includes qapi-types.h. That's in excess of
100KiB of crap most .c files don't actually need.
Add the typedef to qemu/typedefs.h, and include that instead of
qapi/error.h. Include qapi/error.h in .c files that need it and don't
get it now. Include qapi-types.h in qom/object.h for uint16List.
Update scripts/clean-includes accordingly. Update it further to match
reality: replace config.h by config-target.h, add sysemu/os-posix.h,
sysemu/os-win32.h. Update the list of includes in the qemu/osdep.h
comment quoted above similarly.
This reduces the number of objects depending on qapi/error.h from "all
of them" to less than a third. Unfortunately, the number depending on
qapi-types.h shrinks only a little. More work is needed for that one.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
[Fix compilation without the spice devel packages. - Paolo]
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2016-03-14 11:01:28 +03:00
|
|
|
#include "qapi/error.h"
|
2015-03-17 19:22:46 +03:00
|
|
|
#include "qapi/qmp/qerror.h"
|
2012-10-18 18:49:23 +04:00
|
|
|
#include "qemu/ratelimit.h"
|
2013-01-21 20:09:43 +04:00
|
|
|
#include "qemu/bitmap.h"
|
2012-10-18 18:49:23 +04:00
|
|
|
|
2013-01-22 12:03:14 +04:00
|
|
|
#define SLICE_TIME 100000000ULL /* ns */
|
|
|
|
#define MAX_IN_FLIGHT 16
|
2017-07-07 15:44:46 +03:00
|
|
|
#define MAX_IO_BYTES (1 << 20) /* 1 Mb */
|
|
|
|
#define DEFAULT_MIRROR_BUF_SIZE (MAX_IN_FLIGHT * MAX_IO_BYTES)
|
2013-01-22 12:03:14 +04:00
|
|
|
|
|
|
|
/* The mirroring buffer is a list of granularity-sized chunks.
|
|
|
|
* Free chunks are organized in a list.
|
|
|
|
*/
|
|
|
|
typedef struct MirrorBuffer {
|
|
|
|
QSIMPLEQ_ENTRY(MirrorBuffer) next;
|
|
|
|
} MirrorBuffer;
|
2012-10-18 18:49:23 +04:00
|
|
|
|
|
|
|
typedef struct MirrorBlockJob {
|
|
|
|
BlockJob common;
|
|
|
|
RateLimit limit;
|
2016-04-12 17:17:41 +03:00
|
|
|
BlockBackend *target;
|
2017-01-25 21:16:34 +03:00
|
|
|
BlockDriverState *mirror_top_bs;
|
|
|
|
BlockDriverState *source;
|
2013-12-16 10:45:29 +04:00
|
|
|
BlockDriverState *base;
|
2017-01-25 21:16:34 +03:00
|
|
|
|
2014-06-27 20:25:25 +04:00
|
|
|
/* The name of the graph node to replace */
|
|
|
|
char *replaces;
|
|
|
|
/* The BDS to replace */
|
|
|
|
BlockDriverState *to_replace;
|
|
|
|
/* Used to block operations on the drive-mirror-replace target */
|
|
|
|
Error *replace_blocker;
|
2013-12-16 10:45:30 +04:00
|
|
|
bool is_none_mode;
|
block/mirror: Fix target backing BDS
Currently, we are trying to move the backing BDS from the source to the
target in bdrv_replace_in_backing_chain() which is called from
mirror_exit(). However, mirror_complete() already tries to open the
target's backing chain with a call to bdrv_open_backing_file().
First, we should only set the target's backing BDS once. Second, the
mirroring block job has a better idea of what to set it to than the
generic code in bdrv_replace_in_backing_chain() (in fact, the latter's
conditions on when to move the backing BDS from source to target are not
really correct).
Therefore, remove that code from bdrv_replace_in_backing_chain() and
leave it to mirror_complete().
Depending on what kind of mirroring is performed, we furthermore want to
use different strategies to open the target's backing chain:
- If blockdev-mirror is used, we can assume the user made sure that the
target already has the correct backing chain. In particular, we should
not try to open a backing file if the target does not have any yet.
- If drive-mirror with mode=absolute-paths is used, we can and should
reuse the already existing chain of nodes that the source BDS is in.
In case of sync=full, no backing BDS is required; with sync=top, we
just link the source's backing BDS to the target, and with sync=none,
we use the source BDS as the target's backing BDS.
We should not try to open these backing files anew because this would
lead to two BDSs existing per physical file in the backing chain, and
we would like to avoid such concurrent access.
- If drive-mirror with mode=existing is used, we have to use the
information provided in the physical image file which means opening
the target's backing chain completely anew, just as it has been done
already.
If the target's backing chain shares images with the source, this may
lead to multiple BDSs per physical image file. But since we cannot
reliably ascertain this case, there is nothing we can do about it.
Signed-off-by: Max Reitz <mreitz@redhat.com>
Message-id: 20160610185750.30956-3-mreitz@redhat.com
Reviewed-by: Kevin Wolf <kwolf@redhat.com>
Reviewed-by: Fam Zheng <famz@redhat.com>
Signed-off-by: Max Reitz <mreitz@redhat.com>
2016-06-10 21:57:47 +03:00
|
|
|
BlockMirrorBackingMode backing_mode;
|
2012-10-18 18:49:28 +04:00
|
|
|
BlockdevOnError on_source_error, on_target_error;
|
2012-10-18 18:49:25 +04:00
|
|
|
bool synced;
|
|
|
|
bool should_complete;
|
2013-01-21 20:09:46 +04:00
|
|
|
int64_t granularity;
|
2013-01-21 20:09:43 +04:00
|
|
|
size_t buf_size;
|
2014-10-24 17:57:36 +04:00
|
|
|
int64_t bdev_length;
|
2013-01-21 20:09:43 +04:00
|
|
|
unsigned long *cow_bitmap;
|
2013-11-13 14:29:43 +04:00
|
|
|
BdrvDirtyBitmap *dirty_bitmap;
|
2016-10-14 00:58:21 +03:00
|
|
|
BdrvDirtyBitmapIter *dbi;
|
2012-10-18 18:49:23 +04:00
|
|
|
uint8_t *buf;
|
2013-01-22 12:03:14 +04:00
|
|
|
QSIMPLEQ_HEAD(, MirrorBuffer) buf_free;
|
|
|
|
int buf_free_count;
|
2013-01-22 12:03:12 +04:00
|
|
|
|
2016-07-14 16:33:24 +03:00
|
|
|
uint64_t last_pause_ns;
|
2013-01-22 12:03:14 +04:00
|
|
|
unsigned long *in_flight_bitmap;
|
2013-01-22 12:03:12 +04:00
|
|
|
int in_flight;
|
2017-07-07 15:44:46 +03:00
|
|
|
int64_t bytes_in_flight;
|
2013-01-22 12:03:12 +04:00
|
|
|
int ret;
|
2015-06-08 08:56:08 +03:00
|
|
|
bool unmap;
|
2015-08-13 11:41:50 +03:00
|
|
|
bool waiting_for_io;
|
2017-07-07 15:44:46 +03:00
|
|
|
int target_cluster_size;
|
2016-02-05 05:00:29 +03:00
|
|
|
int max_iov;
|
2017-02-02 17:25:15 +03:00
|
|
|
bool initial_zeroing_ongoing;
|
2012-10-18 18:49:23 +04:00
|
|
|
} MirrorBlockJob;
|
|
|
|
|
2013-01-22 12:03:12 +04:00
|
|
|
typedef struct MirrorOp {
|
|
|
|
MirrorBlockJob *s;
|
|
|
|
QEMUIOVector qiov;
|
2017-07-07 15:44:46 +03:00
|
|
|
int64_t offset;
|
|
|
|
uint64_t bytes;
|
2013-01-22 12:03:12 +04:00
|
|
|
} MirrorOp;
|
|
|
|
|
2012-10-18 18:49:28 +04:00
|
|
|
static BlockErrorAction mirror_error_action(MirrorBlockJob *s, bool read,
|
|
|
|
int error)
|
|
|
|
{
|
|
|
|
s->synced = false;
|
|
|
|
if (read) {
|
2016-04-18 12:36:38 +03:00
|
|
|
return block_job_error_action(&s->common, s->on_source_error,
|
|
|
|
true, error);
|
2012-10-18 18:49:28 +04:00
|
|
|
} else {
|
2016-04-18 12:36:38 +03:00
|
|
|
return block_job_error_action(&s->common, s->on_target_error,
|
|
|
|
false, error);
|
2012-10-18 18:49:28 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-22 12:03:12 +04:00
|
|
|
static void mirror_iteration_done(MirrorOp *op, int ret)
|
|
|
|
{
|
|
|
|
MirrorBlockJob *s = op->s;
|
2013-01-22 12:03:14 +04:00
|
|
|
struct iovec *iov;
|
2013-01-22 12:03:12 +04:00
|
|
|
int64_t chunk_num;
|
2017-07-07 15:44:46 +03:00
|
|
|
int i, nb_chunks;
|
2013-01-22 12:03:12 +04:00
|
|
|
|
2017-07-07 15:44:46 +03:00
|
|
|
trace_mirror_iteration_done(s, op->offset, op->bytes, ret);
|
2013-01-22 12:03:12 +04:00
|
|
|
|
|
|
|
s->in_flight--;
|
2017-07-07 15:44:46 +03:00
|
|
|
s->bytes_in_flight -= op->bytes;
|
2013-01-22 12:03:14 +04:00
|
|
|
iov = op->qiov.iov;
|
|
|
|
for (i = 0; i < op->qiov.niov; i++) {
|
|
|
|
MirrorBuffer *buf = (MirrorBuffer *) iov[i].iov_base;
|
|
|
|
QSIMPLEQ_INSERT_TAIL(&s->buf_free, buf, next);
|
|
|
|
s->buf_free_count++;
|
|
|
|
}
|
|
|
|
|
2017-07-07 15:44:46 +03:00
|
|
|
chunk_num = op->offset / s->granularity;
|
|
|
|
nb_chunks = DIV_ROUND_UP(op->bytes, s->granularity);
|
2013-01-22 12:03:14 +04:00
|
|
|
bitmap_clear(s->in_flight_bitmap, chunk_num, nb_chunks);
|
2014-10-24 17:57:36 +04:00
|
|
|
if (ret >= 0) {
|
|
|
|
if (s->cow_bitmap) {
|
|
|
|
bitmap_set(s->cow_bitmap, chunk_num, nb_chunks);
|
|
|
|
}
|
2017-02-02 17:25:15 +03:00
|
|
|
if (!s->initial_zeroing_ongoing) {
|
2017-07-07 15:44:46 +03:00
|
|
|
s->common.offset += op->bytes;
|
2017-02-02 17:25:15 +03:00
|
|
|
}
|
2013-01-22 12:03:12 +04:00
|
|
|
}
|
2014-01-23 11:59:16 +04:00
|
|
|
qemu_iovec_destroy(&op->qiov);
|
2015-10-01 14:04:39 +03:00
|
|
|
g_free(op);
|
2014-03-21 16:55:19 +04:00
|
|
|
|
2015-08-13 11:41:50 +03:00
|
|
|
if (s->waiting_for_io) {
|
coroutine: move entry argument to qemu_coroutine_create
In practice the entry argument is always known at creation time, and
it is confusing that sometimes qemu_coroutine_enter is used with a
non-NULL argument to re-enter a coroutine (this happens in
block/sheepdog.c and tests/test-coroutine.c). So pass the opaque value
at creation time, for consistency with e.g. aio_bh_new.
Mostly done with the following semantic patch:
@ entry1 @
expression entry, arg, co;
@@
- co = qemu_coroutine_create(entry);
+ co = qemu_coroutine_create(entry, arg);
...
- qemu_coroutine_enter(co, arg);
+ qemu_coroutine_enter(co);
@ entry2 @
expression entry, arg;
identifier co;
@@
- Coroutine *co = qemu_coroutine_create(entry);
+ Coroutine *co = qemu_coroutine_create(entry, arg);
...
- qemu_coroutine_enter(co, arg);
+ qemu_coroutine_enter(co);
@ entry3 @
expression entry, arg;
@@
- qemu_coroutine_enter(qemu_coroutine_create(entry), arg);
+ qemu_coroutine_enter(qemu_coroutine_create(entry, arg));
@ reentry @
expression co;
@@
- qemu_coroutine_enter(co, NULL);
+ qemu_coroutine_enter(co);
except for the aforementioned few places where the semantic patch
stumbled (as expected) and for test_co_queue, which would otherwise
produce an uninitialized variable warning.
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Reviewed-by: Fam Zheng <famz@redhat.com>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
2016-07-04 20:10:01 +03:00
|
|
|
qemu_coroutine_enter(s->common.co);
|
2014-03-21 16:55:19 +04:00
|
|
|
}
|
2013-01-22 12:03:12 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void mirror_write_complete(void *opaque, int ret)
|
|
|
|
{
|
|
|
|
MirrorOp *op = opaque;
|
|
|
|
MirrorBlockJob *s = op->s;
|
2017-02-13 16:52:32 +03:00
|
|
|
|
|
|
|
aio_context_acquire(blk_get_aio_context(s->common.blk));
|
2013-01-22 12:03:12 +04:00
|
|
|
if (ret < 0) {
|
|
|
|
BlockErrorAction action;
|
|
|
|
|
2017-09-25 17:55:20 +03:00
|
|
|
bdrv_set_dirty_bitmap(s->dirty_bitmap, op->offset, op->bytes);
|
2013-01-22 12:03:12 +04:00
|
|
|
action = mirror_error_action(s, false, -ret);
|
2014-06-18 10:43:30 +04:00
|
|
|
if (action == BLOCK_ERROR_ACTION_REPORT && s->ret >= 0) {
|
2013-01-22 12:03:12 +04:00
|
|
|
s->ret = ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mirror_iteration_done(op, ret);
|
2017-02-13 16:52:32 +03:00
|
|
|
aio_context_release(blk_get_aio_context(s->common.blk));
|
2013-01-22 12:03:12 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void mirror_read_complete(void *opaque, int ret)
|
|
|
|
{
|
|
|
|
MirrorOp *op = opaque;
|
|
|
|
MirrorBlockJob *s = op->s;
|
2017-02-13 16:52:32 +03:00
|
|
|
|
|
|
|
aio_context_acquire(blk_get_aio_context(s->common.blk));
|
2013-01-22 12:03:12 +04:00
|
|
|
if (ret < 0) {
|
|
|
|
BlockErrorAction action;
|
|
|
|
|
2017-09-25 17:55:20 +03:00
|
|
|
bdrv_set_dirty_bitmap(s->dirty_bitmap, op->offset, op->bytes);
|
2013-01-22 12:03:12 +04:00
|
|
|
action = mirror_error_action(s, true, -ret);
|
2014-06-18 10:43:30 +04:00
|
|
|
if (action == BLOCK_ERROR_ACTION_REPORT && s->ret >= 0) {
|
2013-01-22 12:03:12 +04:00
|
|
|
s->ret = ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
mirror_iteration_done(op, ret);
|
2017-02-13 16:52:32 +03:00
|
|
|
} else {
|
2017-07-07 15:44:46 +03:00
|
|
|
blk_aio_pwritev(s->target, op->offset, &op->qiov,
|
2017-02-13 16:52:32 +03:00
|
|
|
0, mirror_write_complete, op);
|
2013-01-22 12:03:12 +04:00
|
|
|
}
|
2017-02-13 16:52:32 +03:00
|
|
|
aio_context_release(blk_get_aio_context(s->common.blk));
|
2013-01-22 12:03:12 +04:00
|
|
|
}
|
|
|
|
|
2017-07-07 15:44:49 +03:00
|
|
|
/* Clip bytes relative to offset to not exceed end-of-file */
|
|
|
|
static inline int64_t mirror_clip_bytes(MirrorBlockJob *s,
|
|
|
|
int64_t offset,
|
|
|
|
int64_t bytes)
|
|
|
|
{
|
|
|
|
return MIN(bytes, s->bdev_length - offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Round offset and/or bytes to target cluster if COW is needed, and
|
|
|
|
* return the offset of the adjusted tail against original. */
|
|
|
|
static int mirror_cow_align(MirrorBlockJob *s, int64_t *offset,
|
2017-07-07 15:44:50 +03:00
|
|
|
uint64_t *bytes)
|
2012-10-18 18:49:23 +04:00
|
|
|
{
|
2016-02-05 05:00:29 +03:00
|
|
|
bool need_cow;
|
|
|
|
int ret = 0;
|
2017-07-07 15:44:49 +03:00
|
|
|
int64_t align_offset = *offset;
|
2017-10-12 06:46:59 +03:00
|
|
|
int64_t align_bytes = *bytes;
|
2017-07-07 15:44:49 +03:00
|
|
|
int max_bytes = s->granularity * s->max_iov;
|
2016-02-05 05:00:29 +03:00
|
|
|
|
2017-07-07 15:44:49 +03:00
|
|
|
need_cow = !test_bit(*offset / s->granularity, s->cow_bitmap);
|
|
|
|
need_cow |= !test_bit((*offset + *bytes - 1) / s->granularity,
|
2016-02-05 05:00:29 +03:00
|
|
|
s->cow_bitmap);
|
|
|
|
if (need_cow) {
|
2017-07-07 15:44:49 +03:00
|
|
|
bdrv_round_to_clusters(blk_bs(s->target), *offset, *bytes,
|
|
|
|
&align_offset, &align_bytes);
|
2016-02-05 05:00:29 +03:00
|
|
|
}
|
2015-07-09 12:56:47 +03:00
|
|
|
|
2017-07-07 15:44:49 +03:00
|
|
|
if (align_bytes > max_bytes) {
|
|
|
|
align_bytes = max_bytes;
|
2016-02-05 05:00:29 +03:00
|
|
|
if (need_cow) {
|
2017-07-07 15:44:49 +03:00
|
|
|
align_bytes = QEMU_ALIGN_DOWN(align_bytes, s->target_cluster_size);
|
2016-02-05 05:00:29 +03:00
|
|
|
}
|
2013-01-21 20:09:41 +04:00
|
|
|
}
|
2017-07-07 15:44:49 +03:00
|
|
|
/* Clipping may result in align_bytes unaligned to chunk boundary, but
|
2016-04-20 05:48:34 +03:00
|
|
|
* that doesn't matter because it's already the end of source image. */
|
2017-07-07 15:44:49 +03:00
|
|
|
align_bytes = mirror_clip_bytes(s, align_offset, align_bytes);
|
2013-01-21 20:09:41 +04:00
|
|
|
|
2017-07-07 15:44:49 +03:00
|
|
|
ret = align_offset + align_bytes - (*offset + *bytes);
|
|
|
|
*offset = align_offset;
|
|
|
|
*bytes = align_bytes;
|
2016-02-05 05:00:29 +03:00
|
|
|
assert(ret >= 0);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-02-05 05:00:30 +03:00
|
|
|
static inline void mirror_wait_for_io(MirrorBlockJob *s)
|
|
|
|
{
|
|
|
|
assert(!s->waiting_for_io);
|
|
|
|
s->waiting_for_io = true;
|
|
|
|
qemu_coroutine_yield();
|
|
|
|
s->waiting_for_io = false;
|
|
|
|
}
|
|
|
|
|
2016-02-05 05:00:29 +03:00
|
|
|
/* Submit async read while handling COW.
|
2017-07-07 15:44:50 +03:00
|
|
|
* Returns: The number of bytes copied after and including offset,
|
|
|
|
* excluding any bytes copied prior to offset due to alignment.
|
|
|
|
* This will be @bytes if no alignment is necessary, or
|
|
|
|
* (new_end - offset) if tail is rounded up or down due to
|
2016-02-05 05:00:29 +03:00
|
|
|
* alignment or buffer limit.
|
|
|
|
*/
|
2017-07-07 15:44:50 +03:00
|
|
|
static uint64_t mirror_do_read(MirrorBlockJob *s, int64_t offset,
|
|
|
|
uint64_t bytes)
|
2016-02-05 05:00:29 +03:00
|
|
|
{
|
2016-04-12 17:17:41 +03:00
|
|
|
BlockBackend *source = s->common.blk;
|
2017-07-07 15:44:50 +03:00
|
|
|
int nb_chunks;
|
|
|
|
uint64_t ret;
|
2016-02-05 05:00:29 +03:00
|
|
|
MirrorOp *op;
|
2017-07-07 15:44:50 +03:00
|
|
|
uint64_t max_bytes;
|
2016-02-05 05:00:29 +03:00
|
|
|
|
2017-07-07 15:44:50 +03:00
|
|
|
max_bytes = s->granularity * s->max_iov;
|
2013-01-22 12:03:14 +04:00
|
|
|
|
2016-02-05 05:00:29 +03:00
|
|
|
/* We can only handle as much as buf_size at a time. */
|
2017-07-07 15:44:50 +03:00
|
|
|
bytes = MIN(s->buf_size, MIN(max_bytes, bytes));
|
|
|
|
assert(bytes);
|
|
|
|
assert(bytes < BDRV_REQUEST_MAX_BYTES);
|
|
|
|
ret = bytes;
|
2013-01-22 12:03:14 +04:00
|
|
|
|
2016-02-05 05:00:29 +03:00
|
|
|
if (s->cow_bitmap) {
|
2017-07-07 15:44:50 +03:00
|
|
|
ret += mirror_cow_align(s, &offset, &bytes);
|
2016-02-05 05:00:29 +03:00
|
|
|
}
|
2017-07-07 15:44:50 +03:00
|
|
|
assert(bytes <= s->buf_size);
|
|
|
|
/* The offset is granularity-aligned because:
|
2016-02-05 05:00:29 +03:00
|
|
|
* 1) Caller passes in aligned values;
|
|
|
|
* 2) mirror_cow_align is used only when target cluster is larger. */
|
2017-07-07 15:44:50 +03:00
|
|
|
assert(QEMU_IS_ALIGNED(offset, s->granularity));
|
|
|
|
/* The range is sector-aligned, since bdrv_getlength() rounds up. */
|
|
|
|
assert(QEMU_IS_ALIGNED(bytes, BDRV_SECTOR_SIZE));
|
|
|
|
nb_chunks = DIV_ROUND_UP(bytes, s->granularity);
|
2016-02-05 05:00:29 +03:00
|
|
|
|
|
|
|
while (s->buf_free_count < nb_chunks) {
|
2017-07-07 15:44:50 +03:00
|
|
|
trace_mirror_yield_in_flight(s, offset, s->in_flight);
|
2016-02-05 05:00:30 +03:00
|
|
|
mirror_wait_for_io(s);
|
2013-01-21 20:09:43 +04:00
|
|
|
}
|
|
|
|
|
2013-01-22 12:03:12 +04:00
|
|
|
/* Allocate a MirrorOp that is used as an AIO callback. */
|
2015-10-01 14:04:39 +03:00
|
|
|
op = g_new(MirrorOp, 1);
|
2013-01-22 12:03:12 +04:00
|
|
|
op->s = s;
|
2017-07-07 15:44:50 +03:00
|
|
|
op->offset = offset;
|
|
|
|
op->bytes = bytes;
|
2013-01-22 12:03:14 +04:00
|
|
|
|
|
|
|
/* Now make a QEMUIOVector taking enough granularity-sized chunks
|
|
|
|
* from s->buf_free.
|
|
|
|
*/
|
|
|
|
qemu_iovec_init(&op->qiov, nb_chunks);
|
|
|
|
while (nb_chunks-- > 0) {
|
|
|
|
MirrorBuffer *buf = QSIMPLEQ_FIRST(&s->buf_free);
|
2017-07-07 15:44:50 +03:00
|
|
|
size_t remaining = bytes - op->qiov.size;
|
2014-07-01 18:52:21 +04:00
|
|
|
|
2013-01-22 12:03:14 +04:00
|
|
|
QSIMPLEQ_REMOVE_HEAD(&s->buf_free, next);
|
|
|
|
s->buf_free_count--;
|
2014-07-01 18:52:21 +04:00
|
|
|
qemu_iovec_add(&op->qiov, buf, MIN(s->granularity, remaining));
|
2013-01-22 12:03:14 +04:00
|
|
|
}
|
2013-01-22 12:03:12 +04:00
|
|
|
|
2012-10-18 18:49:23 +04:00
|
|
|
/* Copy the dirty cluster. */
|
2013-01-22 12:03:12 +04:00
|
|
|
s->in_flight++;
|
2017-07-07 15:44:50 +03:00
|
|
|
s->bytes_in_flight += bytes;
|
|
|
|
trace_mirror_one_iteration(s, offset, bytes);
|
2015-06-08 08:56:09 +03:00
|
|
|
|
2017-07-07 15:44:50 +03:00
|
|
|
blk_aio_preadv(source, offset, &op->qiov, 0, mirror_read_complete, op);
|
2016-02-05 05:00:29 +03:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void mirror_do_zero_or_discard(MirrorBlockJob *s,
|
2017-07-07 15:44:47 +03:00
|
|
|
int64_t offset,
|
|
|
|
uint64_t bytes,
|
2016-02-05 05:00:29 +03:00
|
|
|
bool is_discard)
|
|
|
|
{
|
|
|
|
MirrorOp *op;
|
|
|
|
|
|
|
|
/* Allocate a MirrorOp that is used as an AIO callback. The qiov is zeroed
|
|
|
|
* so the freeing in mirror_iteration_done is nop. */
|
|
|
|
op = g_new0(MirrorOp, 1);
|
|
|
|
op->s = s;
|
2017-07-07 15:44:47 +03:00
|
|
|
op->offset = offset;
|
|
|
|
op->bytes = bytes;
|
2016-02-05 05:00:29 +03:00
|
|
|
|
|
|
|
s->in_flight++;
|
2017-07-07 15:44:47 +03:00
|
|
|
s->bytes_in_flight += bytes;
|
2016-02-05 05:00:29 +03:00
|
|
|
if (is_discard) {
|
2017-07-07 15:44:47 +03:00
|
|
|
blk_aio_pdiscard(s->target, offset,
|
2017-07-07 15:44:46 +03:00
|
|
|
op->bytes, mirror_write_complete, op);
|
2016-02-05 05:00:29 +03:00
|
|
|
} else {
|
2017-07-07 15:44:47 +03:00
|
|
|
blk_aio_pwrite_zeroes(s->target, offset,
|
2017-07-07 15:44:46 +03:00
|
|
|
op->bytes, s->unmap ? BDRV_REQ_MAY_UNMAP : 0,
|
2015-06-08 08:56:09 +03:00
|
|
|
mirror_write_complete, op);
|
2016-02-05 05:00:29 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint64_t coroutine_fn mirror_iteration(MirrorBlockJob *s)
|
|
|
|
{
|
2017-01-25 21:16:34 +03:00
|
|
|
BlockDriverState *source = s->source;
|
2017-07-07 15:44:51 +03:00
|
|
|
int64_t offset, first_chunk;
|
2016-02-05 05:00:29 +03:00
|
|
|
uint64_t delay_ns = 0;
|
|
|
|
/* At least the first dirty chunk is mirrored in one iteration. */
|
|
|
|
int nb_chunks = 1;
|
2016-07-14 16:33:29 +03:00
|
|
|
bool write_zeroes_ok = bdrv_can_write_zeroes_with_unmap(blk_bs(s->target));
|
2017-07-07 15:44:46 +03:00
|
|
|
int max_io_bytes = MAX(s->buf_size / MAX_IN_FLIGHT, MAX_IO_BYTES);
|
2016-02-05 05:00:29 +03:00
|
|
|
|
2017-06-05 15:39:05 +03:00
|
|
|
bdrv_dirty_bitmap_lock(s->dirty_bitmap);
|
2017-09-25 17:55:17 +03:00
|
|
|
offset = bdrv_dirty_iter_next(s->dbi);
|
2017-07-07 15:44:51 +03:00
|
|
|
if (offset < 0) {
|
2016-10-14 00:58:21 +03:00
|
|
|
bdrv_set_dirty_iter(s->dbi, 0);
|
2017-09-25 17:55:17 +03:00
|
|
|
offset = bdrv_dirty_iter_next(s->dbi);
|
2017-09-25 17:55:18 +03:00
|
|
|
trace_mirror_restart_iter(s, bdrv_get_dirty_count(s->dirty_bitmap));
|
2017-07-07 15:44:51 +03:00
|
|
|
assert(offset >= 0);
|
2016-02-05 05:00:29 +03:00
|
|
|
}
|
2017-06-05 15:39:05 +03:00
|
|
|
bdrv_dirty_bitmap_unlock(s->dirty_bitmap);
|
2016-02-05 05:00:29 +03:00
|
|
|
|
2017-07-07 15:44:51 +03:00
|
|
|
first_chunk = offset / s->granularity;
|
2016-04-20 01:59:47 +03:00
|
|
|
while (test_bit(first_chunk, s->in_flight_bitmap)) {
|
2017-07-07 15:44:51 +03:00
|
|
|
trace_mirror_yield_in_flight(s, offset, s->in_flight);
|
2016-04-20 01:59:47 +03:00
|
|
|
mirror_wait_for_io(s);
|
|
|
|
}
|
|
|
|
|
2016-06-16 19:56:28 +03:00
|
|
|
block_job_pause_point(&s->common);
|
|
|
|
|
2016-02-05 05:00:29 +03:00
|
|
|
/* Find the number of consective dirty chunks following the first dirty
|
|
|
|
* one, and wait for in flight requests in them. */
|
2017-06-05 15:39:05 +03:00
|
|
|
bdrv_dirty_bitmap_lock(s->dirty_bitmap);
|
2017-07-07 15:44:51 +03:00
|
|
|
while (nb_chunks * s->granularity < s->buf_size) {
|
2016-10-14 00:58:21 +03:00
|
|
|
int64_t next_dirty;
|
2017-07-07 15:44:51 +03:00
|
|
|
int64_t next_offset = offset + nb_chunks * s->granularity;
|
|
|
|
int64_t next_chunk = next_offset / s->granularity;
|
|
|
|
if (next_offset >= s->bdev_length ||
|
2017-09-25 17:55:19 +03:00
|
|
|
!bdrv_get_dirty_locked(source, s->dirty_bitmap, next_offset)) {
|
2016-02-05 05:00:29 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (test_bit(next_chunk, s->in_flight_bitmap)) {
|
2016-04-20 01:59:47 +03:00
|
|
|
break;
|
2016-02-05 05:00:29 +03:00
|
|
|
}
|
2016-04-20 01:59:47 +03:00
|
|
|
|
2017-09-25 17:55:17 +03:00
|
|
|
next_dirty = bdrv_dirty_iter_next(s->dbi);
|
2017-07-07 15:44:51 +03:00
|
|
|
if (next_dirty > next_offset || next_dirty < 0) {
|
2016-04-20 01:59:48 +03:00
|
|
|
/* The bitmap iterator's cache is stale, refresh it */
|
2017-09-25 17:55:16 +03:00
|
|
|
bdrv_set_dirty_iter(s->dbi, next_offset);
|
2017-09-25 17:55:17 +03:00
|
|
|
next_dirty = bdrv_dirty_iter_next(s->dbi);
|
2016-04-20 01:59:48 +03:00
|
|
|
}
|
2017-07-07 15:44:51 +03:00
|
|
|
assert(next_dirty == next_offset);
|
2016-04-20 01:59:47 +03:00
|
|
|
nb_chunks++;
|
2016-02-05 05:00:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Clear dirty bits before querying the block status, because
|
block: Convert bdrv_get_block_status_above() to bytes
We are gradually moving away from sector-based interfaces, towards
byte-based. In the common case, allocation is unlikely to ever use
values that are not naturally sector-aligned, but it is possible
that byte-based values will let us be more precise about allocation
at the end of an unaligned file that can do byte-based access.
Changing the name of the function from bdrv_get_block_status_above()
to bdrv_block_status_above() ensures that the compiler enforces that
all callers are updated. Likewise, since it a byte interface allows
an offset mapping that might not be sector aligned, split the mapping
out of the return value and into a pass-by-reference parameter. For
now, the io.c layer still assert()s that all uses are sector-aligned,
but that can be relaxed when a later patch implements byte-based
block status in the drivers.
For the most part this patch is just the addition of scaling at the
callers followed by inverse scaling at bdrv_block_status(), plus
updates for the new split return interface. But some code,
particularly bdrv_block_status(), gets a lot simpler because it no
longer has to mess with sectors. Likewise, mirror code no longer
computes s->granularity >> BDRV_SECTOR_BITS, and can therefore drop
an assertion about alignment because the loop no longer depends on
alignment (never mind that we don't really have a driver that
reports sub-sector alignments, so it's not really possible to test
the effect of sub-sector mirroring). Fix a neighboring assertion to
use is_power_of_2 while there.
For ease of review, bdrv_get_block_status() was tackled separately.
Signed-off-by: Eric Blake <eblake@redhat.com>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
2017-10-12 06:47:08 +03:00
|
|
|
* calling bdrv_block_status_above could yield - if some blocks are
|
2016-02-05 05:00:29 +03:00
|
|
|
* marked dirty in this window, we need to know.
|
|
|
|
*/
|
2017-09-25 17:55:20 +03:00
|
|
|
bdrv_reset_dirty_bitmap_locked(s->dirty_bitmap, offset,
|
|
|
|
nb_chunks * s->granularity);
|
2017-06-05 15:39:05 +03:00
|
|
|
bdrv_dirty_bitmap_unlock(s->dirty_bitmap);
|
|
|
|
|
2017-07-07 15:44:51 +03:00
|
|
|
bitmap_set(s->in_flight_bitmap, offset / s->granularity, nb_chunks);
|
|
|
|
while (nb_chunks > 0 && offset < s->bdev_length) {
|
block: Convert bdrv_get_block_status_above() to bytes
We are gradually moving away from sector-based interfaces, towards
byte-based. In the common case, allocation is unlikely to ever use
values that are not naturally sector-aligned, but it is possible
that byte-based values will let us be more precise about allocation
at the end of an unaligned file that can do byte-based access.
Changing the name of the function from bdrv_get_block_status_above()
to bdrv_block_status_above() ensures that the compiler enforces that
all callers are updated. Likewise, since it a byte interface allows
an offset mapping that might not be sector aligned, split the mapping
out of the return value and into a pass-by-reference parameter. For
now, the io.c layer still assert()s that all uses are sector-aligned,
but that can be relaxed when a later patch implements byte-based
block status in the drivers.
For the most part this patch is just the addition of scaling at the
callers followed by inverse scaling at bdrv_block_status(), plus
updates for the new split return interface. But some code,
particularly bdrv_block_status(), gets a lot simpler because it no
longer has to mess with sectors. Likewise, mirror code no longer
computes s->granularity >> BDRV_SECTOR_BITS, and can therefore drop
an assertion about alignment because the loop no longer depends on
alignment (never mind that we don't really have a driver that
reports sub-sector alignments, so it's not really possible to test
the effect of sub-sector mirroring). Fix a neighboring assertion to
use is_power_of_2 while there.
For ease of review, bdrv_get_block_status() was tackled separately.
Signed-off-by: Eric Blake <eblake@redhat.com>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
2017-10-12 06:47:08 +03:00
|
|
|
int ret;
|
2017-10-12 06:46:59 +03:00
|
|
|
int64_t io_bytes;
|
2017-07-07 15:44:39 +03:00
|
|
|
int64_t io_bytes_acct;
|
2016-02-05 05:00:29 +03:00
|
|
|
enum MirrorMethod {
|
|
|
|
MIRROR_METHOD_COPY,
|
|
|
|
MIRROR_METHOD_ZERO,
|
|
|
|
MIRROR_METHOD_DISCARD
|
|
|
|
} mirror_method = MIRROR_METHOD_COPY;
|
|
|
|
|
2017-07-07 15:44:51 +03:00
|
|
|
assert(!(offset % s->granularity));
|
block: Convert bdrv_get_block_status_above() to bytes
We are gradually moving away from sector-based interfaces, towards
byte-based. In the common case, allocation is unlikely to ever use
values that are not naturally sector-aligned, but it is possible
that byte-based values will let us be more precise about allocation
at the end of an unaligned file that can do byte-based access.
Changing the name of the function from bdrv_get_block_status_above()
to bdrv_block_status_above() ensures that the compiler enforces that
all callers are updated. Likewise, since it a byte interface allows
an offset mapping that might not be sector aligned, split the mapping
out of the return value and into a pass-by-reference parameter. For
now, the io.c layer still assert()s that all uses are sector-aligned,
but that can be relaxed when a later patch implements byte-based
block status in the drivers.
For the most part this patch is just the addition of scaling at the
callers followed by inverse scaling at bdrv_block_status(), plus
updates for the new split return interface. But some code,
particularly bdrv_block_status(), gets a lot simpler because it no
longer has to mess with sectors. Likewise, mirror code no longer
computes s->granularity >> BDRV_SECTOR_BITS, and can therefore drop
an assertion about alignment because the loop no longer depends on
alignment (never mind that we don't really have a driver that
reports sub-sector alignments, so it's not really possible to test
the effect of sub-sector mirroring). Fix a neighboring assertion to
use is_power_of_2 while there.
For ease of review, bdrv_get_block_status() was tackled separately.
Signed-off-by: Eric Blake <eblake@redhat.com>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
2017-10-12 06:47:08 +03:00
|
|
|
ret = bdrv_block_status_above(source, NULL, offset,
|
|
|
|
nb_chunks * s->granularity,
|
|
|
|
&io_bytes, NULL, NULL);
|
2016-02-05 05:00:29 +03:00
|
|
|
if (ret < 0) {
|
2017-07-07 15:44:51 +03:00
|
|
|
io_bytes = MIN(nb_chunks * s->granularity, max_io_bytes);
|
2016-07-14 20:19:01 +03:00
|
|
|
} else if (ret & BDRV_BLOCK_DATA) {
|
2017-07-07 15:44:51 +03:00
|
|
|
io_bytes = MIN(io_bytes, max_io_bytes);
|
2016-02-05 05:00:29 +03:00
|
|
|
}
|
|
|
|
|
2017-07-07 15:44:51 +03:00
|
|
|
io_bytes -= io_bytes % s->granularity;
|
|
|
|
if (io_bytes < s->granularity) {
|
|
|
|
io_bytes = s->granularity;
|
2016-02-05 05:00:29 +03:00
|
|
|
} else if (ret >= 0 && !(ret & BDRV_BLOCK_DATA)) {
|
2017-07-07 15:44:51 +03:00
|
|
|
int64_t target_offset;
|
2017-10-12 06:46:59 +03:00
|
|
|
int64_t target_bytes;
|
2017-07-07 15:44:51 +03:00
|
|
|
bdrv_round_to_clusters(blk_bs(s->target), offset, io_bytes,
|
|
|
|
&target_offset, &target_bytes);
|
|
|
|
if (target_offset == offset &&
|
|
|
|
target_bytes == io_bytes) {
|
2016-02-05 05:00:29 +03:00
|
|
|
mirror_method = ret & BDRV_BLOCK_ZERO ?
|
|
|
|
MIRROR_METHOD_ZERO :
|
|
|
|
MIRROR_METHOD_DISCARD;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-22 15:35:27 +03:00
|
|
|
while (s->in_flight >= MAX_IN_FLIGHT) {
|
2017-07-07 15:44:51 +03:00
|
|
|
trace_mirror_yield_in_flight(s, offset, s->in_flight);
|
2016-06-22 15:35:27 +03:00
|
|
|
mirror_wait_for_io(s);
|
|
|
|
}
|
|
|
|
|
2016-08-03 15:56:44 +03:00
|
|
|
if (s->ret < 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-07-07 15:44:51 +03:00
|
|
|
io_bytes = mirror_clip_bytes(s, offset, io_bytes);
|
2016-02-05 05:00:29 +03:00
|
|
|
switch (mirror_method) {
|
|
|
|
case MIRROR_METHOD_COPY:
|
2017-07-07 15:44:51 +03:00
|
|
|
io_bytes = io_bytes_acct = mirror_do_read(s, offset, io_bytes);
|
2016-02-05 05:00:29 +03:00
|
|
|
break;
|
|
|
|
case MIRROR_METHOD_ZERO:
|
|
|
|
case MIRROR_METHOD_DISCARD:
|
2017-07-07 15:44:51 +03:00
|
|
|
mirror_do_zero_or_discard(s, offset, io_bytes,
|
2016-07-14 16:33:29 +03:00
|
|
|
mirror_method == MIRROR_METHOD_DISCARD);
|
|
|
|
if (write_zeroes_ok) {
|
2017-07-07 15:44:39 +03:00
|
|
|
io_bytes_acct = 0;
|
2016-07-14 16:33:29 +03:00
|
|
|
} else {
|
2017-07-07 15:44:51 +03:00
|
|
|
io_bytes_acct = io_bytes;
|
2016-07-14 16:33:29 +03:00
|
|
|
}
|
2016-02-05 05:00:29 +03:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
abort();
|
|
|
|
}
|
2017-07-07 15:44:51 +03:00
|
|
|
assert(io_bytes);
|
|
|
|
offset += io_bytes;
|
|
|
|
nb_chunks -= DIV_ROUND_UP(io_bytes, s->granularity);
|
Improve block job rate limiting for small bandwidth values
ratelimit_calculate_delay() previously reset the accounting every time
slice, no matter how much data had been processed before. This had (at
least) two consequences:
1. The minimum speed is rather large, e.g. 5 MiB/s for commit and stream.
Not sure if there are real-world use cases where this would be a
problem. Mirroring and backup over a slow link (e.g. DSL) would
come to mind, though.
2. Tests for block job operations (e.g. cancel) were rather racy
All block jobs currently use a time slice of 100ms. That's a
reasonable value to get smooth output during regular
operation. However this also meant that the state of block jobs
changed every 100ms, no matter how low the configured limit was. On
busy hosts, qemu often transferred additional chunks until the test
case had a chance to cancel the job.
Fix the block job rate limit code to delay for more than one time
slice to address the above issues. To make it easier to handle
oversized chunks we switch the semantics from returning a delay
_before_ the current request to a delay _after_ the current
request. If necessary, this delay consists of multiple time slice
units.
Since the mirror job sends multiple chunks in one go even if the rate
limit was exceeded in between, we need to keep track of the start of
the current time slice so we can correctly re-compute the delay for
the updated amount of data.
The minimum bandwidth now is 1 data unit per time slice. The block
jobs are currently passing the amount of data transferred in sectors
and using 100ms time slices, so this translates to 5120
bytes/second. With chunk sizes usually being O(512KiB), tests have
plenty of time (O(100s)) to operate on block jobs. The chance of a
race condition now is fairly remote, except possibly on insanely
loaded systems.
Signed-off-by: Sascha Silbe <silbe@linux.vnet.ibm.com>
Message-id: 1467127721-9564-2-git-send-email-silbe@linux.vnet.ibm.com
Reviewed-by: Max Reitz <mreitz@redhat.com>
Signed-off-by: Max Reitz <mreitz@redhat.com>
2016-06-28 18:28:41 +03:00
|
|
|
if (s->common.speed) {
|
2017-07-07 15:44:39 +03:00
|
|
|
delay_ns = ratelimit_calculate_delay(&s->limit, io_bytes_acct);
|
Improve block job rate limiting for small bandwidth values
ratelimit_calculate_delay() previously reset the accounting every time
slice, no matter how much data had been processed before. This had (at
least) two consequences:
1. The minimum speed is rather large, e.g. 5 MiB/s for commit and stream.
Not sure if there are real-world use cases where this would be a
problem. Mirroring and backup over a slow link (e.g. DSL) would
come to mind, though.
2. Tests for block job operations (e.g. cancel) were rather racy
All block jobs currently use a time slice of 100ms. That's a
reasonable value to get smooth output during regular
operation. However this also meant that the state of block jobs
changed every 100ms, no matter how low the configured limit was. On
busy hosts, qemu often transferred additional chunks until the test
case had a chance to cancel the job.
Fix the block job rate limit code to delay for more than one time
slice to address the above issues. To make it easier to handle
oversized chunks we switch the semantics from returning a delay
_before_ the current request to a delay _after_ the current
request. If necessary, this delay consists of multiple time slice
units.
Since the mirror job sends multiple chunks in one go even if the rate
limit was exceeded in between, we need to keep track of the start of
the current time slice so we can correctly re-compute the delay for
the updated amount of data.
The minimum bandwidth now is 1 data unit per time slice. The block
jobs are currently passing the amount of data transferred in sectors
and using 100ms time slices, so this translates to 5120
bytes/second. With chunk sizes usually being O(512KiB), tests have
plenty of time (O(100s)) to operate on block jobs. The chance of a
race condition now is fairly remote, except possibly on insanely
loaded systems.
Signed-off-by: Sascha Silbe <silbe@linux.vnet.ibm.com>
Message-id: 1467127721-9564-2-git-send-email-silbe@linux.vnet.ibm.com
Reviewed-by: Max Reitz <mreitz@redhat.com>
Signed-off-by: Max Reitz <mreitz@redhat.com>
2016-06-28 18:28:41 +03:00
|
|
|
}
|
2015-06-08 08:56:09 +03:00
|
|
|
}
|
2014-03-21 16:55:18 +04:00
|
|
|
return delay_ns;
|
2013-01-22 12:03:12 +04:00
|
|
|
}
|
2012-10-18 18:49:28 +04:00
|
|
|
|
2013-01-22 12:03:14 +04:00
|
|
|
static void mirror_free_init(MirrorBlockJob *s)
|
|
|
|
{
|
|
|
|
int granularity = s->granularity;
|
|
|
|
size_t buf_size = s->buf_size;
|
|
|
|
uint8_t *buf = s->buf;
|
|
|
|
|
|
|
|
assert(s->buf_free_count == 0);
|
|
|
|
QSIMPLEQ_INIT(&s->buf_free);
|
|
|
|
while (buf_size != 0) {
|
|
|
|
MirrorBuffer *cur = (MirrorBuffer *)buf;
|
|
|
|
QSIMPLEQ_INSERT_TAIL(&s->buf_free, cur, next);
|
|
|
|
s->buf_free_count++;
|
|
|
|
buf_size -= granularity;
|
|
|
|
buf += granularity;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-27 13:48:50 +03:00
|
|
|
/* This is also used for the .pause callback. There is no matching
|
|
|
|
* mirror_resume() because mirror_run() will begin iterating again
|
|
|
|
* when the job is resumed.
|
|
|
|
*/
|
|
|
|
static void mirror_wait_for_all_io(MirrorBlockJob *s)
|
2013-01-22 12:03:12 +04:00
|
|
|
{
|
|
|
|
while (s->in_flight > 0) {
|
2016-02-05 05:00:30 +03:00
|
|
|
mirror_wait_for_io(s);
|
2013-01-22 12:03:12 +04:00
|
|
|
}
|
2012-10-18 18:49:23 +04:00
|
|
|
}
|
|
|
|
|
2014-10-21 15:03:58 +04:00
|
|
|
typedef struct {
|
|
|
|
int ret;
|
|
|
|
} MirrorExitData;
|
|
|
|
|
|
|
|
static void mirror_exit(BlockJob *job, void *opaque)
|
|
|
|
{
|
|
|
|
MirrorBlockJob *s = container_of(job, MirrorBlockJob, common);
|
|
|
|
MirrorExitData *data = opaque;
|
|
|
|
AioContext *replace_aio_context = NULL;
|
2017-01-25 21:16:34 +03:00
|
|
|
BlockDriverState *src = s->source;
|
2016-04-12 17:17:41 +03:00
|
|
|
BlockDriverState *target_bs = blk_bs(s->target);
|
2017-01-25 21:16:34 +03:00
|
|
|
BlockDriverState *mirror_top_bs = s->mirror_top_bs;
|
2017-02-17 22:42:32 +03:00
|
|
|
Error *local_err = NULL;
|
2015-09-15 12:58:23 +03:00
|
|
|
|
2017-06-05 15:39:03 +03:00
|
|
|
bdrv_release_dirty_bitmap(src, s->dirty_bitmap);
|
|
|
|
|
2015-09-15 12:58:23 +03:00
|
|
|
/* Make sure that the source BDS doesn't go away before we called
|
|
|
|
* block_job_completed(). */
|
|
|
|
bdrv_ref(src);
|
2017-01-25 21:16:34 +03:00
|
|
|
bdrv_ref(mirror_top_bs);
|
2017-03-02 19:48:14 +03:00
|
|
|
bdrv_ref(target_bs);
|
|
|
|
|
|
|
|
/* Remove target parent that still uses BLK_PERM_WRITE/RESIZE before
|
|
|
|
* inserting target_bs at s->to_replace, where we might not be able to get
|
2017-05-29 15:08:32 +03:00
|
|
|
* these permissions.
|
|
|
|
*
|
|
|
|
* Note that blk_unref() alone doesn't necessarily drop permissions because
|
|
|
|
* we might be running nested inside mirror_drain(), which takes an extra
|
|
|
|
* reference, so use an explicit blk_set_perm() first. */
|
|
|
|
blk_set_perm(s->target, 0, BLK_PERM_ALL, &error_abort);
|
2017-03-02 19:48:14 +03:00
|
|
|
blk_unref(s->target);
|
|
|
|
s->target = NULL;
|
2017-01-25 21:16:34 +03:00
|
|
|
|
|
|
|
/* We don't access the source any more. Dropping any WRITE/RESIZE is
|
|
|
|
* required before it could become a backing file of target_bs. */
|
|
|
|
bdrv_child_try_set_perm(mirror_top_bs->backing, 0, BLK_PERM_ALL,
|
|
|
|
&error_abort);
|
|
|
|
if (s->backing_mode == MIRROR_SOURCE_BACKING_CHAIN) {
|
|
|
|
BlockDriverState *backing = s->is_none_mode ? src : s->base;
|
|
|
|
if (backing_bs(target_bs) != backing) {
|
2017-02-17 22:42:32 +03:00
|
|
|
bdrv_set_backing_hd(target_bs, backing, &local_err);
|
|
|
|
if (local_err) {
|
|
|
|
error_report_err(local_err);
|
|
|
|
data->ret = -EPERM;
|
|
|
|
}
|
2017-01-25 21:16:34 +03:00
|
|
|
}
|
|
|
|
}
|
2014-10-21 15:03:58 +04:00
|
|
|
|
|
|
|
if (s->to_replace) {
|
|
|
|
replace_aio_context = bdrv_get_aio_context(s->to_replace);
|
|
|
|
aio_context_acquire(replace_aio_context);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (s->should_complete && data->ret == 0) {
|
2016-04-12 17:17:41 +03:00
|
|
|
BlockDriverState *to_replace = src;
|
2014-10-21 15:03:58 +04:00
|
|
|
if (s->to_replace) {
|
|
|
|
to_replace = s->to_replace;
|
|
|
|
}
|
2015-10-28 15:24:26 +03:00
|
|
|
|
2016-04-12 17:17:41 +03:00
|
|
|
if (bdrv_get_flags(target_bs) != bdrv_get_flags(to_replace)) {
|
|
|
|
bdrv_reopen(target_bs, bdrv_get_flags(to_replace), NULL);
|
2014-10-21 15:03:58 +04:00
|
|
|
}
|
2016-04-12 17:20:59 +03:00
|
|
|
|
|
|
|
/* The mirror job has no requests in flight any more, but we need to
|
|
|
|
* drain potential other users of the BDS before changing the graph. */
|
2016-04-12 17:17:41 +03:00
|
|
|
bdrv_drained_begin(target_bs);
|
2017-03-06 18:20:51 +03:00
|
|
|
bdrv_replace_node(to_replace, target_bs, &local_err);
|
2016-04-12 17:17:41 +03:00
|
|
|
bdrv_drained_end(target_bs);
|
2017-03-06 18:20:51 +03:00
|
|
|
if (local_err) {
|
|
|
|
error_report_err(local_err);
|
|
|
|
data->ret = -EPERM;
|
|
|
|
}
|
2014-10-21 15:03:58 +04:00
|
|
|
}
|
|
|
|
if (s->to_replace) {
|
|
|
|
bdrv_op_unblock_all(s->to_replace, s->replace_blocker);
|
|
|
|
error_free(s->replace_blocker);
|
|
|
|
bdrv_unref(s->to_replace);
|
|
|
|
}
|
|
|
|
if (replace_aio_context) {
|
|
|
|
aio_context_release(replace_aio_context);
|
|
|
|
}
|
|
|
|
g_free(s->replaces);
|
2017-03-02 19:48:14 +03:00
|
|
|
bdrv_unref(target_bs);
|
2017-01-25 21:16:34 +03:00
|
|
|
|
|
|
|
/* Remove the mirror filter driver from the graph. Before this, get rid of
|
|
|
|
* the blockers on the intermediate nodes so that the resulting state is
|
2017-03-06 18:03:00 +03:00
|
|
|
* valid. Also give up permissions on mirror_top_bs->backing, which might
|
|
|
|
* block the removal. */
|
2017-01-25 21:16:34 +03:00
|
|
|
block_job_remove_all_bdrv(job);
|
2017-03-14 05:30:50 +03:00
|
|
|
bdrv_child_try_set_perm(mirror_top_bs->backing, 0, BLK_PERM_ALL,
|
|
|
|
&error_abort);
|
2017-03-06 18:20:51 +03:00
|
|
|
bdrv_replace_node(mirror_top_bs, backing_bs(mirror_top_bs), &error_abort);
|
2017-01-25 21:16:34 +03:00
|
|
|
|
|
|
|
/* We just changed the BDS the job BB refers to (with either or both of the
|
2017-03-06 18:20:51 +03:00
|
|
|
* bdrv_replace_node() calls), so switch the BB back so the cleanup does
|
|
|
|
* the right thing. We don't need any permissions any more now. */
|
2017-01-25 21:16:34 +03:00
|
|
|
blk_remove_bs(job->blk);
|
|
|
|
blk_set_perm(job->blk, 0, BLK_PERM_ALL, &error_abort);
|
|
|
|
blk_insert_bs(job->blk, mirror_top_bs, &error_abort);
|
|
|
|
|
2014-10-21 15:03:58 +04:00
|
|
|
block_job_completed(&s->common, data->ret);
|
2017-01-25 21:16:34 +03:00
|
|
|
|
2014-10-21 15:03:58 +04:00
|
|
|
g_free(data);
|
2015-11-23 05:28:04 +03:00
|
|
|
bdrv_drained_end(src);
|
2017-01-25 21:16:34 +03:00
|
|
|
bdrv_unref(mirror_top_bs);
|
2015-09-15 12:58:23 +03:00
|
|
|
bdrv_unref(src);
|
2014-10-21 15:03:58 +04:00
|
|
|
}
|
|
|
|
|
2016-07-14 16:33:24 +03:00
|
|
|
static void mirror_throttle(MirrorBlockJob *s)
|
|
|
|
{
|
|
|
|
int64_t now = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
|
|
|
|
|
|
|
|
if (now - s->last_pause_ns > SLICE_TIME) {
|
|
|
|
s->last_pause_ns = now;
|
2017-11-29 13:25:11 +03:00
|
|
|
block_job_sleep_ns(&s->common, 0);
|
2016-07-14 16:33:24 +03:00
|
|
|
} else {
|
|
|
|
block_job_pause_point(&s->common);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-14 16:33:25 +03:00
|
|
|
static int coroutine_fn mirror_dirty_init(MirrorBlockJob *s)
|
|
|
|
{
|
2017-09-25 17:55:21 +03:00
|
|
|
int64_t offset;
|
2016-07-14 16:33:25 +03:00
|
|
|
BlockDriverState *base = s->base;
|
2017-01-25 21:16:34 +03:00
|
|
|
BlockDriverState *bs = s->source;
|
2016-07-14 16:33:25 +03:00
|
|
|
BlockDriverState *target_bs = blk_bs(s->target);
|
2017-09-25 17:55:21 +03:00
|
|
|
int ret;
|
block: Make bdrv_is_allocated_above() byte-based
We are gradually moving away from sector-based interfaces, towards
byte-based. In the common case, allocation is unlikely to ever use
values that are not naturally sector-aligned, but it is possible
that byte-based values will let us be more precise about allocation
at the end of an unaligned file that can do byte-based access.
Changing the signature of the function to use int64_t *pnum ensures
that the compiler enforces that all callers are updated. For now,
the io.c layer still assert()s that all callers are sector-aligned,
but that can be relaxed when a later patch implements byte-based
block status. Therefore, for the most part this patch is just the
addition of scaling at the callers followed by inverse scaling at
bdrv_is_allocated(). But some code, particularly stream_run(),
gets a lot simpler because it no longer has to mess with sectors.
Leave comments where we can further simplify by switching to
byte-based iterations, once later patches eliminate the need for
sector-aligned operations.
For ease of review, bdrv_is_allocated() was tackled separately.
Signed-off-by: Eric Blake <eblake@redhat.com>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
2017-07-07 15:44:59 +03:00
|
|
|
int64_t count;
|
2016-07-14 16:33:25 +03:00
|
|
|
|
2016-07-14 16:33:27 +03:00
|
|
|
if (base == NULL && !bdrv_has_zero_init(target_bs)) {
|
2016-07-14 16:33:28 +03:00
|
|
|
if (!bdrv_can_write_zeroes_with_unmap(target_bs)) {
|
2017-09-25 17:55:20 +03:00
|
|
|
bdrv_set_dirty_bitmap(s->dirty_bitmap, 0, s->bdev_length);
|
2016-07-14 16:33:28 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-02-02 17:25:15 +03:00
|
|
|
s->initial_zeroing_ongoing = true;
|
2017-09-25 17:55:21 +03:00
|
|
|
for (offset = 0; offset < s->bdev_length; ) {
|
|
|
|
int bytes = MIN(s->bdev_length - offset,
|
|
|
|
QEMU_ALIGN_DOWN(INT_MAX, s->granularity));
|
2016-07-14 16:33:28 +03:00
|
|
|
|
|
|
|
mirror_throttle(s);
|
|
|
|
|
|
|
|
if (block_job_is_cancelled(&s->common)) {
|
2017-02-02 17:25:15 +03:00
|
|
|
s->initial_zeroing_ongoing = false;
|
2016-07-14 16:33:28 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (s->in_flight >= MAX_IN_FLIGHT) {
|
2017-03-13 22:55:18 +03:00
|
|
|
trace_mirror_yield(s, UINT64_MAX, s->buf_free_count,
|
|
|
|
s->in_flight);
|
2016-07-14 16:33:28 +03:00
|
|
|
mirror_wait_for_io(s);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-09-25 17:55:21 +03:00
|
|
|
mirror_do_zero_or_discard(s, offset, bytes, false);
|
|
|
|
offset += bytes;
|
2016-07-14 16:33:28 +03:00
|
|
|
}
|
|
|
|
|
2016-10-27 13:48:50 +03:00
|
|
|
mirror_wait_for_all_io(s);
|
2017-02-02 17:25:15 +03:00
|
|
|
s->initial_zeroing_ongoing = false;
|
2016-07-14 16:33:27 +03:00
|
|
|
}
|
|
|
|
|
2016-07-14 16:33:25 +03:00
|
|
|
/* First part, loop on the sectors and initialize the dirty bitmap. */
|
2017-09-25 17:55:21 +03:00
|
|
|
for (offset = 0; offset < s->bdev_length; ) {
|
2016-07-14 16:33:25 +03:00
|
|
|
/* Just to make sure we are not exceeding int limit. */
|
2017-09-25 17:55:21 +03:00
|
|
|
int bytes = MIN(s->bdev_length - offset,
|
|
|
|
QEMU_ALIGN_DOWN(INT_MAX, s->granularity));
|
2016-07-14 16:33:25 +03:00
|
|
|
|
|
|
|
mirror_throttle(s);
|
|
|
|
|
|
|
|
if (block_job_is_cancelled(&s->common)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-09-25 17:55:21 +03:00
|
|
|
ret = bdrv_is_allocated_above(bs, base, offset, bytes, &count);
|
2016-07-14 16:33:25 +03:00
|
|
|
if (ret < 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-09-25 17:55:21 +03:00
|
|
|
assert(count);
|
2016-07-14 16:33:27 +03:00
|
|
|
if (ret == 1) {
|
2017-09-25 17:55:21 +03:00
|
|
|
bdrv_set_dirty_bitmap(s->dirty_bitmap, offset, count);
|
2016-07-14 16:33:25 +03:00
|
|
|
}
|
2017-09-25 17:55:21 +03:00
|
|
|
offset += count;
|
2016-07-14 16:33:25 +03:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-11-09 19:20:08 +03:00
|
|
|
/* Called when going out of the streaming phase to flush the bulk of the
|
|
|
|
* data to the medium, or just before completing.
|
|
|
|
*/
|
|
|
|
static int mirror_flush(MirrorBlockJob *s)
|
|
|
|
{
|
|
|
|
int ret = blk_flush(s->target);
|
|
|
|
if (ret < 0) {
|
|
|
|
if (mirror_error_action(s, false, -ret) == BLOCK_ERROR_ACTION_REPORT) {
|
|
|
|
s->ret = ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-10-18 18:49:23 +04:00
|
|
|
static void coroutine_fn mirror_run(void *opaque)
|
|
|
|
{
|
|
|
|
MirrorBlockJob *s = opaque;
|
2014-10-21 15:03:58 +04:00
|
|
|
MirrorExitData *data;
|
2017-01-25 21:16:34 +03:00
|
|
|
BlockDriverState *bs = s->source;
|
2016-04-12 17:17:41 +03:00
|
|
|
BlockDriverState *target_bs = blk_bs(s->target);
|
2016-10-27 13:48:51 +03:00
|
|
|
bool need_drain = true;
|
2016-07-14 16:33:25 +03:00
|
|
|
int64_t length;
|
2013-01-21 20:09:43 +04:00
|
|
|
BlockDriverInfo bdi;
|
2015-01-22 16:03:29 +03:00
|
|
|
char backing_filename[2]; /* we only need 2 characters because we are only
|
|
|
|
checking for a NULL string */
|
2012-10-18 18:49:23 +04:00
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
if (block_job_is_cancelled(&s->common)) {
|
|
|
|
goto immediate_exit;
|
|
|
|
}
|
|
|
|
|
2014-10-24 17:57:36 +04:00
|
|
|
s->bdev_length = bdrv_getlength(bs);
|
|
|
|
if (s->bdev_length < 0) {
|
|
|
|
ret = s->bdev_length;
|
2014-04-29 14:09:09 +04:00
|
|
|
goto immediate_exit;
|
2017-02-17 13:11:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Active commit must resize the base image if its size differs from the
|
|
|
|
* active layer. */
|
|
|
|
if (s->base == blk_bs(s->target)) {
|
|
|
|
int64_t base_length;
|
|
|
|
|
|
|
|
base_length = blk_getlength(s->target);
|
|
|
|
if (base_length < 0) {
|
|
|
|
ret = base_length;
|
|
|
|
goto immediate_exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (s->bdev_length > base_length) {
|
2017-06-13 23:20:54 +03:00
|
|
|
ret = blk_truncate(s->target, s->bdev_length, PREALLOC_MODE_OFF,
|
|
|
|
NULL);
|
2017-02-17 13:11:28 +03:00
|
|
|
if (ret < 0) {
|
|
|
|
goto immediate_exit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (s->bdev_length == 0) {
|
2014-06-24 16:26:36 +04:00
|
|
|
/* Report BLOCK_JOB_READY and wait for complete. */
|
|
|
|
block_job_event_ready(&s->common);
|
|
|
|
s->synced = true;
|
|
|
|
while (!block_job_is_cancelled(&s->common) && !s->should_complete) {
|
|
|
|
block_job_yield(&s->common);
|
|
|
|
}
|
|
|
|
s->common.cancelled = false;
|
|
|
|
goto immediate_exit;
|
2012-10-18 18:49:23 +04:00
|
|
|
}
|
|
|
|
|
2014-10-24 17:57:36 +04:00
|
|
|
length = DIV_ROUND_UP(s->bdev_length, s->granularity);
|
2013-01-22 12:03:14 +04:00
|
|
|
s->in_flight_bitmap = bitmap_new(length);
|
|
|
|
|
2013-01-21 20:09:43 +04:00
|
|
|
/* If we have no backing file yet in the destination, we cannot let
|
|
|
|
* the destination do COW. Instead, we copy sectors around the
|
|
|
|
* dirty data if needed. We need a bitmap to do that.
|
|
|
|
*/
|
2016-04-12 17:17:41 +03:00
|
|
|
bdrv_get_backing_filename(target_bs, backing_filename,
|
2013-01-21 20:09:43 +04:00
|
|
|
sizeof(backing_filename));
|
2016-04-12 17:17:41 +03:00
|
|
|
if (!bdrv_get_info(target_bs, &bdi) && bdi.cluster_size) {
|
2017-07-07 15:44:46 +03:00
|
|
|
s->target_cluster_size = bdi.cluster_size;
|
|
|
|
} else {
|
|
|
|
s->target_cluster_size = BDRV_SECTOR_SIZE;
|
2016-02-05 05:00:29 +03:00
|
|
|
}
|
2017-07-07 15:44:46 +03:00
|
|
|
if (backing_filename[0] && !target_bs->backing &&
|
|
|
|
s->granularity < s->target_cluster_size) {
|
|
|
|
s->buf_size = MAX(s->buf_size, s->target_cluster_size);
|
2016-02-05 05:00:29 +03:00
|
|
|
s->cow_bitmap = bitmap_new(length);
|
2013-01-21 20:09:43 +04:00
|
|
|
}
|
2016-04-12 17:17:41 +03:00
|
|
|
s->max_iov = MIN(bs->bl.max_iov, target_bs->bl.max_iov);
|
2013-01-21 20:09:43 +04:00
|
|
|
|
2014-05-21 20:16:21 +04:00
|
|
|
s->buf = qemu_try_blockalign(bs, s->buf_size);
|
|
|
|
if (s->buf == NULL) {
|
|
|
|
ret = -ENOMEM;
|
|
|
|
goto immediate_exit;
|
|
|
|
}
|
|
|
|
|
2013-01-22 12:03:14 +04:00
|
|
|
mirror_free_init(s);
|
2012-10-18 18:49:23 +04:00
|
|
|
|
2016-07-14 16:33:24 +03:00
|
|
|
s->last_pause_ns = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
|
2013-12-16 10:45:30 +04:00
|
|
|
if (!s->is_none_mode) {
|
2016-07-14 16:33:25 +03:00
|
|
|
ret = mirror_dirty_init(s);
|
|
|
|
if (ret < 0 || block_job_is_cancelled(&s->common)) {
|
|
|
|
goto immediate_exit;
|
2012-10-18 18:49:23 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-14 00:58:21 +03:00
|
|
|
assert(!s->dbi);
|
2017-09-25 17:55:16 +03:00
|
|
|
s->dbi = bdrv_dirty_iter_new(s->dirty_bitmap);
|
2012-10-18 18:49:23 +04:00
|
|
|
for (;;) {
|
2014-03-21 16:55:18 +04:00
|
|
|
uint64_t delay_ns = 0;
|
2016-07-14 16:33:24 +03:00
|
|
|
int64_t cnt, delta;
|
2012-10-18 18:49:23 +04:00
|
|
|
bool should_complete;
|
|
|
|
|
2013-01-22 12:03:12 +04:00
|
|
|
if (s->ret < 0) {
|
|
|
|
ret = s->ret;
|
|
|
|
goto immediate_exit;
|
|
|
|
}
|
|
|
|
|
2016-06-16 19:56:28 +03:00
|
|
|
block_job_pause_point(&s->common);
|
|
|
|
|
2015-04-18 02:50:02 +03:00
|
|
|
cnt = bdrv_get_dirty_count(s->dirty_bitmap);
|
2014-10-24 17:57:36 +04:00
|
|
|
/* s->common.offset contains the number of bytes already processed so
|
2017-09-25 17:55:18 +03:00
|
|
|
* far, cnt is the number of dirty bytes remaining and
|
2017-07-07 15:44:46 +03:00
|
|
|
* s->bytes_in_flight is the number of bytes currently being
|
2014-10-24 17:57:36 +04:00
|
|
|
* processed; together those are the current total operation length */
|
2017-09-25 17:55:18 +03:00
|
|
|
s->common.len = s->common.offset + s->bytes_in_flight + cnt;
|
2013-01-22 12:03:12 +04:00
|
|
|
|
|
|
|
/* Note that even when no rate limit is applied we need to yield
|
2015-04-03 17:05:21 +03:00
|
|
|
* periodically with no pending I/O so that bdrv_drain_all() returns.
|
2013-01-22 12:03:12 +04:00
|
|
|
* We do so every SLICE_TIME nanoseconds, or when there is an error,
|
|
|
|
* or when the source is clean, whichever comes first.
|
|
|
|
*/
|
2016-07-14 16:33:24 +03:00
|
|
|
delta = qemu_clock_get_ns(QEMU_CLOCK_REALTIME) - s->last_pause_ns;
|
|
|
|
if (delta < SLICE_TIME &&
|
2013-01-22 12:03:12 +04:00
|
|
|
s->common.iostatus == BLOCK_DEVICE_IO_STATUS_OK) {
|
2016-06-22 15:35:27 +03:00
|
|
|
if (s->in_flight >= MAX_IN_FLIGHT || s->buf_free_count == 0 ||
|
2013-01-22 12:03:14 +04:00
|
|
|
(cnt == 0 && s->in_flight > 0)) {
|
2017-09-25 17:55:18 +03:00
|
|
|
trace_mirror_yield(s, cnt, s->buf_free_count, s->in_flight);
|
2016-02-05 05:00:30 +03:00
|
|
|
mirror_wait_for_io(s);
|
2013-01-22 12:03:12 +04:00
|
|
|
continue;
|
|
|
|
} else if (cnt != 0) {
|
2014-03-21 16:55:18 +04:00
|
|
|
delay_ns = mirror_iteration(s);
|
2012-10-18 18:49:23 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
should_complete = false;
|
2013-01-22 12:03:12 +04:00
|
|
|
if (s->in_flight == 0 && cnt == 0) {
|
2012-10-18 18:49:23 +04:00
|
|
|
trace_mirror_before_flush(s);
|
2016-11-09 19:20:08 +03:00
|
|
|
if (!s->synced) {
|
|
|
|
if (mirror_flush(s) < 0) {
|
|
|
|
/* Go check s->ret. */
|
|
|
|
continue;
|
2012-10-18 18:49:28 +04:00
|
|
|
}
|
|
|
|
/* We're out of the streaming phase. From now on, if the job
|
|
|
|
* is cancelled we will actually complete all pending I/O and
|
|
|
|
* report completion. This way, block-job-cancel will leave
|
|
|
|
* the target in a consistent state.
|
|
|
|
*/
|
2016-11-09 19:20:08 +03:00
|
|
|
block_job_event_ready(&s->common);
|
|
|
|
s->synced = true;
|
2012-10-18 18:49:25 +04:00
|
|
|
}
|
2016-11-09 19:20:08 +03:00
|
|
|
|
|
|
|
should_complete = s->should_complete ||
|
|
|
|
block_job_is_cancelled(&s->common);
|
|
|
|
cnt = bdrv_get_dirty_count(s->dirty_bitmap);
|
2012-10-18 18:49:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (cnt == 0 && should_complete) {
|
|
|
|
/* The dirty bitmap is not updated while operations are pending.
|
|
|
|
* If we're about to exit, wait for pending operations before
|
|
|
|
* calling bdrv_get_dirty_count(bs), or we may exit while the
|
|
|
|
* source has dirty data to copy!
|
|
|
|
*
|
|
|
|
* Note that I/O can be submitted by the guest while
|
2016-10-27 13:48:51 +03:00
|
|
|
* mirror_populate runs, so pause it now. Before deciding
|
|
|
|
* whether to switch to target check one last time if I/O has
|
|
|
|
* come in the meanwhile, and if not flush the data to disk.
|
2012-10-18 18:49:23 +04:00
|
|
|
*/
|
2017-09-25 17:55:18 +03:00
|
|
|
trace_mirror_before_drain(s, cnt);
|
2016-10-27 13:48:51 +03:00
|
|
|
|
|
|
|
bdrv_drained_begin(bs);
|
2015-04-18 02:50:02 +03:00
|
|
|
cnt = bdrv_get_dirty_count(s->dirty_bitmap);
|
2016-11-09 19:20:08 +03:00
|
|
|
if (cnt > 0 || mirror_flush(s) < 0) {
|
2016-10-27 13:48:51 +03:00
|
|
|
bdrv_drained_end(bs);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The two disks are in sync. Exit and report successful
|
|
|
|
* completion.
|
|
|
|
*/
|
|
|
|
assert(QLIST_EMPTY(&bs->tracked_requests));
|
|
|
|
s->common.cancelled = false;
|
|
|
|
need_drain = false;
|
|
|
|
break;
|
2012-10-18 18:49:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = 0;
|
2017-09-25 17:55:18 +03:00
|
|
|
trace_mirror_before_sleep(s, cnt, s->synced, delay_ns);
|
2012-10-18 18:49:25 +04:00
|
|
|
if (!s->synced) {
|
2017-11-29 13:25:11 +03:00
|
|
|
block_job_sleep_ns(&s->common, delay_ns);
|
2012-10-18 18:49:23 +04:00
|
|
|
if (block_job_is_cancelled(&s->common)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else if (!should_complete) {
|
2013-01-22 12:03:12 +04:00
|
|
|
delay_ns = (s->in_flight == 0 && cnt == 0 ? SLICE_TIME : 0);
|
2017-11-29 13:25:11 +03:00
|
|
|
block_job_sleep_ns(&s->common, delay_ns);
|
2012-10-18 18:49:23 +04:00
|
|
|
}
|
2016-07-14 16:33:24 +03:00
|
|
|
s->last_pause_ns = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
|
2012-10-18 18:49:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
immediate_exit:
|
2013-01-22 12:03:12 +04:00
|
|
|
if (s->in_flight > 0) {
|
|
|
|
/* We get here only if something went wrong. Either the job failed,
|
|
|
|
* or it was cancelled prematurely so that we do not guarantee that
|
|
|
|
* the target is a copy of the source.
|
|
|
|
*/
|
|
|
|
assert(ret < 0 || (!s->synced && block_job_is_cancelled(&s->common)));
|
2016-10-27 13:48:51 +03:00
|
|
|
assert(need_drain);
|
2016-10-27 13:48:50 +03:00
|
|
|
mirror_wait_for_all_io(s);
|
2013-01-22 12:03:12 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
assert(s->in_flight == 0);
|
2013-01-15 18:29:10 +04:00
|
|
|
qemu_vfree(s->buf);
|
2013-01-21 20:09:43 +04:00
|
|
|
g_free(s->cow_bitmap);
|
2013-01-22 12:03:14 +04:00
|
|
|
g_free(s->in_flight_bitmap);
|
2016-10-14 00:58:21 +03:00
|
|
|
bdrv_dirty_iter_free(s->dbi);
|
2014-10-21 15:03:58 +04:00
|
|
|
|
|
|
|
data = g_malloc(sizeof(*data));
|
|
|
|
data->ret = ret;
|
2016-10-27 13:48:51 +03:00
|
|
|
|
|
|
|
if (need_drain) {
|
|
|
|
bdrv_drained_begin(bs);
|
|
|
|
}
|
2014-10-21 15:03:58 +04:00
|
|
|
block_job_defer_to_main_loop(&s->common, mirror_exit, data);
|
2012-10-18 18:49:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void mirror_set_speed(BlockJob *job, int64_t speed, Error **errp)
|
|
|
|
{
|
|
|
|
MirrorBlockJob *s = container_of(job, MirrorBlockJob, common);
|
|
|
|
|
|
|
|
if (speed < 0) {
|
2015-03-17 13:54:50 +03:00
|
|
|
error_setg(errp, QERR_INVALID_PARAMETER, "speed");
|
2012-10-18 18:49:23 +04:00
|
|
|
return;
|
|
|
|
}
|
2017-07-07 15:44:39 +03:00
|
|
|
ratelimit_set_speed(&s->limit, speed, SLICE_TIME);
|
2012-10-18 18:49:23 +04:00
|
|
|
}
|
|
|
|
|
2012-10-18 18:49:25 +04:00
|
|
|
static void mirror_complete(BlockJob *job, Error **errp)
|
|
|
|
{
|
|
|
|
MirrorBlockJob *s = container_of(job, MirrorBlockJob, common);
|
2017-01-25 21:16:34 +03:00
|
|
|
BlockDriverState *target;
|
block/mirror: Fix target backing BDS
Currently, we are trying to move the backing BDS from the source to the
target in bdrv_replace_in_backing_chain() which is called from
mirror_exit(). However, mirror_complete() already tries to open the
target's backing chain with a call to bdrv_open_backing_file().
First, we should only set the target's backing BDS once. Second, the
mirroring block job has a better idea of what to set it to than the
generic code in bdrv_replace_in_backing_chain() (in fact, the latter's
conditions on when to move the backing BDS from source to target are not
really correct).
Therefore, remove that code from bdrv_replace_in_backing_chain() and
leave it to mirror_complete().
Depending on what kind of mirroring is performed, we furthermore want to
use different strategies to open the target's backing chain:
- If blockdev-mirror is used, we can assume the user made sure that the
target already has the correct backing chain. In particular, we should
not try to open a backing file if the target does not have any yet.
- If drive-mirror with mode=absolute-paths is used, we can and should
reuse the already existing chain of nodes that the source BDS is in.
In case of sync=full, no backing BDS is required; with sync=top, we
just link the source's backing BDS to the target, and with sync=none,
we use the source BDS as the target's backing BDS.
We should not try to open these backing files anew because this would
lead to two BDSs existing per physical file in the backing chain, and
we would like to avoid such concurrent access.
- If drive-mirror with mode=existing is used, we have to use the
information provided in the physical image file which means opening
the target's backing chain completely anew, just as it has been done
already.
If the target's backing chain shares images with the source, this may
lead to multiple BDSs per physical image file. But since we cannot
reliably ascertain this case, there is nothing we can do about it.
Signed-off-by: Max Reitz <mreitz@redhat.com>
Message-id: 20160610185750.30956-3-mreitz@redhat.com
Reviewed-by: Kevin Wolf <kwolf@redhat.com>
Reviewed-by: Fam Zheng <famz@redhat.com>
Signed-off-by: Max Reitz <mreitz@redhat.com>
2016-06-10 21:57:47 +03:00
|
|
|
|
|
|
|
target = blk_bs(s->target);
|
2012-10-18 18:49:25 +04:00
|
|
|
|
|
|
|
if (!s->synced) {
|
2016-07-05 17:28:53 +03:00
|
|
|
error_setg(errp, "The active block job '%s' cannot be completed",
|
|
|
|
job->id);
|
2012-10-18 18:49:25 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
block/mirror: Fix target backing BDS
Currently, we are trying to move the backing BDS from the source to the
target in bdrv_replace_in_backing_chain() which is called from
mirror_exit(). However, mirror_complete() already tries to open the
target's backing chain with a call to bdrv_open_backing_file().
First, we should only set the target's backing BDS once. Second, the
mirroring block job has a better idea of what to set it to than the
generic code in bdrv_replace_in_backing_chain() (in fact, the latter's
conditions on when to move the backing BDS from source to target are not
really correct).
Therefore, remove that code from bdrv_replace_in_backing_chain() and
leave it to mirror_complete().
Depending on what kind of mirroring is performed, we furthermore want to
use different strategies to open the target's backing chain:
- If blockdev-mirror is used, we can assume the user made sure that the
target already has the correct backing chain. In particular, we should
not try to open a backing file if the target does not have any yet.
- If drive-mirror with mode=absolute-paths is used, we can and should
reuse the already existing chain of nodes that the source BDS is in.
In case of sync=full, no backing BDS is required; with sync=top, we
just link the source's backing BDS to the target, and with sync=none,
we use the source BDS as the target's backing BDS.
We should not try to open these backing files anew because this would
lead to two BDSs existing per physical file in the backing chain, and
we would like to avoid such concurrent access.
- If drive-mirror with mode=existing is used, we have to use the
information provided in the physical image file which means opening
the target's backing chain completely anew, just as it has been done
already.
If the target's backing chain shares images with the source, this may
lead to multiple BDSs per physical image file. But since we cannot
reliably ascertain this case, there is nothing we can do about it.
Signed-off-by: Max Reitz <mreitz@redhat.com>
Message-id: 20160610185750.30956-3-mreitz@redhat.com
Reviewed-by: Kevin Wolf <kwolf@redhat.com>
Reviewed-by: Fam Zheng <famz@redhat.com>
Signed-off-by: Max Reitz <mreitz@redhat.com>
2016-06-10 21:57:47 +03:00
|
|
|
if (s->backing_mode == MIRROR_OPEN_BACKING_CHAIN) {
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
assert(!target->backing);
|
|
|
|
ret = bdrv_open_backing_file(target, NULL, "backing", errp);
|
|
|
|
if (ret < 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-23 11:57:21 +03:00
|
|
|
/* block all operations on to_replace bs */
|
2014-06-27 20:25:25 +04:00
|
|
|
if (s->replaces) {
|
2014-10-21 15:03:58 +04:00
|
|
|
AioContext *replace_aio_context;
|
|
|
|
|
2015-07-17 05:12:22 +03:00
|
|
|
s->to_replace = bdrv_find_node(s->replaces);
|
2014-06-27 20:25:25 +04:00
|
|
|
if (!s->to_replace) {
|
2015-07-17 05:12:22 +03:00
|
|
|
error_setg(errp, "Node name '%s' not found", s->replaces);
|
2014-06-27 20:25:25 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-10-21 15:03:58 +04:00
|
|
|
replace_aio_context = bdrv_get_aio_context(s->to_replace);
|
|
|
|
aio_context_acquire(replace_aio_context);
|
|
|
|
|
2017-01-25 21:16:34 +03:00
|
|
|
/* TODO Translate this into permission system. Current definition of
|
|
|
|
* GRAPH_MOD would require to request it for the parents; they might
|
|
|
|
* not even be BlockDriverStates, however, so a BdrvChild can't address
|
|
|
|
* them. May need redefinition of GRAPH_MOD. */
|
2014-06-27 20:25:25 +04:00
|
|
|
error_setg(&s->replace_blocker,
|
|
|
|
"block device is in use by block-job-complete");
|
|
|
|
bdrv_op_block_all(s->to_replace, s->replace_blocker);
|
|
|
|
bdrv_ref(s->to_replace);
|
2014-10-21 15:03:58 +04:00
|
|
|
|
|
|
|
aio_context_release(replace_aio_context);
|
2014-06-27 20:25:25 +04:00
|
|
|
}
|
|
|
|
|
2012-10-18 18:49:25 +04:00
|
|
|
s->should_complete = true;
|
2015-04-03 17:05:18 +03:00
|
|
|
block_job_enter(&s->common);
|
2012-10-18 18:49:25 +04:00
|
|
|
}
|
|
|
|
|
2016-10-27 13:48:50 +03:00
|
|
|
static void mirror_pause(BlockJob *job)
|
2016-06-16 19:56:28 +03:00
|
|
|
{
|
|
|
|
MirrorBlockJob *s = container_of(job, MirrorBlockJob, common);
|
|
|
|
|
2016-10-27 13:48:50 +03:00
|
|
|
mirror_wait_for_all_io(s);
|
2016-06-16 19:56:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void mirror_attached_aio_context(BlockJob *job, AioContext *new_context)
|
|
|
|
{
|
|
|
|
MirrorBlockJob *s = container_of(job, MirrorBlockJob, common);
|
|
|
|
|
|
|
|
blk_set_aio_context(s->target, new_context);
|
|
|
|
}
|
|
|
|
|
2016-10-27 13:48:50 +03:00
|
|
|
static void mirror_drain(BlockJob *job)
|
|
|
|
{
|
|
|
|
MirrorBlockJob *s = container_of(job, MirrorBlockJob, common);
|
|
|
|
|
|
|
|
/* Need to keep a reference in case blk_drain triggers execution
|
|
|
|
* of mirror_complete...
|
|
|
|
*/
|
|
|
|
if (s->target) {
|
|
|
|
BlockBackend *target = s->target;
|
|
|
|
blk_ref(target);
|
|
|
|
blk_drain(target);
|
|
|
|
blk_unref(target);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-08 13:29:38 +04:00
|
|
|
static const BlockJobDriver mirror_job_driver = {
|
2016-06-16 19:56:28 +03:00
|
|
|
.instance_size = sizeof(MirrorBlockJob),
|
|
|
|
.job_type = BLOCK_JOB_TYPE_MIRROR,
|
|
|
|
.set_speed = mirror_set_speed,
|
2016-11-08 09:50:36 +03:00
|
|
|
.start = mirror_run,
|
2016-06-16 19:56:28 +03:00
|
|
|
.complete = mirror_complete,
|
|
|
|
.pause = mirror_pause,
|
|
|
|
.attached_aio_context = mirror_attached_aio_context,
|
2016-10-27 13:48:50 +03:00
|
|
|
.drain = mirror_drain,
|
2012-10-18 18:49:23 +04:00
|
|
|
};
|
|
|
|
|
2013-12-16 10:45:30 +04:00
|
|
|
static const BlockJobDriver commit_active_job_driver = {
|
2016-06-16 19:56:28 +03:00
|
|
|
.instance_size = sizeof(MirrorBlockJob),
|
|
|
|
.job_type = BLOCK_JOB_TYPE_COMMIT,
|
|
|
|
.set_speed = mirror_set_speed,
|
2016-11-08 09:50:36 +03:00
|
|
|
.start = mirror_run,
|
2016-06-16 19:56:28 +03:00
|
|
|
.complete = mirror_complete,
|
|
|
|
.pause = mirror_pause,
|
|
|
|
.attached_aio_context = mirror_attached_aio_context,
|
2016-10-27 13:48:50 +03:00
|
|
|
.drain = mirror_drain,
|
2013-12-16 10:45:30 +04:00
|
|
|
};
|
|
|
|
|
2017-01-25 21:16:34 +03:00
|
|
|
static int coroutine_fn bdrv_mirror_top_preadv(BlockDriverState *bs,
|
|
|
|
uint64_t offset, uint64_t bytes, QEMUIOVector *qiov, int flags)
|
|
|
|
{
|
|
|
|
return bdrv_co_preadv(bs->backing, offset, bytes, qiov, flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int coroutine_fn bdrv_mirror_top_pwritev(BlockDriverState *bs,
|
|
|
|
uint64_t offset, uint64_t bytes, QEMUIOVector *qiov, int flags)
|
|
|
|
{
|
|
|
|
return bdrv_co_pwritev(bs->backing, offset, bytes, qiov, flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int coroutine_fn bdrv_mirror_top_flush(BlockDriverState *bs)
|
|
|
|
{
|
2017-09-29 18:22:55 +03:00
|
|
|
if (bs->backing == NULL) {
|
|
|
|
/* we can be here after failed bdrv_append in mirror_start_job */
|
|
|
|
return 0;
|
|
|
|
}
|
2017-01-25 21:16:34 +03:00
|
|
|
return bdrv_co_flush(bs->backing->bs);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int coroutine_fn bdrv_mirror_top_pwrite_zeroes(BlockDriverState *bs,
|
2017-06-09 13:18:08 +03:00
|
|
|
int64_t offset, int bytes, BdrvRequestFlags flags)
|
2017-01-25 21:16:34 +03:00
|
|
|
{
|
2017-06-09 13:18:08 +03:00
|
|
|
return bdrv_co_pwrite_zeroes(bs->backing, offset, bytes, flags);
|
2017-01-25 21:16:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static int coroutine_fn bdrv_mirror_top_pdiscard(BlockDriverState *bs,
|
2017-06-09 13:18:08 +03:00
|
|
|
int64_t offset, int bytes)
|
2017-01-25 21:16:34 +03:00
|
|
|
{
|
2017-06-09 13:18:08 +03:00
|
|
|
return bdrv_co_pdiscard(bs->backing->bs, offset, bytes);
|
2017-01-25 21:16:34 +03:00
|
|
|
}
|
|
|
|
|
2017-03-09 13:49:16 +03:00
|
|
|
static void bdrv_mirror_top_refresh_filename(BlockDriverState *bs, QDict *opts)
|
|
|
|
{
|
2017-09-28 15:03:00 +03:00
|
|
|
if (bs->backing == NULL) {
|
|
|
|
/* we can be here after failed bdrv_attach_child in
|
|
|
|
* bdrv_set_backing_hd */
|
|
|
|
return;
|
|
|
|
}
|
2017-03-09 13:49:16 +03:00
|
|
|
bdrv_refresh_filename(bs->backing->bs);
|
|
|
|
pstrcpy(bs->exact_filename, sizeof(bs->exact_filename),
|
|
|
|
bs->backing->bs->filename);
|
|
|
|
}
|
|
|
|
|
2017-01-25 21:16:34 +03:00
|
|
|
static void bdrv_mirror_top_close(BlockDriverState *bs)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
static void bdrv_mirror_top_child_perm(BlockDriverState *bs, BdrvChild *c,
|
|
|
|
const BdrvChildRole *role,
|
2017-09-14 13:47:11 +03:00
|
|
|
BlockReopenQueue *reopen_queue,
|
2017-01-25 21:16:34 +03:00
|
|
|
uint64_t perm, uint64_t shared,
|
|
|
|
uint64_t *nperm, uint64_t *nshared)
|
|
|
|
{
|
|
|
|
/* Must be able to forward guest writes to the real image */
|
|
|
|
*nperm = 0;
|
|
|
|
if (perm & BLK_PERM_WRITE) {
|
|
|
|
*nperm |= BLK_PERM_WRITE;
|
|
|
|
}
|
|
|
|
|
|
|
|
*nshared = BLK_PERM_ALL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Dummy node that provides consistent read to its users without requiring it
|
|
|
|
* from its backing file and that allows writes on the backing file chain. */
|
|
|
|
static BlockDriver bdrv_mirror_top = {
|
|
|
|
.format_name = "mirror_top",
|
|
|
|
.bdrv_co_preadv = bdrv_mirror_top_preadv,
|
|
|
|
.bdrv_co_pwritev = bdrv_mirror_top_pwritev,
|
|
|
|
.bdrv_co_pwrite_zeroes = bdrv_mirror_top_pwrite_zeroes,
|
|
|
|
.bdrv_co_pdiscard = bdrv_mirror_top_pdiscard,
|
|
|
|
.bdrv_co_flush = bdrv_mirror_top_flush,
|
2018-02-13 23:26:43 +03:00
|
|
|
.bdrv_co_block_status = bdrv_co_block_status_from_backing,
|
2017-03-09 13:49:16 +03:00
|
|
|
.bdrv_refresh_filename = bdrv_mirror_top_refresh_filename,
|
2017-01-25 21:16:34 +03:00
|
|
|
.bdrv_close = bdrv_mirror_top_close,
|
|
|
|
.bdrv_child_perm = bdrv_mirror_top_child_perm,
|
|
|
|
};
|
|
|
|
|
2016-07-05 17:28:57 +03:00
|
|
|
static void mirror_start_job(const char *job_id, BlockDriverState *bs,
|
2016-10-27 19:06:57 +03:00
|
|
|
int creation_flags, BlockDriverState *target,
|
|
|
|
const char *replaces, int64_t speed,
|
|
|
|
uint32_t granularity, int64_t buf_size,
|
block/mirror: Fix target backing BDS
Currently, we are trying to move the backing BDS from the source to the
target in bdrv_replace_in_backing_chain() which is called from
mirror_exit(). However, mirror_complete() already tries to open the
target's backing chain with a call to bdrv_open_backing_file().
First, we should only set the target's backing BDS once. Second, the
mirroring block job has a better idea of what to set it to than the
generic code in bdrv_replace_in_backing_chain() (in fact, the latter's
conditions on when to move the backing BDS from source to target are not
really correct).
Therefore, remove that code from bdrv_replace_in_backing_chain() and
leave it to mirror_complete().
Depending on what kind of mirroring is performed, we furthermore want to
use different strategies to open the target's backing chain:
- If blockdev-mirror is used, we can assume the user made sure that the
target already has the correct backing chain. In particular, we should
not try to open a backing file if the target does not have any yet.
- If drive-mirror with mode=absolute-paths is used, we can and should
reuse the already existing chain of nodes that the source BDS is in.
In case of sync=full, no backing BDS is required; with sync=top, we
just link the source's backing BDS to the target, and with sync=none,
we use the source BDS as the target's backing BDS.
We should not try to open these backing files anew because this would
lead to two BDSs existing per physical file in the backing chain, and
we would like to avoid such concurrent access.
- If drive-mirror with mode=existing is used, we have to use the
information provided in the physical image file which means opening
the target's backing chain completely anew, just as it has been done
already.
If the target's backing chain shares images with the source, this may
lead to multiple BDSs per physical image file. But since we cannot
reliably ascertain this case, there is nothing we can do about it.
Signed-off-by: Max Reitz <mreitz@redhat.com>
Message-id: 20160610185750.30956-3-mreitz@redhat.com
Reviewed-by: Kevin Wolf <kwolf@redhat.com>
Reviewed-by: Fam Zheng <famz@redhat.com>
Signed-off-by: Max Reitz <mreitz@redhat.com>
2016-06-10 21:57:47 +03:00
|
|
|
BlockMirrorBackingMode backing_mode,
|
2014-06-27 20:25:25 +04:00
|
|
|
BlockdevOnError on_source_error,
|
|
|
|
BlockdevOnError on_target_error,
|
2015-06-08 08:56:08 +03:00
|
|
|
bool unmap,
|
2014-10-07 15:59:15 +04:00
|
|
|
BlockCompletionFunc *cb,
|
2017-04-21 15:27:03 +03:00
|
|
|
void *opaque,
|
2014-06-27 20:25:25 +04:00
|
|
|
const BlockJobDriver *driver,
|
2016-07-27 10:01:47 +03:00
|
|
|
bool is_none_mode, BlockDriverState *base,
|
2017-04-21 15:27:03 +03:00
|
|
|
bool auto_complete, const char *filter_node_name,
|
2017-08-23 16:42:41 +03:00
|
|
|
bool is_mirror,
|
2017-04-21 15:27:03 +03:00
|
|
|
Error **errp)
|
2012-10-18 18:49:23 +04:00
|
|
|
{
|
|
|
|
MirrorBlockJob *s;
|
2017-01-25 21:16:34 +03:00
|
|
|
BlockDriverState *mirror_top_bs;
|
|
|
|
bool target_graph_mod;
|
|
|
|
bool target_is_backing;
|
2017-02-20 14:46:42 +03:00
|
|
|
Error *local_err = NULL;
|
2017-01-13 21:02:32 +03:00
|
|
|
int ret;
|
2012-10-18 18:49:23 +04:00
|
|
|
|
2013-01-21 20:09:46 +04:00
|
|
|
if (granularity == 0) {
|
2015-04-18 02:49:52 +03:00
|
|
|
granularity = bdrv_get_default_bitmap_granularity(target);
|
2013-01-21 20:09:46 +04:00
|
|
|
}
|
|
|
|
|
block: Convert bdrv_get_block_status_above() to bytes
We are gradually moving away from sector-based interfaces, towards
byte-based. In the common case, allocation is unlikely to ever use
values that are not naturally sector-aligned, but it is possible
that byte-based values will let us be more precise about allocation
at the end of an unaligned file that can do byte-based access.
Changing the name of the function from bdrv_get_block_status_above()
to bdrv_block_status_above() ensures that the compiler enforces that
all callers are updated. Likewise, since it a byte interface allows
an offset mapping that might not be sector aligned, split the mapping
out of the return value and into a pass-by-reference parameter. For
now, the io.c layer still assert()s that all uses are sector-aligned,
but that can be relaxed when a later patch implements byte-based
block status in the drivers.
For the most part this patch is just the addition of scaling at the
callers followed by inverse scaling at bdrv_block_status(), plus
updates for the new split return interface. But some code,
particularly bdrv_block_status(), gets a lot simpler because it no
longer has to mess with sectors. Likewise, mirror code no longer
computes s->granularity >> BDRV_SECTOR_BITS, and can therefore drop
an assertion about alignment because the loop no longer depends on
alignment (never mind that we don't really have a driver that
reports sub-sector alignments, so it's not really possible to test
the effect of sub-sector mirroring). Fix a neighboring assertion to
use is_power_of_2 while there.
For ease of review, bdrv_get_block_status() was tackled separately.
Signed-off-by: Eric Blake <eblake@redhat.com>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
2017-10-12 06:47:08 +03:00
|
|
|
assert(is_power_of_2(granularity));
|
2013-01-21 20:09:46 +04:00
|
|
|
|
2015-05-15 10:51:36 +03:00
|
|
|
if (buf_size < 0) {
|
|
|
|
error_setg(errp, "Invalid parameter 'buf-size'");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (buf_size == 0) {
|
|
|
|
buf_size = DEFAULT_MIRROR_BUF_SIZE;
|
|
|
|
}
|
2013-12-16 10:45:29 +04:00
|
|
|
|
2017-01-25 21:16:34 +03:00
|
|
|
/* In the case of active commit, add dummy driver to provide consistent
|
|
|
|
* reads on the top, while disabling it in the intermediate nodes, and make
|
|
|
|
* the backing chain writable. */
|
2017-02-20 20:10:05 +03:00
|
|
|
mirror_top_bs = bdrv_new_open_driver(&bdrv_mirror_top, filter_node_name,
|
|
|
|
BDRV_O_RDWR, errp);
|
2017-01-25 21:16:34 +03:00
|
|
|
if (mirror_top_bs == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
2017-07-18 18:24:05 +03:00
|
|
|
if (!filter_node_name) {
|
|
|
|
mirror_top_bs->implicit = true;
|
|
|
|
}
|
2017-01-25 21:16:34 +03:00
|
|
|
mirror_top_bs->total_sectors = bs->total_sectors;
|
2017-04-07 09:54:11 +03:00
|
|
|
bdrv_set_aio_context(mirror_top_bs, bdrv_get_aio_context(bs));
|
2017-01-25 21:16:34 +03:00
|
|
|
|
|
|
|
/* bdrv_append takes ownership of the mirror_top_bs reference, need to keep
|
2017-04-03 20:51:49 +03:00
|
|
|
* it alive until block_job_create() succeeds even if bs has no parent. */
|
2017-01-25 21:16:34 +03:00
|
|
|
bdrv_ref(mirror_top_bs);
|
|
|
|
bdrv_drained_begin(bs);
|
2017-02-20 14:46:42 +03:00
|
|
|
bdrv_append(mirror_top_bs, bs, &local_err);
|
2017-01-25 21:16:34 +03:00
|
|
|
bdrv_drained_end(bs);
|
|
|
|
|
2017-02-20 14:46:42 +03:00
|
|
|
if (local_err) {
|
|
|
|
bdrv_unref(mirror_top_bs);
|
|
|
|
error_propagate(errp, local_err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-01-25 21:16:34 +03:00
|
|
|
/* Make sure that the source is not resized while the job is running */
|
|
|
|
s = block_job_create(job_id, driver, mirror_top_bs,
|
|
|
|
BLK_PERM_CONSISTENT_READ,
|
|
|
|
BLK_PERM_CONSISTENT_READ | BLK_PERM_WRITE_UNCHANGED |
|
|
|
|
BLK_PERM_WRITE | BLK_PERM_GRAPH_MOD, speed,
|
2017-01-16 19:18:09 +03:00
|
|
|
creation_flags, cb, opaque, errp);
|
2012-10-18 18:49:23 +04:00
|
|
|
if (!s) {
|
2017-01-25 21:16:34 +03:00
|
|
|
goto fail;
|
2012-10-18 18:49:23 +04:00
|
|
|
}
|
2017-04-03 20:51:49 +03:00
|
|
|
/* The block job now has a reference to this node */
|
|
|
|
bdrv_unref(mirror_top_bs);
|
|
|
|
|
2017-01-25 21:16:34 +03:00
|
|
|
s->source = bs;
|
|
|
|
s->mirror_top_bs = mirror_top_bs;
|
|
|
|
|
|
|
|
/* No resize for the target either; while the mirror is still running, a
|
|
|
|
* consistent read isn't necessarily possible. We could possibly allow
|
|
|
|
* writes and graph modifications, though it would likely defeat the
|
|
|
|
* purpose of a mirror, so leave them blocked for now.
|
|
|
|
*
|
|
|
|
* In the case of active commit, things look a bit different, though,
|
|
|
|
* because the target is an already populated backing file in active use.
|
|
|
|
* We can allow anything except resize there.*/
|
|
|
|
target_is_backing = bdrv_chain_contains(bs, target);
|
|
|
|
target_graph_mod = (backing_mode != MIRROR_LEAVE_BACKING_CHAIN);
|
|
|
|
s->target = blk_new(BLK_PERM_WRITE | BLK_PERM_RESIZE |
|
|
|
|
(target_graph_mod ? BLK_PERM_GRAPH_MOD : 0),
|
|
|
|
BLK_PERM_WRITE_UNCHANGED |
|
|
|
|
(target_is_backing ? BLK_PERM_CONSISTENT_READ |
|
|
|
|
BLK_PERM_WRITE |
|
|
|
|
BLK_PERM_GRAPH_MOD : 0));
|
2017-01-13 21:02:32 +03:00
|
|
|
ret = blk_insert_bs(s->target, target, errp);
|
|
|
|
if (ret < 0) {
|
2017-01-25 21:16:34 +03:00
|
|
|
goto fail;
|
2017-01-13 21:02:32 +03:00
|
|
|
}
|
2017-08-23 16:42:41 +03:00
|
|
|
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);
|
|
|
|
}
|
2016-04-12 17:17:41 +03:00
|
|
|
|
2014-06-27 20:25:25 +04:00
|
|
|
s->replaces = g_strdup(replaces);
|
2012-10-18 18:49:28 +04:00
|
|
|
s->on_source_error = on_source_error;
|
|
|
|
s->on_target_error = on_target_error;
|
2013-12-16 10:45:30 +04:00
|
|
|
s->is_none_mode = is_none_mode;
|
block/mirror: Fix target backing BDS
Currently, we are trying to move the backing BDS from the source to the
target in bdrv_replace_in_backing_chain() which is called from
mirror_exit(). However, mirror_complete() already tries to open the
target's backing chain with a call to bdrv_open_backing_file().
First, we should only set the target's backing BDS once. Second, the
mirroring block job has a better idea of what to set it to than the
generic code in bdrv_replace_in_backing_chain() (in fact, the latter's
conditions on when to move the backing BDS from source to target are not
really correct).
Therefore, remove that code from bdrv_replace_in_backing_chain() and
leave it to mirror_complete().
Depending on what kind of mirroring is performed, we furthermore want to
use different strategies to open the target's backing chain:
- If blockdev-mirror is used, we can assume the user made sure that the
target already has the correct backing chain. In particular, we should
not try to open a backing file if the target does not have any yet.
- If drive-mirror with mode=absolute-paths is used, we can and should
reuse the already existing chain of nodes that the source BDS is in.
In case of sync=full, no backing BDS is required; with sync=top, we
just link the source's backing BDS to the target, and with sync=none,
we use the source BDS as the target's backing BDS.
We should not try to open these backing files anew because this would
lead to two BDSs existing per physical file in the backing chain, and
we would like to avoid such concurrent access.
- If drive-mirror with mode=existing is used, we have to use the
information provided in the physical image file which means opening
the target's backing chain completely anew, just as it has been done
already.
If the target's backing chain shares images with the source, this may
lead to multiple BDSs per physical image file. But since we cannot
reliably ascertain this case, there is nothing we can do about it.
Signed-off-by: Max Reitz <mreitz@redhat.com>
Message-id: 20160610185750.30956-3-mreitz@redhat.com
Reviewed-by: Kevin Wolf <kwolf@redhat.com>
Reviewed-by: Fam Zheng <famz@redhat.com>
Signed-off-by: Max Reitz <mreitz@redhat.com>
2016-06-10 21:57:47 +03:00
|
|
|
s->backing_mode = backing_mode;
|
2013-12-16 10:45:29 +04:00
|
|
|
s->base = base;
|
2013-01-21 20:09:46 +04:00
|
|
|
s->granularity = granularity;
|
2015-05-15 10:51:36 +03:00
|
|
|
s->buf_size = ROUND_UP(buf_size, granularity);
|
2015-06-08 08:56:08 +03:00
|
|
|
s->unmap = unmap;
|
2016-07-27 10:01:47 +03:00
|
|
|
if (auto_complete) {
|
|
|
|
s->should_complete = true;
|
|
|
|
}
|
2013-01-21 20:09:43 +04:00
|
|
|
|
2015-04-18 02:49:50 +03:00
|
|
|
s->dirty_bitmap = bdrv_create_dirty_bitmap(bs, granularity, NULL, errp);
|
2014-04-16 05:34:30 +04:00
|
|
|
if (!s->dirty_bitmap) {
|
2017-03-06 18:12:44 +03:00
|
|
|
goto fail;
|
2014-04-16 05:34:30 +04:00
|
|
|
}
|
2015-11-02 17:51:53 +03:00
|
|
|
|
2017-01-25 21:16:34 +03:00
|
|
|
/* Required permissions are already taken with blk_new() */
|
2017-01-17 13:56:42 +03:00
|
|
|
block_job_add_bdrv(&s->common, "target", target, 0, BLK_PERM_ALL,
|
|
|
|
&error_abort);
|
|
|
|
|
2016-10-28 10:08:09 +03:00
|
|
|
/* In commit_active_start() all intermediate nodes disappear, so
|
|
|
|
* any jobs in them must be blocked */
|
2017-01-25 21:16:34 +03:00
|
|
|
if (target_is_backing) {
|
2016-10-28 10:08:09 +03:00
|
|
|
BlockDriverState *iter;
|
|
|
|
for (iter = backing_bs(bs); iter != target; iter = backing_bs(iter)) {
|
2017-01-25 21:16:34 +03:00
|
|
|
/* XXX BLK_PERM_WRITE needs to be allowed so we don't block
|
|
|
|
* ourselves at s->base (if writes are blocked for a node, they are
|
|
|
|
* also blocked for its backing file). The other options would be a
|
|
|
|
* second filter driver above s->base (== target). */
|
|
|
|
ret = block_job_add_bdrv(&s->common, "intermediate node", iter, 0,
|
|
|
|
BLK_PERM_WRITE_UNCHANGED | BLK_PERM_WRITE,
|
|
|
|
errp);
|
|
|
|
if (ret < 0) {
|
|
|
|
goto fail;
|
|
|
|
}
|
2016-10-28 10:08:09 +03:00
|
|
|
}
|
|
|
|
}
|
2015-11-02 17:51:53 +03:00
|
|
|
|
2016-11-08 09:50:37 +03:00
|
|
|
trace_mirror_start(bs, s, opaque);
|
|
|
|
block_job_start(&s->common);
|
2017-01-25 21:16:34 +03:00
|
|
|
return;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
if (s) {
|
2017-04-03 20:51:49 +03:00
|
|
|
/* Make sure this BDS does not go away until we have completed the graph
|
|
|
|
* changes below */
|
|
|
|
bdrv_ref(mirror_top_bs);
|
|
|
|
|
2017-01-25 21:16:34 +03:00
|
|
|
g_free(s->replaces);
|
|
|
|
blk_unref(s->target);
|
2017-05-08 17:13:02 +03:00
|
|
|
block_job_early_fail(&s->common);
|
2017-01-25 21:16:34 +03:00
|
|
|
}
|
|
|
|
|
2017-03-14 05:30:50 +03:00
|
|
|
bdrv_child_try_set_perm(mirror_top_bs->backing, 0, BLK_PERM_ALL,
|
|
|
|
&error_abort);
|
2017-03-06 18:20:51 +03:00
|
|
|
bdrv_replace_node(mirror_top_bs, backing_bs(mirror_top_bs), &error_abort);
|
2017-04-03 20:51:49 +03:00
|
|
|
|
|
|
|
bdrv_unref(mirror_top_bs);
|
2012-10-18 18:49:23 +04:00
|
|
|
}
|
2013-12-16 10:45:30 +04:00
|
|
|
|
2016-07-05 17:28:57 +03:00
|
|
|
void mirror_start(const char *job_id, BlockDriverState *bs,
|
|
|
|
BlockDriverState *target, const char *replaces,
|
2015-04-18 02:49:51 +03:00
|
|
|
int64_t speed, uint32_t granularity, int64_t buf_size,
|
block/mirror: Fix target backing BDS
Currently, we are trying to move the backing BDS from the source to the
target in bdrv_replace_in_backing_chain() which is called from
mirror_exit(). However, mirror_complete() already tries to open the
target's backing chain with a call to bdrv_open_backing_file().
First, we should only set the target's backing BDS once. Second, the
mirroring block job has a better idea of what to set it to than the
generic code in bdrv_replace_in_backing_chain() (in fact, the latter's
conditions on when to move the backing BDS from source to target are not
really correct).
Therefore, remove that code from bdrv_replace_in_backing_chain() and
leave it to mirror_complete().
Depending on what kind of mirroring is performed, we furthermore want to
use different strategies to open the target's backing chain:
- If blockdev-mirror is used, we can assume the user made sure that the
target already has the correct backing chain. In particular, we should
not try to open a backing file if the target does not have any yet.
- If drive-mirror with mode=absolute-paths is used, we can and should
reuse the already existing chain of nodes that the source BDS is in.
In case of sync=full, no backing BDS is required; with sync=top, we
just link the source's backing BDS to the target, and with sync=none,
we use the source BDS as the target's backing BDS.
We should not try to open these backing files anew because this would
lead to two BDSs existing per physical file in the backing chain, and
we would like to avoid such concurrent access.
- If drive-mirror with mode=existing is used, we have to use the
information provided in the physical image file which means opening
the target's backing chain completely anew, just as it has been done
already.
If the target's backing chain shares images with the source, this may
lead to multiple BDSs per physical image file. But since we cannot
reliably ascertain this case, there is nothing we can do about it.
Signed-off-by: Max Reitz <mreitz@redhat.com>
Message-id: 20160610185750.30956-3-mreitz@redhat.com
Reviewed-by: Kevin Wolf <kwolf@redhat.com>
Reviewed-by: Fam Zheng <famz@redhat.com>
Signed-off-by: Max Reitz <mreitz@redhat.com>
2016-06-10 21:57:47 +03:00
|
|
|
MirrorSyncMode mode, BlockMirrorBackingMode backing_mode,
|
|
|
|
BlockdevOnError on_source_error,
|
2013-12-16 10:45:30 +04:00
|
|
|
BlockdevOnError on_target_error,
|
2017-02-20 20:10:05 +03:00
|
|
|
bool unmap, const char *filter_node_name, Error **errp)
|
2013-12-16 10:45:30 +04:00
|
|
|
{
|
|
|
|
bool is_none_mode;
|
|
|
|
BlockDriverState *base;
|
|
|
|
|
2015-06-05 03:20:34 +03:00
|
|
|
if (mode == MIRROR_SYNC_MODE_INCREMENTAL) {
|
|
|
|
error_setg(errp, "Sync mode 'incremental' not supported");
|
2015-04-18 02:49:58 +03:00
|
|
|
return;
|
|
|
|
}
|
2013-12-16 10:45:30 +04:00
|
|
|
is_none_mode = mode == MIRROR_SYNC_MODE_NONE;
|
2015-06-17 15:55:21 +03:00
|
|
|
base = mode == MIRROR_SYNC_MODE_TOP ? backing_bs(bs) : NULL;
|
2016-10-27 19:06:57 +03:00
|
|
|
mirror_start_job(job_id, bs, BLOCK_JOB_DEFAULT, target, replaces,
|
block/mirror: Fix target backing BDS
Currently, we are trying to move the backing BDS from the source to the
target in bdrv_replace_in_backing_chain() which is called from
mirror_exit(). However, mirror_complete() already tries to open the
target's backing chain with a call to bdrv_open_backing_file().
First, we should only set the target's backing BDS once. Second, the
mirroring block job has a better idea of what to set it to than the
generic code in bdrv_replace_in_backing_chain() (in fact, the latter's
conditions on when to move the backing BDS from source to target are not
really correct).
Therefore, remove that code from bdrv_replace_in_backing_chain() and
leave it to mirror_complete().
Depending on what kind of mirroring is performed, we furthermore want to
use different strategies to open the target's backing chain:
- If blockdev-mirror is used, we can assume the user made sure that the
target already has the correct backing chain. In particular, we should
not try to open a backing file if the target does not have any yet.
- If drive-mirror with mode=absolute-paths is used, we can and should
reuse the already existing chain of nodes that the source BDS is in.
In case of sync=full, no backing BDS is required; with sync=top, we
just link the source's backing BDS to the target, and with sync=none,
we use the source BDS as the target's backing BDS.
We should not try to open these backing files anew because this would
lead to two BDSs existing per physical file in the backing chain, and
we would like to avoid such concurrent access.
- If drive-mirror with mode=existing is used, we have to use the
information provided in the physical image file which means opening
the target's backing chain completely anew, just as it has been done
already.
If the target's backing chain shares images with the source, this may
lead to multiple BDSs per physical image file. But since we cannot
reliably ascertain this case, there is nothing we can do about it.
Signed-off-by: Max Reitz <mreitz@redhat.com>
Message-id: 20160610185750.30956-3-mreitz@redhat.com
Reviewed-by: Kevin Wolf <kwolf@redhat.com>
Reviewed-by: Fam Zheng <famz@redhat.com>
Signed-off-by: Max Reitz <mreitz@redhat.com>
2016-06-10 21:57:47 +03:00
|
|
|
speed, granularity, buf_size, backing_mode,
|
2017-04-21 15:27:03 +03:00
|
|
|
on_source_error, on_target_error, unmap, NULL, NULL,
|
2017-02-20 20:10:05 +03:00
|
|
|
&mirror_job_driver, is_none_mode, base, false,
|
2017-08-23 16:42:41 +03:00
|
|
|
filter_node_name, true, errp);
|
2013-12-16 10:45:30 +04:00
|
|
|
}
|
|
|
|
|
2016-07-05 17:29:00 +03:00
|
|
|
void commit_active_start(const char *job_id, BlockDriverState *bs,
|
2016-10-27 19:06:57 +03:00
|
|
|
BlockDriverState *base, int creation_flags,
|
|
|
|
int64_t speed, BlockdevOnError on_error,
|
2017-02-20 20:10:05 +03:00
|
|
|
const char *filter_node_name,
|
2017-04-21 15:27:04 +03:00
|
|
|
BlockCompletionFunc *cb, void *opaque,
|
|
|
|
bool auto_complete, Error **errp)
|
2013-12-16 10:45:30 +04:00
|
|
|
{
|
2014-01-24 18:02:36 +04:00
|
|
|
int orig_base_flags;
|
2014-02-13 18:23:38 +04:00
|
|
|
Error *local_err = NULL;
|
2014-01-24 18:02:36 +04:00
|
|
|
|
|
|
|
orig_base_flags = bdrv_get_flags(base);
|
|
|
|
|
2013-12-16 10:45:31 +04:00
|
|
|
if (bdrv_reopen(base, bs->open_flags, errp)) {
|
|
|
|
return;
|
|
|
|
}
|
2014-01-24 18:02:36 +04:00
|
|
|
|
2016-10-27 19:06:57 +03:00
|
|
|
mirror_start_job(job_id, bs, creation_flags, base, NULL, speed, 0, 0,
|
2016-07-05 17:28:57 +03:00
|
|
|
MIRROR_LEAVE_BACKING_CHAIN,
|
2017-04-21 15:27:03 +03:00
|
|
|
on_error, on_error, true, cb, opaque,
|
2017-02-20 20:10:05 +03:00
|
|
|
&commit_active_job_driver, false, base, auto_complete,
|
2017-08-23 16:42:41 +03:00
|
|
|
filter_node_name, false, &local_err);
|
2014-04-25 18:50:31 +04:00
|
|
|
if (local_err) {
|
2014-02-13 18:23:38 +04:00
|
|
|
error_propagate(errp, local_err);
|
2014-01-24 18:02:36 +04:00
|
|
|
goto error_restore_flags;
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
error_restore_flags:
|
|
|
|
/* ignore error and errp for bdrv_reopen, because we want to propagate
|
|
|
|
* the original error */
|
|
|
|
bdrv_reopen(base, orig_base_flags, NULL);
|
|
|
|
return;
|
2013-12-16 10:45:30 +04:00
|
|
|
}
|