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