]> git.proxmox.com Git - pve-qemu.git/blob - debian/patches/pve/0029-PVE-Backup-proxmox-backup-patches-for-qemu.patch
PVE backup: don't call no_co_wrapper function from coroutine
[pve-qemu.git] / debian / patches / pve / 0029-PVE-Backup-proxmox-backup-patches-for-qemu.patch
1 From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
2 From: Dietmar Maurer <dietmar@proxmox.com>
3 Date: Mon, 6 Apr 2020 12:16:59 +0200
4 Subject: [PATCH] PVE-Backup: proxmox backup patches for qemu
5
6 Signed-off-by: Dietmar Maurer <dietmar@proxmox.com>
7 [PVE-Backup: avoid coroutines to fix AIO freeze, cleanups]
8 Signed-off-by: Stefan Reiter <s.reiter@proxmox.com>
9 Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
10 [FE: add new force parameter to job_cancel_sync calls
11 adapt for new job lock mechanism replacing AioContext locks
12 adapt to QAPI changes]
13 Signed-off-by: Fiona Ebner <f.ebner@proxmox.com>
14 ---
15 block/meson.build | 5 +
16 block/monitor/block-hmp-cmds.c | 33 ++
17 blockdev.c | 1 +
18 hmp-commands-info.hx | 14 +
19 hmp-commands.hx | 29 +
20 include/monitor/hmp.h | 3 +
21 meson.build | 1 +
22 monitor/hmp-cmds.c | 45 ++
23 proxmox-backup-client.c | 176 +++++++
24 proxmox-backup-client.h | 59 +++
25 pve-backup.c | 938 +++++++++++++++++++++++++++++++++
26 qapi/block-core.json | 109 ++++
27 qapi/common.json | 13 +
28 qapi/machine.json | 15 +-
29 14 files changed, 1428 insertions(+), 13 deletions(-)
30 create mode 100644 proxmox-backup-client.c
31 create mode 100644 proxmox-backup-client.h
32 create mode 100644 pve-backup.c
33
34 diff --git a/block/meson.build b/block/meson.build
35 index f580f95395..5bcebb934b 100644
36 --- a/block/meson.build
37 +++ b/block/meson.build
38 @@ -49,6 +49,11 @@ block_ss.add(files(
39 ), zstd, zlib, gnutls)
40
41 block_ss.add(files('../vma-writer.c'), libuuid)
42 +block_ss.add(files(
43 + '../proxmox-backup-client.c',
44 + '../pve-backup.c',
45 +), libproxmox_backup_qemu)
46 +
47
48 softmmu_ss.add(when: 'CONFIG_TCG', if_true: files('blkreplay.c'))
49 softmmu_ss.add(files('block-ram-registrar.c'))
50 diff --git a/block/monitor/block-hmp-cmds.c b/block/monitor/block-hmp-cmds.c
51 index ca2599de44..d50e99df26 100644
52 --- a/block/monitor/block-hmp-cmds.c
53 +++ b/block/monitor/block-hmp-cmds.c
54 @@ -1029,3 +1029,36 @@ void hmp_change_medium(Monitor *mon, const char *device, const char *target,
55 qmp_blockdev_change_medium(device, NULL, target, arg, true, force,
56 !!read_only, read_only_mode, errp);
57 }
58 +
59 +void hmp_backup_cancel(Monitor *mon, const QDict *qdict)
60 +{
61 + Error *error = NULL;
62 +
63 + qmp_backup_cancel(&error);
64 +
65 + hmp_handle_error(mon, error);
66 +}
67 +
68 +void hmp_backup(Monitor *mon, const QDict *qdict)
69 +{
70 + Error *error = NULL;
71 +
72 + int dir = qdict_get_try_bool(qdict, "directory", 0);
73 + const char *backup_file = qdict_get_str(qdict, "backupfile");
74 + const char *devlist = qdict_get_try_str(qdict, "devlist");
75 + int64_t speed = qdict_get_try_int(qdict, "speed", 0);
76 +
77 + qmp_backup(
78 + backup_file,
79 + NULL, // PBS password
80 + NULL, // PBS keyfile
81 + NULL, // PBS key_password
82 + NULL, // PBS fingerprint
83 + NULL, // PBS backup-id
84 + false, 0, // PBS backup-time
85 + true, dir ? BACKUP_FORMAT_DIR : BACKUP_FORMAT_VMA,
86 + NULL, NULL,
87 + devlist, qdict_haskey(qdict, "speed"), speed, &error);
88 +
89 + hmp_handle_error(mon, error);
90 +}
91 diff --git a/blockdev.c b/blockdev.c
92 index 9a010f3a86..b9505c95d3 100644
93 --- a/blockdev.c
94 +++ b/blockdev.c
95 @@ -37,6 +37,7 @@
96 #include "block/blockjob.h"
97 #include "block/dirty-bitmap.h"
98 #include "block/qdict.h"
99 +#include "block/blockjob_int.h"
100 #include "block/throttle-groups.h"
101 #include "monitor/monitor.h"
102 #include "qemu/error-report.h"
103 diff --git a/hmp-commands-info.hx b/hmp-commands-info.hx
104 index a166bff3d5..4b75966c2e 100644
105 --- a/hmp-commands-info.hx
106 +++ b/hmp-commands-info.hx
107 @@ -486,6 +486,20 @@ SRST
108 Show the current VM UUID.
109 ERST
110
111 +
112 + {
113 + .name = "backup",
114 + .args_type = "",
115 + .params = "",
116 + .help = "show backup status",
117 + .cmd = hmp_info_backup,
118 + },
119 +
120 +SRST
121 + ``info backup``
122 + Show backup status.
123 +ERST
124 +
125 #if defined(CONFIG_SLIRP)
126 {
127 .name = "usernet",
128 diff --git a/hmp-commands.hx b/hmp-commands.hx
129 index b66d7fc4ab..9b6b8e2e9c 100644
130 --- a/hmp-commands.hx
131 +++ b/hmp-commands.hx
132 @@ -101,6 +101,35 @@ ERST
133 SRST
134 ``block_stream``
135 Copy data from a backing file into a block device.
136 +ERST
137 +
138 + {
139 + .name = "backup",
140 + .args_type = "directory:-d,backupfile:s,speed:o?,devlist:s?",
141 + .params = "[-d] backupfile [speed [devlist]]",
142 + .help = "create a VM Backup."
143 + "\n\t\t\t Use -d to dump data into a directory instead"
144 + "\n\t\t\t of using VMA format.",
145 + .cmd = hmp_backup,
146 + },
147 +
148 +SRST
149 +``backup``
150 + Create a VM backup.
151 +ERST
152 +
153 + {
154 + .name = "backup_cancel",
155 + .args_type = "",
156 + .params = "",
157 + .help = "cancel the current VM backup",
158 + .cmd = hmp_backup_cancel,
159 + },
160 +
161 +SRST
162 +``backup_cancel``
163 + Cancel the current VM backup.
164 +
165 ERST
166
167 {
168 diff --git a/include/monitor/hmp.h b/include/monitor/hmp.h
169 index c012bad741..2e504db706 100644
170 --- a/include/monitor/hmp.h
171 +++ b/include/monitor/hmp.h
172 @@ -32,6 +32,7 @@ void hmp_info_savevm(Monitor *mon, const QDict *qdict);
173 void hmp_info_migrate(Monitor *mon, const QDict *qdict);
174 void hmp_info_migrate_capabilities(Monitor *mon, const QDict *qdict);
175 void hmp_info_migrate_parameters(Monitor *mon, const QDict *qdict);
176 +void hmp_info_backup(Monitor *mon, const QDict *qdict);
177 void hmp_info_cpus(Monitor *mon, const QDict *qdict);
178 void hmp_info_vnc(Monitor *mon, const QDict *qdict);
179 void hmp_info_spice(Monitor *mon, const QDict *qdict);
180 @@ -84,6 +85,8 @@ void hmp_change_vnc(Monitor *mon, const char *device, const char *target,
181 void hmp_change_medium(Monitor *mon, const char *device, const char *target,
182 const char *arg, const char *read_only, bool force,
183 Error **errp);
184 +void hmp_backup(Monitor *mon, const QDict *qdict);
185 +void hmp_backup_cancel(Monitor *mon, const QDict *qdict);
186 void hmp_migrate(Monitor *mon, const QDict *qdict);
187 void hmp_device_add(Monitor *mon, const QDict *qdict);
188 void hmp_device_del(Monitor *mon, const QDict *qdict);
189 diff --git a/meson.build b/meson.build
190 index 603cdb97bb..d307d8eabf 100644
191 --- a/meson.build
192 +++ b/meson.build
193 @@ -1528,6 +1528,7 @@ keyutils = dependency('libkeyutils', required: false,
194 has_gettid = cc.has_function('gettid')
195
196 libuuid = cc.find_library('uuid', required: true)
197 +libproxmox_backup_qemu = cc.find_library('proxmox_backup_qemu', required: true)
198
199 # libselinux
200 selinux = dependency('libselinux',
201 diff --git a/monitor/hmp-cmds.c b/monitor/hmp-cmds.c
202 index 435f9334f9..9e1bd57aeb 100644
203 --- a/monitor/hmp-cmds.c
204 +++ b/monitor/hmp-cmds.c
205 @@ -21,6 +21,7 @@
206 #include "qemu/help_option.h"
207 #include "monitor/monitor-internal.h"
208 #include "qapi/error.h"
209 +#include "qapi/qapi-commands-block-core.h"
210 #include "qapi/qapi-commands-control.h"
211 #include "qapi/qapi-commands-migration.h"
212 #include "qapi/qapi-commands-misc.h"
213 @@ -144,6 +145,50 @@ void hmp_sync_profile(Monitor *mon, const QDict *qdict)
214 }
215 }
216
217 +void hmp_info_backup(Monitor *mon, const QDict *qdict)
218 +{
219 + BackupStatus *info;
220 +
221 + info = qmp_query_backup(NULL);
222 +
223 + if (!info) {
224 + monitor_printf(mon, "Backup status: not initialized\n");
225 + return;
226 + }
227 +
228 + if (info->status) {
229 + if (info->errmsg) {
230 + monitor_printf(mon, "Backup status: %s - %s\n",
231 + info->status, info->errmsg);
232 + } else {
233 + monitor_printf(mon, "Backup status: %s\n", info->status);
234 + }
235 + }
236 +
237 + if (info->backup_file) {
238 + monitor_printf(mon, "Start time: %s", ctime(&info->start_time));
239 + if (info->end_time) {
240 + monitor_printf(mon, "End time: %s", ctime(&info->end_time));
241 + }
242 +
243 + int per = (info->has_total && info->total &&
244 + info->has_transferred && info->transferred) ?
245 + (info->transferred * 100)/info->total : 0;
246 + int zero_per = (info->has_total && info->total &&
247 + info->has_zero_bytes && info->zero_bytes) ?
248 + (info->zero_bytes * 100)/info->total : 0;
249 + monitor_printf(mon, "Backup file: %s\n", info->backup_file);
250 + monitor_printf(mon, "Backup uuid: %s\n", info->uuid);
251 + monitor_printf(mon, "Total size: %zd\n", info->total);
252 + monitor_printf(mon, "Transferred bytes: %zd (%d%%)\n",
253 + info->transferred, per);
254 + monitor_printf(mon, "Zero bytes: %zd (%d%%)\n",
255 + info->zero_bytes, zero_per);
256 + }
257 +
258 + qapi_free_BackupStatus(info);
259 +}
260 +
261 void hmp_exit_preconfig(Monitor *mon, const QDict *qdict)
262 {
263 Error *err = NULL;
264 diff --git a/proxmox-backup-client.c b/proxmox-backup-client.c
265 new file mode 100644
266 index 0000000000..a8f6653a81
267 --- /dev/null
268 +++ b/proxmox-backup-client.c
269 @@ -0,0 +1,176 @@
270 +#include "proxmox-backup-client.h"
271 +#include "qemu/main-loop.h"
272 +#include "block/aio-wait.h"
273 +#include "qapi/error.h"
274 +
275 +/* Proxmox Backup Server client bindings using coroutines */
276 +
277 +typedef struct BlockOnCoroutineWrapper {
278 + AioContext *ctx;
279 + CoroutineEntry *entry;
280 + void *entry_arg;
281 + bool finished;
282 +} BlockOnCoroutineWrapper;
283 +
284 +static void coroutine_fn block_on_coroutine_wrapper(void *opaque)
285 +{
286 + BlockOnCoroutineWrapper *wrapper = opaque;
287 + wrapper->entry(wrapper->entry_arg);
288 + wrapper->finished = true;
289 + aio_wait_kick();
290 +}
291 +
292 +void block_on_coroutine_fn(CoroutineEntry *entry, void *entry_arg)
293 +{
294 + assert(!qemu_in_coroutine());
295 +
296 + AioContext *ctx = qemu_get_current_aio_context();
297 + BlockOnCoroutineWrapper wrapper = {
298 + .finished = false,
299 + .entry = entry,
300 + .entry_arg = entry_arg,
301 + .ctx = ctx,
302 + };
303 + Coroutine *wrapper_co = qemu_coroutine_create(block_on_coroutine_wrapper, &wrapper);
304 + aio_co_enter(ctx, wrapper_co);
305 + AIO_WAIT_WHILE(ctx, !wrapper.finished);
306 +}
307 +
308 +// This is called from another thread, so we use aio_co_schedule()
309 +static void proxmox_backup_schedule_wake(void *data) {
310 + CoCtxData *waker = (CoCtxData *)data;
311 + aio_co_schedule(waker->ctx, waker->co);
312 +}
313 +
314 +int coroutine_fn
315 +proxmox_backup_co_connect(ProxmoxBackupHandle *pbs, Error **errp)
316 +{
317 + Coroutine *co = qemu_coroutine_self();
318 + AioContext *ctx = qemu_get_current_aio_context();
319 + CoCtxData waker = { .co = co, .ctx = ctx };
320 + char *pbs_err = NULL;
321 + int pbs_res = -1;
322 +
323 + proxmox_backup_connect_async(pbs, proxmox_backup_schedule_wake, &waker, &pbs_res, &pbs_err);
324 + qemu_coroutine_yield();
325 + if (pbs_res < 0) {
326 + if (errp) error_setg(errp, "backup connect failed: %s", pbs_err ? pbs_err : "unknown error");
327 + if (pbs_err) proxmox_backup_free_error(pbs_err);
328 + }
329 + return pbs_res;
330 +}
331 +
332 +int coroutine_fn
333 +proxmox_backup_co_add_config(
334 + ProxmoxBackupHandle *pbs,
335 + const char *name,
336 + const uint8_t *data,
337 + uint64_t size,
338 + Error **errp)
339 +{
340 + Coroutine *co = qemu_coroutine_self();
341 + AioContext *ctx = qemu_get_current_aio_context();
342 + CoCtxData waker = { .co = co, .ctx = ctx };
343 + char *pbs_err = NULL;
344 + int pbs_res = -1;
345 +
346 + proxmox_backup_add_config_async(
347 + pbs, name, data, size ,proxmox_backup_schedule_wake, &waker, &pbs_res, &pbs_err);
348 + qemu_coroutine_yield();
349 + if (pbs_res < 0) {
350 + if (errp) error_setg(errp, "backup add_config %s failed: %s", name, pbs_err ? pbs_err : "unknown error");
351 + if (pbs_err) proxmox_backup_free_error(pbs_err);
352 + }
353 + return pbs_res;
354 +}
355 +
356 +int coroutine_fn
357 +proxmox_backup_co_register_image(
358 + ProxmoxBackupHandle *pbs,
359 + const char *device_name,
360 + uint64_t size,
361 + Error **errp)
362 +{
363 + Coroutine *co = qemu_coroutine_self();
364 + AioContext *ctx = qemu_get_current_aio_context();
365 + CoCtxData waker = { .co = co, .ctx = ctx };
366 + char *pbs_err = NULL;
367 + int pbs_res = -1;
368 +
369 + proxmox_backup_register_image_async(
370 + pbs, device_name, size ,proxmox_backup_schedule_wake, &waker, &pbs_res, &pbs_err);
371 + qemu_coroutine_yield();
372 + if (pbs_res < 0) {
373 + if (errp) error_setg(errp, "backup register image failed: %s", pbs_err ? pbs_err : "unknown error");
374 + if (pbs_err) proxmox_backup_free_error(pbs_err);
375 + }
376 + return pbs_res;
377 +}
378 +
379 +int coroutine_fn
380 +proxmox_backup_co_finish(
381 + ProxmoxBackupHandle *pbs,
382 + Error **errp)
383 +{
384 + Coroutine *co = qemu_coroutine_self();
385 + AioContext *ctx = qemu_get_current_aio_context();
386 + CoCtxData waker = { .co = co, .ctx = ctx };
387 + char *pbs_err = NULL;
388 + int pbs_res = -1;
389 +
390 + proxmox_backup_finish_async(
391 + pbs, proxmox_backup_schedule_wake, &waker, &pbs_res, &pbs_err);
392 + qemu_coroutine_yield();
393 + if (pbs_res < 0) {
394 + if (errp) error_setg(errp, "backup finish failed: %s", pbs_err ? pbs_err : "unknown error");
395 + if (pbs_err) proxmox_backup_free_error(pbs_err);
396 + }
397 + return pbs_res;
398 +}
399 +
400 +int coroutine_fn
401 +proxmox_backup_co_close_image(
402 + ProxmoxBackupHandle *pbs,
403 + uint8_t dev_id,
404 + Error **errp)
405 +{
406 + Coroutine *co = qemu_coroutine_self();
407 + AioContext *ctx = qemu_get_current_aio_context();
408 + CoCtxData waker = { .co = co, .ctx = ctx };
409 + char *pbs_err = NULL;
410 + int pbs_res = -1;
411 +
412 + proxmox_backup_close_image_async(
413 + pbs, dev_id, proxmox_backup_schedule_wake, &waker, &pbs_res, &pbs_err);
414 + qemu_coroutine_yield();
415 + if (pbs_res < 0) {
416 + if (errp) error_setg(errp, "backup close image failed: %s", pbs_err ? pbs_err : "unknown error");
417 + if (pbs_err) proxmox_backup_free_error(pbs_err);
418 + }
419 + return pbs_res;
420 +}
421 +
422 +int coroutine_fn
423 +proxmox_backup_co_write_data(
424 + ProxmoxBackupHandle *pbs,
425 + uint8_t dev_id,
426 + const uint8_t *data,
427 + uint64_t offset,
428 + uint64_t size,
429 + Error **errp)
430 +{
431 + Coroutine *co = qemu_coroutine_self();
432 + AioContext *ctx = qemu_get_current_aio_context();
433 + CoCtxData waker = { .co = co, .ctx = ctx };
434 + char *pbs_err = NULL;
435 + int pbs_res = -1;
436 +
437 + proxmox_backup_write_data_async(
438 + pbs, dev_id, data, offset, size, proxmox_backup_schedule_wake, &waker, &pbs_res, &pbs_err);
439 + qemu_coroutine_yield();
440 + if (pbs_res < 0) {
441 + if (errp) error_setg(errp, "backup write data failed: %s", pbs_err ? pbs_err : "unknown error");
442 + if (pbs_err) proxmox_backup_free_error(pbs_err);
443 + }
444 + return pbs_res;
445 +}
446 diff --git a/proxmox-backup-client.h b/proxmox-backup-client.h
447 new file mode 100644
448 index 0000000000..1dda8b7d8f
449 --- /dev/null
450 +++ b/proxmox-backup-client.h
451 @@ -0,0 +1,59 @@
452 +#ifndef PROXMOX_BACKUP_CLIENT_H
453 +#define PROXMOX_BACKUP_CLIENT_H
454 +
455 +#include "qemu/osdep.h"
456 +#include "qemu/coroutine.h"
457 +#include "proxmox-backup-qemu.h"
458 +
459 +typedef struct CoCtxData {
460 + Coroutine *co;
461 + AioContext *ctx;
462 + void *data;
463 +} CoCtxData;
464 +
465 +// FIXME: Remove once coroutines are supported for QMP
466 +void block_on_coroutine_fn(CoroutineEntry *entry, void *entry_arg);
467 +
468 +int coroutine_fn
469 +proxmox_backup_co_connect(
470 + ProxmoxBackupHandle *pbs,
471 + Error **errp);
472 +
473 +int coroutine_fn
474 +proxmox_backup_co_add_config(
475 + ProxmoxBackupHandle *pbs,
476 + const char *name,
477 + const uint8_t *data,
478 + uint64_t size,
479 + Error **errp);
480 +
481 +int coroutine_fn
482 +proxmox_backup_co_register_image(
483 + ProxmoxBackupHandle *pbs,
484 + const char *device_name,
485 + uint64_t size,
486 + Error **errp);
487 +
488 +
489 +int coroutine_fn
490 +proxmox_backup_co_finish(
491 + ProxmoxBackupHandle *pbs,
492 + Error **errp);
493 +
494 +int coroutine_fn
495 +proxmox_backup_co_close_image(
496 + ProxmoxBackupHandle *pbs,
497 + uint8_t dev_id,
498 + Error **errp);
499 +
500 +int coroutine_fn
501 +proxmox_backup_co_write_data(
502 + ProxmoxBackupHandle *pbs,
503 + uint8_t dev_id,
504 + const uint8_t *data,
505 + uint64_t offset,
506 + uint64_t size,
507 + Error **errp);
508 +
509 +
510 +#endif /* PROXMOX_BACKUP_CLIENT_H */
511 diff --git a/pve-backup.c b/pve-backup.c
512 new file mode 100644
513 index 0000000000..f77892a509
514 --- /dev/null
515 +++ b/pve-backup.c
516 @@ -0,0 +1,938 @@
517 +#include "proxmox-backup-client.h"
518 +#include "vma.h"
519 +
520 +#include "qemu/osdep.h"
521 +#include "qemu/module.h"
522 +#include "sysemu/block-backend.h"
523 +#include "sysemu/blockdev.h"
524 +#include "block/block_int-global-state.h"
525 +#include "block/blockjob.h"
526 +#include "qapi/qapi-commands-block.h"
527 +#include "qapi/qmp/qerror.h"
528 +
529 +/* PVE backup state and related function */
530 +
531 +/*
532 + * Note: A resume from a qemu_coroutine_yield can happen in a different thread,
533 + * so you may not use normal mutexes within coroutines:
534 + *
535 + * ---bad-example---
536 + * qemu_rec_mutex_lock(lock)
537 + * ...
538 + * qemu_coroutine_yield() // wait for something
539 + * // we are now inside a different thread
540 + * qemu_rec_mutex_unlock(lock) // Crash - wrong thread!!
541 + * ---end-bad-example--
542 + *
543 + * ==> Always use CoMutext inside coroutines.
544 + * ==> Never acquire/release AioContext withing coroutines (because that use QemuRecMutex)
545 + *
546 + */
547 +
548 +static struct PVEBackupState {
549 + struct {
550 + // Everithing accessed from qmp_backup_query command is protected using lock
551 + QemuMutex lock;
552 + Error *error;
553 + time_t start_time;
554 + time_t end_time;
555 + char *backup_file;
556 + uuid_t uuid;
557 + char uuid_str[37];
558 + size_t total;
559 + size_t transferred;
560 + size_t zero_bytes;
561 + } stat;
562 + int64_t speed;
563 + VmaWriter *vmaw;
564 + ProxmoxBackupHandle *pbs;
565 + GList *di_list;
566 + QemuMutex backup_mutex;
567 + CoMutex dump_callback_mutex;
568 +} backup_state;
569 +
570 +static void pvebackup_init(void)
571 +{
572 + qemu_mutex_init(&backup_state.stat.lock);
573 + qemu_mutex_init(&backup_state.backup_mutex);
574 + qemu_co_mutex_init(&backup_state.dump_callback_mutex);
575 +}
576 +
577 +// initialize PVEBackupState at startup
578 +opts_init(pvebackup_init);
579 +
580 +typedef struct PVEBackupDevInfo {
581 + BlockDriverState *bs;
582 + size_t size;
583 + uint8_t dev_id;
584 + bool completed;
585 + char targetfile[PATH_MAX];
586 + BlockDriverState *target;
587 +} PVEBackupDevInfo;
588 +
589 +static void pvebackup_run_next_job(void);
590 +
591 +static BlockJob *
592 +lookup_active_block_job(PVEBackupDevInfo *di)
593 +{
594 + if (!di->completed && di->bs) {
595 + WITH_JOB_LOCK_GUARD() {
596 + for (BlockJob *job = block_job_next_locked(NULL); job; job = block_job_next_locked(job)) {
597 + if (job->job.driver->job_type != JOB_TYPE_BACKUP) {
598 + continue;
599 + }
600 +
601 + BackupBlockJob *bjob = container_of(job, BackupBlockJob, common);
602 + if (bjob && bjob->source_bs == di->bs) {
603 + return job;
604 + }
605 + }
606 + }
607 + }
608 + return NULL;
609 +}
610 +
611 +static void pvebackup_propagate_error(Error *err)
612 +{
613 + qemu_mutex_lock(&backup_state.stat.lock);
614 + error_propagate(&backup_state.stat.error, err);
615 + qemu_mutex_unlock(&backup_state.stat.lock);
616 +}
617 +
618 +static bool pvebackup_error_or_canceled(void)
619 +{
620 + qemu_mutex_lock(&backup_state.stat.lock);
621 + bool error_or_canceled = !!backup_state.stat.error;
622 + qemu_mutex_unlock(&backup_state.stat.lock);
623 +
624 + return error_or_canceled;
625 +}
626 +
627 +static void pvebackup_add_transfered_bytes(size_t transferred, size_t zero_bytes)
628 +{
629 + qemu_mutex_lock(&backup_state.stat.lock);
630 + backup_state.stat.zero_bytes += zero_bytes;
631 + backup_state.stat.transferred += transferred;
632 + qemu_mutex_unlock(&backup_state.stat.lock);
633 +}
634 +
635 +// This may get called from multiple coroutines in multiple io-threads
636 +// Note1: this may get called after job_cancel()
637 +static int coroutine_fn
638 +pvebackup_co_dump_pbs_cb(
639 + void *opaque,
640 + uint64_t start,
641 + uint64_t bytes,
642 + const void *pbuf)
643 +{
644 + assert(qemu_in_coroutine());
645 +
646 + const uint64_t size = bytes;
647 + const unsigned char *buf = pbuf;
648 + PVEBackupDevInfo *di = opaque;
649 +
650 + assert(backup_state.pbs);
651 +
652 + Error *local_err = NULL;
653 + int pbs_res = -1;
654 +
655 + qemu_co_mutex_lock(&backup_state.dump_callback_mutex);
656 +
657 + // avoid deadlock if job is cancelled
658 + if (pvebackup_error_or_canceled()) {
659 + qemu_co_mutex_unlock(&backup_state.dump_callback_mutex);
660 + return -1;
661 + }
662 +
663 + pbs_res = proxmox_backup_co_write_data(backup_state.pbs, di->dev_id, buf, start, size, &local_err);
664 + qemu_co_mutex_unlock(&backup_state.dump_callback_mutex);
665 +
666 + if (pbs_res < 0) {
667 + pvebackup_propagate_error(local_err);
668 + return pbs_res;
669 + } else {
670 + pvebackup_add_transfered_bytes(size, !buf ? size : 0);
671 + }
672 +
673 + return size;
674 +}
675 +
676 +// This may get called from multiple coroutines in multiple io-threads
677 +static int coroutine_fn
678 +pvebackup_co_dump_vma_cb(
679 + void *opaque,
680 + uint64_t start,
681 + uint64_t bytes,
682 + const void *pbuf)
683 +{
684 + assert(qemu_in_coroutine());
685 +
686 + const uint64_t size = bytes;
687 + const unsigned char *buf = pbuf;
688 + PVEBackupDevInfo *di = opaque;
689 +
690 + int ret = -1;
691 +
692 + assert(backup_state.vmaw);
693 +
694 + uint64_t remaining = size;
695 +
696 + uint64_t cluster_num = start / VMA_CLUSTER_SIZE;
697 + if ((cluster_num * VMA_CLUSTER_SIZE) != start) {
698 + Error *local_err = NULL;
699 + error_setg(&local_err,
700 + "got unaligned write inside backup dump "
701 + "callback (sector %ld)", start);
702 + pvebackup_propagate_error(local_err);
703 + return -1; // not aligned to cluster size
704 + }
705 +
706 + while (remaining > 0) {
707 + qemu_co_mutex_lock(&backup_state.dump_callback_mutex);
708 + // avoid deadlock if job is cancelled
709 + if (pvebackup_error_or_canceled()) {
710 + qemu_co_mutex_unlock(&backup_state.dump_callback_mutex);
711 + return -1;
712 + }
713 +
714 + size_t zero_bytes = 0;
715 + ret = vma_writer_write(backup_state.vmaw, di->dev_id, cluster_num, buf, &zero_bytes);
716 + qemu_co_mutex_unlock(&backup_state.dump_callback_mutex);
717 +
718 + ++cluster_num;
719 + if (buf) {
720 + buf += VMA_CLUSTER_SIZE;
721 + }
722 + if (ret < 0) {
723 + Error *local_err = NULL;
724 + vma_writer_error_propagate(backup_state.vmaw, &local_err);
725 + pvebackup_propagate_error(local_err);
726 + return ret;
727 + } else {
728 + if (remaining >= VMA_CLUSTER_SIZE) {
729 + assert(ret == VMA_CLUSTER_SIZE);
730 + pvebackup_add_transfered_bytes(VMA_CLUSTER_SIZE, zero_bytes);
731 + remaining -= VMA_CLUSTER_SIZE;
732 + } else {
733 + assert(ret == remaining);
734 + pvebackup_add_transfered_bytes(remaining, zero_bytes);
735 + remaining = 0;
736 + }
737 + }
738 + }
739 +
740 + return size;
741 +}
742 +
743 +// assumes the caller holds backup_mutex
744 +static void coroutine_fn pvebackup_co_cleanup(void *unused)
745 +{
746 + assert(qemu_in_coroutine());
747 +
748 + qemu_mutex_lock(&backup_state.stat.lock);
749 + backup_state.stat.end_time = time(NULL);
750 + qemu_mutex_unlock(&backup_state.stat.lock);
751 +
752 + if (backup_state.vmaw) {
753 + Error *local_err = NULL;
754 + vma_writer_close(backup_state.vmaw, &local_err);
755 +
756 + if (local_err != NULL) {
757 + pvebackup_propagate_error(local_err);
758 + }
759 +
760 + backup_state.vmaw = NULL;
761 + }
762 +
763 + if (backup_state.pbs) {
764 + if (!pvebackup_error_or_canceled()) {
765 + Error *local_err = NULL;
766 + proxmox_backup_co_finish(backup_state.pbs, &local_err);
767 + if (local_err != NULL) {
768 + pvebackup_propagate_error(local_err);
769 + }
770 + }
771 +
772 + proxmox_backup_disconnect(backup_state.pbs);
773 + backup_state.pbs = NULL;
774 + }
775 +
776 + g_list_free(backup_state.di_list);
777 + backup_state.di_list = NULL;
778 +}
779 +
780 +// assumes the caller holds backup_mutex
781 +static void coroutine_fn pvebackup_complete_stream(void *opaque)
782 +{
783 + PVEBackupDevInfo *di = opaque;
784 +
785 + bool error_or_canceled = pvebackup_error_or_canceled();
786 +
787 + if (backup_state.vmaw) {
788 + vma_writer_close_stream(backup_state.vmaw, di->dev_id);
789 + }
790 +
791 + if (backup_state.pbs && !error_or_canceled) {
792 + Error *local_err = NULL;
793 + proxmox_backup_co_close_image(backup_state.pbs, di->dev_id, &local_err);
794 + if (local_err != NULL) {
795 + pvebackup_propagate_error(local_err);
796 + }
797 + }
798 +}
799 +
800 +static void pvebackup_complete_cb(void *opaque, int ret)
801 +{
802 + assert(!qemu_in_coroutine());
803 +
804 + PVEBackupDevInfo *di = opaque;
805 +
806 + qemu_mutex_lock(&backup_state.backup_mutex);
807 +
808 + di->completed = true;
809 +
810 + if (ret < 0) {
811 + Error *local_err = NULL;
812 + error_setg(&local_err, "job failed with err %d - %s", ret, strerror(-ret));
813 + pvebackup_propagate_error(local_err);
814 + }
815 +
816 + di->bs = NULL;
817 +
818 + assert(di->target == NULL);
819 +
820 + block_on_coroutine_fn(pvebackup_complete_stream, di);
821 +
822 + // remove self from job queue
823 + backup_state.di_list = g_list_remove(backup_state.di_list, di);
824 +
825 + g_free(di);
826 +
827 + qemu_mutex_unlock(&backup_state.backup_mutex);
828 +
829 + pvebackup_run_next_job();
830 +}
831 +
832 +static void pvebackup_cancel(void)
833 +{
834 + assert(!qemu_in_coroutine());
835 +
836 + Error *cancel_err = NULL;
837 + error_setg(&cancel_err, "backup canceled");
838 + pvebackup_propagate_error(cancel_err);
839 +
840 + qemu_mutex_lock(&backup_state.backup_mutex);
841 +
842 + if (backup_state.vmaw) {
843 + /* make sure vma writer does not block anymore */
844 + vma_writer_set_error(backup_state.vmaw, "backup canceled");
845 + }
846 +
847 + if (backup_state.pbs) {
848 + proxmox_backup_abort(backup_state.pbs, "backup canceled");
849 + }
850 +
851 + qemu_mutex_unlock(&backup_state.backup_mutex);
852 +
853 + for(;;) {
854 +
855 + BlockJob *next_job = NULL;
856 +
857 + qemu_mutex_lock(&backup_state.backup_mutex);
858 +
859 + GList *l = backup_state.di_list;
860 + while (l) {
861 + PVEBackupDevInfo *di = (PVEBackupDevInfo *)l->data;
862 + l = g_list_next(l);
863 +
864 + BlockJob *job = lookup_active_block_job(di);
865 + if (job != NULL) {
866 + next_job = job;
867 + break;
868 + }
869 + }
870 +
871 + qemu_mutex_unlock(&backup_state.backup_mutex);
872 +
873 + if (next_job) {
874 + job_cancel_sync(&next_job->job, true);
875 + } else {
876 + break;
877 + }
878 + }
879 +}
880 +
881 +void qmp_backup_cancel(Error **errp)
882 +{
883 + pvebackup_cancel();
884 +}
885 +
886 +// assumes the caller holds backup_mutex
887 +static int coroutine_fn pvebackup_co_add_config(
888 + const char *file,
889 + const char *name,
890 + BackupFormat format,
891 + const char *backup_dir,
892 + VmaWriter *vmaw,
893 + ProxmoxBackupHandle *pbs,
894 + Error **errp)
895 +{
896 + int res = 0;
897 +
898 + char *cdata = NULL;
899 + gsize clen = 0;
900 + GError *err = NULL;
901 + if (!g_file_get_contents(file, &cdata, &clen, &err)) {
902 + error_setg(errp, "unable to read file '%s'", file);
903 + return 1;
904 + }
905 +
906 + char *basename = g_path_get_basename(file);
907 + if (name == NULL) name = basename;
908 +
909 + if (format == BACKUP_FORMAT_VMA) {
910 + if (vma_writer_add_config(vmaw, name, cdata, clen) != 0) {
911 + error_setg(errp, "unable to add %s config data to vma archive", file);
912 + goto err;
913 + }
914 + } else if (format == BACKUP_FORMAT_PBS) {
915 + if (proxmox_backup_co_add_config(pbs, name, (unsigned char *)cdata, clen, errp) < 0)
916 + goto err;
917 + } else if (format == BACKUP_FORMAT_DIR) {
918 + char config_path[PATH_MAX];
919 + snprintf(config_path, PATH_MAX, "%s/%s", backup_dir, name);
920 + if (!g_file_set_contents(config_path, cdata, clen, &err)) {
921 + error_setg(errp, "unable to write config file '%s'", config_path);
922 + goto err;
923 + }
924 + }
925 +
926 + out:
927 + g_free(basename);
928 + g_free(cdata);
929 + return res;
930 +
931 + err:
932 + res = -1;
933 + goto out;
934 +}
935 +
936 +bool job_should_pause_locked(Job *job);
937 +
938 +static void pvebackup_run_next_job(void)
939 +{
940 + assert(!qemu_in_coroutine());
941 +
942 + qemu_mutex_lock(&backup_state.backup_mutex);
943 +
944 + GList *l = backup_state.di_list;
945 + while (l) {
946 + PVEBackupDevInfo *di = (PVEBackupDevInfo *)l->data;
947 + l = g_list_next(l);
948 +
949 + BlockJob *job = lookup_active_block_job(di);
950 +
951 + if (job) {
952 + qemu_mutex_unlock(&backup_state.backup_mutex);
953 +
954 + WITH_JOB_LOCK_GUARD() {
955 + if (job_should_pause_locked(&job->job)) {
956 + bool error_or_canceled = pvebackup_error_or_canceled();
957 + if (error_or_canceled) {
958 + job_cancel_sync_locked(&job->job, true);
959 + } else {
960 + job_resume_locked(&job->job);
961 + }
962 + }
963 + }
964 + return;
965 + }
966 + }
967 +
968 + block_on_coroutine_fn(pvebackup_co_cleanup, NULL); // no more jobs, run cleanup
969 +
970 + qemu_mutex_unlock(&backup_state.backup_mutex);
971 +}
972 +
973 +static bool create_backup_jobs(void) {
974 +
975 + assert(!qemu_in_coroutine());
976 +
977 + Error *local_err = NULL;
978 +
979 + BackupPerf perf = { .max_workers = 16 };
980 +
981 + /* create and start all jobs (paused state) */
982 + GList *l = backup_state.di_list;
983 + while (l) {
984 + PVEBackupDevInfo *di = (PVEBackupDevInfo *)l->data;
985 + l = g_list_next(l);
986 +
987 + assert(di->target != NULL);
988 +
989 + AioContext *aio_context = bdrv_get_aio_context(di->bs);
990 + aio_context_acquire(aio_context);
991 +
992 + BlockJob *job = backup_job_create(
993 + NULL, di->bs, di->target, backup_state.speed, MIRROR_SYNC_MODE_FULL, NULL,
994 + BITMAP_SYNC_MODE_NEVER, false, NULL, &perf, BLOCKDEV_ON_ERROR_REPORT, BLOCKDEV_ON_ERROR_REPORT,
995 + JOB_DEFAULT, pvebackup_complete_cb, di, NULL, &local_err);
996 +
997 + aio_context_release(aio_context);
998 +
999 + if (!job || local_err != NULL) {
1000 + Error *create_job_err = NULL;
1001 + error_setg(&create_job_err, "backup_job_create failed: %s",
1002 + local_err ? error_get_pretty(local_err) : "null");
1003 +
1004 + pvebackup_propagate_error(create_job_err);
1005 + break;
1006 + }
1007 + job_start(&job->job);
1008 +
1009 + bdrv_unref(di->target);
1010 + di->target = NULL;
1011 + }
1012 +
1013 + bool errors = pvebackup_error_or_canceled();
1014 +
1015 + if (errors) {
1016 + l = backup_state.di_list;
1017 + while (l) {
1018 + PVEBackupDevInfo *di = (PVEBackupDevInfo *)l->data;
1019 + l = g_list_next(l);
1020 +
1021 + if (di->target) {
1022 + bdrv_unref(di->target);
1023 + di->target = NULL;
1024 + }
1025 + }
1026 + }
1027 +
1028 + return errors;
1029 +}
1030 +
1031 +typedef struct QmpBackupTask {
1032 + const char *backup_file;
1033 + const char *password;
1034 + const char *keyfile;
1035 + const char *key_password;
1036 + const char *backup_id;
1037 + bool has_backup_time;
1038 + const char *fingerprint;
1039 + int64_t backup_time;
1040 + bool has_format;
1041 + BackupFormat format;
1042 + const char *config_file;
1043 + const char *firewall_file;
1044 + const char *devlist;
1045 + bool has_speed;
1046 + int64_t speed;
1047 + Error **errp;
1048 + UuidInfo *result;
1049 +} QmpBackupTask;
1050 +
1051 +// assumes the caller holds backup_mutex
1052 +static void coroutine_fn pvebackup_co_prepare(void *opaque)
1053 +{
1054 + assert(qemu_in_coroutine());
1055 +
1056 + QmpBackupTask *task = opaque;
1057 +
1058 + task->result = NULL; // just to be sure
1059 +
1060 + BlockBackend *blk;
1061 + BlockDriverState *bs = NULL;
1062 + const char *backup_dir = NULL;
1063 + Error *local_err = NULL;
1064 + uuid_t uuid;
1065 + VmaWriter *vmaw = NULL;
1066 + ProxmoxBackupHandle *pbs = NULL;
1067 + gchar **devs = NULL;
1068 + GList *di_list = NULL;
1069 + GList *l;
1070 + UuidInfo *uuid_info;
1071 +
1072 + const char *config_name = "qemu-server.conf";
1073 + const char *firewall_name = "qemu-server.fw";
1074 +
1075 + if (backup_state.di_list) {
1076 + error_set(task->errp, ERROR_CLASS_GENERIC_ERROR,
1077 + "previous backup not finished");
1078 + return;
1079 + }
1080 +
1081 + /* Todo: try to auto-detect format based on file name */
1082 + BackupFormat format = task->has_format ? task->format : BACKUP_FORMAT_VMA;
1083 +
1084 + if (task->devlist) {
1085 + devs = g_strsplit_set(task->devlist, ",;:", -1);
1086 +
1087 + gchar **d = devs;
1088 + while (d && *d) {
1089 + blk = blk_by_name(*d);
1090 + if (blk) {
1091 + bs = blk_bs(blk);
1092 + if (!bdrv_is_inserted(bs)) {
1093 + error_setg(task->errp, QERR_DEVICE_HAS_NO_MEDIUM, *d);
1094 + goto err;
1095 + }
1096 + PVEBackupDevInfo *di = g_new0(PVEBackupDevInfo, 1);
1097 + di->bs = bs;
1098 + di_list = g_list_append(di_list, di);
1099 + } else {
1100 + error_set(task->errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1101 + "Device '%s' not found", *d);
1102 + goto err;
1103 + }
1104 + d++;
1105 + }
1106 +
1107 + } else {
1108 + BdrvNextIterator it;
1109 +
1110 + bs = NULL;
1111 + for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
1112 + if (!bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {
1113 + continue;
1114 + }
1115 +
1116 + PVEBackupDevInfo *di = g_new0(PVEBackupDevInfo, 1);
1117 + di->bs = bs;
1118 + di_list = g_list_append(di_list, di);
1119 + }
1120 + }
1121 +
1122 + if (!di_list) {
1123 + error_set(task->errp, ERROR_CLASS_GENERIC_ERROR, "empty device list");
1124 + goto err;
1125 + }
1126 +
1127 + size_t total = 0;
1128 +
1129 + l = di_list;
1130 + while (l) {
1131 + PVEBackupDevInfo *di = (PVEBackupDevInfo *)l->data;
1132 + l = g_list_next(l);
1133 + if (bdrv_op_is_blocked(di->bs, BLOCK_OP_TYPE_BACKUP_SOURCE, task->errp)) {
1134 + goto err;
1135 + }
1136 +
1137 + ssize_t size = bdrv_getlength(di->bs);
1138 + if (size < 0) {
1139 + error_setg_errno(task->errp, -size, "bdrv_getlength failed");
1140 + goto err;
1141 + }
1142 + di->size = size;
1143 + total += size;
1144 + }
1145 +
1146 + uuid_generate(uuid);
1147 +
1148 + if (format == BACKUP_FORMAT_PBS) {
1149 + if (!task->password) {
1150 + error_set(task->errp, ERROR_CLASS_GENERIC_ERROR, "missing parameter 'password'");
1151 + goto err;
1152 + }
1153 + if (!task->backup_id) {
1154 + error_set(task->errp, ERROR_CLASS_GENERIC_ERROR, "missing parameter 'backup-id'");
1155 + goto err;
1156 + }
1157 + if (!task->has_backup_time) {
1158 + error_set(task->errp, ERROR_CLASS_GENERIC_ERROR, "missing parameter 'backup-time'");
1159 + goto err;
1160 + }
1161 +
1162 + int dump_cb_block_size = PROXMOX_BACKUP_DEFAULT_CHUNK_SIZE; // Hardcoded (4M)
1163 + firewall_name = "fw.conf";
1164 +
1165 + char *pbs_err = NULL;
1166 + pbs = proxmox_backup_new(
1167 + task->backup_file,
1168 + task->backup_id,
1169 + task->backup_time,
1170 + dump_cb_block_size,
1171 + task->password,
1172 + task->keyfile,
1173 + task->key_password,
1174 + task->fingerprint,
1175 + &pbs_err);
1176 +
1177 + if (!pbs) {
1178 + error_set(task->errp, ERROR_CLASS_GENERIC_ERROR,
1179 + "proxmox_backup_new failed: %s", pbs_err);
1180 + proxmox_backup_free_error(pbs_err);
1181 + goto err;
1182 + }
1183 +
1184 + if (proxmox_backup_co_connect(pbs, task->errp) < 0)
1185 + goto err;
1186 +
1187 + /* register all devices */
1188 + l = di_list;
1189 + while (l) {
1190 + PVEBackupDevInfo *di = (PVEBackupDevInfo *)l->data;
1191 + l = g_list_next(l);
1192 +
1193 + const char *devname = bdrv_get_device_name(di->bs);
1194 +
1195 + int dev_id = proxmox_backup_co_register_image(pbs, devname, di->size, task->errp);
1196 + if (dev_id < 0)
1197 + goto err;
1198 +
1199 + if (!(di->target = bdrv_backup_dump_create(dump_cb_block_size, di->size, pvebackup_co_dump_pbs_cb, di, task->errp))) {
1200 + goto err;
1201 + }
1202 +
1203 + di->dev_id = dev_id;
1204 + }
1205 + } else if (format == BACKUP_FORMAT_VMA) {
1206 + vmaw = vma_writer_create(task->backup_file, uuid, &local_err);
1207 + if (!vmaw) {
1208 + if (local_err) {
1209 + error_propagate(task->errp, local_err);
1210 + }
1211 + goto err;
1212 + }
1213 +
1214 + /* register all devices for vma writer */
1215 + l = di_list;
1216 + while (l) {
1217 + PVEBackupDevInfo *di = (PVEBackupDevInfo *)l->data;
1218 + l = g_list_next(l);
1219 +
1220 + if (!(di->target = bdrv_backup_dump_create(VMA_CLUSTER_SIZE, di->size, pvebackup_co_dump_vma_cb, di, task->errp))) {
1221 + goto err;
1222 + }
1223 +
1224 + const char *devname = bdrv_get_device_name(di->bs);
1225 + di->dev_id = vma_writer_register_stream(vmaw, devname, di->size);
1226 + if (di->dev_id <= 0) {
1227 + error_set(task->errp, ERROR_CLASS_GENERIC_ERROR,
1228 + "register_stream failed");
1229 + goto err;
1230 + }
1231 + }
1232 + } else if (format == BACKUP_FORMAT_DIR) {
1233 + if (mkdir(task->backup_file, 0640) != 0) {
1234 + error_setg_errno(task->errp, errno, "can't create directory '%s'\n",
1235 + task->backup_file);
1236 + goto err;
1237 + }
1238 + backup_dir = task->backup_file;
1239 +
1240 + l = di_list;
1241 + while (l) {
1242 + PVEBackupDevInfo *di = (PVEBackupDevInfo *)l->data;
1243 + l = g_list_next(l);
1244 +
1245 + const char *devname = bdrv_get_device_name(di->bs);
1246 + snprintf(di->targetfile, PATH_MAX, "%s/%s.raw", backup_dir, devname);
1247 +
1248 + int flags = BDRV_O_RDWR;
1249 + bdrv_img_create(di->targetfile, "raw", NULL, NULL, NULL,
1250 + di->size, flags, false, &local_err);
1251 + if (local_err) {
1252 + error_propagate(task->errp, local_err);
1253 + goto err;
1254 + }
1255 +
1256 + di->target = bdrv_co_open(di->targetfile, NULL, NULL, flags, &local_err);
1257 + if (!di->target) {
1258 + error_propagate(task->errp, local_err);
1259 + goto err;
1260 + }
1261 + }
1262 + } else {
1263 + error_set(task->errp, ERROR_CLASS_GENERIC_ERROR, "unknown backup format");
1264 + goto err;
1265 + }
1266 +
1267 +
1268 + /* add configuration file to archive */
1269 + if (task->config_file) {
1270 + if (pvebackup_co_add_config(task->config_file, config_name, format, backup_dir,
1271 + vmaw, pbs, task->errp) != 0) {
1272 + goto err;
1273 + }
1274 + }
1275 +
1276 + /* add firewall file to archive */
1277 + if (task->firewall_file) {
1278 + if (pvebackup_co_add_config(task->firewall_file, firewall_name, format, backup_dir,
1279 + vmaw, pbs, task->errp) != 0) {
1280 + goto err;
1281 + }
1282 + }
1283 + /* initialize global backup_state now */
1284 +
1285 + qemu_mutex_lock(&backup_state.stat.lock);
1286 +
1287 + if (backup_state.stat.error) {
1288 + error_free(backup_state.stat.error);
1289 + backup_state.stat.error = NULL;
1290 + }
1291 +
1292 + backup_state.stat.start_time = time(NULL);
1293 + backup_state.stat.end_time = 0;
1294 +
1295 + if (backup_state.stat.backup_file) {
1296 + g_free(backup_state.stat.backup_file);
1297 + }
1298 + backup_state.stat.backup_file = g_strdup(task->backup_file);
1299 +
1300 + uuid_copy(backup_state.stat.uuid, uuid);
1301 + uuid_unparse_lower(uuid, backup_state.stat.uuid_str);
1302 + char *uuid_str = g_strdup(backup_state.stat.uuid_str);
1303 +
1304 + backup_state.stat.total = total;
1305 + backup_state.stat.transferred = 0;
1306 + backup_state.stat.zero_bytes = 0;
1307 +
1308 + qemu_mutex_unlock(&backup_state.stat.lock);
1309 +
1310 + backup_state.speed = (task->has_speed && task->speed > 0) ? task->speed : 0;
1311 +
1312 + backup_state.vmaw = vmaw;
1313 + backup_state.pbs = pbs;
1314 +
1315 + backup_state.di_list = di_list;
1316 +
1317 + uuid_info = g_malloc0(sizeof(*uuid_info));
1318 + uuid_info->UUID = uuid_str;
1319 +
1320 + task->result = uuid_info;
1321 + return;
1322 +
1323 +err:
1324 +
1325 + l = di_list;
1326 + while (l) {
1327 + PVEBackupDevInfo *di = (PVEBackupDevInfo *)l->data;
1328 + l = g_list_next(l);
1329 +
1330 + if (di->target) {
1331 + bdrv_co_unref(di->target);
1332 + }
1333 +
1334 + if (di->targetfile[0]) {
1335 + unlink(di->targetfile);
1336 + }
1337 + g_free(di);
1338 + }
1339 + g_list_free(di_list);
1340 +
1341 + if (devs) {
1342 + g_strfreev(devs);
1343 + }
1344 +
1345 + if (vmaw) {
1346 + Error *err = NULL;
1347 + vma_writer_close(vmaw, &err);
1348 + unlink(task->backup_file);
1349 + }
1350 +
1351 + if (pbs) {
1352 + proxmox_backup_disconnect(pbs);
1353 + }
1354 +
1355 + if (backup_dir) {
1356 + rmdir(backup_dir);
1357 + }
1358 +
1359 + task->result = NULL;
1360 + return;
1361 +}
1362 +
1363 +UuidInfo *qmp_backup(
1364 + const char *backup_file,
1365 + const char *password,
1366 + const char *keyfile,
1367 + const char *key_password,
1368 + const char *fingerprint,
1369 + const char *backup_id,
1370 + bool has_backup_time, int64_t backup_time,
1371 + bool has_format, BackupFormat format,
1372 + const char *config_file,
1373 + const char *firewall_file,
1374 + const char *devlist,
1375 + bool has_speed, int64_t speed, Error **errp)
1376 +{
1377 + QmpBackupTask task = {
1378 + .backup_file = backup_file,
1379 + .password = password,
1380 + .key_password = key_password,
1381 + .fingerprint = fingerprint,
1382 + .backup_id = backup_id,
1383 + .has_backup_time = has_backup_time,
1384 + .backup_time = backup_time,
1385 + .has_format = has_format,
1386 + .format = format,
1387 + .config_file = config_file,
1388 + .firewall_file = firewall_file,
1389 + .devlist = devlist,
1390 + .has_speed = has_speed,
1391 + .speed = speed,
1392 + .errp = errp,
1393 + };
1394 +
1395 + qemu_mutex_lock(&backup_state.backup_mutex);
1396 +
1397 + block_on_coroutine_fn(pvebackup_co_prepare, &task);
1398 +
1399 + if (*errp == NULL) {
1400 + create_backup_jobs();
1401 + qemu_mutex_unlock(&backup_state.backup_mutex);
1402 + pvebackup_run_next_job();
1403 + } else {
1404 + qemu_mutex_unlock(&backup_state.backup_mutex);
1405 + }
1406 +
1407 + return task.result;
1408 +}
1409 +
1410 +BackupStatus *qmp_query_backup(Error **errp)
1411 +{
1412 + BackupStatus *info = g_malloc0(sizeof(*info));
1413 +
1414 + qemu_mutex_lock(&backup_state.stat.lock);
1415 +
1416 + if (!backup_state.stat.start_time) {
1417 + /* not started, return {} */
1418 + qemu_mutex_unlock(&backup_state.stat.lock);
1419 + return info;
1420 + }
1421 +
1422 + info->has_start_time = true;
1423 + info->start_time = backup_state.stat.start_time;
1424 +
1425 + if (backup_state.stat.backup_file) {
1426 + info->backup_file = g_strdup(backup_state.stat.backup_file);
1427 + }
1428 +
1429 + info->uuid = g_strdup(backup_state.stat.uuid_str);
1430 +
1431 + if (backup_state.stat.end_time) {
1432 + if (backup_state.stat.error) {
1433 + info->status = g_strdup("error");
1434 + info->errmsg = g_strdup(error_get_pretty(backup_state.stat.error));
1435 + } else {
1436 + info->status = g_strdup("done");
1437 + }
1438 + info->has_end_time = true;
1439 + info->end_time = backup_state.stat.end_time;
1440 + } else {
1441 + info->status = g_strdup("active");
1442 + }
1443 +
1444 + info->has_total = true;
1445 + info->total = backup_state.stat.total;
1446 + info->has_zero_bytes = true;
1447 + info->zero_bytes = backup_state.stat.zero_bytes;
1448 + info->has_transferred = true;
1449 + info->transferred = backup_state.stat.transferred;
1450 +
1451 + qemu_mutex_unlock(&backup_state.stat.lock);
1452 +
1453 + return info;
1454 +}
1455 diff --git a/qapi/block-core.json b/qapi/block-core.json
1456 index 542add004b..16fb4c5ea0 100644
1457 --- a/qapi/block-core.json
1458 +++ b/qapi/block-core.json
1459 @@ -835,6 +835,115 @@
1460 { 'command': 'query-block', 'returns': ['BlockInfo'],
1461 'allow-preconfig': true }
1462
1463 +##
1464 +# @BackupStatus:
1465 +#
1466 +# Detailed backup status.
1467 +#
1468 +# @status: string describing the current backup status.
1469 +# This can be 'active', 'done', 'error'. If this field is not
1470 +# returned, no backup process has been initiated
1471 +#
1472 +# @errmsg: error message (only returned if status is 'error')
1473 +#
1474 +# @total: total amount of bytes involved in the backup process
1475 +#
1476 +# @transferred: amount of bytes already backed up.
1477 +#
1478 +# @zero-bytes: amount of 'zero' bytes detected.
1479 +#
1480 +# @start-time: time (epoch) when backup job started.
1481 +#
1482 +# @end-time: time (epoch) when backup job finished.
1483 +#
1484 +# @backup-file: backup file name
1485 +#
1486 +# @uuid: uuid for this backup job
1487 +#
1488 +##
1489 +{ 'struct': 'BackupStatus',
1490 + 'data': {'*status': 'str', '*errmsg': 'str', '*total': 'int',
1491 + '*transferred': 'int', '*zero-bytes': 'int',
1492 + '*start-time': 'int', '*end-time': 'int',
1493 + '*backup-file': 'str', '*uuid': 'str' } }
1494 +
1495 +##
1496 +# @BackupFormat:
1497 +#
1498 +# An enumeration of supported backup formats.
1499 +#
1500 +# @vma: Proxmox vma backup format
1501 +##
1502 +{ 'enum': 'BackupFormat',
1503 + 'data': [ 'vma', 'dir', 'pbs' ] }
1504 +
1505 +##
1506 +# @backup:
1507 +#
1508 +# Starts a VM backup.
1509 +#
1510 +# @backup-file: the backup file name
1511 +#
1512 +# @format: format of the backup file
1513 +#
1514 +# @config-file: a configuration file to include into
1515 +# the backup archive.
1516 +#
1517 +# @speed: the maximum speed, in bytes per second
1518 +#
1519 +# @devlist: list of block device names (separated by ',', ';'
1520 +# or ':'). By default the backup includes all writable block devices.
1521 +#
1522 +# @password: backup server passsword (required for format 'pbs')
1523 +#
1524 +# @keyfile: keyfile used for encryption (optional for format 'pbs')
1525 +#
1526 +# @key-password: password for keyfile (optional for format 'pbs')
1527 +#
1528 +# @fingerprint: server cert fingerprint (optional for format 'pbs')
1529 +#
1530 +# @backup-id: backup ID (required for format 'pbs')
1531 +#
1532 +# @backup-time: backup timestamp (Unix epoch, required for format 'pbs')
1533 +#
1534 +# Returns: the uuid of the backup job
1535 +#
1536 +##
1537 +{ 'command': 'backup', 'data': { 'backup-file': 'str',
1538 + '*password': 'str',
1539 + '*keyfile': 'str',
1540 + '*key-password': 'str',
1541 + '*fingerprint': 'str',
1542 + '*backup-id': 'str',
1543 + '*backup-time': 'int',
1544 + '*format': 'BackupFormat',
1545 + '*config-file': 'str',
1546 + '*firewall-file': 'str',
1547 + '*devlist': 'str', '*speed': 'int' },
1548 + 'returns': 'UuidInfo' }
1549 +
1550 +##
1551 +# @query-backup:
1552 +#
1553 +# Returns information about current/last backup task.
1554 +#
1555 +# Returns: @BackupStatus
1556 +#
1557 +##
1558 +{ 'command': 'query-backup', 'returns': 'BackupStatus' }
1559 +
1560 +##
1561 +# @backup-cancel:
1562 +#
1563 +# Cancel the current executing backup process.
1564 +#
1565 +# Returns: nothing on success
1566 +#
1567 +# Notes: This command succeeds even if there is no backup process running.
1568 +#
1569 +##
1570 +{ 'command': 'backup-cancel' }
1571 +
1572 ##
1573 # @BlockDeviceTimedStats:
1574 #
1575 diff --git a/qapi/common.json b/qapi/common.json
1576 index 356db3f670..aae8a3b682 100644
1577 --- a/qapi/common.json
1578 +++ b/qapi/common.json
1579 @@ -206,3 +206,16 @@
1580 ##
1581 { 'struct': 'HumanReadableText',
1582 'data': { 'human-readable-text': 'str' } }
1583 +
1584 +##
1585 +# @UuidInfo:
1586 +#
1587 +# Guest UUID information (Universally Unique Identifier).
1588 +#
1589 +# @UUID: the UUID of the guest
1590 +#
1591 +# Since: 0.14.0
1592 +#
1593 +# Notes: If no UUID was specified for the guest, a null UUID is returned.
1594 +##
1595 +{ 'struct': 'UuidInfo', 'data': {'UUID': 'str'} }
1596 diff --git a/qapi/machine.json b/qapi/machine.json
1597 index 47f3facdb2..46760978ae 100644
1598 --- a/qapi/machine.json
1599 +++ b/qapi/machine.json
1600 @@ -4,6 +4,8 @@
1601 # This work is licensed under the terms of the GNU GPL, version 2 or later.
1602 # See the COPYING file in the top-level directory.
1603
1604 +{ 'include': 'common.json' }
1605 +
1606 ##
1607 # = Machines
1608 ##
1609 @@ -228,19 +230,6 @@
1610 ##
1611 { 'command': 'query-target', 'returns': 'TargetInfo' }
1612
1613 -##
1614 -# @UuidInfo:
1615 -#
1616 -# Guest UUID information (Universally Unique Identifier).
1617 -#
1618 -# @UUID: the UUID of the guest
1619 -#
1620 -# Since: 0.14
1621 -#
1622 -# Notes: If no UUID was specified for the guest, a null UUID is returned.
1623 -##
1624 -{ 'struct': 'UuidInfo', 'data': {'UUID': 'str'} }
1625 -
1626 ##
1627 # @query-uuid:
1628 #