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