]>
Commit | Line | Data |
---|---|---|
893f7eba PB |
1 | /* |
2 | * Image mirroring | |
3 | * | |
4 | * Copyright Red Hat, Inc. 2012 | |
5 | * | |
6 | * Authors: | |
7 | * Paolo Bonzini <pbonzini@redhat.com> | |
8 | * | |
9 | * This work is licensed under the terms of the GNU LGPL, version 2 or later. | |
10 | * See the COPYING.LIB file in the top-level directory. | |
11 | * | |
12 | */ | |
13 | ||
80c71a24 | 14 | #include "qemu/osdep.h" |
fd4a6493 | 15 | #include "qemu/cutils.h" |
12aa4082 | 16 | #include "qemu/coroutine.h" |
1181e19a | 17 | #include "qemu/range.h" |
893f7eba | 18 | #include "trace.h" |
c87621ea | 19 | #include "block/blockjob_int.h" |
737e150e | 20 | #include "block/block_int.h" |
373340b2 | 21 | #include "sysemu/block-backend.h" |
da34e65c | 22 | #include "qapi/error.h" |
cc7a8ea7 | 23 | #include "qapi/qmp/qerror.h" |
893f7eba | 24 | #include "qemu/ratelimit.h" |
b812f671 | 25 | #include "qemu/bitmap.h" |
893f7eba | 26 | |
402a4741 | 27 | #define MAX_IN_FLIGHT 16 |
b436982f EB |
28 | #define MAX_IO_BYTES (1 << 20) /* 1 Mb */ |
29 | #define DEFAULT_MIRROR_BUF_SIZE (MAX_IN_FLIGHT * MAX_IO_BYTES) | |
402a4741 PB |
30 | |
31 | /* The mirroring buffer is a list of granularity-sized chunks. | |
32 | * Free chunks are organized in a list. | |
33 | */ | |
34 | typedef struct MirrorBuffer { | |
35 | QSIMPLEQ_ENTRY(MirrorBuffer) next; | |
36 | } MirrorBuffer; | |
893f7eba | 37 | |
12aa4082 HR |
38 | typedef struct MirrorOp MirrorOp; |
39 | ||
893f7eba PB |
40 | typedef struct MirrorBlockJob { |
41 | BlockJob common; | |
e253f4b8 | 42 | BlockBackend *target; |
4ef85a9c | 43 | BlockDriverState *mirror_top_bs; |
5bc361b8 | 44 | BlockDriverState *base; |
4ef85a9c | 45 | |
09158f00 BC |
46 | /* The name of the graph node to replace */ |
47 | char *replaces; | |
48 | /* The BDS to replace */ | |
49 | BlockDriverState *to_replace; | |
50 | /* Used to block operations on the drive-mirror-replace target */ | |
51 | Error *replace_blocker; | |
03544a6e | 52 | bool is_none_mode; |
274fccee | 53 | BlockMirrorBackingMode backing_mode; |
d06107ad | 54 | MirrorCopyMode copy_mode; |
b952b558 | 55 | BlockdevOnError on_source_error, on_target_error; |
d63ffd87 | 56 | bool synced; |
d06107ad HR |
57 | /* Set when the target is synced (dirty bitmap is clean, nothing |
58 | * in flight) and the job is running in active mode */ | |
59 | bool actively_synced; | |
d63ffd87 | 60 | bool should_complete; |
eee13dfe | 61 | int64_t granularity; |
b812f671 | 62 | size_t buf_size; |
b21c7652 | 63 | int64_t bdev_length; |
b812f671 | 64 | unsigned long *cow_bitmap; |
e4654d2d | 65 | BdrvDirtyBitmap *dirty_bitmap; |
dc162c8e | 66 | BdrvDirtyBitmapIter *dbi; |
893f7eba | 67 | uint8_t *buf; |
402a4741 PB |
68 | QSIMPLEQ_HEAD(, MirrorBuffer) buf_free; |
69 | int buf_free_count; | |
bd48bde8 | 70 | |
49efb1f5 | 71 | uint64_t last_pause_ns; |
402a4741 | 72 | unsigned long *in_flight_bitmap; |
bd48bde8 | 73 | int in_flight; |
b436982f | 74 | int64_t bytes_in_flight; |
12aa4082 | 75 | QTAILQ_HEAD(MirrorOpList, MirrorOp) ops_in_flight; |
bd48bde8 | 76 | int ret; |
0fc9f8ea | 77 | bool unmap; |
b436982f | 78 | int target_cluster_size; |
e5b43573 | 79 | int max_iov; |
90ab48eb | 80 | bool initial_zeroing_ongoing; |
d06107ad | 81 | int in_active_write_counter; |
737efc1e | 82 | bool prepared; |
893f7eba PB |
83 | } MirrorBlockJob; |
84 | ||
429076e8 HR |
85 | typedef struct MirrorBDSOpaque { |
86 | MirrorBlockJob *job; | |
87 | } MirrorBDSOpaque; | |
88 | ||
12aa4082 | 89 | struct MirrorOp { |
bd48bde8 PB |
90 | MirrorBlockJob *s; |
91 | QEMUIOVector qiov; | |
b436982f EB |
92 | int64_t offset; |
93 | uint64_t bytes; | |
2e1990b2 HR |
94 | |
95 | /* The pointee is set by mirror_co_read(), mirror_co_zero(), and | |
96 | * mirror_co_discard() before yielding for the first time */ | |
97 | int64_t *bytes_handled; | |
12aa4082 | 98 | |
1181e19a | 99 | bool is_pseudo_op; |
d06107ad | 100 | bool is_active_write; |
12aa4082 HR |
101 | CoQueue waiting_requests; |
102 | ||
103 | QTAILQ_ENTRY(MirrorOp) next; | |
104 | }; | |
bd48bde8 | 105 | |
4295c5fc HR |
106 | typedef enum MirrorMethod { |
107 | MIRROR_METHOD_COPY, | |
108 | MIRROR_METHOD_ZERO, | |
109 | MIRROR_METHOD_DISCARD, | |
110 | } MirrorMethod; | |
111 | ||
b952b558 PB |
112 | static BlockErrorAction mirror_error_action(MirrorBlockJob *s, bool read, |
113 | int error) | |
114 | { | |
115 | s->synced = false; | |
d06107ad | 116 | s->actively_synced = false; |
b952b558 | 117 | if (read) { |
81e254dc KW |
118 | return block_job_error_action(&s->common, s->on_source_error, |
119 | true, error); | |
b952b558 | 120 | } else { |
81e254dc KW |
121 | return block_job_error_action(&s->common, s->on_target_error, |
122 | false, error); | |
b952b558 PB |
123 | } |
124 | } | |
125 | ||
1181e19a HR |
126 | static void coroutine_fn mirror_wait_on_conflicts(MirrorOp *self, |
127 | MirrorBlockJob *s, | |
128 | uint64_t offset, | |
129 | uint64_t bytes) | |
130 | { | |
131 | uint64_t self_start_chunk = offset / s->granularity; | |
132 | uint64_t self_end_chunk = DIV_ROUND_UP(offset + bytes, s->granularity); | |
133 | uint64_t self_nb_chunks = self_end_chunk - self_start_chunk; | |
134 | ||
135 | while (find_next_bit(s->in_flight_bitmap, self_end_chunk, | |
136 | self_start_chunk) < self_end_chunk && | |
137 | s->ret >= 0) | |
138 | { | |
139 | MirrorOp *op; | |
140 | ||
141 | QTAILQ_FOREACH(op, &s->ops_in_flight, next) { | |
142 | uint64_t op_start_chunk = op->offset / s->granularity; | |
143 | uint64_t op_nb_chunks = DIV_ROUND_UP(op->offset + op->bytes, | |
144 | s->granularity) - | |
145 | op_start_chunk; | |
146 | ||
147 | if (op == self) { | |
148 | continue; | |
149 | } | |
150 | ||
151 | if (ranges_overlap(self_start_chunk, self_nb_chunks, | |
152 | op_start_chunk, op_nb_chunks)) | |
153 | { | |
154 | qemu_co_queue_wait(&op->waiting_requests, NULL); | |
155 | break; | |
156 | } | |
157 | } | |
158 | } | |
159 | } | |
160 | ||
2e1990b2 | 161 | static void coroutine_fn mirror_iteration_done(MirrorOp *op, int ret) |
bd48bde8 PB |
162 | { |
163 | MirrorBlockJob *s = op->s; | |
402a4741 | 164 | struct iovec *iov; |
bd48bde8 | 165 | int64_t chunk_num; |
b436982f | 166 | int i, nb_chunks; |
bd48bde8 | 167 | |
b436982f | 168 | trace_mirror_iteration_done(s, op->offset, op->bytes, ret); |
bd48bde8 PB |
169 | |
170 | s->in_flight--; | |
b436982f | 171 | s->bytes_in_flight -= op->bytes; |
402a4741 PB |
172 | iov = op->qiov.iov; |
173 | for (i = 0; i < op->qiov.niov; i++) { | |
174 | MirrorBuffer *buf = (MirrorBuffer *) iov[i].iov_base; | |
175 | QSIMPLEQ_INSERT_TAIL(&s->buf_free, buf, next); | |
176 | s->buf_free_count++; | |
177 | } | |
178 | ||
b436982f EB |
179 | chunk_num = op->offset / s->granularity; |
180 | nb_chunks = DIV_ROUND_UP(op->bytes, s->granularity); | |
12aa4082 | 181 | |
402a4741 | 182 | bitmap_clear(s->in_flight_bitmap, chunk_num, nb_chunks); |
12aa4082 | 183 | QTAILQ_REMOVE(&s->ops_in_flight, op, next); |
b21c7652 HR |
184 | if (ret >= 0) { |
185 | if (s->cow_bitmap) { | |
186 | bitmap_set(s->cow_bitmap, chunk_num, nb_chunks); | |
187 | } | |
90ab48eb | 188 | if (!s->initial_zeroing_ongoing) { |
30a5c887 | 189 | job_progress_update(&s->common.job, op->bytes); |
90ab48eb | 190 | } |
bd48bde8 | 191 | } |
6df3bf8e | 192 | qemu_iovec_destroy(&op->qiov); |
7b770c72 | 193 | |
12aa4082 HR |
194 | qemu_co_queue_restart_all(&op->waiting_requests); |
195 | g_free(op); | |
bd48bde8 PB |
196 | } |
197 | ||
2e1990b2 | 198 | static void coroutine_fn mirror_write_complete(MirrorOp *op, int ret) |
bd48bde8 | 199 | { |
bd48bde8 | 200 | MirrorBlockJob *s = op->s; |
b9e413dd | 201 | |
bd48bde8 | 202 | if (ret < 0) { |
bd48bde8 PB |
203 | BlockErrorAction action; |
204 | ||
e0d7f73e | 205 | bdrv_set_dirty_bitmap(s->dirty_bitmap, op->offset, op->bytes); |
bd48bde8 | 206 | action = mirror_error_action(s, false, -ret); |
a589569f | 207 | if (action == BLOCK_ERROR_ACTION_REPORT && s->ret >= 0) { |
bd48bde8 PB |
208 | s->ret = ret; |
209 | } | |
210 | } | |
d12ade57 | 211 | |
bd48bde8 PB |
212 | mirror_iteration_done(op, ret); |
213 | } | |
214 | ||
2e1990b2 | 215 | static void coroutine_fn mirror_read_complete(MirrorOp *op, int ret) |
bd48bde8 | 216 | { |
bd48bde8 | 217 | MirrorBlockJob *s = op->s; |
b9e413dd | 218 | |
bd48bde8 | 219 | if (ret < 0) { |
bd48bde8 PB |
220 | BlockErrorAction action; |
221 | ||
e0d7f73e | 222 | bdrv_set_dirty_bitmap(s->dirty_bitmap, op->offset, op->bytes); |
bd48bde8 | 223 | action = mirror_error_action(s, true, -ret); |
a589569f | 224 | if (action == BLOCK_ERROR_ACTION_REPORT && s->ret >= 0) { |
bd48bde8 PB |
225 | s->ret = ret; |
226 | } | |
227 | ||
228 | mirror_iteration_done(op, ret); | |
d12ade57 | 229 | return; |
bd48bde8 | 230 | } |
d12ade57 VSO |
231 | |
232 | ret = blk_co_pwritev(s->target, op->offset, op->qiov.size, &op->qiov, 0); | |
233 | mirror_write_complete(op, ret); | |
bd48bde8 PB |
234 | } |
235 | ||
782d97ef EB |
236 | /* Clip bytes relative to offset to not exceed end-of-file */ |
237 | static inline int64_t mirror_clip_bytes(MirrorBlockJob *s, | |
238 | int64_t offset, | |
239 | int64_t bytes) | |
240 | { | |
241 | return MIN(bytes, s->bdev_length - offset); | |
242 | } | |
243 | ||
782d97ef EB |
244 | /* Round offset and/or bytes to target cluster if COW is needed, and |
245 | * return the offset of the adjusted tail against original. */ | |
246 | static int mirror_cow_align(MirrorBlockJob *s, int64_t *offset, | |
ae4cc877 | 247 | uint64_t *bytes) |
893f7eba | 248 | { |
e5b43573 FZ |
249 | bool need_cow; |
250 | int ret = 0; | |
782d97ef | 251 | int64_t align_offset = *offset; |
7cfd5275 | 252 | int64_t align_bytes = *bytes; |
782d97ef | 253 | int max_bytes = s->granularity * s->max_iov; |
e5b43573 | 254 | |
782d97ef EB |
255 | need_cow = !test_bit(*offset / s->granularity, s->cow_bitmap); |
256 | need_cow |= !test_bit((*offset + *bytes - 1) / s->granularity, | |
e5b43573 FZ |
257 | s->cow_bitmap); |
258 | if (need_cow) { | |
782d97ef EB |
259 | bdrv_round_to_clusters(blk_bs(s->target), *offset, *bytes, |
260 | &align_offset, &align_bytes); | |
e5b43573 | 261 | } |
3515727f | 262 | |
782d97ef EB |
263 | if (align_bytes > max_bytes) { |
264 | align_bytes = max_bytes; | |
e5b43573 | 265 | if (need_cow) { |
782d97ef | 266 | align_bytes = QEMU_ALIGN_DOWN(align_bytes, s->target_cluster_size); |
e5b43573 | 267 | } |
8f0720ec | 268 | } |
782d97ef | 269 | /* Clipping may result in align_bytes unaligned to chunk boundary, but |
4150ae60 | 270 | * that doesn't matter because it's already the end of source image. */ |
782d97ef | 271 | align_bytes = mirror_clip_bytes(s, align_offset, align_bytes); |
8f0720ec | 272 | |
782d97ef EB |
273 | ret = align_offset + align_bytes - (*offset + *bytes); |
274 | *offset = align_offset; | |
275 | *bytes = align_bytes; | |
e5b43573 FZ |
276 | assert(ret >= 0); |
277 | return ret; | |
278 | } | |
279 | ||
d06107ad | 280 | static inline void mirror_wait_for_any_operation(MirrorBlockJob *s, bool active) |
21cd917f | 281 | { |
12aa4082 HR |
282 | MirrorOp *op; |
283 | ||
1181e19a HR |
284 | QTAILQ_FOREACH(op, &s->ops_in_flight, next) { |
285 | /* Do not wait on pseudo ops, because it may in turn wait on | |
286 | * some other operation to start, which may in fact be the | |
287 | * caller of this function. Since there is only one pseudo op | |
288 | * at any given time, we will always find some real operation | |
289 | * to wait on. */ | |
d06107ad | 290 | if (!op->is_pseudo_op && op->is_active_write == active) { |
1181e19a HR |
291 | qemu_co_queue_wait(&op->waiting_requests, NULL); |
292 | return; | |
293 | } | |
294 | } | |
295 | abort(); | |
21cd917f FZ |
296 | } |
297 | ||
d06107ad HR |
298 | static inline void mirror_wait_for_free_in_flight_slot(MirrorBlockJob *s) |
299 | { | |
300 | /* Only non-active operations use up in-flight slots */ | |
301 | mirror_wait_for_any_operation(s, false); | |
302 | } | |
303 | ||
2e1990b2 HR |
304 | /* Perform a mirror copy operation. |
305 | * | |
306 | * *op->bytes_handled is set to the number of bytes copied after and | |
307 | * including offset, excluding any bytes copied prior to offset due | |
308 | * to alignment. This will be op->bytes if no alignment is necessary, | |
309 | * or (new_end - op->offset) if the tail is rounded up or down due to | |
310 | * alignment or buffer limit. | |
e5b43573 | 311 | */ |
2e1990b2 | 312 | static void coroutine_fn mirror_co_read(void *opaque) |
e5b43573 | 313 | { |
2e1990b2 HR |
314 | MirrorOp *op = opaque; |
315 | MirrorBlockJob *s = op->s; | |
ae4cc877 EB |
316 | int nb_chunks; |
317 | uint64_t ret; | |
ae4cc877 | 318 | uint64_t max_bytes; |
e5b43573 | 319 | |
ae4cc877 | 320 | max_bytes = s->granularity * s->max_iov; |
402a4741 | 321 | |
e5b43573 | 322 | /* We can only handle as much as buf_size at a time. */ |
2e1990b2 HR |
323 | op->bytes = MIN(s->buf_size, MIN(max_bytes, op->bytes)); |
324 | assert(op->bytes); | |
325 | assert(op->bytes < BDRV_REQUEST_MAX_BYTES); | |
326 | *op->bytes_handled = op->bytes; | |
402a4741 | 327 | |
e5b43573 | 328 | if (s->cow_bitmap) { |
2e1990b2 | 329 | *op->bytes_handled += mirror_cow_align(s, &op->offset, &op->bytes); |
e5b43573 | 330 | } |
2e1990b2 HR |
331 | /* Cannot exceed BDRV_REQUEST_MAX_BYTES + INT_MAX */ |
332 | assert(*op->bytes_handled <= UINT_MAX); | |
333 | assert(op->bytes <= s->buf_size); | |
ae4cc877 | 334 | /* The offset is granularity-aligned because: |
e5b43573 FZ |
335 | * 1) Caller passes in aligned values; |
336 | * 2) mirror_cow_align is used only when target cluster is larger. */ | |
2e1990b2 | 337 | assert(QEMU_IS_ALIGNED(op->offset, s->granularity)); |
ae4cc877 | 338 | /* The range is sector-aligned, since bdrv_getlength() rounds up. */ |
2e1990b2 HR |
339 | assert(QEMU_IS_ALIGNED(op->bytes, BDRV_SECTOR_SIZE)); |
340 | nb_chunks = DIV_ROUND_UP(op->bytes, s->granularity); | |
e5b43573 FZ |
341 | |
342 | while (s->buf_free_count < nb_chunks) { | |
2e1990b2 | 343 | trace_mirror_yield_in_flight(s, op->offset, s->in_flight); |
1181e19a | 344 | mirror_wait_for_free_in_flight_slot(s); |
b812f671 PB |
345 | } |
346 | ||
402a4741 PB |
347 | /* Now make a QEMUIOVector taking enough granularity-sized chunks |
348 | * from s->buf_free. | |
349 | */ | |
350 | qemu_iovec_init(&op->qiov, nb_chunks); | |
402a4741 PB |
351 | while (nb_chunks-- > 0) { |
352 | MirrorBuffer *buf = QSIMPLEQ_FIRST(&s->buf_free); | |
2e1990b2 | 353 | size_t remaining = op->bytes - op->qiov.size; |
5a0f6fd5 | 354 | |
402a4741 PB |
355 | QSIMPLEQ_REMOVE_HEAD(&s->buf_free, next); |
356 | s->buf_free_count--; | |
5a0f6fd5 | 357 | qemu_iovec_add(&op->qiov, buf, MIN(s->granularity, remaining)); |
402a4741 | 358 | } |
bd48bde8 | 359 | |
893f7eba | 360 | /* Copy the dirty cluster. */ |
bd48bde8 | 361 | s->in_flight++; |
2e1990b2 HR |
362 | s->bytes_in_flight += op->bytes; |
363 | trace_mirror_one_iteration(s, op->offset, op->bytes); | |
dcfb3beb | 364 | |
138f9fff HR |
365 | ret = bdrv_co_preadv(s->mirror_top_bs->backing, op->offset, op->bytes, |
366 | &op->qiov, 0); | |
2e1990b2 | 367 | mirror_read_complete(op, ret); |
e5b43573 FZ |
368 | } |
369 | ||
2e1990b2 | 370 | static void coroutine_fn mirror_co_zero(void *opaque) |
e5b43573 | 371 | { |
2e1990b2 HR |
372 | MirrorOp *op = opaque; |
373 | int ret; | |
e5b43573 | 374 | |
2e1990b2 HR |
375 | op->s->in_flight++; |
376 | op->s->bytes_in_flight += op->bytes; | |
377 | *op->bytes_handled = op->bytes; | |
e5b43573 | 378 | |
2e1990b2 HR |
379 | ret = blk_co_pwrite_zeroes(op->s->target, op->offset, op->bytes, |
380 | op->s->unmap ? BDRV_REQ_MAY_UNMAP : 0); | |
381 | mirror_write_complete(op, ret); | |
382 | } | |
383 | ||
384 | static void coroutine_fn mirror_co_discard(void *opaque) | |
385 | { | |
386 | MirrorOp *op = opaque; | |
387 | int ret; | |
388 | ||
389 | op->s->in_flight++; | |
390 | op->s->bytes_in_flight += op->bytes; | |
391 | *op->bytes_handled = op->bytes; | |
392 | ||
393 | ret = blk_co_pdiscard(op->s->target, op->offset, op->bytes); | |
394 | mirror_write_complete(op, ret); | |
e5b43573 FZ |
395 | } |
396 | ||
4295c5fc HR |
397 | static unsigned mirror_perform(MirrorBlockJob *s, int64_t offset, |
398 | unsigned bytes, MirrorMethod mirror_method) | |
399 | { | |
2e1990b2 HR |
400 | MirrorOp *op; |
401 | Coroutine *co; | |
402 | int64_t bytes_handled = -1; | |
403 | ||
404 | op = g_new(MirrorOp, 1); | |
405 | *op = (MirrorOp){ | |
406 | .s = s, | |
407 | .offset = offset, | |
408 | .bytes = bytes, | |
409 | .bytes_handled = &bytes_handled, | |
410 | }; | |
12aa4082 | 411 | qemu_co_queue_init(&op->waiting_requests); |
2e1990b2 | 412 | |
4295c5fc HR |
413 | switch (mirror_method) { |
414 | case MIRROR_METHOD_COPY: | |
2e1990b2 HR |
415 | co = qemu_coroutine_create(mirror_co_read, op); |
416 | break; | |
4295c5fc | 417 | case MIRROR_METHOD_ZERO: |
2e1990b2 HR |
418 | co = qemu_coroutine_create(mirror_co_zero, op); |
419 | break; | |
4295c5fc | 420 | case MIRROR_METHOD_DISCARD: |
2e1990b2 HR |
421 | co = qemu_coroutine_create(mirror_co_discard, op); |
422 | break; | |
4295c5fc HR |
423 | default: |
424 | abort(); | |
425 | } | |
2e1990b2 | 426 | |
12aa4082 | 427 | QTAILQ_INSERT_TAIL(&s->ops_in_flight, op, next); |
2e1990b2 HR |
428 | qemu_coroutine_enter(co); |
429 | /* At this point, ownership of op has been moved to the coroutine | |
430 | * and the object may already be freed */ | |
431 | ||
432 | /* Assert that this value has been set */ | |
433 | assert(bytes_handled >= 0); | |
434 | ||
435 | /* Same assertion as in mirror_co_read() (and for mirror_co_read() | |
436 | * and mirror_co_discard(), bytes_handled == op->bytes, which | |
437 | * is the @bytes parameter given to this function) */ | |
438 | assert(bytes_handled <= UINT_MAX); | |
439 | return bytes_handled; | |
4295c5fc HR |
440 | } |
441 | ||
e5b43573 FZ |
442 | static uint64_t coroutine_fn mirror_iteration(MirrorBlockJob *s) |
443 | { | |
138f9fff | 444 | BlockDriverState *source = s->mirror_top_bs->backing->bs; |
1181e19a HR |
445 | MirrorOp *pseudo_op; |
446 | int64_t offset; | |
447 | uint64_t delay_ns = 0, ret = 0; | |
e5b43573 FZ |
448 | /* At least the first dirty chunk is mirrored in one iteration. */ |
449 | int nb_chunks = 1; | |
4b5004d9 | 450 | bool write_zeroes_ok = bdrv_can_write_zeroes_with_unmap(blk_bs(s->target)); |
b436982f | 451 | int max_io_bytes = MAX(s->buf_size / MAX_IN_FLIGHT, MAX_IO_BYTES); |
e5b43573 | 452 | |
b64bd51e | 453 | bdrv_dirty_bitmap_lock(s->dirty_bitmap); |
f798184c | 454 | offset = bdrv_dirty_iter_next(s->dbi); |
fb2ef791 | 455 | if (offset < 0) { |
dc162c8e | 456 | bdrv_set_dirty_iter(s->dbi, 0); |
f798184c | 457 | offset = bdrv_dirty_iter_next(s->dbi); |
9a46dba7 | 458 | trace_mirror_restart_iter(s, bdrv_get_dirty_count(s->dirty_bitmap)); |
fb2ef791 | 459 | assert(offset >= 0); |
e5b43573 | 460 | } |
b64bd51e | 461 | bdrv_dirty_bitmap_unlock(s->dirty_bitmap); |
e5b43573 | 462 | |
1181e19a | 463 | mirror_wait_on_conflicts(NULL, s, offset, 1); |
9c83625b | 464 | |
da01ff7f | 465 | job_pause_point(&s->common.job); |
565ac01f | 466 | |
e5b43573 FZ |
467 | /* Find the number of consective dirty chunks following the first dirty |
468 | * one, and wait for in flight requests in them. */ | |
b64bd51e | 469 | bdrv_dirty_bitmap_lock(s->dirty_bitmap); |
fb2ef791 | 470 | while (nb_chunks * s->granularity < s->buf_size) { |
dc162c8e | 471 | int64_t next_dirty; |
fb2ef791 EB |
472 | int64_t next_offset = offset + nb_chunks * s->granularity; |
473 | int64_t next_chunk = next_offset / s->granularity; | |
474 | if (next_offset >= s->bdev_length || | |
3b5d4df0 | 475 | !bdrv_get_dirty_locked(source, s->dirty_bitmap, next_offset)) { |
e5b43573 FZ |
476 | break; |
477 | } | |
478 | if (test_bit(next_chunk, s->in_flight_bitmap)) { | |
9c83625b | 479 | break; |
e5b43573 | 480 | } |
9c83625b | 481 | |
f798184c | 482 | next_dirty = bdrv_dirty_iter_next(s->dbi); |
fb2ef791 | 483 | if (next_dirty > next_offset || next_dirty < 0) { |
f27a2742 | 484 | /* The bitmap iterator's cache is stale, refresh it */ |
715a74d8 | 485 | bdrv_set_dirty_iter(s->dbi, next_offset); |
f798184c | 486 | next_dirty = bdrv_dirty_iter_next(s->dbi); |
f27a2742 | 487 | } |
fb2ef791 | 488 | assert(next_dirty == next_offset); |
9c83625b | 489 | nb_chunks++; |
e5b43573 FZ |
490 | } |
491 | ||
492 | /* Clear dirty bits before querying the block status, because | |
31826642 | 493 | * calling bdrv_block_status_above could yield - if some blocks are |
e5b43573 FZ |
494 | * marked dirty in this window, we need to know. |
495 | */ | |
e0d7f73e EB |
496 | bdrv_reset_dirty_bitmap_locked(s->dirty_bitmap, offset, |
497 | nb_chunks * s->granularity); | |
b64bd51e PB |
498 | bdrv_dirty_bitmap_unlock(s->dirty_bitmap); |
499 | ||
1181e19a HR |
500 | /* Before claiming an area in the in-flight bitmap, we have to |
501 | * create a MirrorOp for it so that conflicting requests can wait | |
502 | * for it. mirror_perform() will create the real MirrorOps later, | |
503 | * for now we just create a pseudo operation that will wake up all | |
504 | * conflicting requests once all real operations have been | |
505 | * launched. */ | |
506 | pseudo_op = g_new(MirrorOp, 1); | |
507 | *pseudo_op = (MirrorOp){ | |
508 | .offset = offset, | |
509 | .bytes = nb_chunks * s->granularity, | |
510 | .is_pseudo_op = true, | |
511 | }; | |
512 | qemu_co_queue_init(&pseudo_op->waiting_requests); | |
513 | QTAILQ_INSERT_TAIL(&s->ops_in_flight, pseudo_op, next); | |
514 | ||
fb2ef791 EB |
515 | bitmap_set(s->in_flight_bitmap, offset / s->granularity, nb_chunks); |
516 | while (nb_chunks > 0 && offset < s->bdev_length) { | |
31826642 | 517 | int ret; |
7cfd5275 | 518 | int64_t io_bytes; |
f3e4ce4a | 519 | int64_t io_bytes_acct; |
4295c5fc | 520 | MirrorMethod mirror_method = MIRROR_METHOD_COPY; |
e5b43573 | 521 | |
fb2ef791 | 522 | assert(!(offset % s->granularity)); |
31826642 EB |
523 | ret = bdrv_block_status_above(source, NULL, offset, |
524 | nb_chunks * s->granularity, | |
525 | &io_bytes, NULL, NULL); | |
e5b43573 | 526 | if (ret < 0) { |
fb2ef791 | 527 | io_bytes = MIN(nb_chunks * s->granularity, max_io_bytes); |
0965a41e | 528 | } else if (ret & BDRV_BLOCK_DATA) { |
fb2ef791 | 529 | io_bytes = MIN(io_bytes, max_io_bytes); |
e5b43573 FZ |
530 | } |
531 | ||
fb2ef791 EB |
532 | io_bytes -= io_bytes % s->granularity; |
533 | if (io_bytes < s->granularity) { | |
534 | io_bytes = s->granularity; | |
e5b43573 | 535 | } else if (ret >= 0 && !(ret & BDRV_BLOCK_DATA)) { |
fb2ef791 | 536 | int64_t target_offset; |
7cfd5275 | 537 | int64_t target_bytes; |
fb2ef791 EB |
538 | bdrv_round_to_clusters(blk_bs(s->target), offset, io_bytes, |
539 | &target_offset, &target_bytes); | |
540 | if (target_offset == offset && | |
541 | target_bytes == io_bytes) { | |
e5b43573 FZ |
542 | mirror_method = ret & BDRV_BLOCK_ZERO ? |
543 | MIRROR_METHOD_ZERO : | |
544 | MIRROR_METHOD_DISCARD; | |
545 | } | |
546 | } | |
547 | ||
cf56a3c6 | 548 | while (s->in_flight >= MAX_IN_FLIGHT) { |
fb2ef791 | 549 | trace_mirror_yield_in_flight(s, offset, s->in_flight); |
1181e19a | 550 | mirror_wait_for_free_in_flight_slot(s); |
cf56a3c6 DL |
551 | } |
552 | ||
dbaa7b57 | 553 | if (s->ret < 0) { |
1181e19a HR |
554 | ret = 0; |
555 | goto fail; | |
dbaa7b57 VSO |
556 | } |
557 | ||
fb2ef791 | 558 | io_bytes = mirror_clip_bytes(s, offset, io_bytes); |
4295c5fc HR |
559 | io_bytes = mirror_perform(s, offset, io_bytes, mirror_method); |
560 | if (mirror_method != MIRROR_METHOD_COPY && write_zeroes_ok) { | |
561 | io_bytes_acct = 0; | |
562 | } else { | |
563 | io_bytes_acct = io_bytes; | |
e5b43573 | 564 | } |
fb2ef791 EB |
565 | assert(io_bytes); |
566 | offset += io_bytes; | |
567 | nb_chunks -= DIV_ROUND_UP(io_bytes, s->granularity); | |
dee81d51 | 568 | delay_ns = block_job_ratelimit_get_delay(&s->common, io_bytes_acct); |
dcfb3beb | 569 | } |
1181e19a HR |
570 | |
571 | ret = delay_ns; | |
572 | fail: | |
573 | QTAILQ_REMOVE(&s->ops_in_flight, pseudo_op, next); | |
574 | qemu_co_queue_restart_all(&pseudo_op->waiting_requests); | |
575 | g_free(pseudo_op); | |
576 | ||
577 | return ret; | |
bd48bde8 | 578 | } |
b952b558 | 579 | |
402a4741 PB |
580 | static void mirror_free_init(MirrorBlockJob *s) |
581 | { | |
582 | int granularity = s->granularity; | |
583 | size_t buf_size = s->buf_size; | |
584 | uint8_t *buf = s->buf; | |
585 | ||
586 | assert(s->buf_free_count == 0); | |
587 | QSIMPLEQ_INIT(&s->buf_free); | |
588 | while (buf_size != 0) { | |
589 | MirrorBuffer *cur = (MirrorBuffer *)buf; | |
590 | QSIMPLEQ_INSERT_TAIL(&s->buf_free, cur, next); | |
591 | s->buf_free_count++; | |
592 | buf_size -= granularity; | |
593 | buf += granularity; | |
594 | } | |
595 | } | |
596 | ||
bae8196d PB |
597 | /* This is also used for the .pause callback. There is no matching |
598 | * mirror_resume() because mirror_run() will begin iterating again | |
599 | * when the job is resumed. | |
600 | */ | |
601 | static void mirror_wait_for_all_io(MirrorBlockJob *s) | |
bd48bde8 PB |
602 | { |
603 | while (s->in_flight > 0) { | |
1181e19a | 604 | mirror_wait_for_free_in_flight_slot(s); |
bd48bde8 | 605 | } |
893f7eba PB |
606 | } |
607 | ||
737efc1e JS |
608 | /** |
609 | * mirror_exit_common: handle both abort() and prepare() cases. | |
610 | * for .prepare, returns 0 on success and -errno on failure. | |
611 | * for .abort cases, denoted by abort = true, MUST return 0. | |
612 | */ | |
613 | static int mirror_exit_common(Job *job) | |
5a7e7a0b | 614 | { |
1908a559 KW |
615 | MirrorBlockJob *s = container_of(job, MirrorBlockJob, common.job); |
616 | BlockJob *bjob = &s->common; | |
429076e8 | 617 | MirrorBDSOpaque *bs_opaque = s->mirror_top_bs->opaque; |
5a7e7a0b | 618 | AioContext *replace_aio_context = NULL; |
138f9fff | 619 | BlockDriverState *src = s->mirror_top_bs->backing->bs; |
e253f4b8 | 620 | BlockDriverState *target_bs = blk_bs(s->target); |
4ef85a9c | 621 | BlockDriverState *mirror_top_bs = s->mirror_top_bs; |
12fa4af6 | 622 | Error *local_err = NULL; |
737efc1e JS |
623 | bool abort = job->ret < 0; |
624 | int ret = 0; | |
625 | ||
626 | if (s->prepared) { | |
627 | return 0; | |
628 | } | |
629 | s->prepared = true; | |
3f09bfbc | 630 | |
2119882c PB |
631 | bdrv_release_dirty_bitmap(src, s->dirty_bitmap); |
632 | ||
7b508f6b JS |
633 | /* Make sure that the source BDS doesn't go away during bdrv_replace_node, |
634 | * before we can call bdrv_drained_end */ | |
3f09bfbc | 635 | bdrv_ref(src); |
4ef85a9c | 636 | bdrv_ref(mirror_top_bs); |
7d9fcb39 KW |
637 | bdrv_ref(target_bs); |
638 | ||
639 | /* Remove target parent that still uses BLK_PERM_WRITE/RESIZE before | |
640 | * inserting target_bs at s->to_replace, where we might not be able to get | |
63c8ef28 KW |
641 | * these permissions. |
642 | * | |
643 | * Note that blk_unref() alone doesn't necessarily drop permissions because | |
644 | * we might be running nested inside mirror_drain(), which takes an extra | |
645 | * reference, so use an explicit blk_set_perm() first. */ | |
646 | blk_set_perm(s->target, 0, BLK_PERM_ALL, &error_abort); | |
7d9fcb39 KW |
647 | blk_unref(s->target); |
648 | s->target = NULL; | |
4ef85a9c KW |
649 | |
650 | /* We don't access the source any more. Dropping any WRITE/RESIZE is | |
651 | * required before it could become a backing file of target_bs. */ | |
652 | bdrv_child_try_set_perm(mirror_top_bs->backing, 0, BLK_PERM_ALL, | |
653 | &error_abort); | |
737efc1e | 654 | if (!abort && s->backing_mode == MIRROR_SOURCE_BACKING_CHAIN) { |
4ef85a9c KW |
655 | BlockDriverState *backing = s->is_none_mode ? src : s->base; |
656 | if (backing_bs(target_bs) != backing) { | |
12fa4af6 KW |
657 | bdrv_set_backing_hd(target_bs, backing, &local_err); |
658 | if (local_err) { | |
659 | error_report_err(local_err); | |
7b508f6b | 660 | ret = -EPERM; |
12fa4af6 | 661 | } |
4ef85a9c KW |
662 | } |
663 | } | |
5a7e7a0b SH |
664 | |
665 | if (s->to_replace) { | |
666 | replace_aio_context = bdrv_get_aio_context(s->to_replace); | |
667 | aio_context_acquire(replace_aio_context); | |
668 | } | |
669 | ||
737efc1e JS |
670 | if (s->should_complete && !abort) { |
671 | BlockDriverState *to_replace = s->to_replace ?: src; | |
40365552 | 672 | |
e253f4b8 KW |
673 | if (bdrv_get_flags(target_bs) != bdrv_get_flags(to_replace)) { |
674 | bdrv_reopen(target_bs, bdrv_get_flags(to_replace), NULL); | |
5a7e7a0b | 675 | } |
b8804815 KW |
676 | |
677 | /* The mirror job has no requests in flight any more, but we need to | |
678 | * drain potential other users of the BDS before changing the graph. */ | |
e253f4b8 | 679 | bdrv_drained_begin(target_bs); |
5fe31c25 | 680 | bdrv_replace_node(to_replace, target_bs, &local_err); |
e253f4b8 | 681 | bdrv_drained_end(target_bs); |
5fe31c25 KW |
682 | if (local_err) { |
683 | error_report_err(local_err); | |
7b508f6b | 684 | ret = -EPERM; |
5fe31c25 | 685 | } |
5a7e7a0b SH |
686 | } |
687 | if (s->to_replace) { | |
688 | bdrv_op_unblock_all(s->to_replace, s->replace_blocker); | |
689 | error_free(s->replace_blocker); | |
690 | bdrv_unref(s->to_replace); | |
691 | } | |
692 | if (replace_aio_context) { | |
693 | aio_context_release(replace_aio_context); | |
694 | } | |
695 | g_free(s->replaces); | |
7d9fcb39 | 696 | bdrv_unref(target_bs); |
4ef85a9c KW |
697 | |
698 | /* Remove the mirror filter driver from the graph. Before this, get rid of | |
699 | * the blockers on the intermediate nodes so that the resulting state is | |
0bf74767 KW |
700 | * valid. Also give up permissions on mirror_top_bs->backing, which might |
701 | * block the removal. */ | |
1908a559 | 702 | block_job_remove_all_bdrv(bjob); |
c1cef672 FZ |
703 | bdrv_child_try_set_perm(mirror_top_bs->backing, 0, BLK_PERM_ALL, |
704 | &error_abort); | |
5fe31c25 | 705 | bdrv_replace_node(mirror_top_bs, backing_bs(mirror_top_bs), &error_abort); |
4ef85a9c KW |
706 | |
707 | /* We just changed the BDS the job BB refers to (with either or both of the | |
5fe31c25 KW |
708 | * bdrv_replace_node() calls), so switch the BB back so the cleanup does |
709 | * the right thing. We don't need any permissions any more now. */ | |
1908a559 KW |
710 | blk_remove_bs(bjob->blk); |
711 | blk_set_perm(bjob->blk, 0, BLK_PERM_ALL, &error_abort); | |
712 | blk_insert_bs(bjob->blk, mirror_top_bs, &error_abort); | |
4ef85a9c | 713 | |
429076e8 | 714 | bs_opaque->job = NULL; |
4ef85a9c | 715 | |
176c3699 | 716 | bdrv_drained_end(src); |
4ef85a9c | 717 | bdrv_unref(mirror_top_bs); |
3f09bfbc | 718 | bdrv_unref(src); |
7b508f6b | 719 | |
737efc1e JS |
720 | return ret; |
721 | } | |
722 | ||
723 | static int mirror_prepare(Job *job) | |
724 | { | |
725 | return mirror_exit_common(job); | |
726 | } | |
727 | ||
728 | static void mirror_abort(Job *job) | |
729 | { | |
730 | int ret = mirror_exit_common(job); | |
731 | assert(ret == 0); | |
5a7e7a0b SH |
732 | } |
733 | ||
49efb1f5 DL |
734 | static void mirror_throttle(MirrorBlockJob *s) |
735 | { | |
736 | int64_t now = qemu_clock_get_ns(QEMU_CLOCK_REALTIME); | |
737 | ||
18bb6928 | 738 | if (now - s->last_pause_ns > BLOCK_JOB_SLICE_TIME) { |
49efb1f5 | 739 | s->last_pause_ns = now; |
5d43e86e | 740 | job_sleep_ns(&s->common.job, 0); |
49efb1f5 | 741 | } else { |
da01ff7f | 742 | job_pause_point(&s->common.job); |
49efb1f5 DL |
743 | } |
744 | } | |
745 | ||
c0b363ad DL |
746 | static int coroutine_fn mirror_dirty_init(MirrorBlockJob *s) |
747 | { | |
23ca459a | 748 | int64_t offset; |
c0b363ad | 749 | BlockDriverState *base = s->base; |
138f9fff | 750 | BlockDriverState *bs = s->mirror_top_bs->backing->bs; |
c0b363ad | 751 | BlockDriverState *target_bs = blk_bs(s->target); |
23ca459a | 752 | int ret; |
51b0a488 | 753 | int64_t count; |
c0b363ad | 754 | |
b7d5062c | 755 | if (base == NULL && !bdrv_has_zero_init(target_bs)) { |
c7c2769c | 756 | if (!bdrv_can_write_zeroes_with_unmap(target_bs)) { |
e0d7f73e | 757 | bdrv_set_dirty_bitmap(s->dirty_bitmap, 0, s->bdev_length); |
c7c2769c DL |
758 | return 0; |
759 | } | |
760 | ||
90ab48eb | 761 | s->initial_zeroing_ongoing = true; |
23ca459a EB |
762 | for (offset = 0; offset < s->bdev_length; ) { |
763 | int bytes = MIN(s->bdev_length - offset, | |
764 | QEMU_ALIGN_DOWN(INT_MAX, s->granularity)); | |
c7c2769c DL |
765 | |
766 | mirror_throttle(s); | |
767 | ||
daa7f2f9 | 768 | if (job_is_cancelled(&s->common.job)) { |
90ab48eb | 769 | s->initial_zeroing_ongoing = false; |
c7c2769c DL |
770 | return 0; |
771 | } | |
772 | ||
773 | if (s->in_flight >= MAX_IN_FLIGHT) { | |
67adf4b3 EB |
774 | trace_mirror_yield(s, UINT64_MAX, s->buf_free_count, |
775 | s->in_flight); | |
1181e19a | 776 | mirror_wait_for_free_in_flight_slot(s); |
c7c2769c DL |
777 | continue; |
778 | } | |
779 | ||
4295c5fc | 780 | mirror_perform(s, offset, bytes, MIRROR_METHOD_ZERO); |
23ca459a | 781 | offset += bytes; |
c7c2769c DL |
782 | } |
783 | ||
bae8196d | 784 | mirror_wait_for_all_io(s); |
90ab48eb | 785 | s->initial_zeroing_ongoing = false; |
b7d5062c DL |
786 | } |
787 | ||
c0b363ad | 788 | /* First part, loop on the sectors and initialize the dirty bitmap. */ |
23ca459a | 789 | for (offset = 0; offset < s->bdev_length; ) { |
c0b363ad | 790 | /* Just to make sure we are not exceeding int limit. */ |
23ca459a EB |
791 | int bytes = MIN(s->bdev_length - offset, |
792 | QEMU_ALIGN_DOWN(INT_MAX, s->granularity)); | |
c0b363ad DL |
793 | |
794 | mirror_throttle(s); | |
795 | ||
daa7f2f9 | 796 | if (job_is_cancelled(&s->common.job)) { |
c0b363ad DL |
797 | return 0; |
798 | } | |
799 | ||
23ca459a | 800 | ret = bdrv_is_allocated_above(bs, base, offset, bytes, &count); |
c0b363ad DL |
801 | if (ret < 0) { |
802 | return ret; | |
803 | } | |
804 | ||
23ca459a | 805 | assert(count); |
b7d5062c | 806 | if (ret == 1) { |
23ca459a | 807 | bdrv_set_dirty_bitmap(s->dirty_bitmap, offset, count); |
c0b363ad | 808 | } |
23ca459a | 809 | offset += count; |
c0b363ad DL |
810 | } |
811 | return 0; | |
812 | } | |
813 | ||
bdffb31d PB |
814 | /* Called when going out of the streaming phase to flush the bulk of the |
815 | * data to the medium, or just before completing. | |
816 | */ | |
817 | static int mirror_flush(MirrorBlockJob *s) | |
818 | { | |
819 | int ret = blk_flush(s->target); | |
820 | if (ret < 0) { | |
821 | if (mirror_error_action(s, false, -ret) == BLOCK_ERROR_ACTION_REPORT) { | |
822 | s->ret = ret; | |
823 | } | |
824 | } | |
825 | return ret; | |
826 | } | |
827 | ||
f67432a2 | 828 | static int coroutine_fn mirror_run(Job *job, Error **errp) |
893f7eba | 829 | { |
f67432a2 | 830 | MirrorBlockJob *s = container_of(job, MirrorBlockJob, common.job); |
138f9fff | 831 | BlockDriverState *bs = s->mirror_top_bs->backing->bs; |
e253f4b8 | 832 | BlockDriverState *target_bs = blk_bs(s->target); |
9a0cec66 | 833 | bool need_drain = true; |
c0b363ad | 834 | int64_t length; |
b812f671 | 835 | BlockDriverInfo bdi; |
1d33936e JC |
836 | char backing_filename[2]; /* we only need 2 characters because we are only |
837 | checking for a NULL string */ | |
893f7eba | 838 | int ret = 0; |
893f7eba | 839 | |
daa7f2f9 | 840 | if (job_is_cancelled(&s->common.job)) { |
893f7eba PB |
841 | goto immediate_exit; |
842 | } | |
843 | ||
b21c7652 HR |
844 | s->bdev_length = bdrv_getlength(bs); |
845 | if (s->bdev_length < 0) { | |
846 | ret = s->bdev_length; | |
373df5b1 | 847 | goto immediate_exit; |
becc347e KW |
848 | } |
849 | ||
850 | /* Active commit must resize the base image if its size differs from the | |
851 | * active layer. */ | |
852 | if (s->base == blk_bs(s->target)) { | |
853 | int64_t base_length; | |
854 | ||
855 | base_length = blk_getlength(s->target); | |
856 | if (base_length < 0) { | |
857 | ret = base_length; | |
858 | goto immediate_exit; | |
859 | } | |
860 | ||
861 | if (s->bdev_length > base_length) { | |
3a691c50 HR |
862 | ret = blk_truncate(s->target, s->bdev_length, PREALLOC_MODE_OFF, |
863 | NULL); | |
becc347e KW |
864 | if (ret < 0) { |
865 | goto immediate_exit; | |
866 | } | |
867 | } | |
868 | } | |
869 | ||
870 | if (s->bdev_length == 0) { | |
2e1795b5 KW |
871 | /* Transition to the READY state and wait for complete. */ |
872 | job_transition_to_ready(&s->common.job); | |
9e48b025 | 873 | s->synced = true; |
d06107ad | 874 | s->actively_synced = true; |
daa7f2f9 | 875 | while (!job_is_cancelled(&s->common.job) && !s->should_complete) { |
198c49cc | 876 | job_yield(&s->common.job); |
9e48b025 | 877 | } |
daa7f2f9 | 878 | s->common.job.cancelled = false; |
9e48b025 | 879 | goto immediate_exit; |
893f7eba PB |
880 | } |
881 | ||
b21c7652 | 882 | length = DIV_ROUND_UP(s->bdev_length, s->granularity); |
402a4741 PB |
883 | s->in_flight_bitmap = bitmap_new(length); |
884 | ||
b812f671 PB |
885 | /* If we have no backing file yet in the destination, we cannot let |
886 | * the destination do COW. Instead, we copy sectors around the | |
887 | * dirty data if needed. We need a bitmap to do that. | |
888 | */ | |
e253f4b8 | 889 | bdrv_get_backing_filename(target_bs, backing_filename, |
b812f671 | 890 | sizeof(backing_filename)); |
e253f4b8 | 891 | if (!bdrv_get_info(target_bs, &bdi) && bdi.cluster_size) { |
b436982f EB |
892 | s->target_cluster_size = bdi.cluster_size; |
893 | } else { | |
894 | s->target_cluster_size = BDRV_SECTOR_SIZE; | |
e5b43573 | 895 | } |
b436982f EB |
896 | if (backing_filename[0] && !target_bs->backing && |
897 | s->granularity < s->target_cluster_size) { | |
898 | s->buf_size = MAX(s->buf_size, s->target_cluster_size); | |
e5b43573 | 899 | s->cow_bitmap = bitmap_new(length); |
b812f671 | 900 | } |
e253f4b8 | 901 | s->max_iov = MIN(bs->bl.max_iov, target_bs->bl.max_iov); |
b812f671 | 902 | |
7504edf4 KW |
903 | s->buf = qemu_try_blockalign(bs, s->buf_size); |
904 | if (s->buf == NULL) { | |
905 | ret = -ENOMEM; | |
906 | goto immediate_exit; | |
907 | } | |
908 | ||
402a4741 | 909 | mirror_free_init(s); |
893f7eba | 910 | |
49efb1f5 | 911 | s->last_pause_ns = qemu_clock_get_ns(QEMU_CLOCK_REALTIME); |
03544a6e | 912 | if (!s->is_none_mode) { |
c0b363ad | 913 | ret = mirror_dirty_init(s); |
daa7f2f9 | 914 | if (ret < 0 || job_is_cancelled(&s->common.job)) { |
c0b363ad | 915 | goto immediate_exit; |
893f7eba PB |
916 | } |
917 | } | |
918 | ||
dc162c8e | 919 | assert(!s->dbi); |
715a74d8 | 920 | s->dbi = bdrv_dirty_iter_new(s->dirty_bitmap); |
893f7eba | 921 | for (;;) { |
cc8c9d6c | 922 | uint64_t delay_ns = 0; |
49efb1f5 | 923 | int64_t cnt, delta; |
893f7eba PB |
924 | bool should_complete; |
925 | ||
d06107ad HR |
926 | /* Do not start passive operations while there are active |
927 | * writes in progress */ | |
928 | while (s->in_active_write_counter) { | |
929 | mirror_wait_for_any_operation(s, true); | |
930 | } | |
931 | ||
bd48bde8 PB |
932 | if (s->ret < 0) { |
933 | ret = s->ret; | |
934 | goto immediate_exit; | |
935 | } | |
936 | ||
da01ff7f | 937 | job_pause_point(&s->common.job); |
565ac01f | 938 | |
20dca810 | 939 | cnt = bdrv_get_dirty_count(s->dirty_bitmap); |
05df8a6a KW |
940 | /* cnt is the number of dirty bytes remaining and s->bytes_in_flight is |
941 | * the number of bytes currently being processed; together those are | |
942 | * the current remaining operation length */ | |
30a5c887 | 943 | job_progress_set_remaining(&s->common.job, s->bytes_in_flight + cnt); |
bd48bde8 PB |
944 | |
945 | /* Note that even when no rate limit is applied we need to yield | |
a7282330 | 946 | * periodically with no pending I/O so that bdrv_drain_all() returns. |
18bb6928 KW |
947 | * We do so every BLKOCK_JOB_SLICE_TIME nanoseconds, or when there is |
948 | * an error, or when the source is clean, whichever comes first. */ | |
49efb1f5 | 949 | delta = qemu_clock_get_ns(QEMU_CLOCK_REALTIME) - s->last_pause_ns; |
18bb6928 | 950 | if (delta < BLOCK_JOB_SLICE_TIME && |
bd48bde8 | 951 | s->common.iostatus == BLOCK_DEVICE_IO_STATUS_OK) { |
cf56a3c6 | 952 | if (s->in_flight >= MAX_IN_FLIGHT || s->buf_free_count == 0 || |
402a4741 | 953 | (cnt == 0 && s->in_flight > 0)) { |
9a46dba7 | 954 | trace_mirror_yield(s, cnt, s->buf_free_count, s->in_flight); |
1181e19a | 955 | mirror_wait_for_free_in_flight_slot(s); |
bd48bde8 PB |
956 | continue; |
957 | } else if (cnt != 0) { | |
cc8c9d6c | 958 | delay_ns = mirror_iteration(s); |
893f7eba | 959 | } |
893f7eba PB |
960 | } |
961 | ||
962 | should_complete = false; | |
bd48bde8 | 963 | if (s->in_flight == 0 && cnt == 0) { |
893f7eba | 964 | trace_mirror_before_flush(s); |
bdffb31d PB |
965 | if (!s->synced) { |
966 | if (mirror_flush(s) < 0) { | |
967 | /* Go check s->ret. */ | |
968 | continue; | |
b952b558 | 969 | } |
b952b558 PB |
970 | /* We're out of the streaming phase. From now on, if the job |
971 | * is cancelled we will actually complete all pending I/O and | |
972 | * report completion. This way, block-job-cancel will leave | |
973 | * the target in a consistent state. | |
974 | */ | |
2e1795b5 | 975 | job_transition_to_ready(&s->common.job); |
bdffb31d | 976 | s->synced = true; |
d06107ad HR |
977 | if (s->copy_mode != MIRROR_COPY_MODE_BACKGROUND) { |
978 | s->actively_synced = true; | |
979 | } | |
d63ffd87 | 980 | } |
bdffb31d PB |
981 | |
982 | should_complete = s->should_complete || | |
daa7f2f9 | 983 | job_is_cancelled(&s->common.job); |
bdffb31d | 984 | cnt = bdrv_get_dirty_count(s->dirty_bitmap); |
893f7eba PB |
985 | } |
986 | ||
987 | if (cnt == 0 && should_complete) { | |
988 | /* The dirty bitmap is not updated while operations are pending. | |
989 | * If we're about to exit, wait for pending operations before | |
990 | * calling bdrv_get_dirty_count(bs), or we may exit while the | |
991 | * source has dirty data to copy! | |
992 | * | |
993 | * Note that I/O can be submitted by the guest while | |
9a0cec66 PB |
994 | * mirror_populate runs, so pause it now. Before deciding |
995 | * whether to switch to target check one last time if I/O has | |
996 | * come in the meanwhile, and if not flush the data to disk. | |
893f7eba | 997 | */ |
9a46dba7 | 998 | trace_mirror_before_drain(s, cnt); |
9a0cec66 PB |
999 | |
1000 | bdrv_drained_begin(bs); | |
20dca810 | 1001 | cnt = bdrv_get_dirty_count(s->dirty_bitmap); |
bdffb31d | 1002 | if (cnt > 0 || mirror_flush(s) < 0) { |
9a0cec66 PB |
1003 | bdrv_drained_end(bs); |
1004 | continue; | |
1005 | } | |
1006 | ||
1007 | /* The two disks are in sync. Exit and report successful | |
1008 | * completion. | |
1009 | */ | |
1010 | assert(QLIST_EMPTY(&bs->tracked_requests)); | |
daa7f2f9 | 1011 | s->common.job.cancelled = false; |
9a0cec66 PB |
1012 | need_drain = false; |
1013 | break; | |
893f7eba PB |
1014 | } |
1015 | ||
1016 | ret = 0; | |
ddc4115e SH |
1017 | |
1018 | if (s->synced && !should_complete) { | |
18bb6928 KW |
1019 | delay_ns = (s->in_flight == 0 && |
1020 | cnt == 0 ? BLOCK_JOB_SLICE_TIME : 0); | |
ddc4115e | 1021 | } |
9a46dba7 | 1022 | trace_mirror_before_sleep(s, cnt, s->synced, delay_ns); |
5d43e86e | 1023 | job_sleep_ns(&s->common.job, delay_ns); |
daa7f2f9 | 1024 | if (job_is_cancelled(&s->common.job) && |
004e95df | 1025 | (!s->synced || s->common.job.force_cancel)) |
eb36639f | 1026 | { |
b76e4458 | 1027 | break; |
893f7eba | 1028 | } |
49efb1f5 | 1029 | s->last_pause_ns = qemu_clock_get_ns(QEMU_CLOCK_REALTIME); |
893f7eba PB |
1030 | } |
1031 | ||
1032 | immediate_exit: | |
bd48bde8 PB |
1033 | if (s->in_flight > 0) { |
1034 | /* We get here only if something went wrong. Either the job failed, | |
1035 | * or it was cancelled prematurely so that we do not guarantee that | |
1036 | * the target is a copy of the source. | |
1037 | */ | |
004e95df | 1038 | assert(ret < 0 || ((s->common.job.force_cancel || !s->synced) && |
daa7f2f9 | 1039 | job_is_cancelled(&s->common.job))); |
9a0cec66 | 1040 | assert(need_drain); |
bae8196d | 1041 | mirror_wait_for_all_io(s); |
bd48bde8 PB |
1042 | } |
1043 | ||
1044 | assert(s->in_flight == 0); | |
7191bf31 | 1045 | qemu_vfree(s->buf); |
b812f671 | 1046 | g_free(s->cow_bitmap); |
402a4741 | 1047 | g_free(s->in_flight_bitmap); |
dc162c8e | 1048 | bdrv_dirty_iter_free(s->dbi); |
5a7e7a0b | 1049 | |
9a0cec66 PB |
1050 | if (need_drain) { |
1051 | bdrv_drained_begin(bs); | |
1052 | } | |
f67432a2 | 1053 | |
f67432a2 | 1054 | return ret; |
893f7eba PB |
1055 | } |
1056 | ||
3453d972 | 1057 | static void mirror_complete(Job *job, Error **errp) |
d63ffd87 | 1058 | { |
3453d972 | 1059 | MirrorBlockJob *s = container_of(job, MirrorBlockJob, common.job); |
4ef85a9c | 1060 | BlockDriverState *target; |
274fccee | 1061 | |
274fccee | 1062 | target = blk_bs(s->target); |
d63ffd87 | 1063 | |
d63ffd87 | 1064 | if (!s->synced) { |
9df229c3 | 1065 | error_setg(errp, "The active block job '%s' cannot be completed", |
3453d972 | 1066 | job->id); |
d63ffd87 PB |
1067 | return; |
1068 | } | |
1069 | ||
274fccee HR |
1070 | if (s->backing_mode == MIRROR_OPEN_BACKING_CHAIN) { |
1071 | int ret; | |
1072 | ||
1073 | assert(!target->backing); | |
1074 | ret = bdrv_open_backing_file(target, NULL, "backing", errp); | |
1075 | if (ret < 0) { | |
1076 | return; | |
1077 | } | |
1078 | } | |
1079 | ||
15d67298 | 1080 | /* block all operations on to_replace bs */ |
09158f00 | 1081 | if (s->replaces) { |
5a7e7a0b SH |
1082 | AioContext *replace_aio_context; |
1083 | ||
e12f3784 | 1084 | s->to_replace = bdrv_find_node(s->replaces); |
09158f00 | 1085 | if (!s->to_replace) { |
e12f3784 | 1086 | error_setg(errp, "Node name '%s' not found", s->replaces); |
09158f00 BC |
1087 | return; |
1088 | } | |
1089 | ||
5a7e7a0b SH |
1090 | replace_aio_context = bdrv_get_aio_context(s->to_replace); |
1091 | aio_context_acquire(replace_aio_context); | |
1092 | ||
4ef85a9c KW |
1093 | /* TODO Translate this into permission system. Current definition of |
1094 | * GRAPH_MOD would require to request it for the parents; they might | |
1095 | * not even be BlockDriverStates, however, so a BdrvChild can't address | |
1096 | * them. May need redefinition of GRAPH_MOD. */ | |
09158f00 BC |
1097 | error_setg(&s->replace_blocker, |
1098 | "block device is in use by block-job-complete"); | |
1099 | bdrv_op_block_all(s->to_replace, s->replace_blocker); | |
1100 | bdrv_ref(s->to_replace); | |
5a7e7a0b SH |
1101 | |
1102 | aio_context_release(replace_aio_context); | |
09158f00 BC |
1103 | } |
1104 | ||
d63ffd87 | 1105 | s->should_complete = true; |
3d70ff53 | 1106 | job_enter(job); |
d63ffd87 PB |
1107 | } |
1108 | ||
da01ff7f | 1109 | static void mirror_pause(Job *job) |
565ac01f | 1110 | { |
da01ff7f | 1111 | MirrorBlockJob *s = container_of(job, MirrorBlockJob, common.job); |
565ac01f | 1112 | |
bae8196d | 1113 | mirror_wait_for_all_io(s); |
565ac01f SH |
1114 | } |
1115 | ||
89bd0305 KW |
1116 | static bool mirror_drained_poll(BlockJob *job) |
1117 | { | |
1118 | MirrorBlockJob *s = container_of(job, MirrorBlockJob, common); | |
1119 | return !!s->in_flight; | |
1120 | } | |
1121 | ||
565ac01f SH |
1122 | static void mirror_attached_aio_context(BlockJob *job, AioContext *new_context) |
1123 | { | |
1124 | MirrorBlockJob *s = container_of(job, MirrorBlockJob, common); | |
1125 | ||
1126 | blk_set_aio_context(s->target, new_context); | |
1127 | } | |
1128 | ||
bae8196d PB |
1129 | static void mirror_drain(BlockJob *job) |
1130 | { | |
1131 | MirrorBlockJob *s = container_of(job, MirrorBlockJob, common); | |
1132 | ||
1133 | /* Need to keep a reference in case blk_drain triggers execution | |
1134 | * of mirror_complete... | |
1135 | */ | |
1136 | if (s->target) { | |
1137 | BlockBackend *target = s->target; | |
1138 | blk_ref(target); | |
1139 | blk_drain(target); | |
1140 | blk_unref(target); | |
1141 | } | |
1142 | } | |
1143 | ||
3fc4b10a | 1144 | static const BlockJobDriver mirror_job_driver = { |
33e9e9bd KW |
1145 | .job_driver = { |
1146 | .instance_size = sizeof(MirrorBlockJob), | |
252291ea | 1147 | .job_type = JOB_TYPE_MIRROR, |
80fa2c75 | 1148 | .free = block_job_free, |
b15de828 | 1149 | .user_resume = block_job_user_resume, |
b69f777d | 1150 | .drain = block_job_drain, |
f67432a2 | 1151 | .run = mirror_run, |
737efc1e JS |
1152 | .prepare = mirror_prepare, |
1153 | .abort = mirror_abort, | |
da01ff7f | 1154 | .pause = mirror_pause, |
3453d972 | 1155 | .complete = mirror_complete, |
33e9e9bd | 1156 | }, |
89bd0305 | 1157 | .drained_poll = mirror_drained_poll, |
565ac01f | 1158 | .attached_aio_context = mirror_attached_aio_context, |
bae8196d | 1159 | .drain = mirror_drain, |
893f7eba PB |
1160 | }; |
1161 | ||
03544a6e | 1162 | static const BlockJobDriver commit_active_job_driver = { |
33e9e9bd KW |
1163 | .job_driver = { |
1164 | .instance_size = sizeof(MirrorBlockJob), | |
252291ea | 1165 | .job_type = JOB_TYPE_COMMIT, |
80fa2c75 | 1166 | .free = block_job_free, |
b15de828 | 1167 | .user_resume = block_job_user_resume, |
b69f777d | 1168 | .drain = block_job_drain, |
f67432a2 | 1169 | .run = mirror_run, |
737efc1e JS |
1170 | .prepare = mirror_prepare, |
1171 | .abort = mirror_abort, | |
da01ff7f | 1172 | .pause = mirror_pause, |
3453d972 | 1173 | .complete = mirror_complete, |
33e9e9bd | 1174 | }, |
89bd0305 | 1175 | .drained_poll = mirror_drained_poll, |
565ac01f | 1176 | .attached_aio_context = mirror_attached_aio_context, |
bae8196d | 1177 | .drain = mirror_drain, |
03544a6e FZ |
1178 | }; |
1179 | ||
d06107ad HR |
1180 | static void do_sync_target_write(MirrorBlockJob *job, MirrorMethod method, |
1181 | uint64_t offset, uint64_t bytes, | |
1182 | QEMUIOVector *qiov, int flags) | |
1183 | { | |
1184 | BdrvDirtyBitmapIter *iter; | |
1185 | QEMUIOVector target_qiov; | |
1186 | uint64_t dirty_offset; | |
1187 | int dirty_bytes; | |
1188 | ||
1189 | if (qiov) { | |
1190 | qemu_iovec_init(&target_qiov, qiov->niov); | |
1191 | } | |
1192 | ||
1193 | iter = bdrv_dirty_iter_new(job->dirty_bitmap); | |
1194 | bdrv_set_dirty_iter(iter, offset); | |
1195 | ||
1196 | while (true) { | |
1197 | bool valid_area; | |
1198 | int ret; | |
1199 | ||
1200 | bdrv_dirty_bitmap_lock(job->dirty_bitmap); | |
1201 | valid_area = bdrv_dirty_iter_next_area(iter, offset + bytes, | |
1202 | &dirty_offset, &dirty_bytes); | |
1203 | if (!valid_area) { | |
1204 | bdrv_dirty_bitmap_unlock(job->dirty_bitmap); | |
1205 | break; | |
1206 | } | |
1207 | ||
1208 | bdrv_reset_dirty_bitmap_locked(job->dirty_bitmap, | |
1209 | dirty_offset, dirty_bytes); | |
1210 | bdrv_dirty_bitmap_unlock(job->dirty_bitmap); | |
1211 | ||
1212 | job_progress_increase_remaining(&job->common.job, dirty_bytes); | |
1213 | ||
1214 | assert(dirty_offset - offset <= SIZE_MAX); | |
1215 | if (qiov) { | |
1216 | qemu_iovec_reset(&target_qiov); | |
1217 | qemu_iovec_concat(&target_qiov, qiov, | |
1218 | dirty_offset - offset, dirty_bytes); | |
1219 | } | |
1220 | ||
1221 | switch (method) { | |
1222 | case MIRROR_METHOD_COPY: | |
1223 | ret = blk_co_pwritev(job->target, dirty_offset, dirty_bytes, | |
1224 | qiov ? &target_qiov : NULL, flags); | |
1225 | break; | |
1226 | ||
1227 | case MIRROR_METHOD_ZERO: | |
1228 | assert(!qiov); | |
1229 | ret = blk_co_pwrite_zeroes(job->target, dirty_offset, dirty_bytes, | |
1230 | flags); | |
1231 | break; | |
1232 | ||
1233 | case MIRROR_METHOD_DISCARD: | |
1234 | assert(!qiov); | |
1235 | ret = blk_co_pdiscard(job->target, dirty_offset, dirty_bytes); | |
1236 | break; | |
1237 | ||
1238 | default: | |
1239 | abort(); | |
1240 | } | |
1241 | ||
1242 | if (ret >= 0) { | |
1243 | job_progress_update(&job->common.job, dirty_bytes); | |
1244 | } else { | |
1245 | BlockErrorAction action; | |
1246 | ||
1247 | bdrv_set_dirty_bitmap(job->dirty_bitmap, dirty_offset, dirty_bytes); | |
1248 | job->actively_synced = false; | |
1249 | ||
1250 | action = mirror_error_action(job, false, -ret); | |
1251 | if (action == BLOCK_ERROR_ACTION_REPORT) { | |
1252 | if (!job->ret) { | |
1253 | job->ret = ret; | |
1254 | } | |
1255 | break; | |
1256 | } | |
1257 | } | |
1258 | } | |
1259 | ||
1260 | bdrv_dirty_iter_free(iter); | |
1261 | if (qiov) { | |
1262 | qemu_iovec_destroy(&target_qiov); | |
1263 | } | |
1264 | } | |
1265 | ||
1266 | static MirrorOp *coroutine_fn active_write_prepare(MirrorBlockJob *s, | |
1267 | uint64_t offset, | |
1268 | uint64_t bytes) | |
1269 | { | |
1270 | MirrorOp *op; | |
1271 | uint64_t start_chunk = offset / s->granularity; | |
1272 | uint64_t end_chunk = DIV_ROUND_UP(offset + bytes, s->granularity); | |
1273 | ||
1274 | op = g_new(MirrorOp, 1); | |
1275 | *op = (MirrorOp){ | |
1276 | .s = s, | |
1277 | .offset = offset, | |
1278 | .bytes = bytes, | |
1279 | .is_active_write = true, | |
1280 | }; | |
1281 | qemu_co_queue_init(&op->waiting_requests); | |
1282 | QTAILQ_INSERT_TAIL(&s->ops_in_flight, op, next); | |
1283 | ||
1284 | s->in_active_write_counter++; | |
1285 | ||
1286 | mirror_wait_on_conflicts(op, s, offset, bytes); | |
1287 | ||
1288 | bitmap_set(s->in_flight_bitmap, start_chunk, end_chunk - start_chunk); | |
1289 | ||
1290 | return op; | |
1291 | } | |
1292 | ||
1293 | static void coroutine_fn active_write_settle(MirrorOp *op) | |
1294 | { | |
1295 | uint64_t start_chunk = op->offset / op->s->granularity; | |
1296 | uint64_t end_chunk = DIV_ROUND_UP(op->offset + op->bytes, | |
1297 | op->s->granularity); | |
1298 | ||
1299 | if (!--op->s->in_active_write_counter && op->s->actively_synced) { | |
1300 | BdrvChild *source = op->s->mirror_top_bs->backing; | |
1301 | ||
1302 | if (QLIST_FIRST(&source->bs->parents) == source && | |
1303 | QLIST_NEXT(source, next_parent) == NULL) | |
1304 | { | |
1305 | /* Assert that we are back in sync once all active write | |
1306 | * operations are settled. | |
1307 | * Note that we can only assert this if the mirror node | |
1308 | * is the source node's only parent. */ | |
1309 | assert(!bdrv_get_dirty_count(op->s->dirty_bitmap)); | |
1310 | } | |
1311 | } | |
1312 | bitmap_clear(op->s->in_flight_bitmap, start_chunk, end_chunk - start_chunk); | |
1313 | QTAILQ_REMOVE(&op->s->ops_in_flight, op, next); | |
1314 | qemu_co_queue_restart_all(&op->waiting_requests); | |
1315 | g_free(op); | |
1316 | } | |
1317 | ||
4ef85a9c KW |
1318 | static int coroutine_fn bdrv_mirror_top_preadv(BlockDriverState *bs, |
1319 | uint64_t offset, uint64_t bytes, QEMUIOVector *qiov, int flags) | |
1320 | { | |
1321 | return bdrv_co_preadv(bs->backing, offset, bytes, qiov, flags); | |
1322 | } | |
1323 | ||
d06107ad HR |
1324 | static int coroutine_fn bdrv_mirror_top_do_write(BlockDriverState *bs, |
1325 | MirrorMethod method, uint64_t offset, uint64_t bytes, QEMUIOVector *qiov, | |
1326 | int flags) | |
1327 | { | |
1328 | MirrorOp *op = NULL; | |
1329 | MirrorBDSOpaque *s = bs->opaque; | |
1330 | int ret = 0; | |
1331 | bool copy_to_target; | |
1332 | ||
1333 | copy_to_target = s->job->ret >= 0 && | |
1334 | s->job->copy_mode == MIRROR_COPY_MODE_WRITE_BLOCKING; | |
1335 | ||
1336 | if (copy_to_target) { | |
1337 | op = active_write_prepare(s->job, offset, bytes); | |
1338 | } | |
1339 | ||
1340 | switch (method) { | |
1341 | case MIRROR_METHOD_COPY: | |
1342 | ret = bdrv_co_pwritev(bs->backing, offset, bytes, qiov, flags); | |
1343 | break; | |
1344 | ||
1345 | case MIRROR_METHOD_ZERO: | |
1346 | ret = bdrv_co_pwrite_zeroes(bs->backing, offset, bytes, flags); | |
1347 | break; | |
1348 | ||
1349 | case MIRROR_METHOD_DISCARD: | |
0b9fd3f4 | 1350 | ret = bdrv_co_pdiscard(bs->backing, offset, bytes); |
d06107ad HR |
1351 | break; |
1352 | ||
1353 | default: | |
1354 | abort(); | |
1355 | } | |
1356 | ||
1357 | if (ret < 0) { | |
1358 | goto out; | |
1359 | } | |
1360 | ||
1361 | if (copy_to_target) { | |
1362 | do_sync_target_write(s->job, method, offset, bytes, qiov, flags); | |
1363 | } | |
1364 | ||
1365 | out: | |
1366 | if (copy_to_target) { | |
1367 | active_write_settle(op); | |
1368 | } | |
1369 | return ret; | |
1370 | } | |
1371 | ||
4ef85a9c KW |
1372 | static int coroutine_fn bdrv_mirror_top_pwritev(BlockDriverState *bs, |
1373 | uint64_t offset, uint64_t bytes, QEMUIOVector *qiov, int flags) | |
1374 | { | |
d06107ad HR |
1375 | MirrorBDSOpaque *s = bs->opaque; |
1376 | QEMUIOVector bounce_qiov; | |
1377 | void *bounce_buf; | |
1378 | int ret = 0; | |
1379 | bool copy_to_target; | |
1380 | ||
1381 | copy_to_target = s->job->ret >= 0 && | |
1382 | s->job->copy_mode == MIRROR_COPY_MODE_WRITE_BLOCKING; | |
1383 | ||
1384 | if (copy_to_target) { | |
1385 | /* The guest might concurrently modify the data to write; but | |
1386 | * the data on source and destination must match, so we have | |
1387 | * to use a bounce buffer if we are going to write to the | |
1388 | * target now. */ | |
1389 | bounce_buf = qemu_blockalign(bs, bytes); | |
1390 | iov_to_buf_full(qiov->iov, qiov->niov, 0, bounce_buf, bytes); | |
1391 | ||
1392 | qemu_iovec_init(&bounce_qiov, 1); | |
1393 | qemu_iovec_add(&bounce_qiov, bounce_buf, bytes); | |
1394 | qiov = &bounce_qiov; | |
1395 | } | |
1396 | ||
1397 | ret = bdrv_mirror_top_do_write(bs, MIRROR_METHOD_COPY, offset, bytes, qiov, | |
1398 | flags); | |
1399 | ||
1400 | if (copy_to_target) { | |
1401 | qemu_iovec_destroy(&bounce_qiov); | |
1402 | qemu_vfree(bounce_buf); | |
1403 | } | |
1404 | ||
1405 | return ret; | |
4ef85a9c KW |
1406 | } |
1407 | ||
1408 | static int coroutine_fn bdrv_mirror_top_flush(BlockDriverState *bs) | |
1409 | { | |
ce960aa9 VSO |
1410 | if (bs->backing == NULL) { |
1411 | /* we can be here after failed bdrv_append in mirror_start_job */ | |
1412 | return 0; | |
1413 | } | |
4ef85a9c KW |
1414 | return bdrv_co_flush(bs->backing->bs); |
1415 | } | |
1416 | ||
4ef85a9c | 1417 | static int coroutine_fn bdrv_mirror_top_pwrite_zeroes(BlockDriverState *bs, |
f5a5ca79 | 1418 | int64_t offset, int bytes, BdrvRequestFlags flags) |
4ef85a9c | 1419 | { |
d06107ad HR |
1420 | return bdrv_mirror_top_do_write(bs, MIRROR_METHOD_ZERO, offset, bytes, NULL, |
1421 | flags); | |
4ef85a9c KW |
1422 | } |
1423 | ||
1424 | static int coroutine_fn bdrv_mirror_top_pdiscard(BlockDriverState *bs, | |
f5a5ca79 | 1425 | int64_t offset, int bytes) |
4ef85a9c | 1426 | { |
d06107ad HR |
1427 | return bdrv_mirror_top_do_write(bs, MIRROR_METHOD_DISCARD, offset, bytes, |
1428 | NULL, 0); | |
4ef85a9c KW |
1429 | } |
1430 | ||
fd4a6493 KW |
1431 | static void bdrv_mirror_top_refresh_filename(BlockDriverState *bs, QDict *opts) |
1432 | { | |
18775ff3 VSO |
1433 | if (bs->backing == NULL) { |
1434 | /* we can be here after failed bdrv_attach_child in | |
1435 | * bdrv_set_backing_hd */ | |
1436 | return; | |
1437 | } | |
fd4a6493 KW |
1438 | bdrv_refresh_filename(bs->backing->bs); |
1439 | pstrcpy(bs->exact_filename, sizeof(bs->exact_filename), | |
1440 | bs->backing->bs->filename); | |
1441 | } | |
1442 | ||
4ef85a9c KW |
1443 | static void bdrv_mirror_top_child_perm(BlockDriverState *bs, BdrvChild *c, |
1444 | const BdrvChildRole *role, | |
e0995dc3 | 1445 | BlockReopenQueue *reopen_queue, |
4ef85a9c KW |
1446 | uint64_t perm, uint64_t shared, |
1447 | uint64_t *nperm, uint64_t *nshared) | |
1448 | { | |
1449 | /* Must be able to forward guest writes to the real image */ | |
1450 | *nperm = 0; | |
1451 | if (perm & BLK_PERM_WRITE) { | |
1452 | *nperm |= BLK_PERM_WRITE; | |
1453 | } | |
1454 | ||
1455 | *nshared = BLK_PERM_ALL; | |
1456 | } | |
1457 | ||
1458 | /* Dummy node that provides consistent read to its users without requiring it | |
1459 | * from its backing file and that allows writes on the backing file chain. */ | |
1460 | static BlockDriver bdrv_mirror_top = { | |
1461 | .format_name = "mirror_top", | |
1462 | .bdrv_co_preadv = bdrv_mirror_top_preadv, | |
1463 | .bdrv_co_pwritev = bdrv_mirror_top_pwritev, | |
1464 | .bdrv_co_pwrite_zeroes = bdrv_mirror_top_pwrite_zeroes, | |
1465 | .bdrv_co_pdiscard = bdrv_mirror_top_pdiscard, | |
1466 | .bdrv_co_flush = bdrv_mirror_top_flush, | |
3e4d0e72 | 1467 | .bdrv_co_block_status = bdrv_co_block_status_from_backing, |
fd4a6493 | 1468 | .bdrv_refresh_filename = bdrv_mirror_top_refresh_filename, |
4ef85a9c KW |
1469 | .bdrv_child_perm = bdrv_mirror_top_child_perm, |
1470 | }; | |
1471 | ||
71aa9867 | 1472 | static void mirror_start_job(const char *job_id, BlockDriverState *bs, |
47970dfb JS |
1473 | int creation_flags, BlockDriverState *target, |
1474 | const char *replaces, int64_t speed, | |
1475 | uint32_t granularity, int64_t buf_size, | |
274fccee | 1476 | BlockMirrorBackingMode backing_mode, |
09158f00 BC |
1477 | BlockdevOnError on_source_error, |
1478 | BlockdevOnError on_target_error, | |
0fc9f8ea | 1479 | bool unmap, |
097310b5 | 1480 | BlockCompletionFunc *cb, |
51ccfa2d | 1481 | void *opaque, |
09158f00 | 1482 | const BlockJobDriver *driver, |
b49f7ead | 1483 | bool is_none_mode, BlockDriverState *base, |
51ccfa2d | 1484 | bool auto_complete, const char *filter_node_name, |
481debaa | 1485 | bool is_mirror, MirrorCopyMode copy_mode, |
51ccfa2d | 1486 | Error **errp) |
893f7eba PB |
1487 | { |
1488 | MirrorBlockJob *s; | |
429076e8 | 1489 | MirrorBDSOpaque *bs_opaque; |
4ef85a9c KW |
1490 | BlockDriverState *mirror_top_bs; |
1491 | bool target_graph_mod; | |
1492 | bool target_is_backing; | |
b2c2832c | 1493 | Error *local_err = NULL; |
d7086422 | 1494 | int ret; |
893f7eba | 1495 | |
eee13dfe | 1496 | if (granularity == 0) { |
341ebc2f | 1497 | granularity = bdrv_get_default_bitmap_granularity(target); |
eee13dfe PB |
1498 | } |
1499 | ||
31826642 | 1500 | assert(is_power_of_2(granularity)); |
eee13dfe | 1501 | |
48ac0a4d WC |
1502 | if (buf_size < 0) { |
1503 | error_setg(errp, "Invalid parameter 'buf-size'"); | |
1504 | return; | |
1505 | } | |
1506 | ||
1507 | if (buf_size == 0) { | |
1508 | buf_size = DEFAULT_MIRROR_BUF_SIZE; | |
1509 | } | |
5bc361b8 | 1510 | |
86fae10c KW |
1511 | if (bs == target) { |
1512 | error_setg(errp, "Can't mirror node into itself"); | |
1513 | return; | |
1514 | } | |
1515 | ||
4ef85a9c KW |
1516 | /* In the case of active commit, add dummy driver to provide consistent |
1517 | * reads on the top, while disabling it in the intermediate nodes, and make | |
1518 | * the backing chain writable. */ | |
6cdbceb1 KW |
1519 | mirror_top_bs = bdrv_new_open_driver(&bdrv_mirror_top, filter_node_name, |
1520 | BDRV_O_RDWR, errp); | |
4ef85a9c KW |
1521 | if (mirror_top_bs == NULL) { |
1522 | return; | |
1523 | } | |
d3c8c674 KW |
1524 | if (!filter_node_name) { |
1525 | mirror_top_bs->implicit = true; | |
1526 | } | |
4ef85a9c | 1527 | mirror_top_bs->total_sectors = bs->total_sectors; |
228345bf HR |
1528 | mirror_top_bs->supported_write_flags = BDRV_REQ_WRITE_UNCHANGED; |
1529 | mirror_top_bs->supported_zero_flags = BDRV_REQ_WRITE_UNCHANGED; | |
429076e8 HR |
1530 | bs_opaque = g_new0(MirrorBDSOpaque, 1); |
1531 | mirror_top_bs->opaque = bs_opaque; | |
19dd29e8 | 1532 | bdrv_set_aio_context(mirror_top_bs, bdrv_get_aio_context(bs)); |
4ef85a9c KW |
1533 | |
1534 | /* bdrv_append takes ownership of the mirror_top_bs reference, need to keep | |
7a25fcd0 | 1535 | * it alive until block_job_create() succeeds even if bs has no parent. */ |
4ef85a9c KW |
1536 | bdrv_ref(mirror_top_bs); |
1537 | bdrv_drained_begin(bs); | |
b2c2832c | 1538 | bdrv_append(mirror_top_bs, bs, &local_err); |
4ef85a9c KW |
1539 | bdrv_drained_end(bs); |
1540 | ||
b2c2832c KW |
1541 | if (local_err) { |
1542 | bdrv_unref(mirror_top_bs); | |
1543 | error_propagate(errp, local_err); | |
1544 | return; | |
1545 | } | |
1546 | ||
4ef85a9c | 1547 | /* Make sure that the source is not resized while the job is running */ |
75859b94 | 1548 | s = block_job_create(job_id, driver, NULL, mirror_top_bs, |
4ef85a9c KW |
1549 | BLK_PERM_CONSISTENT_READ, |
1550 | BLK_PERM_CONSISTENT_READ | BLK_PERM_WRITE_UNCHANGED | | |
1551 | BLK_PERM_WRITE | BLK_PERM_GRAPH_MOD, speed, | |
c6cc12bf | 1552 | creation_flags, cb, opaque, errp); |
893f7eba | 1553 | if (!s) { |
4ef85a9c | 1554 | goto fail; |
893f7eba | 1555 | } |
429076e8 HR |
1556 | bs_opaque->job = s; |
1557 | ||
7a25fcd0 HR |
1558 | /* The block job now has a reference to this node */ |
1559 | bdrv_unref(mirror_top_bs); | |
1560 | ||
4ef85a9c KW |
1561 | s->mirror_top_bs = mirror_top_bs; |
1562 | ||
1563 | /* No resize for the target either; while the mirror is still running, a | |
1564 | * consistent read isn't necessarily possible. We could possibly allow | |
1565 | * writes and graph modifications, though it would likely defeat the | |
1566 | * purpose of a mirror, so leave them blocked for now. | |
1567 | * | |
1568 | * In the case of active commit, things look a bit different, though, | |
1569 | * because the target is an already populated backing file in active use. | |
1570 | * We can allow anything except resize there.*/ | |
1571 | target_is_backing = bdrv_chain_contains(bs, target); | |
1572 | target_graph_mod = (backing_mode != MIRROR_LEAVE_BACKING_CHAIN); | |
1573 | s->target = blk_new(BLK_PERM_WRITE | BLK_PERM_RESIZE | | |
1574 | (target_graph_mod ? BLK_PERM_GRAPH_MOD : 0), | |
1575 | BLK_PERM_WRITE_UNCHANGED | | |
1576 | (target_is_backing ? BLK_PERM_CONSISTENT_READ | | |
1577 | BLK_PERM_WRITE | | |
1578 | BLK_PERM_GRAPH_MOD : 0)); | |
d7086422 KW |
1579 | ret = blk_insert_bs(s->target, target, errp); |
1580 | if (ret < 0) { | |
4ef85a9c | 1581 | goto fail; |
d7086422 | 1582 | } |
045a2f82 FZ |
1583 | if (is_mirror) { |
1584 | /* XXX: Mirror target could be a NBD server of target QEMU in the case | |
1585 | * of non-shared block migration. To allow migration completion, we | |
1586 | * have to allow "inactivate" of the target BB. When that happens, we | |
1587 | * know the job is drained, and the vcpus are stopped, so no write | |
1588 | * operation will be performed. Block layer already has assertions to | |
1589 | * ensure that. */ | |
1590 | blk_set_force_allow_inactivate(s->target); | |
1591 | } | |
e253f4b8 | 1592 | |
09158f00 | 1593 | s->replaces = g_strdup(replaces); |
b952b558 PB |
1594 | s->on_source_error = on_source_error; |
1595 | s->on_target_error = on_target_error; | |
03544a6e | 1596 | s->is_none_mode = is_none_mode; |
274fccee | 1597 | s->backing_mode = backing_mode; |
481debaa | 1598 | s->copy_mode = copy_mode; |
5bc361b8 | 1599 | s->base = base; |
eee13dfe | 1600 | s->granularity = granularity; |
48ac0a4d | 1601 | s->buf_size = ROUND_UP(buf_size, granularity); |
0fc9f8ea | 1602 | s->unmap = unmap; |
b49f7ead WC |
1603 | if (auto_complete) { |
1604 | s->should_complete = true; | |
1605 | } | |
b812f671 | 1606 | |
0db6e54a | 1607 | s->dirty_bitmap = bdrv_create_dirty_bitmap(bs, granularity, NULL, errp); |
b8afb520 | 1608 | if (!s->dirty_bitmap) { |
88f9d1b3 | 1609 | goto fail; |
b8afb520 | 1610 | } |
10f3cd15 | 1611 | |
4ef85a9c | 1612 | /* Required permissions are already taken with blk_new() */ |
76d554e2 KW |
1613 | block_job_add_bdrv(&s->common, "target", target, 0, BLK_PERM_ALL, |
1614 | &error_abort); | |
1615 | ||
f3ede4b0 AG |
1616 | /* In commit_active_start() all intermediate nodes disappear, so |
1617 | * any jobs in them must be blocked */ | |
4ef85a9c | 1618 | if (target_is_backing) { |
f3ede4b0 AG |
1619 | BlockDriverState *iter; |
1620 | for (iter = backing_bs(bs); iter != target; iter = backing_bs(iter)) { | |
4ef85a9c KW |
1621 | /* XXX BLK_PERM_WRITE needs to be allowed so we don't block |
1622 | * ourselves at s->base (if writes are blocked for a node, they are | |
1623 | * also blocked for its backing file). The other options would be a | |
1624 | * second filter driver above s->base (== target). */ | |
1625 | ret = block_job_add_bdrv(&s->common, "intermediate node", iter, 0, | |
1626 | BLK_PERM_WRITE_UNCHANGED | BLK_PERM_WRITE, | |
1627 | errp); | |
1628 | if (ret < 0) { | |
1629 | goto fail; | |
1630 | } | |
f3ede4b0 AG |
1631 | } |
1632 | } | |
10f3cd15 | 1633 | |
12aa4082 HR |
1634 | QTAILQ_INIT(&s->ops_in_flight); |
1635 | ||
5ccac6f1 | 1636 | trace_mirror_start(bs, s, opaque); |
da01ff7f | 1637 | job_start(&s->common.job); |
4ef85a9c KW |
1638 | return; |
1639 | ||
1640 | fail: | |
1641 | if (s) { | |
7a25fcd0 HR |
1642 | /* Make sure this BDS does not go away until we have completed the graph |
1643 | * changes below */ | |
1644 | bdrv_ref(mirror_top_bs); | |
1645 | ||
4ef85a9c KW |
1646 | g_free(s->replaces); |
1647 | blk_unref(s->target); | |
429076e8 | 1648 | bs_opaque->job = NULL; |
4ad35181 | 1649 | job_early_fail(&s->common.job); |
4ef85a9c KW |
1650 | } |
1651 | ||
c1cef672 FZ |
1652 | bdrv_child_try_set_perm(mirror_top_bs->backing, 0, BLK_PERM_ALL, |
1653 | &error_abort); | |
5fe31c25 | 1654 | bdrv_replace_node(mirror_top_bs, backing_bs(mirror_top_bs), &error_abort); |
7a25fcd0 HR |
1655 | |
1656 | bdrv_unref(mirror_top_bs); | |
893f7eba | 1657 | } |
03544a6e | 1658 | |
71aa9867 AG |
1659 | void mirror_start(const char *job_id, BlockDriverState *bs, |
1660 | BlockDriverState *target, const char *replaces, | |
a1999b33 JS |
1661 | int creation_flags, int64_t speed, |
1662 | uint32_t granularity, int64_t buf_size, | |
274fccee HR |
1663 | MirrorSyncMode mode, BlockMirrorBackingMode backing_mode, |
1664 | BlockdevOnError on_source_error, | |
03544a6e | 1665 | BlockdevOnError on_target_error, |
481debaa HR |
1666 | bool unmap, const char *filter_node_name, |
1667 | MirrorCopyMode copy_mode, Error **errp) | |
03544a6e FZ |
1668 | { |
1669 | bool is_none_mode; | |
1670 | BlockDriverState *base; | |
1671 | ||
4b80ab2b JS |
1672 | if (mode == MIRROR_SYNC_MODE_INCREMENTAL) { |
1673 | error_setg(errp, "Sync mode 'incremental' not supported"); | |
d58d8453 JS |
1674 | return; |
1675 | } | |
03544a6e | 1676 | is_none_mode = mode == MIRROR_SYNC_MODE_NONE; |
760e0063 | 1677 | base = mode == MIRROR_SYNC_MODE_TOP ? backing_bs(bs) : NULL; |
a1999b33 | 1678 | mirror_start_job(job_id, bs, creation_flags, target, replaces, |
274fccee | 1679 | speed, granularity, buf_size, backing_mode, |
51ccfa2d | 1680 | on_source_error, on_target_error, unmap, NULL, NULL, |
6cdbceb1 | 1681 | &mirror_job_driver, is_none_mode, base, false, |
481debaa | 1682 | filter_node_name, true, copy_mode, errp); |
03544a6e FZ |
1683 | } |
1684 | ||
fd62c609 | 1685 | void commit_active_start(const char *job_id, BlockDriverState *bs, |
47970dfb JS |
1686 | BlockDriverState *base, int creation_flags, |
1687 | int64_t speed, BlockdevOnError on_error, | |
0db832f4 | 1688 | const char *filter_node_name, |
78bbd910 FZ |
1689 | BlockCompletionFunc *cb, void *opaque, |
1690 | bool auto_complete, Error **errp) | |
03544a6e | 1691 | { |
4da83585 | 1692 | int orig_base_flags; |
cc67f4d1 | 1693 | Error *local_err = NULL; |
4da83585 JC |
1694 | |
1695 | orig_base_flags = bdrv_get_flags(base); | |
1696 | ||
20a63d2c FZ |
1697 | if (bdrv_reopen(base, bs->open_flags, errp)) { |
1698 | return; | |
1699 | } | |
4da83585 | 1700 | |
47970dfb | 1701 | mirror_start_job(job_id, bs, creation_flags, base, NULL, speed, 0, 0, |
71aa9867 | 1702 | MIRROR_LEAVE_BACKING_CHAIN, |
51ccfa2d | 1703 | on_error, on_error, true, cb, opaque, |
6cdbceb1 | 1704 | &commit_active_job_driver, false, base, auto_complete, |
481debaa HR |
1705 | filter_node_name, false, MIRROR_COPY_MODE_BACKGROUND, |
1706 | &local_err); | |
0fb6395c | 1707 | if (local_err) { |
cc67f4d1 | 1708 | error_propagate(errp, local_err); |
4da83585 JC |
1709 | goto error_restore_flags; |
1710 | } | |
1711 | ||
1712 | return; | |
1713 | ||
1714 | error_restore_flags: | |
1715 | /* ignore error and errp for bdrv_reopen, because we want to propagate | |
1716 | * the original error */ | |
1717 | bdrv_reopen(base, orig_base_flags, NULL); | |
1718 | return; | |
03544a6e | 1719 | } |