1893f7ebaSPaolo Bonzini /* 2893f7ebaSPaolo Bonzini * Image mirroring 3893f7ebaSPaolo Bonzini * 4893f7ebaSPaolo Bonzini * Copyright Red Hat, Inc. 2012 5893f7ebaSPaolo Bonzini * 6893f7ebaSPaolo Bonzini * Authors: 7893f7ebaSPaolo Bonzini * Paolo Bonzini <pbonzini@redhat.com> 8893f7ebaSPaolo Bonzini * 9893f7ebaSPaolo Bonzini * This work is licensed under the terms of the GNU LGPL, version 2 or later. 10893f7ebaSPaolo Bonzini * See the COPYING.LIB file in the top-level directory. 11893f7ebaSPaolo Bonzini * 12893f7ebaSPaolo Bonzini */ 13893f7ebaSPaolo Bonzini 1480c71a24SPeter Maydell #include "qemu/osdep.h" 15fd4a6493SKevin Wolf #include "qemu/cutils.h" 1612aa4082SMax Reitz #include "qemu/coroutine.h" 171181e19aSMax Reitz #include "qemu/range.h" 18893f7ebaSPaolo Bonzini #include "trace.h" 19c87621eaSJohn Snow #include "block/blockjob_int.h" 20737e150eSPaolo Bonzini #include "block/block_int.h" 21e2c1c34fSMarkus Armbruster #include "block/dirty-bitmap.h" 2232cad1ffSPhilippe Mathieu-Daudé #include "system/block-backend.h" 23da34e65cSMarkus Armbruster #include "qapi/error.h" 24893f7ebaSPaolo Bonzini #include "qemu/ratelimit.h" 25b812f671SPaolo Bonzini #include "qemu/bitmap.h" 265df022cfSPeter Maydell #include "qemu/memalign.h" 27893f7ebaSPaolo Bonzini 28402a4741SPaolo Bonzini #define MAX_IN_FLIGHT 16 29b436982fSEric Blake #define MAX_IO_BYTES (1 << 20) /* 1 Mb */ 30b436982fSEric Blake #define DEFAULT_MIRROR_BUF_SIZE (MAX_IN_FLIGHT * MAX_IO_BYTES) 31402a4741SPaolo Bonzini 32402a4741SPaolo Bonzini /* The mirroring buffer is a list of granularity-sized chunks. 33402a4741SPaolo Bonzini * Free chunks are organized in a list. 34402a4741SPaolo Bonzini */ 35402a4741SPaolo Bonzini typedef struct MirrorBuffer { 36402a4741SPaolo Bonzini QSIMPLEQ_ENTRY(MirrorBuffer) next; 37402a4741SPaolo Bonzini } MirrorBuffer; 38893f7ebaSPaolo Bonzini 3912aa4082SMax Reitz typedef struct MirrorOp MirrorOp; 4012aa4082SMax Reitz 41893f7ebaSPaolo Bonzini typedef struct MirrorBlockJob { 42893f7ebaSPaolo Bonzini BlockJob common; 43e253f4b8SKevin Wolf BlockBackend *target; 444ef85a9cSKevin Wolf BlockDriverState *mirror_top_bs; 455bc361b8SFam Zheng BlockDriverState *base; 463f072a7fSMax Reitz BlockDriverState *base_overlay; 474ef85a9cSKevin Wolf 4809158f00SBenoît Canet /* The name of the graph node to replace */ 4909158f00SBenoît Canet char *replaces; 5009158f00SBenoît Canet /* The BDS to replace */ 5109158f00SBenoît Canet BlockDriverState *to_replace; 5209158f00SBenoît Canet /* Used to block operations on the drive-mirror-replace target */ 5309158f00SBenoît Canet Error *replace_blocker; 5403544a6eSFam Zheng bool is_none_mode; 55274fcceeSMax Reitz BlockMirrorBackingMode backing_mode; 56cdf3bc93SMax Reitz /* Whether the target image requires explicit zero-initialization */ 57cdf3bc93SMax Reitz bool zero_target; 582d400d15SFiona Ebner /* 592d400d15SFiona Ebner * To be accesssed with atomics. Written only under the BQL (required by the 602d400d15SFiona Ebner * current implementation of mirror_change()). 612d400d15SFiona Ebner */ 62d06107adSMax Reitz MirrorCopyMode copy_mode; 63b952b558SPaolo Bonzini BlockdevOnError on_source_error, on_target_error; 6476cb2f24SFiona Ebner /* 6576cb2f24SFiona Ebner * To be accessed with atomics. 6676cb2f24SFiona Ebner * 6776cb2f24SFiona Ebner * Set when the target is synced (dirty bitmap is clean, nothing in flight) 6876cb2f24SFiona Ebner * and the job is running in active mode. 6976cb2f24SFiona Ebner */ 70d06107adSMax Reitz bool actively_synced; 71d63ffd87SPaolo Bonzini bool should_complete; 72eee13dfeSPaolo Bonzini int64_t granularity; 73b812f671SPaolo Bonzini size_t buf_size; 74b21c7652SMax Reitz int64_t bdev_length; 75b812f671SPaolo Bonzini unsigned long *cow_bitmap; 76e4654d2dSFam Zheng BdrvDirtyBitmap *dirty_bitmap; 77dc162c8eSFam Zheng BdrvDirtyBitmapIter *dbi; 78893f7ebaSPaolo Bonzini uint8_t *buf; 79402a4741SPaolo Bonzini QSIMPLEQ_HEAD(, MirrorBuffer) buf_free; 80402a4741SPaolo Bonzini int buf_free_count; 81bd48bde8SPaolo Bonzini 8249efb1f5SDenis V. Lunev uint64_t last_pause_ns; 83402a4741SPaolo Bonzini unsigned long *in_flight_bitmap; 841b8f7776SDenis V. Lunev unsigned in_flight; 85b436982fSEric Blake int64_t bytes_in_flight; 86b58deb34SPaolo Bonzini QTAILQ_HEAD(, MirrorOp) ops_in_flight; 87bd48bde8SPaolo Bonzini int ret; 880fc9f8eaSFam Zheng bool unmap; 89b436982fSEric Blake int target_cluster_size; 90e5b43573SFam Zheng int max_iov; 9190ab48ebSAnton Nefedov bool initial_zeroing_ongoing; 92d06107adSMax Reitz int in_active_write_counter; 93d69a879bSHanna Reitz int64_t active_write_bytes_in_flight; 94737efc1eSJohn Snow bool prepared; 955e771752SSergio Lopez bool in_drain; 967d99ae59SAlexander Ivanov bool base_ro; 97893f7ebaSPaolo Bonzini } MirrorBlockJob; 98893f7ebaSPaolo Bonzini 99429076e8SMax Reitz typedef struct MirrorBDSOpaque { 100429076e8SMax Reitz MirrorBlockJob *job; 101f94dc3b4SMax Reitz bool stop; 10253431b90SMax Reitz bool is_commit; 103429076e8SMax Reitz } MirrorBDSOpaque; 104429076e8SMax Reitz 10512aa4082SMax Reitz struct MirrorOp { 106bd48bde8SPaolo Bonzini MirrorBlockJob *s; 107bd48bde8SPaolo Bonzini QEMUIOVector qiov; 108b436982fSEric Blake int64_t offset; 109b436982fSEric Blake uint64_t bytes; 1102e1990b2SMax Reitz 1112e1990b2SMax Reitz /* The pointee is set by mirror_co_read(), mirror_co_zero(), and 1122e1990b2SMax Reitz * mirror_co_discard() before yielding for the first time */ 1132e1990b2SMax Reitz int64_t *bytes_handled; 11412aa4082SMax Reitz 1151181e19aSMax Reitz bool is_pseudo_op; 116d06107adSMax Reitz bool is_active_write; 117ce8cabbdSKevin Wolf bool is_in_flight; 11812aa4082SMax Reitz CoQueue waiting_requests; 119eed325b9SKevin Wolf Coroutine *co; 120d44dae1aSVladimir Sementsov-Ogievskiy MirrorOp *waiting_for_op; 12112aa4082SMax Reitz 12212aa4082SMax Reitz QTAILQ_ENTRY(MirrorOp) next; 12312aa4082SMax Reitz }; 124bd48bde8SPaolo Bonzini 1254295c5fcSMax Reitz typedef enum MirrorMethod { 1264295c5fcSMax Reitz MIRROR_METHOD_COPY, 1274295c5fcSMax Reitz MIRROR_METHOD_ZERO, 1284295c5fcSMax Reitz MIRROR_METHOD_DISCARD, 1294295c5fcSMax Reitz } MirrorMethod; 1304295c5fcSMax Reitz 131b952b558SPaolo Bonzini static BlockErrorAction mirror_error_action(MirrorBlockJob *s, bool read, 132b952b558SPaolo Bonzini int error) 133b952b558SPaolo Bonzini { 13476cb2f24SFiona Ebner qatomic_set(&s->actively_synced, false); 135b952b558SPaolo Bonzini if (read) { 13681e254dcSKevin Wolf return block_job_error_action(&s->common, s->on_source_error, 13781e254dcSKevin Wolf true, error); 138b952b558SPaolo Bonzini } else { 13981e254dcSKevin Wolf return block_job_error_action(&s->common, s->on_target_error, 14081e254dcSKevin Wolf false, error); 141b952b558SPaolo Bonzini } 142b952b558SPaolo Bonzini } 143b952b558SPaolo Bonzini 1441181e19aSMax Reitz static void coroutine_fn mirror_wait_on_conflicts(MirrorOp *self, 1451181e19aSMax Reitz MirrorBlockJob *s, 1461181e19aSMax Reitz uint64_t offset, 1471181e19aSMax Reitz uint64_t bytes) 1481181e19aSMax Reitz { 1491181e19aSMax Reitz uint64_t self_start_chunk = offset / s->granularity; 1501181e19aSMax Reitz uint64_t self_end_chunk = DIV_ROUND_UP(offset + bytes, s->granularity); 1511181e19aSMax Reitz uint64_t self_nb_chunks = self_end_chunk - self_start_chunk; 1521181e19aSMax Reitz 1531181e19aSMax Reitz while (find_next_bit(s->in_flight_bitmap, self_end_chunk, 1541181e19aSMax Reitz self_start_chunk) < self_end_chunk && 1551181e19aSMax Reitz s->ret >= 0) 1561181e19aSMax Reitz { 1571181e19aSMax Reitz MirrorOp *op; 1581181e19aSMax Reitz 1591181e19aSMax Reitz QTAILQ_FOREACH(op, &s->ops_in_flight, next) { 1601181e19aSMax Reitz uint64_t op_start_chunk = op->offset / s->granularity; 1611181e19aSMax Reitz uint64_t op_nb_chunks = DIV_ROUND_UP(op->offset + op->bytes, 1621181e19aSMax Reitz s->granularity) - 1631181e19aSMax Reitz op_start_chunk; 1641181e19aSMax Reitz 1651181e19aSMax Reitz if (op == self) { 1661181e19aSMax Reitz continue; 1671181e19aSMax Reitz } 1681181e19aSMax Reitz 1691181e19aSMax Reitz if (ranges_overlap(self_start_chunk, self_nb_chunks, 1701181e19aSMax Reitz op_start_chunk, op_nb_chunks)) 1711181e19aSMax Reitz { 17266fed30cSStefano Garzarella if (self) { 173d44dae1aSVladimir Sementsov-Ogievskiy /* 17466fed30cSStefano Garzarella * If the operation is already (indirectly) waiting for us, 17566fed30cSStefano Garzarella * or will wait for us as soon as it wakes up, then just go 17666fed30cSStefano Garzarella * on (instead of producing a deadlock in the former case). 177d44dae1aSVladimir Sementsov-Ogievskiy */ 178d44dae1aSVladimir Sementsov-Ogievskiy if (op->waiting_for_op) { 179d44dae1aSVladimir Sementsov-Ogievskiy continue; 180d44dae1aSVladimir Sementsov-Ogievskiy } 181d44dae1aSVladimir Sementsov-Ogievskiy 182d44dae1aSVladimir Sementsov-Ogievskiy self->waiting_for_op = op; 18366fed30cSStefano Garzarella } 18466fed30cSStefano Garzarella 1851181e19aSMax Reitz qemu_co_queue_wait(&op->waiting_requests, NULL); 18666fed30cSStefano Garzarella 18766fed30cSStefano Garzarella if (self) { 188d44dae1aSVladimir Sementsov-Ogievskiy self->waiting_for_op = NULL; 18966fed30cSStefano Garzarella } 19066fed30cSStefano Garzarella 1911181e19aSMax Reitz break; 1921181e19aSMax Reitz } 1931181e19aSMax Reitz } 1941181e19aSMax Reitz } 1951181e19aSMax Reitz } 1961181e19aSMax Reitz 1972e1990b2SMax Reitz static void coroutine_fn mirror_iteration_done(MirrorOp *op, int ret) 198bd48bde8SPaolo Bonzini { 199bd48bde8SPaolo Bonzini MirrorBlockJob *s = op->s; 200402a4741SPaolo Bonzini struct iovec *iov; 201bd48bde8SPaolo Bonzini int64_t chunk_num; 202b436982fSEric Blake int i, nb_chunks; 203bd48bde8SPaolo Bonzini 204b436982fSEric Blake trace_mirror_iteration_done(s, op->offset, op->bytes, ret); 205bd48bde8SPaolo Bonzini 206bd48bde8SPaolo Bonzini s->in_flight--; 207b436982fSEric Blake s->bytes_in_flight -= op->bytes; 208402a4741SPaolo Bonzini iov = op->qiov.iov; 209402a4741SPaolo Bonzini for (i = 0; i < op->qiov.niov; i++) { 210402a4741SPaolo Bonzini MirrorBuffer *buf = (MirrorBuffer *) iov[i].iov_base; 211402a4741SPaolo Bonzini QSIMPLEQ_INSERT_TAIL(&s->buf_free, buf, next); 212402a4741SPaolo Bonzini s->buf_free_count++; 213402a4741SPaolo Bonzini } 214402a4741SPaolo Bonzini 215b436982fSEric Blake chunk_num = op->offset / s->granularity; 216b436982fSEric Blake nb_chunks = DIV_ROUND_UP(op->bytes, s->granularity); 21712aa4082SMax Reitz 218402a4741SPaolo Bonzini bitmap_clear(s->in_flight_bitmap, chunk_num, nb_chunks); 21912aa4082SMax Reitz QTAILQ_REMOVE(&s->ops_in_flight, op, next); 220b21c7652SMax Reitz if (ret >= 0) { 221b21c7652SMax Reitz if (s->cow_bitmap) { 222bd48bde8SPaolo Bonzini bitmap_set(s->cow_bitmap, chunk_num, nb_chunks); 223bd48bde8SPaolo Bonzini } 22490ab48ebSAnton Nefedov if (!s->initial_zeroing_ongoing) { 22530a5c887SKevin Wolf job_progress_update(&s->common.job, op->bytes); 226b21c7652SMax Reitz } 22790ab48ebSAnton Nefedov } 2286df3bf8eSZhang Min qemu_iovec_destroy(&op->qiov); 2297b770c72SStefan Hajnoczi 23012aa4082SMax Reitz qemu_co_queue_restart_all(&op->waiting_requests); 23112aa4082SMax Reitz g_free(op); 2327b770c72SStefan Hajnoczi } 233bd48bde8SPaolo Bonzini 2342e1990b2SMax Reitz static void coroutine_fn mirror_write_complete(MirrorOp *op, int ret) 235bd48bde8SPaolo Bonzini { 236bd48bde8SPaolo Bonzini MirrorBlockJob *s = op->s; 237b9e413ddSPaolo Bonzini 238bd48bde8SPaolo Bonzini if (ret < 0) { 239bd48bde8SPaolo Bonzini BlockErrorAction action; 240bd48bde8SPaolo Bonzini 241e0d7f73eSEric Blake bdrv_set_dirty_bitmap(s->dirty_bitmap, op->offset, op->bytes); 242bd48bde8SPaolo Bonzini action = mirror_error_action(s, false, -ret); 243a589569fSWenchao Xia if (action == BLOCK_ERROR_ACTION_REPORT && s->ret >= 0) { 244bd48bde8SPaolo Bonzini s->ret = ret; 245bd48bde8SPaolo Bonzini } 246bd48bde8SPaolo Bonzini } 247d12ade57SVladimir Sementsov-Ogievskiy 248bd48bde8SPaolo Bonzini mirror_iteration_done(op, ret); 249bd48bde8SPaolo Bonzini } 250bd48bde8SPaolo Bonzini 2512e1990b2SMax Reitz static void coroutine_fn mirror_read_complete(MirrorOp *op, int ret) 252bd48bde8SPaolo Bonzini { 253bd48bde8SPaolo Bonzini MirrorBlockJob *s = op->s; 254b9e413ddSPaolo Bonzini 255bd48bde8SPaolo Bonzini if (ret < 0) { 256bd48bde8SPaolo Bonzini BlockErrorAction action; 257bd48bde8SPaolo Bonzini 258e0d7f73eSEric Blake bdrv_set_dirty_bitmap(s->dirty_bitmap, op->offset, op->bytes); 259bd48bde8SPaolo Bonzini action = mirror_error_action(s, true, -ret); 260a589569fSWenchao Xia if (action == BLOCK_ERROR_ACTION_REPORT && s->ret >= 0) { 261bd48bde8SPaolo Bonzini s->ret = ret; 262bd48bde8SPaolo Bonzini } 263bd48bde8SPaolo Bonzini 264bd48bde8SPaolo Bonzini mirror_iteration_done(op, ret); 265d12ade57SVladimir Sementsov-Ogievskiy return; 266bd48bde8SPaolo Bonzini } 267d12ade57SVladimir Sementsov-Ogievskiy 268d12ade57SVladimir Sementsov-Ogievskiy ret = blk_co_pwritev(s->target, op->offset, op->qiov.size, &op->qiov, 0); 269d12ade57SVladimir Sementsov-Ogievskiy mirror_write_complete(op, ret); 270b9e413ddSPaolo Bonzini } 271bd48bde8SPaolo Bonzini 272782d97efSEric Blake /* Clip bytes relative to offset to not exceed end-of-file */ 273782d97efSEric Blake static inline int64_t mirror_clip_bytes(MirrorBlockJob *s, 274782d97efSEric Blake int64_t offset, 275782d97efSEric Blake int64_t bytes) 276782d97efSEric Blake { 277782d97efSEric Blake return MIN(bytes, s->bdev_length - offset); 278782d97efSEric Blake } 279782d97efSEric Blake 280782d97efSEric Blake /* Round offset and/or bytes to target cluster if COW is needed, and 281782d97efSEric Blake * return the offset of the adjusted tail against original. */ 28217ac39c3SPaolo Bonzini static int coroutine_fn mirror_cow_align(MirrorBlockJob *s, int64_t *offset, 283ae4cc877SEric Blake uint64_t *bytes) 284893f7ebaSPaolo Bonzini { 285e5b43573SFam Zheng bool need_cow; 286e5b43573SFam Zheng int ret = 0; 287782d97efSEric Blake int64_t align_offset = *offset; 2887cfd5275SEric Blake int64_t align_bytes = *bytes; 289782d97efSEric Blake int max_bytes = s->granularity * s->max_iov; 290893f7ebaSPaolo Bonzini 291782d97efSEric Blake need_cow = !test_bit(*offset / s->granularity, s->cow_bitmap); 292782d97efSEric Blake need_cow |= !test_bit((*offset + *bytes - 1) / s->granularity, 293e5b43573SFam Zheng s->cow_bitmap); 294e5b43573SFam Zheng if (need_cow) { 295fc6b211fSAndrey Drobyshev bdrv_round_to_subclusters(blk_bs(s->target), *offset, *bytes, 296782d97efSEric Blake &align_offset, &align_bytes); 2978f0720ecSPaolo Bonzini } 2988f0720ecSPaolo Bonzini 299782d97efSEric Blake if (align_bytes > max_bytes) { 300782d97efSEric Blake align_bytes = max_bytes; 301e5b43573SFam Zheng if (need_cow) { 302782d97efSEric Blake align_bytes = QEMU_ALIGN_DOWN(align_bytes, s->target_cluster_size); 303e5b43573SFam Zheng } 304e5b43573SFam Zheng } 305782d97efSEric Blake /* Clipping may result in align_bytes unaligned to chunk boundary, but 3064150ae60SFam Zheng * that doesn't matter because it's already the end of source image. */ 307782d97efSEric Blake align_bytes = mirror_clip_bytes(s, align_offset, align_bytes); 308402a4741SPaolo Bonzini 309782d97efSEric Blake ret = align_offset + align_bytes - (*offset + *bytes); 310782d97efSEric Blake *offset = align_offset; 311782d97efSEric Blake *bytes = align_bytes; 312e5b43573SFam Zheng assert(ret >= 0); 313e5b43573SFam Zheng return ret; 314e5b43573SFam Zheng } 315e5b43573SFam Zheng 316537c3d4fSStefan Hajnoczi static inline void coroutine_fn 317eb994912SHanna Reitz mirror_wait_for_free_in_flight_slot(MirrorBlockJob *s) 31821cd917fSFam Zheng { 31912aa4082SMax Reitz MirrorOp *op; 32012aa4082SMax Reitz 3211181e19aSMax Reitz QTAILQ_FOREACH(op, &s->ops_in_flight, next) { 322eb994912SHanna Reitz /* 323eb994912SHanna Reitz * Do not wait on pseudo ops, because it may in turn wait on 3241181e19aSMax Reitz * some other operation to start, which may in fact be the 3251181e19aSMax Reitz * caller of this function. Since there is only one pseudo op 3261181e19aSMax Reitz * at any given time, we will always find some real operation 327eb994912SHanna Reitz * to wait on. 328eb994912SHanna Reitz * Also, do not wait on active operations, because they do not 329eb994912SHanna Reitz * use up in-flight slots. 330eb994912SHanna Reitz */ 331eb994912SHanna Reitz if (!op->is_pseudo_op && op->is_in_flight && !op->is_active_write) { 33212aa4082SMax Reitz qemu_co_queue_wait(&op->waiting_requests, NULL); 3331181e19aSMax Reitz return; 3341181e19aSMax Reitz } 3351181e19aSMax Reitz } 3361181e19aSMax Reitz abort(); 33721cd917fSFam Zheng } 33821cd917fSFam Zheng 3392e1990b2SMax Reitz /* Perform a mirror copy operation. 3402e1990b2SMax Reitz * 3412e1990b2SMax Reitz * *op->bytes_handled is set to the number of bytes copied after and 3422e1990b2SMax Reitz * including offset, excluding any bytes copied prior to offset due 3432e1990b2SMax Reitz * to alignment. This will be op->bytes if no alignment is necessary, 3442e1990b2SMax Reitz * or (new_end - op->offset) if the tail is rounded up or down due to 345e5b43573SFam Zheng * alignment or buffer limit. 346402a4741SPaolo Bonzini */ 3472e1990b2SMax Reitz static void coroutine_fn mirror_co_read(void *opaque) 348e5b43573SFam Zheng { 3492e1990b2SMax Reitz MirrorOp *op = opaque; 3502e1990b2SMax Reitz MirrorBlockJob *s = op->s; 351ae4cc877SEric Blake int nb_chunks; 3525791ba52SMarc-André Lureau int ret = -1; 353ae4cc877SEric Blake uint64_t max_bytes; 354402a4741SPaolo Bonzini 355ae4cc877SEric Blake max_bytes = s->granularity * s->max_iov; 356e5b43573SFam Zheng 357e5b43573SFam Zheng /* We can only handle as much as buf_size at a time. */ 3582e1990b2SMax Reitz op->bytes = MIN(s->buf_size, MIN(max_bytes, op->bytes)); 3592e1990b2SMax Reitz assert(op->bytes); 3602e1990b2SMax Reitz assert(op->bytes < BDRV_REQUEST_MAX_BYTES); 3612e1990b2SMax Reitz *op->bytes_handled = op->bytes; 362e5b43573SFam Zheng 363e5b43573SFam Zheng if (s->cow_bitmap) { 3642e1990b2SMax Reitz *op->bytes_handled += mirror_cow_align(s, &op->offset, &op->bytes); 365e5b43573SFam Zheng } 3662e1990b2SMax Reitz /* Cannot exceed BDRV_REQUEST_MAX_BYTES + INT_MAX */ 3672e1990b2SMax Reitz assert(*op->bytes_handled <= UINT_MAX); 3682e1990b2SMax Reitz assert(op->bytes <= s->buf_size); 369ae4cc877SEric Blake /* The offset is granularity-aligned because: 370e5b43573SFam Zheng * 1) Caller passes in aligned values; 371e5b43573SFam Zheng * 2) mirror_cow_align is used only when target cluster is larger. */ 3722e1990b2SMax Reitz assert(QEMU_IS_ALIGNED(op->offset, s->granularity)); 373ae4cc877SEric Blake /* The range is sector-aligned, since bdrv_getlength() rounds up. */ 3742e1990b2SMax Reitz assert(QEMU_IS_ALIGNED(op->bytes, BDRV_SECTOR_SIZE)); 3752e1990b2SMax Reitz nb_chunks = DIV_ROUND_UP(op->bytes, s->granularity); 376e5b43573SFam Zheng 377e5b43573SFam Zheng while (s->buf_free_count < nb_chunks) { 3782e1990b2SMax Reitz trace_mirror_yield_in_flight(s, op->offset, s->in_flight); 3799178f4feSKevin Wolf mirror_wait_for_free_in_flight_slot(s); 380b812f671SPaolo Bonzini } 381b812f671SPaolo Bonzini 382402a4741SPaolo Bonzini /* Now make a QEMUIOVector taking enough granularity-sized chunks 383402a4741SPaolo Bonzini * from s->buf_free. 384402a4741SPaolo Bonzini */ 385402a4741SPaolo Bonzini qemu_iovec_init(&op->qiov, nb_chunks); 386402a4741SPaolo Bonzini while (nb_chunks-- > 0) { 387402a4741SPaolo Bonzini MirrorBuffer *buf = QSIMPLEQ_FIRST(&s->buf_free); 3882e1990b2SMax Reitz size_t remaining = op->bytes - op->qiov.size; 3895a0f6fd5SKevin Wolf 390402a4741SPaolo Bonzini QSIMPLEQ_REMOVE_HEAD(&s->buf_free, next); 391402a4741SPaolo Bonzini s->buf_free_count--; 3925a0f6fd5SKevin Wolf qemu_iovec_add(&op->qiov, buf, MIN(s->granularity, remaining)); 393402a4741SPaolo Bonzini } 394402a4741SPaolo Bonzini 395893f7ebaSPaolo Bonzini /* Copy the dirty cluster. */ 396bd48bde8SPaolo Bonzini s->in_flight++; 3972e1990b2SMax Reitz s->bytes_in_flight += op->bytes; 398ce8cabbdSKevin Wolf op->is_in_flight = true; 3992e1990b2SMax Reitz trace_mirror_one_iteration(s, op->offset, op->bytes); 400dcfb3bebSFam Zheng 401b9b10c35SKevin Wolf WITH_GRAPH_RDLOCK_GUARD() { 402138f9fffSMax Reitz ret = bdrv_co_preadv(s->mirror_top_bs->backing, op->offset, op->bytes, 403138f9fffSMax Reitz &op->qiov, 0); 404b9b10c35SKevin Wolf } 4052e1990b2SMax Reitz mirror_read_complete(op, ret); 406e5b43573SFam Zheng } 407e5b43573SFam Zheng 4082e1990b2SMax Reitz static void coroutine_fn mirror_co_zero(void *opaque) 409e5b43573SFam Zheng { 4102e1990b2SMax Reitz MirrorOp *op = opaque; 4112e1990b2SMax Reitz int ret; 412e5b43573SFam Zheng 4132e1990b2SMax Reitz op->s->in_flight++; 4142e1990b2SMax Reitz op->s->bytes_in_flight += op->bytes; 4152e1990b2SMax Reitz *op->bytes_handled = op->bytes; 416ce8cabbdSKevin Wolf op->is_in_flight = true; 417e5b43573SFam Zheng 4182e1990b2SMax Reitz ret = blk_co_pwrite_zeroes(op->s->target, op->offset, op->bytes, 4192e1990b2SMax Reitz op->s->unmap ? BDRV_REQ_MAY_UNMAP : 0); 4202e1990b2SMax Reitz mirror_write_complete(op, ret); 421e5b43573SFam Zheng } 4222e1990b2SMax Reitz 4232e1990b2SMax Reitz static void coroutine_fn mirror_co_discard(void *opaque) 4242e1990b2SMax Reitz { 4252e1990b2SMax Reitz MirrorOp *op = opaque; 4262e1990b2SMax Reitz int ret; 4272e1990b2SMax Reitz 4282e1990b2SMax Reitz op->s->in_flight++; 4292e1990b2SMax Reitz op->s->bytes_in_flight += op->bytes; 4302e1990b2SMax Reitz *op->bytes_handled = op->bytes; 431ce8cabbdSKevin Wolf op->is_in_flight = true; 4322e1990b2SMax Reitz 4332e1990b2SMax Reitz ret = blk_co_pdiscard(op->s->target, op->offset, op->bytes); 4342e1990b2SMax Reitz mirror_write_complete(op, ret); 435e5b43573SFam Zheng } 436e5b43573SFam Zheng 4374295c5fcSMax Reitz static unsigned mirror_perform(MirrorBlockJob *s, int64_t offset, 4384295c5fcSMax Reitz unsigned bytes, MirrorMethod mirror_method) 4394295c5fcSMax Reitz { 4402e1990b2SMax Reitz MirrorOp *op; 4412e1990b2SMax Reitz Coroutine *co; 4422e1990b2SMax Reitz int64_t bytes_handled = -1; 4432e1990b2SMax Reitz 4442e1990b2SMax Reitz op = g_new(MirrorOp, 1); 4452e1990b2SMax Reitz *op = (MirrorOp){ 4462e1990b2SMax Reitz .s = s, 4472e1990b2SMax Reitz .offset = offset, 4482e1990b2SMax Reitz .bytes = bytes, 4492e1990b2SMax Reitz .bytes_handled = &bytes_handled, 4502e1990b2SMax Reitz }; 45112aa4082SMax Reitz qemu_co_queue_init(&op->waiting_requests); 4522e1990b2SMax Reitz 4534295c5fcSMax Reitz switch (mirror_method) { 4544295c5fcSMax Reitz case MIRROR_METHOD_COPY: 4552e1990b2SMax Reitz co = qemu_coroutine_create(mirror_co_read, op); 4562e1990b2SMax Reitz break; 4574295c5fcSMax Reitz case MIRROR_METHOD_ZERO: 4582e1990b2SMax Reitz co = qemu_coroutine_create(mirror_co_zero, op); 4592e1990b2SMax Reitz break; 4604295c5fcSMax Reitz case MIRROR_METHOD_DISCARD: 4612e1990b2SMax Reitz co = qemu_coroutine_create(mirror_co_discard, op); 4622e1990b2SMax Reitz break; 4634295c5fcSMax Reitz default: 4644295c5fcSMax Reitz abort(); 4654295c5fcSMax Reitz } 466eed325b9SKevin Wolf op->co = co; 4672e1990b2SMax Reitz 46812aa4082SMax Reitz QTAILQ_INSERT_TAIL(&s->ops_in_flight, op, next); 4692e1990b2SMax Reitz qemu_coroutine_enter(co); 4702e1990b2SMax Reitz /* At this point, ownership of op has been moved to the coroutine 4712e1990b2SMax Reitz * and the object may already be freed */ 4722e1990b2SMax Reitz 4732e1990b2SMax Reitz /* Assert that this value has been set */ 4742e1990b2SMax Reitz assert(bytes_handled >= 0); 4752e1990b2SMax Reitz 4762e1990b2SMax Reitz /* Same assertion as in mirror_co_read() (and for mirror_co_read() 4772e1990b2SMax Reitz * and mirror_co_discard(), bytes_handled == op->bytes, which 4782e1990b2SMax Reitz * is the @bytes parameter given to this function) */ 4792e1990b2SMax Reitz assert(bytes_handled <= UINT_MAX); 4802e1990b2SMax Reitz return bytes_handled; 4814295c5fcSMax Reitz } 4824295c5fcSMax Reitz 483ae5a40e8SKevin Wolf static void coroutine_fn GRAPH_UNLOCKED mirror_iteration(MirrorBlockJob *s) 484e5b43573SFam Zheng { 485ae5a40e8SKevin Wolf BlockDriverState *source; 4861181e19aSMax Reitz MirrorOp *pseudo_op; 4871181e19aSMax Reitz int64_t offset; 488e5b43573SFam Zheng /* At least the first dirty chunk is mirrored in one iteration. */ 489e5b43573SFam Zheng int nb_chunks = 1; 4904b5004d9SDenis V. Lunev bool write_zeroes_ok = bdrv_can_write_zeroes_with_unmap(blk_bs(s->target)); 491b436982fSEric Blake int max_io_bytes = MAX(s->buf_size / MAX_IN_FLIGHT, MAX_IO_BYTES); 492e5b43573SFam Zheng 493ae5a40e8SKevin Wolf bdrv_graph_co_rdlock(); 494ae5a40e8SKevin Wolf source = s->mirror_top_bs->backing->bs; 495ae5a40e8SKevin Wolf bdrv_graph_co_rdunlock(); 496ae5a40e8SKevin Wolf 497b64bd51eSPaolo Bonzini bdrv_dirty_bitmap_lock(s->dirty_bitmap); 498f798184cSEric Blake offset = bdrv_dirty_iter_next(s->dbi); 499fb2ef791SEric Blake if (offset < 0) { 500dc162c8eSFam Zheng bdrv_set_dirty_iter(s->dbi, 0); 501f798184cSEric Blake offset = bdrv_dirty_iter_next(s->dbi); 5029a46dba7SEric Blake trace_mirror_restart_iter(s, bdrv_get_dirty_count(s->dirty_bitmap)); 503fb2ef791SEric Blake assert(offset >= 0); 504e5b43573SFam Zheng } 505b64bd51eSPaolo Bonzini bdrv_dirty_bitmap_unlock(s->dirty_bitmap); 506e5b43573SFam Zheng 507d69a879bSHanna Reitz /* 508d69a879bSHanna Reitz * Wait for concurrent requests to @offset. The next loop will limit the 509d69a879bSHanna Reitz * copied area based on in_flight_bitmap so we only copy an area that does 510d69a879bSHanna Reitz * not overlap with concurrent in-flight requests. Still, we would like to 511d69a879bSHanna Reitz * copy something, so wait until there are at least no more requests to the 512d69a879bSHanna Reitz * very beginning of the area. 513d69a879bSHanna Reitz */ 5141181e19aSMax Reitz mirror_wait_on_conflicts(NULL, s, offset, 1); 5159c83625bSMax Reitz 516da01ff7fSKevin Wolf job_pause_point(&s->common.job); 517565ac01fSStefan Hajnoczi 5183202d8e4SMichael Tokarev /* Find the number of consecutive dirty chunks following the first dirty 519e5b43573SFam Zheng * one, and wait for in flight requests in them. */ 520b64bd51eSPaolo Bonzini bdrv_dirty_bitmap_lock(s->dirty_bitmap); 521fb2ef791SEric Blake while (nb_chunks * s->granularity < s->buf_size) { 522dc162c8eSFam Zheng int64_t next_dirty; 523fb2ef791SEric Blake int64_t next_offset = offset + nb_chunks * s->granularity; 524fb2ef791SEric Blake int64_t next_chunk = next_offset / s->granularity; 525fb2ef791SEric Blake if (next_offset >= s->bdev_length || 52628636b82SJohn Snow !bdrv_dirty_bitmap_get_locked(s->dirty_bitmap, next_offset)) { 527e5b43573SFam Zheng break; 528e5b43573SFam Zheng } 529e5b43573SFam Zheng if (test_bit(next_chunk, s->in_flight_bitmap)) { 530e5b43573SFam Zheng break; 531e5b43573SFam Zheng } 5329c83625bSMax Reitz 533f798184cSEric Blake next_dirty = bdrv_dirty_iter_next(s->dbi); 534fb2ef791SEric Blake if (next_dirty > next_offset || next_dirty < 0) { 535f27a2742SMax Reitz /* The bitmap iterator's cache is stale, refresh it */ 536715a74d8SEric Blake bdrv_set_dirty_iter(s->dbi, next_offset); 537f798184cSEric Blake next_dirty = bdrv_dirty_iter_next(s->dbi); 538f27a2742SMax Reitz } 539fb2ef791SEric Blake assert(next_dirty == next_offset); 540e5b43573SFam Zheng nb_chunks++; 541e5b43573SFam Zheng } 542e5b43573SFam Zheng 543e5b43573SFam Zheng /* Clear dirty bits before querying the block status, because 54431826642SEric Blake * calling bdrv_block_status_above could yield - if some blocks are 545e5b43573SFam Zheng * marked dirty in this window, we need to know. 546e5b43573SFam Zheng */ 547e0d7f73eSEric Blake bdrv_reset_dirty_bitmap_locked(s->dirty_bitmap, offset, 548e0d7f73eSEric Blake nb_chunks * s->granularity); 549b64bd51eSPaolo Bonzini bdrv_dirty_bitmap_unlock(s->dirty_bitmap); 550b64bd51eSPaolo Bonzini 5511181e19aSMax Reitz /* Before claiming an area in the in-flight bitmap, we have to 5521181e19aSMax Reitz * create a MirrorOp for it so that conflicting requests can wait 5531181e19aSMax Reitz * for it. mirror_perform() will create the real MirrorOps later, 5541181e19aSMax Reitz * for now we just create a pseudo operation that will wake up all 5551181e19aSMax Reitz * conflicting requests once all real operations have been 5561181e19aSMax Reitz * launched. */ 5571181e19aSMax Reitz pseudo_op = g_new(MirrorOp, 1); 5581181e19aSMax Reitz *pseudo_op = (MirrorOp){ 5591181e19aSMax Reitz .offset = offset, 5601181e19aSMax Reitz .bytes = nb_chunks * s->granularity, 5611181e19aSMax Reitz .is_pseudo_op = true, 5621181e19aSMax Reitz }; 5631181e19aSMax Reitz qemu_co_queue_init(&pseudo_op->waiting_requests); 5641181e19aSMax Reitz QTAILQ_INSERT_TAIL(&s->ops_in_flight, pseudo_op, next); 5651181e19aSMax Reitz 566fb2ef791SEric Blake bitmap_set(s->in_flight_bitmap, offset / s->granularity, nb_chunks); 567fb2ef791SEric Blake while (nb_chunks > 0 && offset < s->bdev_length) { 5685791ba52SMarc-André Lureau int ret = -1; 5697cfd5275SEric Blake int64_t io_bytes; 570f3e4ce4aSEric Blake int64_t io_bytes_acct; 5714295c5fcSMax Reitz MirrorMethod mirror_method = MIRROR_METHOD_COPY; 572e5b43573SFam Zheng 573fb2ef791SEric Blake assert(!(offset % s->granularity)); 5747ff9579eSKevin Wolf WITH_GRAPH_RDLOCK_GUARD() { 575cc323997SPaolo Bonzini ret = bdrv_co_block_status_above(source, NULL, offset, 57631826642SEric Blake nb_chunks * s->granularity, 57731826642SEric Blake &io_bytes, NULL, NULL); 5787ff9579eSKevin Wolf } 579e5b43573SFam Zheng if (ret < 0) { 580fb2ef791SEric Blake io_bytes = MIN(nb_chunks * s->granularity, max_io_bytes); 5810965a41eSVladimir Sementsov-Ogievskiy } else if (ret & BDRV_BLOCK_DATA) { 582fb2ef791SEric Blake io_bytes = MIN(io_bytes, max_io_bytes); 583e5b43573SFam Zheng } 584e5b43573SFam Zheng 585fb2ef791SEric Blake io_bytes -= io_bytes % s->granularity; 586fb2ef791SEric Blake if (io_bytes < s->granularity) { 587fb2ef791SEric Blake io_bytes = s->granularity; 588e5b43573SFam Zheng } else if (ret >= 0 && !(ret & BDRV_BLOCK_DATA)) { 589fb2ef791SEric Blake int64_t target_offset; 5907cfd5275SEric Blake int64_t target_bytes; 591a00e70c0SEmanuele Giuseppe Esposito WITH_GRAPH_RDLOCK_GUARD() { 592fc6b211fSAndrey Drobyshev bdrv_round_to_subclusters(blk_bs(s->target), offset, io_bytes, 593fb2ef791SEric Blake &target_offset, &target_bytes); 594a00e70c0SEmanuele Giuseppe Esposito } 595fb2ef791SEric Blake if (target_offset == offset && 596fb2ef791SEric Blake target_bytes == io_bytes) { 597e5b43573SFam Zheng mirror_method = ret & BDRV_BLOCK_ZERO ? 598e5b43573SFam Zheng MIRROR_METHOD_ZERO : 599e5b43573SFam Zheng MIRROR_METHOD_DISCARD; 600e5b43573SFam Zheng } 601e5b43573SFam Zheng } 602e5b43573SFam Zheng 603cf56a3c6SDenis V. Lunev while (s->in_flight >= MAX_IN_FLIGHT) { 604fb2ef791SEric Blake trace_mirror_yield_in_flight(s, offset, s->in_flight); 6059178f4feSKevin Wolf mirror_wait_for_free_in_flight_slot(s); 606cf56a3c6SDenis V. Lunev } 607cf56a3c6SDenis V. Lunev 608dbaa7b57SVladimir Sementsov-Ogievskiy if (s->ret < 0) { 6091181e19aSMax Reitz ret = 0; 6101181e19aSMax Reitz goto fail; 611dbaa7b57SVladimir Sementsov-Ogievskiy } 612dbaa7b57SVladimir Sementsov-Ogievskiy 613fb2ef791SEric Blake io_bytes = mirror_clip_bytes(s, offset, io_bytes); 6144295c5fcSMax Reitz io_bytes = mirror_perform(s, offset, io_bytes, mirror_method); 6154295c5fcSMax Reitz if (mirror_method != MIRROR_METHOD_COPY && write_zeroes_ok) { 616f3e4ce4aSEric Blake io_bytes_acct = 0; 6174b5004d9SDenis V. Lunev } else { 618fb2ef791SEric Blake io_bytes_acct = io_bytes; 6194b5004d9SDenis V. Lunev } 620fb2ef791SEric Blake assert(io_bytes); 621fb2ef791SEric Blake offset += io_bytes; 622fb2ef791SEric Blake nb_chunks -= DIV_ROUND_UP(io_bytes, s->granularity); 623018e5987SKevin Wolf block_job_ratelimit_processed_bytes(&s->common, io_bytes_acct); 624dcfb3bebSFam Zheng } 6251181e19aSMax Reitz 6261181e19aSMax Reitz fail: 6271181e19aSMax Reitz QTAILQ_REMOVE(&s->ops_in_flight, pseudo_op, next); 6281181e19aSMax Reitz qemu_co_queue_restart_all(&pseudo_op->waiting_requests); 6291181e19aSMax Reitz g_free(pseudo_op); 630893f7ebaSPaolo Bonzini } 631b952b558SPaolo Bonzini 632402a4741SPaolo Bonzini static void mirror_free_init(MirrorBlockJob *s) 633402a4741SPaolo Bonzini { 634402a4741SPaolo Bonzini int granularity = s->granularity; 635402a4741SPaolo Bonzini size_t buf_size = s->buf_size; 636402a4741SPaolo Bonzini uint8_t *buf = s->buf; 637402a4741SPaolo Bonzini 638402a4741SPaolo Bonzini assert(s->buf_free_count == 0); 639402a4741SPaolo Bonzini QSIMPLEQ_INIT(&s->buf_free); 640402a4741SPaolo Bonzini while (buf_size != 0) { 641402a4741SPaolo Bonzini MirrorBuffer *cur = (MirrorBuffer *)buf; 642402a4741SPaolo Bonzini QSIMPLEQ_INSERT_TAIL(&s->buf_free, cur, next); 643402a4741SPaolo Bonzini s->buf_free_count++; 644402a4741SPaolo Bonzini buf_size -= granularity; 645402a4741SPaolo Bonzini buf += granularity; 646402a4741SPaolo Bonzini } 647402a4741SPaolo Bonzini } 648402a4741SPaolo Bonzini 649bae8196dSPaolo Bonzini /* This is also used for the .pause callback. There is no matching 650bae8196dSPaolo Bonzini * mirror_resume() because mirror_run() will begin iterating again 651bae8196dSPaolo Bonzini * when the job is resumed. 652bae8196dSPaolo Bonzini */ 653537c3d4fSStefan Hajnoczi static void coroutine_fn mirror_wait_for_all_io(MirrorBlockJob *s) 654bd48bde8SPaolo Bonzini { 655bd48bde8SPaolo Bonzini while (s->in_flight > 0) { 6569178f4feSKevin Wolf mirror_wait_for_free_in_flight_slot(s); 657bd48bde8SPaolo Bonzini } 658893f7ebaSPaolo Bonzini } 659893f7ebaSPaolo Bonzini 660737efc1eSJohn Snow /** 661737efc1eSJohn Snow * mirror_exit_common: handle both abort() and prepare() cases. 662737efc1eSJohn Snow * for .prepare, returns 0 on success and -errno on failure. 663737efc1eSJohn Snow * for .abort cases, denoted by abort = true, MUST return 0. 664737efc1eSJohn Snow */ 665737efc1eSJohn Snow static int mirror_exit_common(Job *job) 6665a7e7a0bSStefan Hajnoczi { 6671908a559SKevin Wolf MirrorBlockJob *s = container_of(job, MirrorBlockJob, common.job); 6681908a559SKevin Wolf BlockJob *bjob = &s->common; 669f93c3addSMax Reitz MirrorBDSOpaque *bs_opaque; 670f93c3addSMax Reitz BlockDriverState *src; 671f93c3addSMax Reitz BlockDriverState *target_bs; 672f93c3addSMax Reitz BlockDriverState *mirror_top_bs; 67312fa4af6SKevin Wolf Error *local_err = NULL; 674737efc1eSJohn Snow bool abort = job->ret < 0; 675737efc1eSJohn Snow int ret = 0; 676737efc1eSJohn Snow 6772626d27fSKevin Wolf GLOBAL_STATE_CODE(); 6782626d27fSKevin Wolf 679737efc1eSJohn Snow if (s->prepared) { 680737efc1eSJohn Snow return 0; 681737efc1eSJohn Snow } 682737efc1eSJohn Snow s->prepared = true; 6833f09bfbcSKevin Wolf 6849275fc72SKevin Wolf bdrv_graph_rdlock_main_loop(); 6852626d27fSKevin Wolf 686f93c3addSMax Reitz mirror_top_bs = s->mirror_top_bs; 687f93c3addSMax Reitz bs_opaque = mirror_top_bs->opaque; 688f93c3addSMax Reitz src = mirror_top_bs->backing->bs; 689f93c3addSMax Reitz target_bs = blk_bs(s->target); 690f93c3addSMax Reitz 691ef53dc09SAlberto Garcia if (bdrv_chain_contains(src, target_bs)) { 692ef53dc09SAlberto Garcia bdrv_unfreeze_backing_chain(mirror_top_bs, target_bs); 693ef53dc09SAlberto Garcia } 694ef53dc09SAlberto Garcia 6955deb6cbdSVladimir Sementsov-Ogievskiy bdrv_release_dirty_bitmap(s->dirty_bitmap); 6962119882cSPaolo Bonzini 6977b508f6bSJohn Snow /* Make sure that the source BDS doesn't go away during bdrv_replace_node, 6987b508f6bSJohn Snow * before we can call bdrv_drained_end */ 6993f09bfbcSKevin Wolf bdrv_ref(src); 7004ef85a9cSKevin Wolf bdrv_ref(mirror_top_bs); 7017d9fcb39SKevin Wolf bdrv_ref(target_bs); 7027d9fcb39SKevin Wolf 7039275fc72SKevin Wolf bdrv_graph_rdunlock_main_loop(); 7049275fc72SKevin Wolf 705bb0c9409SVladimir Sementsov-Ogievskiy /* 706bb0c9409SVladimir Sementsov-Ogievskiy * Remove target parent that still uses BLK_PERM_WRITE/RESIZE before 7077d9fcb39SKevin Wolf * inserting target_bs at s->to_replace, where we might not be able to get 70863c8ef28SKevin Wolf * these permissions. 709bb0c9409SVladimir Sementsov-Ogievskiy */ 7107d9fcb39SKevin Wolf blk_unref(s->target); 7117d9fcb39SKevin Wolf s->target = NULL; 7124ef85a9cSKevin Wolf 7134ef85a9cSKevin Wolf /* We don't access the source any more. Dropping any WRITE/RESIZE is 714d2da5e28SKevin Wolf * required before it could become a backing file of target_bs. Not having 715d2da5e28SKevin Wolf * these permissions any more means that we can't allow any new requests on 716d2da5e28SKevin Wolf * mirror_top_bs from now on, so keep it drained. */ 717d2da5e28SKevin Wolf bdrv_drained_begin(mirror_top_bs); 718ccd6a379SKevin Wolf bdrv_drained_begin(target_bs); 719f94dc3b4SMax Reitz bs_opaque->stop = true; 7203804e3cfSKevin Wolf 7213804e3cfSKevin Wolf bdrv_graph_rdlock_main_loop(); 722f94dc3b4SMax Reitz bdrv_child_refresh_perms(mirror_top_bs, mirror_top_bs->backing, 7234ef85a9cSKevin Wolf &error_abort); 7243804e3cfSKevin Wolf 725737efc1eSJohn Snow if (!abort && s->backing_mode == MIRROR_SOURCE_BACKING_CHAIN) { 7264ef85a9cSKevin Wolf BlockDriverState *backing = s->is_none_mode ? src : s->base; 7273f072a7fSMax Reitz BlockDriverState *unfiltered_target = bdrv_skip_filters(target_bs); 7283f072a7fSMax Reitz 7293f072a7fSMax Reitz if (bdrv_cow_bs(unfiltered_target) != backing) { 7303f072a7fSMax Reitz bdrv_set_backing_hd(unfiltered_target, backing, &local_err); 73112fa4af6SKevin Wolf if (local_err) { 73212fa4af6SKevin Wolf error_report_err(local_err); 73366c8672dSVladimir Sementsov-Ogievskiy local_err = NULL; 7347b508f6bSJohn Snow ret = -EPERM; 73512fa4af6SKevin Wolf } 7364ef85a9cSKevin Wolf } 737c41f5b96SMax Reitz } else if (!abort && s->backing_mode == MIRROR_OPEN_BACKING_CHAIN) { 738c41f5b96SMax Reitz assert(!bdrv_backing_chain_next(target_bs)); 739c41f5b96SMax Reitz ret = bdrv_open_backing_file(bdrv_skip_filters(target_bs), NULL, 740c41f5b96SMax Reitz "backing", &local_err); 741c41f5b96SMax Reitz if (ret < 0) { 742c41f5b96SMax Reitz error_report_err(local_err); 743c41f5b96SMax Reitz local_err = NULL; 744c41f5b96SMax Reitz } 7454ef85a9cSKevin Wolf } 746ad74751fSKevin Wolf bdrv_graph_rdunlock_main_loop(); 7475a7e7a0bSStefan Hajnoczi 748737efc1eSJohn Snow if (s->should_complete && !abort) { 749737efc1eSJohn Snow BlockDriverState *to_replace = s->to_replace ?: src; 7501ba79388SAlberto Garcia bool ro = bdrv_is_read_only(to_replace); 75140365552SKevin Wolf 7521ba79388SAlberto Garcia if (ro != bdrv_is_read_only(target_bs)) { 7531ba79388SAlberto Garcia bdrv_reopen_set_read_only(target_bs, ro, NULL); 7545a7e7a0bSStefan Hajnoczi } 755b8804815SKevin Wolf 756b8804815SKevin Wolf /* The mirror job has no requests in flight any more, but we need to 757b8804815SKevin Wolf * drain potential other users of the BDS before changing the graph. */ 7585e771752SSergio Lopez assert(s->in_drain); 759ccd6a379SKevin Wolf bdrv_drained_begin(to_replace); 7606e9cc051SMax Reitz /* 7616e9cc051SMax Reitz * Cannot use check_to_replace_node() here, because that would 7626e9cc051SMax Reitz * check for an op blocker on @to_replace, and we have our own 7636e9cc051SMax Reitz * there. 7646e9cc051SMax Reitz */ 7656bc30f19SStefan Hajnoczi bdrv_graph_wrlock(); 7666e9cc051SMax Reitz if (bdrv_recurse_can_replace(src, to_replace)) { 7675fe31c25SKevin Wolf bdrv_replace_node(to_replace, target_bs, &local_err); 7686e9cc051SMax Reitz } else { 7696e9cc051SMax Reitz error_setg(&local_err, "Can no longer replace '%s' by '%s', " 7706e9cc051SMax Reitz "because it can no longer be guaranteed that doing so " 7716e9cc051SMax Reitz "would not lead to an abrupt change of visible data", 7726e9cc051SMax Reitz to_replace->node_name, target_bs->node_name); 7736e9cc051SMax Reitz } 7746bc30f19SStefan Hajnoczi bdrv_graph_wrunlock(); 775ccd6a379SKevin Wolf bdrv_drained_end(to_replace); 7765fe31c25SKevin Wolf if (local_err) { 7775fe31c25SKevin Wolf error_report_err(local_err); 7787b508f6bSJohn Snow ret = -EPERM; 7795fe31c25SKevin Wolf } 7805a7e7a0bSStefan Hajnoczi } 7815a7e7a0bSStefan Hajnoczi if (s->to_replace) { 7825a7e7a0bSStefan Hajnoczi bdrv_op_unblock_all(s->to_replace, s->replace_blocker); 7835a7e7a0bSStefan Hajnoczi error_free(s->replace_blocker); 7845a7e7a0bSStefan Hajnoczi bdrv_unref(s->to_replace); 7855a7e7a0bSStefan Hajnoczi } 7865a7e7a0bSStefan Hajnoczi g_free(s->replaces); 7874ef85a9cSKevin Wolf 788f94dc3b4SMax Reitz /* 789f94dc3b4SMax Reitz * Remove the mirror filter driver from the graph. Before this, get rid of 7904ef85a9cSKevin Wolf * the blockers on the intermediate nodes so that the resulting state is 791f94dc3b4SMax Reitz * valid. 792f94dc3b4SMax Reitz */ 7931908a559SKevin Wolf block_job_remove_all_bdrv(bjob); 7946bc30f19SStefan Hajnoczi bdrv_graph_wrlock(); 7953f072a7fSMax Reitz bdrv_replace_node(mirror_top_bs, mirror_top_bs->backing->bs, &error_abort); 7966bc30f19SStefan Hajnoczi bdrv_graph_wrunlock(); 797ccd6a379SKevin Wolf 7987d99ae59SAlexander Ivanov if (abort && s->base_ro && !bdrv_is_read_only(target_bs)) { 7997d99ae59SAlexander Ivanov bdrv_reopen_set_read_only(target_bs, true, NULL); 8007d99ae59SAlexander Ivanov } 8017d99ae59SAlexander Ivanov 802ccd6a379SKevin Wolf bdrv_drained_end(target_bs); 803ccd6a379SKevin Wolf bdrv_unref(target_bs); 8044ef85a9cSKevin Wolf 805429076e8SMax Reitz bs_opaque->job = NULL; 8064ef85a9cSKevin Wolf 807176c3699SFam Zheng bdrv_drained_end(src); 808d2da5e28SKevin Wolf bdrv_drained_end(mirror_top_bs); 8095e771752SSergio Lopez s->in_drain = false; 8104ef85a9cSKevin Wolf bdrv_unref(mirror_top_bs); 8113f09bfbcSKevin Wolf bdrv_unref(src); 8127b508f6bSJohn Snow 813737efc1eSJohn Snow return ret; 814737efc1eSJohn Snow } 815737efc1eSJohn Snow 816737efc1eSJohn Snow static int mirror_prepare(Job *job) 817737efc1eSJohn Snow { 818737efc1eSJohn Snow return mirror_exit_common(job); 819737efc1eSJohn Snow } 820737efc1eSJohn Snow 821737efc1eSJohn Snow static void mirror_abort(Job *job) 822737efc1eSJohn Snow { 823737efc1eSJohn Snow int ret = mirror_exit_common(job); 824737efc1eSJohn Snow assert(ret == 0); 8255a7e7a0bSStefan Hajnoczi } 8265a7e7a0bSStefan Hajnoczi 827537c3d4fSStefan Hajnoczi static void coroutine_fn mirror_throttle(MirrorBlockJob *s) 82849efb1f5SDenis V. Lunev { 82949efb1f5SDenis V. Lunev int64_t now = qemu_clock_get_ns(QEMU_CLOCK_REALTIME); 83049efb1f5SDenis V. Lunev 83118bb6928SKevin Wolf if (now - s->last_pause_ns > BLOCK_JOB_SLICE_TIME) { 83249efb1f5SDenis V. Lunev s->last_pause_ns = now; 8335d43e86eSKevin Wolf job_sleep_ns(&s->common.job, 0); 83449efb1f5SDenis V. Lunev } else { 835da01ff7fSKevin Wolf job_pause_point(&s->common.job); 83649efb1f5SDenis V. Lunev } 83749efb1f5SDenis V. Lunev } 83849efb1f5SDenis V. Lunev 839004915a9SKevin Wolf static int coroutine_fn GRAPH_UNLOCKED mirror_dirty_init(MirrorBlockJob *s) 840c0b363adSDenis V. Lunev { 84123ca459aSEric Blake int64_t offset; 842004915a9SKevin Wolf BlockDriverState *bs; 843c0b363adSDenis V. Lunev BlockDriverState *target_bs = blk_bs(s->target); 844*870f8963SEric Blake int ret = -EIO; 84551b0a488SEric Blake int64_t count; 846c0b363adSDenis V. Lunev 847004915a9SKevin Wolf bdrv_graph_co_rdlock(); 848004915a9SKevin Wolf bs = s->mirror_top_bs->backing->bs; 849004915a9SKevin Wolf bdrv_graph_co_rdunlock(); 850004915a9SKevin Wolf 851cdf3bc93SMax Reitz if (s->zero_target) { 852c7c2769cSDenis V. Lunev if (!bdrv_can_write_zeroes_with_unmap(target_bs)) { 853e0d7f73eSEric Blake bdrv_set_dirty_bitmap(s->dirty_bitmap, 0, s->bdev_length); 854b7d5062cSDenis V. Lunev return 0; 855b7d5062cSDenis V. Lunev } 856b7d5062cSDenis V. Lunev 85790ab48ebSAnton Nefedov s->initial_zeroing_ongoing = true; 85823ca459aSEric Blake for (offset = 0; offset < s->bdev_length; ) { 85923ca459aSEric Blake int bytes = MIN(s->bdev_length - offset, 86023ca459aSEric Blake QEMU_ALIGN_DOWN(INT_MAX, s->granularity)); 861c7c2769cSDenis V. Lunev 862c7c2769cSDenis V. Lunev mirror_throttle(s); 863c7c2769cSDenis V. Lunev 864daa7f2f9SKevin Wolf if (job_is_cancelled(&s->common.job)) { 86590ab48ebSAnton Nefedov s->initial_zeroing_ongoing = false; 866c7c2769cSDenis V. Lunev return 0; 867c7c2769cSDenis V. Lunev } 868c7c2769cSDenis V. Lunev 869c7c2769cSDenis V. Lunev if (s->in_flight >= MAX_IN_FLIGHT) { 87067adf4b3SEric Blake trace_mirror_yield(s, UINT64_MAX, s->buf_free_count, 87167adf4b3SEric Blake s->in_flight); 8729178f4feSKevin Wolf mirror_wait_for_free_in_flight_slot(s); 873c7c2769cSDenis V. Lunev continue; 874c7c2769cSDenis V. Lunev } 875c7c2769cSDenis V. Lunev 8764295c5fcSMax Reitz mirror_perform(s, offset, bytes, MIRROR_METHOD_ZERO); 87723ca459aSEric Blake offset += bytes; 878c7c2769cSDenis V. Lunev } 879c7c2769cSDenis V. Lunev 880bae8196dSPaolo Bonzini mirror_wait_for_all_io(s); 88190ab48ebSAnton Nefedov s->initial_zeroing_ongoing = false; 882c7c2769cSDenis V. Lunev } 883c7c2769cSDenis V. Lunev 884c0b363adSDenis V. Lunev /* First part, loop on the sectors and initialize the dirty bitmap. */ 88523ca459aSEric Blake for (offset = 0; offset < s->bdev_length; ) { 886c0b363adSDenis V. Lunev /* Just to make sure we are not exceeding int limit. */ 88723ca459aSEric Blake int bytes = MIN(s->bdev_length - offset, 88823ca459aSEric Blake QEMU_ALIGN_DOWN(INT_MAX, s->granularity)); 889c0b363adSDenis V. Lunev 890c0b363adSDenis V. Lunev mirror_throttle(s); 891c0b363adSDenis V. Lunev 892daa7f2f9SKevin Wolf if (job_is_cancelled(&s->common.job)) { 893c0b363adSDenis V. Lunev return 0; 894c0b363adSDenis V. Lunev } 895c0b363adSDenis V. Lunev 8967ff9579eSKevin Wolf WITH_GRAPH_RDLOCK_GUARD() { 897cc323997SPaolo Bonzini ret = bdrv_co_is_allocated_above(bs, s->base_overlay, true, offset, 8987ff9579eSKevin Wolf bytes, &count); 8997ff9579eSKevin Wolf } 900c0b363adSDenis V. Lunev if (ret < 0) { 901c0b363adSDenis V. Lunev return ret; 902c0b363adSDenis V. Lunev } 903c0b363adSDenis V. Lunev 90423ca459aSEric Blake assert(count); 905a92b1b06SEric Blake if (ret > 0) { 90623ca459aSEric Blake bdrv_set_dirty_bitmap(s->dirty_bitmap, offset, count); 907c0b363adSDenis V. Lunev } 90823ca459aSEric Blake offset += count; 909c0b363adSDenis V. Lunev } 910c0b363adSDenis V. Lunev return 0; 911c0b363adSDenis V. Lunev } 912c0b363adSDenis V. Lunev 913bdffb31dSPaolo Bonzini /* Called when going out of the streaming phase to flush the bulk of the 914bdffb31dSPaolo Bonzini * data to the medium, or just before completing. 915bdffb31dSPaolo Bonzini */ 91626bef102SPaolo Bonzini static int coroutine_fn mirror_flush(MirrorBlockJob *s) 917bdffb31dSPaolo Bonzini { 91826bef102SPaolo Bonzini int ret = blk_co_flush(s->target); 919bdffb31dSPaolo Bonzini if (ret < 0) { 920bdffb31dSPaolo Bonzini if (mirror_error_action(s, false, -ret) == BLOCK_ERROR_ACTION_REPORT) { 921bdffb31dSPaolo Bonzini s->ret = ret; 922bdffb31dSPaolo Bonzini } 923bdffb31dSPaolo Bonzini } 924bdffb31dSPaolo Bonzini return ret; 925bdffb31dSPaolo Bonzini } 926bdffb31dSPaolo Bonzini 927f67432a2SJohn Snow static int coroutine_fn mirror_run(Job *job, Error **errp) 928893f7ebaSPaolo Bonzini { 929f67432a2SJohn Snow MirrorBlockJob *s = container_of(job, MirrorBlockJob, common.job); 930004915a9SKevin Wolf BlockDriverState *bs; 93132125b14SKevin Wolf MirrorBDSOpaque *mirror_top_opaque = s->mirror_top_bs->opaque; 932e253f4b8SKevin Wolf BlockDriverState *target_bs = blk_bs(s->target); 9339a0cec66SPaolo Bonzini bool need_drain = true; 934ba11c88dSMarc-André Lureau BlockDeviceIoStatus iostatus = BLOCK_DEVICE_IO_STATUS__MAX; 935c0b363adSDenis V. Lunev int64_t length; 936e83dd680SKevin Wolf int64_t target_length; 937b812f671SPaolo Bonzini BlockDriverInfo bdi; 9381d33936eSJeff Cody char backing_filename[2]; /* we only need 2 characters because we are only 9391d33936eSJeff Cody checking for a NULL string */ 940893f7ebaSPaolo Bonzini int ret = 0; 941893f7ebaSPaolo Bonzini 942004915a9SKevin Wolf bdrv_graph_co_rdlock(); 943004915a9SKevin Wolf bs = bdrv_filter_bs(s->mirror_top_bs); 944004915a9SKevin Wolf bdrv_graph_co_rdunlock(); 945004915a9SKevin Wolf 946daa7f2f9SKevin Wolf if (job_is_cancelled(&s->common.job)) { 947893f7ebaSPaolo Bonzini goto immediate_exit; 948893f7ebaSPaolo Bonzini } 949893f7ebaSPaolo Bonzini 9508ab8140aSKevin Wolf bdrv_graph_co_rdlock(); 951c86422c5SEmanuele Giuseppe Esposito s->bdev_length = bdrv_co_getlength(bs); 9528ab8140aSKevin Wolf bdrv_graph_co_rdunlock(); 9538ab8140aSKevin Wolf 954b21c7652SMax Reitz if (s->bdev_length < 0) { 955b21c7652SMax Reitz ret = s->bdev_length; 956373df5b1SFam Zheng goto immediate_exit; 957becc347eSKevin Wolf } 958becc347eSKevin Wolf 959c86422c5SEmanuele Giuseppe Esposito target_length = blk_co_getlength(s->target); 960e83dd680SKevin Wolf if (target_length < 0) { 961e83dd680SKevin Wolf ret = target_length; 962becc347eSKevin Wolf goto immediate_exit; 963becc347eSKevin Wolf } 964becc347eSKevin Wolf 965e83dd680SKevin Wolf /* Active commit must resize the base image if its size differs from the 966e83dd680SKevin Wolf * active layer. */ 967e83dd680SKevin Wolf if (s->base == blk_bs(s->target)) { 968e83dd680SKevin Wolf if (s->bdev_length > target_length) { 96988276216SAlberto Faria ret = blk_co_truncate(s->target, s->bdev_length, false, 9708c6242b6SKevin Wolf PREALLOC_MODE_OFF, 0, NULL); 971becc347eSKevin Wolf if (ret < 0) { 972becc347eSKevin Wolf goto immediate_exit; 973becc347eSKevin Wolf } 974becc347eSKevin Wolf } 975e83dd680SKevin Wolf } else if (s->bdev_length != target_length) { 976e83dd680SKevin Wolf error_setg(errp, "Source and target image have different sizes"); 977e83dd680SKevin Wolf ret = -EINVAL; 978e83dd680SKevin Wolf goto immediate_exit; 979becc347eSKevin Wolf } 980becc347eSKevin Wolf 981becc347eSKevin Wolf if (s->bdev_length == 0) { 9822e1795b5SKevin Wolf /* Transition to the READY state and wait for complete. */ 9832e1795b5SKevin Wolf job_transition_to_ready(&s->common.job); 98476cb2f24SFiona Ebner qatomic_set(&s->actively_synced, true); 98508b83bffSHanna Reitz while (!job_cancel_requested(&s->common.job) && !s->should_complete) { 986198c49ccSKevin Wolf job_yield(&s->common.job); 9879e48b025SFam Zheng } 9889e48b025SFam Zheng goto immediate_exit; 989893f7ebaSPaolo Bonzini } 990893f7ebaSPaolo Bonzini 991b21c7652SMax Reitz length = DIV_ROUND_UP(s->bdev_length, s->granularity); 992402a4741SPaolo Bonzini s->in_flight_bitmap = bitmap_new(length); 993402a4741SPaolo Bonzini 994b812f671SPaolo Bonzini /* If we have no backing file yet in the destination, we cannot let 995b812f671SPaolo Bonzini * the destination do COW. Instead, we copy sectors around the 996b812f671SPaolo Bonzini * dirty data if needed. We need a bitmap to do that. 997b812f671SPaolo Bonzini */ 998e253f4b8SKevin Wolf bdrv_get_backing_filename(target_bs, backing_filename, 999b812f671SPaolo Bonzini sizeof(backing_filename)); 1000a00e70c0SEmanuele Giuseppe Esposito bdrv_graph_co_rdlock(); 10013d47eb0aSEmanuele Giuseppe Esposito if (!bdrv_co_get_info(target_bs, &bdi) && bdi.cluster_size) { 1002b436982fSEric Blake s->target_cluster_size = bdi.cluster_size; 1003b436982fSEric Blake } else { 1004b436982fSEric Blake s->target_cluster_size = BDRV_SECTOR_SIZE; 1005c3cc95bdSFam Zheng } 10063f072a7fSMax Reitz if (backing_filename[0] && !bdrv_backing_chain_next(target_bs) && 1007b436982fSEric Blake s->granularity < s->target_cluster_size) { 1008b436982fSEric Blake s->buf_size = MAX(s->buf_size, s->target_cluster_size); 1009b812f671SPaolo Bonzini s->cow_bitmap = bitmap_new(length); 1010b812f671SPaolo Bonzini } 1011e253f4b8SKevin Wolf s->max_iov = MIN(bs->bl.max_iov, target_bs->bl.max_iov); 1012ad74751fSKevin Wolf bdrv_graph_co_rdunlock(); 1013b812f671SPaolo Bonzini 10147504edf4SKevin Wolf s->buf = qemu_try_blockalign(bs, s->buf_size); 10157504edf4SKevin Wolf if (s->buf == NULL) { 10167504edf4SKevin Wolf ret = -ENOMEM; 10177504edf4SKevin Wolf goto immediate_exit; 10187504edf4SKevin Wolf } 10197504edf4SKevin Wolf 1020402a4741SPaolo Bonzini mirror_free_init(s); 1021893f7ebaSPaolo Bonzini 102249efb1f5SDenis V. Lunev s->last_pause_ns = qemu_clock_get_ns(QEMU_CLOCK_REALTIME); 102303544a6eSFam Zheng if (!s->is_none_mode) { 1024c0b363adSDenis V. Lunev ret = mirror_dirty_init(s); 1025daa7f2f9SKevin Wolf if (ret < 0 || job_is_cancelled(&s->common.job)) { 10264c0cbd6fSFam Zheng goto immediate_exit; 10274c0cbd6fSFam Zheng } 1028893f7ebaSPaolo Bonzini } 1029893f7ebaSPaolo Bonzini 103032125b14SKevin Wolf /* 103132125b14SKevin Wolf * Only now the job is fully initialised and mirror_top_bs should start 103232125b14SKevin Wolf * accessing it. 103332125b14SKevin Wolf */ 103432125b14SKevin Wolf mirror_top_opaque->job = s; 103532125b14SKevin Wolf 1036dc162c8eSFam Zheng assert(!s->dbi); 1037715a74d8SEric Blake s->dbi = bdrv_dirty_iter_new(s->dirty_bitmap); 1038893f7ebaSPaolo Bonzini for (;;) { 103949efb1f5SDenis V. Lunev int64_t cnt, delta; 1040893f7ebaSPaolo Bonzini bool should_complete; 1041893f7ebaSPaolo Bonzini 1042bd48bde8SPaolo Bonzini if (s->ret < 0) { 1043bd48bde8SPaolo Bonzini ret = s->ret; 1044893f7ebaSPaolo Bonzini goto immediate_exit; 1045893f7ebaSPaolo Bonzini } 1046bd48bde8SPaolo Bonzini 1047da01ff7fSKevin Wolf job_pause_point(&s->common.job); 1048565ac01fSStefan Hajnoczi 10494feeec7eSHanna Reitz if (job_is_cancelled(&s->common.job)) { 10504feeec7eSHanna Reitz ret = 0; 10514feeec7eSHanna Reitz goto immediate_exit; 10524feeec7eSHanna Reitz } 10534feeec7eSHanna Reitz 105420dca810SJohn Snow cnt = bdrv_get_dirty_count(s->dirty_bitmap); 105505df8a6aSKevin Wolf /* cnt is the number of dirty bytes remaining and s->bytes_in_flight is 105605df8a6aSKevin Wolf * the number of bytes currently being processed; together those are 105705df8a6aSKevin Wolf * the current remaining operation length */ 1058d69a879bSHanna Reitz job_progress_set_remaining(&s->common.job, 1059d69a879bSHanna Reitz s->bytes_in_flight + cnt + 1060d69a879bSHanna Reitz s->active_write_bytes_in_flight); 1061bd48bde8SPaolo Bonzini 1062bd48bde8SPaolo Bonzini /* Note that even when no rate limit is applied we need to yield 1063a7282330SFam Zheng * periodically with no pending I/O so that bdrv_drain_all() returns. 106418bb6928SKevin Wolf * We do so every BLKOCK_JOB_SLICE_TIME nanoseconds, or when there is 106518bb6928SKevin Wolf * an error, or when the source is clean, whichever comes first. */ 106649efb1f5SDenis V. Lunev delta = qemu_clock_get_ns(QEMU_CLOCK_REALTIME) - s->last_pause_ns; 1067d59cb66dSEmanuele Giuseppe Esposito WITH_JOB_LOCK_GUARD() { 1068d59cb66dSEmanuele Giuseppe Esposito iostatus = s->common.iostatus; 1069d59cb66dSEmanuele Giuseppe Esposito } 107018bb6928SKevin Wolf if (delta < BLOCK_JOB_SLICE_TIME && 1071d59cb66dSEmanuele Giuseppe Esposito iostatus == BLOCK_DEVICE_IO_STATUS_OK) { 1072cf56a3c6SDenis V. Lunev if (s->in_flight >= MAX_IN_FLIGHT || s->buf_free_count == 0 || 1073402a4741SPaolo Bonzini (cnt == 0 && s->in_flight > 0)) { 10749a46dba7SEric Blake trace_mirror_yield(s, cnt, s->buf_free_count, s->in_flight); 10759178f4feSKevin Wolf mirror_wait_for_free_in_flight_slot(s); 1076bd48bde8SPaolo Bonzini continue; 1077bd48bde8SPaolo Bonzini } else if (cnt != 0) { 1078018e5987SKevin Wolf mirror_iteration(s); 1079893f7ebaSPaolo Bonzini } 1080cc8c9d6cSPaolo Bonzini } 1081893f7ebaSPaolo Bonzini 1082893f7ebaSPaolo Bonzini should_complete = false; 1083bd48bde8SPaolo Bonzini if (s->in_flight == 0 && cnt == 0) { 1084893f7ebaSPaolo Bonzini trace_mirror_before_flush(s); 108544716224SHanna Reitz if (!job_is_ready(&s->common.job)) { 1086bdffb31dSPaolo Bonzini if (mirror_flush(s) < 0) { 1087bdffb31dSPaolo Bonzini /* Go check s->ret. */ 1088bdffb31dSPaolo Bonzini continue; 1089893f7ebaSPaolo Bonzini } 1090893f7ebaSPaolo Bonzini /* We're out of the streaming phase. From now on, if the job 1091893f7ebaSPaolo Bonzini * is cancelled we will actually complete all pending I/O and 1092893f7ebaSPaolo Bonzini * report completion. This way, block-job-cancel will leave 1093893f7ebaSPaolo Bonzini * the target in a consistent state. 1094893f7ebaSPaolo Bonzini */ 10952e1795b5SKevin Wolf job_transition_to_ready(&s->common.job); 1096c45d0e1aSFiona Ebner } 10972d400d15SFiona Ebner if (qatomic_read(&s->copy_mode) != MIRROR_COPY_MODE_BACKGROUND) { 109876cb2f24SFiona Ebner qatomic_set(&s->actively_synced, true); 1099d06107adSMax Reitz } 1100d63ffd87SPaolo Bonzini 1101d63ffd87SPaolo Bonzini should_complete = s->should_complete || 110208b83bffSHanna Reitz job_cancel_requested(&s->common.job); 110320dca810SJohn Snow cnt = bdrv_get_dirty_count(s->dirty_bitmap); 1104893f7ebaSPaolo Bonzini } 1105893f7ebaSPaolo Bonzini 1106893f7ebaSPaolo Bonzini if (cnt == 0 && should_complete) { 1107893f7ebaSPaolo Bonzini /* The dirty bitmap is not updated while operations are pending. 1108893f7ebaSPaolo Bonzini * If we're about to exit, wait for pending operations before 1109893f7ebaSPaolo Bonzini * calling bdrv_get_dirty_count(bs), or we may exit while the 1110893f7ebaSPaolo Bonzini * source has dirty data to copy! 1111893f7ebaSPaolo Bonzini * 1112893f7ebaSPaolo Bonzini * Note that I/O can be submitted by the guest while 11139a0cec66SPaolo Bonzini * mirror_populate runs, so pause it now. Before deciding 11149a0cec66SPaolo Bonzini * whether to switch to target check one last time if I/O has 11159a0cec66SPaolo Bonzini * come in the meanwhile, and if not flush the data to disk. 1116893f7ebaSPaolo Bonzini */ 11179a46dba7SEric Blake trace_mirror_before_drain(s, cnt); 11189a0cec66SPaolo Bonzini 11195e771752SSergio Lopez s->in_drain = true; 11209a0cec66SPaolo Bonzini bdrv_drained_begin(bs); 1121d69a879bSHanna Reitz 1122d69a879bSHanna Reitz /* Must be zero because we are drained */ 1123d69a879bSHanna Reitz assert(s->in_active_write_counter == 0); 1124d69a879bSHanna Reitz 112520dca810SJohn Snow cnt = bdrv_get_dirty_count(s->dirty_bitmap); 1126bdffb31dSPaolo Bonzini if (cnt > 0 || mirror_flush(s) < 0) { 11279a0cec66SPaolo Bonzini bdrv_drained_end(bs); 11285e771752SSergio Lopez s->in_drain = false; 11299a0cec66SPaolo Bonzini continue; 11309a0cec66SPaolo Bonzini } 11319a0cec66SPaolo Bonzini 11329a0cec66SPaolo Bonzini /* The two disks are in sync. Exit and report successful 11339a0cec66SPaolo Bonzini * completion. 11349a0cec66SPaolo Bonzini */ 11359a0cec66SPaolo Bonzini assert(QLIST_EMPTY(&bs->tracked_requests)); 11369a0cec66SPaolo Bonzini need_drain = false; 11379a0cec66SPaolo Bonzini break; 1138893f7ebaSPaolo Bonzini } 1139893f7ebaSPaolo Bonzini 114044716224SHanna Reitz if (job_is_ready(&s->common.job) && !should_complete) { 1141018e5987SKevin Wolf if (s->in_flight == 0 && cnt == 0) { 114244716224SHanna Reitz trace_mirror_before_sleep(s, cnt, job_is_ready(&s->common.job), 1143018e5987SKevin Wolf BLOCK_JOB_SLICE_TIME); 1144018e5987SKevin Wolf job_sleep_ns(&s->common.job, BLOCK_JOB_SLICE_TIME); 1145018e5987SKevin Wolf } 1146018e5987SKevin Wolf } else { 1147018e5987SKevin Wolf block_job_ratelimit_sleep(&s->common); 1148018e5987SKevin Wolf } 114949efb1f5SDenis V. Lunev s->last_pause_ns = qemu_clock_get_ns(QEMU_CLOCK_REALTIME); 1150893f7ebaSPaolo Bonzini } 1151893f7ebaSPaolo Bonzini 1152893f7ebaSPaolo Bonzini immediate_exit: 1153bd48bde8SPaolo Bonzini if (s->in_flight > 0) { 1154bd48bde8SPaolo Bonzini /* We get here only if something went wrong. Either the job failed, 1155bd48bde8SPaolo Bonzini * or it was cancelled prematurely so that we do not guarantee that 1156bd48bde8SPaolo Bonzini * the target is a copy of the source. 1157bd48bde8SPaolo Bonzini */ 115808b83bffSHanna Reitz assert(ret < 0 || job_is_cancelled(&s->common.job)); 11599a0cec66SPaolo Bonzini assert(need_drain); 1160bae8196dSPaolo Bonzini mirror_wait_for_all_io(s); 1161bd48bde8SPaolo Bonzini } 1162bd48bde8SPaolo Bonzini 1163bd48bde8SPaolo Bonzini assert(s->in_flight == 0); 11647191bf31SMarkus Armbruster qemu_vfree(s->buf); 1165b812f671SPaolo Bonzini g_free(s->cow_bitmap); 1166402a4741SPaolo Bonzini g_free(s->in_flight_bitmap); 1167dc162c8eSFam Zheng bdrv_dirty_iter_free(s->dbi); 11685a7e7a0bSStefan Hajnoczi 11699a0cec66SPaolo Bonzini if (need_drain) { 11705e771752SSergio Lopez s->in_drain = true; 1171e253f4b8SKevin Wolf bdrv_drained_begin(bs); 11729a0cec66SPaolo Bonzini } 1173f67432a2SJohn Snow 1174f67432a2SJohn Snow return ret; 1175893f7ebaSPaolo Bonzini } 1176893f7ebaSPaolo Bonzini 11773453d972SKevin Wolf static void mirror_complete(Job *job, Error **errp) 1178d63ffd87SPaolo Bonzini { 11793453d972SKevin Wolf MirrorBlockJob *s = container_of(job, MirrorBlockJob, common.job); 1180274fcceeSMax Reitz 118144716224SHanna Reitz if (!job_is_ready(job)) { 11829df229c3SAlberto Garcia error_setg(errp, "The active block job '%s' cannot be completed", 11833453d972SKevin Wolf job->id); 1184d63ffd87SPaolo Bonzini return; 1185d63ffd87SPaolo Bonzini } 1186d63ffd87SPaolo Bonzini 118715d67298SChanglong Xie /* block all operations on to_replace bs */ 118809158f00SBenoît Canet if (s->replaces) { 1189e12f3784SWen Congyang s->to_replace = bdrv_find_node(s->replaces); 119009158f00SBenoît Canet if (!s->to_replace) { 1191e12f3784SWen Congyang error_setg(errp, "Node name '%s' not found", s->replaces); 119209158f00SBenoît Canet return; 119309158f00SBenoît Canet } 119409158f00SBenoît Canet 119564631f36SVladimir Sementsov-Ogievskiy /* TODO Translate this into child freeze system. */ 119609158f00SBenoît Canet error_setg(&s->replace_blocker, 119709158f00SBenoît Canet "block device is in use by block-job-complete"); 119809158f00SBenoît Canet bdrv_op_block_all(s->to_replace, s->replace_blocker); 119909158f00SBenoît Canet bdrv_ref(s->to_replace); 120009158f00SBenoît Canet } 120109158f00SBenoît Canet 1202d63ffd87SPaolo Bonzini s->should_complete = true; 120300769414SMax Reitz 120400769414SMax Reitz /* If the job is paused, it will be re-entered when it is resumed */ 1205279ac06eSEmanuele Giuseppe Esposito WITH_JOB_LOCK_GUARD() { 120600769414SMax Reitz if (!job->paused) { 1207279ac06eSEmanuele Giuseppe Esposito job_enter_cond_locked(job, NULL); 1208279ac06eSEmanuele Giuseppe Esposito } 1209d63ffd87SPaolo Bonzini } 121000769414SMax Reitz } 1211d63ffd87SPaolo Bonzini 1212537c3d4fSStefan Hajnoczi static void coroutine_fn mirror_pause(Job *job) 1213565ac01fSStefan Hajnoczi { 1214da01ff7fSKevin Wolf MirrorBlockJob *s = container_of(job, MirrorBlockJob, common.job); 1215565ac01fSStefan Hajnoczi 1216bae8196dSPaolo Bonzini mirror_wait_for_all_io(s); 1217565ac01fSStefan Hajnoczi } 1218565ac01fSStefan Hajnoczi 121989bd0305SKevin Wolf static bool mirror_drained_poll(BlockJob *job) 122089bd0305SKevin Wolf { 122189bd0305SKevin Wolf MirrorBlockJob *s = container_of(job, MirrorBlockJob, common); 12225e771752SSergio Lopez 12235e771752SSergio Lopez /* If the job isn't paused nor cancelled, we can't be sure that it won't 12245e771752SSergio Lopez * issue more requests. We make an exception if we've reached this point 12255e771752SSergio Lopez * from one of our own drain sections, to avoid a deadlock waiting for 12265e771752SSergio Lopez * ourselves. 12275e771752SSergio Lopez */ 1228279ac06eSEmanuele Giuseppe Esposito WITH_JOB_LOCK_GUARD() { 1229279ac06eSEmanuele Giuseppe Esposito if (!s->common.job.paused && !job_is_cancelled_locked(&job->job) 1230279ac06eSEmanuele Giuseppe Esposito && !s->in_drain) { 12315e771752SSergio Lopez return true; 12325e771752SSergio Lopez } 1233279ac06eSEmanuele Giuseppe Esposito } 12345e771752SSergio Lopez 123589bd0305SKevin Wolf return !!s->in_flight; 123689bd0305SKevin Wolf } 123789bd0305SKevin Wolf 123873895f38SHanna Reitz static bool mirror_cancel(Job *job, bool force) 1239521ff8b7SVladimir Sementsov-Ogievskiy { 1240521ff8b7SVladimir Sementsov-Ogievskiy MirrorBlockJob *s = container_of(job, MirrorBlockJob, common.job); 1241521ff8b7SVladimir Sementsov-Ogievskiy BlockDriverState *target = blk_bs(s->target); 1242521ff8b7SVladimir Sementsov-Ogievskiy 124373895f38SHanna Reitz /* 124473895f38SHanna Reitz * Before the job is READY, we treat any cancellation like a 124573895f38SHanna Reitz * force-cancellation. 124673895f38SHanna Reitz */ 124773895f38SHanna Reitz force = force || !job_is_ready(job); 124873895f38SHanna Reitz 124973895f38SHanna Reitz if (force) { 1250521ff8b7SVladimir Sementsov-Ogievskiy bdrv_cancel_in_flight(target); 1251521ff8b7SVladimir Sementsov-Ogievskiy } 125273895f38SHanna Reitz return force; 125373895f38SHanna Reitz } 125473895f38SHanna Reitz 125573895f38SHanna Reitz static bool commit_active_cancel(Job *job, bool force) 125673895f38SHanna Reitz { 125773895f38SHanna Reitz /* Same as above in mirror_cancel() */ 125873895f38SHanna Reitz return force || !job_is_ready(job); 12599c785cd7SVladimir Sementsov-Ogievskiy } 1260521ff8b7SVladimir Sementsov-Ogievskiy 12612d400d15SFiona Ebner static void mirror_change(BlockJob *job, BlockJobChangeOptions *opts, 12622d400d15SFiona Ebner Error **errp) 12632d400d15SFiona Ebner { 12642d400d15SFiona Ebner MirrorBlockJob *s = container_of(job, MirrorBlockJob, common); 12652d400d15SFiona Ebner BlockJobChangeOptionsMirror *change_opts = &opts->u.mirror; 12662d400d15SFiona Ebner MirrorCopyMode current; 12672d400d15SFiona Ebner 12682d400d15SFiona Ebner /* 12692d400d15SFiona Ebner * The implementation relies on the fact that copy_mode is only written 12702d400d15SFiona Ebner * under the BQL. Otherwise, further synchronization would be required. 12712d400d15SFiona Ebner */ 12722d400d15SFiona Ebner 12732d400d15SFiona Ebner GLOBAL_STATE_CODE(); 12742d400d15SFiona Ebner 12752d400d15SFiona Ebner if (qatomic_read(&s->copy_mode) == change_opts->copy_mode) { 12762d400d15SFiona Ebner return; 12772d400d15SFiona Ebner } 12782d400d15SFiona Ebner 12792d400d15SFiona Ebner if (change_opts->copy_mode != MIRROR_COPY_MODE_WRITE_BLOCKING) { 12802d400d15SFiona Ebner error_setg(errp, "Change to copy mode '%s' is not implemented", 12812d400d15SFiona Ebner MirrorCopyMode_str(change_opts->copy_mode)); 12822d400d15SFiona Ebner return; 12832d400d15SFiona Ebner } 12842d400d15SFiona Ebner 12852d400d15SFiona Ebner current = qatomic_cmpxchg(&s->copy_mode, MIRROR_COPY_MODE_BACKGROUND, 12862d400d15SFiona Ebner change_opts->copy_mode); 12872d400d15SFiona Ebner if (current != MIRROR_COPY_MODE_BACKGROUND) { 12882d400d15SFiona Ebner error_setg(errp, "Expected current copy mode '%s', got '%s'", 12892d400d15SFiona Ebner MirrorCopyMode_str(MIRROR_COPY_MODE_BACKGROUND), 12902d400d15SFiona Ebner MirrorCopyMode_str(current)); 12912d400d15SFiona Ebner } 12922d400d15SFiona Ebner } 12932d400d15SFiona Ebner 129476cb2f24SFiona Ebner static void mirror_query(BlockJob *job, BlockJobInfo *info) 129576cb2f24SFiona Ebner { 129676cb2f24SFiona Ebner MirrorBlockJob *s = container_of(job, MirrorBlockJob, common); 129776cb2f24SFiona Ebner 129876cb2f24SFiona Ebner info->u.mirror = (BlockJobInfoMirror) { 129976cb2f24SFiona Ebner .actively_synced = qatomic_read(&s->actively_synced), 130076cb2f24SFiona Ebner }; 130176cb2f24SFiona Ebner } 130276cb2f24SFiona Ebner 13033fc4b10aSFam Zheng static const BlockJobDriver mirror_job_driver = { 130433e9e9bdSKevin Wolf .job_driver = { 1305893f7ebaSPaolo Bonzini .instance_size = sizeof(MirrorBlockJob), 13068e4c8700SKevin Wolf .job_type = JOB_TYPE_MIRROR, 130780fa2c75SKevin Wolf .free = block_job_free, 1308b15de828SKevin Wolf .user_resume = block_job_user_resume, 1309f67432a2SJohn Snow .run = mirror_run, 1310737efc1eSJohn Snow .prepare = mirror_prepare, 1311737efc1eSJohn Snow .abort = mirror_abort, 1312565ac01fSStefan Hajnoczi .pause = mirror_pause, 1313da01ff7fSKevin Wolf .complete = mirror_complete, 1314521ff8b7SVladimir Sementsov-Ogievskiy .cancel = mirror_cancel, 13153453d972SKevin Wolf }, 131689bd0305SKevin Wolf .drained_poll = mirror_drained_poll, 13172d400d15SFiona Ebner .change = mirror_change, 131876cb2f24SFiona Ebner .query = mirror_query, 1319893f7ebaSPaolo Bonzini }; 1320893f7ebaSPaolo Bonzini 132103544a6eSFam Zheng static const BlockJobDriver commit_active_job_driver = { 132233e9e9bdSKevin Wolf .job_driver = { 132303544a6eSFam Zheng .instance_size = sizeof(MirrorBlockJob), 13248e4c8700SKevin Wolf .job_type = JOB_TYPE_COMMIT, 132580fa2c75SKevin Wolf .free = block_job_free, 1326b15de828SKevin Wolf .user_resume = block_job_user_resume, 1327f67432a2SJohn Snow .run = mirror_run, 1328737efc1eSJohn Snow .prepare = mirror_prepare, 1329737efc1eSJohn Snow .abort = mirror_abort, 1330565ac01fSStefan Hajnoczi .pause = mirror_pause, 1331da01ff7fSKevin Wolf .complete = mirror_complete, 133273895f38SHanna Reitz .cancel = commit_active_cancel, 13333453d972SKevin Wolf }, 133489bd0305SKevin Wolf .drained_poll = mirror_drained_poll, 133503544a6eSFam Zheng }; 133603544a6eSFam Zheng 1337537c3d4fSStefan Hajnoczi static void coroutine_fn 1338537c3d4fSStefan Hajnoczi do_sync_target_write(MirrorBlockJob *job, MirrorMethod method, 1339d06107adSMax Reitz uint64_t offset, uint64_t bytes, 1340d06107adSMax Reitz QEMUIOVector *qiov, int flags) 1341d06107adSMax Reitz { 1342d06107adSMax Reitz int ret; 1343dbdf699cSVladimir Sementsov-Ogievskiy size_t qiov_offset = 0; 1344*870f8963SEric Blake int64_t dirty_bitmap_offset, dirty_bitmap_end; 1345d06107adSMax Reitz 1346dbdf699cSVladimir Sementsov-Ogievskiy if (!QEMU_IS_ALIGNED(offset, job->granularity) && 1347dbdf699cSVladimir Sementsov-Ogievskiy bdrv_dirty_bitmap_get(job->dirty_bitmap, offset)) 1348dbdf699cSVladimir Sementsov-Ogievskiy { 1349dbdf699cSVladimir Sementsov-Ogievskiy /* 1350dbdf699cSVladimir Sementsov-Ogievskiy * Dirty unaligned padding: ignore it. 1351dbdf699cSVladimir Sementsov-Ogievskiy * 1352dbdf699cSVladimir Sementsov-Ogievskiy * Reasoning: 1353dbdf699cSVladimir Sementsov-Ogievskiy * 1. If we copy it, we can't reset corresponding bit in 1354dbdf699cSVladimir Sementsov-Ogievskiy * dirty_bitmap as there may be some "dirty" bytes still not 1355dbdf699cSVladimir Sementsov-Ogievskiy * copied. 1356dbdf699cSVladimir Sementsov-Ogievskiy * 2. It's already dirty, so skipping it we don't diverge mirror 1357dbdf699cSVladimir Sementsov-Ogievskiy * progress. 1358dbdf699cSVladimir Sementsov-Ogievskiy * 1359dbdf699cSVladimir Sementsov-Ogievskiy * Note, that because of this, guest write may have no contribution 1360dbdf699cSVladimir Sementsov-Ogievskiy * into mirror converge, but that's not bad, as we have background 1361dbdf699cSVladimir Sementsov-Ogievskiy * process of mirroring. If under some bad circumstances (high guest 1362dbdf699cSVladimir Sementsov-Ogievskiy * IO load) background process starve, we will not converge anyway, 1363dbdf699cSVladimir Sementsov-Ogievskiy * even if each write will contribute, as guest is not guaranteed to 1364dbdf699cSVladimir Sementsov-Ogievskiy * rewrite the whole disk. 1365dbdf699cSVladimir Sementsov-Ogievskiy */ 1366dbdf699cSVladimir Sementsov-Ogievskiy qiov_offset = QEMU_ALIGN_UP(offset, job->granularity) - offset; 1367dbdf699cSVladimir Sementsov-Ogievskiy if (bytes <= qiov_offset) { 1368dbdf699cSVladimir Sementsov-Ogievskiy /* nothing to do after shrink */ 1369dbdf699cSVladimir Sementsov-Ogievskiy return; 1370dbdf699cSVladimir Sementsov-Ogievskiy } 1371dbdf699cSVladimir Sementsov-Ogievskiy offset += qiov_offset; 1372dbdf699cSVladimir Sementsov-Ogievskiy bytes -= qiov_offset; 1373dbdf699cSVladimir Sementsov-Ogievskiy } 1374dbdf699cSVladimir Sementsov-Ogievskiy 1375dbdf699cSVladimir Sementsov-Ogievskiy if (!QEMU_IS_ALIGNED(offset + bytes, job->granularity) && 1376dbdf699cSVladimir Sementsov-Ogievskiy bdrv_dirty_bitmap_get(job->dirty_bitmap, offset + bytes - 1)) 1377dbdf699cSVladimir Sementsov-Ogievskiy { 1378dbdf699cSVladimir Sementsov-Ogievskiy uint64_t tail = (offset + bytes) % job->granularity; 1379dbdf699cSVladimir Sementsov-Ogievskiy 1380dbdf699cSVladimir Sementsov-Ogievskiy if (bytes <= tail) { 1381dbdf699cSVladimir Sementsov-Ogievskiy /* nothing to do after shrink */ 1382dbdf699cSVladimir Sementsov-Ogievskiy return; 1383dbdf699cSVladimir Sementsov-Ogievskiy } 1384dbdf699cSVladimir Sementsov-Ogievskiy bytes -= tail; 1385dbdf699cSVladimir Sementsov-Ogievskiy } 1386dbdf699cSVladimir Sementsov-Ogievskiy 1387dbdf699cSVladimir Sementsov-Ogievskiy /* 1388dbdf699cSVladimir Sementsov-Ogievskiy * Tails are either clean or shrunk, so for bitmap resetting 1389dbdf699cSVladimir Sementsov-Ogievskiy * we safely align the range down. 1390dbdf699cSVladimir Sementsov-Ogievskiy */ 1391*870f8963SEric Blake dirty_bitmap_offset = QEMU_ALIGN_UP(offset, job->granularity); 1392*870f8963SEric Blake dirty_bitmap_end = QEMU_ALIGN_DOWN(offset + bytes, job->granularity); 1393*870f8963SEric Blake if (dirty_bitmap_offset < dirty_bitmap_end) { 1394*870f8963SEric Blake bdrv_reset_dirty_bitmap(job->dirty_bitmap, dirty_bitmap_offset, 1395*870f8963SEric Blake dirty_bitmap_end - dirty_bitmap_offset); 1396dbdf699cSVladimir Sementsov-Ogievskiy } 1397d06107adSMax Reitz 13985c511ac3SVladimir Sementsov-Ogievskiy job_progress_increase_remaining(&job->common.job, bytes); 1399d69a879bSHanna Reitz job->active_write_bytes_in_flight += bytes; 1400d06107adSMax Reitz 1401d06107adSMax Reitz switch (method) { 1402d06107adSMax Reitz case MIRROR_METHOD_COPY: 1403dbdf699cSVladimir Sementsov-Ogievskiy ret = blk_co_pwritev_part(job->target, offset, bytes, 1404dbdf699cSVladimir Sementsov-Ogievskiy qiov, qiov_offset, flags); 1405d06107adSMax Reitz break; 1406d06107adSMax Reitz 1407d06107adSMax Reitz case MIRROR_METHOD_ZERO: 1408d06107adSMax Reitz assert(!qiov); 14095c511ac3SVladimir Sementsov-Ogievskiy ret = blk_co_pwrite_zeroes(job->target, offset, bytes, flags); 1410d06107adSMax Reitz break; 1411d06107adSMax Reitz 1412d06107adSMax Reitz case MIRROR_METHOD_DISCARD: 1413d06107adSMax Reitz assert(!qiov); 14145c511ac3SVladimir Sementsov-Ogievskiy ret = blk_co_pdiscard(job->target, offset, bytes); 1415d06107adSMax Reitz break; 1416d06107adSMax Reitz 1417d06107adSMax Reitz default: 1418d06107adSMax Reitz abort(); 1419d06107adSMax Reitz } 1420d06107adSMax Reitz 1421d69a879bSHanna Reitz job->active_write_bytes_in_flight -= bytes; 1422d06107adSMax Reitz if (ret >= 0) { 14235c511ac3SVladimir Sementsov-Ogievskiy job_progress_update(&job->common.job, bytes); 1424d06107adSMax Reitz } else { 1425d06107adSMax Reitz BlockErrorAction action; 1426d06107adSMax Reitz 1427dbdf699cSVladimir Sementsov-Ogievskiy /* 1428dbdf699cSVladimir Sementsov-Ogievskiy * We failed, so we should mark dirty the whole area, aligned up. 1429dbdf699cSVladimir Sementsov-Ogievskiy * Note that we don't care about shrunk tails if any: they were dirty 1430dbdf699cSVladimir Sementsov-Ogievskiy * at function start, and they must be still dirty, as we've locked 1431dbdf699cSVladimir Sementsov-Ogievskiy * the region for in-flight op. 1432dbdf699cSVladimir Sementsov-Ogievskiy */ 1433*870f8963SEric Blake dirty_bitmap_offset = QEMU_ALIGN_DOWN(offset, job->granularity); 1434*870f8963SEric Blake dirty_bitmap_end = QEMU_ALIGN_UP(offset + bytes, job->granularity); 1435*870f8963SEric Blake bdrv_set_dirty_bitmap(job->dirty_bitmap, dirty_bitmap_offset, 1436*870f8963SEric Blake dirty_bitmap_end - dirty_bitmap_offset); 143776cb2f24SFiona Ebner qatomic_set(&job->actively_synced, false); 1438d06107adSMax Reitz 1439d06107adSMax Reitz action = mirror_error_action(job, false, -ret); 1440d06107adSMax Reitz if (action == BLOCK_ERROR_ACTION_REPORT) { 1441d06107adSMax Reitz if (!job->ret) { 1442d06107adSMax Reitz job->ret = ret; 1443d06107adSMax Reitz } 1444d06107adSMax Reitz } 1445d06107adSMax Reitz } 1446d06107adSMax Reitz } 1447d06107adSMax Reitz 1448d06107adSMax Reitz static MirrorOp *coroutine_fn active_write_prepare(MirrorBlockJob *s, 1449d06107adSMax Reitz uint64_t offset, 1450d06107adSMax Reitz uint64_t bytes) 1451d06107adSMax Reitz { 1452d06107adSMax Reitz MirrorOp *op; 1453d06107adSMax Reitz uint64_t start_chunk = offset / s->granularity; 1454d06107adSMax Reitz uint64_t end_chunk = DIV_ROUND_UP(offset + bytes, s->granularity); 1455d06107adSMax Reitz 1456d06107adSMax Reitz op = g_new(MirrorOp, 1); 1457d06107adSMax Reitz *op = (MirrorOp){ 1458d06107adSMax Reitz .s = s, 1459d06107adSMax Reitz .offset = offset, 1460d06107adSMax Reitz .bytes = bytes, 1461d06107adSMax Reitz .is_active_write = true, 1462ce8cabbdSKevin Wolf .is_in_flight = true, 1463ead3f1bfSVladimir Sementsov-Ogievskiy .co = qemu_coroutine_self(), 1464d06107adSMax Reitz }; 1465d06107adSMax Reitz qemu_co_queue_init(&op->waiting_requests); 1466d06107adSMax Reitz QTAILQ_INSERT_TAIL(&s->ops_in_flight, op, next); 1467d06107adSMax Reitz 1468d06107adSMax Reitz s->in_active_write_counter++; 1469d06107adSMax Reitz 1470d69a879bSHanna Reitz /* 1471d69a879bSHanna Reitz * Wait for concurrent requests affecting the area. If there are already 1472d69a879bSHanna Reitz * running requests that are copying off now-to-be stale data in the area, 1473d69a879bSHanna Reitz * we must wait for them to finish before we begin writing fresh data to the 1474d69a879bSHanna Reitz * target so that the write operations appear in the correct order. 1475d69a879bSHanna Reitz * Note that background requests (see mirror_iteration()) in contrast only 1476d69a879bSHanna Reitz * wait for conflicting requests at the start of the dirty area, and then 1477d69a879bSHanna Reitz * (based on the in_flight_bitmap) truncate the area to copy so it will not 1478d69a879bSHanna Reitz * conflict with any requests beyond that. For active writes, however, we 1479d69a879bSHanna Reitz * cannot truncate that area. The request from our parent must be blocked 1480d69a879bSHanna Reitz * until the area is copied in full. Therefore, we must wait for the whole 1481d69a879bSHanna Reitz * area to become free of concurrent requests. 1482d69a879bSHanna Reitz */ 1483d06107adSMax Reitz mirror_wait_on_conflicts(op, s, offset, bytes); 1484d06107adSMax Reitz 1485d06107adSMax Reitz bitmap_set(s->in_flight_bitmap, start_chunk, end_chunk - start_chunk); 1486d06107adSMax Reitz 1487d06107adSMax Reitz return op; 1488d06107adSMax Reitz } 1489d06107adSMax Reitz 14909c93652dSKevin Wolf static void coroutine_fn GRAPH_RDLOCK active_write_settle(MirrorOp *op) 1491d06107adSMax Reitz { 1492d06107adSMax Reitz uint64_t start_chunk = op->offset / op->s->granularity; 1493d06107adSMax Reitz uint64_t end_chunk = DIV_ROUND_UP(op->offset + op->bytes, 1494d06107adSMax Reitz op->s->granularity); 1495d06107adSMax Reitz 149676cb2f24SFiona Ebner if (!--op->s->in_active_write_counter && 149776cb2f24SFiona Ebner qatomic_read(&op->s->actively_synced)) { 1498d06107adSMax Reitz BdrvChild *source = op->s->mirror_top_bs->backing; 1499d06107adSMax Reitz 1500d06107adSMax Reitz if (QLIST_FIRST(&source->bs->parents) == source && 1501d06107adSMax Reitz QLIST_NEXT(source, next_parent) == NULL) 1502d06107adSMax Reitz { 1503d06107adSMax Reitz /* Assert that we are back in sync once all active write 1504d06107adSMax Reitz * operations are settled. 1505d06107adSMax Reitz * Note that we can only assert this if the mirror node 1506d06107adSMax Reitz * is the source node's only parent. */ 1507d06107adSMax Reitz assert(!bdrv_get_dirty_count(op->s->dirty_bitmap)); 1508d06107adSMax Reitz } 1509d06107adSMax Reitz } 1510d06107adSMax Reitz bitmap_clear(op->s->in_flight_bitmap, start_chunk, end_chunk - start_chunk); 1511d06107adSMax Reitz QTAILQ_REMOVE(&op->s->ops_in_flight, op, next); 1512d06107adSMax Reitz qemu_co_queue_restart_all(&op->waiting_requests); 1513d06107adSMax Reitz g_free(op); 1514d06107adSMax Reitz } 1515d06107adSMax Reitz 1516b9b10c35SKevin Wolf static int coroutine_fn GRAPH_RDLOCK 1517b9b10c35SKevin Wolf bdrv_mirror_top_preadv(BlockDriverState *bs, int64_t offset, int64_t bytes, 1518b9b10c35SKevin Wolf QEMUIOVector *qiov, BdrvRequestFlags flags) 15194ef85a9cSKevin Wolf { 15204ef85a9cSKevin Wolf return bdrv_co_preadv(bs->backing, offset, bytes, qiov, flags); 15214ef85a9cSKevin Wolf } 15224ef85a9cSKevin Wolf 15237b32ad22SFiona Ebner static bool should_copy_to_target(MirrorBDSOpaque *s) 15247b32ad22SFiona Ebner { 15257b32ad22SFiona Ebner return s->job && s->job->ret >= 0 && 15267b32ad22SFiona Ebner !job_is_cancelled(&s->job->common.job) && 15272d400d15SFiona Ebner qatomic_read(&s->job->copy_mode) == MIRROR_COPY_MODE_WRITE_BLOCKING; 15287b32ad22SFiona Ebner } 15297b32ad22SFiona Ebner 15309a5a1c62SEmanuele Giuseppe Esposito static int coroutine_fn GRAPH_RDLOCK 15319a5a1c62SEmanuele Giuseppe Esposito bdrv_mirror_top_do_write(BlockDriverState *bs, MirrorMethod method, 15327b32ad22SFiona Ebner bool copy_to_target, uint64_t offset, uint64_t bytes, 15337b32ad22SFiona Ebner QEMUIOVector *qiov, int flags) 1534d06107adSMax Reitz { 1535d06107adSMax Reitz MirrorOp *op = NULL; 1536d06107adSMax Reitz MirrorBDSOpaque *s = bs->opaque; 1537d06107adSMax Reitz int ret = 0; 1538d06107adSMax Reitz 1539d06107adSMax Reitz if (copy_to_target) { 1540d06107adSMax Reitz op = active_write_prepare(s->job, offset, bytes); 1541d06107adSMax Reitz } 1542d06107adSMax Reitz 1543d06107adSMax Reitz switch (method) { 1544d06107adSMax Reitz case MIRROR_METHOD_COPY: 1545d06107adSMax Reitz ret = bdrv_co_pwritev(bs->backing, offset, bytes, qiov, flags); 1546d06107adSMax Reitz break; 1547d06107adSMax Reitz 1548d06107adSMax Reitz case MIRROR_METHOD_ZERO: 1549d06107adSMax Reitz ret = bdrv_co_pwrite_zeroes(bs->backing, offset, bytes, flags); 1550d06107adSMax Reitz break; 1551d06107adSMax Reitz 1552d06107adSMax Reitz case MIRROR_METHOD_DISCARD: 15530b9fd3f4SFam Zheng ret = bdrv_co_pdiscard(bs->backing, offset, bytes); 1554d06107adSMax Reitz break; 1555d06107adSMax Reitz 1556d06107adSMax Reitz default: 1557d06107adSMax Reitz abort(); 1558d06107adSMax Reitz } 1559d06107adSMax Reitz 1560058cfca5SFiona Ebner if (!copy_to_target && s->job && s->job->dirty_bitmap) { 156176cb2f24SFiona Ebner qatomic_set(&s->job->actively_synced, false); 1562058cfca5SFiona Ebner bdrv_set_dirty_bitmap(s->job->dirty_bitmap, offset, bytes); 1563058cfca5SFiona Ebner } 1564058cfca5SFiona Ebner 1565d06107adSMax Reitz if (ret < 0) { 1566d06107adSMax Reitz goto out; 1567d06107adSMax Reitz } 1568d06107adSMax Reitz 1569d06107adSMax Reitz if (copy_to_target) { 1570d06107adSMax Reitz do_sync_target_write(s->job, method, offset, bytes, qiov, flags); 1571d06107adSMax Reitz } 1572d06107adSMax Reitz 1573d06107adSMax Reitz out: 1574d06107adSMax Reitz if (copy_to_target) { 1575d06107adSMax Reitz active_write_settle(op); 1576d06107adSMax Reitz } 1577d06107adSMax Reitz return ret; 1578d06107adSMax Reitz } 1579d06107adSMax Reitz 1580b9b10c35SKevin Wolf static int coroutine_fn GRAPH_RDLOCK 1581b9b10c35SKevin Wolf bdrv_mirror_top_pwritev(BlockDriverState *bs, int64_t offset, int64_t bytes, 1582b9b10c35SKevin Wolf QEMUIOVector *qiov, BdrvRequestFlags flags) 15834ef85a9cSKevin Wolf { 1584d06107adSMax Reitz QEMUIOVector bounce_qiov; 1585d06107adSMax Reitz void *bounce_buf; 1586d06107adSMax Reitz int ret = 0; 15877b32ad22SFiona Ebner bool copy_to_target = should_copy_to_target(bs->opaque); 1588d06107adSMax Reitz 1589d06107adSMax Reitz if (copy_to_target) { 1590d06107adSMax Reitz /* The guest might concurrently modify the data to write; but 1591d06107adSMax Reitz * the data on source and destination must match, so we have 1592d06107adSMax Reitz * to use a bounce buffer if we are going to write to the 1593d06107adSMax Reitz * target now. */ 1594d06107adSMax Reitz bounce_buf = qemu_blockalign(bs, bytes); 1595d06107adSMax Reitz iov_to_buf_full(qiov->iov, qiov->niov, 0, bounce_buf, bytes); 1596d06107adSMax Reitz 1597d06107adSMax Reitz qemu_iovec_init(&bounce_qiov, 1); 1598d06107adSMax Reitz qemu_iovec_add(&bounce_qiov, bounce_buf, bytes); 1599d06107adSMax Reitz qiov = &bounce_qiov; 1600e8b65355SStefan Hajnoczi 1601e8b65355SStefan Hajnoczi flags &= ~BDRV_REQ_REGISTERED_BUF; 1602d06107adSMax Reitz } 1603d06107adSMax Reitz 16047b32ad22SFiona Ebner ret = bdrv_mirror_top_do_write(bs, MIRROR_METHOD_COPY, copy_to_target, 16057b32ad22SFiona Ebner offset, bytes, qiov, flags); 1606d06107adSMax Reitz 1607d06107adSMax Reitz if (copy_to_target) { 1608d06107adSMax Reitz qemu_iovec_destroy(&bounce_qiov); 1609d06107adSMax Reitz qemu_vfree(bounce_buf); 1610d06107adSMax Reitz } 1611d06107adSMax Reitz 1612d06107adSMax Reitz return ret; 16134ef85a9cSKevin Wolf } 16144ef85a9cSKevin Wolf 161588095349SEmanuele Giuseppe Esposito static int coroutine_fn GRAPH_RDLOCK bdrv_mirror_top_flush(BlockDriverState *bs) 16164ef85a9cSKevin Wolf { 1617ce960aa9SVladimir Sementsov-Ogievskiy if (bs->backing == NULL) { 1618ce960aa9SVladimir Sementsov-Ogievskiy /* we can be here after failed bdrv_append in mirror_start_job */ 1619ce960aa9SVladimir Sementsov-Ogievskiy return 0; 1620ce960aa9SVladimir Sementsov-Ogievskiy } 16214ef85a9cSKevin Wolf return bdrv_co_flush(bs->backing->bs); 16224ef85a9cSKevin Wolf } 16234ef85a9cSKevin Wolf 1624abaf8b75SKevin Wolf static int coroutine_fn GRAPH_RDLOCK 1625abaf8b75SKevin Wolf bdrv_mirror_top_pwrite_zeroes(BlockDriverState *bs, int64_t offset, 1626abaf8b75SKevin Wolf int64_t bytes, BdrvRequestFlags flags) 16274ef85a9cSKevin Wolf { 16287b32ad22SFiona Ebner bool copy_to_target = should_copy_to_target(bs->opaque); 16297b32ad22SFiona Ebner return bdrv_mirror_top_do_write(bs, MIRROR_METHOD_ZERO, copy_to_target, 16307b32ad22SFiona Ebner offset, bytes, NULL, flags); 16314ef85a9cSKevin Wolf } 16324ef85a9cSKevin Wolf 16339a5a1c62SEmanuele Giuseppe Esposito static int coroutine_fn GRAPH_RDLOCK 16349a5a1c62SEmanuele Giuseppe Esposito bdrv_mirror_top_pdiscard(BlockDriverState *bs, int64_t offset, int64_t bytes) 16354ef85a9cSKevin Wolf { 16367b32ad22SFiona Ebner bool copy_to_target = should_copy_to_target(bs->opaque); 16377b32ad22SFiona Ebner return bdrv_mirror_top_do_write(bs, MIRROR_METHOD_DISCARD, copy_to_target, 16387b32ad22SFiona Ebner offset, bytes, NULL, 0); 16394ef85a9cSKevin Wolf } 16404ef85a9cSKevin Wolf 1641004915a9SKevin Wolf static void GRAPH_RDLOCK bdrv_mirror_top_refresh_filename(BlockDriverState *bs) 1642fd4a6493SKevin Wolf { 164318775ff3SVladimir Sementsov-Ogievskiy if (bs->backing == NULL) { 164418775ff3SVladimir Sementsov-Ogievskiy /* we can be here after failed bdrv_attach_child in 164518775ff3SVladimir Sementsov-Ogievskiy * bdrv_set_backing_hd */ 164618775ff3SVladimir Sementsov-Ogievskiy return; 164718775ff3SVladimir Sementsov-Ogievskiy } 1648fd4a6493SKevin Wolf pstrcpy(bs->exact_filename, sizeof(bs->exact_filename), 1649fd4a6493SKevin Wolf bs->backing->bs->filename); 1650fd4a6493SKevin Wolf } 1651fd4a6493SKevin Wolf 16524ef85a9cSKevin Wolf static void bdrv_mirror_top_child_perm(BlockDriverState *bs, BdrvChild *c, 1653bf8e925eSMax Reitz BdrvChildRole role, 1654e0995dc3SKevin Wolf BlockReopenQueue *reopen_queue, 16554ef85a9cSKevin Wolf uint64_t perm, uint64_t shared, 16564ef85a9cSKevin Wolf uint64_t *nperm, uint64_t *nshared) 16574ef85a9cSKevin Wolf { 1658f94dc3b4SMax Reitz MirrorBDSOpaque *s = bs->opaque; 1659f94dc3b4SMax Reitz 1660f94dc3b4SMax Reitz if (s->stop) { 1661f94dc3b4SMax Reitz /* 1662f94dc3b4SMax Reitz * If the job is to be stopped, we do not need to forward 1663f94dc3b4SMax Reitz * anything to the real image. 1664f94dc3b4SMax Reitz */ 1665f94dc3b4SMax Reitz *nperm = 0; 1666f94dc3b4SMax Reitz *nshared = BLK_PERM_ALL; 1667f94dc3b4SMax Reitz return; 1668f94dc3b4SMax Reitz } 1669f94dc3b4SMax Reitz 167053431b90SMax Reitz bdrv_default_perms(bs, c, role, reopen_queue, 167153431b90SMax Reitz perm, shared, nperm, nshared); 16724ef85a9cSKevin Wolf 167353431b90SMax Reitz if (s->is_commit) { 167453431b90SMax Reitz /* 167553431b90SMax Reitz * For commit jobs, we cannot take CONSISTENT_READ, because 167653431b90SMax Reitz * that permission is unshared for everything above the base 167753431b90SMax Reitz * node (except for filters on the base node). 167853431b90SMax Reitz * We also have to force-share the WRITE permission, or 167953431b90SMax Reitz * otherwise we would block ourselves at the base node (if 168053431b90SMax Reitz * writes are blocked for a node, they are also blocked for 168153431b90SMax Reitz * its backing file). 168253431b90SMax Reitz * (We could also share RESIZE, because it may be needed for 168353431b90SMax Reitz * the target if its size is less than the top node's; but 168453431b90SMax Reitz * bdrv_default_perms_for_cow() automatically shares RESIZE 168553431b90SMax Reitz * for backing nodes if WRITE is shared, so there is no need 168653431b90SMax Reitz * to do it here.) 168753431b90SMax Reitz */ 168853431b90SMax Reitz *nperm &= ~BLK_PERM_CONSISTENT_READ; 168953431b90SMax Reitz *nshared |= BLK_PERM_WRITE; 169053431b90SMax Reitz } 16914ef85a9cSKevin Wolf } 16924ef85a9cSKevin Wolf 16934ef85a9cSKevin Wolf /* Dummy node that provides consistent read to its users without requiring it 16944ef85a9cSKevin Wolf * from its backing file and that allows writes on the backing file chain. */ 16954ef85a9cSKevin Wolf static BlockDriver bdrv_mirror_top = { 16964ef85a9cSKevin Wolf .format_name = "mirror_top", 16974ef85a9cSKevin Wolf .bdrv_co_preadv = bdrv_mirror_top_preadv, 16984ef85a9cSKevin Wolf .bdrv_co_pwritev = bdrv_mirror_top_pwritev, 16994ef85a9cSKevin Wolf .bdrv_co_pwrite_zeroes = bdrv_mirror_top_pwrite_zeroes, 17004ef85a9cSKevin Wolf .bdrv_co_pdiscard = bdrv_mirror_top_pdiscard, 17014ef85a9cSKevin Wolf .bdrv_co_flush = bdrv_mirror_top_flush, 1702fd4a6493SKevin Wolf .bdrv_refresh_filename = bdrv_mirror_top_refresh_filename, 17034ef85a9cSKevin Wolf .bdrv_child_perm = bdrv_mirror_top_child_perm, 17046540fd15SMax Reitz 17056540fd15SMax Reitz .is_filter = true, 1706046fd84fSVladimir Sementsov-Ogievskiy .filtered_child_is_backing = true, 17074ef85a9cSKevin Wolf }; 17084ef85a9cSKevin Wolf 1709cc19f177SVladimir Sementsov-Ogievskiy static BlockJob *mirror_start_job( 1710cc19f177SVladimir Sementsov-Ogievskiy const char *job_id, BlockDriverState *bs, 171147970dfbSJohn Snow int creation_flags, BlockDriverState *target, 171247970dfbSJohn Snow const char *replaces, int64_t speed, 171347970dfbSJohn Snow uint32_t granularity, int64_t buf_size, 1714274fcceeSMax Reitz BlockMirrorBackingMode backing_mode, 1715cdf3bc93SMax Reitz bool zero_target, 171603544a6eSFam Zheng BlockdevOnError on_source_error, 1717b952b558SPaolo Bonzini BlockdevOnError on_target_error, 17180fc9f8eaSFam Zheng bool unmap, 1719097310b5SMarkus Armbruster BlockCompletionFunc *cb, 172051ccfa2dSFam Zheng void *opaque, 172103544a6eSFam Zheng const BlockJobDriver *driver, 1722b49f7eadSWen Congyang bool is_none_mode, BlockDriverState *base, 172351ccfa2dSFam Zheng bool auto_complete, const char *filter_node_name, 1724481debaaSMax Reitz bool is_mirror, MirrorCopyMode copy_mode, 17257d99ae59SAlexander Ivanov bool base_ro, 172651ccfa2dSFam Zheng Error **errp) 1727893f7ebaSPaolo Bonzini { 1728893f7ebaSPaolo Bonzini MirrorBlockJob *s; 1729429076e8SMax Reitz MirrorBDSOpaque *bs_opaque; 17304ef85a9cSKevin Wolf BlockDriverState *mirror_top_bs; 17314ef85a9cSKevin Wolf bool target_is_backing; 17323f072a7fSMax Reitz uint64_t target_perms, target_shared_perms; 1733d7086422SKevin Wolf int ret; 1734893f7ebaSPaolo Bonzini 17353804e3cfSKevin Wolf GLOBAL_STATE_CODE(); 17363804e3cfSKevin Wolf 1737eee13dfeSPaolo Bonzini if (granularity == 0) { 1738341ebc2fSJohn Snow granularity = bdrv_get_default_bitmap_granularity(target); 1739eee13dfeSPaolo Bonzini } 1740eee13dfeSPaolo Bonzini 174131826642SEric Blake assert(is_power_of_2(granularity)); 1742eee13dfeSPaolo Bonzini 174348ac0a4dSWen Congyang if (buf_size < 0) { 174448ac0a4dSWen Congyang error_setg(errp, "Invalid parameter 'buf-size'"); 1745cc19f177SVladimir Sementsov-Ogievskiy return NULL; 174648ac0a4dSWen Congyang } 174748ac0a4dSWen Congyang 174848ac0a4dSWen Congyang if (buf_size == 0) { 174948ac0a4dSWen Congyang buf_size = DEFAULT_MIRROR_BUF_SIZE; 175048ac0a4dSWen Congyang } 17515bc361b8SFam Zheng 1752ad74751fSKevin Wolf bdrv_graph_rdlock_main_loop(); 17533f072a7fSMax Reitz if (bdrv_skip_filters(bs) == bdrv_skip_filters(target)) { 175486fae10cSKevin Wolf error_setg(errp, "Can't mirror node into itself"); 1755ad74751fSKevin Wolf bdrv_graph_rdunlock_main_loop(); 1756cc19f177SVladimir Sementsov-Ogievskiy return NULL; 175786fae10cSKevin Wolf } 175886fae10cSKevin Wolf 175953431b90SMax Reitz target_is_backing = bdrv_chain_contains(bs, target); 1760ad74751fSKevin Wolf bdrv_graph_rdunlock_main_loop(); 176153431b90SMax Reitz 17624ef85a9cSKevin Wolf /* In the case of active commit, add dummy driver to provide consistent 17634ef85a9cSKevin Wolf * reads on the top, while disabling it in the intermediate nodes, and make 17644ef85a9cSKevin Wolf * the backing chain writable. */ 17656cdbceb1SKevin Wolf mirror_top_bs = bdrv_new_open_driver(&bdrv_mirror_top, filter_node_name, 17666cdbceb1SKevin Wolf BDRV_O_RDWR, errp); 17674ef85a9cSKevin Wolf if (mirror_top_bs == NULL) { 1768cc19f177SVladimir Sementsov-Ogievskiy return NULL; 1769893f7ebaSPaolo Bonzini } 1770d3c8c674SKevin Wolf if (!filter_node_name) { 1771d3c8c674SKevin Wolf mirror_top_bs->implicit = true; 1772d3c8c674SKevin Wolf } 1773e5182c1cSMax Reitz 1774e5182c1cSMax Reitz /* So that we can always drop this node */ 1775e5182c1cSMax Reitz mirror_top_bs->never_freeze = true; 1776e5182c1cSMax Reitz 17774ef85a9cSKevin Wolf mirror_top_bs->total_sectors = bs->total_sectors; 1778228345bfSMax Reitz mirror_top_bs->supported_write_flags = BDRV_REQ_WRITE_UNCHANGED; 177980f5c33fSKevin Wolf mirror_top_bs->supported_zero_flags = BDRV_REQ_WRITE_UNCHANGED | 178080f5c33fSKevin Wolf BDRV_REQ_NO_FALLBACK; 1781429076e8SMax Reitz bs_opaque = g_new0(MirrorBDSOpaque, 1); 1782429076e8SMax Reitz mirror_top_bs->opaque = bs_opaque; 1783893f7ebaSPaolo Bonzini 178453431b90SMax Reitz bs_opaque->is_commit = target_is_backing; 178553431b90SMax Reitz 17864ef85a9cSKevin Wolf bdrv_drained_begin(bs); 1787934aee14SVladimir Sementsov-Ogievskiy ret = bdrv_append(mirror_top_bs, bs, errp); 17884ef85a9cSKevin Wolf bdrv_drained_end(bs); 17894ef85a9cSKevin Wolf 1790934aee14SVladimir Sementsov-Ogievskiy if (ret < 0) { 1791b2c2832cSKevin Wolf bdrv_unref(mirror_top_bs); 1792cc19f177SVladimir Sementsov-Ogievskiy return NULL; 1793b2c2832cSKevin Wolf } 1794b2c2832cSKevin Wolf 17954ef85a9cSKevin Wolf /* Make sure that the source is not resized while the job is running */ 179675859b94SJohn Snow s = block_job_create(job_id, driver, NULL, mirror_top_bs, 17974ef85a9cSKevin Wolf BLK_PERM_CONSISTENT_READ, 17984ef85a9cSKevin Wolf BLK_PERM_CONSISTENT_READ | BLK_PERM_WRITE_UNCHANGED | 179964631f36SVladimir Sementsov-Ogievskiy BLK_PERM_WRITE, speed, 18004ef85a9cSKevin Wolf creation_flags, cb, opaque, errp); 18014ef85a9cSKevin Wolf if (!s) { 18024ef85a9cSKevin Wolf goto fail; 18034ef85a9cSKevin Wolf } 1804429076e8SMax Reitz 18057a25fcd0SMax Reitz /* The block job now has a reference to this node */ 18067a25fcd0SMax Reitz bdrv_unref(mirror_top_bs); 18077a25fcd0SMax Reitz 18084ef85a9cSKevin Wolf s->mirror_top_bs = mirror_top_bs; 18097d99ae59SAlexander Ivanov s->base_ro = base_ro; 18104ef85a9cSKevin Wolf 18114ef85a9cSKevin Wolf /* No resize for the target either; while the mirror is still running, a 18124ef85a9cSKevin Wolf * consistent read isn't necessarily possible. We could possibly allow 18134ef85a9cSKevin Wolf * writes and graph modifications, though it would likely defeat the 18144ef85a9cSKevin Wolf * purpose of a mirror, so leave them blocked for now. 18154ef85a9cSKevin Wolf * 18164ef85a9cSKevin Wolf * In the case of active commit, things look a bit different, though, 18174ef85a9cSKevin Wolf * because the target is an already populated backing file in active use. 18184ef85a9cSKevin Wolf * We can allow anything except resize there.*/ 18193f072a7fSMax Reitz 18203f072a7fSMax Reitz target_perms = BLK_PERM_WRITE; 18213f072a7fSMax Reitz target_shared_perms = BLK_PERM_WRITE_UNCHANGED; 18223f072a7fSMax Reitz 18233f072a7fSMax Reitz if (target_is_backing) { 18243f072a7fSMax Reitz int64_t bs_size, target_size; 18253f072a7fSMax Reitz bs_size = bdrv_getlength(bs); 18263f072a7fSMax Reitz if (bs_size < 0) { 18273f072a7fSMax Reitz error_setg_errno(errp, -bs_size, 18283f072a7fSMax Reitz "Could not inquire top image size"); 18293f072a7fSMax Reitz goto fail; 18303f072a7fSMax Reitz } 18313f072a7fSMax Reitz 18323f072a7fSMax Reitz target_size = bdrv_getlength(target); 18333f072a7fSMax Reitz if (target_size < 0) { 18343f072a7fSMax Reitz error_setg_errno(errp, -target_size, 18353f072a7fSMax Reitz "Could not inquire base image size"); 18363f072a7fSMax Reitz goto fail; 18373f072a7fSMax Reitz } 18383f072a7fSMax Reitz 18393f072a7fSMax Reitz if (target_size < bs_size) { 18403f072a7fSMax Reitz target_perms |= BLK_PERM_RESIZE; 18413f072a7fSMax Reitz } 18423f072a7fSMax Reitz 184364631f36SVladimir Sementsov-Ogievskiy target_shared_perms |= BLK_PERM_CONSISTENT_READ | BLK_PERM_WRITE; 1844ad74751fSKevin Wolf } else { 1845ad74751fSKevin Wolf bdrv_graph_rdlock_main_loop(); 1846ad74751fSKevin Wolf if (bdrv_chain_contains(bs, bdrv_skip_filters(target))) { 18473f072a7fSMax Reitz /* 18483f072a7fSMax Reitz * We may want to allow this in the future, but it would 18493f072a7fSMax Reitz * require taking some extra care. 18503f072a7fSMax Reitz */ 1851ad74751fSKevin Wolf error_setg(errp, "Cannot mirror to a filter on top of a node in " 1852ad74751fSKevin Wolf "the source's backing chain"); 1853ad74751fSKevin Wolf bdrv_graph_rdunlock_main_loop(); 18543f072a7fSMax Reitz goto fail; 18553f072a7fSMax Reitz } 1856ad74751fSKevin Wolf bdrv_graph_rdunlock_main_loop(); 1857ad74751fSKevin Wolf } 18583f072a7fSMax Reitz 1859d861ab3aSKevin Wolf s->target = blk_new(s->common.job.aio_context, 18603f072a7fSMax Reitz target_perms, target_shared_perms); 1861d7086422SKevin Wolf ret = blk_insert_bs(s->target, target, errp); 1862d7086422SKevin Wolf if (ret < 0) { 18634ef85a9cSKevin Wolf goto fail; 1864d7086422SKevin Wolf } 1865045a2f82SFam Zheng if (is_mirror) { 1866045a2f82SFam Zheng /* XXX: Mirror target could be a NBD server of target QEMU in the case 1867045a2f82SFam Zheng * of non-shared block migration. To allow migration completion, we 1868045a2f82SFam Zheng * have to allow "inactivate" of the target BB. When that happens, we 1869045a2f82SFam Zheng * know the job is drained, and the vcpus are stopped, so no write 1870045a2f82SFam Zheng * operation will be performed. Block layer already has assertions to 1871045a2f82SFam Zheng * ensure that. */ 1872045a2f82SFam Zheng blk_set_force_allow_inactivate(s->target); 1873045a2f82SFam Zheng } 18749ff7f0dfSKevin Wolf blk_set_allow_aio_context_change(s->target, true); 1875cf312932SKevin Wolf blk_set_disable_request_queuing(s->target, true); 1876e253f4b8SKevin Wolf 1877ad74751fSKevin Wolf bdrv_graph_rdlock_main_loop(); 187809158f00SBenoît Canet s->replaces = g_strdup(replaces); 1879b952b558SPaolo Bonzini s->on_source_error = on_source_error; 1880b952b558SPaolo Bonzini s->on_target_error = on_target_error; 188103544a6eSFam Zheng s->is_none_mode = is_none_mode; 1882274fcceeSMax Reitz s->backing_mode = backing_mode; 1883cdf3bc93SMax Reitz s->zero_target = zero_target; 18842d400d15SFiona Ebner qatomic_set(&s->copy_mode, copy_mode); 18855bc361b8SFam Zheng s->base = base; 18863f072a7fSMax Reitz s->base_overlay = bdrv_find_overlay(bs, base); 1887eee13dfeSPaolo Bonzini s->granularity = granularity; 188848ac0a4dSWen Congyang s->buf_size = ROUND_UP(buf_size, granularity); 18890fc9f8eaSFam Zheng s->unmap = unmap; 1890b49f7eadSWen Congyang if (auto_complete) { 1891b49f7eadSWen Congyang s->should_complete = true; 1892b49f7eadSWen Congyang } 1893ad74751fSKevin Wolf bdrv_graph_rdunlock_main_loop(); 1894b812f671SPaolo Bonzini 1895058cfca5SFiona Ebner s->dirty_bitmap = bdrv_create_dirty_bitmap(s->mirror_top_bs, granularity, 1896058cfca5SFiona Ebner NULL, errp); 1897b8afb520SFam Zheng if (!s->dirty_bitmap) { 189888f9d1b3SKevin Wolf goto fail; 1899b8afb520SFam Zheng } 1900058cfca5SFiona Ebner 1901058cfca5SFiona Ebner /* 1902058cfca5SFiona Ebner * The dirty bitmap is set by bdrv_mirror_top_do_write() when not in active 1903058cfca5SFiona Ebner * mode. 1904058cfca5SFiona Ebner */ 1905dbdf699cSVladimir Sementsov-Ogievskiy bdrv_disable_dirty_bitmap(s->dirty_bitmap); 190610f3cd15SAlberto Garcia 19076bc30f19SStefan Hajnoczi bdrv_graph_wrlock(); 190867b24427SAlberto Garcia ret = block_job_add_bdrv(&s->common, "source", bs, 0, 190967b24427SAlberto Garcia BLK_PERM_WRITE_UNCHANGED | BLK_PERM_WRITE | 191067b24427SAlberto Garcia BLK_PERM_CONSISTENT_READ, 191167b24427SAlberto Garcia errp); 191267b24427SAlberto Garcia if (ret < 0) { 19136bc30f19SStefan Hajnoczi bdrv_graph_wrunlock(); 191467b24427SAlberto Garcia goto fail; 191567b24427SAlberto Garcia } 191667b24427SAlberto Garcia 19174ef85a9cSKevin Wolf /* Required permissions are already taken with blk_new() */ 191876d554e2SKevin Wolf block_job_add_bdrv(&s->common, "target", target, 0, BLK_PERM_ALL, 191976d554e2SKevin Wolf &error_abort); 192076d554e2SKevin Wolf 1921f3ede4b0SAlberto Garcia /* In commit_active_start() all intermediate nodes disappear, so 1922f3ede4b0SAlberto Garcia * any jobs in them must be blocked */ 19234ef85a9cSKevin Wolf if (target_is_backing) { 19243f072a7fSMax Reitz BlockDriverState *iter, *filtered_target; 19253f072a7fSMax Reitz uint64_t iter_shared_perms; 19263f072a7fSMax Reitz 19273f072a7fSMax Reitz /* 19283f072a7fSMax Reitz * The topmost node with 19293f072a7fSMax Reitz * bdrv_skip_filters(filtered_target) == bdrv_skip_filters(target) 19303f072a7fSMax Reitz */ 19313f072a7fSMax Reitz filtered_target = bdrv_cow_bs(bdrv_find_overlay(bs, target)); 19323f072a7fSMax Reitz 19333f072a7fSMax Reitz assert(bdrv_skip_filters(filtered_target) == 19343f072a7fSMax Reitz bdrv_skip_filters(target)); 19353f072a7fSMax Reitz 19363f072a7fSMax Reitz /* 19373f072a7fSMax Reitz * XXX BLK_PERM_WRITE needs to be allowed so we don't block 19384ef85a9cSKevin Wolf * ourselves at s->base (if writes are blocked for a node, they are 19394ef85a9cSKevin Wolf * also blocked for its backing file). The other options would be a 19403f072a7fSMax Reitz * second filter driver above s->base (== target). 19413f072a7fSMax Reitz */ 19423f072a7fSMax Reitz iter_shared_perms = BLK_PERM_WRITE_UNCHANGED | BLK_PERM_WRITE; 19433f072a7fSMax Reitz 19443f072a7fSMax Reitz for (iter = bdrv_filter_or_cow_bs(bs); iter != target; 19453f072a7fSMax Reitz iter = bdrv_filter_or_cow_bs(iter)) 19463f072a7fSMax Reitz { 19473f072a7fSMax Reitz if (iter == filtered_target) { 19483f072a7fSMax Reitz /* 19493f072a7fSMax Reitz * From here on, all nodes are filters on the base. 19503f072a7fSMax Reitz * This allows us to share BLK_PERM_CONSISTENT_READ. 19513f072a7fSMax Reitz */ 19523f072a7fSMax Reitz iter_shared_perms |= BLK_PERM_CONSISTENT_READ; 19533f072a7fSMax Reitz } 19543f072a7fSMax Reitz 19554ef85a9cSKevin Wolf ret = block_job_add_bdrv(&s->common, "intermediate node", iter, 0, 19563f072a7fSMax Reitz iter_shared_perms, errp); 19574ef85a9cSKevin Wolf if (ret < 0) { 19586bc30f19SStefan Hajnoczi bdrv_graph_wrunlock(); 19594ef85a9cSKevin Wolf goto fail; 19604ef85a9cSKevin Wolf } 1961f3ede4b0SAlberto Garcia } 1962ef53dc09SAlberto Garcia 1963ef53dc09SAlberto Garcia if (bdrv_freeze_backing_chain(mirror_top_bs, target, errp) < 0) { 19646bc30f19SStefan Hajnoczi bdrv_graph_wrunlock(); 1965ef53dc09SAlberto Garcia goto fail; 1966ef53dc09SAlberto Garcia } 1967f3ede4b0SAlberto Garcia } 19686bc30f19SStefan Hajnoczi bdrv_graph_wrunlock(); 196910f3cd15SAlberto Garcia 197012aa4082SMax Reitz QTAILQ_INIT(&s->ops_in_flight); 197112aa4082SMax Reitz 19725ccac6f1SJohn Snow trace_mirror_start(bs, s, opaque); 1973da01ff7fSKevin Wolf job_start(&s->common.job); 1974cc19f177SVladimir Sementsov-Ogievskiy 1975cc19f177SVladimir Sementsov-Ogievskiy return &s->common; 19764ef85a9cSKevin Wolf 19774ef85a9cSKevin Wolf fail: 19784ef85a9cSKevin Wolf if (s) { 19797a25fcd0SMax Reitz /* Make sure this BDS does not go away until we have completed the graph 19807a25fcd0SMax Reitz * changes below */ 19817a25fcd0SMax Reitz bdrv_ref(mirror_top_bs); 19827a25fcd0SMax Reitz 19834ef85a9cSKevin Wolf g_free(s->replaces); 19844ef85a9cSKevin Wolf blk_unref(s->target); 1985429076e8SMax Reitz bs_opaque->job = NULL; 1986e917e2cbSAlberto Garcia if (s->dirty_bitmap) { 19875deb6cbdSVladimir Sementsov-Ogievskiy bdrv_release_dirty_bitmap(s->dirty_bitmap); 1988e917e2cbSAlberto Garcia } 19894ad35181SKevin Wolf job_early_fail(&s->common.job); 19904ef85a9cSKevin Wolf } 19914ef85a9cSKevin Wolf 1992f94dc3b4SMax Reitz bs_opaque->stop = true; 1993ccd6a379SKevin Wolf bdrv_drained_begin(bs); 19946bc30f19SStefan Hajnoczi bdrv_graph_wrlock(); 1995ccd6a379SKevin Wolf assert(mirror_top_bs->backing->bs == bs); 1996f94dc3b4SMax Reitz bdrv_child_refresh_perms(mirror_top_bs, mirror_top_bs->backing, 1997c1cef672SFam Zheng &error_abort); 1998ccd6a379SKevin Wolf bdrv_replace_node(mirror_top_bs, bs, &error_abort); 19996bc30f19SStefan Hajnoczi bdrv_graph_wrunlock(); 2000ccd6a379SKevin Wolf bdrv_drained_end(bs); 20017a25fcd0SMax Reitz 20027a25fcd0SMax Reitz bdrv_unref(mirror_top_bs); 2003cc19f177SVladimir Sementsov-Ogievskiy 2004cc19f177SVladimir Sementsov-Ogievskiy return NULL; 2005893f7ebaSPaolo Bonzini } 200603544a6eSFam Zheng 200771aa9867SAlberto Garcia void mirror_start(const char *job_id, BlockDriverState *bs, 200871aa9867SAlberto Garcia BlockDriverState *target, const char *replaces, 2009a1999b33SJohn Snow int creation_flags, int64_t speed, 2010a1999b33SJohn Snow uint32_t granularity, int64_t buf_size, 2011274fcceeSMax Reitz MirrorSyncMode mode, BlockMirrorBackingMode backing_mode, 2012cdf3bc93SMax Reitz bool zero_target, 2013274fcceeSMax Reitz BlockdevOnError on_source_error, 201403544a6eSFam Zheng BlockdevOnError on_target_error, 2015481debaaSMax Reitz bool unmap, const char *filter_node_name, 2016481debaaSMax Reitz MirrorCopyMode copy_mode, Error **errp) 201703544a6eSFam Zheng { 201803544a6eSFam Zheng bool is_none_mode; 201903544a6eSFam Zheng BlockDriverState *base; 202003544a6eSFam Zheng 2021b4ad82aaSEmanuele Giuseppe Esposito GLOBAL_STATE_CODE(); 2022b4ad82aaSEmanuele Giuseppe Esposito 2023c8b56501SJohn Snow if ((mode == MIRROR_SYNC_MODE_INCREMENTAL) || 2024c8b56501SJohn Snow (mode == MIRROR_SYNC_MODE_BITMAP)) { 2025c8b56501SJohn Snow error_setg(errp, "Sync mode '%s' not supported", 2026c8b56501SJohn Snow MirrorSyncMode_str(mode)); 2027d58d8453SJohn Snow return; 2028d58d8453SJohn Snow } 2029ad74751fSKevin Wolf 2030ad74751fSKevin Wolf bdrv_graph_rdlock_main_loop(); 203103544a6eSFam Zheng is_none_mode = mode == MIRROR_SYNC_MODE_NONE; 20323f072a7fSMax Reitz base = mode == MIRROR_SYNC_MODE_TOP ? bdrv_backing_chain_next(bs) : NULL; 2033ad74751fSKevin Wolf bdrv_graph_rdunlock_main_loop(); 2034ad74751fSKevin Wolf 2035a1999b33SJohn Snow mirror_start_job(job_id, bs, creation_flags, target, replaces, 2036cdf3bc93SMax Reitz speed, granularity, buf_size, backing_mode, zero_target, 203751ccfa2dSFam Zheng on_source_error, on_target_error, unmap, NULL, NULL, 20386cdbceb1SKevin Wolf &mirror_job_driver, is_none_mode, base, false, 20397d99ae59SAlexander Ivanov filter_node_name, true, copy_mode, false, errp); 204003544a6eSFam Zheng } 204103544a6eSFam Zheng 2042cc19f177SVladimir Sementsov-Ogievskiy BlockJob *commit_active_start(const char *job_id, BlockDriverState *bs, 204347970dfbSJohn Snow BlockDriverState *base, int creation_flags, 204447970dfbSJohn Snow int64_t speed, BlockdevOnError on_error, 20450db832f4SKevin Wolf const char *filter_node_name, 204678bbd910SFam Zheng BlockCompletionFunc *cb, void *opaque, 204778bbd910SFam Zheng bool auto_complete, Error **errp) 204803544a6eSFam Zheng { 20491ba79388SAlberto Garcia bool base_read_only; 2050eb5becc1SVladimir Sementsov-Ogievskiy BlockJob *job; 20514da83585SJeff Cody 2052b4ad82aaSEmanuele Giuseppe Esposito GLOBAL_STATE_CODE(); 2053b4ad82aaSEmanuele Giuseppe Esposito 20541ba79388SAlberto Garcia base_read_only = bdrv_is_read_only(base); 20554da83585SJeff Cody 20561ba79388SAlberto Garcia if (base_read_only) { 20571ba79388SAlberto Garcia if (bdrv_reopen_set_read_only(base, false, errp) < 0) { 2058cc19f177SVladimir Sementsov-Ogievskiy return NULL; 205920a63d2cSFam Zheng } 20601ba79388SAlberto Garcia } 20614da83585SJeff Cody 2062eb5becc1SVladimir Sementsov-Ogievskiy job = mirror_start_job( 2063cc19f177SVladimir Sementsov-Ogievskiy job_id, bs, creation_flags, base, NULL, speed, 0, 0, 2064cdf3bc93SMax Reitz MIRROR_LEAVE_BACKING_CHAIN, false, 206551ccfa2dSFam Zheng on_error, on_error, true, cb, opaque, 20666cdbceb1SKevin Wolf &commit_active_job_driver, false, base, auto_complete, 2067481debaaSMax Reitz filter_node_name, false, MIRROR_COPY_MODE_BACKGROUND, 20687d99ae59SAlexander Ivanov base_read_only, errp); 2069eb5becc1SVladimir Sementsov-Ogievskiy if (!job) { 20704da83585SJeff Cody goto error_restore_flags; 20714da83585SJeff Cody } 20724da83585SJeff Cody 2073eb5becc1SVladimir Sementsov-Ogievskiy return job; 20744da83585SJeff Cody 20754da83585SJeff Cody error_restore_flags: 20764da83585SJeff Cody /* ignore error and errp for bdrv_reopen, because we want to propagate 20774da83585SJeff Cody * the original error */ 20781ba79388SAlberto Garcia if (base_read_only) { 20791ba79388SAlberto Garcia bdrv_reopen_set_read_only(base, true, NULL); 20801ba79388SAlberto Garcia } 2081cc19f177SVladimir Sementsov-Ogievskiy return NULL; 208203544a6eSFam Zheng } 2083