]> git.proxmox.com Git - mirror_qemu.git/blame - migration/savevm.c
qerror: Move #include out of qerror.h
[mirror_qemu.git] / migration / savevm.c
CommitLineData
a672b469
AL
1/*
2 * QEMU System Emulator
3 *
4 * Copyright (c) 2003-2008 Fabrice Bellard
76cc7b58
JQ
5 * Copyright (c) 2009-2015 Red Hat Inc
6 *
7 * Authors:
8 * Juan Quintela <quintela@redhat.com>
a672b469
AL
9 *
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
16 *
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 * THE SOFTWARE.
27 */
a672b469 28
d40cdb10 29#include "config-host.h"
511d2b14 30#include "qemu-common.h"
abfd9ce3 31#include "hw/boards.h"
511d2b14 32#include "hw/hw.h"
7685ee6a 33#include "hw/qdev.h"
1422e32d 34#include "net/net.h"
83c9089e 35#include "monitor/monitor.h"
9c17d615 36#include "sysemu/sysemu.h"
1de7afc9 37#include "qemu/timer.h"
511d2b14 38#include "audio/audio.h"
caf71f86 39#include "migration/migration.h"
d49b6836 40#include "qemu/error-report.h"
1de7afc9
PB
41#include "qemu/sockets.h"
42#include "qemu/queue.h"
9c17d615 43#include "sysemu/cpus.h"
022c62cb 44#include "exec/memory.h"
a7ae8355 45#include "qmp-commands.h"
517a13c9 46#include "trace.h"
28085f7b 47#include "qemu/iov.h"
de08c606 48#include "block/snapshot.h"
f364ec65 49#include "block/qapi.h"
511d2b14 50
a672b469 51
18995b98 52#ifndef ETH_P_RARP
f8778a77 53#define ETH_P_RARP 0x8035
18995b98
N
54#endif
55#define ARP_HTYPE_ETH 0x0001
56#define ARP_PTYPE_IP 0x0800
57#define ARP_OP_REQUEST_REV 0x3
58
37fb569c
DDAG
59static bool skip_section_footers;
60
18995b98 61static int announce_self_create(uint8_t *buf,
5cecf414 62 uint8_t *mac_addr)
a672b469 63{
18995b98
N
64 /* Ethernet header. */
65 memset(buf, 0xff, 6); /* destination MAC addr */
66 memcpy(buf + 6, mac_addr, 6); /* source MAC addr */
67 *(uint16_t *)(buf + 12) = htons(ETH_P_RARP); /* ethertype */
68
69 /* RARP header. */
70 *(uint16_t *)(buf + 14) = htons(ARP_HTYPE_ETH); /* hardware addr space */
71 *(uint16_t *)(buf + 16) = htons(ARP_PTYPE_IP); /* protocol addr space */
72 *(buf + 18) = 6; /* hardware addr length (ethernet) */
73 *(buf + 19) = 4; /* protocol addr length (IPv4) */
74 *(uint16_t *)(buf + 20) = htons(ARP_OP_REQUEST_REV); /* opcode */
75 memcpy(buf + 22, mac_addr, 6); /* source hw addr */
76 memset(buf + 28, 0x00, 4); /* source protocol addr */
77 memcpy(buf + 32, mac_addr, 6); /* target hw addr */
78 memset(buf + 38, 0x00, 4); /* target protocol addr */
79
80 /* Padding to get up to 60 bytes (ethernet min packet size, minus FCS). */
81 memset(buf + 42, 0x00, 18);
82
83 return 60; /* len (FCS will be added by hardware) */
a672b469
AL
84}
85
f401ca22 86static void qemu_announce_self_iter(NICState *nic, void *opaque)
a672b469 87{
18995b98 88 uint8_t buf[60];
f401ca22
MM
89 int len;
90
9013dca5 91 trace_qemu_announce_self_iter(qemu_ether_ntoa(&nic->conf->macaddr));
f401ca22
MM
92 len = announce_self_create(buf, nic->conf->macaddr.a);
93
b356f76d 94 qemu_send_packet_raw(qemu_get_queue(nic), buf, len);
f401ca22
MM
95}
96
97
98static void qemu_announce_self_once(void *opaque)
99{
ed8b330b
GN
100 static int count = SELF_ANNOUNCE_ROUNDS;
101 QEMUTimer *timer = *(QEMUTimer **)opaque;
a672b469 102
f401ca22
MM
103 qemu_foreach_nic(qemu_announce_self_iter, NULL);
104
18995b98
N
105 if (--count) {
106 /* delay 50ms, 150ms, 250ms, ... */
bc72ad67 107 timer_mod(timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) +
508e1180 108 self_announce_delay(count));
ed8b330b 109 } else {
5cecf414
EH
110 timer_del(timer);
111 timer_free(timer);
ed8b330b
GN
112 }
113}
114
115void qemu_announce_self(void)
116{
5cecf414
EH
117 static QEMUTimer *timer;
118 timer = timer_new_ms(QEMU_CLOCK_REALTIME, qemu_announce_self_once, &timer);
119 qemu_announce_self_once(&timer);
a672b469
AL
120}
121
122/***********************************************************/
123/* savevm/loadvm support */
124
05fcc848
KW
125static ssize_t block_writev_buffer(void *opaque, struct iovec *iov, int iovcnt,
126 int64_t pos)
127{
128 int ret;
129 QEMUIOVector qiov;
130
131 qemu_iovec_init_external(&qiov, iov, iovcnt);
132 ret = bdrv_writev_vmstate(opaque, &qiov, pos);
133 if (ret < 0) {
134 return ret;
135 }
136
137 return qiov.size;
138}
139
178e08a5 140static int block_put_buffer(void *opaque, const uint8_t *buf,
a672b469
AL
141 int64_t pos, int size)
142{
45566e9c 143 bdrv_save_vmstate(opaque, buf, pos, size);
a672b469
AL
144 return size;
145}
146
178e08a5 147static int block_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
a672b469 148{
45566e9c 149 return bdrv_load_vmstate(opaque, buf, pos, size);
a672b469
AL
150}
151
152static int bdrv_fclose(void *opaque)
153{
ad492c92 154 return bdrv_flush(opaque);
a672b469
AL
155}
156
9229bf3c
PB
157static const QEMUFileOps bdrv_read_ops = {
158 .get_buffer = block_get_buffer,
159 .close = bdrv_fclose
160};
161
162static const QEMUFileOps bdrv_write_ops = {
05fcc848
KW
163 .put_buffer = block_put_buffer,
164 .writev_buffer = block_writev_buffer,
165 .close = bdrv_fclose
9229bf3c
PB
166};
167
45566e9c 168static QEMUFile *qemu_fopen_bdrv(BlockDriverState *bs, int is_writable)
a672b469 169{
38ff78d3 170 if (is_writable) {
9229bf3c 171 return qemu_fopen_ops(bs, &bdrv_write_ops);
38ff78d3 172 }
9229bf3c 173 return qemu_fopen_ops(bs, &bdrv_read_ops);
a672b469
AL
174}
175
2ff68d07 176
bb1a6d8c
EH
177/* QEMUFile timer support.
178 * Not in qemu-file.c to not add qemu-timer.c as dependency to qemu-file.c
179 */
2ff68d07 180
40daca54 181void timer_put(QEMUFile *f, QEMUTimer *ts)
2ff68d07
PB
182{
183 uint64_t expire_time;
184
e93379b0 185 expire_time = timer_expire_time_ns(ts);
2ff68d07
PB
186 qemu_put_be64(f, expire_time);
187}
188
40daca54 189void timer_get(QEMUFile *f, QEMUTimer *ts)
2ff68d07
PB
190{
191 uint64_t expire_time;
192
193 expire_time = qemu_get_be64(f);
194 if (expire_time != -1) {
bc72ad67 195 timer_mod_ns(ts, expire_time);
2ff68d07 196 } else {
bc72ad67 197 timer_del(ts);
2ff68d07
PB
198 }
199}
200
201
bb1a6d8c
EH
202/* VMState timer support.
203 * Not in vmstate.c to not add qemu-timer.c as dependency to vmstate.c
204 */
dde0463b
JQ
205
206static int get_timer(QEMUFile *f, void *pv, size_t size)
207{
208 QEMUTimer *v = pv;
40daca54 209 timer_get(f, v);
dde0463b
JQ
210 return 0;
211}
212
84e2e3eb 213static void put_timer(QEMUFile *f, void *pv, size_t size)
dde0463b 214{
84e2e3eb 215 QEMUTimer *v = pv;
40daca54 216 timer_put(f, v);
dde0463b
JQ
217}
218
219const VMStateInfo vmstate_info_timer = {
220 .name = "timer",
221 .get = get_timer,
222 .put = put_timer,
223};
224
08e99e29 225
7685ee6a
AW
226typedef struct CompatEntry {
227 char idstr[256];
228 int instance_id;
229} CompatEntry;
230
a672b469 231typedef struct SaveStateEntry {
72cf2d4f 232 QTAILQ_ENTRY(SaveStateEntry) entry;
a672b469
AL
233 char idstr[256];
234 int instance_id;
4d2ffa08 235 int alias_id;
a672b469
AL
236 int version_id;
237 int section_id;
22ea40f4 238 SaveVMHandlers *ops;
9ed7d6ae 239 const VMStateDescription *vmsd;
a672b469 240 void *opaque;
7685ee6a 241 CompatEntry *compat;
a7ae8355 242 int is_ram;
a672b469
AL
243} SaveStateEntry;
244
0163a2e0
JQ
245typedef struct SaveState {
246 QTAILQ_HEAD(, SaveStateEntry) handlers;
247 int global_section_id;
248} SaveState;
249
250static SaveState savevm_state = {
251 .handlers = QTAILQ_HEAD_INITIALIZER(savevm_state.handlers),
252 .global_section_id = 0,
253};
a672b469 254
abfd9ce3
AS
255static void dump_vmstate_vmsd(FILE *out_file,
256 const VMStateDescription *vmsd, int indent,
257 bool is_subsection);
258
259static void dump_vmstate_vmsf(FILE *out_file, const VMStateField *field,
260 int indent)
261{
262 fprintf(out_file, "%*s{\n", indent, "");
263 indent += 2;
264 fprintf(out_file, "%*s\"field\": \"%s\",\n", indent, "", field->name);
265 fprintf(out_file, "%*s\"version_id\": %d,\n", indent, "",
266 field->version_id);
267 fprintf(out_file, "%*s\"field_exists\": %s,\n", indent, "",
268 field->field_exists ? "true" : "false");
269 fprintf(out_file, "%*s\"size\": %zu", indent, "", field->size);
270 if (field->vmsd != NULL) {
271 fprintf(out_file, ",\n");
272 dump_vmstate_vmsd(out_file, field->vmsd, indent, false);
273 }
274 fprintf(out_file, "\n%*s}", indent - 2, "");
275}
276
277static void dump_vmstate_vmss(FILE *out_file,
5cd8cada 278 const VMStateDescription **subsection,
abfd9ce3
AS
279 int indent)
280{
5cd8cada
JQ
281 if (*subsection != NULL) {
282 dump_vmstate_vmsd(out_file, *subsection, indent, true);
abfd9ce3
AS
283 }
284}
285
286static void dump_vmstate_vmsd(FILE *out_file,
287 const VMStateDescription *vmsd, int indent,
288 bool is_subsection)
289{
290 if (is_subsection) {
291 fprintf(out_file, "%*s{\n", indent, "");
292 } else {
293 fprintf(out_file, "%*s\"%s\": {\n", indent, "", "Description");
294 }
295 indent += 2;
296 fprintf(out_file, "%*s\"name\": \"%s\",\n", indent, "", vmsd->name);
297 fprintf(out_file, "%*s\"version_id\": %d,\n", indent, "",
298 vmsd->version_id);
299 fprintf(out_file, "%*s\"minimum_version_id\": %d", indent, "",
300 vmsd->minimum_version_id);
301 if (vmsd->fields != NULL) {
302 const VMStateField *field = vmsd->fields;
303 bool first;
304
305 fprintf(out_file, ",\n%*s\"Fields\": [\n", indent, "");
306 first = true;
307 while (field->name != NULL) {
308 if (field->flags & VMS_MUST_EXIST) {
309 /* Ignore VMSTATE_VALIDATE bits; these don't get migrated */
310 field++;
311 continue;
312 }
313 if (!first) {
314 fprintf(out_file, ",\n");
315 }
316 dump_vmstate_vmsf(out_file, field, indent + 2);
317 field++;
318 first = false;
319 }
320 fprintf(out_file, "\n%*s]", indent, "");
321 }
322 if (vmsd->subsections != NULL) {
5cd8cada 323 const VMStateDescription **subsection = vmsd->subsections;
abfd9ce3
AS
324 bool first;
325
326 fprintf(out_file, ",\n%*s\"Subsections\": [\n", indent, "");
327 first = true;
5cd8cada 328 while (*subsection != NULL) {
abfd9ce3
AS
329 if (!first) {
330 fprintf(out_file, ",\n");
331 }
332 dump_vmstate_vmss(out_file, subsection, indent + 2);
333 subsection++;
334 first = false;
335 }
336 fprintf(out_file, "\n%*s]", indent, "");
337 }
338 fprintf(out_file, "\n%*s}", indent - 2, "");
339}
340
341static void dump_machine_type(FILE *out_file)
342{
343 MachineClass *mc;
344
345 mc = MACHINE_GET_CLASS(current_machine);
346
347 fprintf(out_file, " \"vmschkmachine\": {\n");
348 fprintf(out_file, " \"Name\": \"%s\"\n", mc->name);
349 fprintf(out_file, " },\n");
350}
351
352void dump_vmstate_json_to_file(FILE *out_file)
353{
354 GSList *list, *elt;
355 bool first;
356
357 fprintf(out_file, "{\n");
358 dump_machine_type(out_file);
359
360 first = true;
361 list = object_class_get_list(TYPE_DEVICE, true);
362 for (elt = list; elt; elt = elt->next) {
363 DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, elt->data,
364 TYPE_DEVICE);
365 const char *name;
366 int indent = 2;
367
368 if (!dc->vmsd) {
369 continue;
370 }
371
372 if (!first) {
373 fprintf(out_file, ",\n");
374 }
375 name = object_class_get_name(OBJECT_CLASS(dc));
376 fprintf(out_file, "%*s\"%s\": {\n", indent, "", name);
377 indent += 2;
378 fprintf(out_file, "%*s\"Name\": \"%s\",\n", indent, "", name);
379 fprintf(out_file, "%*s\"version_id\": %d,\n", indent, "",
380 dc->vmsd->version_id);
381 fprintf(out_file, "%*s\"minimum_version_id\": %d,\n", indent, "",
382 dc->vmsd->minimum_version_id);
383
384 dump_vmstate_vmsd(out_file, dc->vmsd, indent, false);
385
386 fprintf(out_file, "\n%*s}", indent - 2, "");
387 first = false;
388 }
389 fprintf(out_file, "\n}\n");
390 fclose(out_file);
391}
392
8718e999
JQ
393static int calculate_new_instance_id(const char *idstr)
394{
395 SaveStateEntry *se;
396 int instance_id = 0;
397
0163a2e0 398 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
8718e999
JQ
399 if (strcmp(idstr, se->idstr) == 0
400 && instance_id <= se->instance_id) {
401 instance_id = se->instance_id + 1;
402 }
403 }
404 return instance_id;
405}
406
7685ee6a
AW
407static int calculate_compat_instance_id(const char *idstr)
408{
409 SaveStateEntry *se;
410 int instance_id = 0;
411
0163a2e0 412 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
38ff78d3 413 if (!se->compat) {
7685ee6a 414 continue;
38ff78d3 415 }
7685ee6a
AW
416
417 if (strcmp(idstr, se->compat->idstr) == 0
418 && instance_id <= se->compat->instance_id) {
419 instance_id = se->compat->instance_id + 1;
420 }
421 }
422 return instance_id;
423}
424
a672b469
AL
425/* TODO: Individual devices generally have very little idea about the rest
426 of the system, so instance_id should be removed/replaced.
427 Meanwhile pass -1 as instance_id if you do not already have a clearly
428 distinguishing id for all instances of your device class. */
0be71e32
AW
429int register_savevm_live(DeviceState *dev,
430 const char *idstr,
a672b469
AL
431 int instance_id,
432 int version_id,
7908c78d 433 SaveVMHandlers *ops,
a672b469
AL
434 void *opaque)
435{
8718e999 436 SaveStateEntry *se;
a672b469 437
7267c094 438 se = g_malloc0(sizeof(SaveStateEntry));
a672b469 439 se->version_id = version_id;
0163a2e0 440 se->section_id = savevm_state.global_section_id++;
7908c78d 441 se->ops = ops;
a672b469 442 se->opaque = opaque;
9ed7d6ae 443 se->vmsd = NULL;
a7ae8355 444 /* if this is a live_savem then set is_ram */
16310a3c 445 if (ops->save_live_setup != NULL) {
a7ae8355
SS
446 se->is_ram = 1;
447 }
a672b469 448
09e5ab63
AL
449 if (dev) {
450 char *id = qdev_get_dev_path(dev);
7685ee6a
AW
451 if (id) {
452 pstrcpy(se->idstr, sizeof(se->idstr), id);
453 pstrcat(se->idstr, sizeof(se->idstr), "/");
7267c094 454 g_free(id);
7685ee6a 455
7267c094 456 se->compat = g_malloc0(sizeof(CompatEntry));
7685ee6a
AW
457 pstrcpy(se->compat->idstr, sizeof(se->compat->idstr), idstr);
458 se->compat->instance_id = instance_id == -1 ?
459 calculate_compat_instance_id(idstr) : instance_id;
460 instance_id = -1;
461 }
462 }
463 pstrcat(se->idstr, sizeof(se->idstr), idstr);
464
8718e999 465 if (instance_id == -1) {
7685ee6a 466 se->instance_id = calculate_new_instance_id(se->idstr);
8718e999
JQ
467 } else {
468 se->instance_id = instance_id;
a672b469 469 }
7685ee6a 470 assert(!se->compat || se->instance_id == 0);
8718e999 471 /* add at the end of list */
0163a2e0 472 QTAILQ_INSERT_TAIL(&savevm_state.handlers, se, entry);
a672b469
AL
473 return 0;
474}
475
0be71e32
AW
476int register_savevm(DeviceState *dev,
477 const char *idstr,
a672b469
AL
478 int instance_id,
479 int version_id,
480 SaveStateHandler *save_state,
481 LoadStateHandler *load_state,
482 void *opaque)
483{
7908c78d
JQ
484 SaveVMHandlers *ops = g_malloc0(sizeof(SaveVMHandlers));
485 ops->save_state = save_state;
486 ops->load_state = load_state;
0be71e32 487 return register_savevm_live(dev, idstr, instance_id, version_id,
7908c78d 488 ops, opaque);
a672b469
AL
489}
490
0be71e32 491void unregister_savevm(DeviceState *dev, const char *idstr, void *opaque)
41bd13af 492{
8718e999 493 SaveStateEntry *se, *new_se;
7685ee6a
AW
494 char id[256] = "";
495
09e5ab63
AL
496 if (dev) {
497 char *path = qdev_get_dev_path(dev);
7685ee6a
AW
498 if (path) {
499 pstrcpy(id, sizeof(id), path);
500 pstrcat(id, sizeof(id), "/");
7267c094 501 g_free(path);
7685ee6a
AW
502 }
503 }
504 pstrcat(id, sizeof(id), idstr);
41bd13af 505
0163a2e0 506 QTAILQ_FOREACH_SAFE(se, &savevm_state.handlers, entry, new_se) {
7685ee6a 507 if (strcmp(se->idstr, id) == 0 && se->opaque == opaque) {
0163a2e0 508 QTAILQ_REMOVE(&savevm_state.handlers, se, entry);
69e58af9 509 if (se->compat) {
7267c094 510 g_free(se->compat);
69e58af9 511 }
22ea40f4 512 g_free(se->ops);
7267c094 513 g_free(se);
41bd13af 514 }
41bd13af
AL
515 }
516}
517
0be71e32 518int vmstate_register_with_alias_id(DeviceState *dev, int instance_id,
4d2ffa08
JK
519 const VMStateDescription *vmsd,
520 void *opaque, int alias_id,
521 int required_for_version)
9ed7d6ae 522{
8718e999 523 SaveStateEntry *se;
9ed7d6ae 524
4d2ffa08
JK
525 /* If this triggers, alias support can be dropped for the vmsd. */
526 assert(alias_id == -1 || required_for_version >= vmsd->minimum_version_id);
527
7267c094 528 se = g_malloc0(sizeof(SaveStateEntry));
9ed7d6ae 529 se->version_id = vmsd->version_id;
0163a2e0 530 se->section_id = savevm_state.global_section_id++;
9ed7d6ae
JQ
531 se->opaque = opaque;
532 se->vmsd = vmsd;
4d2ffa08 533 se->alias_id = alias_id;
9ed7d6ae 534
09e5ab63
AL
535 if (dev) {
536 char *id = qdev_get_dev_path(dev);
7685ee6a
AW
537 if (id) {
538 pstrcpy(se->idstr, sizeof(se->idstr), id);
539 pstrcat(se->idstr, sizeof(se->idstr), "/");
7267c094 540 g_free(id);
7685ee6a 541
7267c094 542 se->compat = g_malloc0(sizeof(CompatEntry));
7685ee6a
AW
543 pstrcpy(se->compat->idstr, sizeof(se->compat->idstr), vmsd->name);
544 se->compat->instance_id = instance_id == -1 ?
545 calculate_compat_instance_id(vmsd->name) : instance_id;
546 instance_id = -1;
547 }
548 }
549 pstrcat(se->idstr, sizeof(se->idstr), vmsd->name);
550
8718e999 551 if (instance_id == -1) {
7685ee6a 552 se->instance_id = calculate_new_instance_id(se->idstr);
8718e999
JQ
553 } else {
554 se->instance_id = instance_id;
9ed7d6ae 555 }
7685ee6a 556 assert(!se->compat || se->instance_id == 0);
8718e999 557 /* add at the end of list */
0163a2e0 558 QTAILQ_INSERT_TAIL(&savevm_state.handlers, se, entry);
9ed7d6ae
JQ
559 return 0;
560}
561
0be71e32
AW
562void vmstate_unregister(DeviceState *dev, const VMStateDescription *vmsd,
563 void *opaque)
9ed7d6ae 564{
1eb7538b
JQ
565 SaveStateEntry *se, *new_se;
566
0163a2e0 567 QTAILQ_FOREACH_SAFE(se, &savevm_state.handlers, entry, new_se) {
1eb7538b 568 if (se->vmsd == vmsd && se->opaque == opaque) {
0163a2e0 569 QTAILQ_REMOVE(&savevm_state.handlers, se, entry);
69e58af9 570 if (se->compat) {
7267c094 571 g_free(se->compat);
69e58af9 572 }
7267c094 573 g_free(se);
1eb7538b
JQ
574 }
575 }
9ed7d6ae
JQ
576}
577
4082be4d
JQ
578static int vmstate_load(QEMUFile *f, SaveStateEntry *se, int version_id)
579{
9013dca5 580 trace_vmstate_load(se->idstr, se->vmsd ? se->vmsd->name : "(old)");
9ed7d6ae 581 if (!se->vmsd) { /* Old style */
22ea40f4 582 return se->ops->load_state(f, se->opaque, version_id);
9ed7d6ae
JQ
583 }
584 return vmstate_load_state(f, se->vmsd, se->opaque, version_id);
4082be4d
JQ
585}
586
8118f095
AG
587static void vmstate_save_old_style(QEMUFile *f, SaveStateEntry *se, QJSON *vmdesc)
588{
589 int64_t old_offset, size;
590
591 old_offset = qemu_ftell_fast(f);
592 se->ops->save_state(f, se->opaque);
593 size = qemu_ftell_fast(f) - old_offset;
594
595 if (vmdesc) {
596 json_prop_int(vmdesc, "size", size);
597 json_start_array(vmdesc, "fields");
598 json_start_object(vmdesc, NULL);
599 json_prop_str(vmdesc, "name", "data");
600 json_prop_int(vmdesc, "size", size);
601 json_prop_str(vmdesc, "type", "buffer");
602 json_end_object(vmdesc);
603 json_end_array(vmdesc);
604 }
605}
606
607static void vmstate_save(QEMUFile *f, SaveStateEntry *se, QJSON *vmdesc)
4082be4d 608{
9013dca5 609 trace_vmstate_save(se->idstr, se->vmsd ? se->vmsd->name : "(old)");
8118f095
AG
610 if (!se->vmsd) {
611 vmstate_save_old_style(f, se, vmdesc);
dc912121 612 return;
9ed7d6ae 613 }
8118f095 614 vmstate_save_state(f, se->vmsd, se->opaque, vmdesc);
4082be4d
JQ
615}
616
37fb569c
DDAG
617void savevm_skip_section_footers(void)
618{
619 skip_section_footers = true;
620}
621
ce39bfc9
DDAG
622/*
623 * Write the header for device section (QEMU_VM_SECTION START/END/PART/FULL)
624 */
625static void save_section_header(QEMUFile *f, SaveStateEntry *se,
626 uint8_t section_type)
627{
628 qemu_put_byte(f, section_type);
629 qemu_put_be32(f, se->section_id);
630
631 if (section_type == QEMU_VM_SECTION_FULL ||
632 section_type == QEMU_VM_SECTION_START) {
633 /* ID string */
634 size_t len = strlen(se->idstr);
635 qemu_put_byte(f, len);
636 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
637
638 qemu_put_be32(f, se->instance_id);
639 qemu_put_be32(f, se->version_id);
640 }
641}
642
f68945d4
DDAG
643/*
644 * Write a footer onto device sections that catches cases misformatted device
645 * sections.
646 */
647static void save_section_footer(QEMUFile *f, SaveStateEntry *se)
648{
649 if (!skip_section_footers) {
650 qemu_put_byte(f, QEMU_VM_SECTION_FOOTER);
651 qemu_put_be32(f, se->section_id);
652 }
653}
654
655/*
656 * Read a footer off the wire and check that it matches the expected section
657 *
658 * Returns: true if the footer was good
659 * false if there is a problem (and calls error_report to say why)
660 */
661static bool check_section_footer(QEMUFile *f, SaveStateEntry *se)
662{
663 uint8_t read_mark;
664 uint32_t read_section_id;
665
666 if (skip_section_footers) {
667 /* No footer to check */
668 return true;
669 }
670
671 read_mark = qemu_get_byte(f);
672
673 if (read_mark != QEMU_VM_SECTION_FOOTER) {
674 error_report("Missing section footer for %s", se->idstr);
675 return false;
676 }
677
678 read_section_id = qemu_get_be32(f);
679 if (read_section_id != se->section_id) {
680 error_report("Mismatched section id in footer for %s -"
681 " read 0x%x expected 0x%x",
682 se->idstr, read_section_id, se->section_id);
683 return false;
684 }
685
686 /* All good */
687 return true;
688}
689
e1c37d0e 690bool qemu_savevm_state_blocked(Error **errp)
dc912121
AW
691{
692 SaveStateEntry *se;
693
0163a2e0 694 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
7d854c47 695 if (se->vmsd && se->vmsd->unmigratable) {
f231b88d
CR
696 error_setg(errp, "State blocked by non-migratable device '%s'",
697 se->idstr);
dc912121
AW
698 return true;
699 }
700 }
701 return false;
702}
703
f796baa1
DDAG
704void qemu_savevm_state_header(QEMUFile *f)
705{
706 trace_savevm_state_header();
707 qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
708 qemu_put_be32(f, QEMU_VM_FILE_VERSION);
709}
710
47c8c17a
PB
711void qemu_savevm_state_begin(QEMUFile *f,
712 const MigrationParams *params)
a672b469
AL
713{
714 SaveStateEntry *se;
39346385 715 int ret;
a672b469 716
9013dca5 717 trace_savevm_state_begin();
0163a2e0 718 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
22ea40f4 719 if (!se->ops || !se->ops->set_params) {
c163b5ca 720 continue;
6607ae23 721 }
22ea40f4 722 se->ops->set_params(params, se->opaque);
c163b5ca 723 }
38ff78d3 724
0163a2e0 725 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
d1315aac 726 if (!se->ops || !se->ops->save_live_setup) {
a672b469 727 continue;
22ea40f4 728 }
6bd68781
JQ
729 if (se->ops && se->ops->is_active) {
730 if (!se->ops->is_active(se->opaque)) {
731 continue;
732 }
733 }
ce39bfc9 734 save_section_header(f, se, QEMU_VM_SECTION_START);
a672b469 735
d1315aac 736 ret = se->ops->save_live_setup(f, se->opaque);
f68945d4 737 save_section_footer(f, se);
2975725f 738 if (ret < 0) {
47c8c17a
PB
739 qemu_file_set_error(f, ret);
740 break;
2975725f 741 }
a672b469 742 }
a672b469
AL
743}
744
39346385 745/*
07f35073 746 * this function has three return values:
39346385
JQ
747 * negative: there was one error, and we have -errno.
748 * 0 : We haven't finished, caller have to go again
749 * 1 : We have finished, we can go to complete phase
750 */
539de124 751int qemu_savevm_state_iterate(QEMUFile *f)
a672b469
AL
752{
753 SaveStateEntry *se;
754 int ret = 1;
755
9013dca5 756 trace_savevm_state_iterate();
0163a2e0 757 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
16310a3c 758 if (!se->ops || !se->ops->save_live_iterate) {
a672b469 759 continue;
22ea40f4 760 }
6bd68781
JQ
761 if (se->ops && se->ops->is_active) {
762 if (!se->ops->is_active(se->opaque)) {
763 continue;
764 }
765 }
aac844ed
JQ
766 if (qemu_file_rate_limit(f)) {
767 return 0;
768 }
464400f6 769 trace_savevm_section_start(se->idstr, se->section_id);
ce39bfc9
DDAG
770
771 save_section_header(f, se, QEMU_VM_SECTION_PART);
a672b469 772
16310a3c 773 ret = se->ops->save_live_iterate(f, se->opaque);
a5df2a02 774 trace_savevm_section_end(se->idstr, se->section_id, ret);
f68945d4 775 save_section_footer(f, se);
517a13c9 776
47c8c17a
PB
777 if (ret < 0) {
778 qemu_file_set_error(f, ret);
779 }
2975725f 780 if (ret <= 0) {
90697be8
JK
781 /* Do not proceed to the next vmstate before this one reported
782 completion of the current stage. This serializes the migration
783 and reduces the probability that a faster changing state is
784 synchronized over and over again. */
785 break;
786 }
a672b469 787 }
39346385 788 return ret;
a672b469
AL
789}
790
9850c604
AG
791static bool should_send_vmdesc(void)
792{
793 MachineState *machine = MACHINE(qdev_get_machine());
794 return !machine->suppress_vmdesc;
795}
796
47c8c17a 797void qemu_savevm_state_complete(QEMUFile *f)
a672b469 798{
8118f095
AG
799 QJSON *vmdesc;
800 int vmdesc_len;
a672b469 801 SaveStateEntry *se;
2975725f 802 int ret;
a672b469 803
9013dca5
AK
804 trace_savevm_state_complete();
805
ea375f9a
JK
806 cpu_synchronize_all_states();
807
0163a2e0 808 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
16310a3c 809 if (!se->ops || !se->ops->save_live_complete) {
a672b469 810 continue;
22ea40f4 811 }
6bd68781
JQ
812 if (se->ops && se->ops->is_active) {
813 if (!se->ops->is_active(se->opaque)) {
814 continue;
815 }
816 }
464400f6 817 trace_savevm_section_start(se->idstr, se->section_id);
ce39bfc9
DDAG
818
819 save_section_header(f, se, QEMU_VM_SECTION_END);
a672b469 820
16310a3c 821 ret = se->ops->save_live_complete(f, se->opaque);
a5df2a02 822 trace_savevm_section_end(se->idstr, se->section_id, ret);
f68945d4 823 save_section_footer(f, se);
2975725f 824 if (ret < 0) {
47c8c17a
PB
825 qemu_file_set_error(f, ret);
826 return;
2975725f 827 }
a672b469
AL
828 }
829
8118f095
AG
830 vmdesc = qjson_new();
831 json_prop_int(vmdesc, "page_size", TARGET_PAGE_SIZE);
832 json_start_array(vmdesc, "devices");
0163a2e0 833 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
a672b469 834
22ea40f4 835 if ((!se->ops || !se->ops->save_state) && !se->vmsd) {
5cecf414 836 continue;
22ea40f4 837 }
464400f6 838 trace_savevm_section_start(se->idstr, se->section_id);
8118f095
AG
839
840 json_start_object(vmdesc, NULL);
841 json_prop_str(vmdesc, "name", se->idstr);
842 json_prop_int(vmdesc, "instance_id", se->instance_id);
843
ce39bfc9 844 save_section_header(f, se, QEMU_VM_SECTION_FULL);
a672b469 845
8118f095
AG
846 vmstate_save(f, se, vmdesc);
847
848 json_end_object(vmdesc);
a5df2a02 849 trace_savevm_section_end(se->idstr, se->section_id, 0);
f68945d4 850 save_section_footer(f, se);
a672b469
AL
851 }
852
853 qemu_put_byte(f, QEMU_VM_EOF);
8118f095
AG
854
855 json_end_array(vmdesc);
856 qjson_finish(vmdesc);
857 vmdesc_len = strlen(qjson_get_str(vmdesc));
858
9850c604
AG
859 if (should_send_vmdesc()) {
860 qemu_put_byte(f, QEMU_VM_VMDESCRIPTION);
861 qemu_put_be32(f, vmdesc_len);
862 qemu_put_buffer(f, (uint8_t *)qjson_get_str(vmdesc), vmdesc_len);
863 }
8118f095
AG
864 object_unref(OBJECT(vmdesc));
865
edaae611 866 qemu_fflush(f);
a672b469
AL
867}
868
e4ed1541
JQ
869uint64_t qemu_savevm_state_pending(QEMUFile *f, uint64_t max_size)
870{
871 SaveStateEntry *se;
872 uint64_t ret = 0;
873
0163a2e0 874 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
e4ed1541
JQ
875 if (!se->ops || !se->ops->save_live_pending) {
876 continue;
877 }
878 if (se->ops && se->ops->is_active) {
879 if (!se->ops->is_active(se->opaque)) {
880 continue;
881 }
882 }
883 ret += se->ops->save_live_pending(f, se->opaque, max_size);
884 }
885 return ret;
886}
887
6522773f 888void qemu_savevm_state_cancel(void)
4ec7fcc7
JK
889{
890 SaveStateEntry *se;
891
9013dca5 892 trace_savevm_state_cancel();
0163a2e0 893 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
9b5bfab0
JQ
894 if (se->ops && se->ops->cancel) {
895 se->ops->cancel(se->opaque);
4ec7fcc7
JK
896 }
897 }
898}
899
5d80448c 900static int qemu_savevm_state(QEMUFile *f, Error **errp)
a672b469 901{
a672b469 902 int ret;
6607ae23
IY
903 MigrationParams params = {
904 .blk = 0,
905 .shared = 0
906 };
a672b469 907
5d80448c 908 if (qemu_savevm_state_blocked(errp)) {
04943eba 909 return -EINVAL;
dc912121
AW
910 }
911
9b095037 912 qemu_mutex_unlock_iothread();
f796baa1 913 qemu_savevm_state_header(f);
47c8c17a 914 qemu_savevm_state_begin(f, &params);
9b095037
PB
915 qemu_mutex_lock_iothread();
916
47c8c17a
PB
917 while (qemu_file_get_error(f) == 0) {
918 if (qemu_savevm_state_iterate(f) > 0) {
919 break;
920 }
921 }
a672b469 922
47c8c17a 923 ret = qemu_file_get_error(f);
39346385 924 if (ret == 0) {
47c8c17a 925 qemu_savevm_state_complete(f);
624b9cc2 926 ret = qemu_file_get_error(f);
39346385 927 }
04943eba
PB
928 if (ret != 0) {
929 qemu_savevm_state_cancel();
5d80448c 930 error_setg_errno(errp, -ret, "Error while writing VM state");
04943eba 931 }
a672b469
AL
932 return ret;
933}
934
a7ae8355
SS
935static int qemu_save_device_state(QEMUFile *f)
936{
937 SaveStateEntry *se;
938
939 qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
940 qemu_put_be32(f, QEMU_VM_FILE_VERSION);
941
942 cpu_synchronize_all_states();
943
0163a2e0 944 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
a7ae8355
SS
945 if (se->is_ram) {
946 continue;
947 }
22ea40f4 948 if ((!se->ops || !se->ops->save_state) && !se->vmsd) {
a7ae8355
SS
949 continue;
950 }
951
ce39bfc9 952 save_section_header(f, se, QEMU_VM_SECTION_FULL);
a7ae8355 953
8118f095 954 vmstate_save(f, se, NULL);
f68945d4
DDAG
955
956 save_section_footer(f, se);
a7ae8355
SS
957 }
958
959 qemu_put_byte(f, QEMU_VM_EOF);
960
961 return qemu_file_get_error(f);
962}
963
a672b469
AL
964static SaveStateEntry *find_se(const char *idstr, int instance_id)
965{
966 SaveStateEntry *se;
967
0163a2e0 968 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
a672b469 969 if (!strcmp(se->idstr, idstr) &&
4d2ffa08
JK
970 (instance_id == se->instance_id ||
971 instance_id == se->alias_id))
a672b469 972 return se;
7685ee6a
AW
973 /* Migrating from an older version? */
974 if (strstr(se->idstr, idstr) && se->compat) {
975 if (!strcmp(se->compat->idstr, idstr) &&
976 (instance_id == se->compat->instance_id ||
977 instance_id == se->alias_id))
978 return se;
979 }
a672b469
AL
980 }
981 return NULL;
982}
983
1a8f46f8 984struct LoadStateEntry {
72cf2d4f 985 QLIST_ENTRY(LoadStateEntry) entry;
a672b469
AL
986 SaveStateEntry *se;
987 int section_id;
988 int version_id;
1a8f46f8 989};
a672b469 990
1a8f46f8 991void loadvm_free_handlers(MigrationIncomingState *mis)
a672b469 992{
f4dbb8dd 993 LoadStateEntry *le, *new_le;
1a8f46f8
DDAG
994
995 QLIST_FOREACH_SAFE(le, &mis->loadvm_handlers, entry, new_le) {
996 QLIST_REMOVE(le, entry);
997 g_free(le);
998 }
999}
1000
1001int qemu_loadvm_state(QEMUFile *f)
1002{
1003 MigrationIncomingState *mis = migration_incoming_get_current();
0457d073 1004 Error *local_err = NULL;
a672b469
AL
1005 uint8_t section_type;
1006 unsigned int v;
1007 int ret;
1925cebc 1008 int file_error_after_eof = -1;
a672b469 1009
0457d073 1010 if (qemu_savevm_state_blocked(&local_err)) {
19867549 1011 error_report_err(local_err);
dc912121
AW
1012 return -EINVAL;
1013 }
1014
a672b469 1015 v = qemu_get_be32(f);
38ff78d3 1016 if (v != QEMU_VM_FILE_MAGIC) {
0457d073 1017 error_report("Not a migration stream");
a672b469 1018 return -EINVAL;
38ff78d3 1019 }
a672b469
AL
1020
1021 v = qemu_get_be32(f);
bbfe1408 1022 if (v == QEMU_VM_FILE_VERSION_COMPAT) {
6a64b644 1023 error_report("SaveVM v2 format is obsolete and don't work anymore");
bbfe1408
JQ
1024 return -ENOTSUP;
1025 }
38ff78d3 1026 if (v != QEMU_VM_FILE_VERSION) {
0457d073 1027 error_report("Unsupported migration stream version");
a672b469 1028 return -ENOTSUP;
38ff78d3 1029 }
a672b469
AL
1030
1031 while ((section_type = qemu_get_byte(f)) != QEMU_VM_EOF) {
1032 uint32_t instance_id, version_id, section_id;
a672b469 1033 SaveStateEntry *se;
1a8f46f8 1034 LoadStateEntry *le;
b3af1bc9 1035 char idstr[256];
a672b469 1036
a5df2a02 1037 trace_qemu_loadvm_state_section(section_type);
a672b469
AL
1038 switch (section_type) {
1039 case QEMU_VM_SECTION_START:
1040 case QEMU_VM_SECTION_FULL:
1041 /* Read section start */
1042 section_id = qemu_get_be32(f);
b3af1bc9
DDAG
1043 if (!qemu_get_counted_string(f, idstr)) {
1044 error_report("Unable to read ID string for section %u",
1045 section_id);
1046 return -EINVAL;
1047 }
a672b469
AL
1048 instance_id = qemu_get_be32(f);
1049 version_id = qemu_get_be32(f);
1050
a5df2a02
DDAG
1051 trace_qemu_loadvm_state_section_startfull(section_id, idstr,
1052 instance_id, version_id);
a672b469
AL
1053 /* Find savevm section */
1054 se = find_se(idstr, instance_id);
1055 if (se == NULL) {
6a64b644
DDAG
1056 error_report("Unknown savevm section or instance '%s' %d",
1057 idstr, instance_id);
a672b469
AL
1058 ret = -EINVAL;
1059 goto out;
1060 }
1061
1062 /* Validate version */
1063 if (version_id > se->version_id) {
6a64b644
DDAG
1064 error_report("savevm: unsupported version %d for '%s' v%d",
1065 version_id, idstr, se->version_id);
a672b469
AL
1066 ret = -EINVAL;
1067 goto out;
1068 }
1069
1070 /* Add entry */
7267c094 1071 le = g_malloc0(sizeof(*le));
a672b469
AL
1072
1073 le->se = se;
1074 le->section_id = section_id;
1075 le->version_id = version_id;
1a8f46f8 1076 QLIST_INSERT_HEAD(&mis->loadvm_handlers, le, entry);
a672b469 1077
4082be4d 1078 ret = vmstate_load(f, le->se, le->version_id);
b5a22e4a 1079 if (ret < 0) {
6a64b644
DDAG
1080 error_report("error while loading state for instance 0x%x of"
1081 " device '%s'", instance_id, idstr);
b5a22e4a
JQ
1082 goto out;
1083 }
f68945d4
DDAG
1084 if (!check_section_footer(f, le->se)) {
1085 ret = -EINVAL;
1086 goto out;
1087 }
a672b469
AL
1088 break;
1089 case QEMU_VM_SECTION_PART:
1090 case QEMU_VM_SECTION_END:
1091 section_id = qemu_get_be32(f);
1092
a5df2a02 1093 trace_qemu_loadvm_state_section_partend(section_id);
1a8f46f8 1094 QLIST_FOREACH(le, &mis->loadvm_handlers, entry) {
f4dbb8dd
JQ
1095 if (le->section_id == section_id) {
1096 break;
1097 }
1098 }
a672b469 1099 if (le == NULL) {
6a64b644 1100 error_report("Unknown savevm section %d", section_id);
a672b469
AL
1101 ret = -EINVAL;
1102 goto out;
1103 }
1104
4082be4d 1105 ret = vmstate_load(f, le->se, le->version_id);
b5a22e4a 1106 if (ret < 0) {
6a64b644
DDAG
1107 error_report("error while loading state section id %d(%s)",
1108 section_id, le->se->idstr);
b5a22e4a
JQ
1109 goto out;
1110 }
f68945d4
DDAG
1111 if (!check_section_footer(f, le->se)) {
1112 ret = -EINVAL;
1113 goto out;
1114 }
a672b469
AL
1115 break;
1116 default:
6a64b644 1117 error_report("Unknown savevm section type %d", section_type);
a672b469
AL
1118 ret = -EINVAL;
1119 goto out;
1120 }
1121 }
1122
1925cebc
AG
1123 file_error_after_eof = qemu_file_get_error(f);
1124
1125 /*
1126 * Try to read in the VMDESC section as well, so that dumping tools that
1127 * intercept our migration stream have the chance to see it.
1128 */
1129 if (qemu_get_byte(f) == QEMU_VM_VMDESCRIPTION) {
1130 uint32_t size = qemu_get_be32(f);
1131 uint8_t *buf = g_malloc(0x1000);
1132
1133 while (size > 0) {
1134 uint32_t read_chunk = MIN(size, 0x1000);
1135 qemu_get_buffer(f, buf, read_chunk);
1136 size -= read_chunk;
1137 }
1138 g_free(buf);
1139 }
1140
ea375f9a
JK
1141 cpu_synchronize_all_post_init();
1142
a672b469
AL
1143 ret = 0;
1144
1145out:
42802d47 1146 if (ret == 0) {
1925cebc
AG
1147 /* We may not have a VMDESC section, so ignore relative errors */
1148 ret = file_error_after_eof;
624b9cc2 1149 }
a672b469
AL
1150
1151 return ret;
1152}
1153
29d78271
SH
1154static BlockDriverState *find_vmstate_bs(void)
1155{
1156 BlockDriverState *bs = NULL;
1157 while ((bs = bdrv_next(bs))) {
1158 if (bdrv_can_snapshot(bs)) {
1159 return bs;
1160 }
1161 }
1162 return NULL;
1163}
1164
cb499fb2
KW
1165/*
1166 * Deletes snapshots of a given name in all opened images.
1167 */
1168static int del_existing_snapshots(Monitor *mon, const char *name)
1169{
1170 BlockDriverState *bs;
cb499fb2 1171 QEMUSnapshotInfo sn1, *snapshot = &sn1;
a89d89d3 1172 Error *err = NULL;
cb499fb2 1173
dbc13590
MA
1174 bs = NULL;
1175 while ((bs = bdrv_next(bs))) {
cb499fb2 1176 if (bdrv_can_snapshot(bs) &&
38ff78d3 1177 bdrv_snapshot_find(bs, snapshot, name) >= 0) {
a89d89d3 1178 bdrv_snapshot_delete_by_id_or_name(bs, name, &err);
84d18f06 1179 if (err) {
cb499fb2 1180 monitor_printf(mon,
a89d89d3
WX
1181 "Error while deleting snapshot on device '%s':"
1182 " %s\n",
1183 bdrv_get_device_name(bs),
1184 error_get_pretty(err));
1185 error_free(err);
cb499fb2
KW
1186 return -1;
1187 }
1188 }
1189 }
1190
1191 return 0;
1192}
1193
3e5a50d6 1194void hmp_savevm(Monitor *mon, const QDict *qdict)
a672b469
AL
1195{
1196 BlockDriverState *bs, *bs1;
1197 QEMUSnapshotInfo sn1, *sn = &sn1, old_sn1, *old_sn = &old_sn1;
cb499fb2 1198 int ret;
a672b469
AL
1199 QEMUFile *f;
1200 int saved_vm_running;
c2c9a466 1201 uint64_t vm_state_size;
68b891ec 1202 qemu_timeval tv;
7d631a11 1203 struct tm tm;
d54908a5 1204 const char *name = qdict_get_try_str(qdict, "name");
5d80448c 1205 Error *local_err = NULL;
a672b469 1206
feeee5ac 1207 /* Verify if there is a device that doesn't support snapshots and is writable */
dbc13590
MA
1208 bs = NULL;
1209 while ((bs = bdrv_next(bs))) {
feeee5ac 1210
07b70bfb 1211 if (!bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {
feeee5ac
MDCF
1212 continue;
1213 }
1214
1215 if (!bdrv_can_snapshot(bs)) {
1216 monitor_printf(mon, "Device '%s' is writable but does not support snapshots.\n",
1217 bdrv_get_device_name(bs));
1218 return;
1219 }
1220 }
1221
29d78271 1222 bs = find_vmstate_bs();
a672b469 1223 if (!bs) {
376253ec 1224 monitor_printf(mon, "No block device can accept snapshots\n");
a672b469
AL
1225 return;
1226 }
a672b469 1227
1354869c 1228 saved_vm_running = runstate_is_running();
0461d5a6 1229 vm_stop(RUN_STATE_SAVE_VM);
a672b469 1230
cb499fb2 1231 memset(sn, 0, sizeof(*sn));
a672b469
AL
1232
1233 /* fill auxiliary fields */
68b891ec 1234 qemu_gettimeofday(&tv);
a672b469
AL
1235 sn->date_sec = tv.tv_sec;
1236 sn->date_nsec = tv.tv_usec * 1000;
bc72ad67 1237 sn->vm_clock_nsec = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
a672b469 1238
7d631a11
MDCF
1239 if (name) {
1240 ret = bdrv_snapshot_find(bs, old_sn, name);
1241 if (ret >= 0) {
1242 pstrcpy(sn->name, sizeof(sn->name), old_sn->name);
1243 pstrcpy(sn->id_str, sizeof(sn->id_str), old_sn->id_str);
1244 } else {
1245 pstrcpy(sn->name, sizeof(sn->name), name);
1246 }
1247 } else {
d7d9b528
BS
1248 /* cast below needed for OpenBSD where tv_sec is still 'long' */
1249 localtime_r((const time_t *)&tv.tv_sec, &tm);
7d631a11 1250 strftime(sn->name, sizeof(sn->name), "vm-%Y%m%d%H%M%S", &tm);
7d631a11
MDCF
1251 }
1252
cb499fb2 1253 /* Delete old snapshots of the same name */
f139a412 1254 if (name && del_existing_snapshots(mon, name) < 0) {
cb499fb2
KW
1255 goto the_end;
1256 }
1257
a672b469 1258 /* save the VM state */
45566e9c 1259 f = qemu_fopen_bdrv(bs, 1);
a672b469 1260 if (!f) {
376253ec 1261 monitor_printf(mon, "Could not open VM state file\n");
a672b469
AL
1262 goto the_end;
1263 }
5d80448c 1264 ret = qemu_savevm_state(f, &local_err);
2d22b18f 1265 vm_state_size = qemu_ftell(f);
a672b469
AL
1266 qemu_fclose(f);
1267 if (ret < 0) {
5d80448c
KW
1268 monitor_printf(mon, "%s\n", error_get_pretty(local_err));
1269 error_free(local_err);
a672b469
AL
1270 goto the_end;
1271 }
1272
1273 /* create the snapshots */
1274
dbc13590
MA
1275 bs1 = NULL;
1276 while ((bs1 = bdrv_next(bs1))) {
feeee5ac 1277 if (bdrv_can_snapshot(bs1)) {
2d22b18f
AL
1278 /* Write VM state size only to the image that contains the state */
1279 sn->vm_state_size = (bs == bs1 ? vm_state_size : 0);
a672b469
AL
1280 ret = bdrv_snapshot_create(bs1, sn);
1281 if (ret < 0) {
376253ec
AL
1282 monitor_printf(mon, "Error while creating snapshot on '%s'\n",
1283 bdrv_get_device_name(bs1));
a672b469
AL
1284 }
1285 }
1286 }
1287
1288 the_end:
38ff78d3 1289 if (saved_vm_running) {
a672b469 1290 vm_start();
38ff78d3 1291 }
a672b469
AL
1292}
1293
a7ae8355
SS
1294void qmp_xen_save_devices_state(const char *filename, Error **errp)
1295{
1296 QEMUFile *f;
1297 int saved_vm_running;
1298 int ret;
1299
1300 saved_vm_running = runstate_is_running();
1301 vm_stop(RUN_STATE_SAVE_VM);
1302
1303 f = qemu_fopen(filename, "wb");
1304 if (!f) {
1befce96 1305 error_setg_file_open(errp, errno, filename);
a7ae8355
SS
1306 goto the_end;
1307 }
1308 ret = qemu_save_device_state(f);
1309 qemu_fclose(f);
1310 if (ret < 0) {
c6bd8c70 1311 error_setg(errp, QERR_IO_ERROR);
a7ae8355
SS
1312 }
1313
1314 the_end:
38ff78d3 1315 if (saved_vm_running) {
a7ae8355 1316 vm_start();
38ff78d3 1317 }
a7ae8355
SS
1318}
1319
03cd4655 1320int load_vmstate(const char *name)
a672b469 1321{
f0aa7a8b 1322 BlockDriverState *bs, *bs_vm_state;
2d22b18f 1323 QEMUSnapshotInfo sn;
a672b469 1324 QEMUFile *f;
751c6a17 1325 int ret;
a672b469 1326
29d78271 1327 bs_vm_state = find_vmstate_bs();
f0aa7a8b
MDCF
1328 if (!bs_vm_state) {
1329 error_report("No block device supports snapshots");
1330 return -ENOTSUP;
1331 }
1332
1333 /* Don't even try to load empty VM states */
1334 ret = bdrv_snapshot_find(bs_vm_state, &sn, name);
1335 if (ret < 0) {
1336 return ret;
1337 } else if (sn.vm_state_size == 0) {
e11480db
KW
1338 error_report("This is a disk-only snapshot. Revert to it offline "
1339 "using qemu-img.");
f0aa7a8b
MDCF
1340 return -EINVAL;
1341 }
1342
1343 /* Verify if there is any device that doesn't support snapshots and is
1344 writable and check if the requested snapshot is available too. */
dbc13590
MA
1345 bs = NULL;
1346 while ((bs = bdrv_next(bs))) {
feeee5ac 1347
07b70bfb 1348 if (!bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {
feeee5ac
MDCF
1349 continue;
1350 }
1351
1352 if (!bdrv_can_snapshot(bs)) {
1353 error_report("Device '%s' is writable but does not support snapshots.",
1354 bdrv_get_device_name(bs));
1355 return -ENOTSUP;
1356 }
feeee5ac 1357
f0aa7a8b
MDCF
1358 ret = bdrv_snapshot_find(bs, &sn, name);
1359 if (ret < 0) {
1360 error_report("Device '%s' does not have the requested snapshot '%s'",
1361 bdrv_get_device_name(bs), name);
1362 return ret;
1363 }
a672b469
AL
1364 }
1365
1366 /* Flush all IO requests so they don't interfere with the new state. */
922453bc 1367 bdrv_drain_all();
a672b469 1368
f0aa7a8b
MDCF
1369 bs = NULL;
1370 while ((bs = bdrv_next(bs))) {
1371 if (bdrv_can_snapshot(bs)) {
1372 ret = bdrv_snapshot_goto(bs, name);
a672b469 1373 if (ret < 0) {
f0aa7a8b
MDCF
1374 error_report("Error %d while activating snapshot '%s' on '%s'",
1375 ret, name, bdrv_get_device_name(bs));
1376 return ret;
a672b469
AL
1377 }
1378 }
1379 }
1380
a672b469 1381 /* restore the VM state */
f0aa7a8b 1382 f = qemu_fopen_bdrv(bs_vm_state, 0);
a672b469 1383 if (!f) {
1ecda02b 1384 error_report("Could not open VM state file");
05f2401e 1385 return -EINVAL;
a672b469 1386 }
f0aa7a8b 1387
5a8a49d7 1388 qemu_system_reset(VMRESET_SILENT);
bca7856a 1389 migration_incoming_state_new(f);
a672b469 1390 ret = qemu_loadvm_state(f);
f0aa7a8b 1391
a672b469 1392 qemu_fclose(f);
bca7856a 1393 migration_incoming_state_destroy();
a672b469 1394 if (ret < 0) {
1ecda02b 1395 error_report("Error %d while loading VM state", ret);
05f2401e 1396 return ret;
a672b469 1397 }
f0aa7a8b 1398
05f2401e 1399 return 0;
7b630349
JQ
1400}
1401
3e5a50d6 1402void hmp_delvm(Monitor *mon, const QDict *qdict)
a672b469 1403{
af957387 1404 BlockDriverState *bs;
ba2b2288 1405 Error *err;
d54908a5 1406 const char *name = qdict_get_str(qdict, "name");
a672b469 1407
af957387 1408 if (!find_vmstate_bs()) {
376253ec 1409 monitor_printf(mon, "No block device supports snapshots\n");
a672b469
AL
1410 return;
1411 }
1412
af957387
ZH
1413 bs = NULL;
1414 while ((bs = bdrv_next(bs))) {
1415 if (bdrv_can_snapshot(bs)) {
ba2b2288 1416 err = NULL;
a89d89d3 1417 bdrv_snapshot_delete_by_id_or_name(bs, name, &err);
84d18f06 1418 if (err) {
a89d89d3
WX
1419 monitor_printf(mon,
1420 "Error while deleting snapshot on device '%s':"
1421 " %s\n",
1422 bdrv_get_device_name(bs),
1423 error_get_pretty(err));
1424 error_free(err);
a672b469
AL
1425 }
1426 }
1427 }
1428}
1429
1ce6be24 1430void hmp_info_snapshots(Monitor *mon, const QDict *qdict)
a672b469
AL
1431{
1432 BlockDriverState *bs, *bs1;
f9209915
MDCF
1433 QEMUSnapshotInfo *sn_tab, *sn, s, *sn_info = &s;
1434 int nb_sns, i, ret, available;
1435 int total;
1436 int *available_snapshots;
a672b469 1437
29d78271 1438 bs = find_vmstate_bs();
a672b469 1439 if (!bs) {
376253ec 1440 monitor_printf(mon, "No available block device supports snapshots\n");
a672b469
AL
1441 return;
1442 }
a672b469
AL
1443
1444 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
1445 if (nb_sns < 0) {
376253ec 1446 monitor_printf(mon, "bdrv_snapshot_list: error %d\n", nb_sns);
a672b469
AL
1447 return;
1448 }
f9209915
MDCF
1449
1450 if (nb_sns == 0) {
1451 monitor_printf(mon, "There is no snapshot available.\n");
1452 return;
1453 }
1454
7267c094 1455 available_snapshots = g_malloc0(sizeof(int) * nb_sns);
f9209915
MDCF
1456 total = 0;
1457 for (i = 0; i < nb_sns; i++) {
a672b469 1458 sn = &sn_tab[i];
f9209915
MDCF
1459 available = 1;
1460 bs1 = NULL;
1461
1462 while ((bs1 = bdrv_next(bs1))) {
1463 if (bdrv_can_snapshot(bs1) && bs1 != bs) {
1464 ret = bdrv_snapshot_find(bs1, sn_info, sn->id_str);
1465 if (ret < 0) {
1466 available = 0;
1467 break;
1468 }
1469 }
1470 }
1471
1472 if (available) {
1473 available_snapshots[total] = i;
1474 total++;
1475 }
a672b469 1476 }
f9209915
MDCF
1477
1478 if (total > 0) {
5b917044
WX
1479 bdrv_snapshot_dump((fprintf_function)monitor_printf, mon, NULL);
1480 monitor_printf(mon, "\n");
f9209915
MDCF
1481 for (i = 0; i < total; i++) {
1482 sn = &sn_tab[available_snapshots[i]];
5b917044
WX
1483 bdrv_snapshot_dump((fprintf_function)monitor_printf, mon, sn);
1484 monitor_printf(mon, "\n");
f9209915
MDCF
1485 }
1486 } else {
1487 monitor_printf(mon, "There is no suitable snapshot available\n");
1488 }
1489
7267c094
AL
1490 g_free(sn_tab);
1491 g_free(available_snapshots);
f9209915 1492
a672b469 1493}
c5705a77
AK
1494
1495void vmstate_register_ram(MemoryRegion *mr, DeviceState *dev)
1496{
1ddde087 1497 qemu_ram_set_idstr(memory_region_get_ram_addr(mr) & TARGET_PAGE_MASK,
c5705a77
AK
1498 memory_region_name(mr), dev);
1499}
1500
1501void vmstate_unregister_ram(MemoryRegion *mr, DeviceState *dev)
1502{
b0e56e0b 1503 qemu_ram_unset_idstr(memory_region_get_ram_addr(mr) & TARGET_PAGE_MASK);
c5705a77
AK
1504}
1505
1506void vmstate_register_ram_global(MemoryRegion *mr)
1507{
1508 vmstate_register_ram(mr, NULL);
1509}