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