1893f7ebaSPaolo Bonzini /* 2893f7ebaSPaolo Bonzini * Image mirroring 3893f7ebaSPaolo Bonzini * 4893f7ebaSPaolo Bonzini * Copyright Red Hat, Inc. 2012 5893f7ebaSPaolo Bonzini * 6893f7ebaSPaolo Bonzini * Authors: 7893f7ebaSPaolo Bonzini * Paolo Bonzini <pbonzini@redhat.com> 8893f7ebaSPaolo Bonzini * 9893f7ebaSPaolo Bonzini * This work is licensed under the terms of the GNU LGPL, version 2 or later. 10893f7ebaSPaolo Bonzini * See the COPYING.LIB file in the top-level directory. 11893f7ebaSPaolo Bonzini * 12893f7ebaSPaolo Bonzini */ 13893f7ebaSPaolo Bonzini 1480c71a24SPeter Maydell #include "qemu/osdep.h" 15fd4a6493SKevin Wolf #include "qemu/cutils.h" 1612aa4082SMax Reitz #include "qemu/coroutine.h" 171181e19aSMax Reitz #include "qemu/range.h" 18893f7ebaSPaolo Bonzini #include "trace.h" 19c87621eaSJohn Snow #include "block/blockjob_int.h" 20737e150eSPaolo Bonzini #include "block/block_int.h" 21e2c1c34fSMarkus Armbruster #include "block/dirty-bitmap.h" 2232cad1ffSPhilippe Mathieu-Daudé #include "system/block-backend.h" 23da34e65cSMarkus Armbruster #include "qapi/error.h" 24893f7ebaSPaolo Bonzini #include "qemu/ratelimit.h" 25b812f671SPaolo Bonzini #include "qemu/bitmap.h" 265df022cfSPeter Maydell #include "qemu/memalign.h" 27893f7ebaSPaolo Bonzini 28402a4741SPaolo Bonzini #define MAX_IN_FLIGHT 16 29b436982fSEric Blake #define MAX_IO_BYTES (1 << 20) /* 1 Mb */ 30b436982fSEric Blake #define DEFAULT_MIRROR_BUF_SIZE (MAX_IN_FLIGHT * MAX_IO_BYTES) 31402a4741SPaolo Bonzini 32402a4741SPaolo Bonzini /* The mirroring buffer is a list of granularity-sized chunks. 33402a4741SPaolo Bonzini * Free chunks are organized in a list. 34402a4741SPaolo Bonzini */ 35402a4741SPaolo Bonzini typedef struct MirrorBuffer { 36402a4741SPaolo Bonzini QSIMPLEQ_ENTRY(MirrorBuffer) next; 37402a4741SPaolo Bonzini } MirrorBuffer; 38893f7ebaSPaolo Bonzini 3912aa4082SMax Reitz typedef struct MirrorOp MirrorOp; 4012aa4082SMax Reitz 41893f7ebaSPaolo Bonzini typedef struct MirrorBlockJob { 42893f7ebaSPaolo Bonzini BlockJob common; 43e253f4b8SKevin Wolf BlockBackend *target; 444ef85a9cSKevin Wolf BlockDriverState *mirror_top_bs; 455bc361b8SFam Zheng BlockDriverState *base; 463f072a7fSMax Reitz BlockDriverState *base_overlay; 474ef85a9cSKevin Wolf 4809158f00SBenoît Canet /* The name of the graph node to replace */ 4909158f00SBenoît Canet char *replaces; 5009158f00SBenoît Canet /* The BDS to replace */ 5109158f00SBenoît Canet BlockDriverState *to_replace; 5209158f00SBenoît Canet /* Used to block operations on the drive-mirror-replace target */ 5309158f00SBenoît Canet Error *replace_blocker; 549474d97bSEric Blake MirrorSyncMode sync_mode; 55274fcceeSMax Reitz BlockMirrorBackingMode backing_mode; 56cdf3bc93SMax Reitz /* Whether the target image requires explicit zero-initialization */ 57cdf3bc93SMax Reitz bool zero_target; 58*d17a34bfSEric Blake /* Whether the target should be assumed to be already zero initialized */ 59*d17a34bfSEric Blake bool target_is_zero; 602d400d15SFiona Ebner /* 612d400d15SFiona Ebner * To be accesssed with atomics. Written only under the BQL (required by the 622d400d15SFiona Ebner * current implementation of mirror_change()). 632d400d15SFiona Ebner */ 64d06107adSMax Reitz MirrorCopyMode copy_mode; 65b952b558SPaolo Bonzini BlockdevOnError on_source_error, on_target_error; 6676cb2f24SFiona Ebner /* 6776cb2f24SFiona Ebner * To be accessed with atomics. 6876cb2f24SFiona Ebner * 6976cb2f24SFiona Ebner * Set when the target is synced (dirty bitmap is clean, nothing in flight) 7076cb2f24SFiona Ebner * and the job is running in active mode. 7176cb2f24SFiona Ebner */ 72d06107adSMax Reitz bool actively_synced; 73d63ffd87SPaolo Bonzini bool should_complete; 74eee13dfeSPaolo Bonzini int64_t granularity; 75b812f671SPaolo Bonzini size_t buf_size; 76b21c7652SMax Reitz int64_t bdev_length; 77b812f671SPaolo Bonzini unsigned long *cow_bitmap; 78e4654d2dSFam Zheng BdrvDirtyBitmap *dirty_bitmap; 79dc162c8eSFam Zheng BdrvDirtyBitmapIter *dbi; 80893f7ebaSPaolo Bonzini uint8_t *buf; 81402a4741SPaolo Bonzini QSIMPLEQ_HEAD(, MirrorBuffer) buf_free; 82402a4741SPaolo Bonzini int buf_free_count; 83bd48bde8SPaolo Bonzini 8449efb1f5SDenis V. Lunev uint64_t last_pause_ns; 85402a4741SPaolo Bonzini unsigned long *in_flight_bitmap; 861b8f7776SDenis V. Lunev unsigned in_flight; 87b436982fSEric Blake int64_t bytes_in_flight; 88b58deb34SPaolo Bonzini QTAILQ_HEAD(, MirrorOp) ops_in_flight; 89bd48bde8SPaolo Bonzini int ret; 900fc9f8eaSFam Zheng bool unmap; 91b436982fSEric Blake int target_cluster_size; 92e5b43573SFam Zheng int max_iov; 9390ab48ebSAnton Nefedov bool initial_zeroing_ongoing; 94d06107adSMax Reitz int in_active_write_counter; 95d69a879bSHanna Reitz int64_t active_write_bytes_in_flight; 96737efc1eSJohn Snow bool prepared; 975e771752SSergio Lopez bool in_drain; 987d99ae59SAlexander Ivanov bool base_ro; 99893f7ebaSPaolo Bonzini } MirrorBlockJob; 100893f7ebaSPaolo Bonzini 101429076e8SMax Reitz typedef struct MirrorBDSOpaque { 102429076e8SMax Reitz MirrorBlockJob *job; 103f94dc3b4SMax Reitz bool stop; 10453431b90SMax Reitz bool is_commit; 105429076e8SMax Reitz } MirrorBDSOpaque; 106429076e8SMax Reitz 10712aa4082SMax Reitz struct MirrorOp { 108bd48bde8SPaolo Bonzini MirrorBlockJob *s; 109bd48bde8SPaolo Bonzini QEMUIOVector qiov; 110b436982fSEric Blake int64_t offset; 111b436982fSEric Blake uint64_t bytes; 1122e1990b2SMax Reitz 1132e1990b2SMax Reitz /* The pointee is set by mirror_co_read(), mirror_co_zero(), and 1142e1990b2SMax Reitz * mirror_co_discard() before yielding for the first time */ 1152e1990b2SMax Reitz int64_t *bytes_handled; 11612aa4082SMax Reitz 1171181e19aSMax Reitz bool is_pseudo_op; 118d06107adSMax Reitz bool is_active_write; 119ce8cabbdSKevin Wolf bool is_in_flight; 12012aa4082SMax Reitz CoQueue waiting_requests; 121eed325b9SKevin Wolf Coroutine *co; 122d44dae1aSVladimir Sementsov-Ogievskiy MirrorOp *waiting_for_op; 12312aa4082SMax Reitz 12412aa4082SMax Reitz QTAILQ_ENTRY(MirrorOp) next; 12512aa4082SMax Reitz }; 126bd48bde8SPaolo Bonzini 1274295c5fcSMax Reitz typedef enum MirrorMethod { 1284295c5fcSMax Reitz MIRROR_METHOD_COPY, 1294295c5fcSMax Reitz MIRROR_METHOD_ZERO, 1304295c5fcSMax Reitz MIRROR_METHOD_DISCARD, 1314295c5fcSMax Reitz } MirrorMethod; 1324295c5fcSMax Reitz 133b952b558SPaolo Bonzini static BlockErrorAction mirror_error_action(MirrorBlockJob *s, bool read, 134b952b558SPaolo Bonzini int error) 135b952b558SPaolo Bonzini { 13676cb2f24SFiona Ebner qatomic_set(&s->actively_synced, false); 137b952b558SPaolo Bonzini if (read) { 13881e254dcSKevin Wolf return block_job_error_action(&s->common, s->on_source_error, 13981e254dcSKevin Wolf true, error); 140b952b558SPaolo Bonzini } else { 14181e254dcSKevin Wolf return block_job_error_action(&s->common, s->on_target_error, 14281e254dcSKevin Wolf false, error); 143b952b558SPaolo Bonzini } 144b952b558SPaolo Bonzini } 145b952b558SPaolo Bonzini 1461181e19aSMax Reitz static void coroutine_fn mirror_wait_on_conflicts(MirrorOp *self, 1471181e19aSMax Reitz MirrorBlockJob *s, 1481181e19aSMax Reitz uint64_t offset, 1491181e19aSMax Reitz uint64_t bytes) 1501181e19aSMax Reitz { 1511181e19aSMax Reitz uint64_t self_start_chunk = offset / s->granularity; 1521181e19aSMax Reitz uint64_t self_end_chunk = DIV_ROUND_UP(offset + bytes, s->granularity); 1531181e19aSMax Reitz uint64_t self_nb_chunks = self_end_chunk - self_start_chunk; 1541181e19aSMax Reitz 1551181e19aSMax Reitz while (find_next_bit(s->in_flight_bitmap, self_end_chunk, 1561181e19aSMax Reitz self_start_chunk) < self_end_chunk && 1571181e19aSMax Reitz s->ret >= 0) 1581181e19aSMax Reitz { 1591181e19aSMax Reitz MirrorOp *op; 1601181e19aSMax Reitz 1611181e19aSMax Reitz QTAILQ_FOREACH(op, &s->ops_in_flight, next) { 1621181e19aSMax Reitz uint64_t op_start_chunk = op->offset / s->granularity; 1631181e19aSMax Reitz uint64_t op_nb_chunks = DIV_ROUND_UP(op->offset + op->bytes, 1641181e19aSMax Reitz s->granularity) - 1651181e19aSMax Reitz op_start_chunk; 1661181e19aSMax Reitz 1671181e19aSMax Reitz if (op == self) { 1681181e19aSMax Reitz continue; 1691181e19aSMax Reitz } 1701181e19aSMax Reitz 1711181e19aSMax Reitz if (ranges_overlap(self_start_chunk, self_nb_chunks, 1721181e19aSMax Reitz op_start_chunk, op_nb_chunks)) 1731181e19aSMax Reitz { 17466fed30cSStefano Garzarella if (self) { 175d44dae1aSVladimir Sementsov-Ogievskiy /* 17666fed30cSStefano Garzarella * If the operation is already (indirectly) waiting for us, 17766fed30cSStefano Garzarella * or will wait for us as soon as it wakes up, then just go 17866fed30cSStefano Garzarella * on (instead of producing a deadlock in the former case). 179d44dae1aSVladimir Sementsov-Ogievskiy */ 180d44dae1aSVladimir Sementsov-Ogievskiy if (op->waiting_for_op) { 181d44dae1aSVladimir Sementsov-Ogievskiy continue; 182d44dae1aSVladimir Sementsov-Ogievskiy } 183d44dae1aSVladimir Sementsov-Ogievskiy 184d44dae1aSVladimir Sementsov-Ogievskiy self->waiting_for_op = op; 18566fed30cSStefano Garzarella } 18666fed30cSStefano Garzarella 1871181e19aSMax Reitz qemu_co_queue_wait(&op->waiting_requests, NULL); 18866fed30cSStefano Garzarella 18966fed30cSStefano Garzarella if (self) { 190d44dae1aSVladimir Sementsov-Ogievskiy self->waiting_for_op = NULL; 19166fed30cSStefano Garzarella } 19266fed30cSStefano Garzarella 1931181e19aSMax Reitz break; 1941181e19aSMax Reitz } 1951181e19aSMax Reitz } 1961181e19aSMax Reitz } 1971181e19aSMax Reitz } 1981181e19aSMax Reitz 1992e1990b2SMax Reitz static void coroutine_fn mirror_iteration_done(MirrorOp *op, int ret) 200bd48bde8SPaolo Bonzini { 201bd48bde8SPaolo Bonzini MirrorBlockJob *s = op->s; 202402a4741SPaolo Bonzini struct iovec *iov; 203bd48bde8SPaolo Bonzini int64_t chunk_num; 204b436982fSEric Blake int i, nb_chunks; 205bd48bde8SPaolo Bonzini 206b436982fSEric Blake trace_mirror_iteration_done(s, op->offset, op->bytes, ret); 207bd48bde8SPaolo Bonzini 208bd48bde8SPaolo Bonzini s->in_flight--; 209b436982fSEric Blake s->bytes_in_flight -= op->bytes; 210402a4741SPaolo Bonzini iov = op->qiov.iov; 211402a4741SPaolo Bonzini for (i = 0; i < op->qiov.niov; i++) { 212402a4741SPaolo Bonzini MirrorBuffer *buf = (MirrorBuffer *) iov[i].iov_base; 213402a4741SPaolo Bonzini QSIMPLEQ_INSERT_TAIL(&s->buf_free, buf, next); 214402a4741SPaolo Bonzini s->buf_free_count++; 215402a4741SPaolo Bonzini } 216402a4741SPaolo Bonzini 217b436982fSEric Blake chunk_num = op->offset / s->granularity; 218b436982fSEric Blake nb_chunks = DIV_ROUND_UP(op->bytes, s->granularity); 21912aa4082SMax Reitz 220402a4741SPaolo Bonzini bitmap_clear(s->in_flight_bitmap, chunk_num, nb_chunks); 22112aa4082SMax Reitz QTAILQ_REMOVE(&s->ops_in_flight, op, next); 222b21c7652SMax Reitz if (ret >= 0) { 223b21c7652SMax Reitz if (s->cow_bitmap) { 224bd48bde8SPaolo Bonzini bitmap_set(s->cow_bitmap, chunk_num, nb_chunks); 225bd48bde8SPaolo Bonzini } 22690ab48ebSAnton Nefedov if (!s->initial_zeroing_ongoing) { 22730a5c887SKevin Wolf job_progress_update(&s->common.job, op->bytes); 228b21c7652SMax Reitz } 22990ab48ebSAnton Nefedov } 2306df3bf8eSZhang Min qemu_iovec_destroy(&op->qiov); 2317b770c72SStefan Hajnoczi 23212aa4082SMax Reitz qemu_co_queue_restart_all(&op->waiting_requests); 23312aa4082SMax Reitz g_free(op); 2347b770c72SStefan Hajnoczi } 235bd48bde8SPaolo Bonzini 2362e1990b2SMax Reitz static void coroutine_fn mirror_write_complete(MirrorOp *op, int ret) 237bd48bde8SPaolo Bonzini { 238bd48bde8SPaolo Bonzini MirrorBlockJob *s = op->s; 239b9e413ddSPaolo Bonzini 240bd48bde8SPaolo Bonzini if (ret < 0) { 241bd48bde8SPaolo Bonzini BlockErrorAction action; 242bd48bde8SPaolo Bonzini 243e0d7f73eSEric Blake bdrv_set_dirty_bitmap(s->dirty_bitmap, op->offset, op->bytes); 244bd48bde8SPaolo Bonzini action = mirror_error_action(s, false, -ret); 245a589569fSWenchao Xia if (action == BLOCK_ERROR_ACTION_REPORT && s->ret >= 0) { 246bd48bde8SPaolo Bonzini s->ret = ret; 247bd48bde8SPaolo Bonzini } 248bd48bde8SPaolo Bonzini } 249d12ade57SVladimir Sementsov-Ogievskiy 250bd48bde8SPaolo Bonzini mirror_iteration_done(op, ret); 251bd48bde8SPaolo Bonzini } 252bd48bde8SPaolo Bonzini 2532e1990b2SMax Reitz static void coroutine_fn mirror_read_complete(MirrorOp *op, int ret) 254bd48bde8SPaolo Bonzini { 255bd48bde8SPaolo Bonzini MirrorBlockJob *s = op->s; 256b9e413ddSPaolo Bonzini 257bd48bde8SPaolo Bonzini if (ret < 0) { 258bd48bde8SPaolo Bonzini BlockErrorAction action; 259bd48bde8SPaolo Bonzini 260e0d7f73eSEric Blake bdrv_set_dirty_bitmap(s->dirty_bitmap, op->offset, op->bytes); 261bd48bde8SPaolo Bonzini action = mirror_error_action(s, true, -ret); 262a589569fSWenchao Xia if (action == BLOCK_ERROR_ACTION_REPORT && s->ret >= 0) { 263bd48bde8SPaolo Bonzini s->ret = ret; 264bd48bde8SPaolo Bonzini } 265bd48bde8SPaolo Bonzini 266bd48bde8SPaolo Bonzini mirror_iteration_done(op, ret); 267d12ade57SVladimir Sementsov-Ogievskiy return; 268bd48bde8SPaolo Bonzini } 269d12ade57SVladimir Sementsov-Ogievskiy 270d12ade57SVladimir Sementsov-Ogievskiy ret = blk_co_pwritev(s->target, op->offset, op->qiov.size, &op->qiov, 0); 271d12ade57SVladimir Sementsov-Ogievskiy mirror_write_complete(op, ret); 272b9e413ddSPaolo Bonzini } 273bd48bde8SPaolo Bonzini 274782d97efSEric Blake /* Clip bytes relative to offset to not exceed end-of-file */ 275782d97efSEric Blake static inline int64_t mirror_clip_bytes(MirrorBlockJob *s, 276782d97efSEric Blake int64_t offset, 277782d97efSEric Blake int64_t bytes) 278782d97efSEric Blake { 279782d97efSEric Blake return MIN(bytes, s->bdev_length - offset); 280782d97efSEric Blake } 281782d97efSEric Blake 282782d97efSEric Blake /* Round offset and/or bytes to target cluster if COW is needed, and 283782d97efSEric Blake * return the offset of the adjusted tail against original. */ 28417ac39c3SPaolo Bonzini static int coroutine_fn mirror_cow_align(MirrorBlockJob *s, int64_t *offset, 285ae4cc877SEric Blake uint64_t *bytes) 286893f7ebaSPaolo Bonzini { 287e5b43573SFam Zheng bool need_cow; 288e5b43573SFam Zheng int ret = 0; 289782d97efSEric Blake int64_t align_offset = *offset; 2907cfd5275SEric Blake int64_t align_bytes = *bytes; 291782d97efSEric Blake int max_bytes = s->granularity * s->max_iov; 292893f7ebaSPaolo Bonzini 293782d97efSEric Blake need_cow = !test_bit(*offset / s->granularity, s->cow_bitmap); 294782d97efSEric Blake need_cow |= !test_bit((*offset + *bytes - 1) / s->granularity, 295e5b43573SFam Zheng s->cow_bitmap); 296e5b43573SFam Zheng if (need_cow) { 297fc6b211fSAndrey Drobyshev bdrv_round_to_subclusters(blk_bs(s->target), *offset, *bytes, 298782d97efSEric Blake &align_offset, &align_bytes); 2998f0720ecSPaolo Bonzini } 3008f0720ecSPaolo Bonzini 301782d97efSEric Blake if (align_bytes > max_bytes) { 302782d97efSEric Blake align_bytes = max_bytes; 303e5b43573SFam Zheng if (need_cow) { 304782d97efSEric Blake align_bytes = QEMU_ALIGN_DOWN(align_bytes, s->target_cluster_size); 305e5b43573SFam Zheng } 306e5b43573SFam Zheng } 307782d97efSEric Blake /* Clipping may result in align_bytes unaligned to chunk boundary, but 3084150ae60SFam Zheng * that doesn't matter because it's already the end of source image. */ 309782d97efSEric Blake align_bytes = mirror_clip_bytes(s, align_offset, align_bytes); 310402a4741SPaolo Bonzini 311782d97efSEric Blake ret = align_offset + align_bytes - (*offset + *bytes); 312782d97efSEric Blake *offset = align_offset; 313782d97efSEric Blake *bytes = align_bytes; 314e5b43573SFam Zheng assert(ret >= 0); 315e5b43573SFam Zheng return ret; 316e5b43573SFam Zheng } 317e5b43573SFam Zheng 318537c3d4fSStefan Hajnoczi static inline void coroutine_fn 319eb994912SHanna Reitz mirror_wait_for_free_in_flight_slot(MirrorBlockJob *s) 32021cd917fSFam Zheng { 32112aa4082SMax Reitz MirrorOp *op; 32212aa4082SMax Reitz 3231181e19aSMax Reitz QTAILQ_FOREACH(op, &s->ops_in_flight, next) { 324eb994912SHanna Reitz /* 325eb994912SHanna Reitz * Do not wait on pseudo ops, because it may in turn wait on 3261181e19aSMax Reitz * some other operation to start, which may in fact be the 3271181e19aSMax Reitz * caller of this function. Since there is only one pseudo op 3281181e19aSMax Reitz * at any given time, we will always find some real operation 329eb994912SHanna Reitz * to wait on. 330eb994912SHanna Reitz * Also, do not wait on active operations, because they do not 331eb994912SHanna Reitz * use up in-flight slots. 332eb994912SHanna Reitz */ 333eb994912SHanna Reitz if (!op->is_pseudo_op && op->is_in_flight && !op->is_active_write) { 33412aa4082SMax Reitz qemu_co_queue_wait(&op->waiting_requests, NULL); 3351181e19aSMax Reitz return; 3361181e19aSMax Reitz } 3371181e19aSMax Reitz } 3381181e19aSMax Reitz abort(); 33921cd917fSFam Zheng } 34021cd917fSFam Zheng 3412e1990b2SMax Reitz /* Perform a mirror copy operation. 3422e1990b2SMax Reitz * 3432e1990b2SMax Reitz * *op->bytes_handled is set to the number of bytes copied after and 3442e1990b2SMax Reitz * including offset, excluding any bytes copied prior to offset due 3452e1990b2SMax Reitz * to alignment. This will be op->bytes if no alignment is necessary, 3462e1990b2SMax Reitz * or (new_end - op->offset) if the tail is rounded up or down due to 347e5b43573SFam Zheng * alignment or buffer limit. 348402a4741SPaolo Bonzini */ 3492e1990b2SMax Reitz static void coroutine_fn mirror_co_read(void *opaque) 350e5b43573SFam Zheng { 3512e1990b2SMax Reitz MirrorOp *op = opaque; 3522e1990b2SMax Reitz MirrorBlockJob *s = op->s; 353ae4cc877SEric Blake int nb_chunks; 3545791ba52SMarc-André Lureau int ret = -1; 355ae4cc877SEric Blake uint64_t max_bytes; 356402a4741SPaolo Bonzini 357ae4cc877SEric Blake max_bytes = s->granularity * s->max_iov; 358e5b43573SFam Zheng 359e5b43573SFam Zheng /* We can only handle as much as buf_size at a time. */ 3602e1990b2SMax Reitz op->bytes = MIN(s->buf_size, MIN(max_bytes, op->bytes)); 3612e1990b2SMax Reitz assert(op->bytes); 3622e1990b2SMax Reitz assert(op->bytes < BDRV_REQUEST_MAX_BYTES); 3632e1990b2SMax Reitz *op->bytes_handled = op->bytes; 364e5b43573SFam Zheng 365e5b43573SFam Zheng if (s->cow_bitmap) { 3662e1990b2SMax Reitz *op->bytes_handled += mirror_cow_align(s, &op->offset, &op->bytes); 367e5b43573SFam Zheng } 3682e1990b2SMax Reitz /* Cannot exceed BDRV_REQUEST_MAX_BYTES + INT_MAX */ 3692e1990b2SMax Reitz assert(*op->bytes_handled <= UINT_MAX); 3702e1990b2SMax Reitz assert(op->bytes <= s->buf_size); 371ae4cc877SEric Blake /* The offset is granularity-aligned because: 372e5b43573SFam Zheng * 1) Caller passes in aligned values; 373e5b43573SFam Zheng * 2) mirror_cow_align is used only when target cluster is larger. */ 3742e1990b2SMax Reitz assert(QEMU_IS_ALIGNED(op->offset, s->granularity)); 375ae4cc877SEric Blake /* The range is sector-aligned, since bdrv_getlength() rounds up. */ 3762e1990b2SMax Reitz assert(QEMU_IS_ALIGNED(op->bytes, BDRV_SECTOR_SIZE)); 3772e1990b2SMax Reitz nb_chunks = DIV_ROUND_UP(op->bytes, s->granularity); 378e5b43573SFam Zheng 379e5b43573SFam Zheng while (s->buf_free_count < nb_chunks) { 3802e1990b2SMax Reitz trace_mirror_yield_in_flight(s, op->offset, s->in_flight); 3819178f4feSKevin Wolf mirror_wait_for_free_in_flight_slot(s); 382b812f671SPaolo Bonzini } 383b812f671SPaolo Bonzini 384402a4741SPaolo Bonzini /* Now make a QEMUIOVector taking enough granularity-sized chunks 385402a4741SPaolo Bonzini * from s->buf_free. 386402a4741SPaolo Bonzini */ 387402a4741SPaolo Bonzini qemu_iovec_init(&op->qiov, nb_chunks); 388402a4741SPaolo Bonzini while (nb_chunks-- > 0) { 389402a4741SPaolo Bonzini MirrorBuffer *buf = QSIMPLEQ_FIRST(&s->buf_free); 3902e1990b2SMax Reitz size_t remaining = op->bytes - op->qiov.size; 3915a0f6fd5SKevin Wolf 392402a4741SPaolo Bonzini QSIMPLEQ_REMOVE_HEAD(&s->buf_free, next); 393402a4741SPaolo Bonzini s->buf_free_count--; 3945a0f6fd5SKevin Wolf qemu_iovec_add(&op->qiov, buf, MIN(s->granularity, remaining)); 395402a4741SPaolo Bonzini } 396402a4741SPaolo Bonzini 397893f7ebaSPaolo Bonzini /* Copy the dirty cluster. */ 398bd48bde8SPaolo Bonzini s->in_flight++; 3992e1990b2SMax Reitz s->bytes_in_flight += op->bytes; 400ce8cabbdSKevin Wolf op->is_in_flight = true; 4012e1990b2SMax Reitz trace_mirror_one_iteration(s, op->offset, op->bytes); 402dcfb3bebSFam Zheng 403b9b10c35SKevin Wolf WITH_GRAPH_RDLOCK_GUARD() { 404138f9fffSMax Reitz ret = bdrv_co_preadv(s->mirror_top_bs->backing, op->offset, op->bytes, 405138f9fffSMax Reitz &op->qiov, 0); 406b9b10c35SKevin Wolf } 4072e1990b2SMax Reitz mirror_read_complete(op, ret); 408e5b43573SFam Zheng } 409e5b43573SFam Zheng 4102e1990b2SMax Reitz static void coroutine_fn mirror_co_zero(void *opaque) 411e5b43573SFam Zheng { 4122e1990b2SMax Reitz MirrorOp *op = opaque; 4132e1990b2SMax Reitz int ret; 414e5b43573SFam Zheng 4152e1990b2SMax Reitz op->s->in_flight++; 4162e1990b2SMax Reitz op->s->bytes_in_flight += op->bytes; 4172e1990b2SMax Reitz *op->bytes_handled = op->bytes; 418ce8cabbdSKevin Wolf op->is_in_flight = true; 419e5b43573SFam Zheng 4202e1990b2SMax Reitz ret = blk_co_pwrite_zeroes(op->s->target, op->offset, op->bytes, 4212e1990b2SMax Reitz op->s->unmap ? BDRV_REQ_MAY_UNMAP : 0); 4222e1990b2SMax Reitz mirror_write_complete(op, ret); 423e5b43573SFam Zheng } 4242e1990b2SMax Reitz 4252e1990b2SMax Reitz static void coroutine_fn mirror_co_discard(void *opaque) 4262e1990b2SMax Reitz { 4272e1990b2SMax Reitz MirrorOp *op = opaque; 4282e1990b2SMax Reitz int ret; 4292e1990b2SMax Reitz 4302e1990b2SMax Reitz op->s->in_flight++; 4312e1990b2SMax Reitz op->s->bytes_in_flight += op->bytes; 4322e1990b2SMax Reitz *op->bytes_handled = op->bytes; 433ce8cabbdSKevin Wolf op->is_in_flight = true; 4342e1990b2SMax Reitz 4352e1990b2SMax Reitz ret = blk_co_pdiscard(op->s->target, op->offset, op->bytes); 4362e1990b2SMax Reitz mirror_write_complete(op, ret); 437e5b43573SFam Zheng } 438e5b43573SFam Zheng 4394295c5fcSMax Reitz static unsigned mirror_perform(MirrorBlockJob *s, int64_t offset, 4404295c5fcSMax Reitz unsigned bytes, MirrorMethod mirror_method) 4414295c5fcSMax Reitz { 4422e1990b2SMax Reitz MirrorOp *op; 4432e1990b2SMax Reitz Coroutine *co; 4442e1990b2SMax Reitz int64_t bytes_handled = -1; 4452e1990b2SMax Reitz 4462e1990b2SMax Reitz op = g_new(MirrorOp, 1); 4472e1990b2SMax Reitz *op = (MirrorOp){ 4482e1990b2SMax Reitz .s = s, 4492e1990b2SMax Reitz .offset = offset, 4502e1990b2SMax Reitz .bytes = bytes, 4512e1990b2SMax Reitz .bytes_handled = &bytes_handled, 4522e1990b2SMax Reitz }; 45312aa4082SMax Reitz qemu_co_queue_init(&op->waiting_requests); 4542e1990b2SMax Reitz 4554295c5fcSMax Reitz switch (mirror_method) { 4564295c5fcSMax Reitz case MIRROR_METHOD_COPY: 4572e1990b2SMax Reitz co = qemu_coroutine_create(mirror_co_read, op); 4582e1990b2SMax Reitz break; 4594295c5fcSMax Reitz case MIRROR_METHOD_ZERO: 4602e1990b2SMax Reitz co = qemu_coroutine_create(mirror_co_zero, op); 4612e1990b2SMax Reitz break; 4624295c5fcSMax Reitz case MIRROR_METHOD_DISCARD: 4632e1990b2SMax Reitz co = qemu_coroutine_create(mirror_co_discard, op); 4642e1990b2SMax Reitz break; 4654295c5fcSMax Reitz default: 4664295c5fcSMax Reitz abort(); 4674295c5fcSMax Reitz } 468eed325b9SKevin Wolf op->co = co; 4692e1990b2SMax Reitz 47012aa4082SMax Reitz QTAILQ_INSERT_TAIL(&s->ops_in_flight, op, next); 4712e1990b2SMax Reitz qemu_coroutine_enter(co); 4722e1990b2SMax Reitz /* At this point, ownership of op has been moved to the coroutine 4732e1990b2SMax Reitz * and the object may already be freed */ 4742e1990b2SMax Reitz 4752e1990b2SMax Reitz /* Assert that this value has been set */ 4762e1990b2SMax Reitz assert(bytes_handled >= 0); 4772e1990b2SMax Reitz 4782e1990b2SMax Reitz /* Same assertion as in mirror_co_read() (and for mirror_co_read() 4792e1990b2SMax Reitz * and mirror_co_discard(), bytes_handled == op->bytes, which 4802e1990b2SMax Reitz * is the @bytes parameter given to this function) */ 4812e1990b2SMax Reitz assert(bytes_handled <= UINT_MAX); 4822e1990b2SMax Reitz return bytes_handled; 4834295c5fcSMax Reitz } 4844295c5fcSMax Reitz 485ae5a40e8SKevin Wolf static void coroutine_fn GRAPH_UNLOCKED mirror_iteration(MirrorBlockJob *s) 486e5b43573SFam Zheng { 487ae5a40e8SKevin Wolf BlockDriverState *source; 4881181e19aSMax Reitz MirrorOp *pseudo_op; 4891181e19aSMax Reitz int64_t offset; 490e5b43573SFam Zheng /* At least the first dirty chunk is mirrored in one iteration. */ 491e5b43573SFam Zheng int nb_chunks = 1; 4924b5004d9SDenis V. Lunev bool write_zeroes_ok = bdrv_can_write_zeroes_with_unmap(blk_bs(s->target)); 493b436982fSEric Blake int max_io_bytes = MAX(s->buf_size / MAX_IN_FLIGHT, MAX_IO_BYTES); 494e5b43573SFam Zheng 495ae5a40e8SKevin Wolf bdrv_graph_co_rdlock(); 496ae5a40e8SKevin Wolf source = s->mirror_top_bs->backing->bs; 497ae5a40e8SKevin Wolf bdrv_graph_co_rdunlock(); 498ae5a40e8SKevin Wolf 499b64bd51eSPaolo Bonzini bdrv_dirty_bitmap_lock(s->dirty_bitmap); 500f798184cSEric Blake offset = bdrv_dirty_iter_next(s->dbi); 501fb2ef791SEric Blake if (offset < 0) { 502dc162c8eSFam Zheng bdrv_set_dirty_iter(s->dbi, 0); 503f798184cSEric Blake offset = bdrv_dirty_iter_next(s->dbi); 5049a46dba7SEric Blake trace_mirror_restart_iter(s, bdrv_get_dirty_count(s->dirty_bitmap)); 505fb2ef791SEric Blake assert(offset >= 0); 506e5b43573SFam Zheng } 507b64bd51eSPaolo Bonzini bdrv_dirty_bitmap_unlock(s->dirty_bitmap); 508e5b43573SFam Zheng 509d69a879bSHanna Reitz /* 510d69a879bSHanna Reitz * Wait for concurrent requests to @offset. The next loop will limit the 511d69a879bSHanna Reitz * copied area based on in_flight_bitmap so we only copy an area that does 512d69a879bSHanna Reitz * not overlap with concurrent in-flight requests. Still, we would like to 513d69a879bSHanna Reitz * copy something, so wait until there are at least no more requests to the 514d69a879bSHanna Reitz * very beginning of the area. 515d69a879bSHanna Reitz */ 5161181e19aSMax Reitz mirror_wait_on_conflicts(NULL, s, offset, 1); 5179c83625bSMax Reitz 518da01ff7fSKevin Wolf job_pause_point(&s->common.job); 519565ac01fSStefan Hajnoczi 5203202d8e4SMichael Tokarev /* Find the number of consecutive dirty chunks following the first dirty 521e5b43573SFam Zheng * one, and wait for in flight requests in them. */ 522b64bd51eSPaolo Bonzini bdrv_dirty_bitmap_lock(s->dirty_bitmap); 523fb2ef791SEric Blake while (nb_chunks * s->granularity < s->buf_size) { 524dc162c8eSFam Zheng int64_t next_dirty; 525fb2ef791SEric Blake int64_t next_offset = offset + nb_chunks * s->granularity; 526fb2ef791SEric Blake int64_t next_chunk = next_offset / s->granularity; 527fb2ef791SEric Blake if (next_offset >= s->bdev_length || 52828636b82SJohn Snow !bdrv_dirty_bitmap_get_locked(s->dirty_bitmap, next_offset)) { 529e5b43573SFam Zheng break; 530e5b43573SFam Zheng } 531e5b43573SFam Zheng if (test_bit(next_chunk, s->in_flight_bitmap)) { 532e5b43573SFam Zheng break; 533e5b43573SFam Zheng } 5349c83625bSMax Reitz 535f798184cSEric Blake next_dirty = bdrv_dirty_iter_next(s->dbi); 536fb2ef791SEric Blake if (next_dirty > next_offset || next_dirty < 0) { 537f27a2742SMax Reitz /* The bitmap iterator's cache is stale, refresh it */ 538715a74d8SEric Blake bdrv_set_dirty_iter(s->dbi, next_offset); 539f798184cSEric Blake next_dirty = bdrv_dirty_iter_next(s->dbi); 540f27a2742SMax Reitz } 541fb2ef791SEric Blake assert(next_dirty == next_offset); 542e5b43573SFam Zheng nb_chunks++; 543e5b43573SFam Zheng } 544e5b43573SFam Zheng 545e5b43573SFam Zheng /* Clear dirty bits before querying the block status, because 54631826642SEric Blake * calling bdrv_block_status_above could yield - if some blocks are 547e5b43573SFam Zheng * marked dirty in this window, we need to know. 548e5b43573SFam Zheng */ 549e0d7f73eSEric Blake bdrv_reset_dirty_bitmap_locked(s->dirty_bitmap, offset, 550e0d7f73eSEric Blake nb_chunks * s->granularity); 551b64bd51eSPaolo Bonzini bdrv_dirty_bitmap_unlock(s->dirty_bitmap); 552b64bd51eSPaolo Bonzini 5531181e19aSMax Reitz /* Before claiming an area in the in-flight bitmap, we have to 5541181e19aSMax Reitz * create a MirrorOp for it so that conflicting requests can wait 5551181e19aSMax Reitz * for it. mirror_perform() will create the real MirrorOps later, 5561181e19aSMax Reitz * for now we just create a pseudo operation that will wake up all 5571181e19aSMax Reitz * conflicting requests once all real operations have been 5581181e19aSMax Reitz * launched. */ 5591181e19aSMax Reitz pseudo_op = g_new(MirrorOp, 1); 5601181e19aSMax Reitz *pseudo_op = (MirrorOp){ 5611181e19aSMax Reitz .offset = offset, 5621181e19aSMax Reitz .bytes = nb_chunks * s->granularity, 5631181e19aSMax Reitz .is_pseudo_op = true, 5641181e19aSMax Reitz }; 5651181e19aSMax Reitz qemu_co_queue_init(&pseudo_op->waiting_requests); 5661181e19aSMax Reitz QTAILQ_INSERT_TAIL(&s->ops_in_flight, pseudo_op, next); 5671181e19aSMax Reitz 568fb2ef791SEric Blake bitmap_set(s->in_flight_bitmap, offset / s->granularity, nb_chunks); 569fb2ef791SEric Blake while (nb_chunks > 0 && offset < s->bdev_length) { 5705791ba52SMarc-André Lureau int ret = -1; 5717cfd5275SEric Blake int64_t io_bytes; 572f3e4ce4aSEric Blake int64_t io_bytes_acct; 5734295c5fcSMax Reitz MirrorMethod mirror_method = MIRROR_METHOD_COPY; 574e5b43573SFam Zheng 575fb2ef791SEric Blake assert(!(offset % s->granularity)); 5767ff9579eSKevin Wolf WITH_GRAPH_RDLOCK_GUARD() { 577cc323997SPaolo Bonzini ret = bdrv_co_block_status_above(source, NULL, offset, 57831826642SEric Blake nb_chunks * s->granularity, 57931826642SEric Blake &io_bytes, NULL, NULL); 5807ff9579eSKevin Wolf } 581e5b43573SFam Zheng if (ret < 0) { 582fb2ef791SEric Blake io_bytes = MIN(nb_chunks * s->granularity, max_io_bytes); 5830965a41eSVladimir Sementsov-Ogievskiy } else if (ret & BDRV_BLOCK_DATA) { 584fb2ef791SEric Blake io_bytes = MIN(io_bytes, max_io_bytes); 585e5b43573SFam Zheng } 586e5b43573SFam Zheng 587fb2ef791SEric Blake io_bytes -= io_bytes % s->granularity; 588fb2ef791SEric Blake if (io_bytes < s->granularity) { 589fb2ef791SEric Blake io_bytes = s->granularity; 590e5b43573SFam Zheng } else if (ret >= 0 && !(ret & BDRV_BLOCK_DATA)) { 591fb2ef791SEric Blake int64_t target_offset; 5927cfd5275SEric Blake int64_t target_bytes; 593a00e70c0SEmanuele Giuseppe Esposito WITH_GRAPH_RDLOCK_GUARD() { 594fc6b211fSAndrey Drobyshev bdrv_round_to_subclusters(blk_bs(s->target), offset, io_bytes, 595fb2ef791SEric Blake &target_offset, &target_bytes); 596a00e70c0SEmanuele Giuseppe Esposito } 597fb2ef791SEric Blake if (target_offset == offset && 598fb2ef791SEric Blake target_bytes == io_bytes) { 599e5b43573SFam Zheng mirror_method = ret & BDRV_BLOCK_ZERO ? 600e5b43573SFam Zheng MIRROR_METHOD_ZERO : 601e5b43573SFam Zheng MIRROR_METHOD_DISCARD; 602e5b43573SFam Zheng } 603e5b43573SFam Zheng } 604e5b43573SFam Zheng 605cf56a3c6SDenis V. Lunev while (s->in_flight >= MAX_IN_FLIGHT) { 606fb2ef791SEric Blake trace_mirror_yield_in_flight(s, offset, s->in_flight); 6079178f4feSKevin Wolf mirror_wait_for_free_in_flight_slot(s); 608cf56a3c6SDenis V. Lunev } 609cf56a3c6SDenis V. Lunev 610dbaa7b57SVladimir Sementsov-Ogievskiy if (s->ret < 0) { 6111181e19aSMax Reitz ret = 0; 6121181e19aSMax Reitz goto fail; 613dbaa7b57SVladimir Sementsov-Ogievskiy } 614dbaa7b57SVladimir Sementsov-Ogievskiy 615fb2ef791SEric Blake io_bytes = mirror_clip_bytes(s, offset, io_bytes); 6164295c5fcSMax Reitz io_bytes = mirror_perform(s, offset, io_bytes, mirror_method); 6174295c5fcSMax Reitz if (mirror_method != MIRROR_METHOD_COPY && write_zeroes_ok) { 618f3e4ce4aSEric Blake io_bytes_acct = 0; 6194b5004d9SDenis V. Lunev } else { 620fb2ef791SEric Blake io_bytes_acct = io_bytes; 6214b5004d9SDenis V. Lunev } 622fb2ef791SEric Blake assert(io_bytes); 623fb2ef791SEric Blake offset += io_bytes; 624fb2ef791SEric Blake nb_chunks -= DIV_ROUND_UP(io_bytes, s->granularity); 625018e5987SKevin Wolf block_job_ratelimit_processed_bytes(&s->common, io_bytes_acct); 626dcfb3bebSFam Zheng } 6271181e19aSMax Reitz 6281181e19aSMax Reitz fail: 6291181e19aSMax Reitz QTAILQ_REMOVE(&s->ops_in_flight, pseudo_op, next); 6301181e19aSMax Reitz qemu_co_queue_restart_all(&pseudo_op->waiting_requests); 6311181e19aSMax Reitz g_free(pseudo_op); 632893f7ebaSPaolo Bonzini } 633b952b558SPaolo Bonzini 634402a4741SPaolo Bonzini static void mirror_free_init(MirrorBlockJob *s) 635402a4741SPaolo Bonzini { 636402a4741SPaolo Bonzini int granularity = s->granularity; 637402a4741SPaolo Bonzini size_t buf_size = s->buf_size; 638402a4741SPaolo Bonzini uint8_t *buf = s->buf; 639402a4741SPaolo Bonzini 640402a4741SPaolo Bonzini assert(s->buf_free_count == 0); 641402a4741SPaolo Bonzini QSIMPLEQ_INIT(&s->buf_free); 642402a4741SPaolo Bonzini while (buf_size != 0) { 643402a4741SPaolo Bonzini MirrorBuffer *cur = (MirrorBuffer *)buf; 644402a4741SPaolo Bonzini QSIMPLEQ_INSERT_TAIL(&s->buf_free, cur, next); 645402a4741SPaolo Bonzini s->buf_free_count++; 646402a4741SPaolo Bonzini buf_size -= granularity; 647402a4741SPaolo Bonzini buf += granularity; 648402a4741SPaolo Bonzini } 649402a4741SPaolo Bonzini } 650402a4741SPaolo Bonzini 651bae8196dSPaolo Bonzini /* This is also used for the .pause callback. There is no matching 652bae8196dSPaolo Bonzini * mirror_resume() because mirror_run() will begin iterating again 653bae8196dSPaolo Bonzini * when the job is resumed. 654bae8196dSPaolo Bonzini */ 655537c3d4fSStefan Hajnoczi static void coroutine_fn mirror_wait_for_all_io(MirrorBlockJob *s) 656bd48bde8SPaolo Bonzini { 657bd48bde8SPaolo Bonzini while (s->in_flight > 0) { 6589178f4feSKevin Wolf mirror_wait_for_free_in_flight_slot(s); 659bd48bde8SPaolo Bonzini } 660893f7ebaSPaolo Bonzini } 661893f7ebaSPaolo Bonzini 662737efc1eSJohn Snow /** 663737efc1eSJohn Snow * mirror_exit_common: handle both abort() and prepare() cases. 664737efc1eSJohn Snow * for .prepare, returns 0 on success and -errno on failure. 665737efc1eSJohn Snow * for .abort cases, denoted by abort = true, MUST return 0. 666737efc1eSJohn Snow */ 667737efc1eSJohn Snow static int mirror_exit_common(Job *job) 6685a7e7a0bSStefan Hajnoczi { 6691908a559SKevin Wolf MirrorBlockJob *s = container_of(job, MirrorBlockJob, common.job); 6701908a559SKevin Wolf BlockJob *bjob = &s->common; 671f93c3addSMax Reitz MirrorBDSOpaque *bs_opaque; 672f93c3addSMax Reitz BlockDriverState *src; 673f93c3addSMax Reitz BlockDriverState *target_bs; 674f93c3addSMax Reitz BlockDriverState *mirror_top_bs; 67512fa4af6SKevin Wolf Error *local_err = NULL; 676737efc1eSJohn Snow bool abort = job->ret < 0; 677737efc1eSJohn Snow int ret = 0; 678737efc1eSJohn Snow 6792626d27fSKevin Wolf GLOBAL_STATE_CODE(); 6802626d27fSKevin Wolf 681737efc1eSJohn Snow if (s->prepared) { 682737efc1eSJohn Snow return 0; 683737efc1eSJohn Snow } 684737efc1eSJohn Snow s->prepared = true; 6853f09bfbcSKevin Wolf 6869275fc72SKevin Wolf bdrv_graph_rdlock_main_loop(); 6872626d27fSKevin Wolf 688f93c3addSMax Reitz mirror_top_bs = s->mirror_top_bs; 689f93c3addSMax Reitz bs_opaque = mirror_top_bs->opaque; 690f93c3addSMax Reitz src = mirror_top_bs->backing->bs; 691f93c3addSMax Reitz target_bs = blk_bs(s->target); 692f93c3addSMax Reitz 693ef53dc09SAlberto Garcia if (bdrv_chain_contains(src, target_bs)) { 694ef53dc09SAlberto Garcia bdrv_unfreeze_backing_chain(mirror_top_bs, target_bs); 695ef53dc09SAlberto Garcia } 696ef53dc09SAlberto Garcia 6975deb6cbdSVladimir Sementsov-Ogievskiy bdrv_release_dirty_bitmap(s->dirty_bitmap); 6982119882cSPaolo Bonzini 6997b508f6bSJohn Snow /* Make sure that the source BDS doesn't go away during bdrv_replace_node, 7007b508f6bSJohn Snow * before we can call bdrv_drained_end */ 7013f09bfbcSKevin Wolf bdrv_ref(src); 7024ef85a9cSKevin Wolf bdrv_ref(mirror_top_bs); 7037d9fcb39SKevin Wolf bdrv_ref(target_bs); 7047d9fcb39SKevin Wolf 7059275fc72SKevin Wolf bdrv_graph_rdunlock_main_loop(); 7069275fc72SKevin Wolf 707bb0c9409SVladimir Sementsov-Ogievskiy /* 708bb0c9409SVladimir Sementsov-Ogievskiy * Remove target parent that still uses BLK_PERM_WRITE/RESIZE before 7097d9fcb39SKevin Wolf * inserting target_bs at s->to_replace, where we might not be able to get 71063c8ef28SKevin Wolf * these permissions. 711bb0c9409SVladimir Sementsov-Ogievskiy */ 7127d9fcb39SKevin Wolf blk_unref(s->target); 7137d9fcb39SKevin Wolf s->target = NULL; 7144ef85a9cSKevin Wolf 7154ef85a9cSKevin Wolf /* We don't access the source any more. Dropping any WRITE/RESIZE is 716d2da5e28SKevin Wolf * required before it could become a backing file of target_bs. Not having 717d2da5e28SKevin Wolf * these permissions any more means that we can't allow any new requests on 718d2da5e28SKevin Wolf * mirror_top_bs from now on, so keep it drained. */ 719d2da5e28SKevin Wolf bdrv_drained_begin(mirror_top_bs); 720ccd6a379SKevin Wolf bdrv_drained_begin(target_bs); 721f94dc3b4SMax Reitz bs_opaque->stop = true; 7223804e3cfSKevin Wolf 7233804e3cfSKevin Wolf bdrv_graph_rdlock_main_loop(); 724f94dc3b4SMax Reitz bdrv_child_refresh_perms(mirror_top_bs, mirror_top_bs->backing, 7254ef85a9cSKevin Wolf &error_abort); 7263804e3cfSKevin Wolf 727737efc1eSJohn Snow if (!abort && s->backing_mode == MIRROR_SOURCE_BACKING_CHAIN) { 7289474d97bSEric Blake BlockDriverState *backing; 7293f072a7fSMax Reitz BlockDriverState *unfiltered_target = bdrv_skip_filters(target_bs); 7303f072a7fSMax Reitz 7319474d97bSEric Blake backing = s->sync_mode == MIRROR_SYNC_MODE_NONE ? src : s->base; 7323f072a7fSMax Reitz if (bdrv_cow_bs(unfiltered_target) != backing) { 7333f072a7fSMax Reitz bdrv_set_backing_hd(unfiltered_target, backing, &local_err); 73412fa4af6SKevin Wolf if (local_err) { 73512fa4af6SKevin Wolf error_report_err(local_err); 73666c8672dSVladimir Sementsov-Ogievskiy local_err = NULL; 7377b508f6bSJohn Snow ret = -EPERM; 73812fa4af6SKevin Wolf } 7394ef85a9cSKevin Wolf } 740c41f5b96SMax Reitz } else if (!abort && s->backing_mode == MIRROR_OPEN_BACKING_CHAIN) { 741c41f5b96SMax Reitz assert(!bdrv_backing_chain_next(target_bs)); 742c41f5b96SMax Reitz ret = bdrv_open_backing_file(bdrv_skip_filters(target_bs), NULL, 743c41f5b96SMax Reitz "backing", &local_err); 744c41f5b96SMax Reitz if (ret < 0) { 745c41f5b96SMax Reitz error_report_err(local_err); 746c41f5b96SMax Reitz local_err = NULL; 747c41f5b96SMax Reitz } 7484ef85a9cSKevin Wolf } 749ad74751fSKevin Wolf bdrv_graph_rdunlock_main_loop(); 7505a7e7a0bSStefan Hajnoczi 751737efc1eSJohn Snow if (s->should_complete && !abort) { 752737efc1eSJohn Snow BlockDriverState *to_replace = s->to_replace ?: src; 7531ba79388SAlberto Garcia bool ro = bdrv_is_read_only(to_replace); 75440365552SKevin Wolf 7551ba79388SAlberto Garcia if (ro != bdrv_is_read_only(target_bs)) { 7561ba79388SAlberto Garcia bdrv_reopen_set_read_only(target_bs, ro, NULL); 7575a7e7a0bSStefan Hajnoczi } 758b8804815SKevin Wolf 759b8804815SKevin Wolf /* The mirror job has no requests in flight any more, but we need to 760b8804815SKevin Wolf * drain potential other users of the BDS before changing the graph. */ 7615e771752SSergio Lopez assert(s->in_drain); 762ccd6a379SKevin Wolf bdrv_drained_begin(to_replace); 7636e9cc051SMax Reitz /* 7646e9cc051SMax Reitz * Cannot use check_to_replace_node() here, because that would 7656e9cc051SMax Reitz * check for an op blocker on @to_replace, and we have our own 7666e9cc051SMax Reitz * there. 7676e9cc051SMax Reitz */ 7686bc30f19SStefan Hajnoczi bdrv_graph_wrlock(); 7696e9cc051SMax Reitz if (bdrv_recurse_can_replace(src, to_replace)) { 7705fe31c25SKevin Wolf bdrv_replace_node(to_replace, target_bs, &local_err); 7716e9cc051SMax Reitz } else { 7726e9cc051SMax Reitz error_setg(&local_err, "Can no longer replace '%s' by '%s', " 7736e9cc051SMax Reitz "because it can no longer be guaranteed that doing so " 7746e9cc051SMax Reitz "would not lead to an abrupt change of visible data", 7756e9cc051SMax Reitz to_replace->node_name, target_bs->node_name); 7766e9cc051SMax Reitz } 7776bc30f19SStefan Hajnoczi bdrv_graph_wrunlock(); 778ccd6a379SKevin Wolf bdrv_drained_end(to_replace); 7795fe31c25SKevin Wolf if (local_err) { 7805fe31c25SKevin Wolf error_report_err(local_err); 7817b508f6bSJohn Snow ret = -EPERM; 7825fe31c25SKevin Wolf } 7835a7e7a0bSStefan Hajnoczi } 7845a7e7a0bSStefan Hajnoczi if (s->to_replace) { 7855a7e7a0bSStefan Hajnoczi bdrv_op_unblock_all(s->to_replace, s->replace_blocker); 7865a7e7a0bSStefan Hajnoczi error_free(s->replace_blocker); 7875a7e7a0bSStefan Hajnoczi bdrv_unref(s->to_replace); 7885a7e7a0bSStefan Hajnoczi } 7895a7e7a0bSStefan Hajnoczi g_free(s->replaces); 7904ef85a9cSKevin Wolf 791f94dc3b4SMax Reitz /* 792f94dc3b4SMax Reitz * Remove the mirror filter driver from the graph. Before this, get rid of 7934ef85a9cSKevin Wolf * the blockers on the intermediate nodes so that the resulting state is 794f94dc3b4SMax Reitz * valid. 795f94dc3b4SMax Reitz */ 7961908a559SKevin Wolf block_job_remove_all_bdrv(bjob); 7976bc30f19SStefan Hajnoczi bdrv_graph_wrlock(); 7983f072a7fSMax Reitz bdrv_replace_node(mirror_top_bs, mirror_top_bs->backing->bs, &error_abort); 7996bc30f19SStefan Hajnoczi bdrv_graph_wrunlock(); 800ccd6a379SKevin Wolf 8017d99ae59SAlexander Ivanov if (abort && s->base_ro && !bdrv_is_read_only(target_bs)) { 8027d99ae59SAlexander Ivanov bdrv_reopen_set_read_only(target_bs, true, NULL); 8037d99ae59SAlexander Ivanov } 8047d99ae59SAlexander Ivanov 805ccd6a379SKevin Wolf bdrv_drained_end(target_bs); 806ccd6a379SKevin Wolf bdrv_unref(target_bs); 8074ef85a9cSKevin Wolf 808429076e8SMax Reitz bs_opaque->job = NULL; 8094ef85a9cSKevin Wolf 810176c3699SFam Zheng bdrv_drained_end(src); 811d2da5e28SKevin Wolf bdrv_drained_end(mirror_top_bs); 8125e771752SSergio Lopez s->in_drain = false; 8134ef85a9cSKevin Wolf bdrv_unref(mirror_top_bs); 8143f09bfbcSKevin Wolf bdrv_unref(src); 8157b508f6bSJohn Snow 816737efc1eSJohn Snow return ret; 817737efc1eSJohn Snow } 818737efc1eSJohn Snow 819737efc1eSJohn Snow static int mirror_prepare(Job *job) 820737efc1eSJohn Snow { 821737efc1eSJohn Snow return mirror_exit_common(job); 822737efc1eSJohn Snow } 823737efc1eSJohn Snow 824737efc1eSJohn Snow static void mirror_abort(Job *job) 825737efc1eSJohn Snow { 826737efc1eSJohn Snow int ret = mirror_exit_common(job); 827737efc1eSJohn Snow assert(ret == 0); 8285a7e7a0bSStefan Hajnoczi } 8295a7e7a0bSStefan Hajnoczi 830537c3d4fSStefan Hajnoczi static void coroutine_fn mirror_throttle(MirrorBlockJob *s) 83149efb1f5SDenis V. Lunev { 83249efb1f5SDenis V. Lunev int64_t now = qemu_clock_get_ns(QEMU_CLOCK_REALTIME); 83349efb1f5SDenis V. Lunev 83418bb6928SKevin Wolf if (now - s->last_pause_ns > BLOCK_JOB_SLICE_TIME) { 83549efb1f5SDenis V. Lunev s->last_pause_ns = now; 8365d43e86eSKevin Wolf job_sleep_ns(&s->common.job, 0); 83749efb1f5SDenis V. Lunev } else { 838da01ff7fSKevin Wolf job_pause_point(&s->common.job); 83949efb1f5SDenis V. Lunev } 84049efb1f5SDenis V. Lunev } 84149efb1f5SDenis V. Lunev 842004915a9SKevin Wolf static int coroutine_fn GRAPH_UNLOCKED mirror_dirty_init(MirrorBlockJob *s) 843c0b363adSDenis V. Lunev { 84423ca459aSEric Blake int64_t offset; 845004915a9SKevin Wolf BlockDriverState *bs; 846c0b363adSDenis V. Lunev BlockDriverState *target_bs = blk_bs(s->target); 847870f8963SEric Blake int ret = -EIO; 84851b0a488SEric Blake int64_t count; 849*d17a34bfSEric Blake bool punch_holes = 850*d17a34bfSEric Blake target_bs->detect_zeroes == BLOCKDEV_DETECT_ZEROES_OPTIONS_UNMAP && 851*d17a34bfSEric Blake bdrv_can_write_zeroes_with_unmap(target_bs); 852c0b363adSDenis V. Lunev 853004915a9SKevin Wolf bdrv_graph_co_rdlock(); 854004915a9SKevin Wolf bs = s->mirror_top_bs->backing->bs; 855004915a9SKevin Wolf bdrv_graph_co_rdunlock(); 856004915a9SKevin Wolf 857*d17a34bfSEric Blake if (s->zero_target && (!s->target_is_zero || punch_holes)) { 858*d17a34bfSEric Blake /* 859*d17a34bfSEric Blake * Here, we are in FULL mode; our goal is to avoid writing 860*d17a34bfSEric Blake * zeroes if the destination already reads as zero, except 861*d17a34bfSEric Blake * when we are trying to punch holes. This is possible if 862*d17a34bfSEric Blake * zeroing happened externally (s->target_is_zero) or if we 863*d17a34bfSEric Blake * have a fast way to pre-zero the image (the dirty bitmap 864*d17a34bfSEric Blake * will be populated later by the non-zero portions, the same 865*d17a34bfSEric Blake * as for TOP mode). If pre-zeroing is not fast, or we need 866*d17a34bfSEric Blake * to punch holes, then our only recourse is to write the 867*d17a34bfSEric Blake * entire image. 868*d17a34bfSEric Blake */ 869c7c2769cSDenis V. Lunev if (!bdrv_can_write_zeroes_with_unmap(target_bs)) { 870e0d7f73eSEric Blake bdrv_set_dirty_bitmap(s->dirty_bitmap, 0, s->bdev_length); 871b7d5062cSDenis V. Lunev return 0; 872b7d5062cSDenis V. Lunev } 873b7d5062cSDenis V. Lunev 87490ab48ebSAnton Nefedov s->initial_zeroing_ongoing = true; 87523ca459aSEric Blake for (offset = 0; offset < s->bdev_length; ) { 87623ca459aSEric Blake int bytes = MIN(s->bdev_length - offset, 87723ca459aSEric Blake QEMU_ALIGN_DOWN(INT_MAX, s->granularity)); 878c7c2769cSDenis V. Lunev 879c7c2769cSDenis V. Lunev mirror_throttle(s); 880c7c2769cSDenis V. Lunev 881daa7f2f9SKevin Wolf if (job_is_cancelled(&s->common.job)) { 88290ab48ebSAnton Nefedov s->initial_zeroing_ongoing = false; 883c7c2769cSDenis V. Lunev return 0; 884c7c2769cSDenis V. Lunev } 885c7c2769cSDenis V. Lunev 886c7c2769cSDenis V. Lunev if (s->in_flight >= MAX_IN_FLIGHT) { 88767adf4b3SEric Blake trace_mirror_yield(s, UINT64_MAX, s->buf_free_count, 88867adf4b3SEric Blake s->in_flight); 8899178f4feSKevin Wolf mirror_wait_for_free_in_flight_slot(s); 890c7c2769cSDenis V. Lunev continue; 891c7c2769cSDenis V. Lunev } 892c7c2769cSDenis V. Lunev 8934295c5fcSMax Reitz mirror_perform(s, offset, bytes, MIRROR_METHOD_ZERO); 89423ca459aSEric Blake offset += bytes; 895c7c2769cSDenis V. Lunev } 896c7c2769cSDenis V. Lunev 897bae8196dSPaolo Bonzini mirror_wait_for_all_io(s); 89890ab48ebSAnton Nefedov s->initial_zeroing_ongoing = false; 899c7c2769cSDenis V. Lunev } 900c7c2769cSDenis V. Lunev 901c0b363adSDenis V. Lunev /* First part, loop on the sectors and initialize the dirty bitmap. */ 90223ca459aSEric Blake for (offset = 0; offset < s->bdev_length; ) { 903c0b363adSDenis V. Lunev /* Just to make sure we are not exceeding int limit. */ 90423ca459aSEric Blake int bytes = MIN(s->bdev_length - offset, 90523ca459aSEric Blake QEMU_ALIGN_DOWN(INT_MAX, s->granularity)); 906c0b363adSDenis V. Lunev 907c0b363adSDenis V. Lunev mirror_throttle(s); 908c0b363adSDenis V. Lunev 909daa7f2f9SKevin Wolf if (job_is_cancelled(&s->common.job)) { 910c0b363adSDenis V. Lunev return 0; 911c0b363adSDenis V. Lunev } 912c0b363adSDenis V. Lunev 9137ff9579eSKevin Wolf WITH_GRAPH_RDLOCK_GUARD() { 914cc323997SPaolo Bonzini ret = bdrv_co_is_allocated_above(bs, s->base_overlay, true, offset, 9157ff9579eSKevin Wolf bytes, &count); 9167ff9579eSKevin Wolf } 917c0b363adSDenis V. Lunev if (ret < 0) { 918c0b363adSDenis V. Lunev return ret; 919c0b363adSDenis V. Lunev } 920c0b363adSDenis V. Lunev 92123ca459aSEric Blake assert(count); 922a92b1b06SEric Blake if (ret > 0) { 92323ca459aSEric Blake bdrv_set_dirty_bitmap(s->dirty_bitmap, offset, count); 924c0b363adSDenis V. Lunev } 92523ca459aSEric Blake offset += count; 926c0b363adSDenis V. Lunev } 927c0b363adSDenis V. Lunev return 0; 928c0b363adSDenis V. Lunev } 929c0b363adSDenis V. Lunev 930bdffb31dSPaolo Bonzini /* Called when going out of the streaming phase to flush the bulk of the 931bdffb31dSPaolo Bonzini * data to the medium, or just before completing. 932bdffb31dSPaolo Bonzini */ 93326bef102SPaolo Bonzini static int coroutine_fn mirror_flush(MirrorBlockJob *s) 934bdffb31dSPaolo Bonzini { 93526bef102SPaolo Bonzini int ret = blk_co_flush(s->target); 936bdffb31dSPaolo Bonzini if (ret < 0) { 937bdffb31dSPaolo Bonzini if (mirror_error_action(s, false, -ret) == BLOCK_ERROR_ACTION_REPORT) { 938bdffb31dSPaolo Bonzini s->ret = ret; 939bdffb31dSPaolo Bonzini } 940bdffb31dSPaolo Bonzini } 941bdffb31dSPaolo Bonzini return ret; 942bdffb31dSPaolo Bonzini } 943bdffb31dSPaolo Bonzini 944f67432a2SJohn Snow static int coroutine_fn mirror_run(Job *job, Error **errp) 945893f7ebaSPaolo Bonzini { 946f67432a2SJohn Snow MirrorBlockJob *s = container_of(job, MirrorBlockJob, common.job); 947004915a9SKevin Wolf BlockDriverState *bs; 94832125b14SKevin Wolf MirrorBDSOpaque *mirror_top_opaque = s->mirror_top_bs->opaque; 949e253f4b8SKevin Wolf BlockDriverState *target_bs = blk_bs(s->target); 9509a0cec66SPaolo Bonzini bool need_drain = true; 951ba11c88dSMarc-André Lureau BlockDeviceIoStatus iostatus = BLOCK_DEVICE_IO_STATUS__MAX; 952c0b363adSDenis V. Lunev int64_t length; 953e83dd680SKevin Wolf int64_t target_length; 954b812f671SPaolo Bonzini BlockDriverInfo bdi; 9551d33936eSJeff Cody char backing_filename[2]; /* we only need 2 characters because we are only 9561d33936eSJeff Cody checking for a NULL string */ 957893f7ebaSPaolo Bonzini int ret = 0; 958893f7ebaSPaolo Bonzini 959004915a9SKevin Wolf bdrv_graph_co_rdlock(); 960004915a9SKevin Wolf bs = bdrv_filter_bs(s->mirror_top_bs); 961004915a9SKevin Wolf bdrv_graph_co_rdunlock(); 962004915a9SKevin Wolf 963daa7f2f9SKevin Wolf if (job_is_cancelled(&s->common.job)) { 964893f7ebaSPaolo Bonzini goto immediate_exit; 965893f7ebaSPaolo Bonzini } 966893f7ebaSPaolo Bonzini 9678ab8140aSKevin Wolf bdrv_graph_co_rdlock(); 968c86422c5SEmanuele Giuseppe Esposito s->bdev_length = bdrv_co_getlength(bs); 9698ab8140aSKevin Wolf bdrv_graph_co_rdunlock(); 9708ab8140aSKevin Wolf 971b21c7652SMax Reitz if (s->bdev_length < 0) { 972b21c7652SMax Reitz ret = s->bdev_length; 973373df5b1SFam Zheng goto immediate_exit; 974becc347eSKevin Wolf } 975becc347eSKevin Wolf 976c86422c5SEmanuele Giuseppe Esposito target_length = blk_co_getlength(s->target); 977e83dd680SKevin Wolf if (target_length < 0) { 978e83dd680SKevin Wolf ret = target_length; 979becc347eSKevin Wolf goto immediate_exit; 980becc347eSKevin Wolf } 981becc347eSKevin Wolf 982e83dd680SKevin Wolf /* Active commit must resize the base image if its size differs from the 983e83dd680SKevin Wolf * active layer. */ 984e83dd680SKevin Wolf if (s->base == blk_bs(s->target)) { 985e83dd680SKevin Wolf if (s->bdev_length > target_length) { 98688276216SAlberto Faria ret = blk_co_truncate(s->target, s->bdev_length, false, 9878c6242b6SKevin Wolf PREALLOC_MODE_OFF, 0, NULL); 988becc347eSKevin Wolf if (ret < 0) { 989becc347eSKevin Wolf goto immediate_exit; 990becc347eSKevin Wolf } 991becc347eSKevin Wolf } 992e83dd680SKevin Wolf } else if (s->bdev_length != target_length) { 993e83dd680SKevin Wolf error_setg(errp, "Source and target image have different sizes"); 994e83dd680SKevin Wolf ret = -EINVAL; 995e83dd680SKevin Wolf goto immediate_exit; 996becc347eSKevin Wolf } 997becc347eSKevin Wolf 998becc347eSKevin Wolf if (s->bdev_length == 0) { 9992e1795b5SKevin Wolf /* Transition to the READY state and wait for complete. */ 10002e1795b5SKevin Wolf job_transition_to_ready(&s->common.job); 100176cb2f24SFiona Ebner qatomic_set(&s->actively_synced, true); 100208b83bffSHanna Reitz while (!job_cancel_requested(&s->common.job) && !s->should_complete) { 1003198c49ccSKevin Wolf job_yield(&s->common.job); 10049e48b025SFam Zheng } 10059e48b025SFam Zheng goto immediate_exit; 1006893f7ebaSPaolo Bonzini } 1007893f7ebaSPaolo Bonzini 1008b21c7652SMax Reitz length = DIV_ROUND_UP(s->bdev_length, s->granularity); 1009402a4741SPaolo Bonzini s->in_flight_bitmap = bitmap_new(length); 1010402a4741SPaolo Bonzini 1011b812f671SPaolo Bonzini /* If we have no backing file yet in the destination, we cannot let 1012b812f671SPaolo Bonzini * the destination do COW. Instead, we copy sectors around the 1013b812f671SPaolo Bonzini * dirty data if needed. We need a bitmap to do that. 1014b812f671SPaolo Bonzini */ 1015e253f4b8SKevin Wolf bdrv_get_backing_filename(target_bs, backing_filename, 1016b812f671SPaolo Bonzini sizeof(backing_filename)); 1017a00e70c0SEmanuele Giuseppe Esposito bdrv_graph_co_rdlock(); 10183d47eb0aSEmanuele Giuseppe Esposito if (!bdrv_co_get_info(target_bs, &bdi) && bdi.cluster_size) { 1019b436982fSEric Blake s->target_cluster_size = bdi.cluster_size; 1020b436982fSEric Blake } else { 1021b436982fSEric Blake s->target_cluster_size = BDRV_SECTOR_SIZE; 1022c3cc95bdSFam Zheng } 10233f072a7fSMax Reitz if (backing_filename[0] && !bdrv_backing_chain_next(target_bs) && 1024b436982fSEric Blake s->granularity < s->target_cluster_size) { 1025b436982fSEric Blake s->buf_size = MAX(s->buf_size, s->target_cluster_size); 1026b812f671SPaolo Bonzini s->cow_bitmap = bitmap_new(length); 1027b812f671SPaolo Bonzini } 1028e253f4b8SKevin Wolf s->max_iov = MIN(bs->bl.max_iov, target_bs->bl.max_iov); 1029ad74751fSKevin Wolf bdrv_graph_co_rdunlock(); 1030b812f671SPaolo Bonzini 10317504edf4SKevin Wolf s->buf = qemu_try_blockalign(bs, s->buf_size); 10327504edf4SKevin Wolf if (s->buf == NULL) { 10337504edf4SKevin Wolf ret = -ENOMEM; 10347504edf4SKevin Wolf goto immediate_exit; 10357504edf4SKevin Wolf } 10367504edf4SKevin Wolf 1037402a4741SPaolo Bonzini mirror_free_init(s); 1038893f7ebaSPaolo Bonzini 103949efb1f5SDenis V. Lunev s->last_pause_ns = qemu_clock_get_ns(QEMU_CLOCK_REALTIME); 10409474d97bSEric Blake if (s->sync_mode != MIRROR_SYNC_MODE_NONE) { 1041c0b363adSDenis V. Lunev ret = mirror_dirty_init(s); 1042daa7f2f9SKevin Wolf if (ret < 0 || job_is_cancelled(&s->common.job)) { 10434c0cbd6fSFam Zheng goto immediate_exit; 10444c0cbd6fSFam Zheng } 1045893f7ebaSPaolo Bonzini } 1046893f7ebaSPaolo Bonzini 104732125b14SKevin Wolf /* 104832125b14SKevin Wolf * Only now the job is fully initialised and mirror_top_bs should start 104932125b14SKevin Wolf * accessing it. 105032125b14SKevin Wolf */ 105132125b14SKevin Wolf mirror_top_opaque->job = s; 105232125b14SKevin Wolf 1053dc162c8eSFam Zheng assert(!s->dbi); 1054715a74d8SEric Blake s->dbi = bdrv_dirty_iter_new(s->dirty_bitmap); 1055893f7ebaSPaolo Bonzini for (;;) { 105649efb1f5SDenis V. Lunev int64_t cnt, delta; 1057893f7ebaSPaolo Bonzini bool should_complete; 1058893f7ebaSPaolo Bonzini 1059bd48bde8SPaolo Bonzini if (s->ret < 0) { 1060bd48bde8SPaolo Bonzini ret = s->ret; 1061893f7ebaSPaolo Bonzini goto immediate_exit; 1062893f7ebaSPaolo Bonzini } 1063bd48bde8SPaolo Bonzini 1064da01ff7fSKevin Wolf job_pause_point(&s->common.job); 1065565ac01fSStefan Hajnoczi 10664feeec7eSHanna Reitz if (job_is_cancelled(&s->common.job)) { 10674feeec7eSHanna Reitz ret = 0; 10684feeec7eSHanna Reitz goto immediate_exit; 10694feeec7eSHanna Reitz } 10704feeec7eSHanna Reitz 107120dca810SJohn Snow cnt = bdrv_get_dirty_count(s->dirty_bitmap); 107205df8a6aSKevin Wolf /* cnt is the number of dirty bytes remaining and s->bytes_in_flight is 107305df8a6aSKevin Wolf * the number of bytes currently being processed; together those are 107405df8a6aSKevin Wolf * the current remaining operation length */ 1075d69a879bSHanna Reitz job_progress_set_remaining(&s->common.job, 1076d69a879bSHanna Reitz s->bytes_in_flight + cnt + 1077d69a879bSHanna Reitz s->active_write_bytes_in_flight); 1078bd48bde8SPaolo Bonzini 1079bd48bde8SPaolo Bonzini /* Note that even when no rate limit is applied we need to yield 1080a7282330SFam Zheng * periodically with no pending I/O so that bdrv_drain_all() returns. 108118bb6928SKevin Wolf * We do so every BLKOCK_JOB_SLICE_TIME nanoseconds, or when there is 108218bb6928SKevin Wolf * an error, or when the source is clean, whichever comes first. */ 108349efb1f5SDenis V. Lunev delta = qemu_clock_get_ns(QEMU_CLOCK_REALTIME) - s->last_pause_ns; 1084d59cb66dSEmanuele Giuseppe Esposito WITH_JOB_LOCK_GUARD() { 1085d59cb66dSEmanuele Giuseppe Esposito iostatus = s->common.iostatus; 1086d59cb66dSEmanuele Giuseppe Esposito } 108718bb6928SKevin Wolf if (delta < BLOCK_JOB_SLICE_TIME && 1088d59cb66dSEmanuele Giuseppe Esposito iostatus == BLOCK_DEVICE_IO_STATUS_OK) { 1089cf56a3c6SDenis V. Lunev if (s->in_flight >= MAX_IN_FLIGHT || s->buf_free_count == 0 || 1090402a4741SPaolo Bonzini (cnt == 0 && s->in_flight > 0)) { 10919a46dba7SEric Blake trace_mirror_yield(s, cnt, s->buf_free_count, s->in_flight); 10929178f4feSKevin Wolf mirror_wait_for_free_in_flight_slot(s); 1093bd48bde8SPaolo Bonzini continue; 1094bd48bde8SPaolo Bonzini } else if (cnt != 0) { 1095018e5987SKevin Wolf mirror_iteration(s); 1096893f7ebaSPaolo Bonzini } 1097cc8c9d6cSPaolo Bonzini } 1098893f7ebaSPaolo Bonzini 1099893f7ebaSPaolo Bonzini should_complete = false; 1100bd48bde8SPaolo Bonzini if (s->in_flight == 0 && cnt == 0) { 1101893f7ebaSPaolo Bonzini trace_mirror_before_flush(s); 110244716224SHanna Reitz if (!job_is_ready(&s->common.job)) { 1103bdffb31dSPaolo Bonzini if (mirror_flush(s) < 0) { 1104bdffb31dSPaolo Bonzini /* Go check s->ret. */ 1105bdffb31dSPaolo Bonzini continue; 1106893f7ebaSPaolo Bonzini } 1107893f7ebaSPaolo Bonzini /* We're out of the streaming phase. From now on, if the job 1108893f7ebaSPaolo Bonzini * is cancelled we will actually complete all pending I/O and 1109893f7ebaSPaolo Bonzini * report completion. This way, block-job-cancel will leave 1110893f7ebaSPaolo Bonzini * the target in a consistent state. 1111893f7ebaSPaolo Bonzini */ 11122e1795b5SKevin Wolf job_transition_to_ready(&s->common.job); 1113c45d0e1aSFiona Ebner } 11142d400d15SFiona Ebner if (qatomic_read(&s->copy_mode) != MIRROR_COPY_MODE_BACKGROUND) { 111576cb2f24SFiona Ebner qatomic_set(&s->actively_synced, true); 1116d06107adSMax Reitz } 1117d63ffd87SPaolo Bonzini 1118d63ffd87SPaolo Bonzini should_complete = s->should_complete || 111908b83bffSHanna Reitz job_cancel_requested(&s->common.job); 112020dca810SJohn Snow cnt = bdrv_get_dirty_count(s->dirty_bitmap); 1121893f7ebaSPaolo Bonzini } 1122893f7ebaSPaolo Bonzini 1123893f7ebaSPaolo Bonzini if (cnt == 0 && should_complete) { 1124893f7ebaSPaolo Bonzini /* The dirty bitmap is not updated while operations are pending. 1125893f7ebaSPaolo Bonzini * If we're about to exit, wait for pending operations before 1126893f7ebaSPaolo Bonzini * calling bdrv_get_dirty_count(bs), or we may exit while the 1127893f7ebaSPaolo Bonzini * source has dirty data to copy! 1128893f7ebaSPaolo Bonzini * 1129893f7ebaSPaolo Bonzini * Note that I/O can be submitted by the guest while 11309a0cec66SPaolo Bonzini * mirror_populate runs, so pause it now. Before deciding 11319a0cec66SPaolo Bonzini * whether to switch to target check one last time if I/O has 11329a0cec66SPaolo Bonzini * come in the meanwhile, and if not flush the data to disk. 1133893f7ebaSPaolo Bonzini */ 11349a46dba7SEric Blake trace_mirror_before_drain(s, cnt); 11359a0cec66SPaolo Bonzini 11365e771752SSergio Lopez s->in_drain = true; 11379a0cec66SPaolo Bonzini bdrv_drained_begin(bs); 1138d69a879bSHanna Reitz 1139d69a879bSHanna Reitz /* Must be zero because we are drained */ 1140d69a879bSHanna Reitz assert(s->in_active_write_counter == 0); 1141d69a879bSHanna Reitz 114220dca810SJohn Snow cnt = bdrv_get_dirty_count(s->dirty_bitmap); 1143bdffb31dSPaolo Bonzini if (cnt > 0 || mirror_flush(s) < 0) { 11449a0cec66SPaolo Bonzini bdrv_drained_end(bs); 11455e771752SSergio Lopez s->in_drain = false; 11469a0cec66SPaolo Bonzini continue; 11479a0cec66SPaolo Bonzini } 11489a0cec66SPaolo Bonzini 11499a0cec66SPaolo Bonzini /* The two disks are in sync. Exit and report successful 11509a0cec66SPaolo Bonzini * completion. 11519a0cec66SPaolo Bonzini */ 11529a0cec66SPaolo Bonzini assert(QLIST_EMPTY(&bs->tracked_requests)); 11539a0cec66SPaolo Bonzini need_drain = false; 11549a0cec66SPaolo Bonzini break; 1155893f7ebaSPaolo Bonzini } 1156893f7ebaSPaolo Bonzini 115744716224SHanna Reitz if (job_is_ready(&s->common.job) && !should_complete) { 1158018e5987SKevin Wolf if (s->in_flight == 0 && cnt == 0) { 115944716224SHanna Reitz trace_mirror_before_sleep(s, cnt, job_is_ready(&s->common.job), 1160018e5987SKevin Wolf BLOCK_JOB_SLICE_TIME); 1161018e5987SKevin Wolf job_sleep_ns(&s->common.job, BLOCK_JOB_SLICE_TIME); 1162018e5987SKevin Wolf } 1163018e5987SKevin Wolf } else { 1164018e5987SKevin Wolf block_job_ratelimit_sleep(&s->common); 1165018e5987SKevin Wolf } 116649efb1f5SDenis V. Lunev s->last_pause_ns = qemu_clock_get_ns(QEMU_CLOCK_REALTIME); 1167893f7ebaSPaolo Bonzini } 1168893f7ebaSPaolo Bonzini 1169893f7ebaSPaolo Bonzini immediate_exit: 1170bd48bde8SPaolo Bonzini if (s->in_flight > 0) { 1171bd48bde8SPaolo Bonzini /* We get here only if something went wrong. Either the job failed, 1172bd48bde8SPaolo Bonzini * or it was cancelled prematurely so that we do not guarantee that 1173bd48bde8SPaolo Bonzini * the target is a copy of the source. 1174bd48bde8SPaolo Bonzini */ 117508b83bffSHanna Reitz assert(ret < 0 || job_is_cancelled(&s->common.job)); 11769a0cec66SPaolo Bonzini assert(need_drain); 1177bae8196dSPaolo Bonzini mirror_wait_for_all_io(s); 1178bd48bde8SPaolo Bonzini } 1179bd48bde8SPaolo Bonzini 1180bd48bde8SPaolo Bonzini assert(s->in_flight == 0); 11817191bf31SMarkus Armbruster qemu_vfree(s->buf); 1182b812f671SPaolo Bonzini g_free(s->cow_bitmap); 1183402a4741SPaolo Bonzini g_free(s->in_flight_bitmap); 1184dc162c8eSFam Zheng bdrv_dirty_iter_free(s->dbi); 11855a7e7a0bSStefan Hajnoczi 11869a0cec66SPaolo Bonzini if (need_drain) { 11875e771752SSergio Lopez s->in_drain = true; 1188e253f4b8SKevin Wolf bdrv_drained_begin(bs); 11899a0cec66SPaolo Bonzini } 1190f67432a2SJohn Snow 1191f67432a2SJohn Snow return ret; 1192893f7ebaSPaolo Bonzini } 1193893f7ebaSPaolo Bonzini 11943453d972SKevin Wolf static void mirror_complete(Job *job, Error **errp) 1195d63ffd87SPaolo Bonzini { 11963453d972SKevin Wolf MirrorBlockJob *s = container_of(job, MirrorBlockJob, common.job); 1197274fcceeSMax Reitz 119844716224SHanna Reitz if (!job_is_ready(job)) { 11999df229c3SAlberto Garcia error_setg(errp, "The active block job '%s' cannot be completed", 12003453d972SKevin Wolf job->id); 1201d63ffd87SPaolo Bonzini return; 1202d63ffd87SPaolo Bonzini } 1203d63ffd87SPaolo Bonzini 120415d67298SChanglong Xie /* block all operations on to_replace bs */ 120509158f00SBenoît Canet if (s->replaces) { 1206e12f3784SWen Congyang s->to_replace = bdrv_find_node(s->replaces); 120709158f00SBenoît Canet if (!s->to_replace) { 1208e12f3784SWen Congyang error_setg(errp, "Node name '%s' not found", s->replaces); 120909158f00SBenoît Canet return; 121009158f00SBenoît Canet } 121109158f00SBenoît Canet 121264631f36SVladimir Sementsov-Ogievskiy /* TODO Translate this into child freeze system. */ 121309158f00SBenoît Canet error_setg(&s->replace_blocker, 121409158f00SBenoît Canet "block device is in use by block-job-complete"); 121509158f00SBenoît Canet bdrv_op_block_all(s->to_replace, s->replace_blocker); 121609158f00SBenoît Canet bdrv_ref(s->to_replace); 121709158f00SBenoît Canet } 121809158f00SBenoît Canet 1219d63ffd87SPaolo Bonzini s->should_complete = true; 122000769414SMax Reitz 122100769414SMax Reitz /* If the job is paused, it will be re-entered when it is resumed */ 1222279ac06eSEmanuele Giuseppe Esposito WITH_JOB_LOCK_GUARD() { 122300769414SMax Reitz if (!job->paused) { 1224279ac06eSEmanuele Giuseppe Esposito job_enter_cond_locked(job, NULL); 1225279ac06eSEmanuele Giuseppe Esposito } 1226d63ffd87SPaolo Bonzini } 122700769414SMax Reitz } 1228d63ffd87SPaolo Bonzini 1229537c3d4fSStefan Hajnoczi static void coroutine_fn mirror_pause(Job *job) 1230565ac01fSStefan Hajnoczi { 1231da01ff7fSKevin Wolf MirrorBlockJob *s = container_of(job, MirrorBlockJob, common.job); 1232565ac01fSStefan Hajnoczi 1233bae8196dSPaolo Bonzini mirror_wait_for_all_io(s); 1234565ac01fSStefan Hajnoczi } 1235565ac01fSStefan Hajnoczi 123689bd0305SKevin Wolf static bool mirror_drained_poll(BlockJob *job) 123789bd0305SKevin Wolf { 123889bd0305SKevin Wolf MirrorBlockJob *s = container_of(job, MirrorBlockJob, common); 12395e771752SSergio Lopez 12405e771752SSergio Lopez /* If the job isn't paused nor cancelled, we can't be sure that it won't 12415e771752SSergio Lopez * issue more requests. We make an exception if we've reached this point 12425e771752SSergio Lopez * from one of our own drain sections, to avoid a deadlock waiting for 12435e771752SSergio Lopez * ourselves. 12445e771752SSergio Lopez */ 1245279ac06eSEmanuele Giuseppe Esposito WITH_JOB_LOCK_GUARD() { 1246279ac06eSEmanuele Giuseppe Esposito if (!s->common.job.paused && !job_is_cancelled_locked(&job->job) 1247279ac06eSEmanuele Giuseppe Esposito && !s->in_drain) { 12485e771752SSergio Lopez return true; 12495e771752SSergio Lopez } 1250279ac06eSEmanuele Giuseppe Esposito } 12515e771752SSergio Lopez 125289bd0305SKevin Wolf return !!s->in_flight; 125389bd0305SKevin Wolf } 125489bd0305SKevin Wolf 125573895f38SHanna Reitz static bool mirror_cancel(Job *job, bool force) 1256521ff8b7SVladimir Sementsov-Ogievskiy { 1257521ff8b7SVladimir Sementsov-Ogievskiy MirrorBlockJob *s = container_of(job, MirrorBlockJob, common.job); 1258521ff8b7SVladimir Sementsov-Ogievskiy BlockDriverState *target = blk_bs(s->target); 1259521ff8b7SVladimir Sementsov-Ogievskiy 126073895f38SHanna Reitz /* 126173895f38SHanna Reitz * Before the job is READY, we treat any cancellation like a 126273895f38SHanna Reitz * force-cancellation. 126373895f38SHanna Reitz */ 126473895f38SHanna Reitz force = force || !job_is_ready(job); 126573895f38SHanna Reitz 126673895f38SHanna Reitz if (force) { 1267521ff8b7SVladimir Sementsov-Ogievskiy bdrv_cancel_in_flight(target); 1268521ff8b7SVladimir Sementsov-Ogievskiy } 126973895f38SHanna Reitz return force; 127073895f38SHanna Reitz } 127173895f38SHanna Reitz 127273895f38SHanna Reitz static bool commit_active_cancel(Job *job, bool force) 127373895f38SHanna Reitz { 127473895f38SHanna Reitz /* Same as above in mirror_cancel() */ 127573895f38SHanna Reitz return force || !job_is_ready(job); 12769c785cd7SVladimir Sementsov-Ogievskiy } 1277521ff8b7SVladimir Sementsov-Ogievskiy 12782d400d15SFiona Ebner static void mirror_change(BlockJob *job, BlockJobChangeOptions *opts, 12792d400d15SFiona Ebner Error **errp) 12802d400d15SFiona Ebner { 12812d400d15SFiona Ebner MirrorBlockJob *s = container_of(job, MirrorBlockJob, common); 12822d400d15SFiona Ebner BlockJobChangeOptionsMirror *change_opts = &opts->u.mirror; 12832d400d15SFiona Ebner MirrorCopyMode current; 12842d400d15SFiona Ebner 12852d400d15SFiona Ebner /* 12862d400d15SFiona Ebner * The implementation relies on the fact that copy_mode is only written 12872d400d15SFiona Ebner * under the BQL. Otherwise, further synchronization would be required. 12882d400d15SFiona Ebner */ 12892d400d15SFiona Ebner 12902d400d15SFiona Ebner GLOBAL_STATE_CODE(); 12912d400d15SFiona Ebner 12922d400d15SFiona Ebner if (qatomic_read(&s->copy_mode) == change_opts->copy_mode) { 12932d400d15SFiona Ebner return; 12942d400d15SFiona Ebner } 12952d400d15SFiona Ebner 12962d400d15SFiona Ebner if (change_opts->copy_mode != MIRROR_COPY_MODE_WRITE_BLOCKING) { 12972d400d15SFiona Ebner error_setg(errp, "Change to copy mode '%s' is not implemented", 12982d400d15SFiona Ebner MirrorCopyMode_str(change_opts->copy_mode)); 12992d400d15SFiona Ebner return; 13002d400d15SFiona Ebner } 13012d400d15SFiona Ebner 13022d400d15SFiona Ebner current = qatomic_cmpxchg(&s->copy_mode, MIRROR_COPY_MODE_BACKGROUND, 13032d400d15SFiona Ebner change_opts->copy_mode); 13042d400d15SFiona Ebner if (current != MIRROR_COPY_MODE_BACKGROUND) { 13052d400d15SFiona Ebner error_setg(errp, "Expected current copy mode '%s', got '%s'", 13062d400d15SFiona Ebner MirrorCopyMode_str(MIRROR_COPY_MODE_BACKGROUND), 13072d400d15SFiona Ebner MirrorCopyMode_str(current)); 13082d400d15SFiona Ebner } 13092d400d15SFiona Ebner } 13102d400d15SFiona Ebner 131176cb2f24SFiona Ebner static void mirror_query(BlockJob *job, BlockJobInfo *info) 131276cb2f24SFiona Ebner { 131376cb2f24SFiona Ebner MirrorBlockJob *s = container_of(job, MirrorBlockJob, common); 131476cb2f24SFiona Ebner 131576cb2f24SFiona Ebner info->u.mirror = (BlockJobInfoMirror) { 131676cb2f24SFiona Ebner .actively_synced = qatomic_read(&s->actively_synced), 131776cb2f24SFiona Ebner }; 131876cb2f24SFiona Ebner } 131976cb2f24SFiona Ebner 13203fc4b10aSFam Zheng static const BlockJobDriver mirror_job_driver = { 132133e9e9bdSKevin Wolf .job_driver = { 1322893f7ebaSPaolo Bonzini .instance_size = sizeof(MirrorBlockJob), 13238e4c8700SKevin Wolf .job_type = JOB_TYPE_MIRROR, 132480fa2c75SKevin Wolf .free = block_job_free, 1325b15de828SKevin Wolf .user_resume = block_job_user_resume, 1326f67432a2SJohn Snow .run = mirror_run, 1327737efc1eSJohn Snow .prepare = mirror_prepare, 1328737efc1eSJohn Snow .abort = mirror_abort, 1329565ac01fSStefan Hajnoczi .pause = mirror_pause, 1330da01ff7fSKevin Wolf .complete = mirror_complete, 1331521ff8b7SVladimir Sementsov-Ogievskiy .cancel = mirror_cancel, 13323453d972SKevin Wolf }, 133389bd0305SKevin Wolf .drained_poll = mirror_drained_poll, 13342d400d15SFiona Ebner .change = mirror_change, 133576cb2f24SFiona Ebner .query = mirror_query, 1336893f7ebaSPaolo Bonzini }; 1337893f7ebaSPaolo Bonzini 133803544a6eSFam Zheng static const BlockJobDriver commit_active_job_driver = { 133933e9e9bdSKevin Wolf .job_driver = { 134003544a6eSFam Zheng .instance_size = sizeof(MirrorBlockJob), 13418e4c8700SKevin Wolf .job_type = JOB_TYPE_COMMIT, 134280fa2c75SKevin Wolf .free = block_job_free, 1343b15de828SKevin Wolf .user_resume = block_job_user_resume, 1344f67432a2SJohn Snow .run = mirror_run, 1345737efc1eSJohn Snow .prepare = mirror_prepare, 1346737efc1eSJohn Snow .abort = mirror_abort, 1347565ac01fSStefan Hajnoczi .pause = mirror_pause, 1348da01ff7fSKevin Wolf .complete = mirror_complete, 134973895f38SHanna Reitz .cancel = commit_active_cancel, 13503453d972SKevin Wolf }, 135189bd0305SKevin Wolf .drained_poll = mirror_drained_poll, 135203544a6eSFam Zheng }; 135303544a6eSFam Zheng 1354537c3d4fSStefan Hajnoczi static void coroutine_fn 1355537c3d4fSStefan Hajnoczi do_sync_target_write(MirrorBlockJob *job, MirrorMethod method, 1356d06107adSMax Reitz uint64_t offset, uint64_t bytes, 1357d06107adSMax Reitz QEMUIOVector *qiov, int flags) 1358d06107adSMax Reitz { 1359d06107adSMax Reitz int ret; 1360dbdf699cSVladimir Sementsov-Ogievskiy size_t qiov_offset = 0; 1361870f8963SEric Blake int64_t dirty_bitmap_offset, dirty_bitmap_end; 1362d06107adSMax Reitz 1363dbdf699cSVladimir Sementsov-Ogievskiy if (!QEMU_IS_ALIGNED(offset, job->granularity) && 1364dbdf699cSVladimir Sementsov-Ogievskiy bdrv_dirty_bitmap_get(job->dirty_bitmap, offset)) 1365dbdf699cSVladimir Sementsov-Ogievskiy { 1366dbdf699cSVladimir Sementsov-Ogievskiy /* 1367dbdf699cSVladimir Sementsov-Ogievskiy * Dirty unaligned padding: ignore it. 1368dbdf699cSVladimir Sementsov-Ogievskiy * 1369dbdf699cSVladimir Sementsov-Ogievskiy * Reasoning: 1370dbdf699cSVladimir Sementsov-Ogievskiy * 1. If we copy it, we can't reset corresponding bit in 1371dbdf699cSVladimir Sementsov-Ogievskiy * dirty_bitmap as there may be some "dirty" bytes still not 1372dbdf699cSVladimir Sementsov-Ogievskiy * copied. 1373dbdf699cSVladimir Sementsov-Ogievskiy * 2. It's already dirty, so skipping it we don't diverge mirror 1374dbdf699cSVladimir Sementsov-Ogievskiy * progress. 1375dbdf699cSVladimir Sementsov-Ogievskiy * 1376dbdf699cSVladimir Sementsov-Ogievskiy * Note, that because of this, guest write may have no contribution 1377dbdf699cSVladimir Sementsov-Ogievskiy * into mirror converge, but that's not bad, as we have background 1378dbdf699cSVladimir Sementsov-Ogievskiy * process of mirroring. If under some bad circumstances (high guest 1379dbdf699cSVladimir Sementsov-Ogievskiy * IO load) background process starve, we will not converge anyway, 1380dbdf699cSVladimir Sementsov-Ogievskiy * even if each write will contribute, as guest is not guaranteed to 1381dbdf699cSVladimir Sementsov-Ogievskiy * rewrite the whole disk. 1382dbdf699cSVladimir Sementsov-Ogievskiy */ 1383dbdf699cSVladimir Sementsov-Ogievskiy qiov_offset = QEMU_ALIGN_UP(offset, job->granularity) - offset; 1384dbdf699cSVladimir Sementsov-Ogievskiy if (bytes <= qiov_offset) { 1385dbdf699cSVladimir Sementsov-Ogievskiy /* nothing to do after shrink */ 1386dbdf699cSVladimir Sementsov-Ogievskiy return; 1387dbdf699cSVladimir Sementsov-Ogievskiy } 1388dbdf699cSVladimir Sementsov-Ogievskiy offset += qiov_offset; 1389dbdf699cSVladimir Sementsov-Ogievskiy bytes -= qiov_offset; 1390dbdf699cSVladimir Sementsov-Ogievskiy } 1391dbdf699cSVladimir Sementsov-Ogievskiy 1392dbdf699cSVladimir Sementsov-Ogievskiy if (!QEMU_IS_ALIGNED(offset + bytes, job->granularity) && 1393dbdf699cSVladimir Sementsov-Ogievskiy bdrv_dirty_bitmap_get(job->dirty_bitmap, offset + bytes - 1)) 1394dbdf699cSVladimir Sementsov-Ogievskiy { 1395dbdf699cSVladimir Sementsov-Ogievskiy uint64_t tail = (offset + bytes) % job->granularity; 1396dbdf699cSVladimir Sementsov-Ogievskiy 1397dbdf699cSVladimir Sementsov-Ogievskiy if (bytes <= tail) { 1398dbdf699cSVladimir Sementsov-Ogievskiy /* nothing to do after shrink */ 1399dbdf699cSVladimir Sementsov-Ogievskiy return; 1400dbdf699cSVladimir Sementsov-Ogievskiy } 1401dbdf699cSVladimir Sementsov-Ogievskiy bytes -= tail; 1402dbdf699cSVladimir Sementsov-Ogievskiy } 1403dbdf699cSVladimir Sementsov-Ogievskiy 1404dbdf699cSVladimir Sementsov-Ogievskiy /* 1405dbdf699cSVladimir Sementsov-Ogievskiy * Tails are either clean or shrunk, so for bitmap resetting 1406dbdf699cSVladimir Sementsov-Ogievskiy * we safely align the range down. 1407dbdf699cSVladimir Sementsov-Ogievskiy */ 1408870f8963SEric Blake dirty_bitmap_offset = QEMU_ALIGN_UP(offset, job->granularity); 1409870f8963SEric Blake dirty_bitmap_end = QEMU_ALIGN_DOWN(offset + bytes, job->granularity); 1410870f8963SEric Blake if (dirty_bitmap_offset < dirty_bitmap_end) { 1411870f8963SEric Blake bdrv_reset_dirty_bitmap(job->dirty_bitmap, dirty_bitmap_offset, 1412870f8963SEric Blake dirty_bitmap_end - dirty_bitmap_offset); 1413dbdf699cSVladimir Sementsov-Ogievskiy } 1414d06107adSMax Reitz 14155c511ac3SVladimir Sementsov-Ogievskiy job_progress_increase_remaining(&job->common.job, bytes); 1416d69a879bSHanna Reitz job->active_write_bytes_in_flight += bytes; 1417d06107adSMax Reitz 1418d06107adSMax Reitz switch (method) { 1419d06107adSMax Reitz case MIRROR_METHOD_COPY: 1420dbdf699cSVladimir Sementsov-Ogievskiy ret = blk_co_pwritev_part(job->target, offset, bytes, 1421dbdf699cSVladimir Sementsov-Ogievskiy qiov, qiov_offset, flags); 1422d06107adSMax Reitz break; 1423d06107adSMax Reitz 1424d06107adSMax Reitz case MIRROR_METHOD_ZERO: 1425d06107adSMax Reitz assert(!qiov); 14265c511ac3SVladimir Sementsov-Ogievskiy ret = blk_co_pwrite_zeroes(job->target, offset, bytes, flags); 1427d06107adSMax Reitz break; 1428d06107adSMax Reitz 1429d06107adSMax Reitz case MIRROR_METHOD_DISCARD: 1430d06107adSMax Reitz assert(!qiov); 14315c511ac3SVladimir Sementsov-Ogievskiy ret = blk_co_pdiscard(job->target, offset, bytes); 1432d06107adSMax Reitz break; 1433d06107adSMax Reitz 1434d06107adSMax Reitz default: 1435d06107adSMax Reitz abort(); 1436d06107adSMax Reitz } 1437d06107adSMax Reitz 1438d69a879bSHanna Reitz job->active_write_bytes_in_flight -= bytes; 1439d06107adSMax Reitz if (ret >= 0) { 14405c511ac3SVladimir Sementsov-Ogievskiy job_progress_update(&job->common.job, bytes); 1441d06107adSMax Reitz } else { 1442d06107adSMax Reitz BlockErrorAction action; 1443d06107adSMax Reitz 1444dbdf699cSVladimir Sementsov-Ogievskiy /* 1445dbdf699cSVladimir Sementsov-Ogievskiy * We failed, so we should mark dirty the whole area, aligned up. 1446dbdf699cSVladimir Sementsov-Ogievskiy * Note that we don't care about shrunk tails if any: they were dirty 1447dbdf699cSVladimir Sementsov-Ogievskiy * at function start, and they must be still dirty, as we've locked 1448dbdf699cSVladimir Sementsov-Ogievskiy * the region for in-flight op. 1449dbdf699cSVladimir Sementsov-Ogievskiy */ 1450870f8963SEric Blake dirty_bitmap_offset = QEMU_ALIGN_DOWN(offset, job->granularity); 1451870f8963SEric Blake dirty_bitmap_end = QEMU_ALIGN_UP(offset + bytes, job->granularity); 1452870f8963SEric Blake bdrv_set_dirty_bitmap(job->dirty_bitmap, dirty_bitmap_offset, 1453870f8963SEric Blake dirty_bitmap_end - dirty_bitmap_offset); 145476cb2f24SFiona Ebner qatomic_set(&job->actively_synced, false); 1455d06107adSMax Reitz 1456d06107adSMax Reitz action = mirror_error_action(job, false, -ret); 1457d06107adSMax Reitz if (action == BLOCK_ERROR_ACTION_REPORT) { 1458d06107adSMax Reitz if (!job->ret) { 1459d06107adSMax Reitz job->ret = ret; 1460d06107adSMax Reitz } 1461d06107adSMax Reitz } 1462d06107adSMax Reitz } 1463d06107adSMax Reitz } 1464d06107adSMax Reitz 1465d06107adSMax Reitz static MirrorOp *coroutine_fn active_write_prepare(MirrorBlockJob *s, 1466d06107adSMax Reitz uint64_t offset, 1467d06107adSMax Reitz uint64_t bytes) 1468d06107adSMax Reitz { 1469d06107adSMax Reitz MirrorOp *op; 1470d06107adSMax Reitz uint64_t start_chunk = offset / s->granularity; 1471d06107adSMax Reitz uint64_t end_chunk = DIV_ROUND_UP(offset + bytes, s->granularity); 1472d06107adSMax Reitz 1473d06107adSMax Reitz op = g_new(MirrorOp, 1); 1474d06107adSMax Reitz *op = (MirrorOp){ 1475d06107adSMax Reitz .s = s, 1476d06107adSMax Reitz .offset = offset, 1477d06107adSMax Reitz .bytes = bytes, 1478d06107adSMax Reitz .is_active_write = true, 1479ce8cabbdSKevin Wolf .is_in_flight = true, 1480ead3f1bfSVladimir Sementsov-Ogievskiy .co = qemu_coroutine_self(), 1481d06107adSMax Reitz }; 1482d06107adSMax Reitz qemu_co_queue_init(&op->waiting_requests); 1483d06107adSMax Reitz QTAILQ_INSERT_TAIL(&s->ops_in_flight, op, next); 1484d06107adSMax Reitz 1485d06107adSMax Reitz s->in_active_write_counter++; 1486d06107adSMax Reitz 1487d69a879bSHanna Reitz /* 1488d69a879bSHanna Reitz * Wait for concurrent requests affecting the area. If there are already 1489d69a879bSHanna Reitz * running requests that are copying off now-to-be stale data in the area, 1490d69a879bSHanna Reitz * we must wait for them to finish before we begin writing fresh data to the 1491d69a879bSHanna Reitz * target so that the write operations appear in the correct order. 1492d69a879bSHanna Reitz * Note that background requests (see mirror_iteration()) in contrast only 1493d69a879bSHanna Reitz * wait for conflicting requests at the start of the dirty area, and then 1494d69a879bSHanna Reitz * (based on the in_flight_bitmap) truncate the area to copy so it will not 1495d69a879bSHanna Reitz * conflict with any requests beyond that. For active writes, however, we 1496d69a879bSHanna Reitz * cannot truncate that area. The request from our parent must be blocked 1497d69a879bSHanna Reitz * until the area is copied in full. Therefore, we must wait for the whole 1498d69a879bSHanna Reitz * area to become free of concurrent requests. 1499d69a879bSHanna Reitz */ 1500d06107adSMax Reitz mirror_wait_on_conflicts(op, s, offset, bytes); 1501d06107adSMax Reitz 1502d06107adSMax Reitz bitmap_set(s->in_flight_bitmap, start_chunk, end_chunk - start_chunk); 1503d06107adSMax Reitz 1504d06107adSMax Reitz return op; 1505d06107adSMax Reitz } 1506d06107adSMax Reitz 15079c93652dSKevin Wolf static void coroutine_fn GRAPH_RDLOCK active_write_settle(MirrorOp *op) 1508d06107adSMax Reitz { 1509d06107adSMax Reitz uint64_t start_chunk = op->offset / op->s->granularity; 1510d06107adSMax Reitz uint64_t end_chunk = DIV_ROUND_UP(op->offset + op->bytes, 1511d06107adSMax Reitz op->s->granularity); 1512d06107adSMax Reitz 151376cb2f24SFiona Ebner if (!--op->s->in_active_write_counter && 151476cb2f24SFiona Ebner qatomic_read(&op->s->actively_synced)) { 1515d06107adSMax Reitz BdrvChild *source = op->s->mirror_top_bs->backing; 1516d06107adSMax Reitz 1517d06107adSMax Reitz if (QLIST_FIRST(&source->bs->parents) == source && 1518d06107adSMax Reitz QLIST_NEXT(source, next_parent) == NULL) 1519d06107adSMax Reitz { 1520d06107adSMax Reitz /* Assert that we are back in sync once all active write 1521d06107adSMax Reitz * operations are settled. 1522d06107adSMax Reitz * Note that we can only assert this if the mirror node 1523d06107adSMax Reitz * is the source node's only parent. */ 1524d06107adSMax Reitz assert(!bdrv_get_dirty_count(op->s->dirty_bitmap)); 1525d06107adSMax Reitz } 1526d06107adSMax Reitz } 1527d06107adSMax Reitz bitmap_clear(op->s->in_flight_bitmap, start_chunk, end_chunk - start_chunk); 1528d06107adSMax Reitz QTAILQ_REMOVE(&op->s->ops_in_flight, op, next); 1529d06107adSMax Reitz qemu_co_queue_restart_all(&op->waiting_requests); 1530d06107adSMax Reitz g_free(op); 1531d06107adSMax Reitz } 1532d06107adSMax Reitz 1533b9b10c35SKevin Wolf static int coroutine_fn GRAPH_RDLOCK 1534b9b10c35SKevin Wolf bdrv_mirror_top_preadv(BlockDriverState *bs, int64_t offset, int64_t bytes, 1535b9b10c35SKevin Wolf QEMUIOVector *qiov, BdrvRequestFlags flags) 15364ef85a9cSKevin Wolf { 15374ef85a9cSKevin Wolf return bdrv_co_preadv(bs->backing, offset, bytes, qiov, flags); 15384ef85a9cSKevin Wolf } 15394ef85a9cSKevin Wolf 15407b32ad22SFiona Ebner static bool should_copy_to_target(MirrorBDSOpaque *s) 15417b32ad22SFiona Ebner { 15427b32ad22SFiona Ebner return s->job && s->job->ret >= 0 && 15437b32ad22SFiona Ebner !job_is_cancelled(&s->job->common.job) && 15442d400d15SFiona Ebner qatomic_read(&s->job->copy_mode) == MIRROR_COPY_MODE_WRITE_BLOCKING; 15457b32ad22SFiona Ebner } 15467b32ad22SFiona Ebner 15479a5a1c62SEmanuele Giuseppe Esposito static int coroutine_fn GRAPH_RDLOCK 15489a5a1c62SEmanuele Giuseppe Esposito bdrv_mirror_top_do_write(BlockDriverState *bs, MirrorMethod method, 15497b32ad22SFiona Ebner bool copy_to_target, uint64_t offset, uint64_t bytes, 15507b32ad22SFiona Ebner QEMUIOVector *qiov, int flags) 1551d06107adSMax Reitz { 1552d06107adSMax Reitz MirrorOp *op = NULL; 1553d06107adSMax Reitz MirrorBDSOpaque *s = bs->opaque; 1554d06107adSMax Reitz int ret = 0; 1555d06107adSMax Reitz 1556d06107adSMax Reitz if (copy_to_target) { 1557d06107adSMax Reitz op = active_write_prepare(s->job, offset, bytes); 1558d06107adSMax Reitz } 1559d06107adSMax Reitz 1560d06107adSMax Reitz switch (method) { 1561d06107adSMax Reitz case MIRROR_METHOD_COPY: 1562d06107adSMax Reitz ret = bdrv_co_pwritev(bs->backing, offset, bytes, qiov, flags); 1563d06107adSMax Reitz break; 1564d06107adSMax Reitz 1565d06107adSMax Reitz case MIRROR_METHOD_ZERO: 1566d06107adSMax Reitz ret = bdrv_co_pwrite_zeroes(bs->backing, offset, bytes, flags); 1567d06107adSMax Reitz break; 1568d06107adSMax Reitz 1569d06107adSMax Reitz case MIRROR_METHOD_DISCARD: 15700b9fd3f4SFam Zheng ret = bdrv_co_pdiscard(bs->backing, offset, bytes); 1571d06107adSMax Reitz break; 1572d06107adSMax Reitz 1573d06107adSMax Reitz default: 1574d06107adSMax Reitz abort(); 1575d06107adSMax Reitz } 1576d06107adSMax Reitz 1577058cfca5SFiona Ebner if (!copy_to_target && s->job && s->job->dirty_bitmap) { 157876cb2f24SFiona Ebner qatomic_set(&s->job->actively_synced, false); 1579058cfca5SFiona Ebner bdrv_set_dirty_bitmap(s->job->dirty_bitmap, offset, bytes); 1580058cfca5SFiona Ebner } 1581058cfca5SFiona Ebner 1582d06107adSMax Reitz if (ret < 0) { 1583d06107adSMax Reitz goto out; 1584d06107adSMax Reitz } 1585d06107adSMax Reitz 1586d06107adSMax Reitz if (copy_to_target) { 1587d06107adSMax Reitz do_sync_target_write(s->job, method, offset, bytes, qiov, flags); 1588d06107adSMax Reitz } 1589d06107adSMax Reitz 1590d06107adSMax Reitz out: 1591d06107adSMax Reitz if (copy_to_target) { 1592d06107adSMax Reitz active_write_settle(op); 1593d06107adSMax Reitz } 1594d06107adSMax Reitz return ret; 1595d06107adSMax Reitz } 1596d06107adSMax Reitz 1597b9b10c35SKevin Wolf static int coroutine_fn GRAPH_RDLOCK 1598b9b10c35SKevin Wolf bdrv_mirror_top_pwritev(BlockDriverState *bs, int64_t offset, int64_t bytes, 1599b9b10c35SKevin Wolf QEMUIOVector *qiov, BdrvRequestFlags flags) 16004ef85a9cSKevin Wolf { 1601d06107adSMax Reitz QEMUIOVector bounce_qiov; 1602d06107adSMax Reitz void *bounce_buf; 1603d06107adSMax Reitz int ret = 0; 16047b32ad22SFiona Ebner bool copy_to_target = should_copy_to_target(bs->opaque); 1605d06107adSMax Reitz 1606d06107adSMax Reitz if (copy_to_target) { 1607d06107adSMax Reitz /* The guest might concurrently modify the data to write; but 1608d06107adSMax Reitz * the data on source and destination must match, so we have 1609d06107adSMax Reitz * to use a bounce buffer if we are going to write to the 1610d06107adSMax Reitz * target now. */ 1611d06107adSMax Reitz bounce_buf = qemu_blockalign(bs, bytes); 1612d06107adSMax Reitz iov_to_buf_full(qiov->iov, qiov->niov, 0, bounce_buf, bytes); 1613d06107adSMax Reitz 1614d06107adSMax Reitz qemu_iovec_init(&bounce_qiov, 1); 1615d06107adSMax Reitz qemu_iovec_add(&bounce_qiov, bounce_buf, bytes); 1616d06107adSMax Reitz qiov = &bounce_qiov; 1617e8b65355SStefan Hajnoczi 1618e8b65355SStefan Hajnoczi flags &= ~BDRV_REQ_REGISTERED_BUF; 1619d06107adSMax Reitz } 1620d06107adSMax Reitz 16217b32ad22SFiona Ebner ret = bdrv_mirror_top_do_write(bs, MIRROR_METHOD_COPY, copy_to_target, 16227b32ad22SFiona Ebner offset, bytes, qiov, flags); 1623d06107adSMax Reitz 1624d06107adSMax Reitz if (copy_to_target) { 1625d06107adSMax Reitz qemu_iovec_destroy(&bounce_qiov); 1626d06107adSMax Reitz qemu_vfree(bounce_buf); 1627d06107adSMax Reitz } 1628d06107adSMax Reitz 1629d06107adSMax Reitz return ret; 16304ef85a9cSKevin Wolf } 16314ef85a9cSKevin Wolf 163288095349SEmanuele Giuseppe Esposito static int coroutine_fn GRAPH_RDLOCK bdrv_mirror_top_flush(BlockDriverState *bs) 16334ef85a9cSKevin Wolf { 1634ce960aa9SVladimir Sementsov-Ogievskiy if (bs->backing == NULL) { 1635ce960aa9SVladimir Sementsov-Ogievskiy /* we can be here after failed bdrv_append in mirror_start_job */ 1636ce960aa9SVladimir Sementsov-Ogievskiy return 0; 1637ce960aa9SVladimir Sementsov-Ogievskiy } 16384ef85a9cSKevin Wolf return bdrv_co_flush(bs->backing->bs); 16394ef85a9cSKevin Wolf } 16404ef85a9cSKevin Wolf 1641abaf8b75SKevin Wolf static int coroutine_fn GRAPH_RDLOCK 1642abaf8b75SKevin Wolf bdrv_mirror_top_pwrite_zeroes(BlockDriverState *bs, int64_t offset, 1643abaf8b75SKevin Wolf int64_t bytes, BdrvRequestFlags flags) 16444ef85a9cSKevin Wolf { 16457b32ad22SFiona Ebner bool copy_to_target = should_copy_to_target(bs->opaque); 16467b32ad22SFiona Ebner return bdrv_mirror_top_do_write(bs, MIRROR_METHOD_ZERO, copy_to_target, 16477b32ad22SFiona Ebner offset, bytes, NULL, flags); 16484ef85a9cSKevin Wolf } 16494ef85a9cSKevin Wolf 16509a5a1c62SEmanuele Giuseppe Esposito static int coroutine_fn GRAPH_RDLOCK 16519a5a1c62SEmanuele Giuseppe Esposito bdrv_mirror_top_pdiscard(BlockDriverState *bs, int64_t offset, int64_t bytes) 16524ef85a9cSKevin Wolf { 16537b32ad22SFiona Ebner bool copy_to_target = should_copy_to_target(bs->opaque); 16547b32ad22SFiona Ebner return bdrv_mirror_top_do_write(bs, MIRROR_METHOD_DISCARD, copy_to_target, 16557b32ad22SFiona Ebner offset, bytes, NULL, 0); 16564ef85a9cSKevin Wolf } 16574ef85a9cSKevin Wolf 1658004915a9SKevin Wolf static void GRAPH_RDLOCK bdrv_mirror_top_refresh_filename(BlockDriverState *bs) 1659fd4a6493SKevin Wolf { 166018775ff3SVladimir Sementsov-Ogievskiy if (bs->backing == NULL) { 166118775ff3SVladimir Sementsov-Ogievskiy /* we can be here after failed bdrv_attach_child in 166218775ff3SVladimir Sementsov-Ogievskiy * bdrv_set_backing_hd */ 166318775ff3SVladimir Sementsov-Ogievskiy return; 166418775ff3SVladimir Sementsov-Ogievskiy } 1665fd4a6493SKevin Wolf pstrcpy(bs->exact_filename, sizeof(bs->exact_filename), 1666fd4a6493SKevin Wolf bs->backing->bs->filename); 1667fd4a6493SKevin Wolf } 1668fd4a6493SKevin Wolf 16694ef85a9cSKevin Wolf static void bdrv_mirror_top_child_perm(BlockDriverState *bs, BdrvChild *c, 1670bf8e925eSMax Reitz BdrvChildRole role, 1671e0995dc3SKevin Wolf BlockReopenQueue *reopen_queue, 16724ef85a9cSKevin Wolf uint64_t perm, uint64_t shared, 16734ef85a9cSKevin Wolf uint64_t *nperm, uint64_t *nshared) 16744ef85a9cSKevin Wolf { 1675f94dc3b4SMax Reitz MirrorBDSOpaque *s = bs->opaque; 1676f94dc3b4SMax Reitz 1677f94dc3b4SMax Reitz if (s->stop) { 1678f94dc3b4SMax Reitz /* 1679f94dc3b4SMax Reitz * If the job is to be stopped, we do not need to forward 1680f94dc3b4SMax Reitz * anything to the real image. 1681f94dc3b4SMax Reitz */ 1682f94dc3b4SMax Reitz *nperm = 0; 1683f94dc3b4SMax Reitz *nshared = BLK_PERM_ALL; 1684f94dc3b4SMax Reitz return; 1685f94dc3b4SMax Reitz } 1686f94dc3b4SMax Reitz 168753431b90SMax Reitz bdrv_default_perms(bs, c, role, reopen_queue, 168853431b90SMax Reitz perm, shared, nperm, nshared); 16894ef85a9cSKevin Wolf 169053431b90SMax Reitz if (s->is_commit) { 169153431b90SMax Reitz /* 169253431b90SMax Reitz * For commit jobs, we cannot take CONSISTENT_READ, because 169353431b90SMax Reitz * that permission is unshared for everything above the base 169453431b90SMax Reitz * node (except for filters on the base node). 169553431b90SMax Reitz * We also have to force-share the WRITE permission, or 169653431b90SMax Reitz * otherwise we would block ourselves at the base node (if 169753431b90SMax Reitz * writes are blocked for a node, they are also blocked for 169853431b90SMax Reitz * its backing file). 169953431b90SMax Reitz * (We could also share RESIZE, because it may be needed for 170053431b90SMax Reitz * the target if its size is less than the top node's; but 170153431b90SMax Reitz * bdrv_default_perms_for_cow() automatically shares RESIZE 170253431b90SMax Reitz * for backing nodes if WRITE is shared, so there is no need 170353431b90SMax Reitz * to do it here.) 170453431b90SMax Reitz */ 170553431b90SMax Reitz *nperm &= ~BLK_PERM_CONSISTENT_READ; 170653431b90SMax Reitz *nshared |= BLK_PERM_WRITE; 170753431b90SMax Reitz } 17084ef85a9cSKevin Wolf } 17094ef85a9cSKevin Wolf 17104ef85a9cSKevin Wolf /* Dummy node that provides consistent read to its users without requiring it 17114ef85a9cSKevin Wolf * from its backing file and that allows writes on the backing file chain. */ 17124ef85a9cSKevin Wolf static BlockDriver bdrv_mirror_top = { 17134ef85a9cSKevin Wolf .format_name = "mirror_top", 17144ef85a9cSKevin Wolf .bdrv_co_preadv = bdrv_mirror_top_preadv, 17154ef85a9cSKevin Wolf .bdrv_co_pwritev = bdrv_mirror_top_pwritev, 17164ef85a9cSKevin Wolf .bdrv_co_pwrite_zeroes = bdrv_mirror_top_pwrite_zeroes, 17174ef85a9cSKevin Wolf .bdrv_co_pdiscard = bdrv_mirror_top_pdiscard, 17184ef85a9cSKevin Wolf .bdrv_co_flush = bdrv_mirror_top_flush, 1719fd4a6493SKevin Wolf .bdrv_refresh_filename = bdrv_mirror_top_refresh_filename, 17204ef85a9cSKevin Wolf .bdrv_child_perm = bdrv_mirror_top_child_perm, 17216540fd15SMax Reitz 17226540fd15SMax Reitz .is_filter = true, 1723046fd84fSVladimir Sementsov-Ogievskiy .filtered_child_is_backing = true, 17244ef85a9cSKevin Wolf }; 17254ef85a9cSKevin Wolf 1726cc19f177SVladimir Sementsov-Ogievskiy static BlockJob *mirror_start_job( 1727cc19f177SVladimir Sementsov-Ogievskiy const char *job_id, BlockDriverState *bs, 172847970dfbSJohn Snow int creation_flags, BlockDriverState *target, 172947970dfbSJohn Snow const char *replaces, int64_t speed, 173047970dfbSJohn Snow uint32_t granularity, int64_t buf_size, 17319474d97bSEric Blake MirrorSyncMode sync_mode, 1732274fcceeSMax Reitz BlockMirrorBackingMode backing_mode, 1733*d17a34bfSEric Blake bool zero_target, bool target_is_zero, 173403544a6eSFam Zheng BlockdevOnError on_source_error, 1735b952b558SPaolo Bonzini BlockdevOnError on_target_error, 17360fc9f8eaSFam Zheng bool unmap, 1737097310b5SMarkus Armbruster BlockCompletionFunc *cb, 173851ccfa2dSFam Zheng void *opaque, 173903544a6eSFam Zheng const BlockJobDriver *driver, 17409474d97bSEric Blake BlockDriverState *base, 174151ccfa2dSFam Zheng bool auto_complete, const char *filter_node_name, 1742481debaaSMax Reitz bool is_mirror, MirrorCopyMode copy_mode, 17437d99ae59SAlexander Ivanov bool base_ro, 174451ccfa2dSFam Zheng Error **errp) 1745893f7ebaSPaolo Bonzini { 1746893f7ebaSPaolo Bonzini MirrorBlockJob *s; 1747429076e8SMax Reitz MirrorBDSOpaque *bs_opaque; 17484ef85a9cSKevin Wolf BlockDriverState *mirror_top_bs; 17494ef85a9cSKevin Wolf bool target_is_backing; 17503f072a7fSMax Reitz uint64_t target_perms, target_shared_perms; 1751d7086422SKevin Wolf int ret; 1752893f7ebaSPaolo Bonzini 17533804e3cfSKevin Wolf GLOBAL_STATE_CODE(); 17543804e3cfSKevin Wolf 1755eee13dfeSPaolo Bonzini if (granularity == 0) { 1756341ebc2fSJohn Snow granularity = bdrv_get_default_bitmap_granularity(target); 1757eee13dfeSPaolo Bonzini } 1758eee13dfeSPaolo Bonzini 175931826642SEric Blake assert(is_power_of_2(granularity)); 1760eee13dfeSPaolo Bonzini 176148ac0a4dSWen Congyang if (buf_size < 0) { 176248ac0a4dSWen Congyang error_setg(errp, "Invalid parameter 'buf-size'"); 1763cc19f177SVladimir Sementsov-Ogievskiy return NULL; 176448ac0a4dSWen Congyang } 176548ac0a4dSWen Congyang 176648ac0a4dSWen Congyang if (buf_size == 0) { 176748ac0a4dSWen Congyang buf_size = DEFAULT_MIRROR_BUF_SIZE; 176848ac0a4dSWen Congyang } 17695bc361b8SFam Zheng 1770ad74751fSKevin Wolf bdrv_graph_rdlock_main_loop(); 17713f072a7fSMax Reitz if (bdrv_skip_filters(bs) == bdrv_skip_filters(target)) { 177286fae10cSKevin Wolf error_setg(errp, "Can't mirror node into itself"); 1773ad74751fSKevin Wolf bdrv_graph_rdunlock_main_loop(); 1774cc19f177SVladimir Sementsov-Ogievskiy return NULL; 177586fae10cSKevin Wolf } 177686fae10cSKevin Wolf 177753431b90SMax Reitz target_is_backing = bdrv_chain_contains(bs, target); 1778ad74751fSKevin Wolf bdrv_graph_rdunlock_main_loop(); 177953431b90SMax Reitz 17804ef85a9cSKevin Wolf /* In the case of active commit, add dummy driver to provide consistent 17814ef85a9cSKevin Wolf * reads on the top, while disabling it in the intermediate nodes, and make 17824ef85a9cSKevin Wolf * the backing chain writable. */ 17836cdbceb1SKevin Wolf mirror_top_bs = bdrv_new_open_driver(&bdrv_mirror_top, filter_node_name, 17846cdbceb1SKevin Wolf BDRV_O_RDWR, errp); 17854ef85a9cSKevin Wolf if (mirror_top_bs == NULL) { 1786cc19f177SVladimir Sementsov-Ogievskiy return NULL; 1787893f7ebaSPaolo Bonzini } 1788d3c8c674SKevin Wolf if (!filter_node_name) { 1789d3c8c674SKevin Wolf mirror_top_bs->implicit = true; 1790d3c8c674SKevin Wolf } 1791e5182c1cSMax Reitz 1792e5182c1cSMax Reitz /* So that we can always drop this node */ 1793e5182c1cSMax Reitz mirror_top_bs->never_freeze = true; 1794e5182c1cSMax Reitz 17954ef85a9cSKevin Wolf mirror_top_bs->total_sectors = bs->total_sectors; 1796228345bfSMax Reitz mirror_top_bs->supported_write_flags = BDRV_REQ_WRITE_UNCHANGED; 179780f5c33fSKevin Wolf mirror_top_bs->supported_zero_flags = BDRV_REQ_WRITE_UNCHANGED | 179880f5c33fSKevin Wolf BDRV_REQ_NO_FALLBACK; 1799429076e8SMax Reitz bs_opaque = g_new0(MirrorBDSOpaque, 1); 1800429076e8SMax Reitz mirror_top_bs->opaque = bs_opaque; 1801893f7ebaSPaolo Bonzini 180253431b90SMax Reitz bs_opaque->is_commit = target_is_backing; 180353431b90SMax Reitz 18044ef85a9cSKevin Wolf bdrv_drained_begin(bs); 1805934aee14SVladimir Sementsov-Ogievskiy ret = bdrv_append(mirror_top_bs, bs, errp); 18064ef85a9cSKevin Wolf bdrv_drained_end(bs); 18074ef85a9cSKevin Wolf 1808934aee14SVladimir Sementsov-Ogievskiy if (ret < 0) { 1809b2c2832cSKevin Wolf bdrv_unref(mirror_top_bs); 1810cc19f177SVladimir Sementsov-Ogievskiy return NULL; 1811b2c2832cSKevin Wolf } 1812b2c2832cSKevin Wolf 18134ef85a9cSKevin Wolf /* Make sure that the source is not resized while the job is running */ 181475859b94SJohn Snow s = block_job_create(job_id, driver, NULL, mirror_top_bs, 18154ef85a9cSKevin Wolf BLK_PERM_CONSISTENT_READ, 18164ef85a9cSKevin Wolf BLK_PERM_CONSISTENT_READ | BLK_PERM_WRITE_UNCHANGED | 181764631f36SVladimir Sementsov-Ogievskiy BLK_PERM_WRITE, speed, 18184ef85a9cSKevin Wolf creation_flags, cb, opaque, errp); 18194ef85a9cSKevin Wolf if (!s) { 18204ef85a9cSKevin Wolf goto fail; 18214ef85a9cSKevin Wolf } 1822429076e8SMax Reitz 18237a25fcd0SMax Reitz /* The block job now has a reference to this node */ 18247a25fcd0SMax Reitz bdrv_unref(mirror_top_bs); 18257a25fcd0SMax Reitz 18264ef85a9cSKevin Wolf s->mirror_top_bs = mirror_top_bs; 18277d99ae59SAlexander Ivanov s->base_ro = base_ro; 18284ef85a9cSKevin Wolf 18294ef85a9cSKevin Wolf /* No resize for the target either; while the mirror is still running, a 18304ef85a9cSKevin Wolf * consistent read isn't necessarily possible. We could possibly allow 18314ef85a9cSKevin Wolf * writes and graph modifications, though it would likely defeat the 18324ef85a9cSKevin Wolf * purpose of a mirror, so leave them blocked for now. 18334ef85a9cSKevin Wolf * 18344ef85a9cSKevin Wolf * In the case of active commit, things look a bit different, though, 18354ef85a9cSKevin Wolf * because the target is an already populated backing file in active use. 18364ef85a9cSKevin Wolf * We can allow anything except resize there.*/ 18373f072a7fSMax Reitz 18383f072a7fSMax Reitz target_perms = BLK_PERM_WRITE; 18393f072a7fSMax Reitz target_shared_perms = BLK_PERM_WRITE_UNCHANGED; 18403f072a7fSMax Reitz 18413f072a7fSMax Reitz if (target_is_backing) { 18423f072a7fSMax Reitz int64_t bs_size, target_size; 18433f072a7fSMax Reitz bs_size = bdrv_getlength(bs); 18443f072a7fSMax Reitz if (bs_size < 0) { 18453f072a7fSMax Reitz error_setg_errno(errp, -bs_size, 18463f072a7fSMax Reitz "Could not inquire top image size"); 18473f072a7fSMax Reitz goto fail; 18483f072a7fSMax Reitz } 18493f072a7fSMax Reitz 18503f072a7fSMax Reitz target_size = bdrv_getlength(target); 18513f072a7fSMax Reitz if (target_size < 0) { 18523f072a7fSMax Reitz error_setg_errno(errp, -target_size, 18533f072a7fSMax Reitz "Could not inquire base image size"); 18543f072a7fSMax Reitz goto fail; 18553f072a7fSMax Reitz } 18563f072a7fSMax Reitz 18573f072a7fSMax Reitz if (target_size < bs_size) { 18583f072a7fSMax Reitz target_perms |= BLK_PERM_RESIZE; 18593f072a7fSMax Reitz } 18603f072a7fSMax Reitz 186164631f36SVladimir Sementsov-Ogievskiy target_shared_perms |= BLK_PERM_CONSISTENT_READ | BLK_PERM_WRITE; 1862ad74751fSKevin Wolf } else { 1863ad74751fSKevin Wolf bdrv_graph_rdlock_main_loop(); 1864ad74751fSKevin Wolf if (bdrv_chain_contains(bs, bdrv_skip_filters(target))) { 18653f072a7fSMax Reitz /* 18663f072a7fSMax Reitz * We may want to allow this in the future, but it would 18673f072a7fSMax Reitz * require taking some extra care. 18683f072a7fSMax Reitz */ 1869ad74751fSKevin Wolf error_setg(errp, "Cannot mirror to a filter on top of a node in " 1870ad74751fSKevin Wolf "the source's backing chain"); 1871ad74751fSKevin Wolf bdrv_graph_rdunlock_main_loop(); 18723f072a7fSMax Reitz goto fail; 18733f072a7fSMax Reitz } 1874ad74751fSKevin Wolf bdrv_graph_rdunlock_main_loop(); 1875ad74751fSKevin Wolf } 18763f072a7fSMax Reitz 1877d861ab3aSKevin Wolf s->target = blk_new(s->common.job.aio_context, 18783f072a7fSMax Reitz target_perms, target_shared_perms); 1879d7086422SKevin Wolf ret = blk_insert_bs(s->target, target, errp); 1880d7086422SKevin Wolf if (ret < 0) { 18814ef85a9cSKevin Wolf goto fail; 1882d7086422SKevin Wolf } 1883045a2f82SFam Zheng if (is_mirror) { 1884045a2f82SFam Zheng /* XXX: Mirror target could be a NBD server of target QEMU in the case 1885045a2f82SFam Zheng * of non-shared block migration. To allow migration completion, we 1886045a2f82SFam Zheng * have to allow "inactivate" of the target BB. When that happens, we 1887045a2f82SFam Zheng * know the job is drained, and the vcpus are stopped, so no write 1888045a2f82SFam Zheng * operation will be performed. Block layer already has assertions to 1889045a2f82SFam Zheng * ensure that. */ 1890045a2f82SFam Zheng blk_set_force_allow_inactivate(s->target); 1891045a2f82SFam Zheng } 18929ff7f0dfSKevin Wolf blk_set_allow_aio_context_change(s->target, true); 1893cf312932SKevin Wolf blk_set_disable_request_queuing(s->target, true); 1894e253f4b8SKevin Wolf 1895ad74751fSKevin Wolf bdrv_graph_rdlock_main_loop(); 189609158f00SBenoît Canet s->replaces = g_strdup(replaces); 1897b952b558SPaolo Bonzini s->on_source_error = on_source_error; 1898b952b558SPaolo Bonzini s->on_target_error = on_target_error; 18999474d97bSEric Blake s->sync_mode = sync_mode; 1900274fcceeSMax Reitz s->backing_mode = backing_mode; 1901cdf3bc93SMax Reitz s->zero_target = zero_target; 1902*d17a34bfSEric Blake s->target_is_zero = target_is_zero; 19032d400d15SFiona Ebner qatomic_set(&s->copy_mode, copy_mode); 19045bc361b8SFam Zheng s->base = base; 19053f072a7fSMax Reitz s->base_overlay = bdrv_find_overlay(bs, base); 1906eee13dfeSPaolo Bonzini s->granularity = granularity; 190748ac0a4dSWen Congyang s->buf_size = ROUND_UP(buf_size, granularity); 19080fc9f8eaSFam Zheng s->unmap = unmap; 1909b49f7eadSWen Congyang if (auto_complete) { 1910b49f7eadSWen Congyang s->should_complete = true; 1911b49f7eadSWen Congyang } 1912ad74751fSKevin Wolf bdrv_graph_rdunlock_main_loop(); 1913b812f671SPaolo Bonzini 1914058cfca5SFiona Ebner s->dirty_bitmap = bdrv_create_dirty_bitmap(s->mirror_top_bs, granularity, 1915058cfca5SFiona Ebner NULL, errp); 1916b8afb520SFam Zheng if (!s->dirty_bitmap) { 191788f9d1b3SKevin Wolf goto fail; 1918b8afb520SFam Zheng } 1919058cfca5SFiona Ebner 1920058cfca5SFiona Ebner /* 1921058cfca5SFiona Ebner * The dirty bitmap is set by bdrv_mirror_top_do_write() when not in active 1922058cfca5SFiona Ebner * mode. 1923058cfca5SFiona Ebner */ 1924dbdf699cSVladimir Sementsov-Ogievskiy bdrv_disable_dirty_bitmap(s->dirty_bitmap); 192510f3cd15SAlberto Garcia 19266bc30f19SStefan Hajnoczi bdrv_graph_wrlock(); 192767b24427SAlberto Garcia ret = block_job_add_bdrv(&s->common, "source", bs, 0, 192867b24427SAlberto Garcia BLK_PERM_WRITE_UNCHANGED | BLK_PERM_WRITE | 192967b24427SAlberto Garcia BLK_PERM_CONSISTENT_READ, 193067b24427SAlberto Garcia errp); 193167b24427SAlberto Garcia if (ret < 0) { 19326bc30f19SStefan Hajnoczi bdrv_graph_wrunlock(); 193367b24427SAlberto Garcia goto fail; 193467b24427SAlberto Garcia } 193567b24427SAlberto Garcia 19364ef85a9cSKevin Wolf /* Required permissions are already taken with blk_new() */ 193776d554e2SKevin Wolf block_job_add_bdrv(&s->common, "target", target, 0, BLK_PERM_ALL, 193876d554e2SKevin Wolf &error_abort); 193976d554e2SKevin Wolf 1940f3ede4b0SAlberto Garcia /* In commit_active_start() all intermediate nodes disappear, so 1941f3ede4b0SAlberto Garcia * any jobs in them must be blocked */ 19424ef85a9cSKevin Wolf if (target_is_backing) { 19433f072a7fSMax Reitz BlockDriverState *iter, *filtered_target; 19443f072a7fSMax Reitz uint64_t iter_shared_perms; 19453f072a7fSMax Reitz 19463f072a7fSMax Reitz /* 19473f072a7fSMax Reitz * The topmost node with 19483f072a7fSMax Reitz * bdrv_skip_filters(filtered_target) == bdrv_skip_filters(target) 19493f072a7fSMax Reitz */ 19503f072a7fSMax Reitz filtered_target = bdrv_cow_bs(bdrv_find_overlay(bs, target)); 19513f072a7fSMax Reitz 19523f072a7fSMax Reitz assert(bdrv_skip_filters(filtered_target) == 19533f072a7fSMax Reitz bdrv_skip_filters(target)); 19543f072a7fSMax Reitz 19553f072a7fSMax Reitz /* 19563f072a7fSMax Reitz * XXX BLK_PERM_WRITE needs to be allowed so we don't block 19574ef85a9cSKevin Wolf * ourselves at s->base (if writes are blocked for a node, they are 19584ef85a9cSKevin Wolf * also blocked for its backing file). The other options would be a 19593f072a7fSMax Reitz * second filter driver above s->base (== target). 19603f072a7fSMax Reitz */ 19613f072a7fSMax Reitz iter_shared_perms = BLK_PERM_WRITE_UNCHANGED | BLK_PERM_WRITE; 19623f072a7fSMax Reitz 19633f072a7fSMax Reitz for (iter = bdrv_filter_or_cow_bs(bs); iter != target; 19643f072a7fSMax Reitz iter = bdrv_filter_or_cow_bs(iter)) 19653f072a7fSMax Reitz { 19663f072a7fSMax Reitz if (iter == filtered_target) { 19673f072a7fSMax Reitz /* 19683f072a7fSMax Reitz * From here on, all nodes are filters on the base. 19693f072a7fSMax Reitz * This allows us to share BLK_PERM_CONSISTENT_READ. 19703f072a7fSMax Reitz */ 19713f072a7fSMax Reitz iter_shared_perms |= BLK_PERM_CONSISTENT_READ; 19723f072a7fSMax Reitz } 19733f072a7fSMax Reitz 19744ef85a9cSKevin Wolf ret = block_job_add_bdrv(&s->common, "intermediate node", iter, 0, 19753f072a7fSMax Reitz iter_shared_perms, errp); 19764ef85a9cSKevin Wolf if (ret < 0) { 19776bc30f19SStefan Hajnoczi bdrv_graph_wrunlock(); 19784ef85a9cSKevin Wolf goto fail; 19794ef85a9cSKevin Wolf } 1980f3ede4b0SAlberto Garcia } 1981ef53dc09SAlberto Garcia 1982ef53dc09SAlberto Garcia if (bdrv_freeze_backing_chain(mirror_top_bs, target, errp) < 0) { 19836bc30f19SStefan Hajnoczi bdrv_graph_wrunlock(); 1984ef53dc09SAlberto Garcia goto fail; 1985ef53dc09SAlberto Garcia } 1986f3ede4b0SAlberto Garcia } 19876bc30f19SStefan Hajnoczi bdrv_graph_wrunlock(); 198810f3cd15SAlberto Garcia 198912aa4082SMax Reitz QTAILQ_INIT(&s->ops_in_flight); 199012aa4082SMax Reitz 19915ccac6f1SJohn Snow trace_mirror_start(bs, s, opaque); 1992da01ff7fSKevin Wolf job_start(&s->common.job); 1993cc19f177SVladimir Sementsov-Ogievskiy 1994cc19f177SVladimir Sementsov-Ogievskiy return &s->common; 19954ef85a9cSKevin Wolf 19964ef85a9cSKevin Wolf fail: 19974ef85a9cSKevin Wolf if (s) { 19987a25fcd0SMax Reitz /* Make sure this BDS does not go away until we have completed the graph 19997a25fcd0SMax Reitz * changes below */ 20007a25fcd0SMax Reitz bdrv_ref(mirror_top_bs); 20017a25fcd0SMax Reitz 20024ef85a9cSKevin Wolf g_free(s->replaces); 20034ef85a9cSKevin Wolf blk_unref(s->target); 2004429076e8SMax Reitz bs_opaque->job = NULL; 2005e917e2cbSAlberto Garcia if (s->dirty_bitmap) { 20065deb6cbdSVladimir Sementsov-Ogievskiy bdrv_release_dirty_bitmap(s->dirty_bitmap); 2007e917e2cbSAlberto Garcia } 20084ad35181SKevin Wolf job_early_fail(&s->common.job); 20094ef85a9cSKevin Wolf } 20104ef85a9cSKevin Wolf 2011f94dc3b4SMax Reitz bs_opaque->stop = true; 2012ccd6a379SKevin Wolf bdrv_drained_begin(bs); 20136bc30f19SStefan Hajnoczi bdrv_graph_wrlock(); 2014ccd6a379SKevin Wolf assert(mirror_top_bs->backing->bs == bs); 2015f94dc3b4SMax Reitz bdrv_child_refresh_perms(mirror_top_bs, mirror_top_bs->backing, 2016c1cef672SFam Zheng &error_abort); 2017ccd6a379SKevin Wolf bdrv_replace_node(mirror_top_bs, bs, &error_abort); 20186bc30f19SStefan Hajnoczi bdrv_graph_wrunlock(); 2019ccd6a379SKevin Wolf bdrv_drained_end(bs); 20207a25fcd0SMax Reitz 20217a25fcd0SMax Reitz bdrv_unref(mirror_top_bs); 2022cc19f177SVladimir Sementsov-Ogievskiy 2023cc19f177SVladimir Sementsov-Ogievskiy return NULL; 2024893f7ebaSPaolo Bonzini } 202503544a6eSFam Zheng 202671aa9867SAlberto Garcia void mirror_start(const char *job_id, BlockDriverState *bs, 202771aa9867SAlberto Garcia BlockDriverState *target, const char *replaces, 2028a1999b33SJohn Snow int creation_flags, int64_t speed, 2029a1999b33SJohn Snow uint32_t granularity, int64_t buf_size, 2030274fcceeSMax Reitz MirrorSyncMode mode, BlockMirrorBackingMode backing_mode, 2031*d17a34bfSEric Blake bool zero_target, bool target_is_zero, 2032274fcceeSMax Reitz BlockdevOnError on_source_error, 203303544a6eSFam Zheng BlockdevOnError on_target_error, 2034481debaaSMax Reitz bool unmap, const char *filter_node_name, 2035481debaaSMax Reitz MirrorCopyMode copy_mode, Error **errp) 203603544a6eSFam Zheng { 203703544a6eSFam Zheng BlockDriverState *base; 203803544a6eSFam Zheng 2039b4ad82aaSEmanuele Giuseppe Esposito GLOBAL_STATE_CODE(); 2040b4ad82aaSEmanuele Giuseppe Esposito 2041c8b56501SJohn Snow if ((mode == MIRROR_SYNC_MODE_INCREMENTAL) || 2042c8b56501SJohn Snow (mode == MIRROR_SYNC_MODE_BITMAP)) { 2043c8b56501SJohn Snow error_setg(errp, "Sync mode '%s' not supported", 2044c8b56501SJohn Snow MirrorSyncMode_str(mode)); 2045d58d8453SJohn Snow return; 2046d58d8453SJohn Snow } 2047ad74751fSKevin Wolf 2048ad74751fSKevin Wolf bdrv_graph_rdlock_main_loop(); 20493f072a7fSMax Reitz base = mode == MIRROR_SYNC_MODE_TOP ? bdrv_backing_chain_next(bs) : NULL; 2050ad74751fSKevin Wolf bdrv_graph_rdunlock_main_loop(); 2051ad74751fSKevin Wolf 2052a1999b33SJohn Snow mirror_start_job(job_id, bs, creation_flags, target, replaces, 20539474d97bSEric Blake speed, granularity, buf_size, mode, backing_mode, 2054*d17a34bfSEric Blake zero_target, 2055*d17a34bfSEric Blake target_is_zero, on_source_error, on_target_error, unmap, 20569474d97bSEric Blake NULL, NULL, &mirror_job_driver, base, false, 20577d99ae59SAlexander Ivanov filter_node_name, true, copy_mode, false, errp); 205803544a6eSFam Zheng } 205903544a6eSFam Zheng 2060cc19f177SVladimir Sementsov-Ogievskiy BlockJob *commit_active_start(const char *job_id, BlockDriverState *bs, 206147970dfbSJohn Snow BlockDriverState *base, int creation_flags, 206247970dfbSJohn Snow int64_t speed, BlockdevOnError on_error, 20630db832f4SKevin Wolf const char *filter_node_name, 206478bbd910SFam Zheng BlockCompletionFunc *cb, void *opaque, 206578bbd910SFam Zheng bool auto_complete, Error **errp) 206603544a6eSFam Zheng { 20671ba79388SAlberto Garcia bool base_read_only; 2068eb5becc1SVladimir Sementsov-Ogievskiy BlockJob *job; 20694da83585SJeff Cody 2070b4ad82aaSEmanuele Giuseppe Esposito GLOBAL_STATE_CODE(); 2071b4ad82aaSEmanuele Giuseppe Esposito 20721ba79388SAlberto Garcia base_read_only = bdrv_is_read_only(base); 20734da83585SJeff Cody 20741ba79388SAlberto Garcia if (base_read_only) { 20751ba79388SAlberto Garcia if (bdrv_reopen_set_read_only(base, false, errp) < 0) { 2076cc19f177SVladimir Sementsov-Ogievskiy return NULL; 207720a63d2cSFam Zheng } 20781ba79388SAlberto Garcia } 20794da83585SJeff Cody 2080eb5becc1SVladimir Sementsov-Ogievskiy job = mirror_start_job( 2081cc19f177SVladimir Sementsov-Ogievskiy job_id, bs, creation_flags, base, NULL, speed, 0, 0, 20829474d97bSEric Blake MIRROR_SYNC_MODE_TOP, MIRROR_LEAVE_BACKING_CHAIN, false, 2083*d17a34bfSEric Blake false, 208451ccfa2dSFam Zheng on_error, on_error, true, cb, opaque, 20859474d97bSEric Blake &commit_active_job_driver, base, auto_complete, 2086481debaaSMax Reitz filter_node_name, false, MIRROR_COPY_MODE_BACKGROUND, 20877d99ae59SAlexander Ivanov base_read_only, errp); 2088eb5becc1SVladimir Sementsov-Ogievskiy if (!job) { 20894da83585SJeff Cody goto error_restore_flags; 20904da83585SJeff Cody } 20914da83585SJeff Cody 2092eb5becc1SVladimir Sementsov-Ogievskiy return job; 20934da83585SJeff Cody 20944da83585SJeff Cody error_restore_flags: 20954da83585SJeff Cody /* ignore error and errp for bdrv_reopen, because we want to propagate 20964da83585SJeff Cody * the original error */ 20971ba79388SAlberto Garcia if (base_read_only) { 20981ba79388SAlberto Garcia bdrv_reopen_set_read_only(base, true, NULL); 20991ba79388SAlberto Garcia } 2100cc19f177SVladimir Sementsov-Ogievskiy return NULL; 210103544a6eSFam Zheng } 2102