master
c 675 lines 19.6 KB
Raw
1 /*
2 * Live block commit
3 *
4 * Copyright Red Hat, Inc. 2012
5 *
6 * Authors:
7 * Jeff Cody <jcody@redhat.com>
8 * Based on stream.c by Stefan Hajnoczi
9 *
10 * This work is licensed under the terms of the GNU LGPL, version 2 or later.
11 * See the COPYING.LIB file in the top-level directory.
12 *
13 */
14
15 #include "qemu/osdep.h"
16 #include "qemu/cutils.h"
17 #include "trace.h"
18 #include "block/block-common.h"
19 #include "block/coroutines.h"
20 #include "block/block_int.h"
21 #include "block/blockjob_int.h"
22 #include "qapi/error.h"
23 #include "qemu/ratelimit.h"
24 #include "qemu/memalign.h"
25 #include "system/block-backend.h"
26
27 enum {
28 /*
29 * Size of data buffer for populating the image file. This should be large
30 * enough to process multiple clusters in a single call, so that populating
31 * contiguous regions of the image is efficient.
32 */
33 COMMIT_BUFFER_SIZE = 512 * 1024, /* in bytes */
34 };
35
36 typedef struct CommitBlockJob {
37 BlockJob common;
38 BlockDriverState *commit_top_bs;
39 BlockBackend *top;
40 BlockBackend *base;
41 BlockDriverState *base_bs;
42 BlockDriverState *base_overlay;
43 BlockdevOnError on_error;
44 bool base_read_only;
45 bool chain_frozen;
46 char *backing_file_str;
47 bool backing_mask_protocol;
48 } CommitBlockJob;
49
50 static int commit_prepare(Job *job)
51 {
52 CommitBlockJob *s = container_of(job, CommitBlockJob, common.job);
53
54 bdrv_graph_rdlock_main_loop();
55 bdrv_unfreeze_backing_chain(s->commit_top_bs, s->base_bs);
56 s->chain_frozen = false;
57 bdrv_graph_rdunlock_main_loop();
58
59 /* Remove base node parent that still uses BLK_PERM_WRITE/RESIZE before
60 * the normal backing chain can be restored. */
61 blk_unref(s->base);
62 s->base = NULL;
63
64 /* FIXME: bdrv_drop_intermediate treats total failures and partial failures
65 * identically. Further work is needed to disambiguate these cases. */
66 return bdrv_drop_intermediate(s->commit_top_bs, s->base_bs,
67 s->backing_file_str,
68 s->backing_mask_protocol);
69 }
70
71 static void GRAPH_UNLOCKED commit_abort(Job *job)
72 {
73 CommitBlockJob *s = container_of(job, CommitBlockJob, common.job);
74 BlockDriverState *top_bs = blk_bs(s->top);
75 BlockDriverState *commit_top_backing_bs;
76
77 if (s->chain_frozen) {
78 bdrv_graph_rdlock_main_loop();
79 bdrv_unfreeze_backing_chain(s->commit_top_bs, s->base_bs);
80 bdrv_graph_rdunlock_main_loop();
81 }
82
83 /* Make sure commit_top_bs and top stay around until bdrv_replace_node() */
84 bdrv_ref(top_bs);
85 bdrv_ref(s->commit_top_bs);
86
87 if (s->base) {
88 blk_unref(s->base);
89 }
90
91 /* free the blockers on the intermediate nodes so that bdrv_replace_nodes
92 * can succeed */
93 block_job_remove_all_bdrv(&s->common);
94
95 /* If bdrv_drop_intermediate() failed (or was not invoked), remove the
96 * commit filter driver from the backing chain now. Do this as the final
97 * step so that the 'consistent read' permission can be granted.
98 *
99 * XXX Can (or should) we somehow keep 'consistent read' blocked even
100 * after the failed/cancelled commit job is gone? If we already wrote
101 * something to base, the intermediate images aren't valid any more. */
102 bdrv_graph_rdlock_main_loop();
103 commit_top_backing_bs = s->commit_top_bs->backing->bs;
104 bdrv_graph_rdunlock_main_loop();
105
106 bdrv_drained_begin(commit_top_backing_bs);
107 bdrv_graph_wrlock();
108 bdrv_replace_node(s->commit_top_bs, commit_top_backing_bs, &error_abort);
109 bdrv_graph_wrunlock();
110 bdrv_drained_end(commit_top_backing_bs);
111
112 bdrv_unref(s->commit_top_bs);
113 bdrv_unref(top_bs);
114 }
115
116 static void commit_clean(Job *job)
117 {
118 CommitBlockJob *s = container_of(job, CommitBlockJob, common.job);
119
120 /* restore base open flags here if appropriate (e.g., change the base back
121 * to r/o). These reopens do not need to be atomic, since we won't abort
122 * even on failure here */
123 if (s->base_read_only) {
124 bdrv_reopen_set_read_only(s->base_bs, true, NULL);
125 }
126
127 g_free(s->backing_file_str);
128 blk_unref(s->top);
129 }
130
131 static int coroutine_fn
132 commit_iteration(CommitBlockJob *s, int64_t offset,
133 int64_t *requested_bytes, void *buf)
134 {
135 BlockErrorAction action;
136 int64_t bytes = *requested_bytes;
137 int ret = 0;
138 bool error_in_source = true;
139
140 /* Copy if allocated above the base */
141 WITH_GRAPH_RDLOCK_GUARD() {
142 ret = bdrv_co_common_block_status_above(blk_bs(s->top),
143 s->base_overlay, true, true, offset, COMMIT_BUFFER_SIZE,
144 &bytes, NULL, NULL, NULL);
145 }
146
147 trace_commit_one_iteration(s, offset, bytes, ret);
148
149 if (ret < 0) {
150 goto fail;
151 }
152
153 if (ret & BDRV_BLOCK_ALLOCATED) {
154 if (ret & BDRV_BLOCK_ZERO) {
155 /*
156 * If the top (sub)clusters are smaller than the base
157 * (sub)clusters, this will not unmap unless the underlying device
158 * does some tracking of these requests. Ideally, we would find
159 * the maximal extent of the zero clusters.
160 */
161 ret = blk_co_pwrite_zeroes(s->base, offset, bytes,
162 BDRV_REQ_MAY_UNMAP);
163 if (ret < 0) {
164 error_in_source = false;
165 goto fail;
166 }
167 } else {
168 assert(bytes < SIZE_MAX);
169
170 ret = blk_co_pread(s->top, offset, bytes, buf, 0);
171 if (ret < 0) {
172 goto fail;
173 }
174
175 ret = blk_co_pwrite(s->base, offset, bytes, buf, 0);
176 if (ret < 0) {
177 error_in_source = false;
178 goto fail;
179 }
180 }
181
182 /*
183 * Whether zeroes actually end up on disk depends on the details of
184 * the underlying driver. Therefore, this might rate limit more than
185 * is necessary.
186 */
187 block_job_ratelimit_processed_bytes(&s->common, bytes);
188 }
189
190 /* Publish progress */
191
192 job_progress_update(&s->common.job, bytes);
193
194 *requested_bytes = bytes;
195
196 return 0;
197
198 fail:
199 action = block_job_error_action(&s->common, s->on_error,
200 error_in_source, -ret);
201 if (action == BLOCK_ERROR_ACTION_REPORT) {
202 return ret;
203 }
204
205 *requested_bytes = 0;
206
207 return 0;
208 }
209
210 static int coroutine_fn commit_run(Job *job, Error **errp)
211 {
212 CommitBlockJob *s = container_of(job, CommitBlockJob, common.job);
213 int64_t offset;
214 int ret = 0;
215 int64_t n = 0; /* bytes */
216 QEMU_AUTO_VFREE void *buf = NULL;
217 int64_t len, base_len;
218
219 len = blk_co_getlength(s->top);
220 if (len < 0) {
221 return len;
222 }
223 job_progress_set_remaining(&s->common.job, len);
224
225 base_len = blk_co_getlength(s->base);
226 if (base_len < 0) {
227 return base_len;
228 }
229
230 if (base_len < len) {
231 ret = blk_co_truncate(s->base, len, false, PREALLOC_MODE_OFF, 0, NULL);
232 if (ret) {
233 return ret;
234 }
235 }
236
237 buf = blk_blockalign(s->top, COMMIT_BUFFER_SIZE);
238
239 for (offset = 0; offset < len; offset += n) {
240 /* Note that even when no rate limit is applied we need to yield
241 * with no pending I/O here so that bdrv_drain_all() returns.
242 */
243 block_job_ratelimit_sleep(&s->common);
244 if (job_is_cancelled(&s->common.job)) {
245 break;
246 }
247
248 ret = commit_iteration(s, offset, &n, buf);
249
250 if (ret < 0) {
251 return ret;
252 }
253 }
254
255 return 0;
256 }
257
258 static const BlockJobDriver commit_job_driver = {
259 .job_driver = {
260 .instance_size = sizeof(CommitBlockJob),
261 .job_type = JOB_TYPE_COMMIT,
262 .free = block_job_free,
263 .user_resume = block_job_user_resume,
264 .run = commit_run,
265 .prepare = commit_prepare,
266 .abort = commit_abort,
267 .clean = commit_clean
268 },
269 };
270
271 static int coroutine_fn GRAPH_RDLOCK
272 bdrv_commit_top_preadv(BlockDriverState *bs, int64_t offset, int64_t bytes,
273 QEMUIOVector *qiov, BdrvRequestFlags flags)
274 {
275 return bdrv_co_preadv(bs->backing, offset, bytes, qiov, flags);
276 }
277
278 static GRAPH_RDLOCK void bdrv_commit_top_refresh_filename(BlockDriverState *bs)
279 {
280 pstrcpy(bs->exact_filename, sizeof(bs->exact_filename),
281 bs->backing->bs->filename);
282 }
283
284 static void bdrv_commit_top_child_perm(BlockDriverState *bs, BdrvChild *c,
285 BdrvChildRole role,
286 BlockReopenQueue *reopen_queue,
287 uint64_t perm, uint64_t shared,
288 uint64_t *nperm, uint64_t *nshared)
289 {
290 *nperm = 0;
291 *nshared = BLK_PERM_ALL;
292 }
293
294 /* Dummy node that provides consistent read to its users without requiring it
295 * from its backing file and that allows writes on the backing file chain. */
296 static BlockDriver bdrv_commit_top = {
297 .format_name = "commit_top",
298 .bdrv_co_preadv = bdrv_commit_top_preadv,
299 .bdrv_refresh_filename = bdrv_commit_top_refresh_filename,
300 .bdrv_child_perm = bdrv_commit_top_child_perm,
301
302 .is_filter = true,
303 .filtered_child_is_backing = true,
304 };
305
306 void commit_start(const char *job_id, BlockDriverState *bs,
307 BlockDriverState *base, BlockDriverState *top,
308 int creation_flags, int64_t speed,
309 BlockdevOnError on_error, const char *backing_file_str,
310 bool backing_mask_protocol,
311 const char *filter_node_name, Error **errp)
312 {
313 CommitBlockJob *s;
314 BlockDriverState *iter;
315 BlockDriverState *commit_top_bs = NULL;
316 BlockDriverState *filtered_base;
317 int64_t base_size, top_size;
318 uint64_t base_perms, iter_shared_perms;
319 int ret;
320
321 GLOBAL_STATE_CODE();
322
323 assert(top != bs);
324 bdrv_graph_rdlock_main_loop();
325 if (bdrv_skip_filters(top) == bdrv_skip_filters(base)) {
326 error_setg(errp, "Invalid files for merge: top and base are the same");
327 bdrv_graph_rdunlock_main_loop();
328 return;
329 }
330 bdrv_graph_rdunlock_main_loop();
331
332 base_size = bdrv_getlength(base);
333 if (base_size < 0) {
334 error_setg_errno(errp, -base_size, "Could not inquire base image size");
335 return;
336 }
337
338 top_size = bdrv_getlength(top);
339 if (top_size < 0) {
340 error_setg_errno(errp, -top_size, "Could not inquire top image size");
341 return;
342 }
343
344 base_perms = BLK_PERM_CONSISTENT_READ | BLK_PERM_WRITE;
345 if (base_size < top_size) {
346 base_perms |= BLK_PERM_RESIZE;
347 }
348
349 s = block_job_create(job_id, &commit_job_driver, NULL, bs, 0, BLK_PERM_ALL,
350 speed, creation_flags, NULL, NULL, errp);
351 if (!s) {
352 return;
353 }
354
355 /* convert base to r/w, if necessary */
356 s->base_read_only = bdrv_is_read_only(base);
357 if (s->base_read_only) {
358 if (bdrv_reopen_set_read_only(base, false, errp) != 0) {
359 goto fail;
360 }
361 }
362
363 /* Insert commit_top block node above top, so we can block consistent read
364 * on the backing chain below it */
365 commit_top_bs = bdrv_new_open_driver(&bdrv_commit_top, filter_node_name, 0,
366 errp);
367 if (commit_top_bs == NULL) {
368 goto fail;
369 }
370 if (!filter_node_name) {
371 commit_top_bs->implicit = true;
372 }
373
374 /* So that we can always drop this node */
375 commit_top_bs->never_freeze = true;
376
377 commit_top_bs->total_sectors = top->total_sectors;
378
379 ret = bdrv_append(commit_top_bs, top, errp);
380 bdrv_unref(commit_top_bs); /* referenced by new parents or failed */
381 if (ret < 0) {
382 commit_top_bs = NULL;
383 goto fail;
384 }
385
386 s->commit_top_bs = commit_top_bs;
387
388 /*
389 * Block all nodes between top and base, because they will
390 * disappear from the chain after this operation.
391 * Note that this assumes that the user is fine with removing all
392 * nodes (including R/W filters) between top and base. Assuring
393 * this is the responsibility of the interface (i.e. whoever calls
394 * commit_start()).
395 */
396 bdrv_graph_wrlock_drained();
397 s->base_overlay = bdrv_find_overlay(top, base);
398 assert(s->base_overlay);
399
400 /*
401 * The topmost node with
402 * bdrv_skip_filters(filtered_base) == bdrv_skip_filters(base)
403 */
404 filtered_base = bdrv_cow_bs(s->base_overlay);
405 assert(bdrv_skip_filters(filtered_base) == bdrv_skip_filters(base));
406
407 /*
408 * XXX BLK_PERM_WRITE needs to be allowed so we don't block ourselves
409 * at s->base (if writes are blocked for a node, they are also blocked
410 * for its backing file). The other options would be a second filter
411 * driver above s->base.
412 */
413 iter_shared_perms = BLK_PERM_WRITE_UNCHANGED | BLK_PERM_WRITE;
414
415 for (iter = top; iter != base; iter = bdrv_filter_or_cow_bs(iter)) {
416 if (iter == filtered_base) {
417 /*
418 * From here on, all nodes are filters on the base. This
419 * allows us to share BLK_PERM_CONSISTENT_READ.
420 */
421 iter_shared_perms |= BLK_PERM_CONSISTENT_READ;
422 }
423
424 ret = block_job_add_bdrv(&s->common, "intermediate node", iter, 0,
425 iter_shared_perms, errp);
426 if (ret < 0) {
427 bdrv_graph_wrunlock();
428 goto fail;
429 }
430 }
431
432 if (bdrv_freeze_backing_chain(commit_top_bs, base, errp) < 0) {
433 bdrv_graph_wrunlock();
434 goto fail;
435 }
436 s->chain_frozen = true;
437
438 ret = block_job_add_bdrv(&s->common, "base", base, 0, BLK_PERM_ALL, errp);
439 bdrv_graph_wrunlock();
440
441 if (ret < 0) {
442 goto fail;
443 }
444
445 s->base = blk_new(s->common.job.aio_context,
446 base_perms,
447 BLK_PERM_CONSISTENT_READ
448 | BLK_PERM_WRITE_UNCHANGED);
449 ret = blk_insert_bs(s->base, base, errp);
450 if (ret < 0) {
451 goto fail;
452 }
453 blk_set_disable_request_queuing(s->base, true);
454 s->base_bs = base;
455
456 /* Required permissions are already taken with block_job_add_bdrv() */
457 s->top = blk_new(s->common.job.aio_context, 0, BLK_PERM_ALL);
458 ret = blk_insert_bs(s->top, top, errp);
459 if (ret < 0) {
460 goto fail;
461 }
462 blk_set_disable_request_queuing(s->top, true);
463
464 s->backing_file_str = g_strdup(backing_file_str);
465 s->backing_mask_protocol = backing_mask_protocol;
466 s->on_error = on_error;
467
468 trace_commit_start(bs, base, top, s);
469 job_start(&s->common.job);
470 return;
471
472 fail:
473 if (s->chain_frozen) {
474 bdrv_graph_rdlock_main_loop();
475 bdrv_unfreeze_backing_chain(commit_top_bs, base);
476 bdrv_graph_rdunlock_main_loop();
477 }
478 if (s->base) {
479 blk_unref(s->base);
480 }
481 if (s->top) {
482 blk_unref(s->top);
483 }
484 if (s->base_read_only) {
485 bdrv_reopen_set_read_only(base, true, NULL);
486 }
487 job_early_fail(&s->common.job);
488 /* commit_top_bs has to be replaced after deleting the block job,
489 * otherwise this would fail because of lack of permissions. */
490 if (commit_top_bs) {
491 bdrv_drained_begin(top);
492 bdrv_graph_wrlock();
493 bdrv_replace_node(commit_top_bs, top, &error_abort);
494 bdrv_graph_wrunlock();
495 bdrv_drained_end(top);
496 }
497 }
498
499
500 #define COMMIT_BUF_SIZE (2048 * BDRV_SECTOR_SIZE)
501
502 /* commit COW file into the raw image */
503 int bdrv_commit(BlockDriverState *bs)
504 {
505 BlockBackend *src, *backing;
506 BlockDriverState *backing_file_bs = NULL;
507 BlockDriverState *commit_top_bs = NULL;
508 BlockDriver *drv = bs->drv;
509 AioContext *ctx;
510 int64_t offset, length, backing_length;
511 int ro;
512 int64_t n;
513 int ret = 0;
514 QEMU_AUTO_VFREE uint8_t *buf = NULL;
515 Error *local_err = NULL;
516
517 GLOBAL_STATE_CODE();
518
519 if (!drv)
520 return -ENOMEDIUM;
521
522 bdrv_drain_all_begin();
523 bdrv_graph_rdlock_main_loop();
524
525 backing_file_bs = bdrv_cow_bs(bs);
526
527 if (!backing_file_bs) {
528 ret = -ENOTSUP;
529 goto out;
530 }
531
532 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_COMMIT_SOURCE, NULL) ||
533 bdrv_op_is_blocked(backing_file_bs, BLOCK_OP_TYPE_COMMIT_TARGET, NULL))
534 {
535 ret = -EBUSY;
536 goto out;
537 }
538
539 ro = bdrv_is_read_only(backing_file_bs);
540
541 if (ro) {
542 if (bdrv_reopen_set_read_only(backing_file_bs, false, NULL)) {
543 ret = -EACCES;
544 goto out;
545 }
546 }
547
548 ctx = bdrv_get_aio_context(bs);
549 /* WRITE_UNCHANGED is required for bdrv_make_empty() */
550 src = blk_new(ctx, BLK_PERM_CONSISTENT_READ | BLK_PERM_WRITE_UNCHANGED,
551 BLK_PERM_ALL);
552 backing = blk_new(ctx, BLK_PERM_WRITE | BLK_PERM_RESIZE, BLK_PERM_ALL);
553
554 /* We drained all nodes, but still make requests through BlockBackends */
555 blk_set_disable_request_queuing(src, true);
556 blk_set_disable_request_queuing(backing, true);
557
558 ret = blk_insert_bs(src, bs, &local_err);
559 if (ret < 0) {
560 error_report_err(local_err);
561 goto ro_cleanup;
562 }
563
564 /* Insert commit_top block node above backing, so we can write to it */
565 commit_top_bs = bdrv_new_open_driver(&bdrv_commit_top, NULL, BDRV_O_RDWR,
566 &local_err);
567 if (commit_top_bs == NULL) {
568 error_report_err(local_err);
569 goto ro_cleanup;
570 }
571
572 bdrv_graph_rdunlock_main_loop();
573
574 bdrv_graph_wrlock();
575 bdrv_set_backing_hd(commit_top_bs, backing_file_bs, &error_abort);
576 bdrv_set_backing_hd(bs, commit_top_bs, &error_abort);
577 bdrv_graph_wrunlock();
578
579 bdrv_graph_rdlock_main_loop();
580
581 ret = blk_insert_bs(backing, backing_file_bs, &local_err);
582 if (ret < 0) {
583 error_report_err(local_err);
584 goto ro_cleanup;
585 }
586
587 length = blk_getlength(src);
588 if (length < 0) {
589 ret = length;
590 goto ro_cleanup;
591 }
592
593 backing_length = blk_getlength(backing);
594 if (backing_length < 0) {
595 ret = backing_length;
596 goto ro_cleanup;
597 }
598
599 /* If our top snapshot is larger than the backing file image,
600 * grow the backing file image if possible. If not possible,
601 * we must return an error */
602 if (length > backing_length) {
603 ret = blk_truncate(backing, length, false, PREALLOC_MODE_OFF, 0,
604 &local_err);
605 if (ret < 0) {
606 error_report_err(local_err);
607 goto ro_cleanup;
608 }
609 }
610
611 /* blk_try_blockalign() for src will choose an alignment that works for
612 * backing as well, so no need to compare the alignment manually. */
613 buf = blk_try_blockalign(src, COMMIT_BUF_SIZE);
614 if (buf == NULL) {
615 ret = -ENOMEM;
616 goto ro_cleanup;
617 }
618
619 for (offset = 0; offset < length; offset += n) {
620 ret = bdrv_is_allocated(bs, offset, COMMIT_BUF_SIZE, &n);
621 if (ret < 0) {
622 goto ro_cleanup;
623 }
624 if (ret) {
625 ret = blk_pread(src, offset, n, buf, 0);
626 if (ret < 0) {
627 goto ro_cleanup;
628 }
629
630 ret = blk_pwrite(backing, offset, n, buf, 0);
631 if (ret < 0) {
632 goto ro_cleanup;
633 }
634 }
635 }
636
637 ret = blk_make_empty(src, NULL);
638 /* Ignore -ENOTSUP */
639 if (ret < 0 && ret != -ENOTSUP) {
640 goto ro_cleanup;
641 }
642
643 blk_flush(src);
644
645 /*
646 * Make sure all data we wrote to the backing device is actually
647 * stable on disk.
648 */
649 blk_flush(backing);
650
651 ret = 0;
652 ro_cleanup:
653 blk_unref(backing);
654
655 bdrv_graph_rdunlock_main_loop();
656 bdrv_graph_wrlock();
657 if (bdrv_cow_bs(bs) != backing_file_bs) {
658 bdrv_set_backing_hd(bs, backing_file_bs, &error_abort);
659 }
660 bdrv_graph_wrunlock();
661 bdrv_graph_rdlock_main_loop();
662 bdrv_unref(commit_top_bs);
663 blk_unref(src);
664
665 if (ro) {
666 /* ignoring error return here */
667 bdrv_reopen_set_read_only(backing_file_bs, true, NULL);
668 }
669
670 out:
671 bdrv_graph_rdunlock_main_loop();
672 bdrv_drain_all_end();
673
674 return ret;
675 }