]> git.proxmox.com Git - mirror_qemu.git/blame - block/commit.c
commit: Inline commit_populate()
[mirror_qemu.git] / block / commit.c
CommitLineData
747ff602
JC
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
80c71a24 15#include "qemu/osdep.h"
dcbf37ce 16#include "qemu/cutils.h"
747ff602 17#include "trace.h"
737e150e 18#include "block/block_int.h"
c87621ea 19#include "block/blockjob_int.h"
da34e65c 20#include "qapi/error.h"
cc7a8ea7 21#include "qapi/qmp/qerror.h"
747ff602 22#include "qemu/ratelimit.h"
373340b2 23#include "sysemu/block-backend.h"
747ff602
JC
24
25enum {
26 /*
27 * Size of data buffer for populating the image file. This should be large
28 * enough to process multiple clusters in a single call, so that populating
29 * contiguous regions of the image is efficient.
30 */
31 COMMIT_BUFFER_SIZE = 512 * 1024, /* in bytes */
32};
33
747ff602
JC
34typedef struct CommitBlockJob {
35 BlockJob common;
8dfba279 36 BlockDriverState *commit_top_bs;
4653456a
KW
37 BlockBackend *top;
38 BlockBackend *base;
22dffcbe 39 BlockDriverState *base_bs;
92aa5c6d 40 BlockdevOnError on_error;
e70cdc57 41 bool base_read_only;
df827336 42 bool chain_frozen;
54e26900 43 char *backing_file_str;
747ff602
JC
44} CommitBlockJob;
45
22dffcbe 46static int commit_prepare(Job *job)
747ff602 47{
1908a559 48 CommitBlockJob *s = container_of(job, CommitBlockJob, common.job);
19ebd13e 49
df827336
AG
50 bdrv_unfreeze_backing_chain(s->commit_top_bs, s->base_bs);
51 s->chain_frozen = false;
52
8dfba279
KW
53 /* Remove base node parent that still uses BLK_PERM_WRITE/RESIZE before
54 * the normal backing chain can be restored. */
55 blk_unref(s->base);
22dffcbe
JS
56 s->base = NULL;
57
58 /* FIXME: bdrv_drop_intermediate treats total failures and partial failures
59 * identically. Further work is needed to disambiguate these cases. */
60 return bdrv_drop_intermediate(s->commit_top_bs, s->base_bs,
61 s->backing_file_str);
62}
9e85cd5c 63
22dffcbe
JS
64static void commit_abort(Job *job)
65{
66 CommitBlockJob *s = container_of(job, CommitBlockJob, common.job);
67 BlockDriverState *top_bs = blk_bs(s->top);
68
df827336
AG
69 if (s->chain_frozen) {
70 bdrv_unfreeze_backing_chain(s->commit_top_bs, s->base_bs);
71 }
72
22dffcbe
JS
73 /* Make sure commit_top_bs and top stay around until bdrv_replace_node() */
74 bdrv_ref(top_bs);
75 bdrv_ref(s->commit_top_bs);
76
77 if (s->base) {
78 blk_unref(s->base);
9e85cd5c
SH
79 }
80
22dffcbe
JS
81 /* free the blockers on the intermediate nodes so that bdrv_replace_nodes
82 * can succeed */
83 block_job_remove_all_bdrv(&s->common);
84
85 /* If bdrv_drop_intermediate() failed (or was not invoked), remove the
86 * commit filter driver from the backing chain now. Do this as the final
87 * step so that the 'consistent read' permission can be granted.
88 *
89 * XXX Can (or should) we somehow keep 'consistent read' blocked even
90 * after the failed/cancelled commit job is gone? If we already wrote
91 * something to base, the intermediate images aren't valid any more. */
22dffcbe
JS
92 bdrv_replace_node(s->commit_top_bs, backing_bs(s->commit_top_bs),
93 &error_abort);
94
95 bdrv_unref(s->commit_top_bs);
96 bdrv_unref(top_bs);
97}
98
99static void commit_clean(Job *job)
100{
101 CommitBlockJob *s = container_of(job, CommitBlockJob, common.job);
102
9e85cd5c
SH
103 /* restore base open flags here if appropriate (e.g., change the base back
104 * to r/o). These reopens do not need to be atomic, since we won't abort
105 * even on failure here */
e70cdc57
AG
106 if (s->base_read_only) {
107 bdrv_reopen_set_read_only(s->base_bs, true, NULL);
9e85cd5c 108 }
22dffcbe 109
9e85cd5c 110 g_free(s->backing_file_str);
4653456a 111 blk_unref(s->top);
9e85cd5c
SH
112}
113
f67432a2 114static int coroutine_fn commit_run(Job *job, Error **errp)
9e85cd5c 115{
f67432a2 116 CommitBlockJob *s = container_of(job, CommitBlockJob, common.job);
317a6676 117 int64_t offset;
f14a39cc 118 uint64_t delay_ns = 0;
747ff602 119 int ret = 0;
51b0a488 120 int64_t n = 0; /* bytes */
9e85cd5c 121 void *buf = NULL;
05df8a6a 122 int64_t len, base_len;
747ff602 123
05df8a6a
KW
124 ret = len = blk_getlength(s->top);
125 if (len < 0) {
9e85cd5c 126 goto out;
747ff602 127 }
30a5c887 128 job_progress_set_remaining(&s->common.job, len);
747ff602 129
4653456a 130 ret = base_len = blk_getlength(s->base);
747ff602 131 if (base_len < 0) {
9e85cd5c 132 goto out;
747ff602
JC
133 }
134
05df8a6a 135 if (base_len < len) {
c80d8b06 136 ret = blk_truncate(s->base, len, false, PREALLOC_MODE_OFF, NULL);
747ff602 137 if (ret) {
9e85cd5c 138 goto out;
747ff602
JC
139 }
140 }
141
4653456a 142 buf = blk_blockalign(s->top, COMMIT_BUFFER_SIZE);
747ff602 143
05df8a6a 144 for (offset = 0; offset < len; offset += n) {
747ff602
JC
145 bool copy;
146
747ff602 147 /* Note that even when no rate limit is applied we need to yield
c57b6656 148 * with no pending I/O here so that bdrv_drain_all() returns.
747ff602 149 */
5d43e86e 150 job_sleep_ns(&s->common.job, delay_ns);
daa7f2f9 151 if (job_is_cancelled(&s->common.job)) {
747ff602
JC
152 break;
153 }
154 /* Copy if allocated above the base */
170d3bd3 155 ret = bdrv_is_allocated_above(blk_bs(s->top), blk_bs(s->base), false,
51b0a488 156 offset, COMMIT_BUFFER_SIZE, &n);
747ff602 157 copy = (ret == 1);
51b0a488 158 trace_commit_one_iteration(s, offset, n, ret);
747ff602 159 if (copy) {
0c42e175
KW
160 assert(n < SIZE_MAX);
161
162 ret = blk_co_pread(s->top, offset, n, buf, 0);
163 if (ret >= 0) {
164 ret = blk_co_pwrite(s->base, offset, n, buf, 0);
165 }
747ff602
JC
166 }
167 if (ret < 0) {
1e8fb7f1 168 BlockErrorAction action =
c5507b4d 169 block_job_error_action(&s->common, s->on_error, false, -ret);
1e8fb7f1 170 if (action == BLOCK_ERROR_ACTION_REPORT) {
9e85cd5c 171 goto out;
747ff602
JC
172 } else {
173 n = 0;
174 continue;
175 }
176 }
177 /* Publish progress */
30a5c887 178 job_progress_update(&s->common.job, n);
f14a39cc 179
dee81d51
KW
180 if (copy) {
181 delay_ns = block_job_ratelimit_get_delay(&s->common, n);
2fe4bba1
KW
182 } else {
183 delay_ns = 0;
f14a39cc 184 }
747ff602
JC
185 }
186
187 ret = 0;
188
9e85cd5c 189out:
747ff602
JC
190 qemu_vfree(buf);
191
f67432a2 192 return ret;
747ff602
JC
193}
194
3fc4b10a 195static const BlockJobDriver commit_job_driver = {
33e9e9bd
KW
196 .job_driver = {
197 .instance_size = sizeof(CommitBlockJob),
252291ea 198 .job_type = JOB_TYPE_COMMIT,
80fa2c75 199 .free = block_job_free,
b15de828 200 .user_resume = block_job_user_resume,
f67432a2 201 .run = commit_run,
22dffcbe
JS
202 .prepare = commit_prepare,
203 .abort = commit_abort,
204 .clean = commit_clean
33e9e9bd 205 },
747ff602
JC
206};
207
8dfba279
KW
208static int coroutine_fn bdrv_commit_top_preadv(BlockDriverState *bs,
209 uint64_t offset, uint64_t bytes, QEMUIOVector *qiov, int flags)
210{
211 return bdrv_co_preadv(bs->backing, offset, bytes, qiov, flags);
212}
213
998b3a1e 214static void bdrv_commit_top_refresh_filename(BlockDriverState *bs)
dcbf37ce 215{
dcbf37ce
KW
216 pstrcpy(bs->exact_filename, sizeof(bs->exact_filename),
217 bs->backing->bs->filename);
218}
91965658 219
8dfba279
KW
220static void bdrv_commit_top_child_perm(BlockDriverState *bs, BdrvChild *c,
221 const BdrvChildRole *role,
e0995dc3 222 BlockReopenQueue *reopen_queue,
8dfba279
KW
223 uint64_t perm, uint64_t shared,
224 uint64_t *nperm, uint64_t *nshared)
225{
226 *nperm = 0;
227 *nshared = BLK_PERM_ALL;
228}
229
230/* Dummy node that provides consistent read to its users without requiring it
231 * from its backing file and that allows writes on the backing file chain. */
232static BlockDriver bdrv_commit_top = {
91965658
KW
233 .format_name = "commit_top",
234 .bdrv_co_preadv = bdrv_commit_top_preadv,
3e4d0e72 235 .bdrv_co_block_status = bdrv_co_block_status_from_backing,
dcbf37ce 236 .bdrv_refresh_filename = bdrv_commit_top_refresh_filename,
91965658 237 .bdrv_child_perm = bdrv_commit_top_child_perm,
8dfba279
KW
238};
239
fd62c609 240void commit_start(const char *job_id, BlockDriverState *bs,
5360782d
JS
241 BlockDriverState *base, BlockDriverState *top,
242 int creation_flags, int64_t speed,
8254b6d9 243 BlockdevOnError on_error, const char *backing_file_str,
0db832f4 244 const char *filter_node_name, Error **errp)
747ff602
JC
245{
246 CommitBlockJob *s;
3e4c5122 247 BlockDriverState *iter;
8dfba279 248 BlockDriverState *commit_top_bs = NULL;
747ff602 249 Error *local_err = NULL;
d7086422 250 int ret;
747ff602 251
18da7f94 252 assert(top != bs);
747ff602
JC
253 if (top == base) {
254 error_setg(errp, "Invalid files for merge: top and base are the same");
255 return;
256 }
257
75859b94 258 s = block_job_create(job_id, &commit_job_driver, NULL, bs, 0, BLK_PERM_ALL,
5360782d 259 speed, creation_flags, NULL, NULL, errp);
834fe28d
AG
260 if (!s) {
261 return;
262 }
263
bde70715 264 /* convert base to r/w, if necessary */
e70cdc57
AG
265 s->base_read_only = bdrv_is_read_only(base);
266 if (s->base_read_only) {
267 if (bdrv_reopen_set_read_only(base, false, errp) != 0) {
d7086422 268 goto fail;
747ff602
JC
269 }
270 }
271
8dfba279
KW
272 /* Insert commit_top block node above top, so we can block consistent read
273 * on the backing chain below it */
0db832f4
KW
274 commit_top_bs = bdrv_new_open_driver(&bdrv_commit_top, filter_node_name, 0,
275 errp);
8dfba279
KW
276 if (commit_top_bs == NULL) {
277 goto fail;
278 }
d3c8c674
KW
279 if (!filter_node_name) {
280 commit_top_bs->implicit = true;
281 }
e5182c1c
HR
282
283 /* So that we can always drop this node */
284 commit_top_bs->never_freeze = true;
285
0d0676a1 286 commit_top_bs->total_sectors = top->total_sectors;
8dfba279 287
6a634199 288 bdrv_append(commit_top_bs, top, &local_err);
b69f00dd 289 if (local_err) {
b69f00dd
FZ
290 commit_top_bs = NULL;
291 error_propagate(errp, local_err);
292 goto fail;
293 }
8dfba279
KW
294
295 s->commit_top_bs = commit_top_bs;
747ff602 296
3e4c5122
AG
297 /* Block all nodes between top and base, because they will
298 * disappear from the chain after this operation. */
299 assert(bdrv_chain_contains(top, base));
8dfba279
KW
300 for (iter = top; iter != base; iter = backing_bs(iter)) {
301 /* XXX BLK_PERM_WRITE needs to be allowed so we don't block ourselves
302 * at s->base (if writes are blocked for a node, they are also blocked
303 * for its backing file). The other options would be a second filter
304 * driver above s->base. */
305 ret = block_job_add_bdrv(&s->common, "intermediate node", iter, 0,
306 BLK_PERM_WRITE_UNCHANGED | BLK_PERM_WRITE,
307 errp);
308 if (ret < 0) {
309 goto fail;
310 }
3e4c5122 311 }
8dfba279 312
df827336
AG
313 if (bdrv_freeze_backing_chain(commit_top_bs, base, errp) < 0) {
314 goto fail;
315 }
316 s->chain_frozen = true;
317
8dfba279
KW
318 ret = block_job_add_bdrv(&s->common, "base", base, 0, BLK_PERM_ALL, errp);
319 if (ret < 0) {
320 goto fail;
321 }
322
d861ab3a
KW
323 s->base = blk_new(s->common.job.aio_context,
324 BLK_PERM_CONSISTENT_READ
8dfba279
KW
325 | BLK_PERM_WRITE
326 | BLK_PERM_RESIZE,
327 BLK_PERM_CONSISTENT_READ
328 | BLK_PERM_GRAPH_MOD
329 | BLK_PERM_WRITE_UNCHANGED);
d7086422
KW
330 ret = blk_insert_bs(s->base, base, errp);
331 if (ret < 0) {
332 goto fail;
333 }
cf312932 334 blk_set_disable_request_queuing(s->base, true);
22dffcbe 335 s->base_bs = base;
4653456a 336
8dfba279 337 /* Required permissions are already taken with block_job_add_bdrv() */
d861ab3a 338 s->top = blk_new(s->common.job.aio_context, 0, BLK_PERM_ALL);
b247767a 339 ret = blk_insert_bs(s->top, top, errp);
d7086422
KW
340 if (ret < 0) {
341 goto fail;
342 }
cf312932 343 blk_set_disable_request_queuing(s->top, true);
4653456a 344
54e26900 345 s->backing_file_str = g_strdup(backing_file_str);
747ff602 346 s->on_error = on_error;
747ff602 347
5ccac6f1 348 trace_commit_start(bs, base, top, s);
da01ff7f 349 job_start(&s->common.job);
d7086422
KW
350 return;
351
352fail:
df827336
AG
353 if (s->chain_frozen) {
354 bdrv_unfreeze_backing_chain(commit_top_bs, base);
355 }
d7086422
KW
356 if (s->base) {
357 blk_unref(s->base);
358 }
359 if (s->top) {
360 blk_unref(s->top);
361 }
065abf9f
AG
362 if (s->base_read_only) {
363 bdrv_reopen_set_read_only(base, true, NULL);
364 }
2468eed3
AG
365 job_early_fail(&s->common.job);
366 /* commit_top_bs has to be replaced after deleting the block job,
367 * otherwise this would fail because of lack of permissions. */
8dfba279 368 if (commit_top_bs) {
bde70715 369 bdrv_replace_node(commit_top_bs, top, &error_abort);
8dfba279 370 }
747ff602 371}
83fd6dd3
KW
372
373
d6a644bb 374#define COMMIT_BUF_SIZE (2048 * BDRV_SECTOR_SIZE)
83fd6dd3
KW
375
376/* commit COW file into the raw image */
377int bdrv_commit(BlockDriverState *bs)
378{
f8e2bd53 379 BlockBackend *src, *backing;
d3f06759
KW
380 BlockDriverState *backing_file_bs = NULL;
381 BlockDriverState *commit_top_bs = NULL;
83fd6dd3 382 BlockDriver *drv = bs->drv;
d861ab3a 383 AioContext *ctx;
d6a644bb 384 int64_t offset, length, backing_length;
c742a364 385 int ro;
d6a644bb 386 int64_t n;
83fd6dd3
KW
387 int ret = 0;
388 uint8_t *buf = NULL;
d3f06759 389 Error *local_err = NULL;
83fd6dd3
KW
390
391 if (!drv)
392 return -ENOMEDIUM;
393
394 if (!bs->backing) {
395 return -ENOTSUP;
396 }
397
398 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_COMMIT_SOURCE, NULL) ||
399 bdrv_op_is_blocked(bs->backing->bs, BLOCK_OP_TYPE_COMMIT_TARGET, NULL)) {
400 return -EBUSY;
401 }
402
403 ro = bs->backing->bs->read_only;
83fd6dd3
KW
404
405 if (ro) {
c742a364 406 if (bdrv_reopen_set_read_only(bs->backing->bs, false, NULL)) {
83fd6dd3
KW
407 return -EACCES;
408 }
409 }
410
d861ab3a
KW
411 ctx = bdrv_get_aio_context(bs);
412 src = blk_new(ctx, BLK_PERM_CONSISTENT_READ, BLK_PERM_ALL);
413 backing = blk_new(ctx, BLK_PERM_WRITE | BLK_PERM_RESIZE, BLK_PERM_ALL);
d7086422 414
d3f06759 415 ret = blk_insert_bs(src, bs, &local_err);
d7086422 416 if (ret < 0) {
d3f06759
KW
417 error_report_err(local_err);
418 goto ro_cleanup;
419 }
420
421 /* Insert commit_top block node above backing, so we can write to it */
422 backing_file_bs = backing_bs(bs);
423
424 commit_top_bs = bdrv_new_open_driver(&bdrv_commit_top, NULL, BDRV_O_RDWR,
425 &local_err);
426 if (commit_top_bs == NULL) {
427 error_report_err(local_err);
d7086422
KW
428 goto ro_cleanup;
429 }
430
12fa4af6
KW
431 bdrv_set_backing_hd(commit_top_bs, backing_file_bs, &error_abort);
432 bdrv_set_backing_hd(bs, commit_top_bs, &error_abort);
d3f06759
KW
433
434 ret = blk_insert_bs(backing, backing_file_bs, &local_err);
d7086422 435 if (ret < 0) {
d3f06759 436 error_report_err(local_err);
d7086422
KW
437 goto ro_cleanup;
438 }
f8e2bd53
KW
439
440 length = blk_getlength(src);
83fd6dd3
KW
441 if (length < 0) {
442 ret = length;
443 goto ro_cleanup;
444 }
445
f8e2bd53 446 backing_length = blk_getlength(backing);
83fd6dd3
KW
447 if (backing_length < 0) {
448 ret = backing_length;
449 goto ro_cleanup;
450 }
451
452 /* If our top snapshot is larger than the backing file image,
453 * grow the backing file image if possible. If not possible,
454 * we must return an error */
455 if (length > backing_length) {
c80d8b06
HR
456 ret = blk_truncate(backing, length, false, PREALLOC_MODE_OFF,
457 &local_err);
83fd6dd3 458 if (ret < 0) {
ed3d2ec9 459 error_report_err(local_err);
83fd6dd3
KW
460 goto ro_cleanup;
461 }
462 }
463
f8e2bd53
KW
464 /* blk_try_blockalign() for src will choose an alignment that works for
465 * backing as well, so no need to compare the alignment manually. */
d6a644bb 466 buf = blk_try_blockalign(src, COMMIT_BUF_SIZE);
83fd6dd3
KW
467 if (buf == NULL) {
468 ret = -ENOMEM;
469 goto ro_cleanup;
470 }
471
d6a644bb
EB
472 for (offset = 0; offset < length; offset += n) {
473 ret = bdrv_is_allocated(bs, offset, COMMIT_BUF_SIZE, &n);
83fd6dd3
KW
474 if (ret < 0) {
475 goto ro_cleanup;
476 }
477 if (ret) {
d6a644bb 478 ret = blk_pread(src, offset, buf, n);
83fd6dd3
KW
479 if (ret < 0) {
480 goto ro_cleanup;
481 }
482
d6a644bb 483 ret = blk_pwrite(backing, offset, buf, n, 0);
83fd6dd3
KW
484 if (ret < 0) {
485 goto ro_cleanup;
486 }
487 }
488 }
489
490 if (drv->bdrv_make_empty) {
491 ret = drv->bdrv_make_empty(bs);
492 if (ret < 0) {
493 goto ro_cleanup;
494 }
f8e2bd53 495 blk_flush(src);
83fd6dd3
KW
496 }
497
498 /*
499 * Make sure all data we wrote to the backing device is actually
500 * stable on disk.
501 */
f8e2bd53 502 blk_flush(backing);
83fd6dd3
KW
503
504 ret = 0;
505ro_cleanup:
506 qemu_vfree(buf);
507
f8e2bd53 508 blk_unref(backing);
d3f06759 509 if (backing_file_bs) {
12fa4af6 510 bdrv_set_backing_hd(bs, backing_file_bs, &error_abort);
d3f06759
KW
511 }
512 bdrv_unref(commit_top_bs);
513 blk_unref(src);
f8e2bd53 514
83fd6dd3
KW
515 if (ro) {
516 /* ignoring error return here */
c742a364 517 bdrv_reopen_set_read_only(bs->backing->bs, true, NULL);
83fd6dd3
KW
518 }
519
520 return ret;
521}