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" 21373340b2SMax Reitz #include "sysemu/block-backend.h" 22da34e65cSMarkus Armbruster #include "qapi/error.h" 23cc7a8ea7SMarkus Armbruster #include "qapi/qmp/qerror.h" 24893f7ebaSPaolo Bonzini #include "qemu/ratelimit.h" 25b812f671SPaolo Bonzini #include "qemu/bitmap.h" 26893f7ebaSPaolo Bonzini 27402a4741SPaolo Bonzini #define MAX_IN_FLIGHT 16 28b436982fSEric Blake #define MAX_IO_BYTES (1 << 20) /* 1 Mb */ 29b436982fSEric Blake #define DEFAULT_MIRROR_BUF_SIZE (MAX_IN_FLIGHT * MAX_IO_BYTES) 30402a4741SPaolo Bonzini 31402a4741SPaolo Bonzini /* The mirroring buffer is a list of granularity-sized chunks. 32402a4741SPaolo Bonzini * Free chunks are organized in a list. 33402a4741SPaolo Bonzini */ 34402a4741SPaolo Bonzini typedef struct MirrorBuffer { 35402a4741SPaolo Bonzini QSIMPLEQ_ENTRY(MirrorBuffer) next; 36402a4741SPaolo Bonzini } MirrorBuffer; 37893f7ebaSPaolo Bonzini 3812aa4082SMax Reitz typedef struct MirrorOp MirrorOp; 3912aa4082SMax Reitz 40893f7ebaSPaolo Bonzini typedef struct MirrorBlockJob { 41893f7ebaSPaolo Bonzini BlockJob common; 42e253f4b8SKevin Wolf BlockBackend *target; 434ef85a9cSKevin Wolf BlockDriverState *mirror_top_bs; 445bc361b8SFam Zheng BlockDriverState *base; 454ef85a9cSKevin Wolf 4609158f00SBenoît Canet /* The name of the graph node to replace */ 4709158f00SBenoît Canet char *replaces; 4809158f00SBenoît Canet /* The BDS to replace */ 4909158f00SBenoît Canet BlockDriverState *to_replace; 5009158f00SBenoît Canet /* Used to block operations on the drive-mirror-replace target */ 5109158f00SBenoît Canet Error *replace_blocker; 5203544a6eSFam Zheng bool is_none_mode; 53274fcceeSMax Reitz BlockMirrorBackingMode backing_mode; 54cdf3bc93SMax Reitz /* Whether the target image requires explicit zero-initialization */ 55cdf3bc93SMax Reitz bool zero_target; 56d06107adSMax Reitz MirrorCopyMode copy_mode; 57b952b558SPaolo Bonzini BlockdevOnError on_source_error, on_target_error; 58d63ffd87SPaolo Bonzini bool synced; 59d06107adSMax Reitz /* Set when the target is synced (dirty bitmap is clean, nothing 60d06107adSMax Reitz * in flight) and the job is running in active mode */ 61d06107adSMax Reitz bool actively_synced; 62d63ffd87SPaolo Bonzini bool should_complete; 63eee13dfeSPaolo Bonzini int64_t granularity; 64b812f671SPaolo Bonzini size_t buf_size; 65b21c7652SMax Reitz int64_t bdev_length; 66b812f671SPaolo Bonzini unsigned long *cow_bitmap; 67e4654d2dSFam Zheng BdrvDirtyBitmap *dirty_bitmap; 68dc162c8eSFam Zheng BdrvDirtyBitmapIter *dbi; 69893f7ebaSPaolo Bonzini uint8_t *buf; 70402a4741SPaolo Bonzini QSIMPLEQ_HEAD(, MirrorBuffer) buf_free; 71402a4741SPaolo Bonzini int buf_free_count; 72bd48bde8SPaolo Bonzini 7349efb1f5SDenis V. Lunev uint64_t last_pause_ns; 74402a4741SPaolo Bonzini unsigned long *in_flight_bitmap; 75bd48bde8SPaolo Bonzini int in_flight; 76b436982fSEric Blake int64_t bytes_in_flight; 77b58deb34SPaolo Bonzini QTAILQ_HEAD(, MirrorOp) ops_in_flight; 78bd48bde8SPaolo Bonzini int ret; 790fc9f8eaSFam Zheng bool unmap; 80b436982fSEric Blake int target_cluster_size; 81e5b43573SFam Zheng int max_iov; 8290ab48ebSAnton Nefedov bool initial_zeroing_ongoing; 83d06107adSMax Reitz int in_active_write_counter; 84737efc1eSJohn Snow bool prepared; 855e771752SSergio Lopez bool in_drain; 86893f7ebaSPaolo Bonzini } MirrorBlockJob; 87893f7ebaSPaolo Bonzini 88429076e8SMax Reitz typedef struct MirrorBDSOpaque { 89429076e8SMax Reitz MirrorBlockJob *job; 90f94dc3b4SMax Reitz bool stop; 91429076e8SMax Reitz } MirrorBDSOpaque; 92429076e8SMax Reitz 9312aa4082SMax Reitz struct MirrorOp { 94bd48bde8SPaolo Bonzini MirrorBlockJob *s; 95bd48bde8SPaolo Bonzini QEMUIOVector qiov; 96b436982fSEric Blake int64_t offset; 97b436982fSEric Blake uint64_t bytes; 982e1990b2SMax Reitz 992e1990b2SMax Reitz /* The pointee is set by mirror_co_read(), mirror_co_zero(), and 1002e1990b2SMax Reitz * mirror_co_discard() before yielding for the first time */ 1012e1990b2SMax Reitz int64_t *bytes_handled; 10212aa4082SMax Reitz 1031181e19aSMax Reitz bool is_pseudo_op; 104d06107adSMax Reitz bool is_active_write; 10512aa4082SMax Reitz CoQueue waiting_requests; 106eed325b9SKevin Wolf Coroutine *co; 10712aa4082SMax Reitz 10812aa4082SMax Reitz QTAILQ_ENTRY(MirrorOp) next; 10912aa4082SMax Reitz }; 110bd48bde8SPaolo Bonzini 1114295c5fcSMax Reitz typedef enum MirrorMethod { 1124295c5fcSMax Reitz MIRROR_METHOD_COPY, 1134295c5fcSMax Reitz MIRROR_METHOD_ZERO, 1144295c5fcSMax Reitz MIRROR_METHOD_DISCARD, 1154295c5fcSMax Reitz } MirrorMethod; 1164295c5fcSMax Reitz 117b952b558SPaolo Bonzini static BlockErrorAction mirror_error_action(MirrorBlockJob *s, bool read, 118b952b558SPaolo Bonzini int error) 119b952b558SPaolo Bonzini { 120b952b558SPaolo Bonzini s->synced = false; 121d06107adSMax Reitz s->actively_synced = false; 122b952b558SPaolo Bonzini if (read) { 12381e254dcSKevin Wolf return block_job_error_action(&s->common, s->on_source_error, 12481e254dcSKevin Wolf true, error); 125b952b558SPaolo Bonzini } else { 12681e254dcSKevin Wolf return block_job_error_action(&s->common, s->on_target_error, 12781e254dcSKevin Wolf false, error); 128b952b558SPaolo Bonzini } 129b952b558SPaolo Bonzini } 130b952b558SPaolo Bonzini 1311181e19aSMax Reitz static void coroutine_fn mirror_wait_on_conflicts(MirrorOp *self, 1321181e19aSMax Reitz MirrorBlockJob *s, 1331181e19aSMax Reitz uint64_t offset, 1341181e19aSMax Reitz uint64_t bytes) 1351181e19aSMax Reitz { 1361181e19aSMax Reitz uint64_t self_start_chunk = offset / s->granularity; 1371181e19aSMax Reitz uint64_t self_end_chunk = DIV_ROUND_UP(offset + bytes, s->granularity); 1381181e19aSMax Reitz uint64_t self_nb_chunks = self_end_chunk - self_start_chunk; 1391181e19aSMax Reitz 1401181e19aSMax Reitz while (find_next_bit(s->in_flight_bitmap, self_end_chunk, 1411181e19aSMax Reitz self_start_chunk) < self_end_chunk && 1421181e19aSMax Reitz s->ret >= 0) 1431181e19aSMax Reitz { 1441181e19aSMax Reitz MirrorOp *op; 1451181e19aSMax Reitz 1461181e19aSMax Reitz QTAILQ_FOREACH(op, &s->ops_in_flight, next) { 1471181e19aSMax Reitz uint64_t op_start_chunk = op->offset / s->granularity; 1481181e19aSMax Reitz uint64_t op_nb_chunks = DIV_ROUND_UP(op->offset + op->bytes, 1491181e19aSMax Reitz s->granularity) - 1501181e19aSMax Reitz op_start_chunk; 1511181e19aSMax Reitz 1521181e19aSMax Reitz if (op == self) { 1531181e19aSMax Reitz continue; 1541181e19aSMax Reitz } 1551181e19aSMax Reitz 1561181e19aSMax Reitz if (ranges_overlap(self_start_chunk, self_nb_chunks, 1571181e19aSMax Reitz op_start_chunk, op_nb_chunks)) 1581181e19aSMax Reitz { 1591181e19aSMax Reitz qemu_co_queue_wait(&op->waiting_requests, NULL); 1601181e19aSMax Reitz break; 1611181e19aSMax Reitz } 1621181e19aSMax Reitz } 1631181e19aSMax Reitz } 1641181e19aSMax Reitz } 1651181e19aSMax Reitz 1662e1990b2SMax Reitz static void coroutine_fn mirror_iteration_done(MirrorOp *op, int ret) 167bd48bde8SPaolo Bonzini { 168bd48bde8SPaolo Bonzini MirrorBlockJob *s = op->s; 169402a4741SPaolo Bonzini struct iovec *iov; 170bd48bde8SPaolo Bonzini int64_t chunk_num; 171b436982fSEric Blake int i, nb_chunks; 172bd48bde8SPaolo Bonzini 173b436982fSEric Blake trace_mirror_iteration_done(s, op->offset, op->bytes, ret); 174bd48bde8SPaolo Bonzini 175bd48bde8SPaolo Bonzini s->in_flight--; 176b436982fSEric Blake s->bytes_in_flight -= op->bytes; 177402a4741SPaolo Bonzini iov = op->qiov.iov; 178402a4741SPaolo Bonzini for (i = 0; i < op->qiov.niov; i++) { 179402a4741SPaolo Bonzini MirrorBuffer *buf = (MirrorBuffer *) iov[i].iov_base; 180402a4741SPaolo Bonzini QSIMPLEQ_INSERT_TAIL(&s->buf_free, buf, next); 181402a4741SPaolo Bonzini s->buf_free_count++; 182402a4741SPaolo Bonzini } 183402a4741SPaolo Bonzini 184b436982fSEric Blake chunk_num = op->offset / s->granularity; 185b436982fSEric Blake nb_chunks = DIV_ROUND_UP(op->bytes, s->granularity); 18612aa4082SMax Reitz 187402a4741SPaolo Bonzini bitmap_clear(s->in_flight_bitmap, chunk_num, nb_chunks); 18812aa4082SMax Reitz QTAILQ_REMOVE(&s->ops_in_flight, op, next); 189b21c7652SMax Reitz if (ret >= 0) { 190b21c7652SMax Reitz if (s->cow_bitmap) { 191bd48bde8SPaolo Bonzini bitmap_set(s->cow_bitmap, chunk_num, nb_chunks); 192bd48bde8SPaolo Bonzini } 19390ab48ebSAnton Nefedov if (!s->initial_zeroing_ongoing) { 19430a5c887SKevin Wolf job_progress_update(&s->common.job, op->bytes); 195b21c7652SMax Reitz } 19690ab48ebSAnton Nefedov } 1976df3bf8eSZhang Min qemu_iovec_destroy(&op->qiov); 1987b770c72SStefan Hajnoczi 19912aa4082SMax Reitz qemu_co_queue_restart_all(&op->waiting_requests); 20012aa4082SMax Reitz g_free(op); 2017b770c72SStefan Hajnoczi } 202bd48bde8SPaolo Bonzini 2032e1990b2SMax Reitz static void coroutine_fn mirror_write_complete(MirrorOp *op, int ret) 204bd48bde8SPaolo Bonzini { 205bd48bde8SPaolo Bonzini MirrorBlockJob *s = op->s; 206b9e413ddSPaolo Bonzini 207bd48bde8SPaolo Bonzini if (ret < 0) { 208bd48bde8SPaolo Bonzini BlockErrorAction action; 209bd48bde8SPaolo Bonzini 210e0d7f73eSEric Blake bdrv_set_dirty_bitmap(s->dirty_bitmap, op->offset, op->bytes); 211bd48bde8SPaolo Bonzini action = mirror_error_action(s, false, -ret); 212a589569fSWenchao Xia if (action == BLOCK_ERROR_ACTION_REPORT && s->ret >= 0) { 213bd48bde8SPaolo Bonzini s->ret = ret; 214bd48bde8SPaolo Bonzini } 215bd48bde8SPaolo Bonzini } 216d12ade57SVladimir Sementsov-Ogievskiy 217bd48bde8SPaolo Bonzini mirror_iteration_done(op, ret); 218bd48bde8SPaolo Bonzini } 219bd48bde8SPaolo Bonzini 2202e1990b2SMax Reitz static void coroutine_fn mirror_read_complete(MirrorOp *op, int ret) 221bd48bde8SPaolo Bonzini { 222bd48bde8SPaolo Bonzini MirrorBlockJob *s = op->s; 223b9e413ddSPaolo Bonzini 224bd48bde8SPaolo Bonzini if (ret < 0) { 225bd48bde8SPaolo Bonzini BlockErrorAction action; 226bd48bde8SPaolo Bonzini 227e0d7f73eSEric Blake bdrv_set_dirty_bitmap(s->dirty_bitmap, op->offset, op->bytes); 228bd48bde8SPaolo Bonzini action = mirror_error_action(s, true, -ret); 229a589569fSWenchao Xia if (action == BLOCK_ERROR_ACTION_REPORT && s->ret >= 0) { 230bd48bde8SPaolo Bonzini s->ret = ret; 231bd48bde8SPaolo Bonzini } 232bd48bde8SPaolo Bonzini 233bd48bde8SPaolo Bonzini mirror_iteration_done(op, ret); 234d12ade57SVladimir Sementsov-Ogievskiy return; 235bd48bde8SPaolo Bonzini } 236d12ade57SVladimir Sementsov-Ogievskiy 237d12ade57SVladimir Sementsov-Ogievskiy ret = blk_co_pwritev(s->target, op->offset, op->qiov.size, &op->qiov, 0); 238d12ade57SVladimir Sementsov-Ogievskiy mirror_write_complete(op, ret); 239b9e413ddSPaolo Bonzini } 240bd48bde8SPaolo Bonzini 241782d97efSEric Blake /* Clip bytes relative to offset to not exceed end-of-file */ 242782d97efSEric Blake static inline int64_t mirror_clip_bytes(MirrorBlockJob *s, 243782d97efSEric Blake int64_t offset, 244782d97efSEric Blake int64_t bytes) 245782d97efSEric Blake { 246782d97efSEric Blake return MIN(bytes, s->bdev_length - offset); 247782d97efSEric Blake } 248782d97efSEric Blake 249782d97efSEric Blake /* Round offset and/or bytes to target cluster if COW is needed, and 250782d97efSEric Blake * return the offset of the adjusted tail against original. */ 251782d97efSEric Blake static int mirror_cow_align(MirrorBlockJob *s, int64_t *offset, 252ae4cc877SEric Blake uint64_t *bytes) 253893f7ebaSPaolo Bonzini { 254e5b43573SFam Zheng bool need_cow; 255e5b43573SFam Zheng int ret = 0; 256782d97efSEric Blake int64_t align_offset = *offset; 2577cfd5275SEric Blake int64_t align_bytes = *bytes; 258782d97efSEric Blake int max_bytes = s->granularity * s->max_iov; 259893f7ebaSPaolo Bonzini 260782d97efSEric Blake need_cow = !test_bit(*offset / s->granularity, s->cow_bitmap); 261782d97efSEric Blake need_cow |= !test_bit((*offset + *bytes - 1) / s->granularity, 262e5b43573SFam Zheng s->cow_bitmap); 263e5b43573SFam Zheng if (need_cow) { 264782d97efSEric Blake bdrv_round_to_clusters(blk_bs(s->target), *offset, *bytes, 265782d97efSEric Blake &align_offset, &align_bytes); 2668f0720ecSPaolo Bonzini } 2678f0720ecSPaolo Bonzini 268782d97efSEric Blake if (align_bytes > max_bytes) { 269782d97efSEric Blake align_bytes = max_bytes; 270e5b43573SFam Zheng if (need_cow) { 271782d97efSEric Blake align_bytes = QEMU_ALIGN_DOWN(align_bytes, s->target_cluster_size); 272e5b43573SFam Zheng } 273e5b43573SFam Zheng } 274782d97efSEric Blake /* Clipping may result in align_bytes unaligned to chunk boundary, but 2754150ae60SFam Zheng * that doesn't matter because it's already the end of source image. */ 276782d97efSEric Blake align_bytes = mirror_clip_bytes(s, align_offset, align_bytes); 277402a4741SPaolo Bonzini 278782d97efSEric Blake ret = align_offset + align_bytes - (*offset + *bytes); 279782d97efSEric Blake *offset = align_offset; 280782d97efSEric Blake *bytes = align_bytes; 281e5b43573SFam Zheng assert(ret >= 0); 282e5b43573SFam Zheng return ret; 283e5b43573SFam Zheng } 284e5b43573SFam Zheng 285537c3d4fSStefan Hajnoczi static inline void coroutine_fn 2867e6c4ff7SKevin Wolf mirror_wait_for_any_operation(MirrorBlockJob *s, MirrorOp *self, bool active) 28721cd917fSFam Zheng { 28812aa4082SMax Reitz MirrorOp *op; 28912aa4082SMax Reitz 2901181e19aSMax Reitz QTAILQ_FOREACH(op, &s->ops_in_flight, next) { 2917e6c4ff7SKevin Wolf if (self == op) { 2927e6c4ff7SKevin Wolf continue; 2937e6c4ff7SKevin Wolf } 2941181e19aSMax Reitz /* Do not wait on pseudo ops, because it may in turn wait on 2951181e19aSMax Reitz * some other operation to start, which may in fact be the 2961181e19aSMax Reitz * caller of this function. Since there is only one pseudo op 2971181e19aSMax Reitz * at any given time, we will always find some real operation 2981181e19aSMax Reitz * to wait on. */ 299d06107adSMax Reitz if (!op->is_pseudo_op && op->is_active_write == active) { 30012aa4082SMax Reitz qemu_co_queue_wait(&op->waiting_requests, NULL); 3011181e19aSMax Reitz return; 3021181e19aSMax Reitz } 3031181e19aSMax Reitz } 3041181e19aSMax Reitz abort(); 30521cd917fSFam Zheng } 30621cd917fSFam Zheng 307537c3d4fSStefan Hajnoczi static inline void coroutine_fn 3087e6c4ff7SKevin Wolf mirror_wait_for_free_in_flight_slot(MirrorBlockJob *s, MirrorOp *self) 309d06107adSMax Reitz { 310d06107adSMax Reitz /* Only non-active operations use up in-flight slots */ 3117e6c4ff7SKevin Wolf mirror_wait_for_any_operation(s, self, false); 312d06107adSMax Reitz } 313d06107adSMax Reitz 3142e1990b2SMax Reitz /* Perform a mirror copy operation. 3152e1990b2SMax Reitz * 3162e1990b2SMax Reitz * *op->bytes_handled is set to the number of bytes copied after and 3172e1990b2SMax Reitz * including offset, excluding any bytes copied prior to offset due 3182e1990b2SMax Reitz * to alignment. This will be op->bytes if no alignment is necessary, 3192e1990b2SMax Reitz * or (new_end - op->offset) if the tail is rounded up or down due to 320e5b43573SFam Zheng * alignment or buffer limit. 321402a4741SPaolo Bonzini */ 3222e1990b2SMax Reitz static void coroutine_fn mirror_co_read(void *opaque) 323e5b43573SFam Zheng { 3242e1990b2SMax Reitz MirrorOp *op = opaque; 3252e1990b2SMax Reitz MirrorBlockJob *s = op->s; 326ae4cc877SEric Blake int nb_chunks; 327ae4cc877SEric Blake uint64_t ret; 328ae4cc877SEric Blake uint64_t max_bytes; 329402a4741SPaolo Bonzini 330ae4cc877SEric Blake max_bytes = s->granularity * s->max_iov; 331e5b43573SFam Zheng 332e5b43573SFam Zheng /* We can only handle as much as buf_size at a time. */ 3332e1990b2SMax Reitz op->bytes = MIN(s->buf_size, MIN(max_bytes, op->bytes)); 3342e1990b2SMax Reitz assert(op->bytes); 3352e1990b2SMax Reitz assert(op->bytes < BDRV_REQUEST_MAX_BYTES); 3362e1990b2SMax Reitz *op->bytes_handled = op->bytes; 337e5b43573SFam Zheng 338e5b43573SFam Zheng if (s->cow_bitmap) { 3392e1990b2SMax Reitz *op->bytes_handled += mirror_cow_align(s, &op->offset, &op->bytes); 340e5b43573SFam Zheng } 3412e1990b2SMax Reitz /* Cannot exceed BDRV_REQUEST_MAX_BYTES + INT_MAX */ 3422e1990b2SMax Reitz assert(*op->bytes_handled <= UINT_MAX); 3432e1990b2SMax Reitz assert(op->bytes <= s->buf_size); 344ae4cc877SEric Blake /* The offset is granularity-aligned because: 345e5b43573SFam Zheng * 1) Caller passes in aligned values; 346e5b43573SFam Zheng * 2) mirror_cow_align is used only when target cluster is larger. */ 3472e1990b2SMax Reitz assert(QEMU_IS_ALIGNED(op->offset, s->granularity)); 348ae4cc877SEric Blake /* The range is sector-aligned, since bdrv_getlength() rounds up. */ 3492e1990b2SMax Reitz assert(QEMU_IS_ALIGNED(op->bytes, BDRV_SECTOR_SIZE)); 3502e1990b2SMax Reitz nb_chunks = DIV_ROUND_UP(op->bytes, s->granularity); 351e5b43573SFam Zheng 352e5b43573SFam Zheng while (s->buf_free_count < nb_chunks) { 3532e1990b2SMax Reitz trace_mirror_yield_in_flight(s, op->offset, s->in_flight); 3547e6c4ff7SKevin Wolf mirror_wait_for_free_in_flight_slot(s, op); 355b812f671SPaolo Bonzini } 356b812f671SPaolo Bonzini 357402a4741SPaolo Bonzini /* Now make a QEMUIOVector taking enough granularity-sized chunks 358402a4741SPaolo Bonzini * from s->buf_free. 359402a4741SPaolo Bonzini */ 360402a4741SPaolo Bonzini qemu_iovec_init(&op->qiov, nb_chunks); 361402a4741SPaolo Bonzini while (nb_chunks-- > 0) { 362402a4741SPaolo Bonzini MirrorBuffer *buf = QSIMPLEQ_FIRST(&s->buf_free); 3632e1990b2SMax Reitz size_t remaining = op->bytes - op->qiov.size; 3645a0f6fd5SKevin Wolf 365402a4741SPaolo Bonzini QSIMPLEQ_REMOVE_HEAD(&s->buf_free, next); 366402a4741SPaolo Bonzini s->buf_free_count--; 3675a0f6fd5SKevin Wolf qemu_iovec_add(&op->qiov, buf, MIN(s->granularity, remaining)); 368402a4741SPaolo Bonzini } 369402a4741SPaolo Bonzini 370893f7ebaSPaolo Bonzini /* Copy the dirty cluster. */ 371bd48bde8SPaolo Bonzini s->in_flight++; 3722e1990b2SMax Reitz s->bytes_in_flight += op->bytes; 3732e1990b2SMax Reitz trace_mirror_one_iteration(s, op->offset, op->bytes); 374dcfb3bebSFam Zheng 375138f9fffSMax Reitz ret = bdrv_co_preadv(s->mirror_top_bs->backing, op->offset, op->bytes, 376138f9fffSMax Reitz &op->qiov, 0); 3772e1990b2SMax Reitz mirror_read_complete(op, ret); 378e5b43573SFam Zheng } 379e5b43573SFam Zheng 3802e1990b2SMax Reitz static void coroutine_fn mirror_co_zero(void *opaque) 381e5b43573SFam Zheng { 3822e1990b2SMax Reitz MirrorOp *op = opaque; 3832e1990b2SMax Reitz int ret; 384e5b43573SFam Zheng 3852e1990b2SMax Reitz op->s->in_flight++; 3862e1990b2SMax Reitz op->s->bytes_in_flight += op->bytes; 3872e1990b2SMax Reitz *op->bytes_handled = op->bytes; 388e5b43573SFam Zheng 3892e1990b2SMax Reitz ret = blk_co_pwrite_zeroes(op->s->target, op->offset, op->bytes, 3902e1990b2SMax Reitz op->s->unmap ? BDRV_REQ_MAY_UNMAP : 0); 3912e1990b2SMax Reitz mirror_write_complete(op, ret); 392e5b43573SFam Zheng } 3932e1990b2SMax Reitz 3942e1990b2SMax Reitz static void coroutine_fn mirror_co_discard(void *opaque) 3952e1990b2SMax Reitz { 3962e1990b2SMax Reitz MirrorOp *op = opaque; 3972e1990b2SMax Reitz int ret; 3982e1990b2SMax Reitz 3992e1990b2SMax Reitz op->s->in_flight++; 4002e1990b2SMax Reitz op->s->bytes_in_flight += op->bytes; 4012e1990b2SMax Reitz *op->bytes_handled = op->bytes; 4022e1990b2SMax Reitz 4032e1990b2SMax Reitz ret = blk_co_pdiscard(op->s->target, op->offset, op->bytes); 4042e1990b2SMax Reitz mirror_write_complete(op, ret); 405e5b43573SFam Zheng } 406e5b43573SFam Zheng 4074295c5fcSMax Reitz static unsigned mirror_perform(MirrorBlockJob *s, int64_t offset, 4084295c5fcSMax Reitz unsigned bytes, MirrorMethod mirror_method) 4094295c5fcSMax Reitz { 4102e1990b2SMax Reitz MirrorOp *op; 4112e1990b2SMax Reitz Coroutine *co; 4122e1990b2SMax Reitz int64_t bytes_handled = -1; 4132e1990b2SMax Reitz 4142e1990b2SMax Reitz op = g_new(MirrorOp, 1); 4152e1990b2SMax Reitz *op = (MirrorOp){ 4162e1990b2SMax Reitz .s = s, 4172e1990b2SMax Reitz .offset = offset, 4182e1990b2SMax Reitz .bytes = bytes, 4192e1990b2SMax Reitz .bytes_handled = &bytes_handled, 4202e1990b2SMax Reitz }; 42112aa4082SMax Reitz qemu_co_queue_init(&op->waiting_requests); 4222e1990b2SMax Reitz 4234295c5fcSMax Reitz switch (mirror_method) { 4244295c5fcSMax Reitz case MIRROR_METHOD_COPY: 4252e1990b2SMax Reitz co = qemu_coroutine_create(mirror_co_read, op); 4262e1990b2SMax Reitz break; 4274295c5fcSMax Reitz case MIRROR_METHOD_ZERO: 4282e1990b2SMax Reitz co = qemu_coroutine_create(mirror_co_zero, op); 4292e1990b2SMax Reitz break; 4304295c5fcSMax Reitz case MIRROR_METHOD_DISCARD: 4312e1990b2SMax Reitz co = qemu_coroutine_create(mirror_co_discard, op); 4322e1990b2SMax Reitz break; 4334295c5fcSMax Reitz default: 4344295c5fcSMax Reitz abort(); 4354295c5fcSMax Reitz } 436eed325b9SKevin Wolf op->co = co; 4372e1990b2SMax Reitz 43812aa4082SMax Reitz QTAILQ_INSERT_TAIL(&s->ops_in_flight, op, next); 4392e1990b2SMax Reitz qemu_coroutine_enter(co); 4402e1990b2SMax Reitz /* At this point, ownership of op has been moved to the coroutine 4412e1990b2SMax Reitz * and the object may already be freed */ 4422e1990b2SMax Reitz 4432e1990b2SMax Reitz /* Assert that this value has been set */ 4442e1990b2SMax Reitz assert(bytes_handled >= 0); 4452e1990b2SMax Reitz 4462e1990b2SMax Reitz /* Same assertion as in mirror_co_read() (and for mirror_co_read() 4472e1990b2SMax Reitz * and mirror_co_discard(), bytes_handled == op->bytes, which 4482e1990b2SMax Reitz * is the @bytes parameter given to this function) */ 4492e1990b2SMax Reitz assert(bytes_handled <= UINT_MAX); 4502e1990b2SMax Reitz return bytes_handled; 4514295c5fcSMax Reitz } 4524295c5fcSMax Reitz 453e5b43573SFam Zheng static uint64_t coroutine_fn mirror_iteration(MirrorBlockJob *s) 454e5b43573SFam Zheng { 455138f9fffSMax Reitz BlockDriverState *source = s->mirror_top_bs->backing->bs; 4561181e19aSMax Reitz MirrorOp *pseudo_op; 4571181e19aSMax Reitz int64_t offset; 4581181e19aSMax Reitz uint64_t delay_ns = 0, ret = 0; 459e5b43573SFam Zheng /* At least the first dirty chunk is mirrored in one iteration. */ 460e5b43573SFam Zheng int nb_chunks = 1; 4614b5004d9SDenis V. Lunev bool write_zeroes_ok = bdrv_can_write_zeroes_with_unmap(blk_bs(s->target)); 462b436982fSEric Blake int max_io_bytes = MAX(s->buf_size / MAX_IN_FLIGHT, MAX_IO_BYTES); 463e5b43573SFam Zheng 464b64bd51eSPaolo Bonzini bdrv_dirty_bitmap_lock(s->dirty_bitmap); 465f798184cSEric Blake offset = bdrv_dirty_iter_next(s->dbi); 466fb2ef791SEric Blake if (offset < 0) { 467dc162c8eSFam Zheng bdrv_set_dirty_iter(s->dbi, 0); 468f798184cSEric Blake offset = bdrv_dirty_iter_next(s->dbi); 4699a46dba7SEric Blake trace_mirror_restart_iter(s, bdrv_get_dirty_count(s->dirty_bitmap)); 470fb2ef791SEric Blake assert(offset >= 0); 471e5b43573SFam Zheng } 472b64bd51eSPaolo Bonzini bdrv_dirty_bitmap_unlock(s->dirty_bitmap); 473e5b43573SFam Zheng 4741181e19aSMax Reitz mirror_wait_on_conflicts(NULL, s, offset, 1); 4759c83625bSMax Reitz 476da01ff7fSKevin Wolf job_pause_point(&s->common.job); 477565ac01fSStefan Hajnoczi 478e5b43573SFam Zheng /* Find the number of consective dirty chunks following the first dirty 479e5b43573SFam Zheng * one, and wait for in flight requests in them. */ 480b64bd51eSPaolo Bonzini bdrv_dirty_bitmap_lock(s->dirty_bitmap); 481fb2ef791SEric Blake while (nb_chunks * s->granularity < s->buf_size) { 482dc162c8eSFam Zheng int64_t next_dirty; 483fb2ef791SEric Blake int64_t next_offset = offset + nb_chunks * s->granularity; 484fb2ef791SEric Blake int64_t next_chunk = next_offset / s->granularity; 485fb2ef791SEric Blake if (next_offset >= s->bdev_length || 48628636b82SJohn Snow !bdrv_dirty_bitmap_get_locked(s->dirty_bitmap, next_offset)) { 487e5b43573SFam Zheng break; 488e5b43573SFam Zheng } 489e5b43573SFam Zheng if (test_bit(next_chunk, s->in_flight_bitmap)) { 490e5b43573SFam Zheng break; 491e5b43573SFam Zheng } 4929c83625bSMax Reitz 493f798184cSEric Blake next_dirty = bdrv_dirty_iter_next(s->dbi); 494fb2ef791SEric Blake if (next_dirty > next_offset || next_dirty < 0) { 495f27a2742SMax Reitz /* The bitmap iterator's cache is stale, refresh it */ 496715a74d8SEric Blake bdrv_set_dirty_iter(s->dbi, next_offset); 497f798184cSEric Blake next_dirty = bdrv_dirty_iter_next(s->dbi); 498f27a2742SMax Reitz } 499fb2ef791SEric Blake assert(next_dirty == next_offset); 500e5b43573SFam Zheng nb_chunks++; 501e5b43573SFam Zheng } 502e5b43573SFam Zheng 503e5b43573SFam Zheng /* Clear dirty bits before querying the block status, because 50431826642SEric Blake * calling bdrv_block_status_above could yield - if some blocks are 505e5b43573SFam Zheng * marked dirty in this window, we need to know. 506e5b43573SFam Zheng */ 507e0d7f73eSEric Blake bdrv_reset_dirty_bitmap_locked(s->dirty_bitmap, offset, 508e0d7f73eSEric Blake nb_chunks * s->granularity); 509b64bd51eSPaolo Bonzini bdrv_dirty_bitmap_unlock(s->dirty_bitmap); 510b64bd51eSPaolo Bonzini 5111181e19aSMax Reitz /* Before claiming an area in the in-flight bitmap, we have to 5121181e19aSMax Reitz * create a MirrorOp for it so that conflicting requests can wait 5131181e19aSMax Reitz * for it. mirror_perform() will create the real MirrorOps later, 5141181e19aSMax Reitz * for now we just create a pseudo operation that will wake up all 5151181e19aSMax Reitz * conflicting requests once all real operations have been 5161181e19aSMax Reitz * launched. */ 5171181e19aSMax Reitz pseudo_op = g_new(MirrorOp, 1); 5181181e19aSMax Reitz *pseudo_op = (MirrorOp){ 5191181e19aSMax Reitz .offset = offset, 5201181e19aSMax Reitz .bytes = nb_chunks * s->granularity, 5211181e19aSMax Reitz .is_pseudo_op = true, 5221181e19aSMax Reitz }; 5231181e19aSMax Reitz qemu_co_queue_init(&pseudo_op->waiting_requests); 5241181e19aSMax Reitz QTAILQ_INSERT_TAIL(&s->ops_in_flight, pseudo_op, next); 5251181e19aSMax Reitz 526fb2ef791SEric Blake bitmap_set(s->in_flight_bitmap, offset / s->granularity, nb_chunks); 527fb2ef791SEric Blake while (nb_chunks > 0 && offset < s->bdev_length) { 52831826642SEric Blake int ret; 5297cfd5275SEric Blake int64_t io_bytes; 530f3e4ce4aSEric Blake int64_t io_bytes_acct; 5314295c5fcSMax Reitz MirrorMethod mirror_method = MIRROR_METHOD_COPY; 532e5b43573SFam Zheng 533fb2ef791SEric Blake assert(!(offset % s->granularity)); 53431826642SEric Blake ret = bdrv_block_status_above(source, NULL, offset, 53531826642SEric Blake nb_chunks * s->granularity, 53631826642SEric Blake &io_bytes, NULL, NULL); 537e5b43573SFam Zheng if (ret < 0) { 538fb2ef791SEric Blake io_bytes = MIN(nb_chunks * s->granularity, max_io_bytes); 5390965a41eSVladimir Sementsov-Ogievskiy } else if (ret & BDRV_BLOCK_DATA) { 540fb2ef791SEric Blake io_bytes = MIN(io_bytes, max_io_bytes); 541e5b43573SFam Zheng } 542e5b43573SFam Zheng 543fb2ef791SEric Blake io_bytes -= io_bytes % s->granularity; 544fb2ef791SEric Blake if (io_bytes < s->granularity) { 545fb2ef791SEric Blake io_bytes = s->granularity; 546e5b43573SFam Zheng } else if (ret >= 0 && !(ret & BDRV_BLOCK_DATA)) { 547fb2ef791SEric Blake int64_t target_offset; 5487cfd5275SEric Blake int64_t target_bytes; 549fb2ef791SEric Blake bdrv_round_to_clusters(blk_bs(s->target), offset, io_bytes, 550fb2ef791SEric Blake &target_offset, &target_bytes); 551fb2ef791SEric Blake if (target_offset == offset && 552fb2ef791SEric Blake target_bytes == io_bytes) { 553e5b43573SFam Zheng mirror_method = ret & BDRV_BLOCK_ZERO ? 554e5b43573SFam Zheng MIRROR_METHOD_ZERO : 555e5b43573SFam Zheng MIRROR_METHOD_DISCARD; 556e5b43573SFam Zheng } 557e5b43573SFam Zheng } 558e5b43573SFam Zheng 559cf56a3c6SDenis V. Lunev while (s->in_flight >= MAX_IN_FLIGHT) { 560fb2ef791SEric Blake trace_mirror_yield_in_flight(s, offset, s->in_flight); 5617e6c4ff7SKevin Wolf mirror_wait_for_free_in_flight_slot(s, pseudo_op); 562cf56a3c6SDenis V. Lunev } 563cf56a3c6SDenis V. Lunev 564dbaa7b57SVladimir Sementsov-Ogievskiy if (s->ret < 0) { 5651181e19aSMax Reitz ret = 0; 5661181e19aSMax Reitz goto fail; 567dbaa7b57SVladimir Sementsov-Ogievskiy } 568dbaa7b57SVladimir Sementsov-Ogievskiy 569fb2ef791SEric Blake io_bytes = mirror_clip_bytes(s, offset, io_bytes); 5704295c5fcSMax Reitz io_bytes = mirror_perform(s, offset, io_bytes, mirror_method); 5714295c5fcSMax Reitz if (mirror_method != MIRROR_METHOD_COPY && write_zeroes_ok) { 572f3e4ce4aSEric Blake io_bytes_acct = 0; 5734b5004d9SDenis V. Lunev } else { 574fb2ef791SEric Blake io_bytes_acct = io_bytes; 5754b5004d9SDenis V. Lunev } 576fb2ef791SEric Blake assert(io_bytes); 577fb2ef791SEric Blake offset += io_bytes; 578fb2ef791SEric Blake nb_chunks -= DIV_ROUND_UP(io_bytes, s->granularity); 579dee81d51SKevin Wolf delay_ns = block_job_ratelimit_get_delay(&s->common, io_bytes_acct); 580dcfb3bebSFam Zheng } 5811181e19aSMax Reitz 5821181e19aSMax Reitz ret = delay_ns; 5831181e19aSMax Reitz fail: 5841181e19aSMax Reitz QTAILQ_REMOVE(&s->ops_in_flight, pseudo_op, next); 5851181e19aSMax Reitz qemu_co_queue_restart_all(&pseudo_op->waiting_requests); 5861181e19aSMax Reitz g_free(pseudo_op); 5871181e19aSMax Reitz 5881181e19aSMax Reitz return ret; 589893f7ebaSPaolo Bonzini } 590b952b558SPaolo Bonzini 591402a4741SPaolo Bonzini static void mirror_free_init(MirrorBlockJob *s) 592402a4741SPaolo Bonzini { 593402a4741SPaolo Bonzini int granularity = s->granularity; 594402a4741SPaolo Bonzini size_t buf_size = s->buf_size; 595402a4741SPaolo Bonzini uint8_t *buf = s->buf; 596402a4741SPaolo Bonzini 597402a4741SPaolo Bonzini assert(s->buf_free_count == 0); 598402a4741SPaolo Bonzini QSIMPLEQ_INIT(&s->buf_free); 599402a4741SPaolo Bonzini while (buf_size != 0) { 600402a4741SPaolo Bonzini MirrorBuffer *cur = (MirrorBuffer *)buf; 601402a4741SPaolo Bonzini QSIMPLEQ_INSERT_TAIL(&s->buf_free, cur, next); 602402a4741SPaolo Bonzini s->buf_free_count++; 603402a4741SPaolo Bonzini buf_size -= granularity; 604402a4741SPaolo Bonzini buf += granularity; 605402a4741SPaolo Bonzini } 606402a4741SPaolo Bonzini } 607402a4741SPaolo Bonzini 608bae8196dSPaolo Bonzini /* This is also used for the .pause callback. There is no matching 609bae8196dSPaolo Bonzini * mirror_resume() because mirror_run() will begin iterating again 610bae8196dSPaolo Bonzini * when the job is resumed. 611bae8196dSPaolo Bonzini */ 612537c3d4fSStefan Hajnoczi static void coroutine_fn mirror_wait_for_all_io(MirrorBlockJob *s) 613bd48bde8SPaolo Bonzini { 614bd48bde8SPaolo Bonzini while (s->in_flight > 0) { 6157e6c4ff7SKevin Wolf mirror_wait_for_free_in_flight_slot(s, NULL); 616bd48bde8SPaolo Bonzini } 617893f7ebaSPaolo Bonzini } 618893f7ebaSPaolo Bonzini 619737efc1eSJohn Snow /** 620737efc1eSJohn Snow * mirror_exit_common: handle both abort() and prepare() cases. 621737efc1eSJohn Snow * for .prepare, returns 0 on success and -errno on failure. 622737efc1eSJohn Snow * for .abort cases, denoted by abort = true, MUST return 0. 623737efc1eSJohn Snow */ 624737efc1eSJohn Snow static int mirror_exit_common(Job *job) 6255a7e7a0bSStefan Hajnoczi { 6261908a559SKevin Wolf MirrorBlockJob *s = container_of(job, MirrorBlockJob, common.job); 6271908a559SKevin Wolf BlockJob *bjob = &s->common; 628f93c3addSMax Reitz MirrorBDSOpaque *bs_opaque; 6295a7e7a0bSStefan Hajnoczi AioContext *replace_aio_context = NULL; 630f93c3addSMax Reitz BlockDriverState *src; 631f93c3addSMax Reitz BlockDriverState *target_bs; 632f93c3addSMax Reitz BlockDriverState *mirror_top_bs; 63312fa4af6SKevin Wolf Error *local_err = NULL; 634737efc1eSJohn Snow bool abort = job->ret < 0; 635737efc1eSJohn Snow int ret = 0; 636737efc1eSJohn Snow 637737efc1eSJohn Snow if (s->prepared) { 638737efc1eSJohn Snow return 0; 639737efc1eSJohn Snow } 640737efc1eSJohn Snow s->prepared = true; 6413f09bfbcSKevin Wolf 642f93c3addSMax Reitz mirror_top_bs = s->mirror_top_bs; 643f93c3addSMax Reitz bs_opaque = mirror_top_bs->opaque; 644f93c3addSMax Reitz src = mirror_top_bs->backing->bs; 645f93c3addSMax Reitz target_bs = blk_bs(s->target); 646f93c3addSMax Reitz 647ef53dc09SAlberto Garcia if (bdrv_chain_contains(src, target_bs)) { 648ef53dc09SAlberto Garcia bdrv_unfreeze_backing_chain(mirror_top_bs, target_bs); 649ef53dc09SAlberto Garcia } 650ef53dc09SAlberto Garcia 6515deb6cbdSVladimir Sementsov-Ogievskiy bdrv_release_dirty_bitmap(s->dirty_bitmap); 6522119882cSPaolo Bonzini 6537b508f6bSJohn Snow /* Make sure that the source BDS doesn't go away during bdrv_replace_node, 6547b508f6bSJohn Snow * before we can call bdrv_drained_end */ 6553f09bfbcSKevin Wolf bdrv_ref(src); 6564ef85a9cSKevin Wolf bdrv_ref(mirror_top_bs); 6577d9fcb39SKevin Wolf bdrv_ref(target_bs); 6587d9fcb39SKevin Wolf 659bb0c9409SVladimir Sementsov-Ogievskiy /* 660bb0c9409SVladimir Sementsov-Ogievskiy * Remove target parent that still uses BLK_PERM_WRITE/RESIZE before 6617d9fcb39SKevin Wolf * inserting target_bs at s->to_replace, where we might not be able to get 66263c8ef28SKevin Wolf * these permissions. 663bb0c9409SVladimir Sementsov-Ogievskiy */ 6647d9fcb39SKevin Wolf blk_unref(s->target); 6657d9fcb39SKevin Wolf s->target = NULL; 6664ef85a9cSKevin Wolf 6674ef85a9cSKevin Wolf /* We don't access the source any more. Dropping any WRITE/RESIZE is 668d2da5e28SKevin Wolf * required before it could become a backing file of target_bs. Not having 669d2da5e28SKevin Wolf * these permissions any more means that we can't allow any new requests on 670d2da5e28SKevin Wolf * mirror_top_bs from now on, so keep it drained. */ 671d2da5e28SKevin Wolf bdrv_drained_begin(mirror_top_bs); 672f94dc3b4SMax Reitz bs_opaque->stop = true; 673f94dc3b4SMax Reitz bdrv_child_refresh_perms(mirror_top_bs, mirror_top_bs->backing, 6744ef85a9cSKevin Wolf &error_abort); 675737efc1eSJohn Snow if (!abort && s->backing_mode == MIRROR_SOURCE_BACKING_CHAIN) { 6764ef85a9cSKevin Wolf BlockDriverState *backing = s->is_none_mode ? src : s->base; 6774ef85a9cSKevin Wolf if (backing_bs(target_bs) != backing) { 67812fa4af6SKevin Wolf bdrv_set_backing_hd(target_bs, backing, &local_err); 67912fa4af6SKevin Wolf if (local_err) { 68012fa4af6SKevin Wolf error_report_err(local_err); 681*66c8672dSVladimir Sementsov-Ogievskiy local_err = NULL; 6827b508f6bSJohn Snow ret = -EPERM; 68312fa4af6SKevin Wolf } 6844ef85a9cSKevin Wolf } 6854ef85a9cSKevin Wolf } 6865a7e7a0bSStefan Hajnoczi 6875a7e7a0bSStefan Hajnoczi if (s->to_replace) { 6885a7e7a0bSStefan Hajnoczi replace_aio_context = bdrv_get_aio_context(s->to_replace); 6895a7e7a0bSStefan Hajnoczi aio_context_acquire(replace_aio_context); 6905a7e7a0bSStefan Hajnoczi } 6915a7e7a0bSStefan Hajnoczi 692737efc1eSJohn Snow if (s->should_complete && !abort) { 693737efc1eSJohn Snow BlockDriverState *to_replace = s->to_replace ?: src; 6941ba79388SAlberto Garcia bool ro = bdrv_is_read_only(to_replace); 69540365552SKevin Wolf 6961ba79388SAlberto Garcia if (ro != bdrv_is_read_only(target_bs)) { 6971ba79388SAlberto Garcia bdrv_reopen_set_read_only(target_bs, ro, NULL); 6985a7e7a0bSStefan Hajnoczi } 699b8804815SKevin Wolf 700b8804815SKevin Wolf /* The mirror job has no requests in flight any more, but we need to 701b8804815SKevin Wolf * drain potential other users of the BDS before changing the graph. */ 7025e771752SSergio Lopez assert(s->in_drain); 703e253f4b8SKevin Wolf bdrv_drained_begin(target_bs); 7046e9cc051SMax Reitz /* 7056e9cc051SMax Reitz * Cannot use check_to_replace_node() here, because that would 7066e9cc051SMax Reitz * check for an op blocker on @to_replace, and we have our own 7076e9cc051SMax Reitz * there. 7086e9cc051SMax Reitz */ 7096e9cc051SMax Reitz if (bdrv_recurse_can_replace(src, to_replace)) { 7105fe31c25SKevin Wolf bdrv_replace_node(to_replace, target_bs, &local_err); 7116e9cc051SMax Reitz } else { 7126e9cc051SMax Reitz error_setg(&local_err, "Can no longer replace '%s' by '%s', " 7136e9cc051SMax Reitz "because it can no longer be guaranteed that doing so " 7146e9cc051SMax Reitz "would not lead to an abrupt change of visible data", 7156e9cc051SMax Reitz to_replace->node_name, target_bs->node_name); 7166e9cc051SMax Reitz } 717e253f4b8SKevin Wolf bdrv_drained_end(target_bs); 7185fe31c25SKevin Wolf if (local_err) { 7195fe31c25SKevin Wolf error_report_err(local_err); 7207b508f6bSJohn Snow ret = -EPERM; 7215fe31c25SKevin Wolf } 7225a7e7a0bSStefan Hajnoczi } 7235a7e7a0bSStefan Hajnoczi if (s->to_replace) { 7245a7e7a0bSStefan Hajnoczi bdrv_op_unblock_all(s->to_replace, s->replace_blocker); 7255a7e7a0bSStefan Hajnoczi error_free(s->replace_blocker); 7265a7e7a0bSStefan Hajnoczi bdrv_unref(s->to_replace); 7275a7e7a0bSStefan Hajnoczi } 7285a7e7a0bSStefan Hajnoczi if (replace_aio_context) { 7295a7e7a0bSStefan Hajnoczi aio_context_release(replace_aio_context); 7305a7e7a0bSStefan Hajnoczi } 7315a7e7a0bSStefan Hajnoczi g_free(s->replaces); 7327d9fcb39SKevin Wolf bdrv_unref(target_bs); 7334ef85a9cSKevin Wolf 734f94dc3b4SMax Reitz /* 735f94dc3b4SMax Reitz * Remove the mirror filter driver from the graph. Before this, get rid of 7364ef85a9cSKevin Wolf * the blockers on the intermediate nodes so that the resulting state is 737f94dc3b4SMax Reitz * valid. 738f94dc3b4SMax Reitz */ 7391908a559SKevin Wolf block_job_remove_all_bdrv(bjob); 7405fe31c25SKevin Wolf bdrv_replace_node(mirror_top_bs, backing_bs(mirror_top_bs), &error_abort); 7414ef85a9cSKevin Wolf 7424ef85a9cSKevin Wolf /* We just changed the BDS the job BB refers to (with either or both of the 7435fe31c25SKevin Wolf * bdrv_replace_node() calls), so switch the BB back so the cleanup does 7445fe31c25SKevin Wolf * the right thing. We don't need any permissions any more now. */ 7451908a559SKevin Wolf blk_remove_bs(bjob->blk); 7461908a559SKevin Wolf blk_set_perm(bjob->blk, 0, BLK_PERM_ALL, &error_abort); 7471908a559SKevin Wolf blk_insert_bs(bjob->blk, mirror_top_bs, &error_abort); 7484ef85a9cSKevin Wolf 749429076e8SMax Reitz bs_opaque->job = NULL; 7504ef85a9cSKevin Wolf 751176c3699SFam Zheng bdrv_drained_end(src); 752d2da5e28SKevin Wolf bdrv_drained_end(mirror_top_bs); 7535e771752SSergio Lopez s->in_drain = false; 7544ef85a9cSKevin Wolf bdrv_unref(mirror_top_bs); 7553f09bfbcSKevin Wolf bdrv_unref(src); 7567b508f6bSJohn Snow 757737efc1eSJohn Snow return ret; 758737efc1eSJohn Snow } 759737efc1eSJohn Snow 760737efc1eSJohn Snow static int mirror_prepare(Job *job) 761737efc1eSJohn Snow { 762737efc1eSJohn Snow return mirror_exit_common(job); 763737efc1eSJohn Snow } 764737efc1eSJohn Snow 765737efc1eSJohn Snow static void mirror_abort(Job *job) 766737efc1eSJohn Snow { 767737efc1eSJohn Snow int ret = mirror_exit_common(job); 768737efc1eSJohn Snow assert(ret == 0); 7695a7e7a0bSStefan Hajnoczi } 7705a7e7a0bSStefan Hajnoczi 771537c3d4fSStefan Hajnoczi static void coroutine_fn mirror_throttle(MirrorBlockJob *s) 77249efb1f5SDenis V. Lunev { 77349efb1f5SDenis V. Lunev int64_t now = qemu_clock_get_ns(QEMU_CLOCK_REALTIME); 77449efb1f5SDenis V. Lunev 77518bb6928SKevin Wolf if (now - s->last_pause_ns > BLOCK_JOB_SLICE_TIME) { 77649efb1f5SDenis V. Lunev s->last_pause_ns = now; 7775d43e86eSKevin Wolf job_sleep_ns(&s->common.job, 0); 77849efb1f5SDenis V. Lunev } else { 779da01ff7fSKevin Wolf job_pause_point(&s->common.job); 78049efb1f5SDenis V. Lunev } 78149efb1f5SDenis V. Lunev } 78249efb1f5SDenis V. Lunev 783c0b363adSDenis V. Lunev static int coroutine_fn mirror_dirty_init(MirrorBlockJob *s) 784c0b363adSDenis V. Lunev { 78523ca459aSEric Blake int64_t offset; 786c0b363adSDenis V. Lunev BlockDriverState *base = s->base; 787138f9fffSMax Reitz BlockDriverState *bs = s->mirror_top_bs->backing->bs; 788c0b363adSDenis V. Lunev BlockDriverState *target_bs = blk_bs(s->target); 78923ca459aSEric Blake int ret; 79051b0a488SEric Blake int64_t count; 791c0b363adSDenis V. Lunev 792cdf3bc93SMax Reitz if (s->zero_target) { 793c7c2769cSDenis V. Lunev if (!bdrv_can_write_zeroes_with_unmap(target_bs)) { 794e0d7f73eSEric Blake bdrv_set_dirty_bitmap(s->dirty_bitmap, 0, s->bdev_length); 795b7d5062cSDenis V. Lunev return 0; 796b7d5062cSDenis V. Lunev } 797b7d5062cSDenis V. Lunev 79890ab48ebSAnton Nefedov s->initial_zeroing_ongoing = true; 79923ca459aSEric Blake for (offset = 0; offset < s->bdev_length; ) { 80023ca459aSEric Blake int bytes = MIN(s->bdev_length - offset, 80123ca459aSEric Blake QEMU_ALIGN_DOWN(INT_MAX, s->granularity)); 802c7c2769cSDenis V. Lunev 803c7c2769cSDenis V. Lunev mirror_throttle(s); 804c7c2769cSDenis V. Lunev 805daa7f2f9SKevin Wolf if (job_is_cancelled(&s->common.job)) { 80690ab48ebSAnton Nefedov s->initial_zeroing_ongoing = false; 807c7c2769cSDenis V. Lunev return 0; 808c7c2769cSDenis V. Lunev } 809c7c2769cSDenis V. Lunev 810c7c2769cSDenis V. Lunev if (s->in_flight >= MAX_IN_FLIGHT) { 81167adf4b3SEric Blake trace_mirror_yield(s, UINT64_MAX, s->buf_free_count, 81267adf4b3SEric Blake s->in_flight); 8137e6c4ff7SKevin Wolf mirror_wait_for_free_in_flight_slot(s, NULL); 814c7c2769cSDenis V. Lunev continue; 815c7c2769cSDenis V. Lunev } 816c7c2769cSDenis V. Lunev 8174295c5fcSMax Reitz mirror_perform(s, offset, bytes, MIRROR_METHOD_ZERO); 81823ca459aSEric Blake offset += bytes; 819c7c2769cSDenis V. Lunev } 820c7c2769cSDenis V. Lunev 821bae8196dSPaolo Bonzini mirror_wait_for_all_io(s); 82290ab48ebSAnton Nefedov s->initial_zeroing_ongoing = false; 823c7c2769cSDenis V. Lunev } 824c7c2769cSDenis V. Lunev 825c0b363adSDenis V. Lunev /* First part, loop on the sectors and initialize the dirty bitmap. */ 82623ca459aSEric Blake for (offset = 0; offset < s->bdev_length; ) { 827c0b363adSDenis V. Lunev /* Just to make sure we are not exceeding int limit. */ 82823ca459aSEric Blake int bytes = MIN(s->bdev_length - offset, 82923ca459aSEric Blake QEMU_ALIGN_DOWN(INT_MAX, s->granularity)); 830c0b363adSDenis V. Lunev 831c0b363adSDenis V. Lunev mirror_throttle(s); 832c0b363adSDenis V. Lunev 833daa7f2f9SKevin Wolf if (job_is_cancelled(&s->common.job)) { 834c0b363adSDenis V. Lunev return 0; 835c0b363adSDenis V. Lunev } 836c0b363adSDenis V. Lunev 837170d3bd3SAndrey Shinkevich ret = bdrv_is_allocated_above(bs, base, false, offset, bytes, &count); 838c0b363adSDenis V. Lunev if (ret < 0) { 839c0b363adSDenis V. Lunev return ret; 840c0b363adSDenis V. Lunev } 841c0b363adSDenis V. Lunev 84223ca459aSEric Blake assert(count); 843b7d5062cSDenis V. Lunev if (ret == 1) { 84423ca459aSEric Blake bdrv_set_dirty_bitmap(s->dirty_bitmap, offset, count); 845c0b363adSDenis V. Lunev } 84623ca459aSEric Blake offset += count; 847c0b363adSDenis V. Lunev } 848c0b363adSDenis V. Lunev return 0; 849c0b363adSDenis V. Lunev } 850c0b363adSDenis V. Lunev 851bdffb31dSPaolo Bonzini /* Called when going out of the streaming phase to flush the bulk of the 852bdffb31dSPaolo Bonzini * data to the medium, or just before completing. 853bdffb31dSPaolo Bonzini */ 854bdffb31dSPaolo Bonzini static int mirror_flush(MirrorBlockJob *s) 855bdffb31dSPaolo Bonzini { 856bdffb31dSPaolo Bonzini int ret = blk_flush(s->target); 857bdffb31dSPaolo Bonzini if (ret < 0) { 858bdffb31dSPaolo Bonzini if (mirror_error_action(s, false, -ret) == BLOCK_ERROR_ACTION_REPORT) { 859bdffb31dSPaolo Bonzini s->ret = ret; 860bdffb31dSPaolo Bonzini } 861bdffb31dSPaolo Bonzini } 862bdffb31dSPaolo Bonzini return ret; 863bdffb31dSPaolo Bonzini } 864bdffb31dSPaolo Bonzini 865f67432a2SJohn Snow static int coroutine_fn mirror_run(Job *job, Error **errp) 866893f7ebaSPaolo Bonzini { 867f67432a2SJohn Snow MirrorBlockJob *s = container_of(job, MirrorBlockJob, common.job); 868138f9fffSMax Reitz BlockDriverState *bs = s->mirror_top_bs->backing->bs; 869e253f4b8SKevin Wolf BlockDriverState *target_bs = blk_bs(s->target); 8709a0cec66SPaolo Bonzini bool need_drain = true; 871c0b363adSDenis V. Lunev int64_t length; 872b812f671SPaolo Bonzini BlockDriverInfo bdi; 8731d33936eSJeff Cody char backing_filename[2]; /* we only need 2 characters because we are only 8741d33936eSJeff Cody checking for a NULL string */ 875893f7ebaSPaolo Bonzini int ret = 0; 876893f7ebaSPaolo Bonzini 877daa7f2f9SKevin Wolf if (job_is_cancelled(&s->common.job)) { 878893f7ebaSPaolo Bonzini goto immediate_exit; 879893f7ebaSPaolo Bonzini } 880893f7ebaSPaolo Bonzini 881b21c7652SMax Reitz s->bdev_length = bdrv_getlength(bs); 882b21c7652SMax Reitz if (s->bdev_length < 0) { 883b21c7652SMax Reitz ret = s->bdev_length; 884373df5b1SFam Zheng goto immediate_exit; 885becc347eSKevin Wolf } 886becc347eSKevin Wolf 887becc347eSKevin Wolf /* Active commit must resize the base image if its size differs from the 888becc347eSKevin Wolf * active layer. */ 889becc347eSKevin Wolf if (s->base == blk_bs(s->target)) { 890becc347eSKevin Wolf int64_t base_length; 891becc347eSKevin Wolf 892becc347eSKevin Wolf base_length = blk_getlength(s->target); 893becc347eSKevin Wolf if (base_length < 0) { 894becc347eSKevin Wolf ret = base_length; 895becc347eSKevin Wolf goto immediate_exit; 896becc347eSKevin Wolf } 897becc347eSKevin Wolf 898becc347eSKevin Wolf if (s->bdev_length > base_length) { 899c80d8b06SMax Reitz ret = blk_truncate(s->target, s->bdev_length, false, 900c80d8b06SMax Reitz PREALLOC_MODE_OFF, NULL); 901becc347eSKevin Wolf if (ret < 0) { 902becc347eSKevin Wolf goto immediate_exit; 903becc347eSKevin Wolf } 904becc347eSKevin Wolf } 905becc347eSKevin Wolf } 906becc347eSKevin Wolf 907becc347eSKevin Wolf if (s->bdev_length == 0) { 9082e1795b5SKevin Wolf /* Transition to the READY state and wait for complete. */ 9092e1795b5SKevin Wolf job_transition_to_ready(&s->common.job); 9109e48b025SFam Zheng s->synced = true; 911d06107adSMax Reitz s->actively_synced = true; 912daa7f2f9SKevin Wolf while (!job_is_cancelled(&s->common.job) && !s->should_complete) { 913198c49ccSKevin Wolf job_yield(&s->common.job); 9149e48b025SFam Zheng } 915daa7f2f9SKevin Wolf s->common.job.cancelled = false; 9169e48b025SFam Zheng goto immediate_exit; 917893f7ebaSPaolo Bonzini } 918893f7ebaSPaolo Bonzini 919b21c7652SMax Reitz length = DIV_ROUND_UP(s->bdev_length, s->granularity); 920402a4741SPaolo Bonzini s->in_flight_bitmap = bitmap_new(length); 921402a4741SPaolo Bonzini 922b812f671SPaolo Bonzini /* If we have no backing file yet in the destination, we cannot let 923b812f671SPaolo Bonzini * the destination do COW. Instead, we copy sectors around the 924b812f671SPaolo Bonzini * dirty data if needed. We need a bitmap to do that. 925b812f671SPaolo Bonzini */ 926e253f4b8SKevin Wolf bdrv_get_backing_filename(target_bs, backing_filename, 927b812f671SPaolo Bonzini sizeof(backing_filename)); 928e253f4b8SKevin Wolf if (!bdrv_get_info(target_bs, &bdi) && bdi.cluster_size) { 929b436982fSEric Blake s->target_cluster_size = bdi.cluster_size; 930b436982fSEric Blake } else { 931b436982fSEric Blake s->target_cluster_size = BDRV_SECTOR_SIZE; 932c3cc95bdSFam Zheng } 933b436982fSEric Blake if (backing_filename[0] && !target_bs->backing && 934b436982fSEric Blake s->granularity < s->target_cluster_size) { 935b436982fSEric Blake s->buf_size = MAX(s->buf_size, s->target_cluster_size); 936b812f671SPaolo Bonzini s->cow_bitmap = bitmap_new(length); 937b812f671SPaolo Bonzini } 938e253f4b8SKevin Wolf s->max_iov = MIN(bs->bl.max_iov, target_bs->bl.max_iov); 939b812f671SPaolo Bonzini 9407504edf4SKevin Wolf s->buf = qemu_try_blockalign(bs, s->buf_size); 9417504edf4SKevin Wolf if (s->buf == NULL) { 9427504edf4SKevin Wolf ret = -ENOMEM; 9437504edf4SKevin Wolf goto immediate_exit; 9447504edf4SKevin Wolf } 9457504edf4SKevin Wolf 946402a4741SPaolo Bonzini mirror_free_init(s); 947893f7ebaSPaolo Bonzini 94849efb1f5SDenis V. Lunev s->last_pause_ns = qemu_clock_get_ns(QEMU_CLOCK_REALTIME); 94903544a6eSFam Zheng if (!s->is_none_mode) { 950c0b363adSDenis V. Lunev ret = mirror_dirty_init(s); 951daa7f2f9SKevin Wolf if (ret < 0 || job_is_cancelled(&s->common.job)) { 9524c0cbd6fSFam Zheng goto immediate_exit; 9534c0cbd6fSFam Zheng } 954893f7ebaSPaolo Bonzini } 955893f7ebaSPaolo Bonzini 956dc162c8eSFam Zheng assert(!s->dbi); 957715a74d8SEric Blake s->dbi = bdrv_dirty_iter_new(s->dirty_bitmap); 958893f7ebaSPaolo Bonzini for (;;) { 959cc8c9d6cSPaolo Bonzini uint64_t delay_ns = 0; 96049efb1f5SDenis V. Lunev int64_t cnt, delta; 961893f7ebaSPaolo Bonzini bool should_complete; 962893f7ebaSPaolo Bonzini 963d06107adSMax Reitz /* Do not start passive operations while there are active 964d06107adSMax Reitz * writes in progress */ 965d06107adSMax Reitz while (s->in_active_write_counter) { 9667e6c4ff7SKevin Wolf mirror_wait_for_any_operation(s, NULL, true); 967d06107adSMax Reitz } 968d06107adSMax Reitz 969bd48bde8SPaolo Bonzini if (s->ret < 0) { 970bd48bde8SPaolo Bonzini ret = s->ret; 971893f7ebaSPaolo Bonzini goto immediate_exit; 972893f7ebaSPaolo Bonzini } 973bd48bde8SPaolo Bonzini 974da01ff7fSKevin Wolf job_pause_point(&s->common.job); 975565ac01fSStefan Hajnoczi 97620dca810SJohn Snow cnt = bdrv_get_dirty_count(s->dirty_bitmap); 97705df8a6aSKevin Wolf /* cnt is the number of dirty bytes remaining and s->bytes_in_flight is 97805df8a6aSKevin Wolf * the number of bytes currently being processed; together those are 97905df8a6aSKevin Wolf * the current remaining operation length */ 98030a5c887SKevin Wolf job_progress_set_remaining(&s->common.job, s->bytes_in_flight + cnt); 981bd48bde8SPaolo Bonzini 982bd48bde8SPaolo Bonzini /* Note that even when no rate limit is applied we need to yield 983a7282330SFam Zheng * periodically with no pending I/O so that bdrv_drain_all() returns. 98418bb6928SKevin Wolf * We do so every BLKOCK_JOB_SLICE_TIME nanoseconds, or when there is 98518bb6928SKevin Wolf * an error, or when the source is clean, whichever comes first. */ 98649efb1f5SDenis V. Lunev delta = qemu_clock_get_ns(QEMU_CLOCK_REALTIME) - s->last_pause_ns; 98718bb6928SKevin Wolf if (delta < BLOCK_JOB_SLICE_TIME && 988bd48bde8SPaolo Bonzini s->common.iostatus == BLOCK_DEVICE_IO_STATUS_OK) { 989cf56a3c6SDenis V. Lunev if (s->in_flight >= MAX_IN_FLIGHT || s->buf_free_count == 0 || 990402a4741SPaolo Bonzini (cnt == 0 && s->in_flight > 0)) { 9919a46dba7SEric Blake trace_mirror_yield(s, cnt, s->buf_free_count, s->in_flight); 9927e6c4ff7SKevin Wolf mirror_wait_for_free_in_flight_slot(s, NULL); 993bd48bde8SPaolo Bonzini continue; 994bd48bde8SPaolo Bonzini } else if (cnt != 0) { 995cc8c9d6cSPaolo Bonzini delay_ns = mirror_iteration(s); 996893f7ebaSPaolo Bonzini } 997cc8c9d6cSPaolo Bonzini } 998893f7ebaSPaolo Bonzini 999893f7ebaSPaolo Bonzini should_complete = false; 1000bd48bde8SPaolo Bonzini if (s->in_flight == 0 && cnt == 0) { 1001893f7ebaSPaolo Bonzini trace_mirror_before_flush(s); 1002bdffb31dSPaolo Bonzini if (!s->synced) { 1003bdffb31dSPaolo Bonzini if (mirror_flush(s) < 0) { 1004bdffb31dSPaolo Bonzini /* Go check s->ret. */ 1005bdffb31dSPaolo Bonzini continue; 1006893f7ebaSPaolo Bonzini } 1007893f7ebaSPaolo Bonzini /* We're out of the streaming phase. From now on, if the job 1008893f7ebaSPaolo Bonzini * is cancelled we will actually complete all pending I/O and 1009893f7ebaSPaolo Bonzini * report completion. This way, block-job-cancel will leave 1010893f7ebaSPaolo Bonzini * the target in a consistent state. 1011893f7ebaSPaolo Bonzini */ 10122e1795b5SKevin Wolf job_transition_to_ready(&s->common.job); 1013d63ffd87SPaolo Bonzini s->synced = true; 1014d06107adSMax Reitz if (s->copy_mode != MIRROR_COPY_MODE_BACKGROUND) { 1015d06107adSMax Reitz s->actively_synced = true; 1016d06107adSMax Reitz } 1017d63ffd87SPaolo Bonzini } 1018d63ffd87SPaolo Bonzini 1019d63ffd87SPaolo Bonzini should_complete = s->should_complete || 1020daa7f2f9SKevin Wolf job_is_cancelled(&s->common.job); 102120dca810SJohn Snow cnt = bdrv_get_dirty_count(s->dirty_bitmap); 1022893f7ebaSPaolo Bonzini } 1023893f7ebaSPaolo Bonzini 1024893f7ebaSPaolo Bonzini if (cnt == 0 && should_complete) { 1025893f7ebaSPaolo Bonzini /* The dirty bitmap is not updated while operations are pending. 1026893f7ebaSPaolo Bonzini * If we're about to exit, wait for pending operations before 1027893f7ebaSPaolo Bonzini * calling bdrv_get_dirty_count(bs), or we may exit while the 1028893f7ebaSPaolo Bonzini * source has dirty data to copy! 1029893f7ebaSPaolo Bonzini * 1030893f7ebaSPaolo Bonzini * Note that I/O can be submitted by the guest while 10319a0cec66SPaolo Bonzini * mirror_populate runs, so pause it now. Before deciding 10329a0cec66SPaolo Bonzini * whether to switch to target check one last time if I/O has 10339a0cec66SPaolo Bonzini * come in the meanwhile, and if not flush the data to disk. 1034893f7ebaSPaolo Bonzini */ 10359a46dba7SEric Blake trace_mirror_before_drain(s, cnt); 10369a0cec66SPaolo Bonzini 10375e771752SSergio Lopez s->in_drain = true; 10389a0cec66SPaolo Bonzini bdrv_drained_begin(bs); 103920dca810SJohn Snow cnt = bdrv_get_dirty_count(s->dirty_bitmap); 1040bdffb31dSPaolo Bonzini if (cnt > 0 || mirror_flush(s) < 0) { 10419a0cec66SPaolo Bonzini bdrv_drained_end(bs); 10425e771752SSergio Lopez s->in_drain = false; 10439a0cec66SPaolo Bonzini continue; 10449a0cec66SPaolo Bonzini } 10459a0cec66SPaolo Bonzini 10469a0cec66SPaolo Bonzini /* The two disks are in sync. Exit and report successful 10479a0cec66SPaolo Bonzini * completion. 10489a0cec66SPaolo Bonzini */ 10499a0cec66SPaolo Bonzini assert(QLIST_EMPTY(&bs->tracked_requests)); 1050daa7f2f9SKevin Wolf s->common.job.cancelled = false; 10519a0cec66SPaolo Bonzini need_drain = false; 10529a0cec66SPaolo Bonzini break; 1053893f7ebaSPaolo Bonzini } 1054893f7ebaSPaolo Bonzini 1055893f7ebaSPaolo Bonzini ret = 0; 1056ddc4115eSStefan Hajnoczi 1057ddc4115eSStefan Hajnoczi if (s->synced && !should_complete) { 105818bb6928SKevin Wolf delay_ns = (s->in_flight == 0 && 105918bb6928SKevin Wolf cnt == 0 ? BLOCK_JOB_SLICE_TIME : 0); 1060ddc4115eSStefan Hajnoczi } 10619a46dba7SEric Blake trace_mirror_before_sleep(s, cnt, s->synced, delay_ns); 10625d43e86eSKevin Wolf job_sleep_ns(&s->common.job, delay_ns); 1063daa7f2f9SKevin Wolf if (job_is_cancelled(&s->common.job) && 1064004e95dfSKevin Wolf (!s->synced || s->common.job.force_cancel)) 1065eb36639fSMax Reitz { 1066893f7ebaSPaolo Bonzini break; 1067893f7ebaSPaolo Bonzini } 106849efb1f5SDenis V. Lunev s->last_pause_ns = qemu_clock_get_ns(QEMU_CLOCK_REALTIME); 1069893f7ebaSPaolo Bonzini } 1070893f7ebaSPaolo Bonzini 1071893f7ebaSPaolo Bonzini immediate_exit: 1072bd48bde8SPaolo Bonzini if (s->in_flight > 0) { 1073bd48bde8SPaolo Bonzini /* We get here only if something went wrong. Either the job failed, 1074bd48bde8SPaolo Bonzini * or it was cancelled prematurely so that we do not guarantee that 1075bd48bde8SPaolo Bonzini * the target is a copy of the source. 1076bd48bde8SPaolo Bonzini */ 1077004e95dfSKevin Wolf assert(ret < 0 || ((s->common.job.force_cancel || !s->synced) && 1078daa7f2f9SKevin Wolf job_is_cancelled(&s->common.job))); 10799a0cec66SPaolo Bonzini assert(need_drain); 1080bae8196dSPaolo Bonzini mirror_wait_for_all_io(s); 1081bd48bde8SPaolo Bonzini } 1082bd48bde8SPaolo Bonzini 1083bd48bde8SPaolo Bonzini assert(s->in_flight == 0); 10847191bf31SMarkus Armbruster qemu_vfree(s->buf); 1085b812f671SPaolo Bonzini g_free(s->cow_bitmap); 1086402a4741SPaolo Bonzini g_free(s->in_flight_bitmap); 1087dc162c8eSFam Zheng bdrv_dirty_iter_free(s->dbi); 10885a7e7a0bSStefan Hajnoczi 10899a0cec66SPaolo Bonzini if (need_drain) { 10905e771752SSergio Lopez s->in_drain = true; 1091e253f4b8SKevin Wolf bdrv_drained_begin(bs); 10929a0cec66SPaolo Bonzini } 1093f67432a2SJohn Snow 1094f67432a2SJohn Snow return ret; 1095893f7ebaSPaolo Bonzini } 1096893f7ebaSPaolo Bonzini 10973453d972SKevin Wolf static void mirror_complete(Job *job, Error **errp) 1098d63ffd87SPaolo Bonzini { 10993453d972SKevin Wolf MirrorBlockJob *s = container_of(job, MirrorBlockJob, common.job); 11004ef85a9cSKevin Wolf BlockDriverState *target; 1101d63ffd87SPaolo Bonzini 1102274fcceeSMax Reitz target = blk_bs(s->target); 1103274fcceeSMax Reitz 1104d63ffd87SPaolo Bonzini if (!s->synced) { 11059df229c3SAlberto Garcia error_setg(errp, "The active block job '%s' cannot be completed", 11063453d972SKevin Wolf job->id); 1107d63ffd87SPaolo Bonzini return; 1108d63ffd87SPaolo Bonzini } 1109d63ffd87SPaolo Bonzini 1110274fcceeSMax Reitz if (s->backing_mode == MIRROR_OPEN_BACKING_CHAIN) { 1111274fcceeSMax Reitz int ret; 1112274fcceeSMax Reitz 1113274fcceeSMax Reitz assert(!target->backing); 1114274fcceeSMax Reitz ret = bdrv_open_backing_file(target, NULL, "backing", errp); 1115274fcceeSMax Reitz if (ret < 0) { 1116274fcceeSMax Reitz return; 1117274fcceeSMax Reitz } 1118274fcceeSMax Reitz } 1119274fcceeSMax Reitz 112015d67298SChanglong Xie /* block all operations on to_replace bs */ 112109158f00SBenoît Canet if (s->replaces) { 11225a7e7a0bSStefan Hajnoczi AioContext *replace_aio_context; 11235a7e7a0bSStefan Hajnoczi 1124e12f3784SWen Congyang s->to_replace = bdrv_find_node(s->replaces); 112509158f00SBenoît Canet if (!s->to_replace) { 1126e12f3784SWen Congyang error_setg(errp, "Node name '%s' not found", s->replaces); 112709158f00SBenoît Canet return; 112809158f00SBenoît Canet } 112909158f00SBenoît Canet 11305a7e7a0bSStefan Hajnoczi replace_aio_context = bdrv_get_aio_context(s->to_replace); 11315a7e7a0bSStefan Hajnoczi aio_context_acquire(replace_aio_context); 11325a7e7a0bSStefan Hajnoczi 11334ef85a9cSKevin Wolf /* TODO Translate this into permission system. Current definition of 11344ef85a9cSKevin Wolf * GRAPH_MOD would require to request it for the parents; they might 11354ef85a9cSKevin Wolf * not even be BlockDriverStates, however, so a BdrvChild can't address 11364ef85a9cSKevin Wolf * them. May need redefinition of GRAPH_MOD. */ 113709158f00SBenoît Canet error_setg(&s->replace_blocker, 113809158f00SBenoît Canet "block device is in use by block-job-complete"); 113909158f00SBenoît Canet bdrv_op_block_all(s->to_replace, s->replace_blocker); 114009158f00SBenoît Canet bdrv_ref(s->to_replace); 11415a7e7a0bSStefan Hajnoczi 11425a7e7a0bSStefan Hajnoczi aio_context_release(replace_aio_context); 114309158f00SBenoît Canet } 114409158f00SBenoît Canet 1145d63ffd87SPaolo Bonzini s->should_complete = true; 11463d70ff53SKevin Wolf job_enter(job); 1147d63ffd87SPaolo Bonzini } 1148d63ffd87SPaolo Bonzini 1149537c3d4fSStefan Hajnoczi static void coroutine_fn mirror_pause(Job *job) 1150565ac01fSStefan Hajnoczi { 1151da01ff7fSKevin Wolf MirrorBlockJob *s = container_of(job, MirrorBlockJob, common.job); 1152565ac01fSStefan Hajnoczi 1153bae8196dSPaolo Bonzini mirror_wait_for_all_io(s); 1154565ac01fSStefan Hajnoczi } 1155565ac01fSStefan Hajnoczi 115689bd0305SKevin Wolf static bool mirror_drained_poll(BlockJob *job) 115789bd0305SKevin Wolf { 115889bd0305SKevin Wolf MirrorBlockJob *s = container_of(job, MirrorBlockJob, common); 11595e771752SSergio Lopez 11605e771752SSergio Lopez /* If the job isn't paused nor cancelled, we can't be sure that it won't 11615e771752SSergio Lopez * issue more requests. We make an exception if we've reached this point 11625e771752SSergio Lopez * from one of our own drain sections, to avoid a deadlock waiting for 11635e771752SSergio Lopez * ourselves. 11645e771752SSergio Lopez */ 11655e771752SSergio Lopez if (!s->common.job.paused && !s->common.job.cancelled && !s->in_drain) { 11665e771752SSergio Lopez return true; 11675e771752SSergio Lopez } 11685e771752SSergio Lopez 116989bd0305SKevin Wolf return !!s->in_flight; 117089bd0305SKevin Wolf } 117189bd0305SKevin Wolf 11723fc4b10aSFam Zheng static const BlockJobDriver mirror_job_driver = { 117333e9e9bdSKevin Wolf .job_driver = { 1174893f7ebaSPaolo Bonzini .instance_size = sizeof(MirrorBlockJob), 11758e4c8700SKevin Wolf .job_type = JOB_TYPE_MIRROR, 117680fa2c75SKevin Wolf .free = block_job_free, 1177b15de828SKevin Wolf .user_resume = block_job_user_resume, 1178f67432a2SJohn Snow .run = mirror_run, 1179737efc1eSJohn Snow .prepare = mirror_prepare, 1180737efc1eSJohn Snow .abort = mirror_abort, 1181565ac01fSStefan Hajnoczi .pause = mirror_pause, 1182da01ff7fSKevin Wolf .complete = mirror_complete, 11833453d972SKevin Wolf }, 118489bd0305SKevin Wolf .drained_poll = mirror_drained_poll, 1185893f7ebaSPaolo Bonzini }; 1186893f7ebaSPaolo Bonzini 118703544a6eSFam Zheng static const BlockJobDriver commit_active_job_driver = { 118833e9e9bdSKevin Wolf .job_driver = { 118903544a6eSFam Zheng .instance_size = sizeof(MirrorBlockJob), 11908e4c8700SKevin Wolf .job_type = JOB_TYPE_COMMIT, 119180fa2c75SKevin Wolf .free = block_job_free, 1192b15de828SKevin Wolf .user_resume = block_job_user_resume, 1193f67432a2SJohn Snow .run = mirror_run, 1194737efc1eSJohn Snow .prepare = mirror_prepare, 1195737efc1eSJohn Snow .abort = mirror_abort, 1196565ac01fSStefan Hajnoczi .pause = mirror_pause, 1197da01ff7fSKevin Wolf .complete = mirror_complete, 11983453d972SKevin Wolf }, 119989bd0305SKevin Wolf .drained_poll = mirror_drained_poll, 120003544a6eSFam Zheng }; 120103544a6eSFam Zheng 1202537c3d4fSStefan Hajnoczi static void coroutine_fn 1203537c3d4fSStefan Hajnoczi do_sync_target_write(MirrorBlockJob *job, MirrorMethod method, 1204d06107adSMax Reitz uint64_t offset, uint64_t bytes, 1205d06107adSMax Reitz QEMUIOVector *qiov, int flags) 1206d06107adSMax Reitz { 1207d06107adSMax Reitz int ret; 1208dbdf699cSVladimir Sementsov-Ogievskiy size_t qiov_offset = 0; 1209dbdf699cSVladimir Sementsov-Ogievskiy int64_t bitmap_offset, bitmap_end; 1210d06107adSMax Reitz 1211dbdf699cSVladimir Sementsov-Ogievskiy if (!QEMU_IS_ALIGNED(offset, job->granularity) && 1212dbdf699cSVladimir Sementsov-Ogievskiy bdrv_dirty_bitmap_get(job->dirty_bitmap, offset)) 1213dbdf699cSVladimir Sementsov-Ogievskiy { 1214dbdf699cSVladimir Sementsov-Ogievskiy /* 1215dbdf699cSVladimir Sementsov-Ogievskiy * Dirty unaligned padding: ignore it. 1216dbdf699cSVladimir Sementsov-Ogievskiy * 1217dbdf699cSVladimir Sementsov-Ogievskiy * Reasoning: 1218dbdf699cSVladimir Sementsov-Ogievskiy * 1. If we copy it, we can't reset corresponding bit in 1219dbdf699cSVladimir Sementsov-Ogievskiy * dirty_bitmap as there may be some "dirty" bytes still not 1220dbdf699cSVladimir Sementsov-Ogievskiy * copied. 1221dbdf699cSVladimir Sementsov-Ogievskiy * 2. It's already dirty, so skipping it we don't diverge mirror 1222dbdf699cSVladimir Sementsov-Ogievskiy * progress. 1223dbdf699cSVladimir Sementsov-Ogievskiy * 1224dbdf699cSVladimir Sementsov-Ogievskiy * Note, that because of this, guest write may have no contribution 1225dbdf699cSVladimir Sementsov-Ogievskiy * into mirror converge, but that's not bad, as we have background 1226dbdf699cSVladimir Sementsov-Ogievskiy * process of mirroring. If under some bad circumstances (high guest 1227dbdf699cSVladimir Sementsov-Ogievskiy * IO load) background process starve, we will not converge anyway, 1228dbdf699cSVladimir Sementsov-Ogievskiy * even if each write will contribute, as guest is not guaranteed to 1229dbdf699cSVladimir Sementsov-Ogievskiy * rewrite the whole disk. 1230dbdf699cSVladimir Sementsov-Ogievskiy */ 1231dbdf699cSVladimir Sementsov-Ogievskiy qiov_offset = QEMU_ALIGN_UP(offset, job->granularity) - offset; 1232dbdf699cSVladimir Sementsov-Ogievskiy if (bytes <= qiov_offset) { 1233dbdf699cSVladimir Sementsov-Ogievskiy /* nothing to do after shrink */ 1234dbdf699cSVladimir Sementsov-Ogievskiy return; 1235dbdf699cSVladimir Sementsov-Ogievskiy } 1236dbdf699cSVladimir Sementsov-Ogievskiy offset += qiov_offset; 1237dbdf699cSVladimir Sementsov-Ogievskiy bytes -= qiov_offset; 1238dbdf699cSVladimir Sementsov-Ogievskiy } 1239dbdf699cSVladimir Sementsov-Ogievskiy 1240dbdf699cSVladimir Sementsov-Ogievskiy if (!QEMU_IS_ALIGNED(offset + bytes, job->granularity) && 1241dbdf699cSVladimir Sementsov-Ogievskiy bdrv_dirty_bitmap_get(job->dirty_bitmap, offset + bytes - 1)) 1242dbdf699cSVladimir Sementsov-Ogievskiy { 1243dbdf699cSVladimir Sementsov-Ogievskiy uint64_t tail = (offset + bytes) % job->granularity; 1244dbdf699cSVladimir Sementsov-Ogievskiy 1245dbdf699cSVladimir Sementsov-Ogievskiy if (bytes <= tail) { 1246dbdf699cSVladimir Sementsov-Ogievskiy /* nothing to do after shrink */ 1247dbdf699cSVladimir Sementsov-Ogievskiy return; 1248dbdf699cSVladimir Sementsov-Ogievskiy } 1249dbdf699cSVladimir Sementsov-Ogievskiy bytes -= tail; 1250dbdf699cSVladimir Sementsov-Ogievskiy } 1251dbdf699cSVladimir Sementsov-Ogievskiy 1252dbdf699cSVladimir Sementsov-Ogievskiy /* 1253dbdf699cSVladimir Sementsov-Ogievskiy * Tails are either clean or shrunk, so for bitmap resetting 1254dbdf699cSVladimir Sementsov-Ogievskiy * we safely align the range down. 1255dbdf699cSVladimir Sementsov-Ogievskiy */ 1256dbdf699cSVladimir Sementsov-Ogievskiy bitmap_offset = QEMU_ALIGN_UP(offset, job->granularity); 1257dbdf699cSVladimir Sementsov-Ogievskiy bitmap_end = QEMU_ALIGN_DOWN(offset + bytes, job->granularity); 1258dbdf699cSVladimir Sementsov-Ogievskiy if (bitmap_offset < bitmap_end) { 1259dbdf699cSVladimir Sementsov-Ogievskiy bdrv_reset_dirty_bitmap(job->dirty_bitmap, bitmap_offset, 1260dbdf699cSVladimir Sementsov-Ogievskiy bitmap_end - bitmap_offset); 1261dbdf699cSVladimir Sementsov-Ogievskiy } 1262d06107adSMax Reitz 12635c511ac3SVladimir Sementsov-Ogievskiy job_progress_increase_remaining(&job->common.job, bytes); 1264d06107adSMax Reitz 1265d06107adSMax Reitz switch (method) { 1266d06107adSMax Reitz case MIRROR_METHOD_COPY: 1267dbdf699cSVladimir Sementsov-Ogievskiy ret = blk_co_pwritev_part(job->target, offset, bytes, 1268dbdf699cSVladimir Sementsov-Ogievskiy qiov, qiov_offset, flags); 1269d06107adSMax Reitz break; 1270d06107adSMax Reitz 1271d06107adSMax Reitz case MIRROR_METHOD_ZERO: 1272d06107adSMax Reitz assert(!qiov); 12735c511ac3SVladimir Sementsov-Ogievskiy ret = blk_co_pwrite_zeroes(job->target, offset, bytes, flags); 1274d06107adSMax Reitz break; 1275d06107adSMax Reitz 1276d06107adSMax Reitz case MIRROR_METHOD_DISCARD: 1277d06107adSMax Reitz assert(!qiov); 12785c511ac3SVladimir Sementsov-Ogievskiy ret = blk_co_pdiscard(job->target, offset, bytes); 1279d06107adSMax Reitz break; 1280d06107adSMax Reitz 1281d06107adSMax Reitz default: 1282d06107adSMax Reitz abort(); 1283d06107adSMax Reitz } 1284d06107adSMax Reitz 1285d06107adSMax Reitz if (ret >= 0) { 12865c511ac3SVladimir Sementsov-Ogievskiy job_progress_update(&job->common.job, bytes); 1287d06107adSMax Reitz } else { 1288d06107adSMax Reitz BlockErrorAction action; 1289d06107adSMax Reitz 1290dbdf699cSVladimir Sementsov-Ogievskiy /* 1291dbdf699cSVladimir Sementsov-Ogievskiy * We failed, so we should mark dirty the whole area, aligned up. 1292dbdf699cSVladimir Sementsov-Ogievskiy * Note that we don't care about shrunk tails if any: they were dirty 1293dbdf699cSVladimir Sementsov-Ogievskiy * at function start, and they must be still dirty, as we've locked 1294dbdf699cSVladimir Sementsov-Ogievskiy * the region for in-flight op. 1295dbdf699cSVladimir Sementsov-Ogievskiy */ 1296dbdf699cSVladimir Sementsov-Ogievskiy bitmap_offset = QEMU_ALIGN_DOWN(offset, job->granularity); 1297dbdf699cSVladimir Sementsov-Ogievskiy bitmap_end = QEMU_ALIGN_UP(offset + bytes, job->granularity); 1298dbdf699cSVladimir Sementsov-Ogievskiy bdrv_set_dirty_bitmap(job->dirty_bitmap, bitmap_offset, 1299dbdf699cSVladimir Sementsov-Ogievskiy bitmap_end - bitmap_offset); 1300d06107adSMax Reitz job->actively_synced = false; 1301d06107adSMax Reitz 1302d06107adSMax Reitz action = mirror_error_action(job, false, -ret); 1303d06107adSMax Reitz if (action == BLOCK_ERROR_ACTION_REPORT) { 1304d06107adSMax Reitz if (!job->ret) { 1305d06107adSMax Reitz job->ret = ret; 1306d06107adSMax Reitz } 1307d06107adSMax Reitz } 1308d06107adSMax Reitz } 1309d06107adSMax Reitz } 1310d06107adSMax Reitz 1311d06107adSMax Reitz static MirrorOp *coroutine_fn active_write_prepare(MirrorBlockJob *s, 1312d06107adSMax Reitz uint64_t offset, 1313d06107adSMax Reitz uint64_t bytes) 1314d06107adSMax Reitz { 1315d06107adSMax Reitz MirrorOp *op; 1316d06107adSMax Reitz uint64_t start_chunk = offset / s->granularity; 1317d06107adSMax Reitz uint64_t end_chunk = DIV_ROUND_UP(offset + bytes, s->granularity); 1318d06107adSMax Reitz 1319d06107adSMax Reitz op = g_new(MirrorOp, 1); 1320d06107adSMax Reitz *op = (MirrorOp){ 1321d06107adSMax Reitz .s = s, 1322d06107adSMax Reitz .offset = offset, 1323d06107adSMax Reitz .bytes = bytes, 1324d06107adSMax Reitz .is_active_write = true, 1325d06107adSMax Reitz }; 1326d06107adSMax Reitz qemu_co_queue_init(&op->waiting_requests); 1327d06107adSMax Reitz QTAILQ_INSERT_TAIL(&s->ops_in_flight, op, next); 1328d06107adSMax Reitz 1329d06107adSMax Reitz s->in_active_write_counter++; 1330d06107adSMax Reitz 1331d06107adSMax Reitz mirror_wait_on_conflicts(op, s, offset, bytes); 1332d06107adSMax Reitz 1333d06107adSMax Reitz bitmap_set(s->in_flight_bitmap, start_chunk, end_chunk - start_chunk); 1334d06107adSMax Reitz 1335d06107adSMax Reitz return op; 1336d06107adSMax Reitz } 1337d06107adSMax Reitz 1338d06107adSMax Reitz static void coroutine_fn active_write_settle(MirrorOp *op) 1339d06107adSMax Reitz { 1340d06107adSMax Reitz uint64_t start_chunk = op->offset / op->s->granularity; 1341d06107adSMax Reitz uint64_t end_chunk = DIV_ROUND_UP(op->offset + op->bytes, 1342d06107adSMax Reitz op->s->granularity); 1343d06107adSMax Reitz 1344d06107adSMax Reitz if (!--op->s->in_active_write_counter && op->s->actively_synced) { 1345d06107adSMax Reitz BdrvChild *source = op->s->mirror_top_bs->backing; 1346d06107adSMax Reitz 1347d06107adSMax Reitz if (QLIST_FIRST(&source->bs->parents) == source && 1348d06107adSMax Reitz QLIST_NEXT(source, next_parent) == NULL) 1349d06107adSMax Reitz { 1350d06107adSMax Reitz /* Assert that we are back in sync once all active write 1351d06107adSMax Reitz * operations are settled. 1352d06107adSMax Reitz * Note that we can only assert this if the mirror node 1353d06107adSMax Reitz * is the source node's only parent. */ 1354d06107adSMax Reitz assert(!bdrv_get_dirty_count(op->s->dirty_bitmap)); 1355d06107adSMax Reitz } 1356d06107adSMax Reitz } 1357d06107adSMax Reitz bitmap_clear(op->s->in_flight_bitmap, start_chunk, end_chunk - start_chunk); 1358d06107adSMax Reitz QTAILQ_REMOVE(&op->s->ops_in_flight, op, next); 1359d06107adSMax Reitz qemu_co_queue_restart_all(&op->waiting_requests); 1360d06107adSMax Reitz g_free(op); 1361d06107adSMax Reitz } 1362d06107adSMax Reitz 13634ef85a9cSKevin Wolf static int coroutine_fn bdrv_mirror_top_preadv(BlockDriverState *bs, 13644ef85a9cSKevin Wolf uint64_t offset, uint64_t bytes, QEMUIOVector *qiov, int flags) 13654ef85a9cSKevin Wolf { 13664ef85a9cSKevin Wolf return bdrv_co_preadv(bs->backing, offset, bytes, qiov, flags); 13674ef85a9cSKevin Wolf } 13684ef85a9cSKevin Wolf 1369d06107adSMax Reitz static int coroutine_fn bdrv_mirror_top_do_write(BlockDriverState *bs, 1370d06107adSMax Reitz MirrorMethod method, uint64_t offset, uint64_t bytes, QEMUIOVector *qiov, 1371d06107adSMax Reitz int flags) 1372d06107adSMax Reitz { 1373d06107adSMax Reitz MirrorOp *op = NULL; 1374d06107adSMax Reitz MirrorBDSOpaque *s = bs->opaque; 1375d06107adSMax Reitz int ret = 0; 1376d06107adSMax Reitz bool copy_to_target; 1377d06107adSMax Reitz 1378d06107adSMax Reitz copy_to_target = s->job->ret >= 0 && 1379d06107adSMax Reitz s->job->copy_mode == MIRROR_COPY_MODE_WRITE_BLOCKING; 1380d06107adSMax Reitz 1381d06107adSMax Reitz if (copy_to_target) { 1382d06107adSMax Reitz op = active_write_prepare(s->job, offset, bytes); 1383d06107adSMax Reitz } 1384d06107adSMax Reitz 1385d06107adSMax Reitz switch (method) { 1386d06107adSMax Reitz case MIRROR_METHOD_COPY: 1387d06107adSMax Reitz ret = bdrv_co_pwritev(bs->backing, offset, bytes, qiov, flags); 1388d06107adSMax Reitz break; 1389d06107adSMax Reitz 1390d06107adSMax Reitz case MIRROR_METHOD_ZERO: 1391d06107adSMax Reitz ret = bdrv_co_pwrite_zeroes(bs->backing, offset, bytes, flags); 1392d06107adSMax Reitz break; 1393d06107adSMax Reitz 1394d06107adSMax Reitz case MIRROR_METHOD_DISCARD: 13950b9fd3f4SFam Zheng ret = bdrv_co_pdiscard(bs->backing, offset, bytes); 1396d06107adSMax Reitz break; 1397d06107adSMax Reitz 1398d06107adSMax Reitz default: 1399d06107adSMax Reitz abort(); 1400d06107adSMax Reitz } 1401d06107adSMax Reitz 1402d06107adSMax Reitz if (ret < 0) { 1403d06107adSMax Reitz goto out; 1404d06107adSMax Reitz } 1405d06107adSMax Reitz 1406d06107adSMax Reitz if (copy_to_target) { 1407d06107adSMax Reitz do_sync_target_write(s->job, method, offset, bytes, qiov, flags); 1408d06107adSMax Reitz } 1409d06107adSMax Reitz 1410d06107adSMax Reitz out: 1411d06107adSMax Reitz if (copy_to_target) { 1412d06107adSMax Reitz active_write_settle(op); 1413d06107adSMax Reitz } 1414d06107adSMax Reitz return ret; 1415d06107adSMax Reitz } 1416d06107adSMax Reitz 14174ef85a9cSKevin Wolf static int coroutine_fn bdrv_mirror_top_pwritev(BlockDriverState *bs, 14184ef85a9cSKevin Wolf uint64_t offset, uint64_t bytes, QEMUIOVector *qiov, int flags) 14194ef85a9cSKevin Wolf { 1420d06107adSMax Reitz MirrorBDSOpaque *s = bs->opaque; 1421d06107adSMax Reitz QEMUIOVector bounce_qiov; 1422d06107adSMax Reitz void *bounce_buf; 1423d06107adSMax Reitz int ret = 0; 1424d06107adSMax Reitz bool copy_to_target; 1425d06107adSMax Reitz 1426d06107adSMax Reitz copy_to_target = s->job->ret >= 0 && 1427d06107adSMax Reitz s->job->copy_mode == MIRROR_COPY_MODE_WRITE_BLOCKING; 1428d06107adSMax Reitz 1429d06107adSMax Reitz if (copy_to_target) { 1430d06107adSMax Reitz /* The guest might concurrently modify the data to write; but 1431d06107adSMax Reitz * the data on source and destination must match, so we have 1432d06107adSMax Reitz * to use a bounce buffer if we are going to write to the 1433d06107adSMax Reitz * target now. */ 1434d06107adSMax Reitz bounce_buf = qemu_blockalign(bs, bytes); 1435d06107adSMax Reitz iov_to_buf_full(qiov->iov, qiov->niov, 0, bounce_buf, bytes); 1436d06107adSMax Reitz 1437d06107adSMax Reitz qemu_iovec_init(&bounce_qiov, 1); 1438d06107adSMax Reitz qemu_iovec_add(&bounce_qiov, bounce_buf, bytes); 1439d06107adSMax Reitz qiov = &bounce_qiov; 1440d06107adSMax Reitz } 1441d06107adSMax Reitz 1442d06107adSMax Reitz ret = bdrv_mirror_top_do_write(bs, MIRROR_METHOD_COPY, offset, bytes, qiov, 1443d06107adSMax Reitz flags); 1444d06107adSMax Reitz 1445d06107adSMax Reitz if (copy_to_target) { 1446d06107adSMax Reitz qemu_iovec_destroy(&bounce_qiov); 1447d06107adSMax Reitz qemu_vfree(bounce_buf); 1448d06107adSMax Reitz } 1449d06107adSMax Reitz 1450d06107adSMax Reitz return ret; 14514ef85a9cSKevin Wolf } 14524ef85a9cSKevin Wolf 14534ef85a9cSKevin Wolf static int coroutine_fn bdrv_mirror_top_flush(BlockDriverState *bs) 14544ef85a9cSKevin Wolf { 1455ce960aa9SVladimir Sementsov-Ogievskiy if (bs->backing == NULL) { 1456ce960aa9SVladimir Sementsov-Ogievskiy /* we can be here after failed bdrv_append in mirror_start_job */ 1457ce960aa9SVladimir Sementsov-Ogievskiy return 0; 1458ce960aa9SVladimir Sementsov-Ogievskiy } 14594ef85a9cSKevin Wolf return bdrv_co_flush(bs->backing->bs); 14604ef85a9cSKevin Wolf } 14614ef85a9cSKevin Wolf 14624ef85a9cSKevin Wolf static int coroutine_fn bdrv_mirror_top_pwrite_zeroes(BlockDriverState *bs, 1463f5a5ca79SManos Pitsidianakis int64_t offset, int bytes, BdrvRequestFlags flags) 14644ef85a9cSKevin Wolf { 1465d06107adSMax Reitz return bdrv_mirror_top_do_write(bs, MIRROR_METHOD_ZERO, offset, bytes, NULL, 1466d06107adSMax Reitz flags); 14674ef85a9cSKevin Wolf } 14684ef85a9cSKevin Wolf 14694ef85a9cSKevin Wolf static int coroutine_fn bdrv_mirror_top_pdiscard(BlockDriverState *bs, 1470f5a5ca79SManos Pitsidianakis int64_t offset, int bytes) 14714ef85a9cSKevin Wolf { 1472d06107adSMax Reitz return bdrv_mirror_top_do_write(bs, MIRROR_METHOD_DISCARD, offset, bytes, 1473d06107adSMax Reitz NULL, 0); 14744ef85a9cSKevin Wolf } 14754ef85a9cSKevin Wolf 1476998b3a1eSMax Reitz static void bdrv_mirror_top_refresh_filename(BlockDriverState *bs) 1477fd4a6493SKevin Wolf { 147818775ff3SVladimir Sementsov-Ogievskiy if (bs->backing == NULL) { 147918775ff3SVladimir Sementsov-Ogievskiy /* we can be here after failed bdrv_attach_child in 148018775ff3SVladimir Sementsov-Ogievskiy * bdrv_set_backing_hd */ 148118775ff3SVladimir Sementsov-Ogievskiy return; 148218775ff3SVladimir Sementsov-Ogievskiy } 1483fd4a6493SKevin Wolf pstrcpy(bs->exact_filename, sizeof(bs->exact_filename), 1484fd4a6493SKevin Wolf bs->backing->bs->filename); 1485fd4a6493SKevin Wolf } 1486fd4a6493SKevin Wolf 14874ef85a9cSKevin Wolf static void bdrv_mirror_top_child_perm(BlockDriverState *bs, BdrvChild *c, 14884ef85a9cSKevin Wolf const BdrvChildRole *role, 1489e0995dc3SKevin Wolf BlockReopenQueue *reopen_queue, 14904ef85a9cSKevin Wolf uint64_t perm, uint64_t shared, 14914ef85a9cSKevin Wolf uint64_t *nperm, uint64_t *nshared) 14924ef85a9cSKevin Wolf { 1493f94dc3b4SMax Reitz MirrorBDSOpaque *s = bs->opaque; 1494f94dc3b4SMax Reitz 1495f94dc3b4SMax Reitz if (s->stop) { 1496f94dc3b4SMax Reitz /* 1497f94dc3b4SMax Reitz * If the job is to be stopped, we do not need to forward 1498f94dc3b4SMax Reitz * anything to the real image. 1499f94dc3b4SMax Reitz */ 1500f94dc3b4SMax Reitz *nperm = 0; 1501f94dc3b4SMax Reitz *nshared = BLK_PERM_ALL; 1502f94dc3b4SMax Reitz return; 1503f94dc3b4SMax Reitz } 1504f94dc3b4SMax Reitz 15054ef85a9cSKevin Wolf /* Must be able to forward guest writes to the real image */ 15064ef85a9cSKevin Wolf *nperm = 0; 15074ef85a9cSKevin Wolf if (perm & BLK_PERM_WRITE) { 15084ef85a9cSKevin Wolf *nperm |= BLK_PERM_WRITE; 15094ef85a9cSKevin Wolf } 15104ef85a9cSKevin Wolf 15114ef85a9cSKevin Wolf *nshared = BLK_PERM_ALL; 15124ef85a9cSKevin Wolf } 15134ef85a9cSKevin Wolf 15144ef85a9cSKevin Wolf /* Dummy node that provides consistent read to its users without requiring it 15154ef85a9cSKevin Wolf * from its backing file and that allows writes on the backing file chain. */ 15164ef85a9cSKevin Wolf static BlockDriver bdrv_mirror_top = { 15174ef85a9cSKevin Wolf .format_name = "mirror_top", 15184ef85a9cSKevin Wolf .bdrv_co_preadv = bdrv_mirror_top_preadv, 15194ef85a9cSKevin Wolf .bdrv_co_pwritev = bdrv_mirror_top_pwritev, 15204ef85a9cSKevin Wolf .bdrv_co_pwrite_zeroes = bdrv_mirror_top_pwrite_zeroes, 15214ef85a9cSKevin Wolf .bdrv_co_pdiscard = bdrv_mirror_top_pdiscard, 15224ef85a9cSKevin Wolf .bdrv_co_flush = bdrv_mirror_top_flush, 15233e4d0e72SEric Blake .bdrv_co_block_status = bdrv_co_block_status_from_backing, 1524fd4a6493SKevin Wolf .bdrv_refresh_filename = bdrv_mirror_top_refresh_filename, 15254ef85a9cSKevin Wolf .bdrv_child_perm = bdrv_mirror_top_child_perm, 15264ef85a9cSKevin Wolf }; 15274ef85a9cSKevin Wolf 1528cc19f177SVladimir Sementsov-Ogievskiy static BlockJob *mirror_start_job( 1529cc19f177SVladimir Sementsov-Ogievskiy const char *job_id, BlockDriverState *bs, 153047970dfbSJohn Snow int creation_flags, BlockDriverState *target, 153147970dfbSJohn Snow const char *replaces, int64_t speed, 153247970dfbSJohn Snow uint32_t granularity, int64_t buf_size, 1533274fcceeSMax Reitz BlockMirrorBackingMode backing_mode, 1534cdf3bc93SMax Reitz bool zero_target, 153503544a6eSFam Zheng BlockdevOnError on_source_error, 1536b952b558SPaolo Bonzini BlockdevOnError on_target_error, 15370fc9f8eaSFam Zheng bool unmap, 1538097310b5SMarkus Armbruster BlockCompletionFunc *cb, 153951ccfa2dSFam Zheng void *opaque, 154003544a6eSFam Zheng const BlockJobDriver *driver, 1541b49f7eadSWen Congyang bool is_none_mode, BlockDriverState *base, 154251ccfa2dSFam Zheng bool auto_complete, const char *filter_node_name, 1543481debaaSMax Reitz bool is_mirror, MirrorCopyMode copy_mode, 154451ccfa2dSFam Zheng Error **errp) 1545893f7ebaSPaolo Bonzini { 1546893f7ebaSPaolo Bonzini MirrorBlockJob *s; 1547429076e8SMax Reitz MirrorBDSOpaque *bs_opaque; 15484ef85a9cSKevin Wolf BlockDriverState *mirror_top_bs; 15494ef85a9cSKevin Wolf bool target_graph_mod; 15504ef85a9cSKevin Wolf bool target_is_backing; 1551b2c2832cSKevin Wolf Error *local_err = NULL; 1552d7086422SKevin Wolf int ret; 1553893f7ebaSPaolo Bonzini 1554eee13dfeSPaolo Bonzini if (granularity == 0) { 1555341ebc2fSJohn Snow granularity = bdrv_get_default_bitmap_granularity(target); 1556eee13dfeSPaolo Bonzini } 1557eee13dfeSPaolo Bonzini 155831826642SEric Blake assert(is_power_of_2(granularity)); 1559eee13dfeSPaolo Bonzini 156048ac0a4dSWen Congyang if (buf_size < 0) { 156148ac0a4dSWen Congyang error_setg(errp, "Invalid parameter 'buf-size'"); 1562cc19f177SVladimir Sementsov-Ogievskiy return NULL; 156348ac0a4dSWen Congyang } 156448ac0a4dSWen Congyang 156548ac0a4dSWen Congyang if (buf_size == 0) { 156648ac0a4dSWen Congyang buf_size = DEFAULT_MIRROR_BUF_SIZE; 156748ac0a4dSWen Congyang } 15685bc361b8SFam Zheng 156986fae10cSKevin Wolf if (bs == target) { 157086fae10cSKevin Wolf error_setg(errp, "Can't mirror node into itself"); 1571cc19f177SVladimir Sementsov-Ogievskiy return NULL; 157286fae10cSKevin Wolf } 157386fae10cSKevin Wolf 15744ef85a9cSKevin Wolf /* In the case of active commit, add dummy driver to provide consistent 15754ef85a9cSKevin Wolf * reads on the top, while disabling it in the intermediate nodes, and make 15764ef85a9cSKevin Wolf * the backing chain writable. */ 15776cdbceb1SKevin Wolf mirror_top_bs = bdrv_new_open_driver(&bdrv_mirror_top, filter_node_name, 15786cdbceb1SKevin Wolf BDRV_O_RDWR, errp); 15794ef85a9cSKevin Wolf if (mirror_top_bs == NULL) { 1580cc19f177SVladimir Sementsov-Ogievskiy return NULL; 1581893f7ebaSPaolo Bonzini } 1582d3c8c674SKevin Wolf if (!filter_node_name) { 1583d3c8c674SKevin Wolf mirror_top_bs->implicit = true; 1584d3c8c674SKevin Wolf } 1585e5182c1cSMax Reitz 1586e5182c1cSMax Reitz /* So that we can always drop this node */ 1587e5182c1cSMax Reitz mirror_top_bs->never_freeze = true; 1588e5182c1cSMax Reitz 15894ef85a9cSKevin Wolf mirror_top_bs->total_sectors = bs->total_sectors; 1590228345bfSMax Reitz mirror_top_bs->supported_write_flags = BDRV_REQ_WRITE_UNCHANGED; 159180f5c33fSKevin Wolf mirror_top_bs->supported_zero_flags = BDRV_REQ_WRITE_UNCHANGED | 159280f5c33fSKevin Wolf BDRV_REQ_NO_FALLBACK; 1593429076e8SMax Reitz bs_opaque = g_new0(MirrorBDSOpaque, 1); 1594429076e8SMax Reitz mirror_top_bs->opaque = bs_opaque; 1595893f7ebaSPaolo Bonzini 15964ef85a9cSKevin Wolf /* bdrv_append takes ownership of the mirror_top_bs reference, need to keep 15977a25fcd0SMax Reitz * it alive until block_job_create() succeeds even if bs has no parent. */ 15984ef85a9cSKevin Wolf bdrv_ref(mirror_top_bs); 15994ef85a9cSKevin Wolf bdrv_drained_begin(bs); 1600b2c2832cSKevin Wolf bdrv_append(mirror_top_bs, bs, &local_err); 16014ef85a9cSKevin Wolf bdrv_drained_end(bs); 16024ef85a9cSKevin Wolf 1603b2c2832cSKevin Wolf if (local_err) { 1604b2c2832cSKevin Wolf bdrv_unref(mirror_top_bs); 1605b2c2832cSKevin Wolf error_propagate(errp, local_err); 1606cc19f177SVladimir Sementsov-Ogievskiy return NULL; 1607b2c2832cSKevin Wolf } 1608b2c2832cSKevin Wolf 16094ef85a9cSKevin Wolf /* Make sure that the source is not resized while the job is running */ 161075859b94SJohn Snow s = block_job_create(job_id, driver, NULL, mirror_top_bs, 16114ef85a9cSKevin Wolf BLK_PERM_CONSISTENT_READ, 16124ef85a9cSKevin Wolf BLK_PERM_CONSISTENT_READ | BLK_PERM_WRITE_UNCHANGED | 16134ef85a9cSKevin Wolf BLK_PERM_WRITE | BLK_PERM_GRAPH_MOD, speed, 16144ef85a9cSKevin Wolf creation_flags, cb, opaque, errp); 16154ef85a9cSKevin Wolf if (!s) { 16164ef85a9cSKevin Wolf goto fail; 16174ef85a9cSKevin Wolf } 1618429076e8SMax Reitz bs_opaque->job = s; 1619429076e8SMax Reitz 16207a25fcd0SMax Reitz /* The block job now has a reference to this node */ 16217a25fcd0SMax Reitz bdrv_unref(mirror_top_bs); 16227a25fcd0SMax Reitz 16234ef85a9cSKevin Wolf s->mirror_top_bs = mirror_top_bs; 16244ef85a9cSKevin Wolf 16254ef85a9cSKevin Wolf /* No resize for the target either; while the mirror is still running, a 16264ef85a9cSKevin Wolf * consistent read isn't necessarily possible. We could possibly allow 16274ef85a9cSKevin Wolf * writes and graph modifications, though it would likely defeat the 16284ef85a9cSKevin Wolf * purpose of a mirror, so leave them blocked for now. 16294ef85a9cSKevin Wolf * 16304ef85a9cSKevin Wolf * In the case of active commit, things look a bit different, though, 16314ef85a9cSKevin Wolf * because the target is an already populated backing file in active use. 16324ef85a9cSKevin Wolf * We can allow anything except resize there.*/ 16334ef85a9cSKevin Wolf target_is_backing = bdrv_chain_contains(bs, target); 16344ef85a9cSKevin Wolf target_graph_mod = (backing_mode != MIRROR_LEAVE_BACKING_CHAIN); 1635d861ab3aSKevin Wolf s->target = blk_new(s->common.job.aio_context, 1636d861ab3aSKevin Wolf BLK_PERM_WRITE | BLK_PERM_RESIZE | 16374ef85a9cSKevin Wolf (target_graph_mod ? BLK_PERM_GRAPH_MOD : 0), 16384ef85a9cSKevin Wolf BLK_PERM_WRITE_UNCHANGED | 16394ef85a9cSKevin Wolf (target_is_backing ? BLK_PERM_CONSISTENT_READ | 16404ef85a9cSKevin Wolf BLK_PERM_WRITE | 16414ef85a9cSKevin Wolf BLK_PERM_GRAPH_MOD : 0)); 1642d7086422SKevin Wolf ret = blk_insert_bs(s->target, target, errp); 1643d7086422SKevin Wolf if (ret < 0) { 16444ef85a9cSKevin Wolf goto fail; 1645d7086422SKevin Wolf } 1646045a2f82SFam Zheng if (is_mirror) { 1647045a2f82SFam Zheng /* XXX: Mirror target could be a NBD server of target QEMU in the case 1648045a2f82SFam Zheng * of non-shared block migration. To allow migration completion, we 1649045a2f82SFam Zheng * have to allow "inactivate" of the target BB. When that happens, we 1650045a2f82SFam Zheng * know the job is drained, and the vcpus are stopped, so no write 1651045a2f82SFam Zheng * operation will be performed. Block layer already has assertions to 1652045a2f82SFam Zheng * ensure that. */ 1653045a2f82SFam Zheng blk_set_force_allow_inactivate(s->target); 1654045a2f82SFam Zheng } 16559ff7f0dfSKevin Wolf blk_set_allow_aio_context_change(s->target, true); 1656cf312932SKevin Wolf blk_set_disable_request_queuing(s->target, true); 1657e253f4b8SKevin Wolf 165809158f00SBenoît Canet s->replaces = g_strdup(replaces); 1659b952b558SPaolo Bonzini s->on_source_error = on_source_error; 1660b952b558SPaolo Bonzini s->on_target_error = on_target_error; 166103544a6eSFam Zheng s->is_none_mode = is_none_mode; 1662274fcceeSMax Reitz s->backing_mode = backing_mode; 1663cdf3bc93SMax Reitz s->zero_target = zero_target; 1664481debaaSMax Reitz s->copy_mode = copy_mode; 16655bc361b8SFam Zheng s->base = base; 1666eee13dfeSPaolo Bonzini s->granularity = granularity; 166748ac0a4dSWen Congyang s->buf_size = ROUND_UP(buf_size, granularity); 16680fc9f8eaSFam Zheng s->unmap = unmap; 1669b49f7eadSWen Congyang if (auto_complete) { 1670b49f7eadSWen Congyang s->should_complete = true; 1671b49f7eadSWen Congyang } 1672b812f671SPaolo Bonzini 16730db6e54aSFam Zheng s->dirty_bitmap = bdrv_create_dirty_bitmap(bs, granularity, NULL, errp); 1674b8afb520SFam Zheng if (!s->dirty_bitmap) { 167588f9d1b3SKevin Wolf goto fail; 1676b8afb520SFam Zheng } 1677dbdf699cSVladimir Sementsov-Ogievskiy if (s->copy_mode == MIRROR_COPY_MODE_WRITE_BLOCKING) { 1678dbdf699cSVladimir Sementsov-Ogievskiy bdrv_disable_dirty_bitmap(s->dirty_bitmap); 1679dbdf699cSVladimir Sementsov-Ogievskiy } 168010f3cd15SAlberto Garcia 168167b24427SAlberto Garcia ret = block_job_add_bdrv(&s->common, "source", bs, 0, 168267b24427SAlberto Garcia BLK_PERM_WRITE_UNCHANGED | BLK_PERM_WRITE | 168367b24427SAlberto Garcia BLK_PERM_CONSISTENT_READ, 168467b24427SAlberto Garcia errp); 168567b24427SAlberto Garcia if (ret < 0) { 168667b24427SAlberto Garcia goto fail; 168767b24427SAlberto Garcia } 168867b24427SAlberto Garcia 16894ef85a9cSKevin Wolf /* Required permissions are already taken with blk_new() */ 169076d554e2SKevin Wolf block_job_add_bdrv(&s->common, "target", target, 0, BLK_PERM_ALL, 169176d554e2SKevin Wolf &error_abort); 169276d554e2SKevin Wolf 1693f3ede4b0SAlberto Garcia /* In commit_active_start() all intermediate nodes disappear, so 1694f3ede4b0SAlberto Garcia * any jobs in them must be blocked */ 16954ef85a9cSKevin Wolf if (target_is_backing) { 1696f3ede4b0SAlberto Garcia BlockDriverState *iter; 1697f3ede4b0SAlberto Garcia for (iter = backing_bs(bs); iter != target; iter = backing_bs(iter)) { 16984ef85a9cSKevin Wolf /* XXX BLK_PERM_WRITE needs to be allowed so we don't block 16994ef85a9cSKevin Wolf * ourselves at s->base (if writes are blocked for a node, they are 17004ef85a9cSKevin Wolf * also blocked for its backing file). The other options would be a 17014ef85a9cSKevin Wolf * second filter driver above s->base (== target). */ 17024ef85a9cSKevin Wolf ret = block_job_add_bdrv(&s->common, "intermediate node", iter, 0, 17034ef85a9cSKevin Wolf BLK_PERM_WRITE_UNCHANGED | BLK_PERM_WRITE, 17044ef85a9cSKevin Wolf errp); 17054ef85a9cSKevin Wolf if (ret < 0) { 17064ef85a9cSKevin Wolf goto fail; 17074ef85a9cSKevin Wolf } 1708f3ede4b0SAlberto Garcia } 1709ef53dc09SAlberto Garcia 1710ef53dc09SAlberto Garcia if (bdrv_freeze_backing_chain(mirror_top_bs, target, errp) < 0) { 1711ef53dc09SAlberto Garcia goto fail; 1712ef53dc09SAlberto Garcia } 1713f3ede4b0SAlberto Garcia } 171410f3cd15SAlberto Garcia 171512aa4082SMax Reitz QTAILQ_INIT(&s->ops_in_flight); 171612aa4082SMax Reitz 17175ccac6f1SJohn Snow trace_mirror_start(bs, s, opaque); 1718da01ff7fSKevin Wolf job_start(&s->common.job); 1719cc19f177SVladimir Sementsov-Ogievskiy 1720cc19f177SVladimir Sementsov-Ogievskiy return &s->common; 17214ef85a9cSKevin Wolf 17224ef85a9cSKevin Wolf fail: 17234ef85a9cSKevin Wolf if (s) { 17247a25fcd0SMax Reitz /* Make sure this BDS does not go away until we have completed the graph 17257a25fcd0SMax Reitz * changes below */ 17267a25fcd0SMax Reitz bdrv_ref(mirror_top_bs); 17277a25fcd0SMax Reitz 17284ef85a9cSKevin Wolf g_free(s->replaces); 17294ef85a9cSKevin Wolf blk_unref(s->target); 1730429076e8SMax Reitz bs_opaque->job = NULL; 1731e917e2cbSAlberto Garcia if (s->dirty_bitmap) { 17325deb6cbdSVladimir Sementsov-Ogievskiy bdrv_release_dirty_bitmap(s->dirty_bitmap); 1733e917e2cbSAlberto Garcia } 17344ad35181SKevin Wolf job_early_fail(&s->common.job); 17354ef85a9cSKevin Wolf } 17364ef85a9cSKevin Wolf 1737f94dc3b4SMax Reitz bs_opaque->stop = true; 1738f94dc3b4SMax Reitz bdrv_child_refresh_perms(mirror_top_bs, mirror_top_bs->backing, 1739c1cef672SFam Zheng &error_abort); 17405fe31c25SKevin Wolf bdrv_replace_node(mirror_top_bs, backing_bs(mirror_top_bs), &error_abort); 17417a25fcd0SMax Reitz 17427a25fcd0SMax Reitz bdrv_unref(mirror_top_bs); 1743cc19f177SVladimir Sementsov-Ogievskiy 1744cc19f177SVladimir Sementsov-Ogievskiy return NULL; 1745893f7ebaSPaolo Bonzini } 174603544a6eSFam Zheng 174771aa9867SAlberto Garcia void mirror_start(const char *job_id, BlockDriverState *bs, 174871aa9867SAlberto Garcia BlockDriverState *target, const char *replaces, 1749a1999b33SJohn Snow int creation_flags, int64_t speed, 1750a1999b33SJohn Snow uint32_t granularity, int64_t buf_size, 1751274fcceeSMax Reitz MirrorSyncMode mode, BlockMirrorBackingMode backing_mode, 1752cdf3bc93SMax Reitz bool zero_target, 1753274fcceeSMax Reitz BlockdevOnError on_source_error, 175403544a6eSFam Zheng BlockdevOnError on_target_error, 1755481debaaSMax Reitz bool unmap, const char *filter_node_name, 1756481debaaSMax Reitz MirrorCopyMode copy_mode, Error **errp) 175703544a6eSFam Zheng { 175803544a6eSFam Zheng bool is_none_mode; 175903544a6eSFam Zheng BlockDriverState *base; 176003544a6eSFam Zheng 1761c8b56501SJohn Snow if ((mode == MIRROR_SYNC_MODE_INCREMENTAL) || 1762c8b56501SJohn Snow (mode == MIRROR_SYNC_MODE_BITMAP)) { 1763c8b56501SJohn Snow error_setg(errp, "Sync mode '%s' not supported", 1764c8b56501SJohn Snow MirrorSyncMode_str(mode)); 1765d58d8453SJohn Snow return; 1766d58d8453SJohn Snow } 176703544a6eSFam Zheng is_none_mode = mode == MIRROR_SYNC_MODE_NONE; 1768760e0063SKevin Wolf base = mode == MIRROR_SYNC_MODE_TOP ? backing_bs(bs) : NULL; 1769a1999b33SJohn Snow mirror_start_job(job_id, bs, creation_flags, target, replaces, 1770cdf3bc93SMax Reitz speed, granularity, buf_size, backing_mode, zero_target, 177151ccfa2dSFam Zheng on_source_error, on_target_error, unmap, NULL, NULL, 17726cdbceb1SKevin Wolf &mirror_job_driver, is_none_mode, base, false, 1773481debaaSMax Reitz filter_node_name, true, copy_mode, errp); 177403544a6eSFam Zheng } 177503544a6eSFam Zheng 1776cc19f177SVladimir Sementsov-Ogievskiy BlockJob *commit_active_start(const char *job_id, BlockDriverState *bs, 177747970dfbSJohn Snow BlockDriverState *base, int creation_flags, 177847970dfbSJohn Snow int64_t speed, BlockdevOnError on_error, 17790db832f4SKevin Wolf const char *filter_node_name, 178078bbd910SFam Zheng BlockCompletionFunc *cb, void *opaque, 178178bbd910SFam Zheng bool auto_complete, Error **errp) 178203544a6eSFam Zheng { 17831ba79388SAlberto Garcia bool base_read_only; 1784cc67f4d1SJeff Cody Error *local_err = NULL; 1785cc19f177SVladimir Sementsov-Ogievskiy BlockJob *ret; 17864da83585SJeff Cody 17871ba79388SAlberto Garcia base_read_only = bdrv_is_read_only(base); 17884da83585SJeff Cody 17891ba79388SAlberto Garcia if (base_read_only) { 17901ba79388SAlberto Garcia if (bdrv_reopen_set_read_only(base, false, errp) < 0) { 1791cc19f177SVladimir Sementsov-Ogievskiy return NULL; 179220a63d2cSFam Zheng } 17931ba79388SAlberto Garcia } 17944da83585SJeff Cody 1795cc19f177SVladimir Sementsov-Ogievskiy ret = mirror_start_job( 1796cc19f177SVladimir Sementsov-Ogievskiy job_id, bs, creation_flags, base, NULL, speed, 0, 0, 1797cdf3bc93SMax Reitz MIRROR_LEAVE_BACKING_CHAIN, false, 179851ccfa2dSFam Zheng on_error, on_error, true, cb, opaque, 17996cdbceb1SKevin Wolf &commit_active_job_driver, false, base, auto_complete, 1800481debaaSMax Reitz filter_node_name, false, MIRROR_COPY_MODE_BACKGROUND, 1801481debaaSMax Reitz &local_err); 18020fb6395cSMarkus Armbruster if (local_err) { 1803cc67f4d1SJeff Cody error_propagate(errp, local_err); 18044da83585SJeff Cody goto error_restore_flags; 18054da83585SJeff Cody } 18064da83585SJeff Cody 1807cc19f177SVladimir Sementsov-Ogievskiy return ret; 18084da83585SJeff Cody 18094da83585SJeff Cody error_restore_flags: 18104da83585SJeff Cody /* ignore error and errp for bdrv_reopen, because we want to propagate 18114da83585SJeff Cody * the original error */ 18121ba79388SAlberto Garcia if (base_read_only) { 18131ba79388SAlberto Garcia bdrv_reopen_set_read_only(base, true, NULL); 18141ba79388SAlberto Garcia } 1815cc19f177SVladimir Sementsov-Ogievskiy return NULL; 181603544a6eSFam Zheng } 1817