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