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