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