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