commit: Implement .bdrv_refresh_filename
[qemu/ar7.git] / block / commit.c
blob28324820a40cdfbef45a6c3df6b295feacbb289c
1 /*
2 * Live block commit
4 * Copyright Red Hat, Inc. 2012
6 * Authors:
7 * Jeff Cody <jcody@redhat.com>
8 * Based on stream.c by Stefan Hajnoczi
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.
15 #include "qemu/osdep.h"
16 #include "qemu/cutils.h"
17 #include "trace.h"
18 #include "block/block_int.h"
19 #include "block/blockjob_int.h"
20 #include "qapi/error.h"
21 #include "qapi/qmp/qerror.h"
22 #include "qemu/ratelimit.h"
23 #include "sysemu/block-backend.h"
25 enum {
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.
31 COMMIT_BUFFER_SIZE = 512 * 1024, /* in bytes */
34 #define SLICE_TIME 100000000ULL /* ns */
36 typedef struct CommitBlockJob {
37 BlockJob common;
38 RateLimit limit;
39 BlockDriverState *active;
40 BlockDriverState *commit_top_bs;
41 BlockBackend *top;
42 BlockBackend *base;
43 BlockdevOnError on_error;
44 int base_flags;
45 int orig_overlay_flags;
46 char *backing_file_str;
47 } CommitBlockJob;
49 static int coroutine_fn commit_populate(BlockBackend *bs, BlockBackend *base,
50 int64_t sector_num, int nb_sectors,
51 void *buf)
53 int ret = 0;
54 QEMUIOVector qiov;
55 struct iovec iov = {
56 .iov_base = buf,
57 .iov_len = nb_sectors * BDRV_SECTOR_SIZE,
60 qemu_iovec_init_external(&qiov, &iov, 1);
62 ret = blk_co_preadv(bs, sector_num * BDRV_SECTOR_SIZE,
63 qiov.size, &qiov, 0);
64 if (ret < 0) {
65 return ret;
68 ret = blk_co_pwritev(base, sector_num * BDRV_SECTOR_SIZE,
69 qiov.size, &qiov, 0);
70 if (ret < 0) {
71 return ret;
74 return 0;
77 typedef struct {
78 int ret;
79 } CommitCompleteData;
81 static void commit_complete(BlockJob *job, void *opaque)
83 CommitBlockJob *s = container_of(job, CommitBlockJob, common);
84 CommitCompleteData *data = opaque;
85 BlockDriverState *active = s->active;
86 BlockDriverState *top = blk_bs(s->top);
87 BlockDriverState *base = blk_bs(s->base);
88 BlockDriverState *overlay_bs = bdrv_find_overlay(active, s->commit_top_bs);
89 int ret = data->ret;
90 bool remove_commit_top_bs = false;
92 /* Remove base node parent that still uses BLK_PERM_WRITE/RESIZE before
93 * the normal backing chain can be restored. */
94 blk_unref(s->base);
96 if (!block_job_is_cancelled(&s->common) && ret == 0) {
97 /* success */
98 ret = bdrv_drop_intermediate(active, s->commit_top_bs, base,
99 s->backing_file_str);
100 } else if (overlay_bs) {
101 /* XXX Can (or should) we somehow keep 'consistent read' blocked even
102 * after the failed/cancelled commit job is gone? If we already wrote
103 * something to base, the intermediate images aren't valid any more. */
104 remove_commit_top_bs = true;
107 /* restore base open flags here if appropriate (e.g., change the base back
108 * to r/o). These reopens do not need to be atomic, since we won't abort
109 * even on failure here */
110 if (s->base_flags != bdrv_get_flags(base)) {
111 bdrv_reopen(base, s->base_flags, NULL);
113 if (overlay_bs && s->orig_overlay_flags != bdrv_get_flags(overlay_bs)) {
114 bdrv_reopen(overlay_bs, s->orig_overlay_flags, NULL);
116 g_free(s->backing_file_str);
117 blk_unref(s->top);
118 block_job_completed(&s->common, ret);
119 g_free(data);
121 /* If bdrv_drop_intermediate() didn't already do that, remove the commit
122 * filter driver from the backing chain. Do this as the final step so that
123 * the 'consistent read' permission can be granted. */
124 if (remove_commit_top_bs) {
125 bdrv_set_backing_hd(overlay_bs, top, &error_abort);
129 static void coroutine_fn commit_run(void *opaque)
131 CommitBlockJob *s = opaque;
132 CommitCompleteData *data;
133 int64_t sector_num, end;
134 uint64_t delay_ns = 0;
135 int ret = 0;
136 int n = 0;
137 void *buf = NULL;
138 int bytes_written = 0;
139 int64_t base_len;
141 ret = s->common.len = blk_getlength(s->top);
144 if (s->common.len < 0) {
145 goto out;
148 ret = base_len = blk_getlength(s->base);
149 if (base_len < 0) {
150 goto out;
153 if (base_len < s->common.len) {
154 ret = blk_truncate(s->base, s->common.len);
155 if (ret) {
156 goto out;
160 end = s->common.len >> BDRV_SECTOR_BITS;
161 buf = blk_blockalign(s->top, COMMIT_BUFFER_SIZE);
163 for (sector_num = 0; sector_num < end; sector_num += n) {
164 bool copy;
166 /* Note that even when no rate limit is applied we need to yield
167 * with no pending I/O here so that bdrv_drain_all() returns.
169 block_job_sleep_ns(&s->common, QEMU_CLOCK_REALTIME, delay_ns);
170 if (block_job_is_cancelled(&s->common)) {
171 break;
173 /* Copy if allocated above the base */
174 ret = bdrv_is_allocated_above(blk_bs(s->top), blk_bs(s->base),
175 sector_num,
176 COMMIT_BUFFER_SIZE / BDRV_SECTOR_SIZE,
177 &n);
178 copy = (ret == 1);
179 trace_commit_one_iteration(s, sector_num, n, ret);
180 if (copy) {
181 ret = commit_populate(s->top, s->base, sector_num, n, buf);
182 bytes_written += n * BDRV_SECTOR_SIZE;
184 if (ret < 0) {
185 BlockErrorAction action =
186 block_job_error_action(&s->common, false, s->on_error, -ret);
187 if (action == BLOCK_ERROR_ACTION_REPORT) {
188 goto out;
189 } else {
190 n = 0;
191 continue;
194 /* Publish progress */
195 s->common.offset += n * BDRV_SECTOR_SIZE;
197 if (copy && s->common.speed) {
198 delay_ns = ratelimit_calculate_delay(&s->limit, n);
202 ret = 0;
204 out:
205 qemu_vfree(buf);
207 data = g_malloc(sizeof(*data));
208 data->ret = ret;
209 block_job_defer_to_main_loop(&s->common, commit_complete, data);
212 static void commit_set_speed(BlockJob *job, int64_t speed, Error **errp)
214 CommitBlockJob *s = container_of(job, CommitBlockJob, common);
216 if (speed < 0) {
217 error_setg(errp, QERR_INVALID_PARAMETER, "speed");
218 return;
220 ratelimit_set_speed(&s->limit, speed / BDRV_SECTOR_SIZE, SLICE_TIME);
223 static const BlockJobDriver commit_job_driver = {
224 .instance_size = sizeof(CommitBlockJob),
225 .job_type = BLOCK_JOB_TYPE_COMMIT,
226 .set_speed = commit_set_speed,
227 .start = commit_run,
230 static int coroutine_fn bdrv_commit_top_preadv(BlockDriverState *bs,
231 uint64_t offset, uint64_t bytes, QEMUIOVector *qiov, int flags)
233 return bdrv_co_preadv(bs->backing, offset, bytes, qiov, flags);
236 static int64_t coroutine_fn bdrv_commit_top_get_block_status(
237 BlockDriverState *bs, int64_t sector_num, int nb_sectors, int *pnum,
238 BlockDriverState **file)
240 *pnum = nb_sectors;
241 *file = bs->backing->bs;
242 return BDRV_BLOCK_RAW | BDRV_BLOCK_OFFSET_VALID | BDRV_BLOCK_DATA |
243 (sector_num << BDRV_SECTOR_BITS);
246 static void bdrv_commit_top_refresh_filename(BlockDriverState *bs, QDict *opts)
248 bdrv_refresh_filename(bs->backing->bs);
249 pstrcpy(bs->exact_filename, sizeof(bs->exact_filename),
250 bs->backing->bs->filename);
253 static void bdrv_commit_top_close(BlockDriverState *bs)
257 static void bdrv_commit_top_child_perm(BlockDriverState *bs, BdrvChild *c,
258 const BdrvChildRole *role,
259 uint64_t perm, uint64_t shared,
260 uint64_t *nperm, uint64_t *nshared)
262 *nperm = 0;
263 *nshared = BLK_PERM_ALL;
266 /* Dummy node that provides consistent read to its users without requiring it
267 * from its backing file and that allows writes on the backing file chain. */
268 static BlockDriver bdrv_commit_top = {
269 .format_name = "commit_top",
270 .bdrv_co_preadv = bdrv_commit_top_preadv,
271 .bdrv_co_get_block_status = bdrv_commit_top_get_block_status,
272 .bdrv_refresh_filename = bdrv_commit_top_refresh_filename,
273 .bdrv_close = bdrv_commit_top_close,
274 .bdrv_child_perm = bdrv_commit_top_child_perm,
277 void commit_start(const char *job_id, BlockDriverState *bs,
278 BlockDriverState *base, BlockDriverState *top, int64_t speed,
279 BlockdevOnError on_error, const char *backing_file_str,
280 const char *filter_node_name, Error **errp)
282 CommitBlockJob *s;
283 BlockReopenQueue *reopen_queue = NULL;
284 int orig_overlay_flags;
285 int orig_base_flags;
286 BlockDriverState *iter;
287 BlockDriverState *overlay_bs;
288 BlockDriverState *commit_top_bs = NULL;
289 Error *local_err = NULL;
290 int ret;
292 assert(top != bs);
293 if (top == base) {
294 error_setg(errp, "Invalid files for merge: top and base are the same");
295 return;
298 overlay_bs = bdrv_find_overlay(bs, top);
300 if (overlay_bs == NULL) {
301 error_setg(errp, "Could not find overlay image for %s:", top->filename);
302 return;
305 s = block_job_create(job_id, &commit_job_driver, bs, 0, BLK_PERM_ALL,
306 speed, BLOCK_JOB_DEFAULT, NULL, NULL, errp);
307 if (!s) {
308 return;
311 orig_base_flags = bdrv_get_flags(base);
312 orig_overlay_flags = bdrv_get_flags(overlay_bs);
314 /* convert base & overlay_bs to r/w, if necessary */
315 if (!(orig_base_flags & BDRV_O_RDWR)) {
316 reopen_queue = bdrv_reopen_queue(reopen_queue, base, NULL,
317 orig_base_flags | BDRV_O_RDWR);
319 if (!(orig_overlay_flags & BDRV_O_RDWR)) {
320 reopen_queue = bdrv_reopen_queue(reopen_queue, overlay_bs, NULL,
321 orig_overlay_flags | BDRV_O_RDWR);
323 if (reopen_queue) {
324 bdrv_reopen_multiple(bdrv_get_aio_context(bs), reopen_queue, &local_err);
325 if (local_err != NULL) {
326 error_propagate(errp, local_err);
327 goto fail;
331 /* Insert commit_top block node above top, so we can block consistent read
332 * on the backing chain below it */
333 commit_top_bs = bdrv_new_open_driver(&bdrv_commit_top, filter_node_name, 0,
334 errp);
335 if (commit_top_bs == NULL) {
336 goto fail;
339 bdrv_set_backing_hd(commit_top_bs, top, &local_err);
340 if (local_err) {
341 bdrv_unref(commit_top_bs);
342 commit_top_bs = NULL;
343 error_propagate(errp, local_err);
344 goto fail;
346 bdrv_set_backing_hd(overlay_bs, commit_top_bs, &local_err);
347 if (local_err) {
348 bdrv_unref(commit_top_bs);
349 commit_top_bs = NULL;
350 error_propagate(errp, local_err);
351 goto fail;
354 s->commit_top_bs = commit_top_bs;
355 bdrv_unref(commit_top_bs);
357 /* Block all nodes between top and base, because they will
358 * disappear from the chain after this operation. */
359 assert(bdrv_chain_contains(top, base));
360 for (iter = top; iter != base; iter = backing_bs(iter)) {
361 /* XXX BLK_PERM_WRITE needs to be allowed so we don't block ourselves
362 * at s->base (if writes are blocked for a node, they are also blocked
363 * for its backing file). The other options would be a second filter
364 * driver above s->base. */
365 ret = block_job_add_bdrv(&s->common, "intermediate node", iter, 0,
366 BLK_PERM_WRITE_UNCHANGED | BLK_PERM_WRITE,
367 errp);
368 if (ret < 0) {
369 goto fail;
373 ret = block_job_add_bdrv(&s->common, "base", base, 0, BLK_PERM_ALL, errp);
374 if (ret < 0) {
375 goto fail;
378 /* overlay_bs must be blocked because it needs to be modified to
379 * update the backing image string. */
380 ret = block_job_add_bdrv(&s->common, "overlay of top", overlay_bs,
381 BLK_PERM_GRAPH_MOD, BLK_PERM_ALL, errp);
382 if (ret < 0) {
383 goto fail;
386 s->base = blk_new(BLK_PERM_CONSISTENT_READ
387 | BLK_PERM_WRITE
388 | BLK_PERM_RESIZE,
389 BLK_PERM_CONSISTENT_READ
390 | BLK_PERM_GRAPH_MOD
391 | BLK_PERM_WRITE_UNCHANGED);
392 ret = blk_insert_bs(s->base, base, errp);
393 if (ret < 0) {
394 goto fail;
397 /* Required permissions are already taken with block_job_add_bdrv() */
398 s->top = blk_new(0, BLK_PERM_ALL);
399 ret = blk_insert_bs(s->top, top, errp);
400 if (ret < 0) {
401 goto fail;
404 s->active = bs;
406 s->base_flags = orig_base_flags;
407 s->orig_overlay_flags = orig_overlay_flags;
409 s->backing_file_str = g_strdup(backing_file_str);
411 s->on_error = on_error;
413 trace_commit_start(bs, base, top, s);
414 block_job_start(&s->common);
415 return;
417 fail:
418 if (s->base) {
419 blk_unref(s->base);
421 if (s->top) {
422 blk_unref(s->top);
424 if (commit_top_bs) {
425 bdrv_set_backing_hd(overlay_bs, top, &error_abort);
427 block_job_unref(&s->common);
431 #define COMMIT_BUF_SECTORS 2048
433 /* commit COW file into the raw image */
434 int bdrv_commit(BlockDriverState *bs)
436 BlockBackend *src, *backing;
437 BlockDriverState *backing_file_bs = NULL;
438 BlockDriverState *commit_top_bs = NULL;
439 BlockDriver *drv = bs->drv;
440 int64_t sector, total_sectors, length, backing_length;
441 int n, ro, open_flags;
442 int ret = 0;
443 uint8_t *buf = NULL;
444 Error *local_err = NULL;
446 if (!drv)
447 return -ENOMEDIUM;
449 if (!bs->backing) {
450 return -ENOTSUP;
453 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_COMMIT_SOURCE, NULL) ||
454 bdrv_op_is_blocked(bs->backing->bs, BLOCK_OP_TYPE_COMMIT_TARGET, NULL)) {
455 return -EBUSY;
458 ro = bs->backing->bs->read_only;
459 open_flags = bs->backing->bs->open_flags;
461 if (ro) {
462 if (bdrv_reopen(bs->backing->bs, open_flags | BDRV_O_RDWR, NULL)) {
463 return -EACCES;
467 src = blk_new(BLK_PERM_CONSISTENT_READ, BLK_PERM_ALL);
468 backing = blk_new(BLK_PERM_WRITE | BLK_PERM_RESIZE, BLK_PERM_ALL);
470 ret = blk_insert_bs(src, bs, &local_err);
471 if (ret < 0) {
472 error_report_err(local_err);
473 goto ro_cleanup;
476 /* Insert commit_top block node above backing, so we can write to it */
477 backing_file_bs = backing_bs(bs);
479 commit_top_bs = bdrv_new_open_driver(&bdrv_commit_top, NULL, BDRV_O_RDWR,
480 &local_err);
481 if (commit_top_bs == NULL) {
482 error_report_err(local_err);
483 goto ro_cleanup;
486 bdrv_set_backing_hd(commit_top_bs, backing_file_bs, &error_abort);
487 bdrv_set_backing_hd(bs, commit_top_bs, &error_abort);
489 ret = blk_insert_bs(backing, backing_file_bs, &local_err);
490 if (ret < 0) {
491 error_report_err(local_err);
492 goto ro_cleanup;
495 length = blk_getlength(src);
496 if (length < 0) {
497 ret = length;
498 goto ro_cleanup;
501 backing_length = blk_getlength(backing);
502 if (backing_length < 0) {
503 ret = backing_length;
504 goto ro_cleanup;
507 /* If our top snapshot is larger than the backing file image,
508 * grow the backing file image if possible. If not possible,
509 * we must return an error */
510 if (length > backing_length) {
511 ret = blk_truncate(backing, length);
512 if (ret < 0) {
513 goto ro_cleanup;
517 total_sectors = length >> BDRV_SECTOR_BITS;
519 /* blk_try_blockalign() for src will choose an alignment that works for
520 * backing as well, so no need to compare the alignment manually. */
521 buf = blk_try_blockalign(src, COMMIT_BUF_SECTORS * BDRV_SECTOR_SIZE);
522 if (buf == NULL) {
523 ret = -ENOMEM;
524 goto ro_cleanup;
527 for (sector = 0; sector < total_sectors; sector += n) {
528 ret = bdrv_is_allocated(bs, sector, COMMIT_BUF_SECTORS, &n);
529 if (ret < 0) {
530 goto ro_cleanup;
532 if (ret) {
533 ret = blk_pread(src, sector * BDRV_SECTOR_SIZE, buf,
534 n * BDRV_SECTOR_SIZE);
535 if (ret < 0) {
536 goto ro_cleanup;
539 ret = blk_pwrite(backing, sector * BDRV_SECTOR_SIZE, buf,
540 n * BDRV_SECTOR_SIZE, 0);
541 if (ret < 0) {
542 goto ro_cleanup;
547 if (drv->bdrv_make_empty) {
548 ret = drv->bdrv_make_empty(bs);
549 if (ret < 0) {
550 goto ro_cleanup;
552 blk_flush(src);
556 * Make sure all data we wrote to the backing device is actually
557 * stable on disk.
559 blk_flush(backing);
561 ret = 0;
562 ro_cleanup:
563 qemu_vfree(buf);
565 blk_unref(backing);
566 if (backing_file_bs) {
567 bdrv_set_backing_hd(bs, backing_file_bs, &error_abort);
569 bdrv_unref(commit_top_bs);
570 blk_unref(src);
572 if (ro) {
573 /* ignoring error return here */
574 bdrv_reopen(bs->backing->bs, open_flags & ~BDRV_O_RDWR, NULL);
577 return ret;