linux-user: Use safe_syscall for open and openat system calls
[qemu.git] / block / backup.c
1 /*
2 * QEMU backup
3 *
4 * Copyright (C) 2013 Proxmox Server Solutions
5 *
6 * Authors:
7 * Dietmar Maurer (dietmar@proxmox.com)
8 *
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.
11 *
12 */
13
14 #include "qemu/osdep.h"
15
16 #include "trace.h"
17 #include "block/block.h"
18 #include "block/block_int.h"
19 #include "block/blockjob.h"
20 #include "qapi/error.h"
21 #include "qapi/qmp/qerror.h"
22 #include "qemu/ratelimit.h"
23 #include "qemu/cutils.h"
24 #include "sysemu/block-backend.h"
25 #include "qemu/bitmap.h"
26
27 #define BACKUP_CLUSTER_SIZE_DEFAULT (1 << 16)
28 #define SLICE_TIME 100000000ULL /* ns */
29
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;
36
37 typedef struct BackupBlockJob {
38 BlockJob common;
39 BlockDriverState *target;
40 /* bitmap for sync=incremental */
41 BdrvDirtyBitmap *sync_bitmap;
42 MirrorSyncMode sync_mode;
43 RateLimit limit;
44 BlockdevOnError on_source_error;
45 BlockdevOnError on_target_error;
46 CoRwlock flush_rwlock;
47 uint64_t sectors_read;
48 unsigned long *done_bitmap;
49 int64_t cluster_size;
50 QLIST_HEAD(, CowRequest) inflight_reqs;
51 } BackupBlockJob;
52
53 /* Size of a cluster in sectors, instead of bytes. */
54 static inline int64_t cluster_size_sectors(BackupBlockJob *job)
55 {
56 return job->cluster_size / BDRV_SECTOR_SIZE;
57 }
58
59 /* See if in-flight requests overlap and wait for them to complete */
60 static void coroutine_fn wait_for_overlapping_requests(BackupBlockJob *job,
61 int64_t start,
62 int64_t end)
63 {
64 CowRequest *req;
65 bool retry;
66
67 do {
68 retry = false;
69 QLIST_FOREACH(req, &job->inflight_reqs, list) {
70 if (end > req->start && start < req->end) {
71 qemu_co_queue_wait(&req->wait_queue);
72 retry = true;
73 break;
74 }
75 }
76 } while (retry);
77 }
78
79 /* Keep track of an in-flight request */
80 static void cow_request_begin(CowRequest *req, BackupBlockJob *job,
81 int64_t start, int64_t end)
82 {
83 req->start = start;
84 req->end = end;
85 qemu_co_queue_init(&req->wait_queue);
86 QLIST_INSERT_HEAD(&job->inflight_reqs, req, list);
87 }
88
89 /* Forget about a completed request */
90 static void cow_request_end(CowRequest *req)
91 {
92 QLIST_REMOVE(req, list);
93 qemu_co_queue_restart_all(&req->wait_queue);
94 }
95
96 static int coroutine_fn backup_do_cow(BlockDriverState *bs,
97 int64_t sector_num, int nb_sectors,
98 bool *error_is_read,
99 bool is_write_notifier)
100 {
101 BackupBlockJob *job = (BackupBlockJob *)bs->job;
102 CowRequest cow_request;
103 struct iovec iov;
104 QEMUIOVector bounce_qiov;
105 void *bounce_buffer = NULL;
106 int ret = 0;
107 int64_t sectors_per_cluster = cluster_size_sectors(job);
108 int64_t start, end;
109 int n;
110
111 qemu_co_rwlock_rdlock(&job->flush_rwlock);
112
113 start = sector_num / sectors_per_cluster;
114 end = DIV_ROUND_UP(sector_num + nb_sectors, sectors_per_cluster);
115
116 trace_backup_do_cow_enter(job, start, sector_num, nb_sectors);
117
118 wait_for_overlapping_requests(job, start, end);
119 cow_request_begin(&cow_request, job, start, end);
120
121 for (; start < end; start++) {
122 if (test_bit(start, job->done_bitmap)) {
123 trace_backup_do_cow_skip(job, start);
124 continue; /* already copied */
125 }
126
127 trace_backup_do_cow_process(job, start);
128
129 n = MIN(sectors_per_cluster,
130 job->common.len / BDRV_SECTOR_SIZE -
131 start * sectors_per_cluster);
132
133 if (!bounce_buffer) {
134 bounce_buffer = qemu_blockalign(bs, job->cluster_size);
135 }
136 iov.iov_base = bounce_buffer;
137 iov.iov_len = n * BDRV_SECTOR_SIZE;
138 qemu_iovec_init_external(&bounce_qiov, &iov, 1);
139
140 if (is_write_notifier) {
141 ret = bdrv_co_readv_no_serialising(bs,
142 start * sectors_per_cluster,
143 n, &bounce_qiov);
144 } else {
145 ret = bdrv_co_readv(bs, start * sectors_per_cluster, n,
146 &bounce_qiov);
147 }
148 if (ret < 0) {
149 trace_backup_do_cow_read_fail(job, start, ret);
150 if (error_is_read) {
151 *error_is_read = true;
152 }
153 goto out;
154 }
155
156 if (buffer_is_zero(iov.iov_base, iov.iov_len)) {
157 ret = bdrv_co_write_zeroes(job->target,
158 start * sectors_per_cluster,
159 n, BDRV_REQ_MAY_UNMAP);
160 } else {
161 ret = bdrv_co_writev(job->target,
162 start * sectors_per_cluster, n,
163 &bounce_qiov);
164 }
165 if (ret < 0) {
166 trace_backup_do_cow_write_fail(job, start, ret);
167 if (error_is_read) {
168 *error_is_read = false;
169 }
170 goto out;
171 }
172
173 set_bit(start, job->done_bitmap);
174
175 /* Publish progress, guest I/O counts as progress too. Note that the
176 * offset field is an opaque progress value, it is not a disk offset.
177 */
178 job->sectors_read += n;
179 job->common.offset += n * BDRV_SECTOR_SIZE;
180 }
181
182 out:
183 if (bounce_buffer) {
184 qemu_vfree(bounce_buffer);
185 }
186
187 cow_request_end(&cow_request);
188
189 trace_backup_do_cow_return(job, sector_num, nb_sectors, ret);
190
191 qemu_co_rwlock_unlock(&job->flush_rwlock);
192
193 return ret;
194 }
195
196 static int coroutine_fn backup_before_write_notify(
197 NotifierWithReturn *notifier,
198 void *opaque)
199 {
200 BdrvTrackedRequest *req = opaque;
201 int64_t sector_num = req->offset >> BDRV_SECTOR_BITS;
202 int nb_sectors = req->bytes >> BDRV_SECTOR_BITS;
203
204 assert((req->offset & (BDRV_SECTOR_SIZE - 1)) == 0);
205 assert((req->bytes & (BDRV_SECTOR_SIZE - 1)) == 0);
206
207 return backup_do_cow(req->bs, sector_num, nb_sectors, NULL, true);
208 }
209
210 static void backup_set_speed(BlockJob *job, int64_t speed, Error **errp)
211 {
212 BackupBlockJob *s = container_of(job, BackupBlockJob, common);
213
214 if (speed < 0) {
215 error_setg(errp, QERR_INVALID_PARAMETER, "speed");
216 return;
217 }
218 ratelimit_set_speed(&s->limit, speed / BDRV_SECTOR_SIZE, SLICE_TIME);
219 }
220
221 static void backup_cleanup_sync_bitmap(BackupBlockJob *job, int ret)
222 {
223 BdrvDirtyBitmap *bm;
224 BlockDriverState *bs = job->common.bs;
225
226 if (ret < 0 || block_job_is_cancelled(&job->common)) {
227 /* Merge the successor back into the parent, delete nothing. */
228 bm = bdrv_reclaim_dirty_bitmap(bs, job->sync_bitmap, NULL);
229 assert(bm);
230 } else {
231 /* Everything is fine, delete this bitmap and install the backup. */
232 bm = bdrv_dirty_bitmap_abdicate(bs, job->sync_bitmap, NULL);
233 assert(bm);
234 }
235 }
236
237 static void backup_commit(BlockJob *job)
238 {
239 BackupBlockJob *s = container_of(job, BackupBlockJob, common);
240 if (s->sync_bitmap) {
241 backup_cleanup_sync_bitmap(s, 0);
242 }
243 }
244
245 static void backup_abort(BlockJob *job)
246 {
247 BackupBlockJob *s = container_of(job, BackupBlockJob, common);
248 if (s->sync_bitmap) {
249 backup_cleanup_sync_bitmap(s, -1);
250 }
251 }
252
253 static const BlockJobDriver backup_job_driver = {
254 .instance_size = sizeof(BackupBlockJob),
255 .job_type = BLOCK_JOB_TYPE_BACKUP,
256 .set_speed = backup_set_speed,
257 .commit = backup_commit,
258 .abort = backup_abort,
259 };
260
261 static BlockErrorAction backup_error_action(BackupBlockJob *job,
262 bool read, int error)
263 {
264 if (read) {
265 return block_job_error_action(&job->common, job->on_source_error,
266 true, error);
267 } else {
268 return block_job_error_action(&job->common, job->on_target_error,
269 false, error);
270 }
271 }
272
273 typedef struct {
274 int ret;
275 } BackupCompleteData;
276
277 static void backup_complete(BlockJob *job, void *opaque)
278 {
279 BackupBlockJob *s = container_of(job, BackupBlockJob, common);
280 BackupCompleteData *data = opaque;
281
282 bdrv_unref(s->target);
283
284 block_job_completed(job, data->ret);
285 g_free(data);
286 }
287
288 static bool coroutine_fn yield_and_check(BackupBlockJob *job)
289 {
290 if (block_job_is_cancelled(&job->common)) {
291 return true;
292 }
293
294 /* we need to yield so that bdrv_drain_all() returns.
295 * (without, VM does not reboot)
296 */
297 if (job->common.speed) {
298 uint64_t delay_ns = ratelimit_calculate_delay(&job->limit,
299 job->sectors_read);
300 job->sectors_read = 0;
301 block_job_sleep_ns(&job->common, QEMU_CLOCK_REALTIME, delay_ns);
302 } else {
303 block_job_sleep_ns(&job->common, QEMU_CLOCK_REALTIME, 0);
304 }
305
306 if (block_job_is_cancelled(&job->common)) {
307 return true;
308 }
309
310 return false;
311 }
312
313 static int coroutine_fn backup_run_incremental(BackupBlockJob *job)
314 {
315 bool error_is_read;
316 int ret = 0;
317 int clusters_per_iter;
318 uint32_t granularity;
319 int64_t sector;
320 int64_t cluster;
321 int64_t end;
322 int64_t last_cluster = -1;
323 int64_t sectors_per_cluster = cluster_size_sectors(job);
324 BlockDriverState *bs = job->common.bs;
325 HBitmapIter hbi;
326
327 granularity = bdrv_dirty_bitmap_granularity(job->sync_bitmap);
328 clusters_per_iter = MAX((granularity / job->cluster_size), 1);
329 bdrv_dirty_iter_init(job->sync_bitmap, &hbi);
330
331 /* Find the next dirty sector(s) */
332 while ((sector = hbitmap_iter_next(&hbi)) != -1) {
333 cluster = sector / sectors_per_cluster;
334
335 /* Fake progress updates for any clusters we skipped */
336 if (cluster != last_cluster + 1) {
337 job->common.offset += ((cluster - last_cluster - 1) *
338 job->cluster_size);
339 }
340
341 for (end = cluster + clusters_per_iter; cluster < end; cluster++) {
342 do {
343 if (yield_and_check(job)) {
344 return ret;
345 }
346 ret = backup_do_cow(bs, cluster * sectors_per_cluster,
347 sectors_per_cluster, &error_is_read,
348 false);
349 if ((ret < 0) &&
350 backup_error_action(job, error_is_read, -ret) ==
351 BLOCK_ERROR_ACTION_REPORT) {
352 return ret;
353 }
354 } while (ret < 0);
355 }
356
357 /* If the bitmap granularity is smaller than the backup granularity,
358 * we need to advance the iterator pointer to the next cluster. */
359 if (granularity < job->cluster_size) {
360 bdrv_set_dirty_iter(&hbi, cluster * sectors_per_cluster);
361 }
362
363 last_cluster = cluster - 1;
364 }
365
366 /* Play some final catchup with the progress meter */
367 end = DIV_ROUND_UP(job->common.len, job->cluster_size);
368 if (last_cluster + 1 < end) {
369 job->common.offset += ((end - last_cluster - 1) * job->cluster_size);
370 }
371
372 return ret;
373 }
374
375 static void coroutine_fn backup_run(void *opaque)
376 {
377 BackupBlockJob *job = opaque;
378 BackupCompleteData *data;
379 BlockDriverState *bs = job->common.bs;
380 BlockDriverState *target = job->target;
381 NotifierWithReturn before_write = {
382 .notify = backup_before_write_notify,
383 };
384 int64_t start, end;
385 int64_t sectors_per_cluster = cluster_size_sectors(job);
386 int ret = 0;
387
388 QLIST_INIT(&job->inflight_reqs);
389 qemu_co_rwlock_init(&job->flush_rwlock);
390
391 start = 0;
392 end = DIV_ROUND_UP(job->common.len, job->cluster_size);
393
394 job->done_bitmap = bitmap_new(end);
395
396 bdrv_add_before_write_notifier(bs, &before_write);
397
398 if (job->sync_mode == MIRROR_SYNC_MODE_NONE) {
399 while (!block_job_is_cancelled(&job->common)) {
400 /* Yield until the job is cancelled. We just let our before_write
401 * notify callback service CoW requests. */
402 job->common.busy = false;
403 qemu_coroutine_yield();
404 job->common.busy = true;
405 }
406 } else if (job->sync_mode == MIRROR_SYNC_MODE_INCREMENTAL) {
407 ret = backup_run_incremental(job);
408 } else {
409 /* Both FULL and TOP SYNC_MODE's require copying.. */
410 for (; start < end; start++) {
411 bool error_is_read;
412 if (yield_and_check(job)) {
413 break;
414 }
415
416 if (job->sync_mode == MIRROR_SYNC_MODE_TOP) {
417 int i, n;
418 int alloced = 0;
419
420 /* Check to see if these blocks are already in the
421 * backing file. */
422
423 for (i = 0; i < sectors_per_cluster;) {
424 /* bdrv_is_allocated() only returns true/false based
425 * on the first set of sectors it comes across that
426 * are are all in the same state.
427 * For that reason we must verify each sector in the
428 * backup cluster length. We end up copying more than
429 * needed but at some point that is always the case. */
430 alloced =
431 bdrv_is_allocated(bs,
432 start * sectors_per_cluster + i,
433 sectors_per_cluster - i, &n);
434 i += n;
435
436 if (alloced == 1 || n == 0) {
437 break;
438 }
439 }
440
441 /* If the above loop never found any sectors that are in
442 * the topmost image, skip this backup. */
443 if (alloced == 0) {
444 continue;
445 }
446 }
447 /* FULL sync mode we copy the whole drive. */
448 ret = backup_do_cow(bs, start * sectors_per_cluster,
449 sectors_per_cluster, &error_is_read, false);
450 if (ret < 0) {
451 /* Depending on error action, fail now or retry cluster */
452 BlockErrorAction action =
453 backup_error_action(job, error_is_read, -ret);
454 if (action == BLOCK_ERROR_ACTION_REPORT) {
455 break;
456 } else {
457 start--;
458 continue;
459 }
460 }
461 }
462 }
463
464 notifier_with_return_remove(&before_write);
465
466 /* wait until pending backup_do_cow() calls have completed */
467 qemu_co_rwlock_wrlock(&job->flush_rwlock);
468 qemu_co_rwlock_unlock(&job->flush_rwlock);
469 g_free(job->done_bitmap);
470
471 bdrv_op_unblock_all(target, job->common.blocker);
472
473 data = g_malloc(sizeof(*data));
474 data->ret = ret;
475 block_job_defer_to_main_loop(&job->common, backup_complete, data);
476 }
477
478 void backup_start(BlockDriverState *bs, BlockDriverState *target,
479 int64_t speed, MirrorSyncMode sync_mode,
480 BdrvDirtyBitmap *sync_bitmap,
481 BlockdevOnError on_source_error,
482 BlockdevOnError on_target_error,
483 BlockCompletionFunc *cb, void *opaque,
484 BlockJobTxn *txn, Error **errp)
485 {
486 int64_t len;
487 BlockDriverInfo bdi;
488 int ret;
489
490 assert(bs);
491 assert(target);
492 assert(cb);
493
494 if (bs == target) {
495 error_setg(errp, "Source and target cannot be the same");
496 return;
497 }
498
499 if (!bdrv_is_inserted(bs)) {
500 error_setg(errp, "Device is not inserted: %s",
501 bdrv_get_device_name(bs));
502 return;
503 }
504
505 if (!bdrv_is_inserted(target)) {
506 error_setg(errp, "Device is not inserted: %s",
507 bdrv_get_device_name(target));
508 return;
509 }
510
511 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_BACKUP_SOURCE, errp)) {
512 return;
513 }
514
515 if (bdrv_op_is_blocked(target, BLOCK_OP_TYPE_BACKUP_TARGET, errp)) {
516 return;
517 }
518
519 if (sync_mode == MIRROR_SYNC_MODE_INCREMENTAL) {
520 if (!sync_bitmap) {
521 error_setg(errp, "must provide a valid bitmap name for "
522 "\"incremental\" sync mode");
523 return;
524 }
525
526 /* Create a new bitmap, and freeze/disable this one. */
527 if (bdrv_dirty_bitmap_create_successor(bs, sync_bitmap, errp) < 0) {
528 return;
529 }
530 } else if (sync_bitmap) {
531 error_setg(errp,
532 "a sync_bitmap was provided to backup_run, "
533 "but received an incompatible sync_mode (%s)",
534 MirrorSyncMode_lookup[sync_mode]);
535 return;
536 }
537
538 len = bdrv_getlength(bs);
539 if (len < 0) {
540 error_setg_errno(errp, -len, "unable to get length for '%s'",
541 bdrv_get_device_name(bs));
542 goto error;
543 }
544
545 BackupBlockJob *job = block_job_create(&backup_job_driver, bs, speed,
546 cb, opaque, errp);
547 if (!job) {
548 goto error;
549 }
550
551 job->on_source_error = on_source_error;
552 job->on_target_error = on_target_error;
553 job->target = target;
554 job->sync_mode = sync_mode;
555 job->sync_bitmap = sync_mode == MIRROR_SYNC_MODE_INCREMENTAL ?
556 sync_bitmap : NULL;
557
558 /* If there is no backing file on the target, we cannot rely on COW if our
559 * backup cluster size is smaller than the target cluster size. Even for
560 * targets with a backing file, try to avoid COW if possible. */
561 ret = bdrv_get_info(job->target, &bdi);
562 if (ret < 0 && !target->backing) {
563 error_setg_errno(errp, -ret,
564 "Couldn't determine the cluster size of the target image, "
565 "which has no backing file");
566 error_append_hint(errp,
567 "Aborting, since this may create an unusable destination image\n");
568 goto error;
569 } else if (ret < 0 && target->backing) {
570 /* Not fatal; just trudge on ahead. */
571 job->cluster_size = BACKUP_CLUSTER_SIZE_DEFAULT;
572 } else {
573 job->cluster_size = MAX(BACKUP_CLUSTER_SIZE_DEFAULT, bdi.cluster_size);
574 }
575
576 bdrv_op_block_all(target, job->common.blocker);
577 job->common.len = len;
578 job->common.co = qemu_coroutine_create(backup_run);
579 block_job_txn_add_job(txn, &job->common);
580 qemu_coroutine_enter(job->common.co, job);
581 return;
582
583 error:
584 if (sync_bitmap) {
585 bdrv_reclaim_dirty_bitmap(bs, sync_bitmap, NULL);
586 }
587 }