]>
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 | ||
14 | #include "trace.h" | |
737e150e PB |
15 | #include "block/blockjob.h" |
16 | #include "block/block_int.h" | |
893f7eba | 17 | #include "qemu/ratelimit.h" |
b812f671 | 18 | #include "qemu/bitmap.h" |
893f7eba | 19 | |
402a4741 PB |
20 | #define SLICE_TIME 100000000ULL /* ns */ |
21 | #define MAX_IN_FLIGHT 16 | |
22 | ||
23 | /* The mirroring buffer is a list of granularity-sized chunks. | |
24 | * Free chunks are organized in a list. | |
25 | */ | |
26 | typedef struct MirrorBuffer { | |
27 | QSIMPLEQ_ENTRY(MirrorBuffer) next; | |
28 | } MirrorBuffer; | |
893f7eba PB |
29 | |
30 | typedef struct MirrorBlockJob { | |
31 | BlockJob common; | |
32 | RateLimit limit; | |
33 | BlockDriverState *target; | |
5bc361b8 | 34 | BlockDriverState *base; |
09158f00 BC |
35 | /* The name of the graph node to replace */ |
36 | char *replaces; | |
37 | /* The BDS to replace */ | |
38 | BlockDriverState *to_replace; | |
39 | /* Used to block operations on the drive-mirror-replace target */ | |
40 | Error *replace_blocker; | |
03544a6e | 41 | bool is_none_mode; |
b952b558 | 42 | BlockdevOnError on_source_error, on_target_error; |
d63ffd87 PB |
43 | bool synced; |
44 | bool should_complete; | |
893f7eba | 45 | int64_t sector_num; |
eee13dfe | 46 | int64_t granularity; |
b812f671 | 47 | size_t buf_size; |
b21c7652 | 48 | int64_t bdev_length; |
b812f671 | 49 | unsigned long *cow_bitmap; |
e4654d2d | 50 | BdrvDirtyBitmap *dirty_bitmap; |
8f0720ec | 51 | HBitmapIter hbi; |
893f7eba | 52 | uint8_t *buf; |
402a4741 PB |
53 | QSIMPLEQ_HEAD(, MirrorBuffer) buf_free; |
54 | int buf_free_count; | |
bd48bde8 | 55 | |
402a4741 | 56 | unsigned long *in_flight_bitmap; |
bd48bde8 | 57 | int in_flight; |
b21c7652 | 58 | int sectors_in_flight; |
bd48bde8 | 59 | int ret; |
893f7eba PB |
60 | } MirrorBlockJob; |
61 | ||
bd48bde8 PB |
62 | typedef struct MirrorOp { |
63 | MirrorBlockJob *s; | |
64 | QEMUIOVector qiov; | |
bd48bde8 PB |
65 | int64_t sector_num; |
66 | int nb_sectors; | |
67 | } MirrorOp; | |
68 | ||
b952b558 PB |
69 | static BlockErrorAction mirror_error_action(MirrorBlockJob *s, bool read, |
70 | int error) | |
71 | { | |
72 | s->synced = false; | |
73 | if (read) { | |
74 | return block_job_error_action(&s->common, s->common.bs, | |
75 | s->on_source_error, true, error); | |
76 | } else { | |
77 | return block_job_error_action(&s->common, s->target, | |
78 | s->on_target_error, false, error); | |
79 | } | |
80 | } | |
81 | ||
bd48bde8 PB |
82 | static void mirror_iteration_done(MirrorOp *op, int ret) |
83 | { | |
84 | MirrorBlockJob *s = op->s; | |
402a4741 | 85 | struct iovec *iov; |
bd48bde8 | 86 | int64_t chunk_num; |
402a4741 | 87 | int i, nb_chunks, sectors_per_chunk; |
bd48bde8 PB |
88 | |
89 | trace_mirror_iteration_done(s, op->sector_num, op->nb_sectors, ret); | |
90 | ||
91 | s->in_flight--; | |
b21c7652 | 92 | s->sectors_in_flight -= op->nb_sectors; |
402a4741 PB |
93 | iov = op->qiov.iov; |
94 | for (i = 0; i < op->qiov.niov; i++) { | |
95 | MirrorBuffer *buf = (MirrorBuffer *) iov[i].iov_base; | |
96 | QSIMPLEQ_INSERT_TAIL(&s->buf_free, buf, next); | |
97 | s->buf_free_count++; | |
98 | } | |
99 | ||
bd48bde8 PB |
100 | sectors_per_chunk = s->granularity >> BDRV_SECTOR_BITS; |
101 | chunk_num = op->sector_num / sectors_per_chunk; | |
102 | nb_chunks = op->nb_sectors / sectors_per_chunk; | |
402a4741 | 103 | bitmap_clear(s->in_flight_bitmap, chunk_num, nb_chunks); |
b21c7652 HR |
104 | if (ret >= 0) { |
105 | if (s->cow_bitmap) { | |
106 | bitmap_set(s->cow_bitmap, chunk_num, nb_chunks); | |
107 | } | |
108 | s->common.offset += (uint64_t)op->nb_sectors * BDRV_SECTOR_SIZE; | |
bd48bde8 PB |
109 | } |
110 | ||
6df3bf8e | 111 | qemu_iovec_destroy(&op->qiov); |
bd48bde8 | 112 | g_slice_free(MirrorOp, op); |
7b770c72 SH |
113 | |
114 | /* Enter coroutine when it is not sleeping. The coroutine sleeps to | |
115 | * rate-limit itself. The coroutine will eventually resume since there is | |
116 | * a sleep timeout so don't wake it early. | |
117 | */ | |
118 | if (s->common.busy) { | |
119 | qemu_coroutine_enter(s->common.co, NULL); | |
120 | } | |
bd48bde8 PB |
121 | } |
122 | ||
123 | static void mirror_write_complete(void *opaque, int ret) | |
124 | { | |
125 | MirrorOp *op = opaque; | |
126 | MirrorBlockJob *s = op->s; | |
127 | if (ret < 0) { | |
bd48bde8 PB |
128 | BlockErrorAction action; |
129 | ||
20dca810 | 130 | bdrv_set_dirty_bitmap(s->dirty_bitmap, op->sector_num, op->nb_sectors); |
bd48bde8 | 131 | action = mirror_error_action(s, false, -ret); |
a589569f | 132 | if (action == BLOCK_ERROR_ACTION_REPORT && s->ret >= 0) { |
bd48bde8 PB |
133 | s->ret = ret; |
134 | } | |
135 | } | |
136 | mirror_iteration_done(op, ret); | |
137 | } | |
138 | ||
139 | static void mirror_read_complete(void *opaque, int ret) | |
140 | { | |
141 | MirrorOp *op = opaque; | |
142 | MirrorBlockJob *s = op->s; | |
143 | if (ret < 0) { | |
bd48bde8 PB |
144 | BlockErrorAction action; |
145 | ||
20dca810 | 146 | bdrv_set_dirty_bitmap(s->dirty_bitmap, op->sector_num, op->nb_sectors); |
bd48bde8 | 147 | action = mirror_error_action(s, true, -ret); |
a589569f | 148 | if (action == BLOCK_ERROR_ACTION_REPORT && s->ret >= 0) { |
bd48bde8 PB |
149 | s->ret = ret; |
150 | } | |
151 | ||
152 | mirror_iteration_done(op, ret); | |
153 | return; | |
154 | } | |
155 | bdrv_aio_writev(s->target, op->sector_num, &op->qiov, op->nb_sectors, | |
156 | mirror_write_complete, op); | |
157 | } | |
158 | ||
cc8c9d6c | 159 | static uint64_t coroutine_fn mirror_iteration(MirrorBlockJob *s) |
893f7eba PB |
160 | { |
161 | BlockDriverState *source = s->common.bs; | |
402a4741 | 162 | int nb_sectors, sectors_per_chunk, nb_chunks; |
884fea4e | 163 | int64_t end, sector_num, next_chunk, next_sector, hbitmap_next_sector; |
6d0de8eb | 164 | uint64_t delay_ns = 0; |
bd48bde8 | 165 | MirrorOp *op; |
893f7eba | 166 | |
8f0720ec PB |
167 | s->sector_num = hbitmap_iter_next(&s->hbi); |
168 | if (s->sector_num < 0) { | |
20dca810 | 169 | bdrv_dirty_iter_init(s->dirty_bitmap, &s->hbi); |
8f0720ec | 170 | s->sector_num = hbitmap_iter_next(&s->hbi); |
20dca810 | 171 | trace_mirror_restart_iter(s, bdrv_get_dirty_count(s->dirty_bitmap)); |
8f0720ec PB |
172 | assert(s->sector_num >= 0); |
173 | } | |
174 | ||
402a4741 | 175 | hbitmap_next_sector = s->sector_num; |
884fea4e PB |
176 | sector_num = s->sector_num; |
177 | sectors_per_chunk = s->granularity >> BDRV_SECTOR_BITS; | |
b21c7652 | 178 | end = s->bdev_length / BDRV_SECTOR_SIZE; |
402a4741 | 179 | |
884fea4e PB |
180 | /* Extend the QEMUIOVector to include all adjacent blocks that will |
181 | * be copied in this operation. | |
b812f671 | 182 | * |
884fea4e PB |
183 | * We have to do this if we have no backing file yet in the destination, |
184 | * and the cluster size is very large. Then we need to do COW ourselves. | |
185 | * The first time a cluster is copied, copy it entirely. Note that, | |
186 | * because both the granularity and the cluster size are powers of two, | |
187 | * the number of sectors to copy cannot exceed one cluster. | |
188 | * | |
189 | * We also want to extend the QEMUIOVector to include more adjacent | |
190 | * dirty blocks if possible, to limit the number of I/O operations and | |
191 | * run efficiently even with a small granularity. | |
b812f671 | 192 | */ |
884fea4e PB |
193 | nb_chunks = 0; |
194 | nb_sectors = 0; | |
195 | next_sector = sector_num; | |
196 | next_chunk = sector_num / sectors_per_chunk; | |
402a4741 PB |
197 | |
198 | /* Wait for I/O to this cluster (from a previous iteration) to be done. */ | |
884fea4e | 199 | while (test_bit(next_chunk, s->in_flight_bitmap)) { |
402a4741 PB |
200 | trace_mirror_yield_in_flight(s, sector_num, s->in_flight); |
201 | qemu_coroutine_yield(); | |
b812f671 PB |
202 | } |
203 | ||
884fea4e PB |
204 | do { |
205 | int added_sectors, added_chunks; | |
206 | ||
e4654d2d | 207 | if (!bdrv_get_dirty(source, s->dirty_bitmap, next_sector) || |
884fea4e PB |
208 | test_bit(next_chunk, s->in_flight_bitmap)) { |
209 | assert(nb_sectors > 0); | |
210 | break; | |
211 | } | |
212 | ||
213 | added_sectors = sectors_per_chunk; | |
214 | if (s->cow_bitmap && !test_bit(next_chunk, s->cow_bitmap)) { | |
215 | bdrv_round_to_clusters(s->target, | |
216 | next_sector, added_sectors, | |
217 | &next_sector, &added_sectors); | |
218 | ||
219 | /* On the first iteration, the rounding may make us copy | |
220 | * sectors before the first dirty one. | |
221 | */ | |
222 | if (next_sector < sector_num) { | |
223 | assert(nb_sectors == 0); | |
224 | sector_num = next_sector; | |
225 | next_chunk = next_sector / sectors_per_chunk; | |
226 | } | |
227 | } | |
228 | ||
229 | added_sectors = MIN(added_sectors, end - (sector_num + nb_sectors)); | |
230 | added_chunks = (added_sectors + sectors_per_chunk - 1) / sectors_per_chunk; | |
231 | ||
232 | /* When doing COW, it may happen that there is not enough space for | |
233 | * a full cluster. Wait if that is the case. | |
234 | */ | |
235 | while (nb_chunks == 0 && s->buf_free_count < added_chunks) { | |
236 | trace_mirror_yield_buf_busy(s, nb_chunks, s->in_flight); | |
237 | qemu_coroutine_yield(); | |
238 | } | |
239 | if (s->buf_free_count < nb_chunks + added_chunks) { | |
240 | trace_mirror_break_buf_busy(s, nb_chunks, s->in_flight); | |
241 | break; | |
242 | } | |
243 | ||
244 | /* We have enough free space to copy these sectors. */ | |
245 | bitmap_set(s->in_flight_bitmap, next_chunk, added_chunks); | |
402a4741 | 246 | |
884fea4e PB |
247 | nb_sectors += added_sectors; |
248 | nb_chunks += added_chunks; | |
249 | next_sector += added_sectors; | |
250 | next_chunk += added_chunks; | |
cc8c9d6c PB |
251 | if (!s->synced && s->common.speed) { |
252 | delay_ns = ratelimit_calculate_delay(&s->limit, added_sectors); | |
cc8c9d6c PB |
253 | } |
254 | } while (delay_ns == 0 && next_sector < end); | |
bd48bde8 PB |
255 | |
256 | /* Allocate a MirrorOp that is used as an AIO callback. */ | |
257 | op = g_slice_new(MirrorOp); | |
258 | op->s = s; | |
bd48bde8 PB |
259 | op->sector_num = sector_num; |
260 | op->nb_sectors = nb_sectors; | |
402a4741 PB |
261 | |
262 | /* Now make a QEMUIOVector taking enough granularity-sized chunks | |
263 | * from s->buf_free. | |
264 | */ | |
265 | qemu_iovec_init(&op->qiov, nb_chunks); | |
266 | next_sector = sector_num; | |
267 | while (nb_chunks-- > 0) { | |
268 | MirrorBuffer *buf = QSIMPLEQ_FIRST(&s->buf_free); | |
5a0f6fd5 KW |
269 | size_t remaining = (nb_sectors * BDRV_SECTOR_SIZE) - op->qiov.size; |
270 | ||
402a4741 PB |
271 | QSIMPLEQ_REMOVE_HEAD(&s->buf_free, next); |
272 | s->buf_free_count--; | |
5a0f6fd5 | 273 | qemu_iovec_add(&op->qiov, buf, MIN(s->granularity, remaining)); |
402a4741 PB |
274 | |
275 | /* Advance the HBitmapIter in parallel, so that we do not examine | |
276 | * the same sector twice. | |
277 | */ | |
e4654d2d FZ |
278 | if (next_sector > hbitmap_next_sector |
279 | && bdrv_get_dirty(source, s->dirty_bitmap, next_sector)) { | |
402a4741 PB |
280 | hbitmap_next_sector = hbitmap_iter_next(&s->hbi); |
281 | } | |
282 | ||
283 | next_sector += sectors_per_chunk; | |
284 | } | |
bd48bde8 | 285 | |
20dca810 | 286 | bdrv_reset_dirty_bitmap(s->dirty_bitmap, sector_num, nb_sectors); |
893f7eba PB |
287 | |
288 | /* Copy the dirty cluster. */ | |
bd48bde8 | 289 | s->in_flight++; |
b21c7652 | 290 | s->sectors_in_flight += nb_sectors; |
b812f671 | 291 | trace_mirror_one_iteration(s, sector_num, nb_sectors); |
bd48bde8 PB |
292 | bdrv_aio_readv(source, sector_num, &op->qiov, nb_sectors, |
293 | mirror_read_complete, op); | |
cc8c9d6c | 294 | return delay_ns; |
bd48bde8 | 295 | } |
b952b558 | 296 | |
402a4741 PB |
297 | static void mirror_free_init(MirrorBlockJob *s) |
298 | { | |
299 | int granularity = s->granularity; | |
300 | size_t buf_size = s->buf_size; | |
301 | uint8_t *buf = s->buf; | |
302 | ||
303 | assert(s->buf_free_count == 0); | |
304 | QSIMPLEQ_INIT(&s->buf_free); | |
305 | while (buf_size != 0) { | |
306 | MirrorBuffer *cur = (MirrorBuffer *)buf; | |
307 | QSIMPLEQ_INSERT_TAIL(&s->buf_free, cur, next); | |
308 | s->buf_free_count++; | |
309 | buf_size -= granularity; | |
310 | buf += granularity; | |
311 | } | |
312 | } | |
313 | ||
bd48bde8 PB |
314 | static void mirror_drain(MirrorBlockJob *s) |
315 | { | |
316 | while (s->in_flight > 0) { | |
317 | qemu_coroutine_yield(); | |
318 | } | |
893f7eba PB |
319 | } |
320 | ||
5a7e7a0b SH |
321 | typedef struct { |
322 | int ret; | |
323 | } MirrorExitData; | |
324 | ||
325 | static void mirror_exit(BlockJob *job, void *opaque) | |
326 | { | |
327 | MirrorBlockJob *s = container_of(job, MirrorBlockJob, common); | |
328 | MirrorExitData *data = opaque; | |
329 | AioContext *replace_aio_context = NULL; | |
330 | ||
331 | if (s->to_replace) { | |
332 | replace_aio_context = bdrv_get_aio_context(s->to_replace); | |
333 | aio_context_acquire(replace_aio_context); | |
334 | } | |
335 | ||
336 | if (s->should_complete && data->ret == 0) { | |
337 | BlockDriverState *to_replace = s->common.bs; | |
338 | if (s->to_replace) { | |
339 | to_replace = s->to_replace; | |
340 | } | |
341 | if (bdrv_get_flags(s->target) != bdrv_get_flags(to_replace)) { | |
342 | bdrv_reopen(s->target, bdrv_get_flags(to_replace), NULL); | |
343 | } | |
344 | bdrv_swap(s->target, to_replace); | |
345 | if (s->common.driver->job_type == BLOCK_JOB_TYPE_COMMIT) { | |
346 | /* drop the bs loop chain formed by the swap: break the loop then | |
347 | * trigger the unref from the top one */ | |
348 | BlockDriverState *p = s->base->backing_hd; | |
349 | bdrv_set_backing_hd(s->base, NULL); | |
350 | bdrv_unref(p); | |
351 | } | |
352 | } | |
353 | if (s->to_replace) { | |
354 | bdrv_op_unblock_all(s->to_replace, s->replace_blocker); | |
355 | error_free(s->replace_blocker); | |
356 | bdrv_unref(s->to_replace); | |
357 | } | |
358 | if (replace_aio_context) { | |
359 | aio_context_release(replace_aio_context); | |
360 | } | |
361 | g_free(s->replaces); | |
362 | bdrv_unref(s->target); | |
363 | block_job_completed(&s->common, data->ret); | |
364 | g_free(data); | |
365 | } | |
366 | ||
893f7eba PB |
367 | static void coroutine_fn mirror_run(void *opaque) |
368 | { | |
369 | MirrorBlockJob *s = opaque; | |
5a7e7a0b | 370 | MirrorExitData *data; |
893f7eba | 371 | BlockDriverState *bs = s->common.bs; |
eee13dfe | 372 | int64_t sector_num, end, sectors_per_chunk, length; |
bd48bde8 | 373 | uint64_t last_pause_ns; |
b812f671 | 374 | BlockDriverInfo bdi; |
1d33936e JC |
375 | char backing_filename[2]; /* we only need 2 characters because we are only |
376 | checking for a NULL string */ | |
893f7eba PB |
377 | int ret = 0; |
378 | int n; | |
893f7eba PB |
379 | |
380 | if (block_job_is_cancelled(&s->common)) { | |
381 | goto immediate_exit; | |
382 | } | |
383 | ||
b21c7652 HR |
384 | s->bdev_length = bdrv_getlength(bs); |
385 | if (s->bdev_length < 0) { | |
386 | ret = s->bdev_length; | |
373df5b1 | 387 | goto immediate_exit; |
b21c7652 | 388 | } else if (s->bdev_length == 0) { |
9e48b025 FZ |
389 | /* Report BLOCK_JOB_READY and wait for complete. */ |
390 | block_job_event_ready(&s->common); | |
391 | s->synced = true; | |
392 | while (!block_job_is_cancelled(&s->common) && !s->should_complete) { | |
393 | block_job_yield(&s->common); | |
394 | } | |
395 | s->common.cancelled = false; | |
396 | goto immediate_exit; | |
893f7eba PB |
397 | } |
398 | ||
b21c7652 | 399 | length = DIV_ROUND_UP(s->bdev_length, s->granularity); |
402a4741 PB |
400 | s->in_flight_bitmap = bitmap_new(length); |
401 | ||
b812f671 PB |
402 | /* If we have no backing file yet in the destination, we cannot let |
403 | * the destination do COW. Instead, we copy sectors around the | |
404 | * dirty data if needed. We need a bitmap to do that. | |
405 | */ | |
406 | bdrv_get_backing_filename(s->target, backing_filename, | |
407 | sizeof(backing_filename)); | |
408 | if (backing_filename[0] && !s->target->backing_hd) { | |
c3cc95bd FZ |
409 | ret = bdrv_get_info(s->target, &bdi); |
410 | if (ret < 0) { | |
411 | goto immediate_exit; | |
412 | } | |
eee13dfe | 413 | if (s->granularity < bdi.cluster_size) { |
08e4ed6c | 414 | s->buf_size = MAX(s->buf_size, bdi.cluster_size); |
b812f671 PB |
415 | s->cow_bitmap = bitmap_new(length); |
416 | } | |
417 | } | |
418 | ||
b21c7652 | 419 | end = s->bdev_length / BDRV_SECTOR_SIZE; |
7504edf4 KW |
420 | s->buf = qemu_try_blockalign(bs, s->buf_size); |
421 | if (s->buf == NULL) { | |
422 | ret = -ENOMEM; | |
423 | goto immediate_exit; | |
424 | } | |
425 | ||
eee13dfe | 426 | sectors_per_chunk = s->granularity >> BDRV_SECTOR_BITS; |
402a4741 | 427 | mirror_free_init(s); |
893f7eba | 428 | |
03544a6e | 429 | if (!s->is_none_mode) { |
893f7eba | 430 | /* First part, loop on the sectors and initialize the dirty bitmap. */ |
5bc361b8 | 431 | BlockDriverState *base = s->base; |
893f7eba | 432 | for (sector_num = 0; sector_num < end; ) { |
eee13dfe | 433 | int64_t next = (sector_num | (sectors_per_chunk - 1)) + 1; |
4f578637 PB |
434 | ret = bdrv_is_allocated_above(bs, base, |
435 | sector_num, next - sector_num, &n); | |
893f7eba PB |
436 | |
437 | if (ret < 0) { | |
438 | goto immediate_exit; | |
439 | } | |
440 | ||
441 | assert(n > 0); | |
442 | if (ret == 1) { | |
20dca810 | 443 | bdrv_set_dirty_bitmap(s->dirty_bitmap, sector_num, n); |
893f7eba PB |
444 | sector_num = next; |
445 | } else { | |
446 | sector_num += n; | |
447 | } | |
448 | } | |
449 | } | |
450 | ||
20dca810 | 451 | bdrv_dirty_iter_init(s->dirty_bitmap, &s->hbi); |
bc72ad67 | 452 | last_pause_ns = qemu_clock_get_ns(QEMU_CLOCK_REALTIME); |
893f7eba | 453 | for (;;) { |
cc8c9d6c | 454 | uint64_t delay_ns = 0; |
893f7eba PB |
455 | int64_t cnt; |
456 | bool should_complete; | |
457 | ||
bd48bde8 PB |
458 | if (s->ret < 0) { |
459 | ret = s->ret; | |
460 | goto immediate_exit; | |
461 | } | |
462 | ||
20dca810 | 463 | cnt = bdrv_get_dirty_count(s->dirty_bitmap); |
b21c7652 HR |
464 | /* s->common.offset contains the number of bytes already processed so |
465 | * far, cnt is the number of dirty sectors remaining and | |
466 | * s->sectors_in_flight is the number of sectors currently being | |
467 | * processed; together those are the current total operation length */ | |
468 | s->common.len = s->common.offset + | |
469 | (cnt + s->sectors_in_flight) * BDRV_SECTOR_SIZE; | |
bd48bde8 PB |
470 | |
471 | /* Note that even when no rate limit is applied we need to yield | |
a7282330 | 472 | * periodically with no pending I/O so that bdrv_drain_all() returns. |
bd48bde8 PB |
473 | * We do so every SLICE_TIME nanoseconds, or when there is an error, |
474 | * or when the source is clean, whichever comes first. | |
475 | */ | |
bc72ad67 | 476 | if (qemu_clock_get_ns(QEMU_CLOCK_REALTIME) - last_pause_ns < SLICE_TIME && |
bd48bde8 | 477 | s->common.iostatus == BLOCK_DEVICE_IO_STATUS_OK) { |
402a4741 PB |
478 | if (s->in_flight == MAX_IN_FLIGHT || s->buf_free_count == 0 || |
479 | (cnt == 0 && s->in_flight > 0)) { | |
480 | trace_mirror_yield(s, s->in_flight, s->buf_free_count, cnt); | |
bd48bde8 PB |
481 | qemu_coroutine_yield(); |
482 | continue; | |
483 | } else if (cnt != 0) { | |
cc8c9d6c | 484 | delay_ns = mirror_iteration(s); |
893f7eba | 485 | } |
893f7eba PB |
486 | } |
487 | ||
488 | should_complete = false; | |
bd48bde8 | 489 | if (s->in_flight == 0 && cnt == 0) { |
893f7eba PB |
490 | trace_mirror_before_flush(s); |
491 | ret = bdrv_flush(s->target); | |
492 | if (ret < 0) { | |
a589569f WX |
493 | if (mirror_error_action(s, false, -ret) == |
494 | BLOCK_ERROR_ACTION_REPORT) { | |
b952b558 PB |
495 | goto immediate_exit; |
496 | } | |
497 | } else { | |
498 | /* We're out of the streaming phase. From now on, if the job | |
499 | * is cancelled we will actually complete all pending I/O and | |
500 | * report completion. This way, block-job-cancel will leave | |
501 | * the target in a consistent state. | |
502 | */ | |
b952b558 | 503 | if (!s->synced) { |
bcada37b | 504 | block_job_event_ready(&s->common); |
b952b558 PB |
505 | s->synced = true; |
506 | } | |
507 | ||
508 | should_complete = s->should_complete || | |
509 | block_job_is_cancelled(&s->common); | |
20dca810 | 510 | cnt = bdrv_get_dirty_count(s->dirty_bitmap); |
d63ffd87 | 511 | } |
893f7eba PB |
512 | } |
513 | ||
514 | if (cnt == 0 && should_complete) { | |
515 | /* The dirty bitmap is not updated while operations are pending. | |
516 | * If we're about to exit, wait for pending operations before | |
517 | * calling bdrv_get_dirty_count(bs), or we may exit while the | |
518 | * source has dirty data to copy! | |
519 | * | |
520 | * Note that I/O can be submitted by the guest while | |
521 | * mirror_populate runs. | |
522 | */ | |
523 | trace_mirror_before_drain(s, cnt); | |
5a7e7a0b | 524 | bdrv_drain(bs); |
20dca810 | 525 | cnt = bdrv_get_dirty_count(s->dirty_bitmap); |
893f7eba PB |
526 | } |
527 | ||
528 | ret = 0; | |
cc8c9d6c | 529 | trace_mirror_before_sleep(s, cnt, s->synced, delay_ns); |
d63ffd87 | 530 | if (!s->synced) { |
7483d1e5 | 531 | block_job_sleep_ns(&s->common, QEMU_CLOCK_REALTIME, delay_ns); |
893f7eba PB |
532 | if (block_job_is_cancelled(&s->common)) { |
533 | break; | |
534 | } | |
535 | } else if (!should_complete) { | |
bd48bde8 | 536 | delay_ns = (s->in_flight == 0 && cnt == 0 ? SLICE_TIME : 0); |
7483d1e5 | 537 | block_job_sleep_ns(&s->common, QEMU_CLOCK_REALTIME, delay_ns); |
893f7eba PB |
538 | } else if (cnt == 0) { |
539 | /* The two disks are in sync. Exit and report successful | |
540 | * completion. | |
541 | */ | |
542 | assert(QLIST_EMPTY(&bs->tracked_requests)); | |
543 | s->common.cancelled = false; | |
544 | break; | |
545 | } | |
bc72ad67 | 546 | last_pause_ns = qemu_clock_get_ns(QEMU_CLOCK_REALTIME); |
893f7eba PB |
547 | } |
548 | ||
549 | immediate_exit: | |
bd48bde8 PB |
550 | if (s->in_flight > 0) { |
551 | /* We get here only if something went wrong. Either the job failed, | |
552 | * or it was cancelled prematurely so that we do not guarantee that | |
553 | * the target is a copy of the source. | |
554 | */ | |
555 | assert(ret < 0 || (!s->synced && block_job_is_cancelled(&s->common))); | |
556 | mirror_drain(s); | |
557 | } | |
558 | ||
559 | assert(s->in_flight == 0); | |
7191bf31 | 560 | qemu_vfree(s->buf); |
b812f671 | 561 | g_free(s->cow_bitmap); |
402a4741 | 562 | g_free(s->in_flight_bitmap); |
e4654d2d | 563 | bdrv_release_dirty_bitmap(bs, s->dirty_bitmap); |
b952b558 | 564 | bdrv_iostatus_disable(s->target); |
5a7e7a0b SH |
565 | |
566 | data = g_malloc(sizeof(*data)); | |
567 | data->ret = ret; | |
568 | block_job_defer_to_main_loop(&s->common, mirror_exit, data); | |
893f7eba PB |
569 | } |
570 | ||
571 | static void mirror_set_speed(BlockJob *job, int64_t speed, Error **errp) | |
572 | { | |
573 | MirrorBlockJob *s = container_of(job, MirrorBlockJob, common); | |
574 | ||
575 | if (speed < 0) { | |
576 | error_set(errp, QERR_INVALID_PARAMETER, "speed"); | |
577 | return; | |
578 | } | |
579 | ratelimit_set_speed(&s->limit, speed / BDRV_SECTOR_SIZE, SLICE_TIME); | |
580 | } | |
581 | ||
b952b558 PB |
582 | static void mirror_iostatus_reset(BlockJob *job) |
583 | { | |
584 | MirrorBlockJob *s = container_of(job, MirrorBlockJob, common); | |
585 | ||
586 | bdrv_iostatus_reset(s->target); | |
587 | } | |
588 | ||
d63ffd87 PB |
589 | static void mirror_complete(BlockJob *job, Error **errp) |
590 | { | |
591 | MirrorBlockJob *s = container_of(job, MirrorBlockJob, common); | |
34b5d2c6 | 592 | Error *local_err = NULL; |
d63ffd87 PB |
593 | int ret; |
594 | ||
34b5d2c6 | 595 | ret = bdrv_open_backing_file(s->target, NULL, &local_err); |
d63ffd87 | 596 | if (ret < 0) { |
34b5d2c6 | 597 | error_propagate(errp, local_err); |
d63ffd87 PB |
598 | return; |
599 | } | |
600 | if (!s->synced) { | |
bfb197e0 MA |
601 | error_set(errp, QERR_BLOCK_JOB_NOT_READY, |
602 | bdrv_get_device_name(job->bs)); | |
d63ffd87 PB |
603 | return; |
604 | } | |
605 | ||
09158f00 BC |
606 | /* check the target bs is not blocked and block all operations on it */ |
607 | if (s->replaces) { | |
5a7e7a0b SH |
608 | AioContext *replace_aio_context; |
609 | ||
09158f00 BC |
610 | s->to_replace = check_to_replace_node(s->replaces, &local_err); |
611 | if (!s->to_replace) { | |
612 | error_propagate(errp, local_err); | |
613 | return; | |
614 | } | |
615 | ||
5a7e7a0b SH |
616 | replace_aio_context = bdrv_get_aio_context(s->to_replace); |
617 | aio_context_acquire(replace_aio_context); | |
618 | ||
09158f00 BC |
619 | error_setg(&s->replace_blocker, |
620 | "block device is in use by block-job-complete"); | |
621 | bdrv_op_block_all(s->to_replace, s->replace_blocker); | |
622 | bdrv_ref(s->to_replace); | |
5a7e7a0b SH |
623 | |
624 | aio_context_release(replace_aio_context); | |
09158f00 BC |
625 | } |
626 | ||
d63ffd87 | 627 | s->should_complete = true; |
751ebd76 | 628 | block_job_enter(&s->common); |
d63ffd87 PB |
629 | } |
630 | ||
3fc4b10a | 631 | static const BlockJobDriver mirror_job_driver = { |
893f7eba | 632 | .instance_size = sizeof(MirrorBlockJob), |
79e14bf7 | 633 | .job_type = BLOCK_JOB_TYPE_MIRROR, |
893f7eba | 634 | .set_speed = mirror_set_speed, |
b952b558 | 635 | .iostatus_reset= mirror_iostatus_reset, |
d63ffd87 | 636 | .complete = mirror_complete, |
893f7eba PB |
637 | }; |
638 | ||
03544a6e FZ |
639 | static const BlockJobDriver commit_active_job_driver = { |
640 | .instance_size = sizeof(MirrorBlockJob), | |
641 | .job_type = BLOCK_JOB_TYPE_COMMIT, | |
642 | .set_speed = mirror_set_speed, | |
643 | .iostatus_reset | |
644 | = mirror_iostatus_reset, | |
645 | .complete = mirror_complete, | |
646 | }; | |
647 | ||
648 | static void mirror_start_job(BlockDriverState *bs, BlockDriverState *target, | |
09158f00 | 649 | const char *replaces, |
5fba6c0e | 650 | int64_t speed, uint32_t granularity, |
09158f00 BC |
651 | int64_t buf_size, |
652 | BlockdevOnError on_source_error, | |
653 | BlockdevOnError on_target_error, | |
097310b5 | 654 | BlockCompletionFunc *cb, |
09158f00 BC |
655 | void *opaque, Error **errp, |
656 | const BlockJobDriver *driver, | |
657 | bool is_none_mode, BlockDriverState *base) | |
893f7eba PB |
658 | { |
659 | MirrorBlockJob *s; | |
660 | ||
eee13dfe | 661 | if (granularity == 0) { |
341ebc2f | 662 | granularity = bdrv_get_default_bitmap_granularity(target); |
eee13dfe PB |
663 | } |
664 | ||
665 | assert ((granularity & (granularity - 1)) == 0); | |
666 | ||
b952b558 PB |
667 | if ((on_source_error == BLOCKDEV_ON_ERROR_STOP || |
668 | on_source_error == BLOCKDEV_ON_ERROR_ENOSPC) && | |
669 | !bdrv_iostatus_is_enabled(bs)) { | |
670 | error_set(errp, QERR_INVALID_PARAMETER, "on-source-error"); | |
671 | return; | |
672 | } | |
673 | ||
5bc361b8 | 674 | |
03544a6e | 675 | s = block_job_create(driver, bs, speed, cb, opaque, errp); |
893f7eba PB |
676 | if (!s) { |
677 | return; | |
678 | } | |
679 | ||
09158f00 | 680 | s->replaces = g_strdup(replaces); |
b952b558 PB |
681 | s->on_source_error = on_source_error; |
682 | s->on_target_error = on_target_error; | |
893f7eba | 683 | s->target = target; |
03544a6e | 684 | s->is_none_mode = is_none_mode; |
5bc361b8 | 685 | s->base = base; |
eee13dfe | 686 | s->granularity = granularity; |
08e4ed6c | 687 | s->buf_size = MAX(buf_size, granularity); |
b812f671 | 688 | |
0db6e54a | 689 | s->dirty_bitmap = bdrv_create_dirty_bitmap(bs, granularity, NULL, errp); |
b8afb520 FZ |
690 | if (!s->dirty_bitmap) { |
691 | return; | |
692 | } | |
893f7eba | 693 | bdrv_set_enable_write_cache(s->target, true); |
b952b558 PB |
694 | bdrv_set_on_error(s->target, on_target_error, on_target_error); |
695 | bdrv_iostatus_enable(s->target); | |
893f7eba PB |
696 | s->common.co = qemu_coroutine_create(mirror_run); |
697 | trace_mirror_start(bs, s, s->common.co, opaque); | |
698 | qemu_coroutine_enter(s->common.co, s); | |
699 | } | |
03544a6e FZ |
700 | |
701 | void mirror_start(BlockDriverState *bs, BlockDriverState *target, | |
09158f00 | 702 | const char *replaces, |
5fba6c0e | 703 | int64_t speed, uint32_t granularity, int64_t buf_size, |
03544a6e FZ |
704 | MirrorSyncMode mode, BlockdevOnError on_source_error, |
705 | BlockdevOnError on_target_error, | |
097310b5 | 706 | BlockCompletionFunc *cb, |
03544a6e FZ |
707 | void *opaque, Error **errp) |
708 | { | |
709 | bool is_none_mode; | |
710 | BlockDriverState *base; | |
711 | ||
d58d8453 JS |
712 | if (mode == MIRROR_SYNC_MODE_DIRTY_BITMAP) { |
713 | error_setg(errp, "Sync mode 'dirty-bitmap' not supported"); | |
714 | return; | |
715 | } | |
03544a6e FZ |
716 | is_none_mode = mode == MIRROR_SYNC_MODE_NONE; |
717 | base = mode == MIRROR_SYNC_MODE_TOP ? bs->backing_hd : NULL; | |
09158f00 BC |
718 | mirror_start_job(bs, target, replaces, |
719 | speed, granularity, buf_size, | |
03544a6e FZ |
720 | on_source_error, on_target_error, cb, opaque, errp, |
721 | &mirror_job_driver, is_none_mode, base); | |
722 | } | |
723 | ||
724 | void commit_active_start(BlockDriverState *bs, BlockDriverState *base, | |
725 | int64_t speed, | |
726 | BlockdevOnError on_error, | |
097310b5 | 727 | BlockCompletionFunc *cb, |
03544a6e FZ |
728 | void *opaque, Error **errp) |
729 | { | |
4da83585 JC |
730 | int64_t length, base_length; |
731 | int orig_base_flags; | |
39a611a3 | 732 | int ret; |
cc67f4d1 | 733 | Error *local_err = NULL; |
4da83585 JC |
734 | |
735 | orig_base_flags = bdrv_get_flags(base); | |
736 | ||
20a63d2c FZ |
737 | if (bdrv_reopen(base, bs->open_flags, errp)) { |
738 | return; | |
739 | } | |
4da83585 JC |
740 | |
741 | length = bdrv_getlength(bs); | |
742 | if (length < 0) { | |
39a611a3 JC |
743 | error_setg_errno(errp, -length, |
744 | "Unable to determine length of %s", bs->filename); | |
4da83585 JC |
745 | goto error_restore_flags; |
746 | } | |
747 | ||
748 | base_length = bdrv_getlength(base); | |
749 | if (base_length < 0) { | |
39a611a3 JC |
750 | error_setg_errno(errp, -base_length, |
751 | "Unable to determine length of %s", base->filename); | |
4da83585 JC |
752 | goto error_restore_flags; |
753 | } | |
754 | ||
755 | if (length > base_length) { | |
39a611a3 JC |
756 | ret = bdrv_truncate(base, length); |
757 | if (ret < 0) { | |
758 | error_setg_errno(errp, -ret, | |
759 | "Top image %s is larger than base image %s, and " | |
4da83585 JC |
760 | "resize of base image failed", |
761 | bs->filename, base->filename); | |
762 | goto error_restore_flags; | |
763 | } | |
764 | } | |
765 | ||
20a63d2c | 766 | bdrv_ref(base); |
09158f00 | 767 | mirror_start_job(bs, base, NULL, speed, 0, 0, |
cc67f4d1 | 768 | on_error, on_error, cb, opaque, &local_err, |
03544a6e | 769 | &commit_active_job_driver, false, base); |
0fb6395c | 770 | if (local_err) { |
cc67f4d1 | 771 | error_propagate(errp, local_err); |
4da83585 JC |
772 | goto error_restore_flags; |
773 | } | |
774 | ||
775 | return; | |
776 | ||
777 | error_restore_flags: | |
778 | /* ignore error and errp for bdrv_reopen, because we want to propagate | |
779 | * the original error */ | |
780 | bdrv_reopen(base, orig_base_flags, NULL); | |
781 | return; | |
03544a6e | 782 | } |