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