qemu-char: minor mux chardev fixes
[qemu.git] / block / backup.c
blob16105d40b193be9bb40346027bdf58e62b956a96
1 /*
2 * QEMU backup
4 * Copyright (C) 2013 Proxmox Server Solutions
6 * Authors:
7 * Dietmar Maurer (dietmar@proxmox.com)
9 * This work is licensed under the terms of the GNU GPL, version 2 or later.
10 * See the COPYING file in the top-level directory.
14 #include <stdio.h>
15 #include <errno.h>
16 #include <unistd.h>
18 #include "trace.h"
19 #include "block/block.h"
20 #include "block/block_int.h"
21 #include "block/blockjob.h"
22 #include "qemu/ratelimit.h"
24 #define BACKUP_CLUSTER_BITS 16
25 #define BACKUP_CLUSTER_SIZE (1 << BACKUP_CLUSTER_BITS)
26 #define BACKUP_SECTORS_PER_CLUSTER (BACKUP_CLUSTER_SIZE / BDRV_SECTOR_SIZE)
28 #define SLICE_TIME 100000000ULL /* ns */
30 typedef struct CowRequest {
31 int64_t start;
32 int64_t end;
33 QLIST_ENTRY(CowRequest) list;
34 CoQueue wait_queue; /* coroutines blocked on this request */
35 } CowRequest;
37 typedef struct BackupBlockJob {
38 BlockJob common;
39 BlockDriverState *target;
40 RateLimit limit;
41 BlockdevOnError on_source_error;
42 BlockdevOnError on_target_error;
43 CoRwlock flush_rwlock;
44 uint64_t sectors_read;
45 HBitmap *bitmap;
46 QLIST_HEAD(, CowRequest) inflight_reqs;
47 } BackupBlockJob;
49 /* See if in-flight requests overlap and wait for them to complete */
50 static void coroutine_fn wait_for_overlapping_requests(BackupBlockJob *job,
51 int64_t start,
52 int64_t end)
54 CowRequest *req;
55 bool retry;
57 do {
58 retry = false;
59 QLIST_FOREACH(req, &job->inflight_reqs, list) {
60 if (end > req->start && start < req->end) {
61 qemu_co_queue_wait(&req->wait_queue);
62 retry = true;
63 break;
66 } while (retry);
69 /* Keep track of an in-flight request */
70 static void cow_request_begin(CowRequest *req, BackupBlockJob *job,
71 int64_t start, int64_t end)
73 req->start = start;
74 req->end = end;
75 qemu_co_queue_init(&req->wait_queue);
76 QLIST_INSERT_HEAD(&job->inflight_reqs, req, list);
79 /* Forget about a completed request */
80 static void cow_request_end(CowRequest *req)
82 QLIST_REMOVE(req, list);
83 qemu_co_queue_restart_all(&req->wait_queue);
86 static int coroutine_fn backup_do_cow(BlockDriverState *bs,
87 int64_t sector_num, int nb_sectors,
88 bool *error_is_read)
90 BackupBlockJob *job = (BackupBlockJob *)bs->job;
91 CowRequest cow_request;
92 struct iovec iov;
93 QEMUIOVector bounce_qiov;
94 void *bounce_buffer = NULL;
95 int ret = 0;
96 int64_t start, end;
97 int n;
99 qemu_co_rwlock_rdlock(&job->flush_rwlock);
101 start = sector_num / BACKUP_SECTORS_PER_CLUSTER;
102 end = DIV_ROUND_UP(sector_num + nb_sectors, BACKUP_SECTORS_PER_CLUSTER);
104 trace_backup_do_cow_enter(job, start, sector_num, nb_sectors);
106 wait_for_overlapping_requests(job, start, end);
107 cow_request_begin(&cow_request, job, start, end);
109 for (; start < end; start++) {
110 if (hbitmap_get(job->bitmap, start)) {
111 trace_backup_do_cow_skip(job, start);
112 continue; /* already copied */
115 trace_backup_do_cow_process(job, start);
117 n = MIN(BACKUP_SECTORS_PER_CLUSTER,
118 job->common.len / BDRV_SECTOR_SIZE -
119 start * BACKUP_SECTORS_PER_CLUSTER);
121 if (!bounce_buffer) {
122 bounce_buffer = qemu_blockalign(bs, BACKUP_CLUSTER_SIZE);
124 iov.iov_base = bounce_buffer;
125 iov.iov_len = n * BDRV_SECTOR_SIZE;
126 qemu_iovec_init_external(&bounce_qiov, &iov, 1);
128 ret = bdrv_co_readv(bs, start * BACKUP_SECTORS_PER_CLUSTER, n,
129 &bounce_qiov);
130 if (ret < 0) {
131 trace_backup_do_cow_read_fail(job, start, ret);
132 if (error_is_read) {
133 *error_is_read = true;
135 goto out;
138 if (buffer_is_zero(iov.iov_base, iov.iov_len)) {
139 ret = bdrv_co_write_zeroes(job->target,
140 start * BACKUP_SECTORS_PER_CLUSTER, n);
141 } else {
142 ret = bdrv_co_writev(job->target,
143 start * BACKUP_SECTORS_PER_CLUSTER, n,
144 &bounce_qiov);
146 if (ret < 0) {
147 trace_backup_do_cow_write_fail(job, start, ret);
148 if (error_is_read) {
149 *error_is_read = false;
151 goto out;
154 hbitmap_set(job->bitmap, start, 1);
156 /* Publish progress, guest I/O counts as progress too. Note that the
157 * offset field is an opaque progress value, it is not a disk offset.
159 job->sectors_read += n;
160 job->common.offset += n * BDRV_SECTOR_SIZE;
163 out:
164 if (bounce_buffer) {
165 qemu_vfree(bounce_buffer);
168 cow_request_end(&cow_request);
170 trace_backup_do_cow_return(job, sector_num, nb_sectors, ret);
172 qemu_co_rwlock_unlock(&job->flush_rwlock);
174 return ret;
177 static int coroutine_fn backup_before_write_notify(
178 NotifierWithReturn *notifier,
179 void *opaque)
181 BdrvTrackedRequest *req = opaque;
183 return backup_do_cow(req->bs, req->sector_num, req->nb_sectors, NULL);
186 static void backup_set_speed(BlockJob *job, int64_t speed, Error **errp)
188 BackupBlockJob *s = container_of(job, BackupBlockJob, common);
190 if (speed < 0) {
191 error_set(errp, QERR_INVALID_PARAMETER, "speed");
192 return;
194 ratelimit_set_speed(&s->limit, speed / BDRV_SECTOR_SIZE, SLICE_TIME);
197 static void backup_iostatus_reset(BlockJob *job)
199 BackupBlockJob *s = container_of(job, BackupBlockJob, common);
201 bdrv_iostatus_reset(s->target);
204 static const BlockJobType backup_job_type = {
205 .instance_size = sizeof(BackupBlockJob),
206 .job_type = "backup",
207 .set_speed = backup_set_speed,
208 .iostatus_reset = backup_iostatus_reset,
211 static BlockErrorAction backup_error_action(BackupBlockJob *job,
212 bool read, int error)
214 if (read) {
215 return block_job_error_action(&job->common, job->common.bs,
216 job->on_source_error, true, error);
217 } else {
218 return block_job_error_action(&job->common, job->target,
219 job->on_target_error, false, error);
223 static void coroutine_fn backup_run(void *opaque)
225 BackupBlockJob *job = opaque;
226 BlockDriverState *bs = job->common.bs;
227 BlockDriverState *target = job->target;
228 BlockdevOnError on_target_error = job->on_target_error;
229 NotifierWithReturn before_write = {
230 .notify = backup_before_write_notify,
232 int64_t start, end;
233 int ret = 0;
235 QLIST_INIT(&job->inflight_reqs);
236 qemu_co_rwlock_init(&job->flush_rwlock);
238 start = 0;
239 end = DIV_ROUND_UP(job->common.len / BDRV_SECTOR_SIZE,
240 BACKUP_SECTORS_PER_CLUSTER);
242 job->bitmap = hbitmap_alloc(end, 0);
244 bdrv_set_enable_write_cache(target, true);
245 bdrv_set_on_error(target, on_target_error, on_target_error);
246 bdrv_iostatus_enable(target);
248 bdrv_add_before_write_notifier(bs, &before_write);
250 for (; start < end; start++) {
251 bool error_is_read;
253 if (block_job_is_cancelled(&job->common)) {
254 break;
257 /* we need to yield so that qemu_aio_flush() returns.
258 * (without, VM does not reboot)
260 if (job->common.speed) {
261 uint64_t delay_ns = ratelimit_calculate_delay(
262 &job->limit, job->sectors_read);
263 job->sectors_read = 0;
264 block_job_sleep_ns(&job->common, rt_clock, delay_ns);
265 } else {
266 block_job_sleep_ns(&job->common, rt_clock, 0);
269 if (block_job_is_cancelled(&job->common)) {
270 break;
273 ret = backup_do_cow(bs, start * BACKUP_SECTORS_PER_CLUSTER,
274 BACKUP_SECTORS_PER_CLUSTER, &error_is_read);
275 if (ret < 0) {
276 /* Depending on error action, fail now or retry cluster */
277 BlockErrorAction action =
278 backup_error_action(job, error_is_read, -ret);
279 if (action == BDRV_ACTION_REPORT) {
280 break;
281 } else {
282 start--;
283 continue;
288 notifier_with_return_remove(&before_write);
290 /* wait until pending backup_do_cow() calls have completed */
291 qemu_co_rwlock_wrlock(&job->flush_rwlock);
292 qemu_co_rwlock_unlock(&job->flush_rwlock);
294 hbitmap_free(job->bitmap);
296 bdrv_iostatus_disable(target);
297 bdrv_delete(target);
299 block_job_completed(&job->common, ret);
302 void backup_start(BlockDriverState *bs, BlockDriverState *target,
303 int64_t speed,
304 BlockdevOnError on_source_error,
305 BlockdevOnError on_target_error,
306 BlockDriverCompletionFunc *cb, void *opaque,
307 Error **errp)
309 int64_t len;
311 assert(bs);
312 assert(target);
313 assert(cb);
315 if ((on_source_error == BLOCKDEV_ON_ERROR_STOP ||
316 on_source_error == BLOCKDEV_ON_ERROR_ENOSPC) &&
317 !bdrv_iostatus_is_enabled(bs)) {
318 error_set(errp, QERR_INVALID_PARAMETER, "on-source-error");
319 return;
322 len = bdrv_getlength(bs);
323 if (len < 0) {
324 error_setg_errno(errp, -len, "unable to get length for '%s'",
325 bdrv_get_device_name(bs));
326 return;
329 BackupBlockJob *job = block_job_create(&backup_job_type, bs, speed,
330 cb, opaque, errp);
331 if (!job) {
332 return;
335 job->on_source_error = on_source_error;
336 job->on_target_error = on_target_error;
337 job->target = target;
338 job->common.len = len;
339 job->common.co = qemu_coroutine_create(backup_run);
340 qemu_coroutine_enter(job->common.co, job);