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