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