]>
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 | ||
80c71a24 | 14 | #include "qemu/osdep.h" |
893f7eba | 15 | #include "trace.h" |
737e150e PB |
16 | #include "block/blockjob.h" |
17 | #include "block/block_int.h" | |
373340b2 | 18 | #include "sysemu/block-backend.h" |
da34e65c | 19 | #include "qapi/error.h" |
cc7a8ea7 | 20 | #include "qapi/qmp/qerror.h" |
893f7eba | 21 | #include "qemu/ratelimit.h" |
b812f671 | 22 | #include "qemu/bitmap.h" |
893f7eba | 23 | |
402a4741 PB |
24 | #define SLICE_TIME 100000000ULL /* ns */ |
25 | #define MAX_IN_FLIGHT 16 | |
48ac0a4d | 26 | #define DEFAULT_MIRROR_BUF_SIZE (10 << 20) |
402a4741 PB |
27 | |
28 | /* The mirroring buffer is a list of granularity-sized chunks. | |
29 | * Free chunks are organized in a list. | |
30 | */ | |
31 | typedef struct MirrorBuffer { | |
32 | QSIMPLEQ_ENTRY(MirrorBuffer) next; | |
33 | } MirrorBuffer; | |
893f7eba PB |
34 | |
35 | typedef struct MirrorBlockJob { | |
36 | BlockJob common; | |
37 | RateLimit limit; | |
e253f4b8 | 38 | BlockBackend *target; |
5bc361b8 | 39 | BlockDriverState *base; |
09158f00 BC |
40 | /* The name of the graph node to replace */ |
41 | char *replaces; | |
42 | /* The BDS to replace */ | |
43 | BlockDriverState *to_replace; | |
44 | /* Used to block operations on the drive-mirror-replace target */ | |
45 | Error *replace_blocker; | |
03544a6e | 46 | bool is_none_mode; |
274fccee | 47 | BlockMirrorBackingMode backing_mode; |
b952b558 | 48 | BlockdevOnError on_source_error, on_target_error; |
d63ffd87 PB |
49 | bool synced; |
50 | bool should_complete; | |
eee13dfe | 51 | int64_t granularity; |
b812f671 | 52 | size_t buf_size; |
b21c7652 | 53 | int64_t bdev_length; |
b812f671 | 54 | unsigned long *cow_bitmap; |
e4654d2d | 55 | BdrvDirtyBitmap *dirty_bitmap; |
8f0720ec | 56 | HBitmapIter hbi; |
893f7eba | 57 | uint8_t *buf; |
402a4741 PB |
58 | QSIMPLEQ_HEAD(, MirrorBuffer) buf_free; |
59 | int buf_free_count; | |
bd48bde8 | 60 | |
402a4741 | 61 | unsigned long *in_flight_bitmap; |
bd48bde8 | 62 | int in_flight; |
b21c7652 | 63 | int sectors_in_flight; |
bd48bde8 | 64 | int ret; |
0fc9f8ea | 65 | bool unmap; |
e424aff5 | 66 | bool waiting_for_io; |
e5b43573 FZ |
67 | int target_cluster_sectors; |
68 | int max_iov; | |
893f7eba PB |
69 | } MirrorBlockJob; |
70 | ||
bd48bde8 PB |
71 | typedef struct MirrorOp { |
72 | MirrorBlockJob *s; | |
73 | QEMUIOVector qiov; | |
bd48bde8 PB |
74 | int64_t sector_num; |
75 | int nb_sectors; | |
76 | } MirrorOp; | |
77 | ||
b952b558 PB |
78 | static BlockErrorAction mirror_error_action(MirrorBlockJob *s, bool read, |
79 | int error) | |
80 | { | |
81 | s->synced = false; | |
82 | if (read) { | |
81e254dc KW |
83 | return block_job_error_action(&s->common, s->on_source_error, |
84 | true, error); | |
b952b558 | 85 | } else { |
81e254dc KW |
86 | return block_job_error_action(&s->common, s->on_target_error, |
87 | false, error); | |
b952b558 PB |
88 | } |
89 | } | |
90 | ||
bd48bde8 PB |
91 | static void mirror_iteration_done(MirrorOp *op, int ret) |
92 | { | |
93 | MirrorBlockJob *s = op->s; | |
402a4741 | 94 | struct iovec *iov; |
bd48bde8 | 95 | int64_t chunk_num; |
402a4741 | 96 | int i, nb_chunks, sectors_per_chunk; |
bd48bde8 PB |
97 | |
98 | trace_mirror_iteration_done(s, op->sector_num, op->nb_sectors, ret); | |
99 | ||
100 | s->in_flight--; | |
b21c7652 | 101 | s->sectors_in_flight -= op->nb_sectors; |
402a4741 PB |
102 | iov = op->qiov.iov; |
103 | for (i = 0; i < op->qiov.niov; i++) { | |
104 | MirrorBuffer *buf = (MirrorBuffer *) iov[i].iov_base; | |
105 | QSIMPLEQ_INSERT_TAIL(&s->buf_free, buf, next); | |
106 | s->buf_free_count++; | |
107 | } | |
108 | ||
bd48bde8 PB |
109 | sectors_per_chunk = s->granularity >> BDRV_SECTOR_BITS; |
110 | chunk_num = op->sector_num / sectors_per_chunk; | |
4150ae60 | 111 | nb_chunks = DIV_ROUND_UP(op->nb_sectors, sectors_per_chunk); |
402a4741 | 112 | bitmap_clear(s->in_flight_bitmap, chunk_num, nb_chunks); |
b21c7652 HR |
113 | if (ret >= 0) { |
114 | if (s->cow_bitmap) { | |
115 | bitmap_set(s->cow_bitmap, chunk_num, nb_chunks); | |
116 | } | |
117 | s->common.offset += (uint64_t)op->nb_sectors * BDRV_SECTOR_SIZE; | |
bd48bde8 PB |
118 | } |
119 | ||
6df3bf8e | 120 | qemu_iovec_destroy(&op->qiov); |
c84b3192 | 121 | g_free(op); |
7b770c72 | 122 | |
e424aff5 | 123 | if (s->waiting_for_io) { |
7b770c72 SH |
124 | qemu_coroutine_enter(s->common.co, NULL); |
125 | } | |
bd48bde8 PB |
126 | } |
127 | ||
128 | static void mirror_write_complete(void *opaque, int ret) | |
129 | { | |
130 | MirrorOp *op = opaque; | |
131 | MirrorBlockJob *s = op->s; | |
132 | if (ret < 0) { | |
bd48bde8 PB |
133 | BlockErrorAction action; |
134 | ||
20dca810 | 135 | bdrv_set_dirty_bitmap(s->dirty_bitmap, op->sector_num, op->nb_sectors); |
bd48bde8 | 136 | action = mirror_error_action(s, false, -ret); |
a589569f | 137 | if (action == BLOCK_ERROR_ACTION_REPORT && s->ret >= 0) { |
bd48bde8 PB |
138 | s->ret = ret; |
139 | } | |
140 | } | |
141 | mirror_iteration_done(op, ret); | |
142 | } | |
143 | ||
144 | static void mirror_read_complete(void *opaque, int ret) | |
145 | { | |
146 | MirrorOp *op = opaque; | |
147 | MirrorBlockJob *s = op->s; | |
148 | if (ret < 0) { | |
bd48bde8 PB |
149 | BlockErrorAction action; |
150 | ||
20dca810 | 151 | bdrv_set_dirty_bitmap(s->dirty_bitmap, op->sector_num, op->nb_sectors); |
bd48bde8 | 152 | action = mirror_error_action(s, true, -ret); |
a589569f | 153 | if (action == BLOCK_ERROR_ACTION_REPORT && s->ret >= 0) { |
bd48bde8 PB |
154 | s->ret = ret; |
155 | } | |
156 | ||
157 | mirror_iteration_done(op, ret); | |
158 | return; | |
159 | } | |
e253f4b8 | 160 | blk_aio_pwritev(s->target, op->sector_num * BDRV_SECTOR_SIZE, &op->qiov, |
73698c30 | 161 | 0, mirror_write_complete, op); |
bd48bde8 PB |
162 | } |
163 | ||
4150ae60 FZ |
164 | static inline void mirror_clip_sectors(MirrorBlockJob *s, |
165 | int64_t sector_num, | |
166 | int *nb_sectors) | |
167 | { | |
168 | *nb_sectors = MIN(*nb_sectors, | |
169 | s->bdev_length / BDRV_SECTOR_SIZE - sector_num); | |
170 | } | |
171 | ||
e5b43573 FZ |
172 | /* Round sector_num and/or nb_sectors to target cluster if COW is needed, and |
173 | * return the offset of the adjusted tail sector against original. */ | |
174 | static int mirror_cow_align(MirrorBlockJob *s, | |
175 | int64_t *sector_num, | |
176 | int *nb_sectors) | |
893f7eba | 177 | { |
e5b43573 FZ |
178 | bool need_cow; |
179 | int ret = 0; | |
180 | int chunk_sectors = s->granularity >> BDRV_SECTOR_BITS; | |
181 | int64_t align_sector_num = *sector_num; | |
182 | int align_nb_sectors = *nb_sectors; | |
183 | int max_sectors = chunk_sectors * s->max_iov; | |
184 | ||
185 | need_cow = !test_bit(*sector_num / chunk_sectors, s->cow_bitmap); | |
186 | need_cow |= !test_bit((*sector_num + *nb_sectors - 1) / chunk_sectors, | |
187 | s->cow_bitmap); | |
188 | if (need_cow) { | |
244483e6 KW |
189 | bdrv_round_sectors_to_clusters(blk_bs(s->target), *sector_num, |
190 | *nb_sectors, &align_sector_num, | |
191 | &align_nb_sectors); | |
e5b43573 | 192 | } |
3515727f | 193 | |
e5b43573 FZ |
194 | if (align_nb_sectors > max_sectors) { |
195 | align_nb_sectors = max_sectors; | |
196 | if (need_cow) { | |
197 | align_nb_sectors = QEMU_ALIGN_DOWN(align_nb_sectors, | |
198 | s->target_cluster_sectors); | |
199 | } | |
8f0720ec | 200 | } |
4150ae60 FZ |
201 | /* Clipping may result in align_nb_sectors unaligned to chunk boundary, but |
202 | * that doesn't matter because it's already the end of source image. */ | |
203 | mirror_clip_sectors(s, align_sector_num, &align_nb_sectors); | |
8f0720ec | 204 | |
e5b43573 FZ |
205 | ret = align_sector_num + align_nb_sectors - (*sector_num + *nb_sectors); |
206 | *sector_num = align_sector_num; | |
207 | *nb_sectors = align_nb_sectors; | |
208 | assert(ret >= 0); | |
209 | return ret; | |
210 | } | |
211 | ||
21cd917f FZ |
212 | static inline void mirror_wait_for_io(MirrorBlockJob *s) |
213 | { | |
214 | assert(!s->waiting_for_io); | |
215 | s->waiting_for_io = true; | |
216 | qemu_coroutine_yield(); | |
217 | s->waiting_for_io = false; | |
218 | } | |
219 | ||
e5b43573 | 220 | /* Submit async read while handling COW. |
17612955 JS |
221 | * Returns: The number of sectors copied after and including sector_num, |
222 | * excluding any sectors copied prior to sector_num due to alignment. | |
223 | * This will be nb_sectors if no alignment is necessary, or | |
e5b43573 FZ |
224 | * (new_end - sector_num) if tail is rounded up or down due to |
225 | * alignment or buffer limit. | |
226 | */ | |
227 | static int mirror_do_read(MirrorBlockJob *s, int64_t sector_num, | |
228 | int nb_sectors) | |
229 | { | |
e253f4b8 | 230 | BlockBackend *source = s->common.blk; |
e5b43573 | 231 | int sectors_per_chunk, nb_chunks; |
17612955 | 232 | int ret; |
e5b43573 | 233 | MirrorOp *op; |
e4808881 | 234 | int max_sectors; |
e5b43573 | 235 | |
884fea4e | 236 | sectors_per_chunk = s->granularity >> BDRV_SECTOR_BITS; |
e4808881 | 237 | max_sectors = sectors_per_chunk * s->max_iov; |
402a4741 | 238 | |
e5b43573 FZ |
239 | /* We can only handle as much as buf_size at a time. */ |
240 | nb_sectors = MIN(s->buf_size >> BDRV_SECTOR_BITS, nb_sectors); | |
e4808881 | 241 | nb_sectors = MIN(max_sectors, nb_sectors); |
e5b43573 | 242 | assert(nb_sectors); |
17612955 | 243 | ret = nb_sectors; |
402a4741 | 244 | |
e5b43573 FZ |
245 | if (s->cow_bitmap) { |
246 | ret += mirror_cow_align(s, §or_num, &nb_sectors); | |
247 | } | |
248 | assert(nb_sectors << BDRV_SECTOR_BITS <= s->buf_size); | |
249 | /* The sector range must meet granularity because: | |
250 | * 1) Caller passes in aligned values; | |
251 | * 2) mirror_cow_align is used only when target cluster is larger. */ | |
e5b43573 | 252 | assert(!(sector_num % sectors_per_chunk)); |
4150ae60 | 253 | nb_chunks = DIV_ROUND_UP(nb_sectors, sectors_per_chunk); |
e5b43573 FZ |
254 | |
255 | while (s->buf_free_count < nb_chunks) { | |
402a4741 | 256 | trace_mirror_yield_in_flight(s, sector_num, s->in_flight); |
21cd917f | 257 | mirror_wait_for_io(s); |
b812f671 PB |
258 | } |
259 | ||
bd48bde8 | 260 | /* Allocate a MirrorOp that is used as an AIO callback. */ |
c84b3192 | 261 | op = g_new(MirrorOp, 1); |
bd48bde8 | 262 | op->s = s; |
bd48bde8 PB |
263 | op->sector_num = sector_num; |
264 | op->nb_sectors = nb_sectors; | |
402a4741 PB |
265 | |
266 | /* Now make a QEMUIOVector taking enough granularity-sized chunks | |
267 | * from s->buf_free. | |
268 | */ | |
269 | qemu_iovec_init(&op->qiov, nb_chunks); | |
402a4741 PB |
270 | while (nb_chunks-- > 0) { |
271 | MirrorBuffer *buf = QSIMPLEQ_FIRST(&s->buf_free); | |
e5b43573 | 272 | size_t remaining = nb_sectors * BDRV_SECTOR_SIZE - op->qiov.size; |
5a0f6fd5 | 273 | |
402a4741 PB |
274 | QSIMPLEQ_REMOVE_HEAD(&s->buf_free, next); |
275 | s->buf_free_count--; | |
5a0f6fd5 | 276 | qemu_iovec_add(&op->qiov, buf, MIN(s->granularity, remaining)); |
402a4741 | 277 | } |
bd48bde8 | 278 | |
893f7eba | 279 | /* Copy the dirty cluster. */ |
bd48bde8 | 280 | s->in_flight++; |
b21c7652 | 281 | s->sectors_in_flight += nb_sectors; |
b812f671 | 282 | trace_mirror_one_iteration(s, sector_num, nb_sectors); |
dcfb3beb | 283 | |
73698c30 | 284 | blk_aio_preadv(source, sector_num * BDRV_SECTOR_SIZE, &op->qiov, 0, |
e5b43573 FZ |
285 | mirror_read_complete, op); |
286 | return ret; | |
287 | } | |
288 | ||
289 | static void mirror_do_zero_or_discard(MirrorBlockJob *s, | |
290 | int64_t sector_num, | |
291 | int nb_sectors, | |
292 | bool is_discard) | |
293 | { | |
294 | MirrorOp *op; | |
295 | ||
296 | /* Allocate a MirrorOp that is used as an AIO callback. The qiov is zeroed | |
297 | * so the freeing in mirror_iteration_done is nop. */ | |
298 | op = g_new0(MirrorOp, 1); | |
299 | op->s = s; | |
300 | op->sector_num = sector_num; | |
301 | op->nb_sectors = nb_sectors; | |
302 | ||
303 | s->in_flight++; | |
304 | s->sectors_in_flight += nb_sectors; | |
305 | if (is_discard) { | |
e253f4b8 KW |
306 | blk_aio_discard(s->target, sector_num, op->nb_sectors, |
307 | mirror_write_complete, op); | |
e5b43573 | 308 | } else { |
e253f4b8 KW |
309 | blk_aio_pwrite_zeroes(s->target, sector_num * BDRV_SECTOR_SIZE, |
310 | op->nb_sectors * BDRV_SECTOR_SIZE, | |
dcfb3beb FZ |
311 | s->unmap ? BDRV_REQ_MAY_UNMAP : 0, |
312 | mirror_write_complete, op); | |
e5b43573 FZ |
313 | } |
314 | } | |
315 | ||
316 | static uint64_t coroutine_fn mirror_iteration(MirrorBlockJob *s) | |
317 | { | |
e253f4b8 | 318 | BlockDriverState *source = blk_bs(s->common.blk); |
9c83625b | 319 | int64_t sector_num, first_chunk; |
e5b43573 FZ |
320 | uint64_t delay_ns = 0; |
321 | /* At least the first dirty chunk is mirrored in one iteration. */ | |
322 | int nb_chunks = 1; | |
323 | int64_t end = s->bdev_length / BDRV_SECTOR_SIZE; | |
324 | int sectors_per_chunk = s->granularity >> BDRV_SECTOR_BITS; | |
325 | ||
326 | sector_num = hbitmap_iter_next(&s->hbi); | |
327 | if (sector_num < 0) { | |
328 | bdrv_dirty_iter_init(s->dirty_bitmap, &s->hbi); | |
329 | sector_num = hbitmap_iter_next(&s->hbi); | |
330 | trace_mirror_restart_iter(s, bdrv_get_dirty_count(s->dirty_bitmap)); | |
331 | assert(sector_num >= 0); | |
332 | } | |
333 | ||
9c83625b HR |
334 | first_chunk = sector_num / sectors_per_chunk; |
335 | while (test_bit(first_chunk, s->in_flight_bitmap)) { | |
ff04198b | 336 | trace_mirror_yield_in_flight(s, sector_num, s->in_flight); |
9c83625b HR |
337 | mirror_wait_for_io(s); |
338 | } | |
339 | ||
565ac01f SH |
340 | block_job_pause_point(&s->common); |
341 | ||
e5b43573 FZ |
342 | /* Find the number of consective dirty chunks following the first dirty |
343 | * one, and wait for in flight requests in them. */ | |
344 | while (nb_chunks * sectors_per_chunk < (s->buf_size >> BDRV_SECTOR_BITS)) { | |
345 | int64_t hbitmap_next; | |
346 | int64_t next_sector = sector_num + nb_chunks * sectors_per_chunk; | |
347 | int64_t next_chunk = next_sector / sectors_per_chunk; | |
348 | if (next_sector >= end || | |
349 | !bdrv_get_dirty(source, s->dirty_bitmap, next_sector)) { | |
350 | break; | |
351 | } | |
352 | if (test_bit(next_chunk, s->in_flight_bitmap)) { | |
9c83625b | 353 | break; |
e5b43573 | 354 | } |
9c83625b HR |
355 | |
356 | hbitmap_next = hbitmap_iter_next(&s->hbi); | |
f27a2742 HR |
357 | if (hbitmap_next > next_sector || hbitmap_next < 0) { |
358 | /* The bitmap iterator's cache is stale, refresh it */ | |
359 | bdrv_set_dirty_iter(&s->hbi, next_sector); | |
360 | hbitmap_next = hbitmap_iter_next(&s->hbi); | |
361 | } | |
9c83625b HR |
362 | assert(hbitmap_next == next_sector); |
363 | nb_chunks++; | |
e5b43573 FZ |
364 | } |
365 | ||
366 | /* Clear dirty bits before querying the block status, because | |
367 | * calling bdrv_get_block_status_above could yield - if some blocks are | |
368 | * marked dirty in this window, we need to know. | |
369 | */ | |
370 | bdrv_reset_dirty_bitmap(s->dirty_bitmap, sector_num, | |
371 | nb_chunks * sectors_per_chunk); | |
372 | bitmap_set(s->in_flight_bitmap, sector_num / sectors_per_chunk, nb_chunks); | |
373 | while (nb_chunks > 0 && sector_num < end) { | |
374 | int ret; | |
375 | int io_sectors; | |
376 | BlockDriverState *file; | |
377 | enum MirrorMethod { | |
378 | MIRROR_METHOD_COPY, | |
379 | MIRROR_METHOD_ZERO, | |
380 | MIRROR_METHOD_DISCARD | |
381 | } mirror_method = MIRROR_METHOD_COPY; | |
382 | ||
383 | assert(!(sector_num % sectors_per_chunk)); | |
384 | ret = bdrv_get_block_status_above(source, NULL, sector_num, | |
385 | nb_chunks * sectors_per_chunk, | |
386 | &io_sectors, &file); | |
387 | if (ret < 0) { | |
388 | io_sectors = nb_chunks * sectors_per_chunk; | |
389 | } | |
390 | ||
391 | io_sectors -= io_sectors % sectors_per_chunk; | |
392 | if (io_sectors < sectors_per_chunk) { | |
393 | io_sectors = sectors_per_chunk; | |
394 | } else if (ret >= 0 && !(ret & BDRV_BLOCK_DATA)) { | |
395 | int64_t target_sector_num; | |
396 | int target_nb_sectors; | |
244483e6 KW |
397 | bdrv_round_sectors_to_clusters(blk_bs(s->target), sector_num, |
398 | io_sectors, &target_sector_num, | |
399 | &target_nb_sectors); | |
e5b43573 FZ |
400 | if (target_sector_num == sector_num && |
401 | target_nb_sectors == io_sectors) { | |
402 | mirror_method = ret & BDRV_BLOCK_ZERO ? | |
403 | MIRROR_METHOD_ZERO : | |
404 | MIRROR_METHOD_DISCARD; | |
405 | } | |
406 | } | |
407 | ||
4150ae60 | 408 | mirror_clip_sectors(s, sector_num, &io_sectors); |
e5b43573 FZ |
409 | switch (mirror_method) { |
410 | case MIRROR_METHOD_COPY: | |
411 | io_sectors = mirror_do_read(s, sector_num, io_sectors); | |
412 | break; | |
413 | case MIRROR_METHOD_ZERO: | |
414 | mirror_do_zero_or_discard(s, sector_num, io_sectors, false); | |
415 | break; | |
416 | case MIRROR_METHOD_DISCARD: | |
417 | mirror_do_zero_or_discard(s, sector_num, io_sectors, true); | |
418 | break; | |
419 | default: | |
420 | abort(); | |
421 | } | |
422 | assert(io_sectors); | |
423 | sector_num += io_sectors; | |
4150ae60 | 424 | nb_chunks -= DIV_ROUND_UP(io_sectors, sectors_per_chunk); |
e5b43573 | 425 | delay_ns += ratelimit_calculate_delay(&s->limit, io_sectors); |
dcfb3beb | 426 | } |
cc8c9d6c | 427 | return delay_ns; |
bd48bde8 | 428 | } |
b952b558 | 429 | |
402a4741 PB |
430 | static void mirror_free_init(MirrorBlockJob *s) |
431 | { | |
432 | int granularity = s->granularity; | |
433 | size_t buf_size = s->buf_size; | |
434 | uint8_t *buf = s->buf; | |
435 | ||
436 | assert(s->buf_free_count == 0); | |
437 | QSIMPLEQ_INIT(&s->buf_free); | |
438 | while (buf_size != 0) { | |
439 | MirrorBuffer *cur = (MirrorBuffer *)buf; | |
440 | QSIMPLEQ_INSERT_TAIL(&s->buf_free, cur, next); | |
441 | s->buf_free_count++; | |
442 | buf_size -= granularity; | |
443 | buf += granularity; | |
444 | } | |
445 | } | |
446 | ||
bd48bde8 PB |
447 | static void mirror_drain(MirrorBlockJob *s) |
448 | { | |
449 | while (s->in_flight > 0) { | |
21cd917f | 450 | mirror_wait_for_io(s); |
bd48bde8 | 451 | } |
893f7eba PB |
452 | } |
453 | ||
5a7e7a0b SH |
454 | typedef struct { |
455 | int ret; | |
456 | } MirrorExitData; | |
457 | ||
458 | static void mirror_exit(BlockJob *job, void *opaque) | |
459 | { | |
460 | MirrorBlockJob *s = container_of(job, MirrorBlockJob, common); | |
461 | MirrorExitData *data = opaque; | |
462 | AioContext *replace_aio_context = NULL; | |
e253f4b8 KW |
463 | BlockDriverState *src = blk_bs(s->common.blk); |
464 | BlockDriverState *target_bs = blk_bs(s->target); | |
3f09bfbc KW |
465 | |
466 | /* Make sure that the source BDS doesn't go away before we called | |
467 | * block_job_completed(). */ | |
468 | bdrv_ref(src); | |
5a7e7a0b SH |
469 | |
470 | if (s->to_replace) { | |
471 | replace_aio_context = bdrv_get_aio_context(s->to_replace); | |
472 | aio_context_acquire(replace_aio_context); | |
473 | } | |
474 | ||
475 | if (s->should_complete && data->ret == 0) { | |
e253f4b8 | 476 | BlockDriverState *to_replace = src; |
5a7e7a0b SH |
477 | if (s->to_replace) { |
478 | to_replace = s->to_replace; | |
479 | } | |
40365552 | 480 | |
e253f4b8 KW |
481 | if (bdrv_get_flags(target_bs) != bdrv_get_flags(to_replace)) { |
482 | bdrv_reopen(target_bs, bdrv_get_flags(to_replace), NULL); | |
5a7e7a0b | 483 | } |
b8804815 KW |
484 | |
485 | /* The mirror job has no requests in flight any more, but we need to | |
486 | * drain potential other users of the BDS before changing the graph. */ | |
e253f4b8 KW |
487 | bdrv_drained_begin(target_bs); |
488 | bdrv_replace_in_backing_chain(to_replace, target_bs); | |
489 | bdrv_drained_end(target_bs); | |
b8804815 | 490 | |
b6d2e599 KW |
491 | /* We just changed the BDS the job BB refers to */ |
492 | blk_remove_bs(job->blk); | |
493 | blk_insert_bs(job->blk, src); | |
5a7e7a0b SH |
494 | } |
495 | if (s->to_replace) { | |
496 | bdrv_op_unblock_all(s->to_replace, s->replace_blocker); | |
497 | error_free(s->replace_blocker); | |
498 | bdrv_unref(s->to_replace); | |
499 | } | |
500 | if (replace_aio_context) { | |
501 | aio_context_release(replace_aio_context); | |
502 | } | |
503 | g_free(s->replaces); | |
e253f4b8 KW |
504 | bdrv_op_unblock_all(target_bs, s->common.blocker); |
505 | blk_unref(s->target); | |
5a7e7a0b SH |
506 | block_job_completed(&s->common, data->ret); |
507 | g_free(data); | |
176c3699 | 508 | bdrv_drained_end(src); |
ab27c3b5 FZ |
509 | if (qemu_get_aio_context() == bdrv_get_aio_context(src)) { |
510 | aio_enable_external(iohandler_get_aio_context()); | |
511 | } | |
3f09bfbc | 512 | bdrv_unref(src); |
5a7e7a0b SH |
513 | } |
514 | ||
893f7eba PB |
515 | static void coroutine_fn mirror_run(void *opaque) |
516 | { | |
517 | MirrorBlockJob *s = opaque; | |
5a7e7a0b | 518 | MirrorExitData *data; |
e253f4b8 KW |
519 | BlockDriverState *bs = blk_bs(s->common.blk); |
520 | BlockDriverState *target_bs = blk_bs(s->target); | |
99900697 | 521 | int64_t sector_num, end, length; |
bd48bde8 | 522 | uint64_t last_pause_ns; |
b812f671 | 523 | BlockDriverInfo bdi; |
1d33936e JC |
524 | char backing_filename[2]; /* we only need 2 characters because we are only |
525 | checking for a NULL string */ | |
893f7eba PB |
526 | int ret = 0; |
527 | int n; | |
e5b43573 | 528 | int target_cluster_size = BDRV_SECTOR_SIZE; |
893f7eba PB |
529 | |
530 | if (block_job_is_cancelled(&s->common)) { | |
531 | goto immediate_exit; | |
532 | } | |
533 | ||
b21c7652 HR |
534 | s->bdev_length = bdrv_getlength(bs); |
535 | if (s->bdev_length < 0) { | |
536 | ret = s->bdev_length; | |
373df5b1 | 537 | goto immediate_exit; |
b21c7652 | 538 | } else if (s->bdev_length == 0) { |
9e48b025 FZ |
539 | /* Report BLOCK_JOB_READY and wait for complete. */ |
540 | block_job_event_ready(&s->common); | |
541 | s->synced = true; | |
542 | while (!block_job_is_cancelled(&s->common) && !s->should_complete) { | |
543 | block_job_yield(&s->common); | |
544 | } | |
545 | s->common.cancelled = false; | |
546 | goto immediate_exit; | |
893f7eba PB |
547 | } |
548 | ||
b21c7652 | 549 | length = DIV_ROUND_UP(s->bdev_length, s->granularity); |
402a4741 PB |
550 | s->in_flight_bitmap = bitmap_new(length); |
551 | ||
b812f671 PB |
552 | /* If we have no backing file yet in the destination, we cannot let |
553 | * the destination do COW. Instead, we copy sectors around the | |
554 | * dirty data if needed. We need a bitmap to do that. | |
555 | */ | |
e253f4b8 | 556 | bdrv_get_backing_filename(target_bs, backing_filename, |
b812f671 | 557 | sizeof(backing_filename)); |
e253f4b8 | 558 | if (!bdrv_get_info(target_bs, &bdi) && bdi.cluster_size) { |
e5b43573 FZ |
559 | target_cluster_size = bdi.cluster_size; |
560 | } | |
e253f4b8 | 561 | if (backing_filename[0] && !target_bs->backing |
e5b43573 FZ |
562 | && s->granularity < target_cluster_size) { |
563 | s->buf_size = MAX(s->buf_size, target_cluster_size); | |
564 | s->cow_bitmap = bitmap_new(length); | |
b812f671 | 565 | } |
e5b43573 | 566 | s->target_cluster_sectors = target_cluster_size >> BDRV_SECTOR_BITS; |
e253f4b8 | 567 | s->max_iov = MIN(bs->bl.max_iov, target_bs->bl.max_iov); |
b812f671 | 568 | |
b21c7652 | 569 | end = s->bdev_length / BDRV_SECTOR_SIZE; |
7504edf4 KW |
570 | s->buf = qemu_try_blockalign(bs, s->buf_size); |
571 | if (s->buf == NULL) { | |
572 | ret = -ENOMEM; | |
573 | goto immediate_exit; | |
574 | } | |
575 | ||
402a4741 | 576 | mirror_free_init(s); |
893f7eba | 577 | |
4c0cbd6f | 578 | last_pause_ns = qemu_clock_get_ns(QEMU_CLOCK_REALTIME); |
03544a6e | 579 | if (!s->is_none_mode) { |
893f7eba | 580 | /* First part, loop on the sectors and initialize the dirty bitmap. */ |
5bc361b8 | 581 | BlockDriverState *base = s->base; |
e253f4b8 | 582 | bool mark_all_dirty = s->base == NULL && !bdrv_has_zero_init(target_bs); |
5279efeb | 583 | |
893f7eba | 584 | for (sector_num = 0; sector_num < end; ) { |
99900697 FZ |
585 | /* Just to make sure we are not exceeding int limit. */ |
586 | int nb_sectors = MIN(INT_MAX >> BDRV_SECTOR_BITS, | |
587 | end - sector_num); | |
4c0cbd6f FZ |
588 | int64_t now = qemu_clock_get_ns(QEMU_CLOCK_REALTIME); |
589 | ||
590 | if (now - last_pause_ns > SLICE_TIME) { | |
591 | last_pause_ns = now; | |
592 | block_job_sleep_ns(&s->common, QEMU_CLOCK_REALTIME, 0); | |
565ac01f SH |
593 | } else { |
594 | block_job_pause_point(&s->common); | |
4c0cbd6f FZ |
595 | } |
596 | ||
597 | if (block_job_is_cancelled(&s->common)) { | |
598 | goto immediate_exit; | |
599 | } | |
600 | ||
99900697 | 601 | ret = bdrv_is_allocated_above(bs, base, sector_num, nb_sectors, &n); |
893f7eba PB |
602 | |
603 | if (ret < 0) { | |
604 | goto immediate_exit; | |
605 | } | |
606 | ||
607 | assert(n > 0); | |
5279efeb | 608 | if (ret == 1 || mark_all_dirty) { |
20dca810 | 609 | bdrv_set_dirty_bitmap(s->dirty_bitmap, sector_num, n); |
893f7eba | 610 | } |
99900697 | 611 | sector_num += n; |
893f7eba PB |
612 | } |
613 | } | |
614 | ||
20dca810 | 615 | bdrv_dirty_iter_init(s->dirty_bitmap, &s->hbi); |
893f7eba | 616 | for (;;) { |
cc8c9d6c | 617 | uint64_t delay_ns = 0; |
893f7eba PB |
618 | int64_t cnt; |
619 | bool should_complete; | |
620 | ||
bd48bde8 PB |
621 | if (s->ret < 0) { |
622 | ret = s->ret; | |
623 | goto immediate_exit; | |
624 | } | |
625 | ||
565ac01f SH |
626 | block_job_pause_point(&s->common); |
627 | ||
20dca810 | 628 | cnt = bdrv_get_dirty_count(s->dirty_bitmap); |
b21c7652 HR |
629 | /* s->common.offset contains the number of bytes already processed so |
630 | * far, cnt is the number of dirty sectors remaining and | |
631 | * s->sectors_in_flight is the number of sectors currently being | |
632 | * processed; together those are the current total operation length */ | |
633 | s->common.len = s->common.offset + | |
634 | (cnt + s->sectors_in_flight) * BDRV_SECTOR_SIZE; | |
bd48bde8 PB |
635 | |
636 | /* Note that even when no rate limit is applied we need to yield | |
a7282330 | 637 | * periodically with no pending I/O so that bdrv_drain_all() returns. |
bd48bde8 PB |
638 | * We do so every SLICE_TIME nanoseconds, or when there is an error, |
639 | * or when the source is clean, whichever comes first. | |
640 | */ | |
bc72ad67 | 641 | if (qemu_clock_get_ns(QEMU_CLOCK_REALTIME) - last_pause_ns < SLICE_TIME && |
bd48bde8 | 642 | s->common.iostatus == BLOCK_DEVICE_IO_STATUS_OK) { |
402a4741 PB |
643 | if (s->in_flight == MAX_IN_FLIGHT || s->buf_free_count == 0 || |
644 | (cnt == 0 && s->in_flight > 0)) { | |
645 | trace_mirror_yield(s, s->in_flight, s->buf_free_count, cnt); | |
21cd917f | 646 | mirror_wait_for_io(s); |
bd48bde8 PB |
647 | continue; |
648 | } else if (cnt != 0) { | |
cc8c9d6c | 649 | delay_ns = mirror_iteration(s); |
893f7eba | 650 | } |
893f7eba PB |
651 | } |
652 | ||
653 | should_complete = false; | |
bd48bde8 | 654 | if (s->in_flight == 0 && cnt == 0) { |
893f7eba | 655 | trace_mirror_before_flush(s); |
e253f4b8 | 656 | ret = blk_flush(s->target); |
893f7eba | 657 | if (ret < 0) { |
a589569f WX |
658 | if (mirror_error_action(s, false, -ret) == |
659 | BLOCK_ERROR_ACTION_REPORT) { | |
b952b558 PB |
660 | goto immediate_exit; |
661 | } | |
662 | } else { | |
663 | /* We're out of the streaming phase. From now on, if the job | |
664 | * is cancelled we will actually complete all pending I/O and | |
665 | * report completion. This way, block-job-cancel will leave | |
666 | * the target in a consistent state. | |
667 | */ | |
b952b558 | 668 | if (!s->synced) { |
bcada37b | 669 | block_job_event_ready(&s->common); |
b952b558 PB |
670 | s->synced = true; |
671 | } | |
672 | ||
673 | should_complete = s->should_complete || | |
674 | block_job_is_cancelled(&s->common); | |
20dca810 | 675 | cnt = bdrv_get_dirty_count(s->dirty_bitmap); |
d63ffd87 | 676 | } |
893f7eba PB |
677 | } |
678 | ||
679 | if (cnt == 0 && should_complete) { | |
680 | /* The dirty bitmap is not updated while operations are pending. | |
681 | * If we're about to exit, wait for pending operations before | |
682 | * calling bdrv_get_dirty_count(bs), or we may exit while the | |
683 | * source has dirty data to copy! | |
684 | * | |
685 | * Note that I/O can be submitted by the guest while | |
686 | * mirror_populate runs. | |
687 | */ | |
688 | trace_mirror_before_drain(s, cnt); | |
39bf92dd | 689 | bdrv_co_drain(bs); |
20dca810 | 690 | cnt = bdrv_get_dirty_count(s->dirty_bitmap); |
893f7eba PB |
691 | } |
692 | ||
693 | ret = 0; | |
cc8c9d6c | 694 | trace_mirror_before_sleep(s, cnt, s->synced, delay_ns); |
d63ffd87 | 695 | if (!s->synced) { |
7483d1e5 | 696 | block_job_sleep_ns(&s->common, QEMU_CLOCK_REALTIME, delay_ns); |
893f7eba PB |
697 | if (block_job_is_cancelled(&s->common)) { |
698 | break; | |
699 | } | |
700 | } else if (!should_complete) { | |
bd48bde8 | 701 | delay_ns = (s->in_flight == 0 && cnt == 0 ? SLICE_TIME : 0); |
7483d1e5 | 702 | block_job_sleep_ns(&s->common, QEMU_CLOCK_REALTIME, delay_ns); |
893f7eba PB |
703 | } else if (cnt == 0) { |
704 | /* The two disks are in sync. Exit and report successful | |
705 | * completion. | |
706 | */ | |
707 | assert(QLIST_EMPTY(&bs->tracked_requests)); | |
708 | s->common.cancelled = false; | |
709 | break; | |
710 | } | |
bc72ad67 | 711 | last_pause_ns = qemu_clock_get_ns(QEMU_CLOCK_REALTIME); |
893f7eba PB |
712 | } |
713 | ||
714 | immediate_exit: | |
bd48bde8 PB |
715 | if (s->in_flight > 0) { |
716 | /* We get here only if something went wrong. Either the job failed, | |
717 | * or it was cancelled prematurely so that we do not guarantee that | |
718 | * the target is a copy of the source. | |
719 | */ | |
720 | assert(ret < 0 || (!s->synced && block_job_is_cancelled(&s->common))); | |
721 | mirror_drain(s); | |
722 | } | |
723 | ||
724 | assert(s->in_flight == 0); | |
7191bf31 | 725 | qemu_vfree(s->buf); |
b812f671 | 726 | g_free(s->cow_bitmap); |
402a4741 | 727 | g_free(s->in_flight_bitmap); |
e4654d2d | 728 | bdrv_release_dirty_bitmap(bs, s->dirty_bitmap); |
5a7e7a0b SH |
729 | |
730 | data = g_malloc(sizeof(*data)); | |
731 | data->ret = ret; | |
176c3699 FZ |
732 | /* Before we switch to target in mirror_exit, make sure data doesn't |
733 | * change. */ | |
e253f4b8 | 734 | bdrv_drained_begin(bs); |
ab27c3b5 FZ |
735 | if (qemu_get_aio_context() == bdrv_get_aio_context(bs)) { |
736 | /* FIXME: virtio host notifiers run on iohandler_ctx, therefore the | |
737 | * above bdrv_drained_end isn't enough to quiesce it. This is ugly, we | |
738 | * need a block layer API change to achieve this. */ | |
739 | aio_disable_external(iohandler_get_aio_context()); | |
740 | } | |
5a7e7a0b | 741 | block_job_defer_to_main_loop(&s->common, mirror_exit, data); |
893f7eba PB |
742 | } |
743 | ||
744 | static void mirror_set_speed(BlockJob *job, int64_t speed, Error **errp) | |
745 | { | |
746 | MirrorBlockJob *s = container_of(job, MirrorBlockJob, common); | |
747 | ||
748 | if (speed < 0) { | |
c6bd8c70 | 749 | error_setg(errp, QERR_INVALID_PARAMETER, "speed"); |
893f7eba PB |
750 | return; |
751 | } | |
752 | ratelimit_set_speed(&s->limit, speed / BDRV_SECTOR_SIZE, SLICE_TIME); | |
753 | } | |
754 | ||
d63ffd87 PB |
755 | static void mirror_complete(BlockJob *job, Error **errp) |
756 | { | |
757 | MirrorBlockJob *s = container_of(job, MirrorBlockJob, common); | |
274fccee HR |
758 | BlockDriverState *src, *target; |
759 | ||
760 | src = blk_bs(job->blk); | |
761 | target = blk_bs(s->target); | |
d63ffd87 | 762 | |
d63ffd87 | 763 | if (!s->synced) { |
9df229c3 AG |
764 | error_setg(errp, "The active block job '%s' cannot be completed", |
765 | job->id); | |
d63ffd87 PB |
766 | return; |
767 | } | |
768 | ||
274fccee HR |
769 | if (s->backing_mode == MIRROR_OPEN_BACKING_CHAIN) { |
770 | int ret; | |
771 | ||
772 | assert(!target->backing); | |
773 | ret = bdrv_open_backing_file(target, NULL, "backing", errp); | |
774 | if (ret < 0) { | |
775 | return; | |
776 | } | |
777 | } | |
778 | ||
15d67298 | 779 | /* block all operations on to_replace bs */ |
09158f00 | 780 | if (s->replaces) { |
5a7e7a0b SH |
781 | AioContext *replace_aio_context; |
782 | ||
e12f3784 | 783 | s->to_replace = bdrv_find_node(s->replaces); |
09158f00 | 784 | if (!s->to_replace) { |
e12f3784 | 785 | error_setg(errp, "Node name '%s' not found", s->replaces); |
09158f00 BC |
786 | return; |
787 | } | |
788 | ||
5a7e7a0b SH |
789 | replace_aio_context = bdrv_get_aio_context(s->to_replace); |
790 | aio_context_acquire(replace_aio_context); | |
791 | ||
09158f00 BC |
792 | error_setg(&s->replace_blocker, |
793 | "block device is in use by block-job-complete"); | |
794 | bdrv_op_block_all(s->to_replace, s->replace_blocker); | |
795 | bdrv_ref(s->to_replace); | |
5a7e7a0b SH |
796 | |
797 | aio_context_release(replace_aio_context); | |
09158f00 BC |
798 | } |
799 | ||
274fccee HR |
800 | if (s->backing_mode == MIRROR_SOURCE_BACKING_CHAIN) { |
801 | BlockDriverState *backing = s->is_none_mode ? src : s->base; | |
802 | if (backing_bs(target) != backing) { | |
803 | bdrv_set_backing_hd(target, backing); | |
804 | } | |
805 | } | |
806 | ||
d63ffd87 | 807 | s->should_complete = true; |
751ebd76 | 808 | block_job_enter(&s->common); |
d63ffd87 PB |
809 | } |
810 | ||
565ac01f SH |
811 | /* There is no matching mirror_resume() because mirror_run() will begin |
812 | * iterating again when the job is resumed. | |
813 | */ | |
814 | static void coroutine_fn mirror_pause(BlockJob *job) | |
815 | { | |
816 | MirrorBlockJob *s = container_of(job, MirrorBlockJob, common); | |
817 | ||
818 | mirror_drain(s); | |
819 | } | |
820 | ||
821 | static void mirror_attached_aio_context(BlockJob *job, AioContext *new_context) | |
822 | { | |
823 | MirrorBlockJob *s = container_of(job, MirrorBlockJob, common); | |
824 | ||
825 | blk_set_aio_context(s->target, new_context); | |
826 | } | |
827 | ||
3fc4b10a | 828 | static const BlockJobDriver mirror_job_driver = { |
565ac01f SH |
829 | .instance_size = sizeof(MirrorBlockJob), |
830 | .job_type = BLOCK_JOB_TYPE_MIRROR, | |
831 | .set_speed = mirror_set_speed, | |
832 | .complete = mirror_complete, | |
833 | .pause = mirror_pause, | |
834 | .attached_aio_context = mirror_attached_aio_context, | |
893f7eba PB |
835 | }; |
836 | ||
03544a6e | 837 | static const BlockJobDriver commit_active_job_driver = { |
565ac01f SH |
838 | .instance_size = sizeof(MirrorBlockJob), |
839 | .job_type = BLOCK_JOB_TYPE_COMMIT, | |
840 | .set_speed = mirror_set_speed, | |
841 | .complete = mirror_complete, | |
842 | .pause = mirror_pause, | |
843 | .attached_aio_context = mirror_attached_aio_context, | |
03544a6e FZ |
844 | }; |
845 | ||
846 | static void mirror_start_job(BlockDriverState *bs, BlockDriverState *target, | |
09158f00 | 847 | const char *replaces, |
5fba6c0e | 848 | int64_t speed, uint32_t granularity, |
09158f00 | 849 | int64_t buf_size, |
274fccee | 850 | BlockMirrorBackingMode backing_mode, |
09158f00 BC |
851 | BlockdevOnError on_source_error, |
852 | BlockdevOnError on_target_error, | |
0fc9f8ea | 853 | bool unmap, |
097310b5 | 854 | BlockCompletionFunc *cb, |
09158f00 BC |
855 | void *opaque, Error **errp, |
856 | const BlockJobDriver *driver, | |
857 | bool is_none_mode, BlockDriverState *base) | |
893f7eba PB |
858 | { |
859 | MirrorBlockJob *s; | |
860 | ||
eee13dfe | 861 | if (granularity == 0) { |
341ebc2f | 862 | granularity = bdrv_get_default_bitmap_granularity(target); |
eee13dfe PB |
863 | } |
864 | ||
865 | assert ((granularity & (granularity - 1)) == 0); | |
866 | ||
48ac0a4d WC |
867 | if (buf_size < 0) { |
868 | error_setg(errp, "Invalid parameter 'buf-size'"); | |
869 | return; | |
870 | } | |
871 | ||
872 | if (buf_size == 0) { | |
873 | buf_size = DEFAULT_MIRROR_BUF_SIZE; | |
874 | } | |
5bc361b8 | 875 | |
7f0317cf | 876 | s = block_job_create(NULL, driver, bs, speed, cb, opaque, errp); |
893f7eba PB |
877 | if (!s) { |
878 | return; | |
879 | } | |
880 | ||
e253f4b8 KW |
881 | s->target = blk_new(); |
882 | blk_insert_bs(s->target, target); | |
883 | ||
09158f00 | 884 | s->replaces = g_strdup(replaces); |
b952b558 PB |
885 | s->on_source_error = on_source_error; |
886 | s->on_target_error = on_target_error; | |
03544a6e | 887 | s->is_none_mode = is_none_mode; |
274fccee | 888 | s->backing_mode = backing_mode; |
5bc361b8 | 889 | s->base = base; |
eee13dfe | 890 | s->granularity = granularity; |
48ac0a4d | 891 | s->buf_size = ROUND_UP(buf_size, granularity); |
0fc9f8ea | 892 | s->unmap = unmap; |
b812f671 | 893 | |
0db6e54a | 894 | s->dirty_bitmap = bdrv_create_dirty_bitmap(bs, granularity, NULL, errp); |
b8afb520 | 895 | if (!s->dirty_bitmap) { |
97031164 | 896 | g_free(s->replaces); |
e253f4b8 | 897 | blk_unref(s->target); |
18930ba3 | 898 | block_job_unref(&s->common); |
b8afb520 FZ |
899 | return; |
900 | } | |
10f3cd15 | 901 | |
e253f4b8 | 902 | bdrv_op_block_all(target, s->common.blocker); |
10f3cd15 | 903 | |
893f7eba PB |
904 | s->common.co = qemu_coroutine_create(mirror_run); |
905 | trace_mirror_start(bs, s, s->common.co, opaque); | |
906 | qemu_coroutine_enter(s->common.co, s); | |
907 | } | |
03544a6e FZ |
908 | |
909 | void mirror_start(BlockDriverState *bs, BlockDriverState *target, | |
09158f00 | 910 | const char *replaces, |
5fba6c0e | 911 | int64_t speed, uint32_t granularity, int64_t buf_size, |
274fccee HR |
912 | MirrorSyncMode mode, BlockMirrorBackingMode backing_mode, |
913 | BlockdevOnError on_source_error, | |
03544a6e | 914 | BlockdevOnError on_target_error, |
0fc9f8ea | 915 | bool unmap, |
097310b5 | 916 | BlockCompletionFunc *cb, |
03544a6e FZ |
917 | void *opaque, Error **errp) |
918 | { | |
919 | bool is_none_mode; | |
920 | BlockDriverState *base; | |
921 | ||
4b80ab2b JS |
922 | if (mode == MIRROR_SYNC_MODE_INCREMENTAL) { |
923 | error_setg(errp, "Sync mode 'incremental' not supported"); | |
d58d8453 JS |
924 | return; |
925 | } | |
03544a6e | 926 | is_none_mode = mode == MIRROR_SYNC_MODE_NONE; |
760e0063 | 927 | base = mode == MIRROR_SYNC_MODE_TOP ? backing_bs(bs) : NULL; |
09158f00 | 928 | mirror_start_job(bs, target, replaces, |
274fccee | 929 | speed, granularity, buf_size, backing_mode, |
0fc9f8ea | 930 | on_source_error, on_target_error, unmap, cb, opaque, errp, |
03544a6e FZ |
931 | &mirror_job_driver, is_none_mode, base); |
932 | } | |
933 | ||
934 | void commit_active_start(BlockDriverState *bs, BlockDriverState *base, | |
935 | int64_t speed, | |
936 | BlockdevOnError on_error, | |
097310b5 | 937 | BlockCompletionFunc *cb, |
03544a6e FZ |
938 | void *opaque, Error **errp) |
939 | { | |
4da83585 JC |
940 | int64_t length, base_length; |
941 | int orig_base_flags; | |
39a611a3 | 942 | int ret; |
cc67f4d1 | 943 | Error *local_err = NULL; |
4da83585 JC |
944 | |
945 | orig_base_flags = bdrv_get_flags(base); | |
946 | ||
20a63d2c FZ |
947 | if (bdrv_reopen(base, bs->open_flags, errp)) { |
948 | return; | |
949 | } | |
4da83585 JC |
950 | |
951 | length = bdrv_getlength(bs); | |
952 | if (length < 0) { | |
39a611a3 JC |
953 | error_setg_errno(errp, -length, |
954 | "Unable to determine length of %s", bs->filename); | |
4da83585 JC |
955 | goto error_restore_flags; |
956 | } | |
957 | ||
958 | base_length = bdrv_getlength(base); | |
959 | if (base_length < 0) { | |
39a611a3 JC |
960 | error_setg_errno(errp, -base_length, |
961 | "Unable to determine length of %s", base->filename); | |
4da83585 JC |
962 | goto error_restore_flags; |
963 | } | |
964 | ||
965 | if (length > base_length) { | |
39a611a3 JC |
966 | ret = bdrv_truncate(base, length); |
967 | if (ret < 0) { | |
968 | error_setg_errno(errp, -ret, | |
969 | "Top image %s is larger than base image %s, and " | |
4da83585 JC |
970 | "resize of base image failed", |
971 | bs->filename, base->filename); | |
972 | goto error_restore_flags; | |
973 | } | |
974 | } | |
975 | ||
274fccee | 976 | mirror_start_job(bs, base, NULL, speed, 0, 0, MIRROR_LEAVE_BACKING_CHAIN, |
0fc9f8ea | 977 | on_error, on_error, false, cb, opaque, &local_err, |
03544a6e | 978 | &commit_active_job_driver, false, base); |
0fb6395c | 979 | if (local_err) { |
cc67f4d1 | 980 | error_propagate(errp, local_err); |
4da83585 JC |
981 | goto error_restore_flags; |
982 | } | |
983 | ||
984 | return; | |
985 | ||
986 | error_restore_flags: | |
987 | /* ignore error and errp for bdrv_reopen, because we want to propagate | |
988 | * the original error */ | |
989 | bdrv_reopen(base, orig_base_flags, NULL); | |
990 | return; | |
03544a6e | 991 | } |