]>
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" |
fd4a6493 | 15 | #include "qemu/cutils.h" |
12aa4082 | 16 | #include "qemu/coroutine.h" |
1181e19a | 17 | #include "qemu/range.h" |
893f7eba | 18 | #include "trace.h" |
c87621ea | 19 | #include "block/blockjob_int.h" |
737e150e | 20 | #include "block/block_int.h" |
373340b2 | 21 | #include "sysemu/block-backend.h" |
da34e65c | 22 | #include "qapi/error.h" |
cc7a8ea7 | 23 | #include "qapi/qmp/qerror.h" |
893f7eba | 24 | #include "qemu/ratelimit.h" |
b812f671 | 25 | #include "qemu/bitmap.h" |
893f7eba | 26 | |
402a4741 | 27 | #define MAX_IN_FLIGHT 16 |
b436982f EB |
28 | #define MAX_IO_BYTES (1 << 20) /* 1 Mb */ |
29 | #define DEFAULT_MIRROR_BUF_SIZE (MAX_IN_FLIGHT * MAX_IO_BYTES) | |
402a4741 PB |
30 | |
31 | /* The mirroring buffer is a list of granularity-sized chunks. | |
32 | * Free chunks are organized in a list. | |
33 | */ | |
34 | typedef struct MirrorBuffer { | |
35 | QSIMPLEQ_ENTRY(MirrorBuffer) next; | |
36 | } MirrorBuffer; | |
893f7eba | 37 | |
12aa4082 HR |
38 | typedef struct MirrorOp MirrorOp; |
39 | ||
893f7eba PB |
40 | typedef struct MirrorBlockJob { |
41 | BlockJob common; | |
e253f4b8 | 42 | BlockBackend *target; |
4ef85a9c | 43 | BlockDriverState *mirror_top_bs; |
5bc361b8 | 44 | BlockDriverState *base; |
4ef85a9c | 45 | |
09158f00 BC |
46 | /* The name of the graph node to replace */ |
47 | char *replaces; | |
48 | /* The BDS to replace */ | |
49 | BlockDriverState *to_replace; | |
50 | /* Used to block operations on the drive-mirror-replace target */ | |
51 | Error *replace_blocker; | |
03544a6e | 52 | bool is_none_mode; |
274fccee | 53 | BlockMirrorBackingMode backing_mode; |
cdf3bc93 HR |
54 | /* Whether the target image requires explicit zero-initialization */ |
55 | bool zero_target; | |
d06107ad | 56 | MirrorCopyMode copy_mode; |
b952b558 | 57 | BlockdevOnError on_source_error, on_target_error; |
d63ffd87 | 58 | bool synced; |
d06107ad HR |
59 | /* Set when the target is synced (dirty bitmap is clean, nothing |
60 | * in flight) and the job is running in active mode */ | |
61 | bool actively_synced; | |
d63ffd87 | 62 | bool should_complete; |
eee13dfe | 63 | int64_t granularity; |
b812f671 | 64 | size_t buf_size; |
b21c7652 | 65 | int64_t bdev_length; |
b812f671 | 66 | unsigned long *cow_bitmap; |
e4654d2d | 67 | BdrvDirtyBitmap *dirty_bitmap; |
dc162c8e | 68 | BdrvDirtyBitmapIter *dbi; |
893f7eba | 69 | uint8_t *buf; |
402a4741 PB |
70 | QSIMPLEQ_HEAD(, MirrorBuffer) buf_free; |
71 | int buf_free_count; | |
bd48bde8 | 72 | |
49efb1f5 | 73 | uint64_t last_pause_ns; |
402a4741 | 74 | unsigned long *in_flight_bitmap; |
bd48bde8 | 75 | int in_flight; |
b436982f | 76 | int64_t bytes_in_flight; |
b58deb34 | 77 | QTAILQ_HEAD(, MirrorOp) ops_in_flight; |
bd48bde8 | 78 | int ret; |
0fc9f8ea | 79 | bool unmap; |
b436982f | 80 | int target_cluster_size; |
e5b43573 | 81 | int max_iov; |
90ab48eb | 82 | bool initial_zeroing_ongoing; |
d06107ad | 83 | int in_active_write_counter; |
737efc1e | 84 | bool prepared; |
5e771752 | 85 | bool in_drain; |
893f7eba PB |
86 | } MirrorBlockJob; |
87 | ||
429076e8 HR |
88 | typedef struct MirrorBDSOpaque { |
89 | MirrorBlockJob *job; | |
f94dc3b4 | 90 | bool stop; |
429076e8 HR |
91 | } MirrorBDSOpaque; |
92 | ||
12aa4082 | 93 | struct MirrorOp { |
bd48bde8 PB |
94 | MirrorBlockJob *s; |
95 | QEMUIOVector qiov; | |
b436982f EB |
96 | int64_t offset; |
97 | uint64_t bytes; | |
2e1990b2 HR |
98 | |
99 | /* The pointee is set by mirror_co_read(), mirror_co_zero(), and | |
100 | * mirror_co_discard() before yielding for the first time */ | |
101 | int64_t *bytes_handled; | |
12aa4082 | 102 | |
1181e19a | 103 | bool is_pseudo_op; |
d06107ad | 104 | bool is_active_write; |
12aa4082 | 105 | CoQueue waiting_requests; |
eed325b9 | 106 | Coroutine *co; |
12aa4082 HR |
107 | |
108 | QTAILQ_ENTRY(MirrorOp) next; | |
109 | }; | |
bd48bde8 | 110 | |
4295c5fc HR |
111 | typedef enum MirrorMethod { |
112 | MIRROR_METHOD_COPY, | |
113 | MIRROR_METHOD_ZERO, | |
114 | MIRROR_METHOD_DISCARD, | |
115 | } MirrorMethod; | |
116 | ||
b952b558 PB |
117 | static BlockErrorAction mirror_error_action(MirrorBlockJob *s, bool read, |
118 | int error) | |
119 | { | |
120 | s->synced = false; | |
d06107ad | 121 | s->actively_synced = false; |
b952b558 | 122 | if (read) { |
81e254dc KW |
123 | return block_job_error_action(&s->common, s->on_source_error, |
124 | true, error); | |
b952b558 | 125 | } else { |
81e254dc KW |
126 | return block_job_error_action(&s->common, s->on_target_error, |
127 | false, error); | |
b952b558 PB |
128 | } |
129 | } | |
130 | ||
1181e19a HR |
131 | static void coroutine_fn mirror_wait_on_conflicts(MirrorOp *self, |
132 | MirrorBlockJob *s, | |
133 | uint64_t offset, | |
134 | uint64_t bytes) | |
135 | { | |
136 | uint64_t self_start_chunk = offset / s->granularity; | |
137 | uint64_t self_end_chunk = DIV_ROUND_UP(offset + bytes, s->granularity); | |
138 | uint64_t self_nb_chunks = self_end_chunk - self_start_chunk; | |
139 | ||
140 | while (find_next_bit(s->in_flight_bitmap, self_end_chunk, | |
141 | self_start_chunk) < self_end_chunk && | |
142 | s->ret >= 0) | |
143 | { | |
144 | MirrorOp *op; | |
145 | ||
146 | QTAILQ_FOREACH(op, &s->ops_in_flight, next) { | |
147 | uint64_t op_start_chunk = op->offset / s->granularity; | |
148 | uint64_t op_nb_chunks = DIV_ROUND_UP(op->offset + op->bytes, | |
149 | s->granularity) - | |
150 | op_start_chunk; | |
151 | ||
152 | if (op == self) { | |
153 | continue; | |
154 | } | |
155 | ||
156 | if (ranges_overlap(self_start_chunk, self_nb_chunks, | |
157 | op_start_chunk, op_nb_chunks)) | |
158 | { | |
159 | qemu_co_queue_wait(&op->waiting_requests, NULL); | |
160 | break; | |
161 | } | |
162 | } | |
163 | } | |
164 | } | |
165 | ||
2e1990b2 | 166 | static void coroutine_fn mirror_iteration_done(MirrorOp *op, int ret) |
bd48bde8 PB |
167 | { |
168 | MirrorBlockJob *s = op->s; | |
402a4741 | 169 | struct iovec *iov; |
bd48bde8 | 170 | int64_t chunk_num; |
b436982f | 171 | int i, nb_chunks; |
bd48bde8 | 172 | |
b436982f | 173 | trace_mirror_iteration_done(s, op->offset, op->bytes, ret); |
bd48bde8 PB |
174 | |
175 | s->in_flight--; | |
b436982f | 176 | s->bytes_in_flight -= op->bytes; |
402a4741 PB |
177 | iov = op->qiov.iov; |
178 | for (i = 0; i < op->qiov.niov; i++) { | |
179 | MirrorBuffer *buf = (MirrorBuffer *) iov[i].iov_base; | |
180 | QSIMPLEQ_INSERT_TAIL(&s->buf_free, buf, next); | |
181 | s->buf_free_count++; | |
182 | } | |
183 | ||
b436982f EB |
184 | chunk_num = op->offset / s->granularity; |
185 | nb_chunks = DIV_ROUND_UP(op->bytes, s->granularity); | |
12aa4082 | 186 | |
402a4741 | 187 | bitmap_clear(s->in_flight_bitmap, chunk_num, nb_chunks); |
12aa4082 | 188 | QTAILQ_REMOVE(&s->ops_in_flight, op, next); |
b21c7652 HR |
189 | if (ret >= 0) { |
190 | if (s->cow_bitmap) { | |
191 | bitmap_set(s->cow_bitmap, chunk_num, nb_chunks); | |
192 | } | |
90ab48eb | 193 | if (!s->initial_zeroing_ongoing) { |
30a5c887 | 194 | job_progress_update(&s->common.job, op->bytes); |
90ab48eb | 195 | } |
bd48bde8 | 196 | } |
6df3bf8e | 197 | qemu_iovec_destroy(&op->qiov); |
7b770c72 | 198 | |
12aa4082 HR |
199 | qemu_co_queue_restart_all(&op->waiting_requests); |
200 | g_free(op); | |
bd48bde8 PB |
201 | } |
202 | ||
2e1990b2 | 203 | static void coroutine_fn mirror_write_complete(MirrorOp *op, int ret) |
bd48bde8 | 204 | { |
bd48bde8 | 205 | MirrorBlockJob *s = op->s; |
b9e413dd | 206 | |
bd48bde8 | 207 | if (ret < 0) { |
bd48bde8 PB |
208 | BlockErrorAction action; |
209 | ||
e0d7f73e | 210 | bdrv_set_dirty_bitmap(s->dirty_bitmap, op->offset, op->bytes); |
bd48bde8 | 211 | action = mirror_error_action(s, false, -ret); |
a589569f | 212 | if (action == BLOCK_ERROR_ACTION_REPORT && s->ret >= 0) { |
bd48bde8 PB |
213 | s->ret = ret; |
214 | } | |
215 | } | |
d12ade57 | 216 | |
bd48bde8 PB |
217 | mirror_iteration_done(op, ret); |
218 | } | |
219 | ||
2e1990b2 | 220 | static void coroutine_fn mirror_read_complete(MirrorOp *op, int ret) |
bd48bde8 | 221 | { |
bd48bde8 | 222 | MirrorBlockJob *s = op->s; |
b9e413dd | 223 | |
bd48bde8 | 224 | if (ret < 0) { |
bd48bde8 PB |
225 | BlockErrorAction action; |
226 | ||
e0d7f73e | 227 | bdrv_set_dirty_bitmap(s->dirty_bitmap, op->offset, op->bytes); |
bd48bde8 | 228 | action = mirror_error_action(s, true, -ret); |
a589569f | 229 | if (action == BLOCK_ERROR_ACTION_REPORT && s->ret >= 0) { |
bd48bde8 PB |
230 | s->ret = ret; |
231 | } | |
232 | ||
233 | mirror_iteration_done(op, ret); | |
d12ade57 | 234 | return; |
bd48bde8 | 235 | } |
d12ade57 VSO |
236 | |
237 | ret = blk_co_pwritev(s->target, op->offset, op->qiov.size, &op->qiov, 0); | |
238 | mirror_write_complete(op, ret); | |
bd48bde8 PB |
239 | } |
240 | ||
782d97ef EB |
241 | /* Clip bytes relative to offset to not exceed end-of-file */ |
242 | static inline int64_t mirror_clip_bytes(MirrorBlockJob *s, | |
243 | int64_t offset, | |
244 | int64_t bytes) | |
245 | { | |
246 | return MIN(bytes, s->bdev_length - offset); | |
247 | } | |
248 | ||
782d97ef EB |
249 | /* Round offset and/or bytes to target cluster if COW is needed, and |
250 | * return the offset of the adjusted tail against original. */ | |
251 | static int mirror_cow_align(MirrorBlockJob *s, int64_t *offset, | |
ae4cc877 | 252 | uint64_t *bytes) |
893f7eba | 253 | { |
e5b43573 FZ |
254 | bool need_cow; |
255 | int ret = 0; | |
782d97ef | 256 | int64_t align_offset = *offset; |
7cfd5275 | 257 | int64_t align_bytes = *bytes; |
782d97ef | 258 | int max_bytes = s->granularity * s->max_iov; |
e5b43573 | 259 | |
782d97ef EB |
260 | need_cow = !test_bit(*offset / s->granularity, s->cow_bitmap); |
261 | need_cow |= !test_bit((*offset + *bytes - 1) / s->granularity, | |
e5b43573 FZ |
262 | s->cow_bitmap); |
263 | if (need_cow) { | |
782d97ef EB |
264 | bdrv_round_to_clusters(blk_bs(s->target), *offset, *bytes, |
265 | &align_offset, &align_bytes); | |
e5b43573 | 266 | } |
3515727f | 267 | |
782d97ef EB |
268 | if (align_bytes > max_bytes) { |
269 | align_bytes = max_bytes; | |
e5b43573 | 270 | if (need_cow) { |
782d97ef | 271 | align_bytes = QEMU_ALIGN_DOWN(align_bytes, s->target_cluster_size); |
e5b43573 | 272 | } |
8f0720ec | 273 | } |
782d97ef | 274 | /* Clipping may result in align_bytes unaligned to chunk boundary, but |
4150ae60 | 275 | * that doesn't matter because it's already the end of source image. */ |
782d97ef | 276 | align_bytes = mirror_clip_bytes(s, align_offset, align_bytes); |
8f0720ec | 277 | |
782d97ef EB |
278 | ret = align_offset + align_bytes - (*offset + *bytes); |
279 | *offset = align_offset; | |
280 | *bytes = align_bytes; | |
e5b43573 FZ |
281 | assert(ret >= 0); |
282 | return ret; | |
283 | } | |
284 | ||
537c3d4f | 285 | static inline void coroutine_fn |
7e6c4ff7 | 286 | mirror_wait_for_any_operation(MirrorBlockJob *s, MirrorOp *self, bool active) |
21cd917f | 287 | { |
12aa4082 HR |
288 | MirrorOp *op; |
289 | ||
1181e19a | 290 | QTAILQ_FOREACH(op, &s->ops_in_flight, next) { |
7e6c4ff7 KW |
291 | if (self == op) { |
292 | continue; | |
293 | } | |
1181e19a HR |
294 | /* Do not wait on pseudo ops, because it may in turn wait on |
295 | * some other operation to start, which may in fact be the | |
296 | * caller of this function. Since there is only one pseudo op | |
297 | * at any given time, we will always find some real operation | |
298 | * to wait on. */ | |
d06107ad | 299 | if (!op->is_pseudo_op && op->is_active_write == active) { |
1181e19a HR |
300 | qemu_co_queue_wait(&op->waiting_requests, NULL); |
301 | return; | |
302 | } | |
303 | } | |
304 | abort(); | |
21cd917f FZ |
305 | } |
306 | ||
537c3d4f | 307 | static inline void coroutine_fn |
7e6c4ff7 | 308 | mirror_wait_for_free_in_flight_slot(MirrorBlockJob *s, MirrorOp *self) |
d06107ad HR |
309 | { |
310 | /* Only non-active operations use up in-flight slots */ | |
7e6c4ff7 | 311 | mirror_wait_for_any_operation(s, self, false); |
d06107ad HR |
312 | } |
313 | ||
2e1990b2 HR |
314 | /* Perform a mirror copy operation. |
315 | * | |
316 | * *op->bytes_handled is set to the number of bytes copied after and | |
317 | * including offset, excluding any bytes copied prior to offset due | |
318 | * to alignment. This will be op->bytes if no alignment is necessary, | |
319 | * or (new_end - op->offset) if the tail is rounded up or down due to | |
320 | * alignment or buffer limit. | |
e5b43573 | 321 | */ |
2e1990b2 | 322 | static void coroutine_fn mirror_co_read(void *opaque) |
e5b43573 | 323 | { |
2e1990b2 HR |
324 | MirrorOp *op = opaque; |
325 | MirrorBlockJob *s = op->s; | |
ae4cc877 EB |
326 | int nb_chunks; |
327 | uint64_t ret; | |
ae4cc877 | 328 | uint64_t max_bytes; |
e5b43573 | 329 | |
ae4cc877 | 330 | max_bytes = s->granularity * s->max_iov; |
402a4741 | 331 | |
e5b43573 | 332 | /* We can only handle as much as buf_size at a time. */ |
2e1990b2 HR |
333 | op->bytes = MIN(s->buf_size, MIN(max_bytes, op->bytes)); |
334 | assert(op->bytes); | |
335 | assert(op->bytes < BDRV_REQUEST_MAX_BYTES); | |
336 | *op->bytes_handled = op->bytes; | |
402a4741 | 337 | |
e5b43573 | 338 | if (s->cow_bitmap) { |
2e1990b2 | 339 | *op->bytes_handled += mirror_cow_align(s, &op->offset, &op->bytes); |
e5b43573 | 340 | } |
2e1990b2 HR |
341 | /* Cannot exceed BDRV_REQUEST_MAX_BYTES + INT_MAX */ |
342 | assert(*op->bytes_handled <= UINT_MAX); | |
343 | assert(op->bytes <= s->buf_size); | |
ae4cc877 | 344 | /* The offset is granularity-aligned because: |
e5b43573 FZ |
345 | * 1) Caller passes in aligned values; |
346 | * 2) mirror_cow_align is used only when target cluster is larger. */ | |
2e1990b2 | 347 | assert(QEMU_IS_ALIGNED(op->offset, s->granularity)); |
ae4cc877 | 348 | /* The range is sector-aligned, since bdrv_getlength() rounds up. */ |
2e1990b2 HR |
349 | assert(QEMU_IS_ALIGNED(op->bytes, BDRV_SECTOR_SIZE)); |
350 | nb_chunks = DIV_ROUND_UP(op->bytes, s->granularity); | |
e5b43573 FZ |
351 | |
352 | while (s->buf_free_count < nb_chunks) { | |
2e1990b2 | 353 | trace_mirror_yield_in_flight(s, op->offset, s->in_flight); |
7e6c4ff7 | 354 | mirror_wait_for_free_in_flight_slot(s, op); |
b812f671 PB |
355 | } |
356 | ||
402a4741 PB |
357 | /* Now make a QEMUIOVector taking enough granularity-sized chunks |
358 | * from s->buf_free. | |
359 | */ | |
360 | qemu_iovec_init(&op->qiov, nb_chunks); | |
402a4741 PB |
361 | while (nb_chunks-- > 0) { |
362 | MirrorBuffer *buf = QSIMPLEQ_FIRST(&s->buf_free); | |
2e1990b2 | 363 | size_t remaining = op->bytes - op->qiov.size; |
5a0f6fd5 | 364 | |
402a4741 PB |
365 | QSIMPLEQ_REMOVE_HEAD(&s->buf_free, next); |
366 | s->buf_free_count--; | |
5a0f6fd5 | 367 | qemu_iovec_add(&op->qiov, buf, MIN(s->granularity, remaining)); |
402a4741 | 368 | } |
bd48bde8 | 369 | |
893f7eba | 370 | /* Copy the dirty cluster. */ |
bd48bde8 | 371 | s->in_flight++; |
2e1990b2 HR |
372 | s->bytes_in_flight += op->bytes; |
373 | trace_mirror_one_iteration(s, op->offset, op->bytes); | |
dcfb3beb | 374 | |
138f9fff HR |
375 | ret = bdrv_co_preadv(s->mirror_top_bs->backing, op->offset, op->bytes, |
376 | &op->qiov, 0); | |
2e1990b2 | 377 | mirror_read_complete(op, ret); |
e5b43573 FZ |
378 | } |
379 | ||
2e1990b2 | 380 | static void coroutine_fn mirror_co_zero(void *opaque) |
e5b43573 | 381 | { |
2e1990b2 HR |
382 | MirrorOp *op = opaque; |
383 | int ret; | |
e5b43573 | 384 | |
2e1990b2 HR |
385 | op->s->in_flight++; |
386 | op->s->bytes_in_flight += op->bytes; | |
387 | *op->bytes_handled = op->bytes; | |
e5b43573 | 388 | |
2e1990b2 HR |
389 | ret = blk_co_pwrite_zeroes(op->s->target, op->offset, op->bytes, |
390 | op->s->unmap ? BDRV_REQ_MAY_UNMAP : 0); | |
391 | mirror_write_complete(op, ret); | |
392 | } | |
393 | ||
394 | static void coroutine_fn mirror_co_discard(void *opaque) | |
395 | { | |
396 | MirrorOp *op = opaque; | |
397 | int ret; | |
398 | ||
399 | op->s->in_flight++; | |
400 | op->s->bytes_in_flight += op->bytes; | |
401 | *op->bytes_handled = op->bytes; | |
402 | ||
403 | ret = blk_co_pdiscard(op->s->target, op->offset, op->bytes); | |
404 | mirror_write_complete(op, ret); | |
e5b43573 FZ |
405 | } |
406 | ||
4295c5fc HR |
407 | static unsigned mirror_perform(MirrorBlockJob *s, int64_t offset, |
408 | unsigned bytes, MirrorMethod mirror_method) | |
409 | { | |
2e1990b2 HR |
410 | MirrorOp *op; |
411 | Coroutine *co; | |
412 | int64_t bytes_handled = -1; | |
413 | ||
414 | op = g_new(MirrorOp, 1); | |
415 | *op = (MirrorOp){ | |
416 | .s = s, | |
417 | .offset = offset, | |
418 | .bytes = bytes, | |
419 | .bytes_handled = &bytes_handled, | |
420 | }; | |
12aa4082 | 421 | qemu_co_queue_init(&op->waiting_requests); |
2e1990b2 | 422 | |
4295c5fc HR |
423 | switch (mirror_method) { |
424 | case MIRROR_METHOD_COPY: | |
2e1990b2 HR |
425 | co = qemu_coroutine_create(mirror_co_read, op); |
426 | break; | |
4295c5fc | 427 | case MIRROR_METHOD_ZERO: |
2e1990b2 HR |
428 | co = qemu_coroutine_create(mirror_co_zero, op); |
429 | break; | |
4295c5fc | 430 | case MIRROR_METHOD_DISCARD: |
2e1990b2 HR |
431 | co = qemu_coroutine_create(mirror_co_discard, op); |
432 | break; | |
4295c5fc HR |
433 | default: |
434 | abort(); | |
435 | } | |
eed325b9 | 436 | op->co = co; |
2e1990b2 | 437 | |
12aa4082 | 438 | QTAILQ_INSERT_TAIL(&s->ops_in_flight, op, next); |
2e1990b2 HR |
439 | qemu_coroutine_enter(co); |
440 | /* At this point, ownership of op has been moved to the coroutine | |
441 | * and the object may already be freed */ | |
442 | ||
443 | /* Assert that this value has been set */ | |
444 | assert(bytes_handled >= 0); | |
445 | ||
446 | /* Same assertion as in mirror_co_read() (and for mirror_co_read() | |
447 | * and mirror_co_discard(), bytes_handled == op->bytes, which | |
448 | * is the @bytes parameter given to this function) */ | |
449 | assert(bytes_handled <= UINT_MAX); | |
450 | return bytes_handled; | |
4295c5fc HR |
451 | } |
452 | ||
e5b43573 FZ |
453 | static uint64_t coroutine_fn mirror_iteration(MirrorBlockJob *s) |
454 | { | |
138f9fff | 455 | BlockDriverState *source = s->mirror_top_bs->backing->bs; |
1181e19a HR |
456 | MirrorOp *pseudo_op; |
457 | int64_t offset; | |
458 | uint64_t delay_ns = 0, ret = 0; | |
e5b43573 FZ |
459 | /* At least the first dirty chunk is mirrored in one iteration. */ |
460 | int nb_chunks = 1; | |
4b5004d9 | 461 | bool write_zeroes_ok = bdrv_can_write_zeroes_with_unmap(blk_bs(s->target)); |
b436982f | 462 | int max_io_bytes = MAX(s->buf_size / MAX_IN_FLIGHT, MAX_IO_BYTES); |
e5b43573 | 463 | |
b64bd51e | 464 | bdrv_dirty_bitmap_lock(s->dirty_bitmap); |
f798184c | 465 | offset = bdrv_dirty_iter_next(s->dbi); |
fb2ef791 | 466 | if (offset < 0) { |
dc162c8e | 467 | bdrv_set_dirty_iter(s->dbi, 0); |
f798184c | 468 | offset = bdrv_dirty_iter_next(s->dbi); |
9a46dba7 | 469 | trace_mirror_restart_iter(s, bdrv_get_dirty_count(s->dirty_bitmap)); |
fb2ef791 | 470 | assert(offset >= 0); |
e5b43573 | 471 | } |
b64bd51e | 472 | bdrv_dirty_bitmap_unlock(s->dirty_bitmap); |
e5b43573 | 473 | |
1181e19a | 474 | mirror_wait_on_conflicts(NULL, s, offset, 1); |
9c83625b | 475 | |
da01ff7f | 476 | job_pause_point(&s->common.job); |
565ac01f | 477 | |
e5b43573 FZ |
478 | /* Find the number of consective dirty chunks following the first dirty |
479 | * one, and wait for in flight requests in them. */ | |
b64bd51e | 480 | bdrv_dirty_bitmap_lock(s->dirty_bitmap); |
fb2ef791 | 481 | while (nb_chunks * s->granularity < s->buf_size) { |
dc162c8e | 482 | int64_t next_dirty; |
fb2ef791 EB |
483 | int64_t next_offset = offset + nb_chunks * s->granularity; |
484 | int64_t next_chunk = next_offset / s->granularity; | |
485 | if (next_offset >= s->bdev_length || | |
28636b82 | 486 | !bdrv_dirty_bitmap_get_locked(s->dirty_bitmap, next_offset)) { |
e5b43573 FZ |
487 | break; |
488 | } | |
489 | if (test_bit(next_chunk, s->in_flight_bitmap)) { | |
9c83625b | 490 | break; |
e5b43573 | 491 | } |
9c83625b | 492 | |
f798184c | 493 | next_dirty = bdrv_dirty_iter_next(s->dbi); |
fb2ef791 | 494 | if (next_dirty > next_offset || next_dirty < 0) { |
f27a2742 | 495 | /* The bitmap iterator's cache is stale, refresh it */ |
715a74d8 | 496 | bdrv_set_dirty_iter(s->dbi, next_offset); |
f798184c | 497 | next_dirty = bdrv_dirty_iter_next(s->dbi); |
f27a2742 | 498 | } |
fb2ef791 | 499 | assert(next_dirty == next_offset); |
9c83625b | 500 | nb_chunks++; |
e5b43573 FZ |
501 | } |
502 | ||
503 | /* Clear dirty bits before querying the block status, because | |
31826642 | 504 | * calling bdrv_block_status_above could yield - if some blocks are |
e5b43573 FZ |
505 | * marked dirty in this window, we need to know. |
506 | */ | |
e0d7f73e EB |
507 | bdrv_reset_dirty_bitmap_locked(s->dirty_bitmap, offset, |
508 | nb_chunks * s->granularity); | |
b64bd51e PB |
509 | bdrv_dirty_bitmap_unlock(s->dirty_bitmap); |
510 | ||
1181e19a HR |
511 | /* Before claiming an area in the in-flight bitmap, we have to |
512 | * create a MirrorOp for it so that conflicting requests can wait | |
513 | * for it. mirror_perform() will create the real MirrorOps later, | |
514 | * for now we just create a pseudo operation that will wake up all | |
515 | * conflicting requests once all real operations have been | |
516 | * launched. */ | |
517 | pseudo_op = g_new(MirrorOp, 1); | |
518 | *pseudo_op = (MirrorOp){ | |
519 | .offset = offset, | |
520 | .bytes = nb_chunks * s->granularity, | |
521 | .is_pseudo_op = true, | |
522 | }; | |
523 | qemu_co_queue_init(&pseudo_op->waiting_requests); | |
524 | QTAILQ_INSERT_TAIL(&s->ops_in_flight, pseudo_op, next); | |
525 | ||
fb2ef791 EB |
526 | bitmap_set(s->in_flight_bitmap, offset / s->granularity, nb_chunks); |
527 | while (nb_chunks > 0 && offset < s->bdev_length) { | |
31826642 | 528 | int ret; |
7cfd5275 | 529 | int64_t io_bytes; |
f3e4ce4a | 530 | int64_t io_bytes_acct; |
4295c5fc | 531 | MirrorMethod mirror_method = MIRROR_METHOD_COPY; |
e5b43573 | 532 | |
fb2ef791 | 533 | assert(!(offset % s->granularity)); |
31826642 EB |
534 | ret = bdrv_block_status_above(source, NULL, offset, |
535 | nb_chunks * s->granularity, | |
536 | &io_bytes, NULL, NULL); | |
e5b43573 | 537 | if (ret < 0) { |
fb2ef791 | 538 | io_bytes = MIN(nb_chunks * s->granularity, max_io_bytes); |
0965a41e | 539 | } else if (ret & BDRV_BLOCK_DATA) { |
fb2ef791 | 540 | io_bytes = MIN(io_bytes, max_io_bytes); |
e5b43573 FZ |
541 | } |
542 | ||
fb2ef791 EB |
543 | io_bytes -= io_bytes % s->granularity; |
544 | if (io_bytes < s->granularity) { | |
545 | io_bytes = s->granularity; | |
e5b43573 | 546 | } else if (ret >= 0 && !(ret & BDRV_BLOCK_DATA)) { |
fb2ef791 | 547 | int64_t target_offset; |
7cfd5275 | 548 | int64_t target_bytes; |
fb2ef791 EB |
549 | bdrv_round_to_clusters(blk_bs(s->target), offset, io_bytes, |
550 | &target_offset, &target_bytes); | |
551 | if (target_offset == offset && | |
552 | target_bytes == io_bytes) { | |
e5b43573 FZ |
553 | mirror_method = ret & BDRV_BLOCK_ZERO ? |
554 | MIRROR_METHOD_ZERO : | |
555 | MIRROR_METHOD_DISCARD; | |
556 | } | |
557 | } | |
558 | ||
cf56a3c6 | 559 | while (s->in_flight >= MAX_IN_FLIGHT) { |
fb2ef791 | 560 | trace_mirror_yield_in_flight(s, offset, s->in_flight); |
7e6c4ff7 | 561 | mirror_wait_for_free_in_flight_slot(s, pseudo_op); |
cf56a3c6 DL |
562 | } |
563 | ||
dbaa7b57 | 564 | if (s->ret < 0) { |
1181e19a HR |
565 | ret = 0; |
566 | goto fail; | |
dbaa7b57 VSO |
567 | } |
568 | ||
fb2ef791 | 569 | io_bytes = mirror_clip_bytes(s, offset, io_bytes); |
4295c5fc HR |
570 | io_bytes = mirror_perform(s, offset, io_bytes, mirror_method); |
571 | if (mirror_method != MIRROR_METHOD_COPY && write_zeroes_ok) { | |
572 | io_bytes_acct = 0; | |
573 | } else { | |
574 | io_bytes_acct = io_bytes; | |
e5b43573 | 575 | } |
fb2ef791 EB |
576 | assert(io_bytes); |
577 | offset += io_bytes; | |
578 | nb_chunks -= DIV_ROUND_UP(io_bytes, s->granularity); | |
dee81d51 | 579 | delay_ns = block_job_ratelimit_get_delay(&s->common, io_bytes_acct); |
dcfb3beb | 580 | } |
1181e19a HR |
581 | |
582 | ret = delay_ns; | |
583 | fail: | |
584 | QTAILQ_REMOVE(&s->ops_in_flight, pseudo_op, next); | |
585 | qemu_co_queue_restart_all(&pseudo_op->waiting_requests); | |
586 | g_free(pseudo_op); | |
587 | ||
588 | return ret; | |
bd48bde8 | 589 | } |
b952b558 | 590 | |
402a4741 PB |
591 | static void mirror_free_init(MirrorBlockJob *s) |
592 | { | |
593 | int granularity = s->granularity; | |
594 | size_t buf_size = s->buf_size; | |
595 | uint8_t *buf = s->buf; | |
596 | ||
597 | assert(s->buf_free_count == 0); | |
598 | QSIMPLEQ_INIT(&s->buf_free); | |
599 | while (buf_size != 0) { | |
600 | MirrorBuffer *cur = (MirrorBuffer *)buf; | |
601 | QSIMPLEQ_INSERT_TAIL(&s->buf_free, cur, next); | |
602 | s->buf_free_count++; | |
603 | buf_size -= granularity; | |
604 | buf += granularity; | |
605 | } | |
606 | } | |
607 | ||
bae8196d PB |
608 | /* This is also used for the .pause callback. There is no matching |
609 | * mirror_resume() because mirror_run() will begin iterating again | |
610 | * when the job is resumed. | |
611 | */ | |
537c3d4f | 612 | static void coroutine_fn mirror_wait_for_all_io(MirrorBlockJob *s) |
bd48bde8 PB |
613 | { |
614 | while (s->in_flight > 0) { | |
7e6c4ff7 | 615 | mirror_wait_for_free_in_flight_slot(s, NULL); |
bd48bde8 | 616 | } |
893f7eba PB |
617 | } |
618 | ||
737efc1e JS |
619 | /** |
620 | * mirror_exit_common: handle both abort() and prepare() cases. | |
621 | * for .prepare, returns 0 on success and -errno on failure. | |
622 | * for .abort cases, denoted by abort = true, MUST return 0. | |
623 | */ | |
624 | static int mirror_exit_common(Job *job) | |
5a7e7a0b | 625 | { |
1908a559 KW |
626 | MirrorBlockJob *s = container_of(job, MirrorBlockJob, common.job); |
627 | BlockJob *bjob = &s->common; | |
f93c3add | 628 | MirrorBDSOpaque *bs_opaque; |
5a7e7a0b | 629 | AioContext *replace_aio_context = NULL; |
f93c3add HR |
630 | BlockDriverState *src; |
631 | BlockDriverState *target_bs; | |
632 | BlockDriverState *mirror_top_bs; | |
12fa4af6 | 633 | Error *local_err = NULL; |
737efc1e JS |
634 | bool abort = job->ret < 0; |
635 | int ret = 0; | |
636 | ||
637 | if (s->prepared) { | |
638 | return 0; | |
639 | } | |
640 | s->prepared = true; | |
3f09bfbc | 641 | |
f93c3add HR |
642 | mirror_top_bs = s->mirror_top_bs; |
643 | bs_opaque = mirror_top_bs->opaque; | |
644 | src = mirror_top_bs->backing->bs; | |
645 | target_bs = blk_bs(s->target); | |
646 | ||
ef53dc09 AG |
647 | if (bdrv_chain_contains(src, target_bs)) { |
648 | bdrv_unfreeze_backing_chain(mirror_top_bs, target_bs); | |
649 | } | |
650 | ||
5deb6cbd | 651 | bdrv_release_dirty_bitmap(s->dirty_bitmap); |
2119882c | 652 | |
7b508f6b JS |
653 | /* Make sure that the source BDS doesn't go away during bdrv_replace_node, |
654 | * before we can call bdrv_drained_end */ | |
3f09bfbc | 655 | bdrv_ref(src); |
4ef85a9c | 656 | bdrv_ref(mirror_top_bs); |
7d9fcb39 KW |
657 | bdrv_ref(target_bs); |
658 | ||
bb0c9409 VSO |
659 | /* |
660 | * Remove target parent that still uses BLK_PERM_WRITE/RESIZE before | |
7d9fcb39 | 661 | * inserting target_bs at s->to_replace, where we might not be able to get |
63c8ef28 | 662 | * these permissions. |
bb0c9409 | 663 | */ |
7d9fcb39 KW |
664 | blk_unref(s->target); |
665 | s->target = NULL; | |
4ef85a9c KW |
666 | |
667 | /* We don't access the source any more. Dropping any WRITE/RESIZE is | |
d2da5e28 KW |
668 | * required before it could become a backing file of target_bs. Not having |
669 | * these permissions any more means that we can't allow any new requests on | |
670 | * mirror_top_bs from now on, so keep it drained. */ | |
671 | bdrv_drained_begin(mirror_top_bs); | |
f94dc3b4 HR |
672 | bs_opaque->stop = true; |
673 | bdrv_child_refresh_perms(mirror_top_bs, mirror_top_bs->backing, | |
674 | &error_abort); | |
737efc1e | 675 | if (!abort && s->backing_mode == MIRROR_SOURCE_BACKING_CHAIN) { |
4ef85a9c KW |
676 | BlockDriverState *backing = s->is_none_mode ? src : s->base; |
677 | if (backing_bs(target_bs) != backing) { | |
12fa4af6 KW |
678 | bdrv_set_backing_hd(target_bs, backing, &local_err); |
679 | if (local_err) { | |
680 | error_report_err(local_err); | |
66c8672d | 681 | local_err = NULL; |
7b508f6b | 682 | ret = -EPERM; |
12fa4af6 | 683 | } |
4ef85a9c KW |
684 | } |
685 | } | |
5a7e7a0b SH |
686 | |
687 | if (s->to_replace) { | |
688 | replace_aio_context = bdrv_get_aio_context(s->to_replace); | |
689 | aio_context_acquire(replace_aio_context); | |
690 | } | |
691 | ||
737efc1e JS |
692 | if (s->should_complete && !abort) { |
693 | BlockDriverState *to_replace = s->to_replace ?: src; | |
1ba79388 | 694 | bool ro = bdrv_is_read_only(to_replace); |
40365552 | 695 | |
1ba79388 AG |
696 | if (ro != bdrv_is_read_only(target_bs)) { |
697 | bdrv_reopen_set_read_only(target_bs, ro, NULL); | |
5a7e7a0b | 698 | } |
b8804815 KW |
699 | |
700 | /* The mirror job has no requests in flight any more, but we need to | |
701 | * drain potential other users of the BDS before changing the graph. */ | |
5e771752 | 702 | assert(s->in_drain); |
e253f4b8 | 703 | bdrv_drained_begin(target_bs); |
6e9cc051 HR |
704 | /* |
705 | * Cannot use check_to_replace_node() here, because that would | |
706 | * check for an op blocker on @to_replace, and we have our own | |
707 | * there. | |
708 | */ | |
709 | if (bdrv_recurse_can_replace(src, to_replace)) { | |
710 | bdrv_replace_node(to_replace, target_bs, &local_err); | |
711 | } else { | |
712 | error_setg(&local_err, "Can no longer replace '%s' by '%s', " | |
713 | "because it can no longer be guaranteed that doing so " | |
714 | "would not lead to an abrupt change of visible data", | |
715 | to_replace->node_name, target_bs->node_name); | |
716 | } | |
e253f4b8 | 717 | bdrv_drained_end(target_bs); |
5fe31c25 KW |
718 | if (local_err) { |
719 | error_report_err(local_err); | |
7b508f6b | 720 | ret = -EPERM; |
5fe31c25 | 721 | } |
5a7e7a0b SH |
722 | } |
723 | if (s->to_replace) { | |
724 | bdrv_op_unblock_all(s->to_replace, s->replace_blocker); | |
725 | error_free(s->replace_blocker); | |
726 | bdrv_unref(s->to_replace); | |
727 | } | |
728 | if (replace_aio_context) { | |
729 | aio_context_release(replace_aio_context); | |
730 | } | |
731 | g_free(s->replaces); | |
7d9fcb39 | 732 | bdrv_unref(target_bs); |
4ef85a9c | 733 | |
f94dc3b4 HR |
734 | /* |
735 | * Remove the mirror filter driver from the graph. Before this, get rid of | |
4ef85a9c | 736 | * the blockers on the intermediate nodes so that the resulting state is |
f94dc3b4 HR |
737 | * valid. |
738 | */ | |
1908a559 | 739 | block_job_remove_all_bdrv(bjob); |
5fe31c25 | 740 | bdrv_replace_node(mirror_top_bs, backing_bs(mirror_top_bs), &error_abort); |
4ef85a9c KW |
741 | |
742 | /* We just changed the BDS the job BB refers to (with either or both of the | |
5fe31c25 KW |
743 | * bdrv_replace_node() calls), so switch the BB back so the cleanup does |
744 | * the right thing. We don't need any permissions any more now. */ | |
1908a559 KW |
745 | blk_remove_bs(bjob->blk); |
746 | blk_set_perm(bjob->blk, 0, BLK_PERM_ALL, &error_abort); | |
747 | blk_insert_bs(bjob->blk, mirror_top_bs, &error_abort); | |
4ef85a9c | 748 | |
429076e8 | 749 | bs_opaque->job = NULL; |
4ef85a9c | 750 | |
176c3699 | 751 | bdrv_drained_end(src); |
d2da5e28 | 752 | bdrv_drained_end(mirror_top_bs); |
5e771752 | 753 | s->in_drain = false; |
4ef85a9c | 754 | bdrv_unref(mirror_top_bs); |
3f09bfbc | 755 | bdrv_unref(src); |
7b508f6b | 756 | |
737efc1e JS |
757 | return ret; |
758 | } | |
759 | ||
760 | static int mirror_prepare(Job *job) | |
761 | { | |
762 | return mirror_exit_common(job); | |
763 | } | |
764 | ||
765 | static void mirror_abort(Job *job) | |
766 | { | |
767 | int ret = mirror_exit_common(job); | |
768 | assert(ret == 0); | |
5a7e7a0b SH |
769 | } |
770 | ||
537c3d4f | 771 | static void coroutine_fn mirror_throttle(MirrorBlockJob *s) |
49efb1f5 DL |
772 | { |
773 | int64_t now = qemu_clock_get_ns(QEMU_CLOCK_REALTIME); | |
774 | ||
18bb6928 | 775 | if (now - s->last_pause_ns > BLOCK_JOB_SLICE_TIME) { |
49efb1f5 | 776 | s->last_pause_ns = now; |
5d43e86e | 777 | job_sleep_ns(&s->common.job, 0); |
49efb1f5 | 778 | } else { |
da01ff7f | 779 | job_pause_point(&s->common.job); |
49efb1f5 DL |
780 | } |
781 | } | |
782 | ||
c0b363ad DL |
783 | static int coroutine_fn mirror_dirty_init(MirrorBlockJob *s) |
784 | { | |
23ca459a | 785 | int64_t offset; |
c0b363ad | 786 | BlockDriverState *base = s->base; |
138f9fff | 787 | BlockDriverState *bs = s->mirror_top_bs->backing->bs; |
c0b363ad | 788 | BlockDriverState *target_bs = blk_bs(s->target); |
23ca459a | 789 | int ret; |
51b0a488 | 790 | int64_t count; |
c0b363ad | 791 | |
cdf3bc93 | 792 | if (s->zero_target) { |
c7c2769c | 793 | if (!bdrv_can_write_zeroes_with_unmap(target_bs)) { |
e0d7f73e | 794 | bdrv_set_dirty_bitmap(s->dirty_bitmap, 0, s->bdev_length); |
c7c2769c DL |
795 | return 0; |
796 | } | |
797 | ||
90ab48eb | 798 | s->initial_zeroing_ongoing = true; |
23ca459a EB |
799 | for (offset = 0; offset < s->bdev_length; ) { |
800 | int bytes = MIN(s->bdev_length - offset, | |
801 | QEMU_ALIGN_DOWN(INT_MAX, s->granularity)); | |
c7c2769c DL |
802 | |
803 | mirror_throttle(s); | |
804 | ||
daa7f2f9 | 805 | if (job_is_cancelled(&s->common.job)) { |
90ab48eb | 806 | s->initial_zeroing_ongoing = false; |
c7c2769c DL |
807 | return 0; |
808 | } | |
809 | ||
810 | if (s->in_flight >= MAX_IN_FLIGHT) { | |
67adf4b3 EB |
811 | trace_mirror_yield(s, UINT64_MAX, s->buf_free_count, |
812 | s->in_flight); | |
7e6c4ff7 | 813 | mirror_wait_for_free_in_flight_slot(s, NULL); |
c7c2769c DL |
814 | continue; |
815 | } | |
816 | ||
4295c5fc | 817 | mirror_perform(s, offset, bytes, MIRROR_METHOD_ZERO); |
23ca459a | 818 | offset += bytes; |
c7c2769c DL |
819 | } |
820 | ||
bae8196d | 821 | mirror_wait_for_all_io(s); |
90ab48eb | 822 | s->initial_zeroing_ongoing = false; |
b7d5062c DL |
823 | } |
824 | ||
c0b363ad | 825 | /* First part, loop on the sectors and initialize the dirty bitmap. */ |
23ca459a | 826 | for (offset = 0; offset < s->bdev_length; ) { |
c0b363ad | 827 | /* Just to make sure we are not exceeding int limit. */ |
23ca459a EB |
828 | int bytes = MIN(s->bdev_length - offset, |
829 | QEMU_ALIGN_DOWN(INT_MAX, s->granularity)); | |
c0b363ad DL |
830 | |
831 | mirror_throttle(s); | |
832 | ||
daa7f2f9 | 833 | if (job_is_cancelled(&s->common.job)) { |
c0b363ad DL |
834 | return 0; |
835 | } | |
836 | ||
170d3bd3 | 837 | ret = bdrv_is_allocated_above(bs, base, false, offset, bytes, &count); |
c0b363ad DL |
838 | if (ret < 0) { |
839 | return ret; | |
840 | } | |
841 | ||
23ca459a | 842 | assert(count); |
b7d5062c | 843 | if (ret == 1) { |
23ca459a | 844 | bdrv_set_dirty_bitmap(s->dirty_bitmap, offset, count); |
c0b363ad | 845 | } |
23ca459a | 846 | offset += count; |
c0b363ad DL |
847 | } |
848 | return 0; | |
849 | } | |
850 | ||
bdffb31d PB |
851 | /* Called when going out of the streaming phase to flush the bulk of the |
852 | * data to the medium, or just before completing. | |
853 | */ | |
854 | static int mirror_flush(MirrorBlockJob *s) | |
855 | { | |
856 | int ret = blk_flush(s->target); | |
857 | if (ret < 0) { | |
858 | if (mirror_error_action(s, false, -ret) == BLOCK_ERROR_ACTION_REPORT) { | |
859 | s->ret = ret; | |
860 | } | |
861 | } | |
862 | return ret; | |
863 | } | |
864 | ||
f67432a2 | 865 | static int coroutine_fn mirror_run(Job *job, Error **errp) |
893f7eba | 866 | { |
f67432a2 | 867 | MirrorBlockJob *s = container_of(job, MirrorBlockJob, common.job); |
138f9fff | 868 | BlockDriverState *bs = s->mirror_top_bs->backing->bs; |
e253f4b8 | 869 | BlockDriverState *target_bs = blk_bs(s->target); |
9a0cec66 | 870 | bool need_drain = true; |
c0b363ad | 871 | int64_t length; |
b812f671 | 872 | BlockDriverInfo bdi; |
1d33936e JC |
873 | char backing_filename[2]; /* we only need 2 characters because we are only |
874 | checking for a NULL string */ | |
893f7eba | 875 | int ret = 0; |
893f7eba | 876 | |
daa7f2f9 | 877 | if (job_is_cancelled(&s->common.job)) { |
893f7eba PB |
878 | goto immediate_exit; |
879 | } | |
880 | ||
b21c7652 HR |
881 | s->bdev_length = bdrv_getlength(bs); |
882 | if (s->bdev_length < 0) { | |
883 | ret = s->bdev_length; | |
373df5b1 | 884 | goto immediate_exit; |
becc347e KW |
885 | } |
886 | ||
887 | /* Active commit must resize the base image if its size differs from the | |
888 | * active layer. */ | |
889 | if (s->base == blk_bs(s->target)) { | |
890 | int64_t base_length; | |
891 | ||
892 | base_length = blk_getlength(s->target); | |
893 | if (base_length < 0) { | |
894 | ret = base_length; | |
895 | goto immediate_exit; | |
896 | } | |
897 | ||
898 | if (s->bdev_length > base_length) { | |
c80d8b06 HR |
899 | ret = blk_truncate(s->target, s->bdev_length, false, |
900 | PREALLOC_MODE_OFF, NULL); | |
becc347e KW |
901 | if (ret < 0) { |
902 | goto immediate_exit; | |
903 | } | |
904 | } | |
905 | } | |
906 | ||
907 | if (s->bdev_length == 0) { | |
2e1795b5 KW |
908 | /* Transition to the READY state and wait for complete. */ |
909 | job_transition_to_ready(&s->common.job); | |
9e48b025 | 910 | s->synced = true; |
d06107ad | 911 | s->actively_synced = true; |
daa7f2f9 | 912 | while (!job_is_cancelled(&s->common.job) && !s->should_complete) { |
198c49cc | 913 | job_yield(&s->common.job); |
9e48b025 | 914 | } |
daa7f2f9 | 915 | s->common.job.cancelled = false; |
9e48b025 | 916 | goto immediate_exit; |
893f7eba PB |
917 | } |
918 | ||
b21c7652 | 919 | length = DIV_ROUND_UP(s->bdev_length, s->granularity); |
402a4741 PB |
920 | s->in_flight_bitmap = bitmap_new(length); |
921 | ||
b812f671 PB |
922 | /* If we have no backing file yet in the destination, we cannot let |
923 | * the destination do COW. Instead, we copy sectors around the | |
924 | * dirty data if needed. We need a bitmap to do that. | |
925 | */ | |
e253f4b8 | 926 | bdrv_get_backing_filename(target_bs, backing_filename, |
b812f671 | 927 | sizeof(backing_filename)); |
e253f4b8 | 928 | if (!bdrv_get_info(target_bs, &bdi) && bdi.cluster_size) { |
b436982f EB |
929 | s->target_cluster_size = bdi.cluster_size; |
930 | } else { | |
931 | s->target_cluster_size = BDRV_SECTOR_SIZE; | |
e5b43573 | 932 | } |
b436982f EB |
933 | if (backing_filename[0] && !target_bs->backing && |
934 | s->granularity < s->target_cluster_size) { | |
935 | s->buf_size = MAX(s->buf_size, s->target_cluster_size); | |
e5b43573 | 936 | s->cow_bitmap = bitmap_new(length); |
b812f671 | 937 | } |
e253f4b8 | 938 | s->max_iov = MIN(bs->bl.max_iov, target_bs->bl.max_iov); |
b812f671 | 939 | |
7504edf4 KW |
940 | s->buf = qemu_try_blockalign(bs, s->buf_size); |
941 | if (s->buf == NULL) { | |
942 | ret = -ENOMEM; | |
943 | goto immediate_exit; | |
944 | } | |
945 | ||
402a4741 | 946 | mirror_free_init(s); |
893f7eba | 947 | |
49efb1f5 | 948 | s->last_pause_ns = qemu_clock_get_ns(QEMU_CLOCK_REALTIME); |
03544a6e | 949 | if (!s->is_none_mode) { |
c0b363ad | 950 | ret = mirror_dirty_init(s); |
daa7f2f9 | 951 | if (ret < 0 || job_is_cancelled(&s->common.job)) { |
c0b363ad | 952 | goto immediate_exit; |
893f7eba PB |
953 | } |
954 | } | |
955 | ||
dc162c8e | 956 | assert(!s->dbi); |
715a74d8 | 957 | s->dbi = bdrv_dirty_iter_new(s->dirty_bitmap); |
893f7eba | 958 | for (;;) { |
cc8c9d6c | 959 | uint64_t delay_ns = 0; |
49efb1f5 | 960 | int64_t cnt, delta; |
893f7eba PB |
961 | bool should_complete; |
962 | ||
d06107ad HR |
963 | /* Do not start passive operations while there are active |
964 | * writes in progress */ | |
965 | while (s->in_active_write_counter) { | |
7e6c4ff7 | 966 | mirror_wait_for_any_operation(s, NULL, true); |
d06107ad HR |
967 | } |
968 | ||
bd48bde8 PB |
969 | if (s->ret < 0) { |
970 | ret = s->ret; | |
971 | goto immediate_exit; | |
972 | } | |
973 | ||
da01ff7f | 974 | job_pause_point(&s->common.job); |
565ac01f | 975 | |
20dca810 | 976 | cnt = bdrv_get_dirty_count(s->dirty_bitmap); |
05df8a6a KW |
977 | /* cnt is the number of dirty bytes remaining and s->bytes_in_flight is |
978 | * the number of bytes currently being processed; together those are | |
979 | * the current remaining operation length */ | |
30a5c887 | 980 | job_progress_set_remaining(&s->common.job, s->bytes_in_flight + cnt); |
bd48bde8 PB |
981 | |
982 | /* Note that even when no rate limit is applied we need to yield | |
a7282330 | 983 | * periodically with no pending I/O so that bdrv_drain_all() returns. |
18bb6928 KW |
984 | * We do so every BLKOCK_JOB_SLICE_TIME nanoseconds, or when there is |
985 | * an error, or when the source is clean, whichever comes first. */ | |
49efb1f5 | 986 | delta = qemu_clock_get_ns(QEMU_CLOCK_REALTIME) - s->last_pause_ns; |
18bb6928 | 987 | if (delta < BLOCK_JOB_SLICE_TIME && |
bd48bde8 | 988 | s->common.iostatus == BLOCK_DEVICE_IO_STATUS_OK) { |
cf56a3c6 | 989 | if (s->in_flight >= MAX_IN_FLIGHT || s->buf_free_count == 0 || |
402a4741 | 990 | (cnt == 0 && s->in_flight > 0)) { |
9a46dba7 | 991 | trace_mirror_yield(s, cnt, s->buf_free_count, s->in_flight); |
7e6c4ff7 | 992 | mirror_wait_for_free_in_flight_slot(s, NULL); |
bd48bde8 PB |
993 | continue; |
994 | } else if (cnt != 0) { | |
cc8c9d6c | 995 | delay_ns = mirror_iteration(s); |
893f7eba | 996 | } |
893f7eba PB |
997 | } |
998 | ||
999 | should_complete = false; | |
bd48bde8 | 1000 | if (s->in_flight == 0 && cnt == 0) { |
893f7eba | 1001 | trace_mirror_before_flush(s); |
bdffb31d PB |
1002 | if (!s->synced) { |
1003 | if (mirror_flush(s) < 0) { | |
1004 | /* Go check s->ret. */ | |
1005 | continue; | |
b952b558 | 1006 | } |
b952b558 PB |
1007 | /* We're out of the streaming phase. From now on, if the job |
1008 | * is cancelled we will actually complete all pending I/O and | |
1009 | * report completion. This way, block-job-cancel will leave | |
1010 | * the target in a consistent state. | |
1011 | */ | |
2e1795b5 | 1012 | job_transition_to_ready(&s->common.job); |
bdffb31d | 1013 | s->synced = true; |
d06107ad HR |
1014 | if (s->copy_mode != MIRROR_COPY_MODE_BACKGROUND) { |
1015 | s->actively_synced = true; | |
1016 | } | |
d63ffd87 | 1017 | } |
bdffb31d PB |
1018 | |
1019 | should_complete = s->should_complete || | |
daa7f2f9 | 1020 | job_is_cancelled(&s->common.job); |
bdffb31d | 1021 | cnt = bdrv_get_dirty_count(s->dirty_bitmap); |
893f7eba PB |
1022 | } |
1023 | ||
1024 | if (cnt == 0 && should_complete) { | |
1025 | /* The dirty bitmap is not updated while operations are pending. | |
1026 | * If we're about to exit, wait for pending operations before | |
1027 | * calling bdrv_get_dirty_count(bs), or we may exit while the | |
1028 | * source has dirty data to copy! | |
1029 | * | |
1030 | * Note that I/O can be submitted by the guest while | |
9a0cec66 PB |
1031 | * mirror_populate runs, so pause it now. Before deciding |
1032 | * whether to switch to target check one last time if I/O has | |
1033 | * come in the meanwhile, and if not flush the data to disk. | |
893f7eba | 1034 | */ |
9a46dba7 | 1035 | trace_mirror_before_drain(s, cnt); |
9a0cec66 | 1036 | |
5e771752 | 1037 | s->in_drain = true; |
9a0cec66 | 1038 | bdrv_drained_begin(bs); |
20dca810 | 1039 | cnt = bdrv_get_dirty_count(s->dirty_bitmap); |
bdffb31d | 1040 | if (cnt > 0 || mirror_flush(s) < 0) { |
9a0cec66 | 1041 | bdrv_drained_end(bs); |
5e771752 | 1042 | s->in_drain = false; |
9a0cec66 PB |
1043 | continue; |
1044 | } | |
1045 | ||
1046 | /* The two disks are in sync. Exit and report successful | |
1047 | * completion. | |
1048 | */ | |
1049 | assert(QLIST_EMPTY(&bs->tracked_requests)); | |
daa7f2f9 | 1050 | s->common.job.cancelled = false; |
9a0cec66 PB |
1051 | need_drain = false; |
1052 | break; | |
893f7eba PB |
1053 | } |
1054 | ||
1055 | ret = 0; | |
ddc4115e SH |
1056 | |
1057 | if (s->synced && !should_complete) { | |
18bb6928 KW |
1058 | delay_ns = (s->in_flight == 0 && |
1059 | cnt == 0 ? BLOCK_JOB_SLICE_TIME : 0); | |
ddc4115e | 1060 | } |
9a46dba7 | 1061 | trace_mirror_before_sleep(s, cnt, s->synced, delay_ns); |
5d43e86e | 1062 | job_sleep_ns(&s->common.job, delay_ns); |
daa7f2f9 | 1063 | if (job_is_cancelled(&s->common.job) && |
004e95df | 1064 | (!s->synced || s->common.job.force_cancel)) |
eb36639f | 1065 | { |
b76e4458 | 1066 | break; |
893f7eba | 1067 | } |
49efb1f5 | 1068 | s->last_pause_ns = qemu_clock_get_ns(QEMU_CLOCK_REALTIME); |
893f7eba PB |
1069 | } |
1070 | ||
1071 | immediate_exit: | |
bd48bde8 PB |
1072 | if (s->in_flight > 0) { |
1073 | /* We get here only if something went wrong. Either the job failed, | |
1074 | * or it was cancelled prematurely so that we do not guarantee that | |
1075 | * the target is a copy of the source. | |
1076 | */ | |
004e95df | 1077 | assert(ret < 0 || ((s->common.job.force_cancel || !s->synced) && |
daa7f2f9 | 1078 | job_is_cancelled(&s->common.job))); |
9a0cec66 | 1079 | assert(need_drain); |
bae8196d | 1080 | mirror_wait_for_all_io(s); |
bd48bde8 PB |
1081 | } |
1082 | ||
1083 | assert(s->in_flight == 0); | |
7191bf31 | 1084 | qemu_vfree(s->buf); |
b812f671 | 1085 | g_free(s->cow_bitmap); |
402a4741 | 1086 | g_free(s->in_flight_bitmap); |
dc162c8e | 1087 | bdrv_dirty_iter_free(s->dbi); |
5a7e7a0b | 1088 | |
9a0cec66 | 1089 | if (need_drain) { |
5e771752 | 1090 | s->in_drain = true; |
9a0cec66 PB |
1091 | bdrv_drained_begin(bs); |
1092 | } | |
f67432a2 | 1093 | |
f67432a2 | 1094 | return ret; |
893f7eba PB |
1095 | } |
1096 | ||
3453d972 | 1097 | static void mirror_complete(Job *job, Error **errp) |
d63ffd87 | 1098 | { |
3453d972 | 1099 | MirrorBlockJob *s = container_of(job, MirrorBlockJob, common.job); |
4ef85a9c | 1100 | BlockDriverState *target; |
274fccee | 1101 | |
274fccee | 1102 | target = blk_bs(s->target); |
d63ffd87 | 1103 | |
d63ffd87 | 1104 | if (!s->synced) { |
9df229c3 | 1105 | error_setg(errp, "The active block job '%s' cannot be completed", |
3453d972 | 1106 | job->id); |
d63ffd87 PB |
1107 | return; |
1108 | } | |
1109 | ||
274fccee HR |
1110 | if (s->backing_mode == MIRROR_OPEN_BACKING_CHAIN) { |
1111 | int ret; | |
1112 | ||
1113 | assert(!target->backing); | |
1114 | ret = bdrv_open_backing_file(target, NULL, "backing", errp); | |
1115 | if (ret < 0) { | |
1116 | return; | |
1117 | } | |
1118 | } | |
1119 | ||
15d67298 | 1120 | /* block all operations on to_replace bs */ |
09158f00 | 1121 | if (s->replaces) { |
5a7e7a0b SH |
1122 | AioContext *replace_aio_context; |
1123 | ||
e12f3784 | 1124 | s->to_replace = bdrv_find_node(s->replaces); |
09158f00 | 1125 | if (!s->to_replace) { |
e12f3784 | 1126 | error_setg(errp, "Node name '%s' not found", s->replaces); |
09158f00 BC |
1127 | return; |
1128 | } | |
1129 | ||
5a7e7a0b SH |
1130 | replace_aio_context = bdrv_get_aio_context(s->to_replace); |
1131 | aio_context_acquire(replace_aio_context); | |
1132 | ||
4ef85a9c KW |
1133 | /* TODO Translate this into permission system. Current definition of |
1134 | * GRAPH_MOD would require to request it for the parents; they might | |
1135 | * not even be BlockDriverStates, however, so a BdrvChild can't address | |
1136 | * them. May need redefinition of GRAPH_MOD. */ | |
09158f00 BC |
1137 | error_setg(&s->replace_blocker, |
1138 | "block device is in use by block-job-complete"); | |
1139 | bdrv_op_block_all(s->to_replace, s->replace_blocker); | |
1140 | bdrv_ref(s->to_replace); | |
5a7e7a0b SH |
1141 | |
1142 | aio_context_release(replace_aio_context); | |
09158f00 BC |
1143 | } |
1144 | ||
d63ffd87 | 1145 | s->should_complete = true; |
3d70ff53 | 1146 | job_enter(job); |
d63ffd87 PB |
1147 | } |
1148 | ||
537c3d4f | 1149 | static void coroutine_fn mirror_pause(Job *job) |
565ac01f | 1150 | { |
da01ff7f | 1151 | MirrorBlockJob *s = container_of(job, MirrorBlockJob, common.job); |
565ac01f | 1152 | |
bae8196d | 1153 | mirror_wait_for_all_io(s); |
565ac01f SH |
1154 | } |
1155 | ||
89bd0305 KW |
1156 | static bool mirror_drained_poll(BlockJob *job) |
1157 | { | |
1158 | MirrorBlockJob *s = container_of(job, MirrorBlockJob, common); | |
5e771752 SL |
1159 | |
1160 | /* If the job isn't paused nor cancelled, we can't be sure that it won't | |
1161 | * issue more requests. We make an exception if we've reached this point | |
1162 | * from one of our own drain sections, to avoid a deadlock waiting for | |
1163 | * ourselves. | |
1164 | */ | |
1165 | if (!s->common.job.paused && !s->common.job.cancelled && !s->in_drain) { | |
1166 | return true; | |
1167 | } | |
1168 | ||
89bd0305 KW |
1169 | return !!s->in_flight; |
1170 | } | |
1171 | ||
3fc4b10a | 1172 | static const BlockJobDriver mirror_job_driver = { |
33e9e9bd KW |
1173 | .job_driver = { |
1174 | .instance_size = sizeof(MirrorBlockJob), | |
252291ea | 1175 | .job_type = JOB_TYPE_MIRROR, |
80fa2c75 | 1176 | .free = block_job_free, |
b15de828 | 1177 | .user_resume = block_job_user_resume, |
f67432a2 | 1178 | .run = mirror_run, |
737efc1e JS |
1179 | .prepare = mirror_prepare, |
1180 | .abort = mirror_abort, | |
da01ff7f | 1181 | .pause = mirror_pause, |
3453d972 | 1182 | .complete = mirror_complete, |
33e9e9bd | 1183 | }, |
89bd0305 | 1184 | .drained_poll = mirror_drained_poll, |
893f7eba PB |
1185 | }; |
1186 | ||
03544a6e | 1187 | static const BlockJobDriver commit_active_job_driver = { |
33e9e9bd KW |
1188 | .job_driver = { |
1189 | .instance_size = sizeof(MirrorBlockJob), | |
252291ea | 1190 | .job_type = JOB_TYPE_COMMIT, |
80fa2c75 | 1191 | .free = block_job_free, |
b15de828 | 1192 | .user_resume = block_job_user_resume, |
f67432a2 | 1193 | .run = mirror_run, |
737efc1e JS |
1194 | .prepare = mirror_prepare, |
1195 | .abort = mirror_abort, | |
da01ff7f | 1196 | .pause = mirror_pause, |
3453d972 | 1197 | .complete = mirror_complete, |
33e9e9bd | 1198 | }, |
89bd0305 | 1199 | .drained_poll = mirror_drained_poll, |
03544a6e FZ |
1200 | }; |
1201 | ||
537c3d4f SH |
1202 | static void coroutine_fn |
1203 | do_sync_target_write(MirrorBlockJob *job, MirrorMethod method, | |
1204 | uint64_t offset, uint64_t bytes, | |
1205 | QEMUIOVector *qiov, int flags) | |
d06107ad | 1206 | { |
5c511ac3 | 1207 | int ret; |
dbdf699c VSO |
1208 | size_t qiov_offset = 0; |
1209 | int64_t bitmap_offset, bitmap_end; | |
d06107ad | 1210 | |
dbdf699c VSO |
1211 | if (!QEMU_IS_ALIGNED(offset, job->granularity) && |
1212 | bdrv_dirty_bitmap_get(job->dirty_bitmap, offset)) | |
1213 | { | |
1214 | /* | |
1215 | * Dirty unaligned padding: ignore it. | |
1216 | * | |
1217 | * Reasoning: | |
1218 | * 1. If we copy it, we can't reset corresponding bit in | |
1219 | * dirty_bitmap as there may be some "dirty" bytes still not | |
1220 | * copied. | |
1221 | * 2. It's already dirty, so skipping it we don't diverge mirror | |
1222 | * progress. | |
1223 | * | |
1224 | * Note, that because of this, guest write may have no contribution | |
1225 | * into mirror converge, but that's not bad, as we have background | |
1226 | * process of mirroring. If under some bad circumstances (high guest | |
1227 | * IO load) background process starve, we will not converge anyway, | |
1228 | * even if each write will contribute, as guest is not guaranteed to | |
1229 | * rewrite the whole disk. | |
1230 | */ | |
1231 | qiov_offset = QEMU_ALIGN_UP(offset, job->granularity) - offset; | |
1232 | if (bytes <= qiov_offset) { | |
1233 | /* nothing to do after shrink */ | |
1234 | return; | |
1235 | } | |
1236 | offset += qiov_offset; | |
1237 | bytes -= qiov_offset; | |
1238 | } | |
1239 | ||
1240 | if (!QEMU_IS_ALIGNED(offset + bytes, job->granularity) && | |
1241 | bdrv_dirty_bitmap_get(job->dirty_bitmap, offset + bytes - 1)) | |
1242 | { | |
1243 | uint64_t tail = (offset + bytes) % job->granularity; | |
1244 | ||
1245 | if (bytes <= tail) { | |
1246 | /* nothing to do after shrink */ | |
1247 | return; | |
1248 | } | |
1249 | bytes -= tail; | |
1250 | } | |
1251 | ||
1252 | /* | |
1253 | * Tails are either clean or shrunk, so for bitmap resetting | |
1254 | * we safely align the range down. | |
1255 | */ | |
1256 | bitmap_offset = QEMU_ALIGN_UP(offset, job->granularity); | |
1257 | bitmap_end = QEMU_ALIGN_DOWN(offset + bytes, job->granularity); | |
1258 | if (bitmap_offset < bitmap_end) { | |
1259 | bdrv_reset_dirty_bitmap(job->dirty_bitmap, bitmap_offset, | |
1260 | bitmap_end - bitmap_offset); | |
1261 | } | |
d06107ad | 1262 | |
5c511ac3 | 1263 | job_progress_increase_remaining(&job->common.job, bytes); |
d06107ad | 1264 | |
5c511ac3 VSO |
1265 | switch (method) { |
1266 | case MIRROR_METHOD_COPY: | |
dbdf699c VSO |
1267 | ret = blk_co_pwritev_part(job->target, offset, bytes, |
1268 | qiov, qiov_offset, flags); | |
5c511ac3 | 1269 | break; |
d06107ad | 1270 | |
5c511ac3 VSO |
1271 | case MIRROR_METHOD_ZERO: |
1272 | assert(!qiov); | |
1273 | ret = blk_co_pwrite_zeroes(job->target, offset, bytes, flags); | |
1274 | break; | |
d06107ad | 1275 | |
5c511ac3 VSO |
1276 | case MIRROR_METHOD_DISCARD: |
1277 | assert(!qiov); | |
1278 | ret = blk_co_pdiscard(job->target, offset, bytes); | |
1279 | break; | |
d06107ad | 1280 | |
5c511ac3 VSO |
1281 | default: |
1282 | abort(); | |
1283 | } | |
d06107ad | 1284 | |
5c511ac3 VSO |
1285 | if (ret >= 0) { |
1286 | job_progress_update(&job->common.job, bytes); | |
1287 | } else { | |
1288 | BlockErrorAction action; | |
d06107ad | 1289 | |
dbdf699c VSO |
1290 | /* |
1291 | * We failed, so we should mark dirty the whole area, aligned up. | |
1292 | * Note that we don't care about shrunk tails if any: they were dirty | |
1293 | * at function start, and they must be still dirty, as we've locked | |
1294 | * the region for in-flight op. | |
1295 | */ | |
1296 | bitmap_offset = QEMU_ALIGN_DOWN(offset, job->granularity); | |
1297 | bitmap_end = QEMU_ALIGN_UP(offset + bytes, job->granularity); | |
1298 | bdrv_set_dirty_bitmap(job->dirty_bitmap, bitmap_offset, | |
1299 | bitmap_end - bitmap_offset); | |
5c511ac3 | 1300 | job->actively_synced = false; |
d06107ad | 1301 | |
5c511ac3 VSO |
1302 | action = mirror_error_action(job, false, -ret); |
1303 | if (action == BLOCK_ERROR_ACTION_REPORT) { | |
1304 | if (!job->ret) { | |
1305 | job->ret = ret; | |
d06107ad HR |
1306 | } |
1307 | } | |
d06107ad HR |
1308 | } |
1309 | } | |
1310 | ||
1311 | static MirrorOp *coroutine_fn active_write_prepare(MirrorBlockJob *s, | |
1312 | uint64_t offset, | |
1313 | uint64_t bytes) | |
1314 | { | |
1315 | MirrorOp *op; | |
1316 | uint64_t start_chunk = offset / s->granularity; | |
1317 | uint64_t end_chunk = DIV_ROUND_UP(offset + bytes, s->granularity); | |
1318 | ||
1319 | op = g_new(MirrorOp, 1); | |
1320 | *op = (MirrorOp){ | |
1321 | .s = s, | |
1322 | .offset = offset, | |
1323 | .bytes = bytes, | |
1324 | .is_active_write = true, | |
1325 | }; | |
1326 | qemu_co_queue_init(&op->waiting_requests); | |
1327 | QTAILQ_INSERT_TAIL(&s->ops_in_flight, op, next); | |
1328 | ||
1329 | s->in_active_write_counter++; | |
1330 | ||
1331 | mirror_wait_on_conflicts(op, s, offset, bytes); | |
1332 | ||
1333 | bitmap_set(s->in_flight_bitmap, start_chunk, end_chunk - start_chunk); | |
1334 | ||
1335 | return op; | |
1336 | } | |
1337 | ||
1338 | static void coroutine_fn active_write_settle(MirrorOp *op) | |
1339 | { | |
1340 | uint64_t start_chunk = op->offset / op->s->granularity; | |
1341 | uint64_t end_chunk = DIV_ROUND_UP(op->offset + op->bytes, | |
1342 | op->s->granularity); | |
1343 | ||
1344 | if (!--op->s->in_active_write_counter && op->s->actively_synced) { | |
1345 | BdrvChild *source = op->s->mirror_top_bs->backing; | |
1346 | ||
1347 | if (QLIST_FIRST(&source->bs->parents) == source && | |
1348 | QLIST_NEXT(source, next_parent) == NULL) | |
1349 | { | |
1350 | /* Assert that we are back in sync once all active write | |
1351 | * operations are settled. | |
1352 | * Note that we can only assert this if the mirror node | |
1353 | * is the source node's only parent. */ | |
1354 | assert(!bdrv_get_dirty_count(op->s->dirty_bitmap)); | |
1355 | } | |
1356 | } | |
1357 | bitmap_clear(op->s->in_flight_bitmap, start_chunk, end_chunk - start_chunk); | |
1358 | QTAILQ_REMOVE(&op->s->ops_in_flight, op, next); | |
1359 | qemu_co_queue_restart_all(&op->waiting_requests); | |
1360 | g_free(op); | |
1361 | } | |
1362 | ||
4ef85a9c KW |
1363 | static int coroutine_fn bdrv_mirror_top_preadv(BlockDriverState *bs, |
1364 | uint64_t offset, uint64_t bytes, QEMUIOVector *qiov, int flags) | |
1365 | { | |
1366 | return bdrv_co_preadv(bs->backing, offset, bytes, qiov, flags); | |
1367 | } | |
1368 | ||
d06107ad HR |
1369 | static int coroutine_fn bdrv_mirror_top_do_write(BlockDriverState *bs, |
1370 | MirrorMethod method, uint64_t offset, uint64_t bytes, QEMUIOVector *qiov, | |
1371 | int flags) | |
1372 | { | |
1373 | MirrorOp *op = NULL; | |
1374 | MirrorBDSOpaque *s = bs->opaque; | |
1375 | int ret = 0; | |
1376 | bool copy_to_target; | |
1377 | ||
1378 | copy_to_target = s->job->ret >= 0 && | |
1379 | s->job->copy_mode == MIRROR_COPY_MODE_WRITE_BLOCKING; | |
1380 | ||
1381 | if (copy_to_target) { | |
1382 | op = active_write_prepare(s->job, offset, bytes); | |
1383 | } | |
1384 | ||
1385 | switch (method) { | |
1386 | case MIRROR_METHOD_COPY: | |
1387 | ret = bdrv_co_pwritev(bs->backing, offset, bytes, qiov, flags); | |
1388 | break; | |
1389 | ||
1390 | case MIRROR_METHOD_ZERO: | |
1391 | ret = bdrv_co_pwrite_zeroes(bs->backing, offset, bytes, flags); | |
1392 | break; | |
1393 | ||
1394 | case MIRROR_METHOD_DISCARD: | |
0b9fd3f4 | 1395 | ret = bdrv_co_pdiscard(bs->backing, offset, bytes); |
d06107ad HR |
1396 | break; |
1397 | ||
1398 | default: | |
1399 | abort(); | |
1400 | } | |
1401 | ||
1402 | if (ret < 0) { | |
1403 | goto out; | |
1404 | } | |
1405 | ||
1406 | if (copy_to_target) { | |
1407 | do_sync_target_write(s->job, method, offset, bytes, qiov, flags); | |
1408 | } | |
1409 | ||
1410 | out: | |
1411 | if (copy_to_target) { | |
1412 | active_write_settle(op); | |
1413 | } | |
1414 | return ret; | |
1415 | } | |
1416 | ||
4ef85a9c KW |
1417 | static int coroutine_fn bdrv_mirror_top_pwritev(BlockDriverState *bs, |
1418 | uint64_t offset, uint64_t bytes, QEMUIOVector *qiov, int flags) | |
1419 | { | |
d06107ad HR |
1420 | MirrorBDSOpaque *s = bs->opaque; |
1421 | QEMUIOVector bounce_qiov; | |
1422 | void *bounce_buf; | |
1423 | int ret = 0; | |
1424 | bool copy_to_target; | |
1425 | ||
1426 | copy_to_target = s->job->ret >= 0 && | |
1427 | s->job->copy_mode == MIRROR_COPY_MODE_WRITE_BLOCKING; | |
1428 | ||
1429 | if (copy_to_target) { | |
1430 | /* The guest might concurrently modify the data to write; but | |
1431 | * the data on source and destination must match, so we have | |
1432 | * to use a bounce buffer if we are going to write to the | |
1433 | * target now. */ | |
1434 | bounce_buf = qemu_blockalign(bs, bytes); | |
1435 | iov_to_buf_full(qiov->iov, qiov->niov, 0, bounce_buf, bytes); | |
1436 | ||
1437 | qemu_iovec_init(&bounce_qiov, 1); | |
1438 | qemu_iovec_add(&bounce_qiov, bounce_buf, bytes); | |
1439 | qiov = &bounce_qiov; | |
1440 | } | |
1441 | ||
1442 | ret = bdrv_mirror_top_do_write(bs, MIRROR_METHOD_COPY, offset, bytes, qiov, | |
1443 | flags); | |
1444 | ||
1445 | if (copy_to_target) { | |
1446 | qemu_iovec_destroy(&bounce_qiov); | |
1447 | qemu_vfree(bounce_buf); | |
1448 | } | |
1449 | ||
1450 | return ret; | |
4ef85a9c KW |
1451 | } |
1452 | ||
1453 | static int coroutine_fn bdrv_mirror_top_flush(BlockDriverState *bs) | |
1454 | { | |
ce960aa9 VSO |
1455 | if (bs->backing == NULL) { |
1456 | /* we can be here after failed bdrv_append in mirror_start_job */ | |
1457 | return 0; | |
1458 | } | |
4ef85a9c KW |
1459 | return bdrv_co_flush(bs->backing->bs); |
1460 | } | |
1461 | ||
4ef85a9c | 1462 | static int coroutine_fn bdrv_mirror_top_pwrite_zeroes(BlockDriverState *bs, |
f5a5ca79 | 1463 | int64_t offset, int bytes, BdrvRequestFlags flags) |
4ef85a9c | 1464 | { |
d06107ad HR |
1465 | return bdrv_mirror_top_do_write(bs, MIRROR_METHOD_ZERO, offset, bytes, NULL, |
1466 | flags); | |
4ef85a9c KW |
1467 | } |
1468 | ||
1469 | static int coroutine_fn bdrv_mirror_top_pdiscard(BlockDriverState *bs, | |
f5a5ca79 | 1470 | int64_t offset, int bytes) |
4ef85a9c | 1471 | { |
d06107ad HR |
1472 | return bdrv_mirror_top_do_write(bs, MIRROR_METHOD_DISCARD, offset, bytes, |
1473 | NULL, 0); | |
4ef85a9c KW |
1474 | } |
1475 | ||
998b3a1e | 1476 | static void bdrv_mirror_top_refresh_filename(BlockDriverState *bs) |
fd4a6493 | 1477 | { |
18775ff3 VSO |
1478 | if (bs->backing == NULL) { |
1479 | /* we can be here after failed bdrv_attach_child in | |
1480 | * bdrv_set_backing_hd */ | |
1481 | return; | |
1482 | } | |
fd4a6493 KW |
1483 | pstrcpy(bs->exact_filename, sizeof(bs->exact_filename), |
1484 | bs->backing->bs->filename); | |
1485 | } | |
1486 | ||
4ef85a9c KW |
1487 | static void bdrv_mirror_top_child_perm(BlockDriverState *bs, BdrvChild *c, |
1488 | const BdrvChildRole *role, | |
e0995dc3 | 1489 | BlockReopenQueue *reopen_queue, |
4ef85a9c KW |
1490 | uint64_t perm, uint64_t shared, |
1491 | uint64_t *nperm, uint64_t *nshared) | |
1492 | { | |
f94dc3b4 HR |
1493 | MirrorBDSOpaque *s = bs->opaque; |
1494 | ||
1495 | if (s->stop) { | |
1496 | /* | |
1497 | * If the job is to be stopped, we do not need to forward | |
1498 | * anything to the real image. | |
1499 | */ | |
1500 | *nperm = 0; | |
1501 | *nshared = BLK_PERM_ALL; | |
1502 | return; | |
1503 | } | |
1504 | ||
4ef85a9c KW |
1505 | /* Must be able to forward guest writes to the real image */ |
1506 | *nperm = 0; | |
1507 | if (perm & BLK_PERM_WRITE) { | |
1508 | *nperm |= BLK_PERM_WRITE; | |
1509 | } | |
1510 | ||
1511 | *nshared = BLK_PERM_ALL; | |
1512 | } | |
1513 | ||
1514 | /* Dummy node that provides consistent read to its users without requiring it | |
1515 | * from its backing file and that allows writes on the backing file chain. */ | |
1516 | static BlockDriver bdrv_mirror_top = { | |
1517 | .format_name = "mirror_top", | |
1518 | .bdrv_co_preadv = bdrv_mirror_top_preadv, | |
1519 | .bdrv_co_pwritev = bdrv_mirror_top_pwritev, | |
1520 | .bdrv_co_pwrite_zeroes = bdrv_mirror_top_pwrite_zeroes, | |
1521 | .bdrv_co_pdiscard = bdrv_mirror_top_pdiscard, | |
1522 | .bdrv_co_flush = bdrv_mirror_top_flush, | |
3e4d0e72 | 1523 | .bdrv_co_block_status = bdrv_co_block_status_from_backing, |
fd4a6493 | 1524 | .bdrv_refresh_filename = bdrv_mirror_top_refresh_filename, |
4ef85a9c KW |
1525 | .bdrv_child_perm = bdrv_mirror_top_child_perm, |
1526 | }; | |
1527 | ||
cc19f177 VSO |
1528 | static BlockJob *mirror_start_job( |
1529 | const char *job_id, BlockDriverState *bs, | |
47970dfb JS |
1530 | int creation_flags, BlockDriverState *target, |
1531 | const char *replaces, int64_t speed, | |
1532 | uint32_t granularity, int64_t buf_size, | |
274fccee | 1533 | BlockMirrorBackingMode backing_mode, |
cdf3bc93 | 1534 | bool zero_target, |
09158f00 BC |
1535 | BlockdevOnError on_source_error, |
1536 | BlockdevOnError on_target_error, | |
0fc9f8ea | 1537 | bool unmap, |
097310b5 | 1538 | BlockCompletionFunc *cb, |
51ccfa2d | 1539 | void *opaque, |
09158f00 | 1540 | const BlockJobDriver *driver, |
b49f7ead | 1541 | bool is_none_mode, BlockDriverState *base, |
51ccfa2d | 1542 | bool auto_complete, const char *filter_node_name, |
481debaa | 1543 | bool is_mirror, MirrorCopyMode copy_mode, |
51ccfa2d | 1544 | Error **errp) |
893f7eba PB |
1545 | { |
1546 | MirrorBlockJob *s; | |
429076e8 | 1547 | MirrorBDSOpaque *bs_opaque; |
4ef85a9c KW |
1548 | BlockDriverState *mirror_top_bs; |
1549 | bool target_graph_mod; | |
1550 | bool target_is_backing; | |
b2c2832c | 1551 | Error *local_err = NULL; |
d7086422 | 1552 | int ret; |
893f7eba | 1553 | |
eee13dfe | 1554 | if (granularity == 0) { |
341ebc2f | 1555 | granularity = bdrv_get_default_bitmap_granularity(target); |
eee13dfe PB |
1556 | } |
1557 | ||
31826642 | 1558 | assert(is_power_of_2(granularity)); |
eee13dfe | 1559 | |
48ac0a4d WC |
1560 | if (buf_size < 0) { |
1561 | error_setg(errp, "Invalid parameter 'buf-size'"); | |
cc19f177 | 1562 | return NULL; |
48ac0a4d WC |
1563 | } |
1564 | ||
1565 | if (buf_size == 0) { | |
1566 | buf_size = DEFAULT_MIRROR_BUF_SIZE; | |
1567 | } | |
5bc361b8 | 1568 | |
86fae10c KW |
1569 | if (bs == target) { |
1570 | error_setg(errp, "Can't mirror node into itself"); | |
cc19f177 | 1571 | return NULL; |
86fae10c KW |
1572 | } |
1573 | ||
4ef85a9c KW |
1574 | /* In the case of active commit, add dummy driver to provide consistent |
1575 | * reads on the top, while disabling it in the intermediate nodes, and make | |
1576 | * the backing chain writable. */ | |
6cdbceb1 KW |
1577 | mirror_top_bs = bdrv_new_open_driver(&bdrv_mirror_top, filter_node_name, |
1578 | BDRV_O_RDWR, errp); | |
4ef85a9c | 1579 | if (mirror_top_bs == NULL) { |
cc19f177 | 1580 | return NULL; |
4ef85a9c | 1581 | } |
d3c8c674 KW |
1582 | if (!filter_node_name) { |
1583 | mirror_top_bs->implicit = true; | |
1584 | } | |
e5182c1c HR |
1585 | |
1586 | /* So that we can always drop this node */ | |
1587 | mirror_top_bs->never_freeze = true; | |
1588 | ||
4ef85a9c | 1589 | mirror_top_bs->total_sectors = bs->total_sectors; |
228345bf | 1590 | mirror_top_bs->supported_write_flags = BDRV_REQ_WRITE_UNCHANGED; |
80f5c33f KW |
1591 | mirror_top_bs->supported_zero_flags = BDRV_REQ_WRITE_UNCHANGED | |
1592 | BDRV_REQ_NO_FALLBACK; | |
429076e8 HR |
1593 | bs_opaque = g_new0(MirrorBDSOpaque, 1); |
1594 | mirror_top_bs->opaque = bs_opaque; | |
4ef85a9c KW |
1595 | |
1596 | /* bdrv_append takes ownership of the mirror_top_bs reference, need to keep | |
7a25fcd0 | 1597 | * it alive until block_job_create() succeeds even if bs has no parent. */ |
4ef85a9c KW |
1598 | bdrv_ref(mirror_top_bs); |
1599 | bdrv_drained_begin(bs); | |
b2c2832c | 1600 | bdrv_append(mirror_top_bs, bs, &local_err); |
4ef85a9c KW |
1601 | bdrv_drained_end(bs); |
1602 | ||
b2c2832c KW |
1603 | if (local_err) { |
1604 | bdrv_unref(mirror_top_bs); | |
1605 | error_propagate(errp, local_err); | |
cc19f177 | 1606 | return NULL; |
b2c2832c KW |
1607 | } |
1608 | ||
4ef85a9c | 1609 | /* Make sure that the source is not resized while the job is running */ |
75859b94 | 1610 | s = block_job_create(job_id, driver, NULL, mirror_top_bs, |
4ef85a9c KW |
1611 | BLK_PERM_CONSISTENT_READ, |
1612 | BLK_PERM_CONSISTENT_READ | BLK_PERM_WRITE_UNCHANGED | | |
1613 | BLK_PERM_WRITE | BLK_PERM_GRAPH_MOD, speed, | |
c6cc12bf | 1614 | creation_flags, cb, opaque, errp); |
893f7eba | 1615 | if (!s) { |
4ef85a9c | 1616 | goto fail; |
893f7eba | 1617 | } |
429076e8 HR |
1618 | bs_opaque->job = s; |
1619 | ||
7a25fcd0 HR |
1620 | /* The block job now has a reference to this node */ |
1621 | bdrv_unref(mirror_top_bs); | |
1622 | ||
4ef85a9c KW |
1623 | s->mirror_top_bs = mirror_top_bs; |
1624 | ||
1625 | /* No resize for the target either; while the mirror is still running, a | |
1626 | * consistent read isn't necessarily possible. We could possibly allow | |
1627 | * writes and graph modifications, though it would likely defeat the | |
1628 | * purpose of a mirror, so leave them blocked for now. | |
1629 | * | |
1630 | * In the case of active commit, things look a bit different, though, | |
1631 | * because the target is an already populated backing file in active use. | |
1632 | * We can allow anything except resize there.*/ | |
1633 | target_is_backing = bdrv_chain_contains(bs, target); | |
1634 | target_graph_mod = (backing_mode != MIRROR_LEAVE_BACKING_CHAIN); | |
d861ab3a KW |
1635 | s->target = blk_new(s->common.job.aio_context, |
1636 | BLK_PERM_WRITE | BLK_PERM_RESIZE | | |
4ef85a9c KW |
1637 | (target_graph_mod ? BLK_PERM_GRAPH_MOD : 0), |
1638 | BLK_PERM_WRITE_UNCHANGED | | |
1639 | (target_is_backing ? BLK_PERM_CONSISTENT_READ | | |
1640 | BLK_PERM_WRITE | | |
1641 | BLK_PERM_GRAPH_MOD : 0)); | |
d7086422 KW |
1642 | ret = blk_insert_bs(s->target, target, errp); |
1643 | if (ret < 0) { | |
4ef85a9c | 1644 | goto fail; |
d7086422 | 1645 | } |
045a2f82 FZ |
1646 | if (is_mirror) { |
1647 | /* XXX: Mirror target could be a NBD server of target QEMU in the case | |
1648 | * of non-shared block migration. To allow migration completion, we | |
1649 | * have to allow "inactivate" of the target BB. When that happens, we | |
1650 | * know the job is drained, and the vcpus are stopped, so no write | |
1651 | * operation will be performed. Block layer already has assertions to | |
1652 | * ensure that. */ | |
1653 | blk_set_force_allow_inactivate(s->target); | |
1654 | } | |
9ff7f0df | 1655 | blk_set_allow_aio_context_change(s->target, true); |
cf312932 | 1656 | blk_set_disable_request_queuing(s->target, true); |
e253f4b8 | 1657 | |
09158f00 | 1658 | s->replaces = g_strdup(replaces); |
b952b558 PB |
1659 | s->on_source_error = on_source_error; |
1660 | s->on_target_error = on_target_error; | |
03544a6e | 1661 | s->is_none_mode = is_none_mode; |
274fccee | 1662 | s->backing_mode = backing_mode; |
cdf3bc93 | 1663 | s->zero_target = zero_target; |
481debaa | 1664 | s->copy_mode = copy_mode; |
5bc361b8 | 1665 | s->base = base; |
eee13dfe | 1666 | s->granularity = granularity; |
48ac0a4d | 1667 | s->buf_size = ROUND_UP(buf_size, granularity); |
0fc9f8ea | 1668 | s->unmap = unmap; |
b49f7ead WC |
1669 | if (auto_complete) { |
1670 | s->should_complete = true; | |
1671 | } | |
b812f671 | 1672 | |
0db6e54a | 1673 | s->dirty_bitmap = bdrv_create_dirty_bitmap(bs, granularity, NULL, errp); |
b8afb520 | 1674 | if (!s->dirty_bitmap) { |
88f9d1b3 | 1675 | goto fail; |
b8afb520 | 1676 | } |
dbdf699c VSO |
1677 | if (s->copy_mode == MIRROR_COPY_MODE_WRITE_BLOCKING) { |
1678 | bdrv_disable_dirty_bitmap(s->dirty_bitmap); | |
1679 | } | |
10f3cd15 | 1680 | |
67b24427 AG |
1681 | ret = block_job_add_bdrv(&s->common, "source", bs, 0, |
1682 | BLK_PERM_WRITE_UNCHANGED | BLK_PERM_WRITE | | |
1683 | BLK_PERM_CONSISTENT_READ, | |
1684 | errp); | |
1685 | if (ret < 0) { | |
1686 | goto fail; | |
1687 | } | |
1688 | ||
4ef85a9c | 1689 | /* Required permissions are already taken with blk_new() */ |
76d554e2 KW |
1690 | block_job_add_bdrv(&s->common, "target", target, 0, BLK_PERM_ALL, |
1691 | &error_abort); | |
1692 | ||
f3ede4b0 AG |
1693 | /* In commit_active_start() all intermediate nodes disappear, so |
1694 | * any jobs in them must be blocked */ | |
4ef85a9c | 1695 | if (target_is_backing) { |
f3ede4b0 AG |
1696 | BlockDriverState *iter; |
1697 | for (iter = backing_bs(bs); iter != target; iter = backing_bs(iter)) { | |
4ef85a9c KW |
1698 | /* XXX BLK_PERM_WRITE needs to be allowed so we don't block |
1699 | * ourselves at s->base (if writes are blocked for a node, they are | |
1700 | * also blocked for its backing file). The other options would be a | |
1701 | * second filter driver above s->base (== target). */ | |
1702 | ret = block_job_add_bdrv(&s->common, "intermediate node", iter, 0, | |
1703 | BLK_PERM_WRITE_UNCHANGED | BLK_PERM_WRITE, | |
1704 | errp); | |
1705 | if (ret < 0) { | |
1706 | goto fail; | |
1707 | } | |
f3ede4b0 | 1708 | } |
ef53dc09 AG |
1709 | |
1710 | if (bdrv_freeze_backing_chain(mirror_top_bs, target, errp) < 0) { | |
1711 | goto fail; | |
1712 | } | |
f3ede4b0 | 1713 | } |
10f3cd15 | 1714 | |
12aa4082 HR |
1715 | QTAILQ_INIT(&s->ops_in_flight); |
1716 | ||
5ccac6f1 | 1717 | trace_mirror_start(bs, s, opaque); |
da01ff7f | 1718 | job_start(&s->common.job); |
cc19f177 VSO |
1719 | |
1720 | return &s->common; | |
4ef85a9c KW |
1721 | |
1722 | fail: | |
1723 | if (s) { | |
7a25fcd0 HR |
1724 | /* Make sure this BDS does not go away until we have completed the graph |
1725 | * changes below */ | |
1726 | bdrv_ref(mirror_top_bs); | |
1727 | ||
4ef85a9c KW |
1728 | g_free(s->replaces); |
1729 | blk_unref(s->target); | |
429076e8 | 1730 | bs_opaque->job = NULL; |
e917e2cb | 1731 | if (s->dirty_bitmap) { |
5deb6cbd | 1732 | bdrv_release_dirty_bitmap(s->dirty_bitmap); |
e917e2cb | 1733 | } |
4ad35181 | 1734 | job_early_fail(&s->common.job); |
4ef85a9c KW |
1735 | } |
1736 | ||
f94dc3b4 HR |
1737 | bs_opaque->stop = true; |
1738 | bdrv_child_refresh_perms(mirror_top_bs, mirror_top_bs->backing, | |
1739 | &error_abort); | |
5fe31c25 | 1740 | bdrv_replace_node(mirror_top_bs, backing_bs(mirror_top_bs), &error_abort); |
7a25fcd0 HR |
1741 | |
1742 | bdrv_unref(mirror_top_bs); | |
cc19f177 VSO |
1743 | |
1744 | return NULL; | |
893f7eba | 1745 | } |
03544a6e | 1746 | |
71aa9867 AG |
1747 | void mirror_start(const char *job_id, BlockDriverState *bs, |
1748 | BlockDriverState *target, const char *replaces, | |
a1999b33 JS |
1749 | int creation_flags, int64_t speed, |
1750 | uint32_t granularity, int64_t buf_size, | |
274fccee | 1751 | MirrorSyncMode mode, BlockMirrorBackingMode backing_mode, |
cdf3bc93 | 1752 | bool zero_target, |
274fccee | 1753 | BlockdevOnError on_source_error, |
03544a6e | 1754 | BlockdevOnError on_target_error, |
481debaa HR |
1755 | bool unmap, const char *filter_node_name, |
1756 | MirrorCopyMode copy_mode, Error **errp) | |
03544a6e FZ |
1757 | { |
1758 | bool is_none_mode; | |
1759 | BlockDriverState *base; | |
1760 | ||
c8b56501 JS |
1761 | if ((mode == MIRROR_SYNC_MODE_INCREMENTAL) || |
1762 | (mode == MIRROR_SYNC_MODE_BITMAP)) { | |
1763 | error_setg(errp, "Sync mode '%s' not supported", | |
1764 | MirrorSyncMode_str(mode)); | |
d58d8453 JS |
1765 | return; |
1766 | } | |
03544a6e | 1767 | is_none_mode = mode == MIRROR_SYNC_MODE_NONE; |
760e0063 | 1768 | base = mode == MIRROR_SYNC_MODE_TOP ? backing_bs(bs) : NULL; |
a1999b33 | 1769 | mirror_start_job(job_id, bs, creation_flags, target, replaces, |
cdf3bc93 | 1770 | speed, granularity, buf_size, backing_mode, zero_target, |
51ccfa2d | 1771 | on_source_error, on_target_error, unmap, NULL, NULL, |
6cdbceb1 | 1772 | &mirror_job_driver, is_none_mode, base, false, |
481debaa | 1773 | filter_node_name, true, copy_mode, errp); |
03544a6e FZ |
1774 | } |
1775 | ||
cc19f177 VSO |
1776 | BlockJob *commit_active_start(const char *job_id, BlockDriverState *bs, |
1777 | BlockDriverState *base, int creation_flags, | |
1778 | int64_t speed, BlockdevOnError on_error, | |
1779 | const char *filter_node_name, | |
1780 | BlockCompletionFunc *cb, void *opaque, | |
1781 | bool auto_complete, Error **errp) | |
03544a6e | 1782 | { |
1ba79388 | 1783 | bool base_read_only; |
cc67f4d1 | 1784 | Error *local_err = NULL; |
cc19f177 | 1785 | BlockJob *ret; |
4da83585 | 1786 | |
1ba79388 | 1787 | base_read_only = bdrv_is_read_only(base); |
4da83585 | 1788 | |
1ba79388 AG |
1789 | if (base_read_only) { |
1790 | if (bdrv_reopen_set_read_only(base, false, errp) < 0) { | |
cc19f177 | 1791 | return NULL; |
1ba79388 | 1792 | } |
20a63d2c | 1793 | } |
4da83585 | 1794 | |
cc19f177 VSO |
1795 | ret = mirror_start_job( |
1796 | job_id, bs, creation_flags, base, NULL, speed, 0, 0, | |
cdf3bc93 | 1797 | MIRROR_LEAVE_BACKING_CHAIN, false, |
51ccfa2d | 1798 | on_error, on_error, true, cb, opaque, |
6cdbceb1 | 1799 | &commit_active_job_driver, false, base, auto_complete, |
481debaa HR |
1800 | filter_node_name, false, MIRROR_COPY_MODE_BACKGROUND, |
1801 | &local_err); | |
0fb6395c | 1802 | if (local_err) { |
cc67f4d1 | 1803 | error_propagate(errp, local_err); |
4da83585 JC |
1804 | goto error_restore_flags; |
1805 | } | |
1806 | ||
cc19f177 | 1807 | return ret; |
4da83585 JC |
1808 | |
1809 | error_restore_flags: | |
1810 | /* ignore error and errp for bdrv_reopen, because we want to propagate | |
1811 | * the original error */ | |
1ba79388 AG |
1812 | if (base_read_only) { |
1813 | bdrv_reopen_set_read_only(base, true, NULL); | |
1814 | } | |
cc19f177 | 1815 | return NULL; |
03544a6e | 1816 | } |