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