]>
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 | |
659 | * required before it could become a backing file of target_bs. */ | |
f94dc3b4 HR |
660 | bs_opaque->stop = true; |
661 | bdrv_child_refresh_perms(mirror_top_bs, mirror_top_bs->backing, | |
662 | &error_abort); | |
737efc1e | 663 | if (!abort && s->backing_mode == MIRROR_SOURCE_BACKING_CHAIN) { |
4ef85a9c KW |
664 | BlockDriverState *backing = s->is_none_mode ? src : s->base; |
665 | if (backing_bs(target_bs) != backing) { | |
12fa4af6 KW |
666 | bdrv_set_backing_hd(target_bs, backing, &local_err); |
667 | if (local_err) { | |
668 | error_report_err(local_err); | |
7b508f6b | 669 | ret = -EPERM; |
12fa4af6 | 670 | } |
4ef85a9c KW |
671 | } |
672 | } | |
5a7e7a0b SH |
673 | |
674 | if (s->to_replace) { | |
675 | replace_aio_context = bdrv_get_aio_context(s->to_replace); | |
676 | aio_context_acquire(replace_aio_context); | |
677 | } | |
678 | ||
737efc1e JS |
679 | if (s->should_complete && !abort) { |
680 | BlockDriverState *to_replace = s->to_replace ?: src; | |
1ba79388 | 681 | bool ro = bdrv_is_read_only(to_replace); |
40365552 | 682 | |
1ba79388 AG |
683 | if (ro != bdrv_is_read_only(target_bs)) { |
684 | bdrv_reopen_set_read_only(target_bs, ro, NULL); | |
5a7e7a0b | 685 | } |
b8804815 KW |
686 | |
687 | /* The mirror job has no requests in flight any more, but we need to | |
688 | * drain potential other users of the BDS before changing the graph. */ | |
5e771752 | 689 | assert(s->in_drain); |
e253f4b8 | 690 | bdrv_drained_begin(target_bs); |
5fe31c25 | 691 | bdrv_replace_node(to_replace, target_bs, &local_err); |
e253f4b8 | 692 | bdrv_drained_end(target_bs); |
5fe31c25 KW |
693 | if (local_err) { |
694 | error_report_err(local_err); | |
7b508f6b | 695 | ret = -EPERM; |
5fe31c25 | 696 | } |
5a7e7a0b SH |
697 | } |
698 | if (s->to_replace) { | |
699 | bdrv_op_unblock_all(s->to_replace, s->replace_blocker); | |
700 | error_free(s->replace_blocker); | |
701 | bdrv_unref(s->to_replace); | |
702 | } | |
703 | if (replace_aio_context) { | |
704 | aio_context_release(replace_aio_context); | |
705 | } | |
706 | g_free(s->replaces); | |
7d9fcb39 | 707 | bdrv_unref(target_bs); |
4ef85a9c | 708 | |
f94dc3b4 HR |
709 | /* |
710 | * Remove the mirror filter driver from the graph. Before this, get rid of | |
4ef85a9c | 711 | * the blockers on the intermediate nodes so that the resulting state is |
f94dc3b4 HR |
712 | * valid. |
713 | */ | |
1908a559 | 714 | block_job_remove_all_bdrv(bjob); |
5fe31c25 | 715 | bdrv_replace_node(mirror_top_bs, backing_bs(mirror_top_bs), &error_abort); |
4ef85a9c KW |
716 | |
717 | /* We just changed the BDS the job BB refers to (with either or both of the | |
5fe31c25 KW |
718 | * bdrv_replace_node() calls), so switch the BB back so the cleanup does |
719 | * the right thing. We don't need any permissions any more now. */ | |
1908a559 KW |
720 | blk_remove_bs(bjob->blk); |
721 | blk_set_perm(bjob->blk, 0, BLK_PERM_ALL, &error_abort); | |
722 | blk_insert_bs(bjob->blk, mirror_top_bs, &error_abort); | |
4ef85a9c | 723 | |
429076e8 | 724 | bs_opaque->job = NULL; |
4ef85a9c | 725 | |
176c3699 | 726 | bdrv_drained_end(src); |
5e771752 | 727 | s->in_drain = false; |
4ef85a9c | 728 | bdrv_unref(mirror_top_bs); |
3f09bfbc | 729 | bdrv_unref(src); |
7b508f6b | 730 | |
737efc1e JS |
731 | return ret; |
732 | } | |
733 | ||
734 | static int mirror_prepare(Job *job) | |
735 | { | |
736 | return mirror_exit_common(job); | |
737 | } | |
738 | ||
739 | static void mirror_abort(Job *job) | |
740 | { | |
741 | int ret = mirror_exit_common(job); | |
742 | assert(ret == 0); | |
5a7e7a0b SH |
743 | } |
744 | ||
537c3d4f | 745 | static void coroutine_fn mirror_throttle(MirrorBlockJob *s) |
49efb1f5 DL |
746 | { |
747 | int64_t now = qemu_clock_get_ns(QEMU_CLOCK_REALTIME); | |
748 | ||
18bb6928 | 749 | if (now - s->last_pause_ns > BLOCK_JOB_SLICE_TIME) { |
49efb1f5 | 750 | s->last_pause_ns = now; |
5d43e86e | 751 | job_sleep_ns(&s->common.job, 0); |
49efb1f5 | 752 | } else { |
da01ff7f | 753 | job_pause_point(&s->common.job); |
49efb1f5 DL |
754 | } |
755 | } | |
756 | ||
c0b363ad DL |
757 | static int coroutine_fn mirror_dirty_init(MirrorBlockJob *s) |
758 | { | |
23ca459a | 759 | int64_t offset; |
c0b363ad | 760 | BlockDriverState *base = s->base; |
138f9fff | 761 | BlockDriverState *bs = s->mirror_top_bs->backing->bs; |
c0b363ad | 762 | BlockDriverState *target_bs = blk_bs(s->target); |
23ca459a | 763 | int ret; |
51b0a488 | 764 | int64_t count; |
c0b363ad | 765 | |
b7d5062c | 766 | if (base == NULL && !bdrv_has_zero_init(target_bs)) { |
c7c2769c | 767 | if (!bdrv_can_write_zeroes_with_unmap(target_bs)) { |
e0d7f73e | 768 | bdrv_set_dirty_bitmap(s->dirty_bitmap, 0, s->bdev_length); |
c7c2769c DL |
769 | return 0; |
770 | } | |
771 | ||
90ab48eb | 772 | s->initial_zeroing_ongoing = true; |
23ca459a EB |
773 | for (offset = 0; offset < s->bdev_length; ) { |
774 | int bytes = MIN(s->bdev_length - offset, | |
775 | QEMU_ALIGN_DOWN(INT_MAX, s->granularity)); | |
c7c2769c DL |
776 | |
777 | mirror_throttle(s); | |
778 | ||
daa7f2f9 | 779 | if (job_is_cancelled(&s->common.job)) { |
90ab48eb | 780 | s->initial_zeroing_ongoing = false; |
c7c2769c DL |
781 | return 0; |
782 | } | |
783 | ||
784 | if (s->in_flight >= MAX_IN_FLIGHT) { | |
67adf4b3 EB |
785 | trace_mirror_yield(s, UINT64_MAX, s->buf_free_count, |
786 | s->in_flight); | |
1181e19a | 787 | mirror_wait_for_free_in_flight_slot(s); |
c7c2769c DL |
788 | continue; |
789 | } | |
790 | ||
4295c5fc | 791 | mirror_perform(s, offset, bytes, MIRROR_METHOD_ZERO); |
23ca459a | 792 | offset += bytes; |
c7c2769c DL |
793 | } |
794 | ||
bae8196d | 795 | mirror_wait_for_all_io(s); |
90ab48eb | 796 | s->initial_zeroing_ongoing = false; |
b7d5062c DL |
797 | } |
798 | ||
c0b363ad | 799 | /* First part, loop on the sectors and initialize the dirty bitmap. */ |
23ca459a | 800 | for (offset = 0; offset < s->bdev_length; ) { |
c0b363ad | 801 | /* Just to make sure we are not exceeding int limit. */ |
23ca459a EB |
802 | int bytes = MIN(s->bdev_length - offset, |
803 | QEMU_ALIGN_DOWN(INT_MAX, s->granularity)); | |
c0b363ad DL |
804 | |
805 | mirror_throttle(s); | |
806 | ||
daa7f2f9 | 807 | if (job_is_cancelled(&s->common.job)) { |
c0b363ad DL |
808 | return 0; |
809 | } | |
810 | ||
23ca459a | 811 | ret = bdrv_is_allocated_above(bs, base, offset, bytes, &count); |
c0b363ad DL |
812 | if (ret < 0) { |
813 | return ret; | |
814 | } | |
815 | ||
23ca459a | 816 | assert(count); |
b7d5062c | 817 | if (ret == 1) { |
23ca459a | 818 | bdrv_set_dirty_bitmap(s->dirty_bitmap, offset, count); |
c0b363ad | 819 | } |
23ca459a | 820 | offset += count; |
c0b363ad DL |
821 | } |
822 | return 0; | |
823 | } | |
824 | ||
bdffb31d PB |
825 | /* Called when going out of the streaming phase to flush the bulk of the |
826 | * data to the medium, or just before completing. | |
827 | */ | |
828 | static int mirror_flush(MirrorBlockJob *s) | |
829 | { | |
830 | int ret = blk_flush(s->target); | |
831 | if (ret < 0) { | |
832 | if (mirror_error_action(s, false, -ret) == BLOCK_ERROR_ACTION_REPORT) { | |
833 | s->ret = ret; | |
834 | } | |
835 | } | |
836 | return ret; | |
837 | } | |
838 | ||
f67432a2 | 839 | static int coroutine_fn mirror_run(Job *job, Error **errp) |
893f7eba | 840 | { |
f67432a2 | 841 | MirrorBlockJob *s = container_of(job, MirrorBlockJob, common.job); |
138f9fff | 842 | BlockDriverState *bs = s->mirror_top_bs->backing->bs; |
e253f4b8 | 843 | BlockDriverState *target_bs = blk_bs(s->target); |
9a0cec66 | 844 | bool need_drain = true; |
c0b363ad | 845 | int64_t length; |
b812f671 | 846 | BlockDriverInfo bdi; |
1d33936e JC |
847 | char backing_filename[2]; /* we only need 2 characters because we are only |
848 | checking for a NULL string */ | |
893f7eba | 849 | int ret = 0; |
893f7eba | 850 | |
daa7f2f9 | 851 | if (job_is_cancelled(&s->common.job)) { |
893f7eba PB |
852 | goto immediate_exit; |
853 | } | |
854 | ||
b21c7652 HR |
855 | s->bdev_length = bdrv_getlength(bs); |
856 | if (s->bdev_length < 0) { | |
857 | ret = s->bdev_length; | |
373df5b1 | 858 | goto immediate_exit; |
becc347e KW |
859 | } |
860 | ||
861 | /* Active commit must resize the base image if its size differs from the | |
862 | * active layer. */ | |
863 | if (s->base == blk_bs(s->target)) { | |
864 | int64_t base_length; | |
865 | ||
866 | base_length = blk_getlength(s->target); | |
867 | if (base_length < 0) { | |
868 | ret = base_length; | |
869 | goto immediate_exit; | |
870 | } | |
871 | ||
872 | if (s->bdev_length > base_length) { | |
3a691c50 HR |
873 | ret = blk_truncate(s->target, s->bdev_length, PREALLOC_MODE_OFF, |
874 | NULL); | |
becc347e KW |
875 | if (ret < 0) { |
876 | goto immediate_exit; | |
877 | } | |
878 | } | |
879 | } | |
880 | ||
881 | if (s->bdev_length == 0) { | |
2e1795b5 KW |
882 | /* Transition to the READY state and wait for complete. */ |
883 | job_transition_to_ready(&s->common.job); | |
9e48b025 | 884 | s->synced = true; |
d06107ad | 885 | s->actively_synced = true; |
daa7f2f9 | 886 | while (!job_is_cancelled(&s->common.job) && !s->should_complete) { |
198c49cc | 887 | job_yield(&s->common.job); |
9e48b025 | 888 | } |
daa7f2f9 | 889 | s->common.job.cancelled = false; |
9e48b025 | 890 | goto immediate_exit; |
893f7eba PB |
891 | } |
892 | ||
b21c7652 | 893 | length = DIV_ROUND_UP(s->bdev_length, s->granularity); |
402a4741 PB |
894 | s->in_flight_bitmap = bitmap_new(length); |
895 | ||
b812f671 PB |
896 | /* If we have no backing file yet in the destination, we cannot let |
897 | * the destination do COW. Instead, we copy sectors around the | |
898 | * dirty data if needed. We need a bitmap to do that. | |
899 | */ | |
e253f4b8 | 900 | bdrv_get_backing_filename(target_bs, backing_filename, |
b812f671 | 901 | sizeof(backing_filename)); |
e253f4b8 | 902 | if (!bdrv_get_info(target_bs, &bdi) && bdi.cluster_size) { |
b436982f EB |
903 | s->target_cluster_size = bdi.cluster_size; |
904 | } else { | |
905 | s->target_cluster_size = BDRV_SECTOR_SIZE; | |
e5b43573 | 906 | } |
b436982f EB |
907 | if (backing_filename[0] && !target_bs->backing && |
908 | s->granularity < s->target_cluster_size) { | |
909 | s->buf_size = MAX(s->buf_size, s->target_cluster_size); | |
e5b43573 | 910 | s->cow_bitmap = bitmap_new(length); |
b812f671 | 911 | } |
e253f4b8 | 912 | s->max_iov = MIN(bs->bl.max_iov, target_bs->bl.max_iov); |
b812f671 | 913 | |
7504edf4 KW |
914 | s->buf = qemu_try_blockalign(bs, s->buf_size); |
915 | if (s->buf == NULL) { | |
916 | ret = -ENOMEM; | |
917 | goto immediate_exit; | |
918 | } | |
919 | ||
402a4741 | 920 | mirror_free_init(s); |
893f7eba | 921 | |
49efb1f5 | 922 | s->last_pause_ns = qemu_clock_get_ns(QEMU_CLOCK_REALTIME); |
03544a6e | 923 | if (!s->is_none_mode) { |
c0b363ad | 924 | ret = mirror_dirty_init(s); |
daa7f2f9 | 925 | if (ret < 0 || job_is_cancelled(&s->common.job)) { |
c0b363ad | 926 | goto immediate_exit; |
893f7eba PB |
927 | } |
928 | } | |
929 | ||
dc162c8e | 930 | assert(!s->dbi); |
715a74d8 | 931 | s->dbi = bdrv_dirty_iter_new(s->dirty_bitmap); |
893f7eba | 932 | for (;;) { |
cc8c9d6c | 933 | uint64_t delay_ns = 0; |
49efb1f5 | 934 | int64_t cnt, delta; |
893f7eba PB |
935 | bool should_complete; |
936 | ||
d06107ad HR |
937 | /* Do not start passive operations while there are active |
938 | * writes in progress */ | |
939 | while (s->in_active_write_counter) { | |
940 | mirror_wait_for_any_operation(s, true); | |
941 | } | |
942 | ||
bd48bde8 PB |
943 | if (s->ret < 0) { |
944 | ret = s->ret; | |
945 | goto immediate_exit; | |
946 | } | |
947 | ||
da01ff7f | 948 | job_pause_point(&s->common.job); |
565ac01f | 949 | |
20dca810 | 950 | cnt = bdrv_get_dirty_count(s->dirty_bitmap); |
05df8a6a KW |
951 | /* cnt is the number of dirty bytes remaining and s->bytes_in_flight is |
952 | * the number of bytes currently being processed; together those are | |
953 | * the current remaining operation length */ | |
30a5c887 | 954 | job_progress_set_remaining(&s->common.job, s->bytes_in_flight + cnt); |
bd48bde8 PB |
955 | |
956 | /* Note that even when no rate limit is applied we need to yield | |
a7282330 | 957 | * periodically with no pending I/O so that bdrv_drain_all() returns. |
18bb6928 KW |
958 | * We do so every BLKOCK_JOB_SLICE_TIME nanoseconds, or when there is |
959 | * an error, or when the source is clean, whichever comes first. */ | |
49efb1f5 | 960 | delta = qemu_clock_get_ns(QEMU_CLOCK_REALTIME) - s->last_pause_ns; |
18bb6928 | 961 | if (delta < BLOCK_JOB_SLICE_TIME && |
bd48bde8 | 962 | s->common.iostatus == BLOCK_DEVICE_IO_STATUS_OK) { |
cf56a3c6 | 963 | if (s->in_flight >= MAX_IN_FLIGHT || s->buf_free_count == 0 || |
402a4741 | 964 | (cnt == 0 && s->in_flight > 0)) { |
9a46dba7 | 965 | trace_mirror_yield(s, cnt, s->buf_free_count, s->in_flight); |
1181e19a | 966 | mirror_wait_for_free_in_flight_slot(s); |
bd48bde8 PB |
967 | continue; |
968 | } else if (cnt != 0) { | |
cc8c9d6c | 969 | delay_ns = mirror_iteration(s); |
893f7eba | 970 | } |
893f7eba PB |
971 | } |
972 | ||
973 | should_complete = false; | |
bd48bde8 | 974 | if (s->in_flight == 0 && cnt == 0) { |
893f7eba | 975 | trace_mirror_before_flush(s); |
bdffb31d PB |
976 | if (!s->synced) { |
977 | if (mirror_flush(s) < 0) { | |
978 | /* Go check s->ret. */ | |
979 | continue; | |
b952b558 | 980 | } |
b952b558 PB |
981 | /* We're out of the streaming phase. From now on, if the job |
982 | * is cancelled we will actually complete all pending I/O and | |
983 | * report completion. This way, block-job-cancel will leave | |
984 | * the target in a consistent state. | |
985 | */ | |
2e1795b5 | 986 | job_transition_to_ready(&s->common.job); |
bdffb31d | 987 | s->synced = true; |
d06107ad HR |
988 | if (s->copy_mode != MIRROR_COPY_MODE_BACKGROUND) { |
989 | s->actively_synced = true; | |
990 | } | |
d63ffd87 | 991 | } |
bdffb31d PB |
992 | |
993 | should_complete = s->should_complete || | |
daa7f2f9 | 994 | job_is_cancelled(&s->common.job); |
bdffb31d | 995 | cnt = bdrv_get_dirty_count(s->dirty_bitmap); |
893f7eba PB |
996 | } |
997 | ||
998 | if (cnt == 0 && should_complete) { | |
999 | /* The dirty bitmap is not updated while operations are pending. | |
1000 | * If we're about to exit, wait for pending operations before | |
1001 | * calling bdrv_get_dirty_count(bs), or we may exit while the | |
1002 | * source has dirty data to copy! | |
1003 | * | |
1004 | * Note that I/O can be submitted by the guest while | |
9a0cec66 PB |
1005 | * mirror_populate runs, so pause it now. Before deciding |
1006 | * whether to switch to target check one last time if I/O has | |
1007 | * come in the meanwhile, and if not flush the data to disk. | |
893f7eba | 1008 | */ |
9a46dba7 | 1009 | trace_mirror_before_drain(s, cnt); |
9a0cec66 | 1010 | |
5e771752 | 1011 | s->in_drain = true; |
9a0cec66 | 1012 | bdrv_drained_begin(bs); |
20dca810 | 1013 | cnt = bdrv_get_dirty_count(s->dirty_bitmap); |
bdffb31d | 1014 | if (cnt > 0 || mirror_flush(s) < 0) { |
9a0cec66 | 1015 | bdrv_drained_end(bs); |
5e771752 | 1016 | s->in_drain = false; |
9a0cec66 PB |
1017 | continue; |
1018 | } | |
1019 | ||
1020 | /* The two disks are in sync. Exit and report successful | |
1021 | * completion. | |
1022 | */ | |
1023 | assert(QLIST_EMPTY(&bs->tracked_requests)); | |
daa7f2f9 | 1024 | s->common.job.cancelled = false; |
9a0cec66 PB |
1025 | need_drain = false; |
1026 | break; | |
893f7eba PB |
1027 | } |
1028 | ||
1029 | ret = 0; | |
ddc4115e SH |
1030 | |
1031 | if (s->synced && !should_complete) { | |
18bb6928 KW |
1032 | delay_ns = (s->in_flight == 0 && |
1033 | cnt == 0 ? BLOCK_JOB_SLICE_TIME : 0); | |
ddc4115e | 1034 | } |
9a46dba7 | 1035 | trace_mirror_before_sleep(s, cnt, s->synced, delay_ns); |
5d43e86e | 1036 | job_sleep_ns(&s->common.job, delay_ns); |
daa7f2f9 | 1037 | if (job_is_cancelled(&s->common.job) && |
004e95df | 1038 | (!s->synced || s->common.job.force_cancel)) |
eb36639f | 1039 | { |
b76e4458 | 1040 | break; |
893f7eba | 1041 | } |
49efb1f5 | 1042 | s->last_pause_ns = qemu_clock_get_ns(QEMU_CLOCK_REALTIME); |
893f7eba PB |
1043 | } |
1044 | ||
1045 | immediate_exit: | |
bd48bde8 PB |
1046 | if (s->in_flight > 0) { |
1047 | /* We get here only if something went wrong. Either the job failed, | |
1048 | * or it was cancelled prematurely so that we do not guarantee that | |
1049 | * the target is a copy of the source. | |
1050 | */ | |
004e95df | 1051 | assert(ret < 0 || ((s->common.job.force_cancel || !s->synced) && |
daa7f2f9 | 1052 | job_is_cancelled(&s->common.job))); |
9a0cec66 | 1053 | assert(need_drain); |
bae8196d | 1054 | mirror_wait_for_all_io(s); |
bd48bde8 PB |
1055 | } |
1056 | ||
1057 | assert(s->in_flight == 0); | |
7191bf31 | 1058 | qemu_vfree(s->buf); |
b812f671 | 1059 | g_free(s->cow_bitmap); |
402a4741 | 1060 | g_free(s->in_flight_bitmap); |
dc162c8e | 1061 | bdrv_dirty_iter_free(s->dbi); |
5a7e7a0b | 1062 | |
9a0cec66 | 1063 | if (need_drain) { |
5e771752 | 1064 | s->in_drain = true; |
9a0cec66 PB |
1065 | bdrv_drained_begin(bs); |
1066 | } | |
f67432a2 | 1067 | |
f67432a2 | 1068 | return ret; |
893f7eba PB |
1069 | } |
1070 | ||
3453d972 | 1071 | static void mirror_complete(Job *job, Error **errp) |
d63ffd87 | 1072 | { |
3453d972 | 1073 | MirrorBlockJob *s = container_of(job, MirrorBlockJob, common.job); |
4ef85a9c | 1074 | BlockDriverState *target; |
274fccee | 1075 | |
274fccee | 1076 | target = blk_bs(s->target); |
d63ffd87 | 1077 | |
d63ffd87 | 1078 | if (!s->synced) { |
9df229c3 | 1079 | error_setg(errp, "The active block job '%s' cannot be completed", |
3453d972 | 1080 | job->id); |
d63ffd87 PB |
1081 | return; |
1082 | } | |
1083 | ||
274fccee HR |
1084 | if (s->backing_mode == MIRROR_OPEN_BACKING_CHAIN) { |
1085 | int ret; | |
1086 | ||
1087 | assert(!target->backing); | |
1088 | ret = bdrv_open_backing_file(target, NULL, "backing", errp); | |
1089 | if (ret < 0) { | |
1090 | return; | |
1091 | } | |
1092 | } | |
1093 | ||
15d67298 | 1094 | /* block all operations on to_replace bs */ |
09158f00 | 1095 | if (s->replaces) { |
5a7e7a0b SH |
1096 | AioContext *replace_aio_context; |
1097 | ||
e12f3784 | 1098 | s->to_replace = bdrv_find_node(s->replaces); |
09158f00 | 1099 | if (!s->to_replace) { |
e12f3784 | 1100 | error_setg(errp, "Node name '%s' not found", s->replaces); |
09158f00 BC |
1101 | return; |
1102 | } | |
1103 | ||
5a7e7a0b SH |
1104 | replace_aio_context = bdrv_get_aio_context(s->to_replace); |
1105 | aio_context_acquire(replace_aio_context); | |
1106 | ||
4ef85a9c KW |
1107 | /* TODO Translate this into permission system. Current definition of |
1108 | * GRAPH_MOD would require to request it for the parents; they might | |
1109 | * not even be BlockDriverStates, however, so a BdrvChild can't address | |
1110 | * them. May need redefinition of GRAPH_MOD. */ | |
09158f00 BC |
1111 | error_setg(&s->replace_blocker, |
1112 | "block device is in use by block-job-complete"); | |
1113 | bdrv_op_block_all(s->to_replace, s->replace_blocker); | |
1114 | bdrv_ref(s->to_replace); | |
5a7e7a0b SH |
1115 | |
1116 | aio_context_release(replace_aio_context); | |
09158f00 BC |
1117 | } |
1118 | ||
d63ffd87 | 1119 | s->should_complete = true; |
3d70ff53 | 1120 | job_enter(job); |
d63ffd87 PB |
1121 | } |
1122 | ||
537c3d4f | 1123 | static void coroutine_fn mirror_pause(Job *job) |
565ac01f | 1124 | { |
da01ff7f | 1125 | MirrorBlockJob *s = container_of(job, MirrorBlockJob, common.job); |
565ac01f | 1126 | |
bae8196d | 1127 | mirror_wait_for_all_io(s); |
565ac01f SH |
1128 | } |
1129 | ||
89bd0305 KW |
1130 | static bool mirror_drained_poll(BlockJob *job) |
1131 | { | |
1132 | MirrorBlockJob *s = container_of(job, MirrorBlockJob, common); | |
5e771752 SL |
1133 | |
1134 | /* If the job isn't paused nor cancelled, we can't be sure that it won't | |
1135 | * issue more requests. We make an exception if we've reached this point | |
1136 | * from one of our own drain sections, to avoid a deadlock waiting for | |
1137 | * ourselves. | |
1138 | */ | |
1139 | if (!s->common.job.paused && !s->common.job.cancelled && !s->in_drain) { | |
1140 | return true; | |
1141 | } | |
1142 | ||
89bd0305 KW |
1143 | return !!s->in_flight; |
1144 | } | |
1145 | ||
bae8196d PB |
1146 | static void mirror_drain(BlockJob *job) |
1147 | { | |
1148 | MirrorBlockJob *s = container_of(job, MirrorBlockJob, common); | |
1149 | ||
1150 | /* Need to keep a reference in case blk_drain triggers execution | |
1151 | * of mirror_complete... | |
1152 | */ | |
1153 | if (s->target) { | |
1154 | BlockBackend *target = s->target; | |
1155 | blk_ref(target); | |
1156 | blk_drain(target); | |
1157 | blk_unref(target); | |
1158 | } | |
1159 | } | |
1160 | ||
3fc4b10a | 1161 | static const BlockJobDriver mirror_job_driver = { |
33e9e9bd KW |
1162 | .job_driver = { |
1163 | .instance_size = sizeof(MirrorBlockJob), | |
252291ea | 1164 | .job_type = JOB_TYPE_MIRROR, |
80fa2c75 | 1165 | .free = block_job_free, |
b15de828 | 1166 | .user_resume = block_job_user_resume, |
b69f777d | 1167 | .drain = block_job_drain, |
f67432a2 | 1168 | .run = mirror_run, |
737efc1e JS |
1169 | .prepare = mirror_prepare, |
1170 | .abort = mirror_abort, | |
da01ff7f | 1171 | .pause = mirror_pause, |
3453d972 | 1172 | .complete = mirror_complete, |
33e9e9bd | 1173 | }, |
89bd0305 | 1174 | .drained_poll = mirror_drained_poll, |
bae8196d | 1175 | .drain = mirror_drain, |
893f7eba PB |
1176 | }; |
1177 | ||
03544a6e | 1178 | static const BlockJobDriver commit_active_job_driver = { |
33e9e9bd KW |
1179 | .job_driver = { |
1180 | .instance_size = sizeof(MirrorBlockJob), | |
252291ea | 1181 | .job_type = JOB_TYPE_COMMIT, |
80fa2c75 | 1182 | .free = block_job_free, |
b15de828 | 1183 | .user_resume = block_job_user_resume, |
b69f777d | 1184 | .drain = block_job_drain, |
f67432a2 | 1185 | .run = mirror_run, |
737efc1e JS |
1186 | .prepare = mirror_prepare, |
1187 | .abort = mirror_abort, | |
da01ff7f | 1188 | .pause = mirror_pause, |
3453d972 | 1189 | .complete = mirror_complete, |
33e9e9bd | 1190 | }, |
89bd0305 | 1191 | .drained_poll = mirror_drained_poll, |
bae8196d | 1192 | .drain = mirror_drain, |
03544a6e FZ |
1193 | }; |
1194 | ||
537c3d4f SH |
1195 | static void coroutine_fn |
1196 | do_sync_target_write(MirrorBlockJob *job, MirrorMethod method, | |
1197 | uint64_t offset, uint64_t bytes, | |
1198 | QEMUIOVector *qiov, int flags) | |
d06107ad | 1199 | { |
d06107ad | 1200 | QEMUIOVector target_qiov; |
1eaf1b0f VSO |
1201 | uint64_t dirty_offset = offset; |
1202 | uint64_t dirty_bytes; | |
d06107ad HR |
1203 | |
1204 | if (qiov) { | |
1205 | qemu_iovec_init(&target_qiov, qiov->niov); | |
1206 | } | |
1207 | ||
d06107ad HR |
1208 | while (true) { |
1209 | bool valid_area; | |
1210 | int ret; | |
1211 | ||
1212 | bdrv_dirty_bitmap_lock(job->dirty_bitmap); | |
1eaf1b0f VSO |
1213 | dirty_bytes = MIN(offset + bytes - dirty_offset, INT_MAX); |
1214 | valid_area = bdrv_dirty_bitmap_next_dirty_area(job->dirty_bitmap, | |
1215 | &dirty_offset, | |
1216 | &dirty_bytes); | |
d06107ad HR |
1217 | if (!valid_area) { |
1218 | bdrv_dirty_bitmap_unlock(job->dirty_bitmap); | |
1219 | break; | |
1220 | } | |
1221 | ||
1222 | bdrv_reset_dirty_bitmap_locked(job->dirty_bitmap, | |
1223 | dirty_offset, dirty_bytes); | |
1224 | bdrv_dirty_bitmap_unlock(job->dirty_bitmap); | |
1225 | ||
1226 | job_progress_increase_remaining(&job->common.job, dirty_bytes); | |
1227 | ||
1228 | assert(dirty_offset - offset <= SIZE_MAX); | |
1229 | if (qiov) { | |
1230 | qemu_iovec_reset(&target_qiov); | |
1231 | qemu_iovec_concat(&target_qiov, qiov, | |
1232 | dirty_offset - offset, dirty_bytes); | |
1233 | } | |
1234 | ||
1235 | switch (method) { | |
1236 | case MIRROR_METHOD_COPY: | |
1237 | ret = blk_co_pwritev(job->target, dirty_offset, dirty_bytes, | |
1238 | qiov ? &target_qiov : NULL, flags); | |
1239 | break; | |
1240 | ||
1241 | case MIRROR_METHOD_ZERO: | |
1242 | assert(!qiov); | |
1243 | ret = blk_co_pwrite_zeroes(job->target, dirty_offset, dirty_bytes, | |
1244 | flags); | |
1245 | break; | |
1246 | ||
1247 | case MIRROR_METHOD_DISCARD: | |
1248 | assert(!qiov); | |
1249 | ret = blk_co_pdiscard(job->target, dirty_offset, dirty_bytes); | |
1250 | break; | |
1251 | ||
1252 | default: | |
1253 | abort(); | |
1254 | } | |
1255 | ||
1256 | if (ret >= 0) { | |
1257 | job_progress_update(&job->common.job, dirty_bytes); | |
1258 | } else { | |
1259 | BlockErrorAction action; | |
1260 | ||
1261 | bdrv_set_dirty_bitmap(job->dirty_bitmap, dirty_offset, dirty_bytes); | |
1262 | job->actively_synced = false; | |
1263 | ||
1264 | action = mirror_error_action(job, false, -ret); | |
1265 | if (action == BLOCK_ERROR_ACTION_REPORT) { | |
1266 | if (!job->ret) { | |
1267 | job->ret = ret; | |
1268 | } | |
1269 | break; | |
1270 | } | |
1271 | } | |
1eaf1b0f VSO |
1272 | |
1273 | dirty_offset += dirty_bytes; | |
d06107ad HR |
1274 | } |
1275 | ||
d06107ad HR |
1276 | if (qiov) { |
1277 | qemu_iovec_destroy(&target_qiov); | |
1278 | } | |
1279 | } | |
1280 | ||
1281 | static MirrorOp *coroutine_fn active_write_prepare(MirrorBlockJob *s, | |
1282 | uint64_t offset, | |
1283 | uint64_t bytes) | |
1284 | { | |
1285 | MirrorOp *op; | |
1286 | uint64_t start_chunk = offset / s->granularity; | |
1287 | uint64_t end_chunk = DIV_ROUND_UP(offset + bytes, s->granularity); | |
1288 | ||
1289 | op = g_new(MirrorOp, 1); | |
1290 | *op = (MirrorOp){ | |
1291 | .s = s, | |
1292 | .offset = offset, | |
1293 | .bytes = bytes, | |
1294 | .is_active_write = true, | |
1295 | }; | |
1296 | qemu_co_queue_init(&op->waiting_requests); | |
1297 | QTAILQ_INSERT_TAIL(&s->ops_in_flight, op, next); | |
1298 | ||
1299 | s->in_active_write_counter++; | |
1300 | ||
1301 | mirror_wait_on_conflicts(op, s, offset, bytes); | |
1302 | ||
1303 | bitmap_set(s->in_flight_bitmap, start_chunk, end_chunk - start_chunk); | |
1304 | ||
1305 | return op; | |
1306 | } | |
1307 | ||
1308 | static void coroutine_fn active_write_settle(MirrorOp *op) | |
1309 | { | |
1310 | uint64_t start_chunk = op->offset / op->s->granularity; | |
1311 | uint64_t end_chunk = DIV_ROUND_UP(op->offset + op->bytes, | |
1312 | op->s->granularity); | |
1313 | ||
1314 | if (!--op->s->in_active_write_counter && op->s->actively_synced) { | |
1315 | BdrvChild *source = op->s->mirror_top_bs->backing; | |
1316 | ||
1317 | if (QLIST_FIRST(&source->bs->parents) == source && | |
1318 | QLIST_NEXT(source, next_parent) == NULL) | |
1319 | { | |
1320 | /* Assert that we are back in sync once all active write | |
1321 | * operations are settled. | |
1322 | * Note that we can only assert this if the mirror node | |
1323 | * is the source node's only parent. */ | |
1324 | assert(!bdrv_get_dirty_count(op->s->dirty_bitmap)); | |
1325 | } | |
1326 | } | |
1327 | bitmap_clear(op->s->in_flight_bitmap, start_chunk, end_chunk - start_chunk); | |
1328 | QTAILQ_REMOVE(&op->s->ops_in_flight, op, next); | |
1329 | qemu_co_queue_restart_all(&op->waiting_requests); | |
1330 | g_free(op); | |
1331 | } | |
1332 | ||
4ef85a9c KW |
1333 | static int coroutine_fn bdrv_mirror_top_preadv(BlockDriverState *bs, |
1334 | uint64_t offset, uint64_t bytes, QEMUIOVector *qiov, int flags) | |
1335 | { | |
1336 | return bdrv_co_preadv(bs->backing, offset, bytes, qiov, flags); | |
1337 | } | |
1338 | ||
d06107ad HR |
1339 | static int coroutine_fn bdrv_mirror_top_do_write(BlockDriverState *bs, |
1340 | MirrorMethod method, uint64_t offset, uint64_t bytes, QEMUIOVector *qiov, | |
1341 | int flags) | |
1342 | { | |
1343 | MirrorOp *op = NULL; | |
1344 | MirrorBDSOpaque *s = bs->opaque; | |
1345 | int ret = 0; | |
1346 | bool copy_to_target; | |
1347 | ||
1348 | copy_to_target = s->job->ret >= 0 && | |
1349 | s->job->copy_mode == MIRROR_COPY_MODE_WRITE_BLOCKING; | |
1350 | ||
1351 | if (copy_to_target) { | |
1352 | op = active_write_prepare(s->job, offset, bytes); | |
1353 | } | |
1354 | ||
1355 | switch (method) { | |
1356 | case MIRROR_METHOD_COPY: | |
1357 | ret = bdrv_co_pwritev(bs->backing, offset, bytes, qiov, flags); | |
1358 | break; | |
1359 | ||
1360 | case MIRROR_METHOD_ZERO: | |
1361 | ret = bdrv_co_pwrite_zeroes(bs->backing, offset, bytes, flags); | |
1362 | break; | |
1363 | ||
1364 | case MIRROR_METHOD_DISCARD: | |
0b9fd3f4 | 1365 | ret = bdrv_co_pdiscard(bs->backing, offset, bytes); |
d06107ad HR |
1366 | break; |
1367 | ||
1368 | default: | |
1369 | abort(); | |
1370 | } | |
1371 | ||
1372 | if (ret < 0) { | |
1373 | goto out; | |
1374 | } | |
1375 | ||
1376 | if (copy_to_target) { | |
1377 | do_sync_target_write(s->job, method, offset, bytes, qiov, flags); | |
1378 | } | |
1379 | ||
1380 | out: | |
1381 | if (copy_to_target) { | |
1382 | active_write_settle(op); | |
1383 | } | |
1384 | return ret; | |
1385 | } | |
1386 | ||
4ef85a9c KW |
1387 | static int coroutine_fn bdrv_mirror_top_pwritev(BlockDriverState *bs, |
1388 | uint64_t offset, uint64_t bytes, QEMUIOVector *qiov, int flags) | |
1389 | { | |
d06107ad HR |
1390 | MirrorBDSOpaque *s = bs->opaque; |
1391 | QEMUIOVector bounce_qiov; | |
1392 | void *bounce_buf; | |
1393 | int ret = 0; | |
1394 | bool copy_to_target; | |
1395 | ||
1396 | copy_to_target = s->job->ret >= 0 && | |
1397 | s->job->copy_mode == MIRROR_COPY_MODE_WRITE_BLOCKING; | |
1398 | ||
1399 | if (copy_to_target) { | |
1400 | /* The guest might concurrently modify the data to write; but | |
1401 | * the data on source and destination must match, so we have | |
1402 | * to use a bounce buffer if we are going to write to the | |
1403 | * target now. */ | |
1404 | bounce_buf = qemu_blockalign(bs, bytes); | |
1405 | iov_to_buf_full(qiov->iov, qiov->niov, 0, bounce_buf, bytes); | |
1406 | ||
1407 | qemu_iovec_init(&bounce_qiov, 1); | |
1408 | qemu_iovec_add(&bounce_qiov, bounce_buf, bytes); | |
1409 | qiov = &bounce_qiov; | |
1410 | } | |
1411 | ||
1412 | ret = bdrv_mirror_top_do_write(bs, MIRROR_METHOD_COPY, offset, bytes, qiov, | |
1413 | flags); | |
1414 | ||
1415 | if (copy_to_target) { | |
1416 | qemu_iovec_destroy(&bounce_qiov); | |
1417 | qemu_vfree(bounce_buf); | |
1418 | } | |
1419 | ||
1420 | return ret; | |
4ef85a9c KW |
1421 | } |
1422 | ||
1423 | static int coroutine_fn bdrv_mirror_top_flush(BlockDriverState *bs) | |
1424 | { | |
ce960aa9 VSO |
1425 | if (bs->backing == NULL) { |
1426 | /* we can be here after failed bdrv_append in mirror_start_job */ | |
1427 | return 0; | |
1428 | } | |
4ef85a9c KW |
1429 | return bdrv_co_flush(bs->backing->bs); |
1430 | } | |
1431 | ||
4ef85a9c | 1432 | static int coroutine_fn bdrv_mirror_top_pwrite_zeroes(BlockDriverState *bs, |
f5a5ca79 | 1433 | int64_t offset, int bytes, BdrvRequestFlags flags) |
4ef85a9c | 1434 | { |
d06107ad HR |
1435 | return bdrv_mirror_top_do_write(bs, MIRROR_METHOD_ZERO, offset, bytes, NULL, |
1436 | flags); | |
4ef85a9c KW |
1437 | } |
1438 | ||
1439 | static int coroutine_fn bdrv_mirror_top_pdiscard(BlockDriverState *bs, | |
f5a5ca79 | 1440 | int64_t offset, int bytes) |
4ef85a9c | 1441 | { |
d06107ad HR |
1442 | return bdrv_mirror_top_do_write(bs, MIRROR_METHOD_DISCARD, offset, bytes, |
1443 | NULL, 0); | |
4ef85a9c KW |
1444 | } |
1445 | ||
998b3a1e | 1446 | static void bdrv_mirror_top_refresh_filename(BlockDriverState *bs) |
fd4a6493 | 1447 | { |
18775ff3 VSO |
1448 | if (bs->backing == NULL) { |
1449 | /* we can be here after failed bdrv_attach_child in | |
1450 | * bdrv_set_backing_hd */ | |
1451 | return; | |
1452 | } | |
fd4a6493 KW |
1453 | pstrcpy(bs->exact_filename, sizeof(bs->exact_filename), |
1454 | bs->backing->bs->filename); | |
1455 | } | |
1456 | ||
4ef85a9c KW |
1457 | static void bdrv_mirror_top_child_perm(BlockDriverState *bs, BdrvChild *c, |
1458 | const BdrvChildRole *role, | |
e0995dc3 | 1459 | BlockReopenQueue *reopen_queue, |
4ef85a9c KW |
1460 | uint64_t perm, uint64_t shared, |
1461 | uint64_t *nperm, uint64_t *nshared) | |
1462 | { | |
f94dc3b4 HR |
1463 | MirrorBDSOpaque *s = bs->opaque; |
1464 | ||
1465 | if (s->stop) { | |
1466 | /* | |
1467 | * If the job is to be stopped, we do not need to forward | |
1468 | * anything to the real image. | |
1469 | */ | |
1470 | *nperm = 0; | |
1471 | *nshared = BLK_PERM_ALL; | |
1472 | return; | |
1473 | } | |
1474 | ||
4ef85a9c KW |
1475 | /* Must be able to forward guest writes to the real image */ |
1476 | *nperm = 0; | |
1477 | if (perm & BLK_PERM_WRITE) { | |
1478 | *nperm |= BLK_PERM_WRITE; | |
1479 | } | |
1480 | ||
1481 | *nshared = BLK_PERM_ALL; | |
1482 | } | |
1483 | ||
1484 | /* Dummy node that provides consistent read to its users without requiring it | |
1485 | * from its backing file and that allows writes on the backing file chain. */ | |
1486 | static BlockDriver bdrv_mirror_top = { | |
1487 | .format_name = "mirror_top", | |
1488 | .bdrv_co_preadv = bdrv_mirror_top_preadv, | |
1489 | .bdrv_co_pwritev = bdrv_mirror_top_pwritev, | |
1490 | .bdrv_co_pwrite_zeroes = bdrv_mirror_top_pwrite_zeroes, | |
1491 | .bdrv_co_pdiscard = bdrv_mirror_top_pdiscard, | |
1492 | .bdrv_co_flush = bdrv_mirror_top_flush, | |
3e4d0e72 | 1493 | .bdrv_co_block_status = bdrv_co_block_status_from_backing, |
fd4a6493 | 1494 | .bdrv_refresh_filename = bdrv_mirror_top_refresh_filename, |
4ef85a9c KW |
1495 | .bdrv_child_perm = bdrv_mirror_top_child_perm, |
1496 | }; | |
1497 | ||
cc19f177 VSO |
1498 | static BlockJob *mirror_start_job( |
1499 | const char *job_id, BlockDriverState *bs, | |
47970dfb JS |
1500 | int creation_flags, BlockDriverState *target, |
1501 | const char *replaces, int64_t speed, | |
1502 | uint32_t granularity, int64_t buf_size, | |
274fccee | 1503 | BlockMirrorBackingMode backing_mode, |
09158f00 BC |
1504 | BlockdevOnError on_source_error, |
1505 | BlockdevOnError on_target_error, | |
0fc9f8ea | 1506 | bool unmap, |
097310b5 | 1507 | BlockCompletionFunc *cb, |
51ccfa2d | 1508 | void *opaque, |
09158f00 | 1509 | const BlockJobDriver *driver, |
b49f7ead | 1510 | bool is_none_mode, BlockDriverState *base, |
51ccfa2d | 1511 | bool auto_complete, const char *filter_node_name, |
481debaa | 1512 | bool is_mirror, MirrorCopyMode copy_mode, |
51ccfa2d | 1513 | Error **errp) |
893f7eba PB |
1514 | { |
1515 | MirrorBlockJob *s; | |
429076e8 | 1516 | MirrorBDSOpaque *bs_opaque; |
4ef85a9c KW |
1517 | BlockDriverState *mirror_top_bs; |
1518 | bool target_graph_mod; | |
1519 | bool target_is_backing; | |
b2c2832c | 1520 | Error *local_err = NULL; |
d7086422 | 1521 | int ret; |
893f7eba | 1522 | |
eee13dfe | 1523 | if (granularity == 0) { |
341ebc2f | 1524 | granularity = bdrv_get_default_bitmap_granularity(target); |
eee13dfe PB |
1525 | } |
1526 | ||
31826642 | 1527 | assert(is_power_of_2(granularity)); |
eee13dfe | 1528 | |
48ac0a4d WC |
1529 | if (buf_size < 0) { |
1530 | error_setg(errp, "Invalid parameter 'buf-size'"); | |
cc19f177 | 1531 | return NULL; |
48ac0a4d WC |
1532 | } |
1533 | ||
1534 | if (buf_size == 0) { | |
1535 | buf_size = DEFAULT_MIRROR_BUF_SIZE; | |
1536 | } | |
5bc361b8 | 1537 | |
86fae10c KW |
1538 | if (bs == target) { |
1539 | error_setg(errp, "Can't mirror node into itself"); | |
cc19f177 | 1540 | return NULL; |
86fae10c KW |
1541 | } |
1542 | ||
4ef85a9c KW |
1543 | /* In the case of active commit, add dummy driver to provide consistent |
1544 | * reads on the top, while disabling it in the intermediate nodes, and make | |
1545 | * the backing chain writable. */ | |
6cdbceb1 KW |
1546 | mirror_top_bs = bdrv_new_open_driver(&bdrv_mirror_top, filter_node_name, |
1547 | BDRV_O_RDWR, errp); | |
4ef85a9c | 1548 | if (mirror_top_bs == NULL) { |
cc19f177 | 1549 | return NULL; |
4ef85a9c | 1550 | } |
d3c8c674 KW |
1551 | if (!filter_node_name) { |
1552 | mirror_top_bs->implicit = true; | |
1553 | } | |
4ef85a9c | 1554 | mirror_top_bs->total_sectors = bs->total_sectors; |
228345bf | 1555 | mirror_top_bs->supported_write_flags = BDRV_REQ_WRITE_UNCHANGED; |
80f5c33f KW |
1556 | mirror_top_bs->supported_zero_flags = BDRV_REQ_WRITE_UNCHANGED | |
1557 | BDRV_REQ_NO_FALLBACK; | |
429076e8 HR |
1558 | bs_opaque = g_new0(MirrorBDSOpaque, 1); |
1559 | mirror_top_bs->opaque = bs_opaque; | |
4ef85a9c KW |
1560 | |
1561 | /* bdrv_append takes ownership of the mirror_top_bs reference, need to keep | |
7a25fcd0 | 1562 | * it alive until block_job_create() succeeds even if bs has no parent. */ |
4ef85a9c KW |
1563 | bdrv_ref(mirror_top_bs); |
1564 | bdrv_drained_begin(bs); | |
b2c2832c | 1565 | bdrv_append(mirror_top_bs, bs, &local_err); |
4ef85a9c KW |
1566 | bdrv_drained_end(bs); |
1567 | ||
b2c2832c KW |
1568 | if (local_err) { |
1569 | bdrv_unref(mirror_top_bs); | |
1570 | error_propagate(errp, local_err); | |
cc19f177 | 1571 | return NULL; |
b2c2832c KW |
1572 | } |
1573 | ||
4ef85a9c | 1574 | /* Make sure that the source is not resized while the job is running */ |
75859b94 | 1575 | s = block_job_create(job_id, driver, NULL, mirror_top_bs, |
4ef85a9c KW |
1576 | BLK_PERM_CONSISTENT_READ, |
1577 | BLK_PERM_CONSISTENT_READ | BLK_PERM_WRITE_UNCHANGED | | |
1578 | BLK_PERM_WRITE | BLK_PERM_GRAPH_MOD, speed, | |
c6cc12bf | 1579 | creation_flags, cb, opaque, errp); |
893f7eba | 1580 | if (!s) { |
4ef85a9c | 1581 | goto fail; |
893f7eba | 1582 | } |
429076e8 HR |
1583 | bs_opaque->job = s; |
1584 | ||
7a25fcd0 HR |
1585 | /* The block job now has a reference to this node */ |
1586 | bdrv_unref(mirror_top_bs); | |
1587 | ||
4ef85a9c KW |
1588 | s->mirror_top_bs = mirror_top_bs; |
1589 | ||
1590 | /* No resize for the target either; while the mirror is still running, a | |
1591 | * consistent read isn't necessarily possible. We could possibly allow | |
1592 | * writes and graph modifications, though it would likely defeat the | |
1593 | * purpose of a mirror, so leave them blocked for now. | |
1594 | * | |
1595 | * In the case of active commit, things look a bit different, though, | |
1596 | * because the target is an already populated backing file in active use. | |
1597 | * We can allow anything except resize there.*/ | |
1598 | target_is_backing = bdrv_chain_contains(bs, target); | |
1599 | target_graph_mod = (backing_mode != MIRROR_LEAVE_BACKING_CHAIN); | |
d861ab3a KW |
1600 | s->target = blk_new(s->common.job.aio_context, |
1601 | BLK_PERM_WRITE | BLK_PERM_RESIZE | | |
4ef85a9c KW |
1602 | (target_graph_mod ? BLK_PERM_GRAPH_MOD : 0), |
1603 | BLK_PERM_WRITE_UNCHANGED | | |
1604 | (target_is_backing ? BLK_PERM_CONSISTENT_READ | | |
1605 | BLK_PERM_WRITE | | |
1606 | BLK_PERM_GRAPH_MOD : 0)); | |
d7086422 KW |
1607 | ret = blk_insert_bs(s->target, target, errp); |
1608 | if (ret < 0) { | |
4ef85a9c | 1609 | goto fail; |
d7086422 | 1610 | } |
045a2f82 FZ |
1611 | if (is_mirror) { |
1612 | /* XXX: Mirror target could be a NBD server of target QEMU in the case | |
1613 | * of non-shared block migration. To allow migration completion, we | |
1614 | * have to allow "inactivate" of the target BB. When that happens, we | |
1615 | * know the job is drained, and the vcpus are stopped, so no write | |
1616 | * operation will be performed. Block layer already has assertions to | |
1617 | * ensure that. */ | |
1618 | blk_set_force_allow_inactivate(s->target); | |
1619 | } | |
9ff7f0df | 1620 | blk_set_allow_aio_context_change(s->target, true); |
e253f4b8 | 1621 | |
09158f00 | 1622 | s->replaces = g_strdup(replaces); |
b952b558 PB |
1623 | s->on_source_error = on_source_error; |
1624 | s->on_target_error = on_target_error; | |
03544a6e | 1625 | s->is_none_mode = is_none_mode; |
274fccee | 1626 | s->backing_mode = backing_mode; |
481debaa | 1627 | s->copy_mode = copy_mode; |
5bc361b8 | 1628 | s->base = base; |
eee13dfe | 1629 | s->granularity = granularity; |
48ac0a4d | 1630 | s->buf_size = ROUND_UP(buf_size, granularity); |
0fc9f8ea | 1631 | s->unmap = unmap; |
b49f7ead WC |
1632 | if (auto_complete) { |
1633 | s->should_complete = true; | |
1634 | } | |
b812f671 | 1635 | |
0db6e54a | 1636 | s->dirty_bitmap = bdrv_create_dirty_bitmap(bs, granularity, NULL, errp); |
b8afb520 | 1637 | if (!s->dirty_bitmap) { |
88f9d1b3 | 1638 | goto fail; |
b8afb520 | 1639 | } |
10f3cd15 | 1640 | |
67b24427 AG |
1641 | ret = block_job_add_bdrv(&s->common, "source", bs, 0, |
1642 | BLK_PERM_WRITE_UNCHANGED | BLK_PERM_WRITE | | |
1643 | BLK_PERM_CONSISTENT_READ, | |
1644 | errp); | |
1645 | if (ret < 0) { | |
1646 | goto fail; | |
1647 | } | |
1648 | ||
4ef85a9c | 1649 | /* Required permissions are already taken with blk_new() */ |
76d554e2 KW |
1650 | block_job_add_bdrv(&s->common, "target", target, 0, BLK_PERM_ALL, |
1651 | &error_abort); | |
1652 | ||
f3ede4b0 AG |
1653 | /* In commit_active_start() all intermediate nodes disappear, so |
1654 | * any jobs in them must be blocked */ | |
4ef85a9c | 1655 | if (target_is_backing) { |
f3ede4b0 AG |
1656 | BlockDriverState *iter; |
1657 | for (iter = backing_bs(bs); iter != target; iter = backing_bs(iter)) { | |
4ef85a9c KW |
1658 | /* XXX BLK_PERM_WRITE needs to be allowed so we don't block |
1659 | * ourselves at s->base (if writes are blocked for a node, they are | |
1660 | * also blocked for its backing file). The other options would be a | |
1661 | * second filter driver above s->base (== target). */ | |
1662 | ret = block_job_add_bdrv(&s->common, "intermediate node", iter, 0, | |
1663 | BLK_PERM_WRITE_UNCHANGED | BLK_PERM_WRITE, | |
1664 | errp); | |
1665 | if (ret < 0) { | |
1666 | goto fail; | |
1667 | } | |
f3ede4b0 | 1668 | } |
ef53dc09 AG |
1669 | |
1670 | if (bdrv_freeze_backing_chain(mirror_top_bs, target, errp) < 0) { | |
1671 | goto fail; | |
1672 | } | |
f3ede4b0 | 1673 | } |
10f3cd15 | 1674 | |
12aa4082 HR |
1675 | QTAILQ_INIT(&s->ops_in_flight); |
1676 | ||
5ccac6f1 | 1677 | trace_mirror_start(bs, s, opaque); |
da01ff7f | 1678 | job_start(&s->common.job); |
cc19f177 VSO |
1679 | |
1680 | return &s->common; | |
4ef85a9c KW |
1681 | |
1682 | fail: | |
1683 | if (s) { | |
7a25fcd0 HR |
1684 | /* Make sure this BDS does not go away until we have completed the graph |
1685 | * changes below */ | |
1686 | bdrv_ref(mirror_top_bs); | |
1687 | ||
4ef85a9c KW |
1688 | g_free(s->replaces); |
1689 | blk_unref(s->target); | |
429076e8 | 1690 | bs_opaque->job = NULL; |
e917e2cb AG |
1691 | if (s->dirty_bitmap) { |
1692 | bdrv_release_dirty_bitmap(bs, s->dirty_bitmap); | |
1693 | } | |
4ad35181 | 1694 | job_early_fail(&s->common.job); |
4ef85a9c KW |
1695 | } |
1696 | ||
f94dc3b4 HR |
1697 | bs_opaque->stop = true; |
1698 | bdrv_child_refresh_perms(mirror_top_bs, mirror_top_bs->backing, | |
1699 | &error_abort); | |
5fe31c25 | 1700 | bdrv_replace_node(mirror_top_bs, backing_bs(mirror_top_bs), &error_abort); |
7a25fcd0 HR |
1701 | |
1702 | bdrv_unref(mirror_top_bs); | |
cc19f177 VSO |
1703 | |
1704 | return NULL; | |
893f7eba | 1705 | } |
03544a6e | 1706 | |
71aa9867 AG |
1707 | void mirror_start(const char *job_id, BlockDriverState *bs, |
1708 | BlockDriverState *target, const char *replaces, | |
a1999b33 JS |
1709 | int creation_flags, int64_t speed, |
1710 | uint32_t granularity, int64_t buf_size, | |
274fccee HR |
1711 | MirrorSyncMode mode, BlockMirrorBackingMode backing_mode, |
1712 | BlockdevOnError on_source_error, | |
03544a6e | 1713 | BlockdevOnError on_target_error, |
481debaa HR |
1714 | bool unmap, const char *filter_node_name, |
1715 | MirrorCopyMode copy_mode, Error **errp) | |
03544a6e FZ |
1716 | { |
1717 | bool is_none_mode; | |
1718 | BlockDriverState *base; | |
1719 | ||
4b80ab2b JS |
1720 | if (mode == MIRROR_SYNC_MODE_INCREMENTAL) { |
1721 | error_setg(errp, "Sync mode 'incremental' not supported"); | |
d58d8453 JS |
1722 | return; |
1723 | } | |
03544a6e | 1724 | is_none_mode = mode == MIRROR_SYNC_MODE_NONE; |
760e0063 | 1725 | base = mode == MIRROR_SYNC_MODE_TOP ? backing_bs(bs) : NULL; |
a1999b33 | 1726 | mirror_start_job(job_id, bs, creation_flags, target, replaces, |
274fccee | 1727 | speed, granularity, buf_size, backing_mode, |
51ccfa2d | 1728 | on_source_error, on_target_error, unmap, NULL, NULL, |
6cdbceb1 | 1729 | &mirror_job_driver, is_none_mode, base, false, |
481debaa | 1730 | filter_node_name, true, copy_mode, errp); |
03544a6e FZ |
1731 | } |
1732 | ||
cc19f177 VSO |
1733 | BlockJob *commit_active_start(const char *job_id, BlockDriverState *bs, |
1734 | BlockDriverState *base, int creation_flags, | |
1735 | int64_t speed, BlockdevOnError on_error, | |
1736 | const char *filter_node_name, | |
1737 | BlockCompletionFunc *cb, void *opaque, | |
1738 | bool auto_complete, Error **errp) | |
03544a6e | 1739 | { |
1ba79388 | 1740 | bool base_read_only; |
cc67f4d1 | 1741 | Error *local_err = NULL; |
cc19f177 | 1742 | BlockJob *ret; |
4da83585 | 1743 | |
1ba79388 | 1744 | base_read_only = bdrv_is_read_only(base); |
4da83585 | 1745 | |
1ba79388 AG |
1746 | if (base_read_only) { |
1747 | if (bdrv_reopen_set_read_only(base, false, errp) < 0) { | |
cc19f177 | 1748 | return NULL; |
1ba79388 | 1749 | } |
20a63d2c | 1750 | } |
4da83585 | 1751 | |
cc19f177 VSO |
1752 | ret = mirror_start_job( |
1753 | job_id, bs, creation_flags, base, NULL, speed, 0, 0, | |
71aa9867 | 1754 | MIRROR_LEAVE_BACKING_CHAIN, |
51ccfa2d | 1755 | on_error, on_error, true, cb, opaque, |
6cdbceb1 | 1756 | &commit_active_job_driver, false, base, auto_complete, |
481debaa HR |
1757 | filter_node_name, false, MIRROR_COPY_MODE_BACKGROUND, |
1758 | &local_err); | |
0fb6395c | 1759 | if (local_err) { |
cc67f4d1 | 1760 | error_propagate(errp, local_err); |
4da83585 JC |
1761 | goto error_restore_flags; |
1762 | } | |
1763 | ||
cc19f177 | 1764 | return ret; |
4da83585 JC |
1765 | |
1766 | error_restore_flags: | |
1767 | /* ignore error and errp for bdrv_reopen, because we want to propagate | |
1768 | * the original error */ | |
1ba79388 AG |
1769 | if (base_read_only) { |
1770 | bdrv_reopen_set_read_only(base, true, NULL); | |
1771 | } | |
cc19f177 | 1772 | return NULL; |
03544a6e | 1773 | } |