xref: /qemu/migration/savevm.c (revision 46517dd4)
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 "qemu/osdep.h"
30 #include "hw/boards.h"
31 #include "hw/xen/xen.h"
32 #include "net/net.h"
33 #include "migration.h"
34 #include "migration/snapshot.h"
35 #include "migration/vmstate.h"
36 #include "migration/misc.h"
37 #include "migration/register.h"
38 #include "migration/global_state.h"
39 #include "ram.h"
40 #include "qemu-file-channel.h"
41 #include "qemu-file.h"
42 #include "savevm.h"
43 #include "postcopy-ram.h"
44 #include "qapi/error.h"
45 #include "qapi/qapi-commands-migration.h"
46 #include "qapi/qapi-commands-misc.h"
47 #include "qapi/qmp/qerror.h"
48 #include "qemu/error-report.h"
49 #include "sysemu/cpus.h"
50 #include "exec/memory.h"
51 #include "exec/target_page.h"
52 #include "trace.h"
53 #include "qemu/iov.h"
54 #include "qemu/main-loop.h"
55 #include "block/snapshot.h"
56 #include "qemu/cutils.h"
57 #include "io/channel-buffer.h"
58 #include "io/channel-file.h"
59 #include "sysemu/replay.h"
60 #include "sysemu/sysemu.h"
61 #include "qjson.h"
62 #include "migration/colo.h"
63 #include "qemu/bitmap.h"
64 #include "net/announce.h"
65 
66 const unsigned int postcopy_ram_discard_version = 0;
67 
68 /* Subcommands for QEMU_VM_COMMAND */
69 enum qemu_vm_cmd {
70     MIG_CMD_INVALID = 0,   /* Must be 0 */
71     MIG_CMD_OPEN_RETURN_PATH,  /* Tell the dest to open the Return path */
72     MIG_CMD_PING,              /* Request a PONG on the RP */
73 
74     MIG_CMD_POSTCOPY_ADVISE,       /* Prior to any page transfers, just
75                                       warn we might want to do PC */
76     MIG_CMD_POSTCOPY_LISTEN,       /* Start listening for incoming
77                                       pages as it's running. */
78     MIG_CMD_POSTCOPY_RUN,          /* Start execution */
79 
80     MIG_CMD_POSTCOPY_RAM_DISCARD,  /* A list of pages to discard that
81                                       were previously sent during
82                                       precopy but are dirty. */
83     MIG_CMD_PACKAGED,          /* Send a wrapped stream within this stream */
84     MIG_CMD_ENABLE_COLO,       /* Enable COLO */
85     MIG_CMD_POSTCOPY_RESUME,   /* resume postcopy on dest */
86     MIG_CMD_RECV_BITMAP,       /* Request for recved bitmap on dst */
87     MIG_CMD_MAX
88 };
89 
90 #define MAX_VM_CMD_PACKAGED_SIZE UINT32_MAX
91 static struct mig_cmd_args {
92     ssize_t     len; /* -1 = variable */
93     const char *name;
94 } mig_cmd_args[] = {
95     [MIG_CMD_INVALID]          = { .len = -1, .name = "INVALID" },
96     [MIG_CMD_OPEN_RETURN_PATH] = { .len =  0, .name = "OPEN_RETURN_PATH" },
97     [MIG_CMD_PING]             = { .len = sizeof(uint32_t), .name = "PING" },
98     [MIG_CMD_POSTCOPY_ADVISE]  = { .len = -1, .name = "POSTCOPY_ADVISE" },
99     [MIG_CMD_POSTCOPY_LISTEN]  = { .len =  0, .name = "POSTCOPY_LISTEN" },
100     [MIG_CMD_POSTCOPY_RUN]     = { .len =  0, .name = "POSTCOPY_RUN" },
101     [MIG_CMD_POSTCOPY_RAM_DISCARD] = {
102                                    .len = -1, .name = "POSTCOPY_RAM_DISCARD" },
103     [MIG_CMD_POSTCOPY_RESUME]  = { .len =  0, .name = "POSTCOPY_RESUME" },
104     [MIG_CMD_PACKAGED]         = { .len =  4, .name = "PACKAGED" },
105     [MIG_CMD_RECV_BITMAP]      = { .len = -1, .name = "RECV_BITMAP" },
106     [MIG_CMD_MAX]              = { .len = -1, .name = "MAX" },
107 };
108 
109 /* Note for MIG_CMD_POSTCOPY_ADVISE:
110  * The format of arguments is depending on postcopy mode:
111  * - postcopy RAM only
112  *   uint64_t host page size
113  *   uint64_t taget page size
114  *
115  * - postcopy RAM and postcopy dirty bitmaps
116  *   format is the same as for postcopy RAM only
117  *
118  * - postcopy dirty bitmaps only
119  *   Nothing. Command length field is 0.
120  *
121  * Be careful: adding a new postcopy entity with some other parameters should
122  * not break format self-description ability. Good way is to introduce some
123  * generic extendable format with an exception for two old entities.
124  */
125 
126 /***********************************************************/
127 /* savevm/loadvm support */
128 
129 static ssize_t block_writev_buffer(void *opaque, struct iovec *iov, int iovcnt,
130                                    int64_t pos)
131 {
132     int ret;
133     QEMUIOVector qiov;
134 
135     qemu_iovec_init_external(&qiov, iov, iovcnt);
136     ret = bdrv_writev_vmstate(opaque, &qiov, pos);
137     if (ret < 0) {
138         return ret;
139     }
140 
141     return qiov.size;
142 }
143 
144 static ssize_t block_get_buffer(void *opaque, uint8_t *buf, int64_t pos,
145                                 size_t size)
146 {
147     return bdrv_load_vmstate(opaque, buf, pos, size);
148 }
149 
150 static int bdrv_fclose(void *opaque)
151 {
152     return bdrv_flush(opaque);
153 }
154 
155 static const QEMUFileOps bdrv_read_ops = {
156     .get_buffer = block_get_buffer,
157     .close =      bdrv_fclose
158 };
159 
160 static const QEMUFileOps bdrv_write_ops = {
161     .writev_buffer  = block_writev_buffer,
162     .close          = bdrv_fclose
163 };
164 
165 static QEMUFile *qemu_fopen_bdrv(BlockDriverState *bs, int is_writable)
166 {
167     if (is_writable) {
168         return qemu_fopen_ops(bs, &bdrv_write_ops);
169     }
170     return qemu_fopen_ops(bs, &bdrv_read_ops);
171 }
172 
173 
174 /* QEMUFile timer support.
175  * Not in qemu-file.c to not add qemu-timer.c as dependency to qemu-file.c
176  */
177 
178 void timer_put(QEMUFile *f, QEMUTimer *ts)
179 {
180     uint64_t expire_time;
181 
182     expire_time = timer_expire_time_ns(ts);
183     qemu_put_be64(f, expire_time);
184 }
185 
186 void timer_get(QEMUFile *f, QEMUTimer *ts)
187 {
188     uint64_t expire_time;
189 
190     expire_time = qemu_get_be64(f);
191     if (expire_time != -1) {
192         timer_mod_ns(ts, expire_time);
193     } else {
194         timer_del(ts);
195     }
196 }
197 
198 
199 /* VMState timer support.
200  * Not in vmstate.c to not add qemu-timer.c as dependency to vmstate.c
201  */
202 
203 static int get_timer(QEMUFile *f, void *pv, size_t size,
204                      const VMStateField *field)
205 {
206     QEMUTimer *v = pv;
207     timer_get(f, v);
208     return 0;
209 }
210 
211 static int put_timer(QEMUFile *f, void *pv, size_t size,
212                      const VMStateField *field, QJSON *vmdesc)
213 {
214     QEMUTimer *v = pv;
215     timer_put(f, v);
216 
217     return 0;
218 }
219 
220 const VMStateInfo vmstate_info_timer = {
221     .name = "timer",
222     .get  = get_timer,
223     .put  = put_timer,
224 };
225 
226 
227 typedef struct CompatEntry {
228     char idstr[256];
229     int instance_id;
230 } CompatEntry;
231 
232 typedef struct SaveStateEntry {
233     QTAILQ_ENTRY(SaveStateEntry) entry;
234     char idstr[256];
235     int instance_id;
236     int alias_id;
237     int version_id;
238     /* version id read from the stream */
239     int load_version_id;
240     int section_id;
241     /* section id read from the stream */
242     int load_section_id;
243     const SaveVMHandlers *ops;
244     const VMStateDescription *vmsd;
245     void *opaque;
246     CompatEntry *compat;
247     int is_ram;
248 } SaveStateEntry;
249 
250 typedef struct SaveState {
251     QTAILQ_HEAD(, SaveStateEntry) handlers;
252     int global_section_id;
253     uint32_t len;
254     const char *name;
255     uint32_t target_page_bits;
256     uint32_t caps_count;
257     MigrationCapability *capabilities;
258 } SaveState;
259 
260 static SaveState savevm_state = {
261     .handlers = QTAILQ_HEAD_INITIALIZER(savevm_state.handlers),
262     .global_section_id = 0,
263 };
264 
265 static bool should_validate_capability(int capability)
266 {
267     assert(capability >= 0 && capability < MIGRATION_CAPABILITY__MAX);
268     /* Validate only new capabilities to keep compatibility. */
269     switch (capability) {
270     case MIGRATION_CAPABILITY_X_IGNORE_SHARED:
271         return true;
272     default:
273         return false;
274     }
275 }
276 
277 static uint32_t get_validatable_capabilities_count(void)
278 {
279     MigrationState *s = migrate_get_current();
280     uint32_t result = 0;
281     int i;
282     for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
283         if (should_validate_capability(i) && s->enabled_capabilities[i]) {
284             result++;
285         }
286     }
287     return result;
288 }
289 
290 static int configuration_pre_save(void *opaque)
291 {
292     SaveState *state = opaque;
293     const char *current_name = MACHINE_GET_CLASS(current_machine)->name;
294     MigrationState *s = migrate_get_current();
295     int i, j;
296 
297     state->len = strlen(current_name);
298     state->name = current_name;
299     state->target_page_bits = qemu_target_page_bits();
300 
301     state->caps_count = get_validatable_capabilities_count();
302     state->capabilities = g_renew(MigrationCapability, state->capabilities,
303                                   state->caps_count);
304     for (i = j = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
305         if (should_validate_capability(i) && s->enabled_capabilities[i]) {
306             state->capabilities[j++] = i;
307         }
308     }
309 
310     return 0;
311 }
312 
313 static int configuration_pre_load(void *opaque)
314 {
315     SaveState *state = opaque;
316 
317     /* If there is no target-page-bits subsection it means the source
318      * predates the variable-target-page-bits support and is using the
319      * minimum possible value for this CPU.
320      */
321     state->target_page_bits = qemu_target_page_bits_min();
322     return 0;
323 }
324 
325 static bool configuration_validate_capabilities(SaveState *state)
326 {
327     bool ret = true;
328     MigrationState *s = migrate_get_current();
329     unsigned long *source_caps_bm;
330     int i;
331 
332     source_caps_bm = bitmap_new(MIGRATION_CAPABILITY__MAX);
333     for (i = 0; i < state->caps_count; i++) {
334         MigrationCapability capability = state->capabilities[i];
335         set_bit(capability, source_caps_bm);
336     }
337 
338     for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
339         bool source_state, target_state;
340         if (!should_validate_capability(i)) {
341             continue;
342         }
343         source_state = test_bit(i, source_caps_bm);
344         target_state = s->enabled_capabilities[i];
345         if (source_state != target_state) {
346             error_report("Capability %s is %s, but received capability is %s",
347                          MigrationCapability_str(i),
348                          target_state ? "on" : "off",
349                          source_state ? "on" : "off");
350             ret = false;
351             /* Don't break here to report all failed capabilities */
352         }
353     }
354 
355     g_free(source_caps_bm);
356     return ret;
357 }
358 
359 static int configuration_post_load(void *opaque, int version_id)
360 {
361     SaveState *state = opaque;
362     const char *current_name = MACHINE_GET_CLASS(current_machine)->name;
363 
364     if (strncmp(state->name, current_name, state->len) != 0) {
365         error_report("Machine type received is '%.*s' and local is '%s'",
366                      (int) state->len, state->name, current_name);
367         return -EINVAL;
368     }
369 
370     if (state->target_page_bits != qemu_target_page_bits()) {
371         error_report("Received TARGET_PAGE_BITS is %d but local is %d",
372                      state->target_page_bits, qemu_target_page_bits());
373         return -EINVAL;
374     }
375 
376     if (!configuration_validate_capabilities(state)) {
377         return -EINVAL;
378     }
379 
380     return 0;
381 }
382 
383 static int get_capability(QEMUFile *f, void *pv, size_t size,
384                           const VMStateField *field)
385 {
386     MigrationCapability *capability = pv;
387     char capability_str[UINT8_MAX + 1];
388     uint8_t len;
389     int i;
390 
391     len = qemu_get_byte(f);
392     qemu_get_buffer(f, (uint8_t *)capability_str, len);
393     capability_str[len] = '\0';
394     for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
395         if (!strcmp(MigrationCapability_str(i), capability_str)) {
396             *capability = i;
397             return 0;
398         }
399     }
400     error_report("Received unknown capability %s", capability_str);
401     return -EINVAL;
402 }
403 
404 static int put_capability(QEMUFile *f, void *pv, size_t size,
405                           const VMStateField *field, QJSON *vmdesc)
406 {
407     MigrationCapability *capability = pv;
408     const char *capability_str = MigrationCapability_str(*capability);
409     size_t len = strlen(capability_str);
410     assert(len <= UINT8_MAX);
411 
412     qemu_put_byte(f, len);
413     qemu_put_buffer(f, (uint8_t *)capability_str, len);
414     return 0;
415 }
416 
417 static const VMStateInfo vmstate_info_capability = {
418     .name = "capability",
419     .get  = get_capability,
420     .put  = put_capability,
421 };
422 
423 /* The target-page-bits subsection is present only if the
424  * target page size is not the same as the default (ie the
425  * minimum page size for a variable-page-size guest CPU).
426  * If it is present then it contains the actual target page
427  * bits for the machine, and migration will fail if the
428  * two ends don't agree about it.
429  */
430 static bool vmstate_target_page_bits_needed(void *opaque)
431 {
432     return qemu_target_page_bits()
433         > qemu_target_page_bits_min();
434 }
435 
436 static const VMStateDescription vmstate_target_page_bits = {
437     .name = "configuration/target-page-bits",
438     .version_id = 1,
439     .minimum_version_id = 1,
440     .needed = vmstate_target_page_bits_needed,
441     .fields = (VMStateField[]) {
442         VMSTATE_UINT32(target_page_bits, SaveState),
443         VMSTATE_END_OF_LIST()
444     }
445 };
446 
447 static bool vmstate_capabilites_needed(void *opaque)
448 {
449     return get_validatable_capabilities_count() > 0;
450 }
451 
452 static const VMStateDescription vmstate_capabilites = {
453     .name = "configuration/capabilities",
454     .version_id = 1,
455     .minimum_version_id = 1,
456     .needed = vmstate_capabilites_needed,
457     .fields = (VMStateField[]) {
458         VMSTATE_UINT32_V(caps_count, SaveState, 1),
459         VMSTATE_VARRAY_UINT32_ALLOC(capabilities, SaveState, caps_count, 1,
460                                     vmstate_info_capability,
461                                     MigrationCapability),
462         VMSTATE_END_OF_LIST()
463     }
464 };
465 
466 static const VMStateDescription vmstate_configuration = {
467     .name = "configuration",
468     .version_id = 1,
469     .pre_load = configuration_pre_load,
470     .post_load = configuration_post_load,
471     .pre_save = configuration_pre_save,
472     .fields = (VMStateField[]) {
473         VMSTATE_UINT32(len, SaveState),
474         VMSTATE_VBUFFER_ALLOC_UINT32(name, SaveState, 0, NULL, len),
475         VMSTATE_END_OF_LIST()
476     },
477     .subsections = (const VMStateDescription*[]) {
478         &vmstate_target_page_bits,
479         &vmstate_capabilites,
480         NULL
481     }
482 };
483 
484 static void dump_vmstate_vmsd(FILE *out_file,
485                               const VMStateDescription *vmsd, int indent,
486                               bool is_subsection);
487 
488 static void dump_vmstate_vmsf(FILE *out_file, const VMStateField *field,
489                               int indent)
490 {
491     fprintf(out_file, "%*s{\n", indent, "");
492     indent += 2;
493     fprintf(out_file, "%*s\"field\": \"%s\",\n", indent, "", field->name);
494     fprintf(out_file, "%*s\"version_id\": %d,\n", indent, "",
495             field->version_id);
496     fprintf(out_file, "%*s\"field_exists\": %s,\n", indent, "",
497             field->field_exists ? "true" : "false");
498     fprintf(out_file, "%*s\"size\": %zu", indent, "", field->size);
499     if (field->vmsd != NULL) {
500         fprintf(out_file, ",\n");
501         dump_vmstate_vmsd(out_file, field->vmsd, indent, false);
502     }
503     fprintf(out_file, "\n%*s}", indent - 2, "");
504 }
505 
506 static void dump_vmstate_vmss(FILE *out_file,
507                               const VMStateDescription **subsection,
508                               int indent)
509 {
510     if (*subsection != NULL) {
511         dump_vmstate_vmsd(out_file, *subsection, indent, true);
512     }
513 }
514 
515 static void dump_vmstate_vmsd(FILE *out_file,
516                               const VMStateDescription *vmsd, int indent,
517                               bool is_subsection)
518 {
519     if (is_subsection) {
520         fprintf(out_file, "%*s{\n", indent, "");
521     } else {
522         fprintf(out_file, "%*s\"%s\": {\n", indent, "", "Description");
523     }
524     indent += 2;
525     fprintf(out_file, "%*s\"name\": \"%s\",\n", indent, "", vmsd->name);
526     fprintf(out_file, "%*s\"version_id\": %d,\n", indent, "",
527             vmsd->version_id);
528     fprintf(out_file, "%*s\"minimum_version_id\": %d", indent, "",
529             vmsd->minimum_version_id);
530     if (vmsd->fields != NULL) {
531         const VMStateField *field = vmsd->fields;
532         bool first;
533 
534         fprintf(out_file, ",\n%*s\"Fields\": [\n", indent, "");
535         first = true;
536         while (field->name != NULL) {
537             if (field->flags & VMS_MUST_EXIST) {
538                 /* Ignore VMSTATE_VALIDATE bits; these don't get migrated */
539                 field++;
540                 continue;
541             }
542             if (!first) {
543                 fprintf(out_file, ",\n");
544             }
545             dump_vmstate_vmsf(out_file, field, indent + 2);
546             field++;
547             first = false;
548         }
549         fprintf(out_file, "\n%*s]", indent, "");
550     }
551     if (vmsd->subsections != NULL) {
552         const VMStateDescription **subsection = vmsd->subsections;
553         bool first;
554 
555         fprintf(out_file, ",\n%*s\"Subsections\": [\n", indent, "");
556         first = true;
557         while (*subsection != NULL) {
558             if (!first) {
559                 fprintf(out_file, ",\n");
560             }
561             dump_vmstate_vmss(out_file, subsection, indent + 2);
562             subsection++;
563             first = false;
564         }
565         fprintf(out_file, "\n%*s]", indent, "");
566     }
567     fprintf(out_file, "\n%*s}", indent - 2, "");
568 }
569 
570 static void dump_machine_type(FILE *out_file)
571 {
572     MachineClass *mc;
573 
574     mc = MACHINE_GET_CLASS(current_machine);
575 
576     fprintf(out_file, "  \"vmschkmachine\": {\n");
577     fprintf(out_file, "    \"Name\": \"%s\"\n", mc->name);
578     fprintf(out_file, "  },\n");
579 }
580 
581 void dump_vmstate_json_to_file(FILE *out_file)
582 {
583     GSList *list, *elt;
584     bool first;
585 
586     fprintf(out_file, "{\n");
587     dump_machine_type(out_file);
588 
589     first = true;
590     list = object_class_get_list(TYPE_DEVICE, true);
591     for (elt = list; elt; elt = elt->next) {
592         DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, elt->data,
593                                              TYPE_DEVICE);
594         const char *name;
595         int indent = 2;
596 
597         if (!dc->vmsd) {
598             continue;
599         }
600 
601         if (!first) {
602             fprintf(out_file, ",\n");
603         }
604         name = object_class_get_name(OBJECT_CLASS(dc));
605         fprintf(out_file, "%*s\"%s\": {\n", indent, "", name);
606         indent += 2;
607         fprintf(out_file, "%*s\"Name\": \"%s\",\n", indent, "", name);
608         fprintf(out_file, "%*s\"version_id\": %d,\n", indent, "",
609                 dc->vmsd->version_id);
610         fprintf(out_file, "%*s\"minimum_version_id\": %d,\n", indent, "",
611                 dc->vmsd->minimum_version_id);
612 
613         dump_vmstate_vmsd(out_file, dc->vmsd, indent, false);
614 
615         fprintf(out_file, "\n%*s}", indent - 2, "");
616         first = false;
617     }
618     fprintf(out_file, "\n}\n");
619     fclose(out_file);
620 }
621 
622 static int calculate_new_instance_id(const char *idstr)
623 {
624     SaveStateEntry *se;
625     int instance_id = 0;
626 
627     QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
628         if (strcmp(idstr, se->idstr) == 0
629             && instance_id <= se->instance_id) {
630             instance_id = se->instance_id + 1;
631         }
632     }
633     return instance_id;
634 }
635 
636 static int calculate_compat_instance_id(const char *idstr)
637 {
638     SaveStateEntry *se;
639     int instance_id = 0;
640 
641     QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
642         if (!se->compat) {
643             continue;
644         }
645 
646         if (strcmp(idstr, se->compat->idstr) == 0
647             && instance_id <= se->compat->instance_id) {
648             instance_id = se->compat->instance_id + 1;
649         }
650     }
651     return instance_id;
652 }
653 
654 static inline MigrationPriority save_state_priority(SaveStateEntry *se)
655 {
656     if (se->vmsd) {
657         return se->vmsd->priority;
658     }
659     return MIG_PRI_DEFAULT;
660 }
661 
662 static void savevm_state_handler_insert(SaveStateEntry *nse)
663 {
664     MigrationPriority priority = save_state_priority(nse);
665     SaveStateEntry *se;
666 
667     assert(priority <= MIG_PRI_MAX);
668 
669     QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
670         if (save_state_priority(se) < priority) {
671             break;
672         }
673     }
674 
675     if (se) {
676         QTAILQ_INSERT_BEFORE(se, nse, entry);
677     } else {
678         QTAILQ_INSERT_TAIL(&savevm_state.handlers, nse, entry);
679     }
680 }
681 
682 /* TODO: Individual devices generally have very little idea about the rest
683    of the system, so instance_id should be removed/replaced.
684    Meanwhile pass -1 as instance_id if you do not already have a clearly
685    distinguishing id for all instances of your device class. */
686 int register_savevm_live(DeviceState *dev,
687                          const char *idstr,
688                          int instance_id,
689                          int version_id,
690                          const SaveVMHandlers *ops,
691                          void *opaque)
692 {
693     SaveStateEntry *se;
694 
695     se = g_new0(SaveStateEntry, 1);
696     se->version_id = version_id;
697     se->section_id = savevm_state.global_section_id++;
698     se->ops = ops;
699     se->opaque = opaque;
700     se->vmsd = NULL;
701     /* if this is a live_savem then set is_ram */
702     if (ops->save_setup != NULL) {
703         se->is_ram = 1;
704     }
705 
706     if (dev) {
707         char *id = qdev_get_dev_path(dev);
708         if (id) {
709             if (snprintf(se->idstr, sizeof(se->idstr), "%s/", id) >=
710                 sizeof(se->idstr)) {
711                 error_report("Path too long for VMState (%s)", id);
712                 g_free(id);
713                 g_free(se);
714 
715                 return -1;
716             }
717             g_free(id);
718 
719             se->compat = g_new0(CompatEntry, 1);
720             pstrcpy(se->compat->idstr, sizeof(se->compat->idstr), idstr);
721             se->compat->instance_id = instance_id == -1 ?
722                          calculate_compat_instance_id(idstr) : instance_id;
723             instance_id = -1;
724         }
725     }
726     pstrcat(se->idstr, sizeof(se->idstr), idstr);
727 
728     if (instance_id == -1) {
729         se->instance_id = calculate_new_instance_id(se->idstr);
730     } else {
731         se->instance_id = instance_id;
732     }
733     assert(!se->compat || se->instance_id == 0);
734     savevm_state_handler_insert(se);
735     return 0;
736 }
737 
738 void unregister_savevm(DeviceState *dev, const char *idstr, void *opaque)
739 {
740     SaveStateEntry *se, *new_se;
741     char id[256] = "";
742 
743     if (dev) {
744         char *path = qdev_get_dev_path(dev);
745         if (path) {
746             pstrcpy(id, sizeof(id), path);
747             pstrcat(id, sizeof(id), "/");
748             g_free(path);
749         }
750     }
751     pstrcat(id, sizeof(id), idstr);
752 
753     QTAILQ_FOREACH_SAFE(se, &savevm_state.handlers, entry, new_se) {
754         if (strcmp(se->idstr, id) == 0 && se->opaque == opaque) {
755             QTAILQ_REMOVE(&savevm_state.handlers, se, entry);
756             g_free(se->compat);
757             g_free(se);
758         }
759     }
760 }
761 
762 int vmstate_register_with_alias_id(DeviceState *dev, int instance_id,
763                                    const VMStateDescription *vmsd,
764                                    void *opaque, int alias_id,
765                                    int required_for_version,
766                                    Error **errp)
767 {
768     SaveStateEntry *se;
769 
770     /* If this triggers, alias support can be dropped for the vmsd. */
771     assert(alias_id == -1 || required_for_version >= vmsd->minimum_version_id);
772 
773     se = g_new0(SaveStateEntry, 1);
774     se->version_id = vmsd->version_id;
775     se->section_id = savevm_state.global_section_id++;
776     se->opaque = opaque;
777     se->vmsd = vmsd;
778     se->alias_id = alias_id;
779 
780     if (dev) {
781         char *id = qdev_get_dev_path(dev);
782         if (id) {
783             if (snprintf(se->idstr, sizeof(se->idstr), "%s/", id) >=
784                 sizeof(se->idstr)) {
785                 error_setg(errp, "Path too long for VMState (%s)", id);
786                 g_free(id);
787                 g_free(se);
788 
789                 return -1;
790             }
791             g_free(id);
792 
793             se->compat = g_new0(CompatEntry, 1);
794             pstrcpy(se->compat->idstr, sizeof(se->compat->idstr), vmsd->name);
795             se->compat->instance_id = instance_id == -1 ?
796                          calculate_compat_instance_id(vmsd->name) : instance_id;
797             instance_id = -1;
798         }
799     }
800     pstrcat(se->idstr, sizeof(se->idstr), vmsd->name);
801 
802     if (instance_id == -1) {
803         se->instance_id = calculate_new_instance_id(se->idstr);
804     } else {
805         se->instance_id = instance_id;
806     }
807     assert(!se->compat || se->instance_id == 0);
808     savevm_state_handler_insert(se);
809     return 0;
810 }
811 
812 void vmstate_unregister(DeviceState *dev, const VMStateDescription *vmsd,
813                         void *opaque)
814 {
815     SaveStateEntry *se, *new_se;
816 
817     QTAILQ_FOREACH_SAFE(se, &savevm_state.handlers, entry, new_se) {
818         if (se->vmsd == vmsd && se->opaque == opaque) {
819             QTAILQ_REMOVE(&savevm_state.handlers, se, entry);
820             g_free(se->compat);
821             g_free(se);
822         }
823     }
824 }
825 
826 static int vmstate_load(QEMUFile *f, SaveStateEntry *se)
827 {
828     trace_vmstate_load(se->idstr, se->vmsd ? se->vmsd->name : "(old)");
829     if (!se->vmsd) {         /* Old style */
830         return se->ops->load_state(f, se->opaque, se->load_version_id);
831     }
832     return vmstate_load_state(f, se->vmsd, se->opaque, se->load_version_id);
833 }
834 
835 static void vmstate_save_old_style(QEMUFile *f, SaveStateEntry *se, QJSON *vmdesc)
836 {
837     int64_t old_offset, size;
838 
839     old_offset = qemu_ftell_fast(f);
840     se->ops->save_state(f, se->opaque);
841     size = qemu_ftell_fast(f) - old_offset;
842 
843     if (vmdesc) {
844         json_prop_int(vmdesc, "size", size);
845         json_start_array(vmdesc, "fields");
846         json_start_object(vmdesc, NULL);
847         json_prop_str(vmdesc, "name", "data");
848         json_prop_int(vmdesc, "size", size);
849         json_prop_str(vmdesc, "type", "buffer");
850         json_end_object(vmdesc);
851         json_end_array(vmdesc);
852     }
853 }
854 
855 static int vmstate_save(QEMUFile *f, SaveStateEntry *se, QJSON *vmdesc)
856 {
857     trace_vmstate_save(se->idstr, se->vmsd ? se->vmsd->name : "(old)");
858     if (!se->vmsd) {
859         vmstate_save_old_style(f, se, vmdesc);
860         return 0;
861     }
862     return vmstate_save_state(f, se->vmsd, se->opaque, vmdesc);
863 }
864 
865 /*
866  * Write the header for device section (QEMU_VM_SECTION START/END/PART/FULL)
867  */
868 static void save_section_header(QEMUFile *f, SaveStateEntry *se,
869                                 uint8_t section_type)
870 {
871     qemu_put_byte(f, section_type);
872     qemu_put_be32(f, se->section_id);
873 
874     if (section_type == QEMU_VM_SECTION_FULL ||
875         section_type == QEMU_VM_SECTION_START) {
876         /* ID string */
877         size_t len = strlen(se->idstr);
878         qemu_put_byte(f, len);
879         qemu_put_buffer(f, (uint8_t *)se->idstr, len);
880 
881         qemu_put_be32(f, se->instance_id);
882         qemu_put_be32(f, se->version_id);
883     }
884 }
885 
886 /*
887  * Write a footer onto device sections that catches cases misformatted device
888  * sections.
889  */
890 static void save_section_footer(QEMUFile *f, SaveStateEntry *se)
891 {
892     if (migrate_get_current()->send_section_footer) {
893         qemu_put_byte(f, QEMU_VM_SECTION_FOOTER);
894         qemu_put_be32(f, se->section_id);
895     }
896 }
897 
898 /**
899  * qemu_savevm_command_send: Send a 'QEMU_VM_COMMAND' type element with the
900  *                           command and associated data.
901  *
902  * @f: File to send command on
903  * @command: Command type to send
904  * @len: Length of associated data
905  * @data: Data associated with command.
906  */
907 static void qemu_savevm_command_send(QEMUFile *f,
908                                      enum qemu_vm_cmd command,
909                                      uint16_t len,
910                                      uint8_t *data)
911 {
912     trace_savevm_command_send(command, len);
913     qemu_put_byte(f, QEMU_VM_COMMAND);
914     qemu_put_be16(f, (uint16_t)command);
915     qemu_put_be16(f, len);
916     qemu_put_buffer(f, data, len);
917     qemu_fflush(f);
918 }
919 
920 void qemu_savevm_send_colo_enable(QEMUFile *f)
921 {
922     trace_savevm_send_colo_enable();
923     qemu_savevm_command_send(f, MIG_CMD_ENABLE_COLO, 0, NULL);
924 }
925 
926 void qemu_savevm_send_ping(QEMUFile *f, uint32_t value)
927 {
928     uint32_t buf;
929 
930     trace_savevm_send_ping(value);
931     buf = cpu_to_be32(value);
932     qemu_savevm_command_send(f, MIG_CMD_PING, sizeof(value), (uint8_t *)&buf);
933 }
934 
935 void qemu_savevm_send_open_return_path(QEMUFile *f)
936 {
937     trace_savevm_send_open_return_path();
938     qemu_savevm_command_send(f, MIG_CMD_OPEN_RETURN_PATH, 0, NULL);
939 }
940 
941 /* We have a buffer of data to send; we don't want that all to be loaded
942  * by the command itself, so the command contains just the length of the
943  * extra buffer that we then send straight after it.
944  * TODO: Must be a better way to organise that
945  *
946  * Returns:
947  *    0 on success
948  *    -ve on error
949  */
950 int qemu_savevm_send_packaged(QEMUFile *f, const uint8_t *buf, size_t len)
951 {
952     uint32_t tmp;
953 
954     if (len > MAX_VM_CMD_PACKAGED_SIZE) {
955         error_report("%s: Unreasonably large packaged state: %zu",
956                      __func__, len);
957         return -1;
958     }
959 
960     tmp = cpu_to_be32(len);
961 
962     trace_qemu_savevm_send_packaged();
963     qemu_savevm_command_send(f, MIG_CMD_PACKAGED, 4, (uint8_t *)&tmp);
964 
965     qemu_put_buffer(f, buf, len);
966 
967     return 0;
968 }
969 
970 /* Send prior to any postcopy transfer */
971 void qemu_savevm_send_postcopy_advise(QEMUFile *f)
972 {
973     if (migrate_postcopy_ram()) {
974         uint64_t tmp[2];
975         tmp[0] = cpu_to_be64(ram_pagesize_summary());
976         tmp[1] = cpu_to_be64(qemu_target_page_size());
977 
978         trace_qemu_savevm_send_postcopy_advise();
979         qemu_savevm_command_send(f, MIG_CMD_POSTCOPY_ADVISE,
980                                  16, (uint8_t *)tmp);
981     } else {
982         qemu_savevm_command_send(f, MIG_CMD_POSTCOPY_ADVISE, 0, NULL);
983     }
984 }
985 
986 /* Sent prior to starting the destination running in postcopy, discard pages
987  * that have already been sent but redirtied on the source.
988  * CMD_POSTCOPY_RAM_DISCARD consist of:
989  *      byte   version (0)
990  *      byte   Length of name field (not including 0)
991  *  n x byte   RAM block name
992  *      byte   0 terminator (just for safety)
993  *  n x        Byte ranges within the named RAMBlock
994  *      be64   Start of the range
995  *      be64   Length
996  *
997  *  name:  RAMBlock name that these entries are part of
998  *  len: Number of page entries
999  *  start_list: 'len' addresses
1000  *  length_list: 'len' addresses
1001  *
1002  */
1003 void qemu_savevm_send_postcopy_ram_discard(QEMUFile *f, const char *name,
1004                                            uint16_t len,
1005                                            uint64_t *start_list,
1006                                            uint64_t *length_list)
1007 {
1008     uint8_t *buf;
1009     uint16_t tmplen;
1010     uint16_t t;
1011     size_t name_len = strlen(name);
1012 
1013     trace_qemu_savevm_send_postcopy_ram_discard(name, len);
1014     assert(name_len < 256);
1015     buf = g_malloc0(1 + 1 + name_len + 1 + (8 + 8) * len);
1016     buf[0] = postcopy_ram_discard_version;
1017     buf[1] = name_len;
1018     memcpy(buf + 2, name, name_len);
1019     tmplen = 2 + name_len;
1020     buf[tmplen++] = '\0';
1021 
1022     for (t = 0; t < len; t++) {
1023         stq_be_p(buf + tmplen, start_list[t]);
1024         tmplen += 8;
1025         stq_be_p(buf + tmplen, length_list[t]);
1026         tmplen += 8;
1027     }
1028     qemu_savevm_command_send(f, MIG_CMD_POSTCOPY_RAM_DISCARD, tmplen, buf);
1029     g_free(buf);
1030 }
1031 
1032 /* Get the destination into a state where it can receive postcopy data. */
1033 void qemu_savevm_send_postcopy_listen(QEMUFile *f)
1034 {
1035     trace_savevm_send_postcopy_listen();
1036     qemu_savevm_command_send(f, MIG_CMD_POSTCOPY_LISTEN, 0, NULL);
1037 }
1038 
1039 /* Kick the destination into running */
1040 void qemu_savevm_send_postcopy_run(QEMUFile *f)
1041 {
1042     trace_savevm_send_postcopy_run();
1043     qemu_savevm_command_send(f, MIG_CMD_POSTCOPY_RUN, 0, NULL);
1044 }
1045 
1046 void qemu_savevm_send_postcopy_resume(QEMUFile *f)
1047 {
1048     trace_savevm_send_postcopy_resume();
1049     qemu_savevm_command_send(f, MIG_CMD_POSTCOPY_RESUME, 0, NULL);
1050 }
1051 
1052 void qemu_savevm_send_recv_bitmap(QEMUFile *f, char *block_name)
1053 {
1054     size_t len;
1055     char buf[256];
1056 
1057     trace_savevm_send_recv_bitmap(block_name);
1058 
1059     buf[0] = len = strlen(block_name);
1060     memcpy(buf + 1, block_name, len);
1061 
1062     qemu_savevm_command_send(f, MIG_CMD_RECV_BITMAP, len + 1, (uint8_t *)buf);
1063 }
1064 
1065 bool qemu_savevm_state_blocked(Error **errp)
1066 {
1067     SaveStateEntry *se;
1068 
1069     QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
1070         if (se->vmsd && se->vmsd->unmigratable) {
1071             error_setg(errp, "State blocked by non-migratable device '%s'",
1072                        se->idstr);
1073             return true;
1074         }
1075     }
1076     return false;
1077 }
1078 
1079 void qemu_savevm_state_header(QEMUFile *f)
1080 {
1081     trace_savevm_state_header();
1082     qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
1083     qemu_put_be32(f, QEMU_VM_FILE_VERSION);
1084 
1085     if (migrate_get_current()->send_configuration) {
1086         qemu_put_byte(f, QEMU_VM_CONFIGURATION);
1087         vmstate_save_state(f, &vmstate_configuration, &savevm_state, 0);
1088     }
1089 }
1090 
1091 void qemu_savevm_state_setup(QEMUFile *f)
1092 {
1093     SaveStateEntry *se;
1094     Error *local_err = NULL;
1095     int ret;
1096 
1097     trace_savevm_state_setup();
1098     QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
1099         if (!se->ops || !se->ops->save_setup) {
1100             continue;
1101         }
1102         if (se->ops && se->ops->is_active) {
1103             if (!se->ops->is_active(se->opaque)) {
1104                 continue;
1105             }
1106         }
1107         save_section_header(f, se, QEMU_VM_SECTION_START);
1108 
1109         ret = se->ops->save_setup(f, se->opaque);
1110         save_section_footer(f, se);
1111         if (ret < 0) {
1112             qemu_file_set_error(f, ret);
1113             break;
1114         }
1115     }
1116 
1117     if (precopy_notify(PRECOPY_NOTIFY_SETUP, &local_err)) {
1118         error_report_err(local_err);
1119     }
1120 }
1121 
1122 int qemu_savevm_state_resume_prepare(MigrationState *s)
1123 {
1124     SaveStateEntry *se;
1125     int ret;
1126 
1127     trace_savevm_state_resume_prepare();
1128 
1129     QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
1130         if (!se->ops || !se->ops->resume_prepare) {
1131             continue;
1132         }
1133         if (se->ops && se->ops->is_active) {
1134             if (!se->ops->is_active(se->opaque)) {
1135                 continue;
1136             }
1137         }
1138         ret = se->ops->resume_prepare(s, se->opaque);
1139         if (ret < 0) {
1140             return ret;
1141         }
1142     }
1143 
1144     return 0;
1145 }
1146 
1147 /*
1148  * this function has three return values:
1149  *   negative: there was one error, and we have -errno.
1150  *   0 : We haven't finished, caller have to go again
1151  *   1 : We have finished, we can go to complete phase
1152  */
1153 int qemu_savevm_state_iterate(QEMUFile *f, bool postcopy)
1154 {
1155     SaveStateEntry *se;
1156     int ret = 1;
1157 
1158     trace_savevm_state_iterate();
1159     QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
1160         if (!se->ops || !se->ops->save_live_iterate) {
1161             continue;
1162         }
1163         if (se->ops->is_active &&
1164             !se->ops->is_active(se->opaque)) {
1165             continue;
1166         }
1167         if (se->ops->is_active_iterate &&
1168             !se->ops->is_active_iterate(se->opaque)) {
1169             continue;
1170         }
1171         /*
1172          * In the postcopy phase, any device that doesn't know how to
1173          * do postcopy should have saved it's state in the _complete
1174          * call that's already run, it might get confused if we call
1175          * iterate afterwards.
1176          */
1177         if (postcopy &&
1178             !(se->ops->has_postcopy && se->ops->has_postcopy(se->opaque))) {
1179             continue;
1180         }
1181         if (qemu_file_rate_limit(f)) {
1182             return 0;
1183         }
1184         trace_savevm_section_start(se->idstr, se->section_id);
1185 
1186         save_section_header(f, se, QEMU_VM_SECTION_PART);
1187 
1188         ret = se->ops->save_live_iterate(f, se->opaque);
1189         trace_savevm_section_end(se->idstr, se->section_id, ret);
1190         save_section_footer(f, se);
1191 
1192         if (ret < 0) {
1193             qemu_file_set_error(f, ret);
1194         }
1195         if (ret <= 0) {
1196             /* Do not proceed to the next vmstate before this one reported
1197                completion of the current stage. This serializes the migration
1198                and reduces the probability that a faster changing state is
1199                synchronized over and over again. */
1200             break;
1201         }
1202     }
1203     return ret;
1204 }
1205 
1206 static bool should_send_vmdesc(void)
1207 {
1208     MachineState *machine = MACHINE(qdev_get_machine());
1209     bool in_postcopy = migration_in_postcopy();
1210     return !machine->suppress_vmdesc && !in_postcopy;
1211 }
1212 
1213 /*
1214  * Calls the save_live_complete_postcopy methods
1215  * causing the last few pages to be sent immediately and doing any associated
1216  * cleanup.
1217  * Note postcopy also calls qemu_savevm_state_complete_precopy to complete
1218  * all the other devices, but that happens at the point we switch to postcopy.
1219  */
1220 void qemu_savevm_state_complete_postcopy(QEMUFile *f)
1221 {
1222     SaveStateEntry *se;
1223     int ret;
1224 
1225     QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
1226         if (!se->ops || !se->ops->save_live_complete_postcopy) {
1227             continue;
1228         }
1229         if (se->ops && se->ops->is_active) {
1230             if (!se->ops->is_active(se->opaque)) {
1231                 continue;
1232             }
1233         }
1234         trace_savevm_section_start(se->idstr, se->section_id);
1235         /* Section type */
1236         qemu_put_byte(f, QEMU_VM_SECTION_END);
1237         qemu_put_be32(f, se->section_id);
1238 
1239         ret = se->ops->save_live_complete_postcopy(f, se->opaque);
1240         trace_savevm_section_end(se->idstr, se->section_id, ret);
1241         save_section_footer(f, se);
1242         if (ret < 0) {
1243             qemu_file_set_error(f, ret);
1244             return;
1245         }
1246     }
1247 
1248     qemu_put_byte(f, QEMU_VM_EOF);
1249     qemu_fflush(f);
1250 }
1251 
1252 int qemu_savevm_state_complete_precopy(QEMUFile *f, bool iterable_only,
1253                                        bool inactivate_disks)
1254 {
1255     QJSON *vmdesc;
1256     int vmdesc_len;
1257     SaveStateEntry *se;
1258     int ret;
1259     bool in_postcopy = migration_in_postcopy();
1260     Error *local_err = NULL;
1261 
1262     if (precopy_notify(PRECOPY_NOTIFY_COMPLETE, &local_err)) {
1263         error_report_err(local_err);
1264     }
1265 
1266     trace_savevm_state_complete_precopy();
1267 
1268     cpu_synchronize_all_states();
1269 
1270     QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
1271         if (!se->ops ||
1272             (in_postcopy && se->ops->has_postcopy &&
1273              se->ops->has_postcopy(se->opaque)) ||
1274             (in_postcopy && !iterable_only) ||
1275             !se->ops->save_live_complete_precopy) {
1276             continue;
1277         }
1278 
1279         if (se->ops && se->ops->is_active) {
1280             if (!se->ops->is_active(se->opaque)) {
1281                 continue;
1282             }
1283         }
1284         trace_savevm_section_start(se->idstr, se->section_id);
1285 
1286         save_section_header(f, se, QEMU_VM_SECTION_END);
1287 
1288         ret = se->ops->save_live_complete_precopy(f, se->opaque);
1289         trace_savevm_section_end(se->idstr, se->section_id, ret);
1290         save_section_footer(f, se);
1291         if (ret < 0) {
1292             qemu_file_set_error(f, ret);
1293             return -1;
1294         }
1295     }
1296 
1297     if (iterable_only) {
1298         return 0;
1299     }
1300 
1301     vmdesc = qjson_new();
1302     json_prop_int(vmdesc, "page_size", qemu_target_page_size());
1303     json_start_array(vmdesc, "devices");
1304     QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
1305 
1306         if ((!se->ops || !se->ops->save_state) && !se->vmsd) {
1307             continue;
1308         }
1309         if (se->vmsd && !vmstate_save_needed(se->vmsd, se->opaque)) {
1310             trace_savevm_section_skip(se->idstr, se->section_id);
1311             continue;
1312         }
1313 
1314         trace_savevm_section_start(se->idstr, se->section_id);
1315 
1316         json_start_object(vmdesc, NULL);
1317         json_prop_str(vmdesc, "name", se->idstr);
1318         json_prop_int(vmdesc, "instance_id", se->instance_id);
1319 
1320         save_section_header(f, se, QEMU_VM_SECTION_FULL);
1321         ret = vmstate_save(f, se, vmdesc);
1322         if (ret) {
1323             qemu_file_set_error(f, ret);
1324             return ret;
1325         }
1326         trace_savevm_section_end(se->idstr, se->section_id, 0);
1327         save_section_footer(f, se);
1328 
1329         json_end_object(vmdesc);
1330     }
1331 
1332     if (inactivate_disks) {
1333         /* Inactivate before sending QEMU_VM_EOF so that the
1334          * bdrv_invalidate_cache_all() on the other end won't fail. */
1335         ret = bdrv_inactivate_all();
1336         if (ret) {
1337             error_report("%s: bdrv_inactivate_all() failed (%d)",
1338                          __func__, ret);
1339             qemu_file_set_error(f, ret);
1340             return ret;
1341         }
1342     }
1343     if (!in_postcopy) {
1344         /* Postcopy stream will still be going */
1345         qemu_put_byte(f, QEMU_VM_EOF);
1346     }
1347 
1348     json_end_array(vmdesc);
1349     qjson_finish(vmdesc);
1350     vmdesc_len = strlen(qjson_get_str(vmdesc));
1351 
1352     if (should_send_vmdesc()) {
1353         qemu_put_byte(f, QEMU_VM_VMDESCRIPTION);
1354         qemu_put_be32(f, vmdesc_len);
1355         qemu_put_buffer(f, (uint8_t *)qjson_get_str(vmdesc), vmdesc_len);
1356     }
1357     qjson_destroy(vmdesc);
1358 
1359     qemu_fflush(f);
1360     return 0;
1361 }
1362 
1363 /* Give an estimate of the amount left to be transferred,
1364  * the result is split into the amount for units that can and
1365  * for units that can't do postcopy.
1366  */
1367 void qemu_savevm_state_pending(QEMUFile *f, uint64_t threshold_size,
1368                                uint64_t *res_precopy_only,
1369                                uint64_t *res_compatible,
1370                                uint64_t *res_postcopy_only)
1371 {
1372     SaveStateEntry *se;
1373 
1374     *res_precopy_only = 0;
1375     *res_compatible = 0;
1376     *res_postcopy_only = 0;
1377 
1378 
1379     QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
1380         if (!se->ops || !se->ops->save_live_pending) {
1381             continue;
1382         }
1383         if (se->ops && se->ops->is_active) {
1384             if (!se->ops->is_active(se->opaque)) {
1385                 continue;
1386             }
1387         }
1388         se->ops->save_live_pending(f, se->opaque, threshold_size,
1389                                    res_precopy_only, res_compatible,
1390                                    res_postcopy_only);
1391     }
1392 }
1393 
1394 void qemu_savevm_state_cleanup(void)
1395 {
1396     SaveStateEntry *se;
1397     Error *local_err = NULL;
1398 
1399     if (precopy_notify(PRECOPY_NOTIFY_CLEANUP, &local_err)) {
1400         error_report_err(local_err);
1401     }
1402 
1403     trace_savevm_state_cleanup();
1404     QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
1405         if (se->ops && se->ops->save_cleanup) {
1406             se->ops->save_cleanup(se->opaque);
1407         }
1408     }
1409 }
1410 
1411 static int qemu_savevm_state(QEMUFile *f, Error **errp)
1412 {
1413     int ret;
1414     MigrationState *ms = migrate_get_current();
1415     MigrationStatus status;
1416 
1417     if (migration_is_setup_or_active(ms->state) ||
1418         ms->state == MIGRATION_STATUS_CANCELLING ||
1419         ms->state == MIGRATION_STATUS_COLO) {
1420         error_setg(errp, QERR_MIGRATION_ACTIVE);
1421         return -EINVAL;
1422     }
1423 
1424     if (migrate_use_block()) {
1425         error_setg(errp, "Block migration and snapshots are incompatible");
1426         return -EINVAL;
1427     }
1428 
1429     migrate_init(ms);
1430     ms->to_dst_file = f;
1431 
1432     qemu_mutex_unlock_iothread();
1433     qemu_savevm_state_header(f);
1434     qemu_savevm_state_setup(f);
1435     qemu_mutex_lock_iothread();
1436 
1437     while (qemu_file_get_error(f) == 0) {
1438         if (qemu_savevm_state_iterate(f, false) > 0) {
1439             break;
1440         }
1441     }
1442 
1443     ret = qemu_file_get_error(f);
1444     if (ret == 0) {
1445         qemu_savevm_state_complete_precopy(f, false, false);
1446         ret = qemu_file_get_error(f);
1447     }
1448     qemu_savevm_state_cleanup();
1449     if (ret != 0) {
1450         error_setg_errno(errp, -ret, "Error while writing VM state");
1451     }
1452 
1453     if (ret != 0) {
1454         status = MIGRATION_STATUS_FAILED;
1455     } else {
1456         status = MIGRATION_STATUS_COMPLETED;
1457     }
1458     migrate_set_state(&ms->state, MIGRATION_STATUS_SETUP, status);
1459 
1460     /* f is outer parameter, it should not stay in global migration state after
1461      * this function finished */
1462     ms->to_dst_file = NULL;
1463 
1464     return ret;
1465 }
1466 
1467 void qemu_savevm_live_state(QEMUFile *f)
1468 {
1469     /* save QEMU_VM_SECTION_END section */
1470     qemu_savevm_state_complete_precopy(f, true, false);
1471     qemu_put_byte(f, QEMU_VM_EOF);
1472 }
1473 
1474 int qemu_save_device_state(QEMUFile *f)
1475 {
1476     SaveStateEntry *se;
1477 
1478     if (!migration_in_colo_state()) {
1479         qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
1480         qemu_put_be32(f, QEMU_VM_FILE_VERSION);
1481     }
1482     cpu_synchronize_all_states();
1483 
1484     QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
1485         int ret;
1486 
1487         if (se->is_ram) {
1488             continue;
1489         }
1490         if ((!se->ops || !se->ops->save_state) && !se->vmsd) {
1491             continue;
1492         }
1493         if (se->vmsd && !vmstate_save_needed(se->vmsd, se->opaque)) {
1494             continue;
1495         }
1496 
1497         save_section_header(f, se, QEMU_VM_SECTION_FULL);
1498 
1499         ret = vmstate_save(f, se, NULL);
1500         if (ret) {
1501             return ret;
1502         }
1503 
1504         save_section_footer(f, se);
1505     }
1506 
1507     qemu_put_byte(f, QEMU_VM_EOF);
1508 
1509     return qemu_file_get_error(f);
1510 }
1511 
1512 static SaveStateEntry *find_se(const char *idstr, int instance_id)
1513 {
1514     SaveStateEntry *se;
1515 
1516     QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
1517         if (!strcmp(se->idstr, idstr) &&
1518             (instance_id == se->instance_id ||
1519              instance_id == se->alias_id))
1520             return se;
1521         /* Migrating from an older version? */
1522         if (strstr(se->idstr, idstr) && se->compat) {
1523             if (!strcmp(se->compat->idstr, idstr) &&
1524                 (instance_id == se->compat->instance_id ||
1525                  instance_id == se->alias_id))
1526                 return se;
1527         }
1528     }
1529     return NULL;
1530 }
1531 
1532 enum LoadVMExitCodes {
1533     /* Allow a command to quit all layers of nested loadvm loops */
1534     LOADVM_QUIT     =  1,
1535 };
1536 
1537 /* ------ incoming postcopy messages ------ */
1538 /* 'advise' arrives before any transfers just to tell us that a postcopy
1539  * *might* happen - it might be skipped if precopy transferred everything
1540  * quickly.
1541  */
1542 static int loadvm_postcopy_handle_advise(MigrationIncomingState *mis,
1543                                          uint16_t len)
1544 {
1545     PostcopyState ps = postcopy_state_set(POSTCOPY_INCOMING_ADVISE);
1546     uint64_t remote_pagesize_summary, local_pagesize_summary, remote_tps;
1547     Error *local_err = NULL;
1548 
1549     trace_loadvm_postcopy_handle_advise();
1550     if (ps != POSTCOPY_INCOMING_NONE) {
1551         error_report("CMD_POSTCOPY_ADVISE in wrong postcopy state (%d)", ps);
1552         return -1;
1553     }
1554 
1555     switch (len) {
1556     case 0:
1557         if (migrate_postcopy_ram()) {
1558             error_report("RAM postcopy is enabled but have 0 byte advise");
1559             return -EINVAL;
1560         }
1561         return 0;
1562     case 8 + 8:
1563         if (!migrate_postcopy_ram()) {
1564             error_report("RAM postcopy is disabled but have 16 byte advise");
1565             return -EINVAL;
1566         }
1567         break;
1568     default:
1569         error_report("CMD_POSTCOPY_ADVISE invalid length (%d)", len);
1570         return -EINVAL;
1571     }
1572 
1573     if (!postcopy_ram_supported_by_host(mis)) {
1574         postcopy_state_set(POSTCOPY_INCOMING_NONE);
1575         return -1;
1576     }
1577 
1578     remote_pagesize_summary = qemu_get_be64(mis->from_src_file);
1579     local_pagesize_summary = ram_pagesize_summary();
1580 
1581     if (remote_pagesize_summary != local_pagesize_summary)  {
1582         /*
1583          * This detects two potential causes of mismatch:
1584          *   a) A mismatch in host page sizes
1585          *      Some combinations of mismatch are probably possible but it gets
1586          *      a bit more complicated.  In particular we need to place whole
1587          *      host pages on the dest at once, and we need to ensure that we
1588          *      handle dirtying to make sure we never end up sending part of
1589          *      a hostpage on it's own.
1590          *   b) The use of different huge page sizes on source/destination
1591          *      a more fine grain test is performed during RAM block migration
1592          *      but this test here causes a nice early clear failure, and
1593          *      also fails when passed to an older qemu that doesn't
1594          *      do huge pages.
1595          */
1596         error_report("Postcopy needs matching RAM page sizes (s=%" PRIx64
1597                                                              " d=%" PRIx64 ")",
1598                      remote_pagesize_summary, local_pagesize_summary);
1599         return -1;
1600     }
1601 
1602     remote_tps = qemu_get_be64(mis->from_src_file);
1603     if (remote_tps != qemu_target_page_size()) {
1604         /*
1605          * Again, some differences could be dealt with, but for now keep it
1606          * simple.
1607          */
1608         error_report("Postcopy needs matching target page sizes (s=%d d=%zd)",
1609                      (int)remote_tps, qemu_target_page_size());
1610         return -1;
1611     }
1612 
1613     if (postcopy_notify(POSTCOPY_NOTIFY_INBOUND_ADVISE, &local_err)) {
1614         error_report_err(local_err);
1615         return -1;
1616     }
1617 
1618     if (ram_postcopy_incoming_init(mis)) {
1619         return -1;
1620     }
1621 
1622     postcopy_state_set(POSTCOPY_INCOMING_ADVISE);
1623 
1624     return 0;
1625 }
1626 
1627 /* After postcopy we will be told to throw some pages away since they're
1628  * dirty and will have to be demand fetched.  Must happen before CPU is
1629  * started.
1630  * There can be 0..many of these messages, each encoding multiple pages.
1631  */
1632 static int loadvm_postcopy_ram_handle_discard(MigrationIncomingState *mis,
1633                                               uint16_t len)
1634 {
1635     int tmp;
1636     char ramid[256];
1637     PostcopyState ps = postcopy_state_get();
1638 
1639     trace_loadvm_postcopy_ram_handle_discard();
1640 
1641     switch (ps) {
1642     case POSTCOPY_INCOMING_ADVISE:
1643         /* 1st discard */
1644         tmp = postcopy_ram_prepare_discard(mis);
1645         if (tmp) {
1646             return tmp;
1647         }
1648         break;
1649 
1650     case POSTCOPY_INCOMING_DISCARD:
1651         /* Expected state */
1652         break;
1653 
1654     default:
1655         error_report("CMD_POSTCOPY_RAM_DISCARD in wrong postcopy state (%d)",
1656                      ps);
1657         return -1;
1658     }
1659     /* We're expecting a
1660      *    Version (0)
1661      *    a RAM ID string (length byte, name, 0 term)
1662      *    then at least 1 16 byte chunk
1663     */
1664     if (len < (1 + 1 + 1 + 1 + 2 * 8)) {
1665         error_report("CMD_POSTCOPY_RAM_DISCARD invalid length (%d)", len);
1666         return -1;
1667     }
1668 
1669     tmp = qemu_get_byte(mis->from_src_file);
1670     if (tmp != postcopy_ram_discard_version) {
1671         error_report("CMD_POSTCOPY_RAM_DISCARD invalid version (%d)", tmp);
1672         return -1;
1673     }
1674 
1675     if (!qemu_get_counted_string(mis->from_src_file, ramid)) {
1676         error_report("CMD_POSTCOPY_RAM_DISCARD Failed to read RAMBlock ID");
1677         return -1;
1678     }
1679     tmp = qemu_get_byte(mis->from_src_file);
1680     if (tmp != 0) {
1681         error_report("CMD_POSTCOPY_RAM_DISCARD missing nil (%d)", tmp);
1682         return -1;
1683     }
1684 
1685     len -= 3 + strlen(ramid);
1686     if (len % 16) {
1687         error_report("CMD_POSTCOPY_RAM_DISCARD invalid length (%d)", len);
1688         return -1;
1689     }
1690     trace_loadvm_postcopy_ram_handle_discard_header(ramid, len);
1691     while (len) {
1692         uint64_t start_addr, block_length;
1693         start_addr = qemu_get_be64(mis->from_src_file);
1694         block_length = qemu_get_be64(mis->from_src_file);
1695 
1696         len -= 16;
1697         int ret = ram_discard_range(ramid, start_addr, block_length);
1698         if (ret) {
1699             return ret;
1700         }
1701     }
1702     trace_loadvm_postcopy_ram_handle_discard_end();
1703 
1704     return 0;
1705 }
1706 
1707 /*
1708  * Triggered by a postcopy_listen command; this thread takes over reading
1709  * the input stream, leaving the main thread free to carry on loading the rest
1710  * of the device state (from RAM).
1711  * (TODO:This could do with being in a postcopy file - but there again it's
1712  * just another input loop, not that postcopy specific)
1713  */
1714 static void *postcopy_ram_listen_thread(void *opaque)
1715 {
1716     MigrationIncomingState *mis = migration_incoming_get_current();
1717     QEMUFile *f = mis->from_src_file;
1718     int load_res;
1719 
1720     migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE,
1721                                    MIGRATION_STATUS_POSTCOPY_ACTIVE);
1722     qemu_sem_post(&mis->listen_thread_sem);
1723     trace_postcopy_ram_listen_thread_start();
1724 
1725     rcu_register_thread();
1726     /*
1727      * Because we're a thread and not a coroutine we can't yield
1728      * in qemu_file, and thus we must be blocking now.
1729      */
1730     qemu_file_set_blocking(f, true);
1731     load_res = qemu_loadvm_state_main(f, mis);
1732 
1733     /*
1734      * This is tricky, but, mis->from_src_file can change after it
1735      * returns, when postcopy recovery happened. In the future, we may
1736      * want a wrapper for the QEMUFile handle.
1737      */
1738     f = mis->from_src_file;
1739 
1740     /* And non-blocking again so we don't block in any cleanup */
1741     qemu_file_set_blocking(f, false);
1742 
1743     trace_postcopy_ram_listen_thread_exit();
1744     if (load_res < 0) {
1745         error_report("%s: loadvm failed: %d", __func__, load_res);
1746         qemu_file_set_error(f, load_res);
1747         migrate_set_state(&mis->state, MIGRATION_STATUS_POSTCOPY_ACTIVE,
1748                                        MIGRATION_STATUS_FAILED);
1749     } else {
1750         /*
1751          * This looks good, but it's possible that the device loading in the
1752          * main thread hasn't finished yet, and so we might not be in 'RUN'
1753          * state yet; wait for the end of the main thread.
1754          */
1755         qemu_event_wait(&mis->main_thread_load_event);
1756     }
1757     postcopy_ram_incoming_cleanup(mis);
1758 
1759     if (load_res < 0) {
1760         /*
1761          * If something went wrong then we have a bad state so exit;
1762          * depending how far we got it might be possible at this point
1763          * to leave the guest running and fire MCEs for pages that never
1764          * arrived as a desperate recovery step.
1765          */
1766         rcu_unregister_thread();
1767         exit(EXIT_FAILURE);
1768     }
1769 
1770     migrate_set_state(&mis->state, MIGRATION_STATUS_POSTCOPY_ACTIVE,
1771                                    MIGRATION_STATUS_COMPLETED);
1772     /*
1773      * If everything has worked fine, then the main thread has waited
1774      * for us to start, and we're the last use of the mis.
1775      * (If something broke then qemu will have to exit anyway since it's
1776      * got a bad migration state).
1777      */
1778     migration_incoming_state_destroy();
1779     qemu_loadvm_state_cleanup();
1780 
1781     rcu_unregister_thread();
1782     mis->have_listen_thread = false;
1783     return NULL;
1784 }
1785 
1786 /* After this message we must be able to immediately receive postcopy data */
1787 static int loadvm_postcopy_handle_listen(MigrationIncomingState *mis)
1788 {
1789     PostcopyState ps = postcopy_state_set(POSTCOPY_INCOMING_LISTENING);
1790     trace_loadvm_postcopy_handle_listen();
1791     Error *local_err = NULL;
1792 
1793     if (ps != POSTCOPY_INCOMING_ADVISE && ps != POSTCOPY_INCOMING_DISCARD) {
1794         error_report("CMD_POSTCOPY_LISTEN in wrong postcopy state (%d)", ps);
1795         return -1;
1796     }
1797     if (ps == POSTCOPY_INCOMING_ADVISE) {
1798         /*
1799          * A rare case, we entered listen without having to do any discards,
1800          * so do the setup that's normally done at the time of the 1st discard.
1801          */
1802         if (migrate_postcopy_ram()) {
1803             postcopy_ram_prepare_discard(mis);
1804         }
1805     }
1806 
1807     /*
1808      * Sensitise RAM - can now generate requests for blocks that don't exist
1809      * However, at this point the CPU shouldn't be running, and the IO
1810      * shouldn't be doing anything yet so don't actually expect requests
1811      */
1812     if (migrate_postcopy_ram()) {
1813         if (postcopy_ram_enable_notify(mis)) {
1814             postcopy_ram_incoming_cleanup(mis);
1815             return -1;
1816         }
1817     }
1818 
1819     if (postcopy_notify(POSTCOPY_NOTIFY_INBOUND_LISTEN, &local_err)) {
1820         error_report_err(local_err);
1821         return -1;
1822     }
1823 
1824     if (mis->have_listen_thread) {
1825         error_report("CMD_POSTCOPY_RAM_LISTEN already has a listen thread");
1826         return -1;
1827     }
1828 
1829     mis->have_listen_thread = true;
1830     /* Start up the listening thread and wait for it to signal ready */
1831     qemu_sem_init(&mis->listen_thread_sem, 0);
1832     qemu_thread_create(&mis->listen_thread, "postcopy/listen",
1833                        postcopy_ram_listen_thread, NULL,
1834                        QEMU_THREAD_DETACHED);
1835     qemu_sem_wait(&mis->listen_thread_sem);
1836     qemu_sem_destroy(&mis->listen_thread_sem);
1837 
1838     return 0;
1839 }
1840 
1841 
1842 typedef struct {
1843     QEMUBH *bh;
1844 } HandleRunBhData;
1845 
1846 static void loadvm_postcopy_handle_run_bh(void *opaque)
1847 {
1848     Error *local_err = NULL;
1849     HandleRunBhData *data = opaque;
1850     MigrationIncomingState *mis = migration_incoming_get_current();
1851 
1852     /* TODO we should move all of this lot into postcopy_ram.c or a shared code
1853      * in migration.c
1854      */
1855     cpu_synchronize_all_post_init();
1856 
1857     qemu_announce_self(&mis->announce_timer, migrate_announce_params());
1858 
1859     /* Make sure all file formats flush their mutable metadata.
1860      * If we get an error here, just don't restart the VM yet. */
1861     bdrv_invalidate_cache_all(&local_err);
1862     if (local_err) {
1863         error_report_err(local_err);
1864         local_err = NULL;
1865         autostart = false;
1866     }
1867 
1868     trace_loadvm_postcopy_handle_run_cpu_sync();
1869 
1870     trace_loadvm_postcopy_handle_run_vmstart();
1871 
1872     dirty_bitmap_mig_before_vm_start();
1873 
1874     if (autostart) {
1875         /* Hold onto your hats, starting the CPU */
1876         vm_start();
1877     } else {
1878         /* leave it paused and let management decide when to start the CPU */
1879         runstate_set(RUN_STATE_PAUSED);
1880     }
1881 
1882     qemu_bh_delete(data->bh);
1883     g_free(data);
1884 }
1885 
1886 /* After all discards we can start running and asking for pages */
1887 static int loadvm_postcopy_handle_run(MigrationIncomingState *mis)
1888 {
1889     PostcopyState ps = postcopy_state_set(POSTCOPY_INCOMING_RUNNING);
1890     HandleRunBhData *data;
1891 
1892     trace_loadvm_postcopy_handle_run();
1893     if (ps != POSTCOPY_INCOMING_LISTENING) {
1894         error_report("CMD_POSTCOPY_RUN in wrong postcopy state (%d)", ps);
1895         return -1;
1896     }
1897 
1898     data = g_new(HandleRunBhData, 1);
1899     data->bh = qemu_bh_new(loadvm_postcopy_handle_run_bh, data);
1900     qemu_bh_schedule(data->bh);
1901 
1902     /* We need to finish reading the stream from the package
1903      * and also stop reading anything more from the stream that loaded the
1904      * package (since it's now being read by the listener thread).
1905      * LOADVM_QUIT will quit all the layers of nested loadvm loops.
1906      */
1907     return LOADVM_QUIT;
1908 }
1909 
1910 static int loadvm_postcopy_handle_resume(MigrationIncomingState *mis)
1911 {
1912     if (mis->state != MIGRATION_STATUS_POSTCOPY_RECOVER) {
1913         error_report("%s: illegal resume received", __func__);
1914         /* Don't fail the load, only for this. */
1915         return 0;
1916     }
1917 
1918     /*
1919      * This means source VM is ready to resume the postcopy migration.
1920      * It's time to switch state and release the fault thread to
1921      * continue service page faults.
1922      */
1923     migrate_set_state(&mis->state, MIGRATION_STATUS_POSTCOPY_RECOVER,
1924                       MIGRATION_STATUS_POSTCOPY_ACTIVE);
1925     qemu_sem_post(&mis->postcopy_pause_sem_fault);
1926 
1927     trace_loadvm_postcopy_handle_resume();
1928 
1929     /* Tell source that "we are ready" */
1930     migrate_send_rp_resume_ack(mis, MIGRATION_RESUME_ACK_VALUE);
1931 
1932     return 0;
1933 }
1934 
1935 /**
1936  * Immediately following this command is a blob of data containing an embedded
1937  * chunk of migration stream; read it and load it.
1938  *
1939  * @mis: Incoming state
1940  * @length: Length of packaged data to read
1941  *
1942  * Returns: Negative values on error
1943  *
1944  */
1945 static int loadvm_handle_cmd_packaged(MigrationIncomingState *mis)
1946 {
1947     int ret;
1948     size_t length;
1949     QIOChannelBuffer *bioc;
1950 
1951     length = qemu_get_be32(mis->from_src_file);
1952     trace_loadvm_handle_cmd_packaged(length);
1953 
1954     if (length > MAX_VM_CMD_PACKAGED_SIZE) {
1955         error_report("Unreasonably large packaged state: %zu", length);
1956         return -1;
1957     }
1958 
1959     bioc = qio_channel_buffer_new(length);
1960     qio_channel_set_name(QIO_CHANNEL(bioc), "migration-loadvm-buffer");
1961     ret = qemu_get_buffer(mis->from_src_file,
1962                           bioc->data,
1963                           length);
1964     if (ret != length) {
1965         object_unref(OBJECT(bioc));
1966         error_report("CMD_PACKAGED: Buffer receive fail ret=%d length=%zu",
1967                      ret, length);
1968         return (ret < 0) ? ret : -EAGAIN;
1969     }
1970     bioc->usage += length;
1971     trace_loadvm_handle_cmd_packaged_received(ret);
1972 
1973     QEMUFile *packf = qemu_fopen_channel_input(QIO_CHANNEL(bioc));
1974 
1975     ret = qemu_loadvm_state_main(packf, mis);
1976     trace_loadvm_handle_cmd_packaged_main(ret);
1977     qemu_fclose(packf);
1978     object_unref(OBJECT(bioc));
1979 
1980     return ret;
1981 }
1982 
1983 /*
1984  * Handle request that source requests for recved_bitmap on
1985  * destination. Payload format:
1986  *
1987  * len (1 byte) + ramblock_name (<255 bytes)
1988  */
1989 static int loadvm_handle_recv_bitmap(MigrationIncomingState *mis,
1990                                      uint16_t len)
1991 {
1992     QEMUFile *file = mis->from_src_file;
1993     RAMBlock *rb;
1994     char block_name[256];
1995     size_t cnt;
1996 
1997     cnt = qemu_get_counted_string(file, block_name);
1998     if (!cnt) {
1999         error_report("%s: failed to read block name", __func__);
2000         return -EINVAL;
2001     }
2002 
2003     /* Validate before using the data */
2004     if (qemu_file_get_error(file)) {
2005         return qemu_file_get_error(file);
2006     }
2007 
2008     if (len != cnt + 1) {
2009         error_report("%s: invalid payload length (%d)", __func__, len);
2010         return -EINVAL;
2011     }
2012 
2013     rb = qemu_ram_block_by_name(block_name);
2014     if (!rb) {
2015         error_report("%s: block '%s' not found", __func__, block_name);
2016         return -EINVAL;
2017     }
2018 
2019     migrate_send_rp_recv_bitmap(mis, block_name);
2020 
2021     trace_loadvm_handle_recv_bitmap(block_name);
2022 
2023     return 0;
2024 }
2025 
2026 static int loadvm_process_enable_colo(MigrationIncomingState *mis)
2027 {
2028     migration_incoming_enable_colo();
2029     return colo_init_ram_cache();
2030 }
2031 
2032 /*
2033  * Process an incoming 'QEMU_VM_COMMAND'
2034  * 0           just a normal return
2035  * LOADVM_QUIT All good, but exit the loop
2036  * <0          Error
2037  */
2038 static int loadvm_process_command(QEMUFile *f)
2039 {
2040     MigrationIncomingState *mis = migration_incoming_get_current();
2041     uint16_t cmd;
2042     uint16_t len;
2043     uint32_t tmp32;
2044 
2045     cmd = qemu_get_be16(f);
2046     len = qemu_get_be16(f);
2047 
2048     /* Check validity before continue processing of cmds */
2049     if (qemu_file_get_error(f)) {
2050         return qemu_file_get_error(f);
2051     }
2052 
2053     trace_loadvm_process_command(cmd, len);
2054     if (cmd >= MIG_CMD_MAX || cmd == MIG_CMD_INVALID) {
2055         error_report("MIG_CMD 0x%x unknown (len 0x%x)", cmd, len);
2056         return -EINVAL;
2057     }
2058 
2059     if (mig_cmd_args[cmd].len != -1 && mig_cmd_args[cmd].len != len) {
2060         error_report("%s received with bad length - expecting %zu, got %d",
2061                      mig_cmd_args[cmd].name,
2062                      (size_t)mig_cmd_args[cmd].len, len);
2063         return -ERANGE;
2064     }
2065 
2066     switch (cmd) {
2067     case MIG_CMD_OPEN_RETURN_PATH:
2068         if (mis->to_src_file) {
2069             error_report("CMD_OPEN_RETURN_PATH called when RP already open");
2070             /* Not really a problem, so don't give up */
2071             return 0;
2072         }
2073         mis->to_src_file = qemu_file_get_return_path(f);
2074         if (!mis->to_src_file) {
2075             error_report("CMD_OPEN_RETURN_PATH failed");
2076             return -1;
2077         }
2078         break;
2079 
2080     case MIG_CMD_PING:
2081         tmp32 = qemu_get_be32(f);
2082         trace_loadvm_process_command_ping(tmp32);
2083         if (!mis->to_src_file) {
2084             error_report("CMD_PING (0x%x) received with no return path",
2085                          tmp32);
2086             return -1;
2087         }
2088         migrate_send_rp_pong(mis, tmp32);
2089         break;
2090 
2091     case MIG_CMD_PACKAGED:
2092         return loadvm_handle_cmd_packaged(mis);
2093 
2094     case MIG_CMD_POSTCOPY_ADVISE:
2095         return loadvm_postcopy_handle_advise(mis, len);
2096 
2097     case MIG_CMD_POSTCOPY_LISTEN:
2098         return loadvm_postcopy_handle_listen(mis);
2099 
2100     case MIG_CMD_POSTCOPY_RUN:
2101         return loadvm_postcopy_handle_run(mis);
2102 
2103     case MIG_CMD_POSTCOPY_RAM_DISCARD:
2104         return loadvm_postcopy_ram_handle_discard(mis, len);
2105 
2106     case MIG_CMD_POSTCOPY_RESUME:
2107         return loadvm_postcopy_handle_resume(mis);
2108 
2109     case MIG_CMD_RECV_BITMAP:
2110         return loadvm_handle_recv_bitmap(mis, len);
2111 
2112     case MIG_CMD_ENABLE_COLO:
2113         return loadvm_process_enable_colo(mis);
2114     }
2115 
2116     return 0;
2117 }
2118 
2119 /*
2120  * Read a footer off the wire and check that it matches the expected section
2121  *
2122  * Returns: true if the footer was good
2123  *          false if there is a problem (and calls error_report to say why)
2124  */
2125 static bool check_section_footer(QEMUFile *f, SaveStateEntry *se)
2126 {
2127     int ret;
2128     uint8_t read_mark;
2129     uint32_t read_section_id;
2130 
2131     if (!migrate_get_current()->send_section_footer) {
2132         /* No footer to check */
2133         return true;
2134     }
2135 
2136     read_mark = qemu_get_byte(f);
2137 
2138     ret = qemu_file_get_error(f);
2139     if (ret) {
2140         error_report("%s: Read section footer failed: %d",
2141                      __func__, ret);
2142         return false;
2143     }
2144 
2145     if (read_mark != QEMU_VM_SECTION_FOOTER) {
2146         error_report("Missing section footer for %s", se->idstr);
2147         return false;
2148     }
2149 
2150     read_section_id = qemu_get_be32(f);
2151     if (read_section_id != se->load_section_id) {
2152         error_report("Mismatched section id in footer for %s -"
2153                      " read 0x%x expected 0x%x",
2154                      se->idstr, read_section_id, se->load_section_id);
2155         return false;
2156     }
2157 
2158     /* All good */
2159     return true;
2160 }
2161 
2162 static int
2163 qemu_loadvm_section_start_full(QEMUFile *f, MigrationIncomingState *mis)
2164 {
2165     uint32_t instance_id, version_id, section_id;
2166     SaveStateEntry *se;
2167     char idstr[256];
2168     int ret;
2169 
2170     /* Read section start */
2171     section_id = qemu_get_be32(f);
2172     if (!qemu_get_counted_string(f, idstr)) {
2173         error_report("Unable to read ID string for section %u",
2174                      section_id);
2175         return -EINVAL;
2176     }
2177     instance_id = qemu_get_be32(f);
2178     version_id = qemu_get_be32(f);
2179 
2180     ret = qemu_file_get_error(f);
2181     if (ret) {
2182         error_report("%s: Failed to read instance/version ID: %d",
2183                      __func__, ret);
2184         return ret;
2185     }
2186 
2187     trace_qemu_loadvm_state_section_startfull(section_id, idstr,
2188             instance_id, version_id);
2189     /* Find savevm section */
2190     se = find_se(idstr, instance_id);
2191     if (se == NULL) {
2192         error_report("Unknown savevm section or instance '%s' %d. "
2193                      "Make sure that your current VM setup matches your "
2194                      "saved VM setup, including any hotplugged devices",
2195                      idstr, instance_id);
2196         return -EINVAL;
2197     }
2198 
2199     /* Validate version */
2200     if (version_id > se->version_id) {
2201         error_report("savevm: unsupported version %d for '%s' v%d",
2202                      version_id, idstr, se->version_id);
2203         return -EINVAL;
2204     }
2205     se->load_version_id = version_id;
2206     se->load_section_id = section_id;
2207 
2208     /* Validate if it is a device's state */
2209     if (xen_enabled() && se->is_ram) {
2210         error_report("loadvm: %s RAM loading not allowed on Xen", idstr);
2211         return -EINVAL;
2212     }
2213 
2214     ret = vmstate_load(f, se);
2215     if (ret < 0) {
2216         error_report("error while loading state for instance 0x%x of"
2217                      " device '%s'", instance_id, idstr);
2218         return ret;
2219     }
2220     if (!check_section_footer(f, se)) {
2221         return -EINVAL;
2222     }
2223 
2224     return 0;
2225 }
2226 
2227 static int
2228 qemu_loadvm_section_part_end(QEMUFile *f, MigrationIncomingState *mis)
2229 {
2230     uint32_t section_id;
2231     SaveStateEntry *se;
2232     int ret;
2233 
2234     section_id = qemu_get_be32(f);
2235 
2236     ret = qemu_file_get_error(f);
2237     if (ret) {
2238         error_report("%s: Failed to read section ID: %d",
2239                      __func__, ret);
2240         return ret;
2241     }
2242 
2243     trace_qemu_loadvm_state_section_partend(section_id);
2244     QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
2245         if (se->load_section_id == section_id) {
2246             break;
2247         }
2248     }
2249     if (se == NULL) {
2250         error_report("Unknown savevm section %d", section_id);
2251         return -EINVAL;
2252     }
2253 
2254     ret = vmstate_load(f, se);
2255     if (ret < 0) {
2256         error_report("error while loading state section id %d(%s)",
2257                      section_id, se->idstr);
2258         return ret;
2259     }
2260     if (!check_section_footer(f, se)) {
2261         return -EINVAL;
2262     }
2263 
2264     return 0;
2265 }
2266 
2267 static int qemu_loadvm_state_header(QEMUFile *f)
2268 {
2269     unsigned int v;
2270     int ret;
2271 
2272     v = qemu_get_be32(f);
2273     if (v != QEMU_VM_FILE_MAGIC) {
2274         error_report("Not a migration stream");
2275         return -EINVAL;
2276     }
2277 
2278     v = qemu_get_be32(f);
2279     if (v == QEMU_VM_FILE_VERSION_COMPAT) {
2280         error_report("SaveVM v2 format is obsolete and don't work anymore");
2281         return -ENOTSUP;
2282     }
2283     if (v != QEMU_VM_FILE_VERSION) {
2284         error_report("Unsupported migration stream version");
2285         return -ENOTSUP;
2286     }
2287 
2288     if (migrate_get_current()->send_configuration) {
2289         if (qemu_get_byte(f) != QEMU_VM_CONFIGURATION) {
2290             error_report("Configuration section missing");
2291             qemu_loadvm_state_cleanup();
2292             return -EINVAL;
2293         }
2294         ret = vmstate_load_state(f, &vmstate_configuration, &savevm_state, 0);
2295 
2296         if (ret) {
2297             qemu_loadvm_state_cleanup();
2298             return ret;
2299         }
2300     }
2301     return 0;
2302 }
2303 
2304 static int qemu_loadvm_state_setup(QEMUFile *f)
2305 {
2306     SaveStateEntry *se;
2307     int ret;
2308 
2309     trace_loadvm_state_setup();
2310     QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
2311         if (!se->ops || !se->ops->load_setup) {
2312             continue;
2313         }
2314         if (se->ops && se->ops->is_active) {
2315             if (!se->ops->is_active(se->opaque)) {
2316                 continue;
2317             }
2318         }
2319 
2320         ret = se->ops->load_setup(f, se->opaque);
2321         if (ret < 0) {
2322             qemu_file_set_error(f, ret);
2323             error_report("Load state of device %s failed", se->idstr);
2324             return ret;
2325         }
2326     }
2327     return 0;
2328 }
2329 
2330 void qemu_loadvm_state_cleanup(void)
2331 {
2332     SaveStateEntry *se;
2333 
2334     trace_loadvm_state_cleanup();
2335     QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
2336         if (se->ops && se->ops->load_cleanup) {
2337             se->ops->load_cleanup(se->opaque);
2338         }
2339     }
2340 }
2341 
2342 /* Return true if we should continue the migration, or false. */
2343 static bool postcopy_pause_incoming(MigrationIncomingState *mis)
2344 {
2345     trace_postcopy_pause_incoming();
2346 
2347     /* Clear the triggered bit to allow one recovery */
2348     mis->postcopy_recover_triggered = false;
2349 
2350     assert(mis->from_src_file);
2351     qemu_file_shutdown(mis->from_src_file);
2352     qemu_fclose(mis->from_src_file);
2353     mis->from_src_file = NULL;
2354 
2355     assert(mis->to_src_file);
2356     qemu_file_shutdown(mis->to_src_file);
2357     qemu_mutex_lock(&mis->rp_mutex);
2358     qemu_fclose(mis->to_src_file);
2359     mis->to_src_file = NULL;
2360     qemu_mutex_unlock(&mis->rp_mutex);
2361 
2362     migrate_set_state(&mis->state, MIGRATION_STATUS_POSTCOPY_ACTIVE,
2363                       MIGRATION_STATUS_POSTCOPY_PAUSED);
2364 
2365     /* Notify the fault thread for the invalidated file handle */
2366     postcopy_fault_thread_notify(mis);
2367 
2368     error_report("Detected IO failure for postcopy. "
2369                  "Migration paused.");
2370 
2371     while (mis->state == MIGRATION_STATUS_POSTCOPY_PAUSED) {
2372         qemu_sem_wait(&mis->postcopy_pause_sem_dst);
2373     }
2374 
2375     trace_postcopy_pause_incoming_continued();
2376 
2377     return true;
2378 }
2379 
2380 int qemu_loadvm_state_main(QEMUFile *f, MigrationIncomingState *mis)
2381 {
2382     uint8_t section_type;
2383     int ret = 0;
2384 
2385 retry:
2386     while (true) {
2387         section_type = qemu_get_byte(f);
2388 
2389         if (qemu_file_get_error(f)) {
2390             ret = qemu_file_get_error(f);
2391             break;
2392         }
2393 
2394         trace_qemu_loadvm_state_section(section_type);
2395         switch (section_type) {
2396         case QEMU_VM_SECTION_START:
2397         case QEMU_VM_SECTION_FULL:
2398             ret = qemu_loadvm_section_start_full(f, mis);
2399             if (ret < 0) {
2400                 goto out;
2401             }
2402             break;
2403         case QEMU_VM_SECTION_PART:
2404         case QEMU_VM_SECTION_END:
2405             ret = qemu_loadvm_section_part_end(f, mis);
2406             if (ret < 0) {
2407                 goto out;
2408             }
2409             break;
2410         case QEMU_VM_COMMAND:
2411             ret = loadvm_process_command(f);
2412             trace_qemu_loadvm_state_section_command(ret);
2413             if ((ret < 0) || (ret & LOADVM_QUIT)) {
2414                 goto out;
2415             }
2416             break;
2417         case QEMU_VM_EOF:
2418             /* This is the end of migration */
2419             goto out;
2420         default:
2421             error_report("Unknown savevm section type %d", section_type);
2422             ret = -EINVAL;
2423             goto out;
2424         }
2425     }
2426 
2427 out:
2428     if (ret < 0) {
2429         qemu_file_set_error(f, ret);
2430 
2431         /*
2432          * If we are during an active postcopy, then we pause instead
2433          * of bail out to at least keep the VM's dirty data.  Note
2434          * that POSTCOPY_INCOMING_LISTENING stage is still not enough,
2435          * during which we're still receiving device states and we
2436          * still haven't yet started the VM on destination.
2437          */
2438         if (postcopy_state_get() == POSTCOPY_INCOMING_RUNNING &&
2439             postcopy_pause_incoming(mis)) {
2440             /* Reset f to point to the newly created channel */
2441             f = mis->from_src_file;
2442             goto retry;
2443         }
2444     }
2445     return ret;
2446 }
2447 
2448 int qemu_loadvm_state(QEMUFile *f)
2449 {
2450     MigrationIncomingState *mis = migration_incoming_get_current();
2451     Error *local_err = NULL;
2452     int ret;
2453 
2454     if (qemu_savevm_state_blocked(&local_err)) {
2455         error_report_err(local_err);
2456         return -EINVAL;
2457     }
2458 
2459     ret = qemu_loadvm_state_header(f);
2460     if (ret) {
2461         return ret;
2462     }
2463 
2464     if (qemu_loadvm_state_setup(f) != 0) {
2465         return -EINVAL;
2466     }
2467 
2468     cpu_synchronize_all_pre_loadvm();
2469 
2470     ret = qemu_loadvm_state_main(f, mis);
2471     qemu_event_set(&mis->main_thread_load_event);
2472 
2473     trace_qemu_loadvm_state_post_main(ret);
2474 
2475     if (mis->have_listen_thread) {
2476         /* Listen thread still going, can't clean up yet */
2477         return ret;
2478     }
2479 
2480     if (ret == 0) {
2481         ret = qemu_file_get_error(f);
2482     }
2483 
2484     /*
2485      * Try to read in the VMDESC section as well, so that dumping tools that
2486      * intercept our migration stream have the chance to see it.
2487      */
2488 
2489     /* We've got to be careful; if we don't read the data and just shut the fd
2490      * then the sender can error if we close while it's still sending.
2491      * We also mustn't read data that isn't there; some transports (RDMA)
2492      * will stall waiting for that data when the source has already closed.
2493      */
2494     if (ret == 0 && should_send_vmdesc()) {
2495         uint8_t *buf;
2496         uint32_t size;
2497         uint8_t  section_type = qemu_get_byte(f);
2498 
2499         if (section_type != QEMU_VM_VMDESCRIPTION) {
2500             error_report("Expected vmdescription section, but got %d",
2501                          section_type);
2502             /*
2503              * It doesn't seem worth failing at this point since
2504              * we apparently have an otherwise valid VM state
2505              */
2506         } else {
2507             buf = g_malloc(0x1000);
2508             size = qemu_get_be32(f);
2509 
2510             while (size > 0) {
2511                 uint32_t read_chunk = MIN(size, 0x1000);
2512                 qemu_get_buffer(f, buf, read_chunk);
2513                 size -= read_chunk;
2514             }
2515             g_free(buf);
2516         }
2517     }
2518 
2519     qemu_loadvm_state_cleanup();
2520     cpu_synchronize_all_post_init();
2521 
2522     return ret;
2523 }
2524 
2525 int qemu_load_device_state(QEMUFile *f)
2526 {
2527     MigrationIncomingState *mis = migration_incoming_get_current();
2528     int ret;
2529 
2530     /* Load QEMU_VM_SECTION_FULL section */
2531     ret = qemu_loadvm_state_main(f, mis);
2532     if (ret < 0) {
2533         error_report("Failed to load device state: %d", ret);
2534         return ret;
2535     }
2536 
2537     cpu_synchronize_all_post_init();
2538     return 0;
2539 }
2540 
2541 int save_snapshot(const char *name, Error **errp)
2542 {
2543     BlockDriverState *bs, *bs1;
2544     QEMUSnapshotInfo sn1, *sn = &sn1, old_sn1, *old_sn = &old_sn1;
2545     int ret = -1;
2546     QEMUFile *f;
2547     int saved_vm_running;
2548     uint64_t vm_state_size;
2549     qemu_timeval tv;
2550     struct tm tm;
2551     AioContext *aio_context;
2552 
2553     if (migration_is_blocked(errp)) {
2554         return ret;
2555     }
2556 
2557     if (!replay_can_snapshot()) {
2558         error_setg(errp, "Record/replay does not allow making snapshot "
2559                    "right now. Try once more later.");
2560         return ret;
2561     }
2562 
2563     if (!bdrv_all_can_snapshot(&bs)) {
2564         error_setg(errp, "Device '%s' is writable but does not support "
2565                    "snapshots", bdrv_get_device_name(bs));
2566         return ret;
2567     }
2568 
2569     /* Delete old snapshots of the same name */
2570     if (name) {
2571         ret = bdrv_all_delete_snapshot(name, &bs1, errp);
2572         if (ret < 0) {
2573             error_prepend(errp, "Error while deleting snapshot on device "
2574                           "'%s': ", bdrv_get_device_name(bs1));
2575             return ret;
2576         }
2577     }
2578 
2579     bs = bdrv_all_find_vmstate_bs();
2580     if (bs == NULL) {
2581         error_setg(errp, "No block device can accept snapshots");
2582         return ret;
2583     }
2584     aio_context = bdrv_get_aio_context(bs);
2585 
2586     saved_vm_running = runstate_is_running();
2587 
2588     ret = global_state_store();
2589     if (ret) {
2590         error_setg(errp, "Error saving global state");
2591         return ret;
2592     }
2593     vm_stop(RUN_STATE_SAVE_VM);
2594 
2595     bdrv_drain_all_begin();
2596 
2597     aio_context_acquire(aio_context);
2598 
2599     memset(sn, 0, sizeof(*sn));
2600 
2601     /* fill auxiliary fields */
2602     qemu_gettimeofday(&tv);
2603     sn->date_sec = tv.tv_sec;
2604     sn->date_nsec = tv.tv_usec * 1000;
2605     sn->vm_clock_nsec = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
2606 
2607     if (name) {
2608         ret = bdrv_snapshot_find(bs, old_sn, name);
2609         if (ret >= 0) {
2610             pstrcpy(sn->name, sizeof(sn->name), old_sn->name);
2611             pstrcpy(sn->id_str, sizeof(sn->id_str), old_sn->id_str);
2612         } else {
2613             pstrcpy(sn->name, sizeof(sn->name), name);
2614         }
2615     } else {
2616         /* cast below needed for OpenBSD where tv_sec is still 'long' */
2617         localtime_r((const time_t *)&tv.tv_sec, &tm);
2618         strftime(sn->name, sizeof(sn->name), "vm-%Y%m%d%H%M%S", &tm);
2619     }
2620 
2621     /* save the VM state */
2622     f = qemu_fopen_bdrv(bs, 1);
2623     if (!f) {
2624         error_setg(errp, "Could not open VM state file");
2625         goto the_end;
2626     }
2627     ret = qemu_savevm_state(f, errp);
2628     vm_state_size = qemu_ftell(f);
2629     qemu_fclose(f);
2630     if (ret < 0) {
2631         goto the_end;
2632     }
2633 
2634     /* The bdrv_all_create_snapshot() call that follows acquires the AioContext
2635      * for itself.  BDRV_POLL_WHILE() does not support nested locking because
2636      * it only releases the lock once.  Therefore synchronous I/O will deadlock
2637      * unless we release the AioContext before bdrv_all_create_snapshot().
2638      */
2639     aio_context_release(aio_context);
2640     aio_context = NULL;
2641 
2642     ret = bdrv_all_create_snapshot(sn, bs, vm_state_size, &bs);
2643     if (ret < 0) {
2644         error_setg(errp, "Error while creating snapshot on '%s'",
2645                    bdrv_get_device_name(bs));
2646         goto the_end;
2647     }
2648 
2649     ret = 0;
2650 
2651  the_end:
2652     if (aio_context) {
2653         aio_context_release(aio_context);
2654     }
2655 
2656     bdrv_drain_all_end();
2657 
2658     if (saved_vm_running) {
2659         vm_start();
2660     }
2661     return ret;
2662 }
2663 
2664 void qmp_xen_save_devices_state(const char *filename, bool has_live, bool live,
2665                                 Error **errp)
2666 {
2667     QEMUFile *f;
2668     QIOChannelFile *ioc;
2669     int saved_vm_running;
2670     int ret;
2671 
2672     if (!has_live) {
2673         /* live default to true so old version of Xen tool stack can have a
2674          * successfull live migration */
2675         live = true;
2676     }
2677 
2678     saved_vm_running = runstate_is_running();
2679     vm_stop(RUN_STATE_SAVE_VM);
2680     global_state_store_running();
2681 
2682     ioc = qio_channel_file_new_path(filename, O_WRONLY | O_CREAT, 0660, errp);
2683     if (!ioc) {
2684         goto the_end;
2685     }
2686     qio_channel_set_name(QIO_CHANNEL(ioc), "migration-xen-save-state");
2687     f = qemu_fopen_channel_output(QIO_CHANNEL(ioc));
2688     object_unref(OBJECT(ioc));
2689     ret = qemu_save_device_state(f);
2690     if (ret < 0 || qemu_fclose(f) < 0) {
2691         error_setg(errp, QERR_IO_ERROR);
2692     } else {
2693         /* libxl calls the QMP command "stop" before calling
2694          * "xen-save-devices-state" and in case of migration failure, libxl
2695          * would call "cont".
2696          * So call bdrv_inactivate_all (release locks) here to let the other
2697          * side of the migration take controle of the images.
2698          */
2699         if (live && !saved_vm_running) {
2700             ret = bdrv_inactivate_all();
2701             if (ret) {
2702                 error_setg(errp, "%s: bdrv_inactivate_all() failed (%d)",
2703                            __func__, ret);
2704             }
2705         }
2706     }
2707 
2708  the_end:
2709     if (saved_vm_running) {
2710         vm_start();
2711     }
2712 }
2713 
2714 void qmp_xen_load_devices_state(const char *filename, Error **errp)
2715 {
2716     QEMUFile *f;
2717     QIOChannelFile *ioc;
2718     int ret;
2719 
2720     /* Guest must be paused before loading the device state; the RAM state
2721      * will already have been loaded by xc
2722      */
2723     if (runstate_is_running()) {
2724         error_setg(errp, "Cannot update device state while vm is running");
2725         return;
2726     }
2727     vm_stop(RUN_STATE_RESTORE_VM);
2728 
2729     ioc = qio_channel_file_new_path(filename, O_RDONLY | O_BINARY, 0, errp);
2730     if (!ioc) {
2731         return;
2732     }
2733     qio_channel_set_name(QIO_CHANNEL(ioc), "migration-xen-load-state");
2734     f = qemu_fopen_channel_input(QIO_CHANNEL(ioc));
2735     object_unref(OBJECT(ioc));
2736 
2737     ret = qemu_loadvm_state(f);
2738     qemu_fclose(f);
2739     if (ret < 0) {
2740         error_setg(errp, QERR_IO_ERROR);
2741     }
2742     migration_incoming_state_destroy();
2743 }
2744 
2745 int load_snapshot(const char *name, Error **errp)
2746 {
2747     BlockDriverState *bs, *bs_vm_state;
2748     QEMUSnapshotInfo sn;
2749     QEMUFile *f;
2750     int ret;
2751     AioContext *aio_context;
2752     MigrationIncomingState *mis = migration_incoming_get_current();
2753 
2754     if (!replay_can_snapshot()) {
2755         error_setg(errp, "Record/replay does not allow loading snapshot "
2756                    "right now. Try once more later.");
2757         return -EINVAL;
2758     }
2759 
2760     if (!bdrv_all_can_snapshot(&bs)) {
2761         error_setg(errp,
2762                    "Device '%s' is writable but does not support snapshots",
2763                    bdrv_get_device_name(bs));
2764         return -ENOTSUP;
2765     }
2766     ret = bdrv_all_find_snapshot(name, &bs);
2767     if (ret < 0) {
2768         error_setg(errp,
2769                    "Device '%s' does not have the requested snapshot '%s'",
2770                    bdrv_get_device_name(bs), name);
2771         return ret;
2772     }
2773 
2774     bs_vm_state = bdrv_all_find_vmstate_bs();
2775     if (!bs_vm_state) {
2776         error_setg(errp, "No block device supports snapshots");
2777         return -ENOTSUP;
2778     }
2779     aio_context = bdrv_get_aio_context(bs_vm_state);
2780 
2781     /* Don't even try to load empty VM states */
2782     aio_context_acquire(aio_context);
2783     ret = bdrv_snapshot_find(bs_vm_state, &sn, name);
2784     aio_context_release(aio_context);
2785     if (ret < 0) {
2786         return ret;
2787     } else if (sn.vm_state_size == 0) {
2788         error_setg(errp, "This is a disk-only snapshot. Revert to it "
2789                    " offline using qemu-img");
2790         return -EINVAL;
2791     }
2792 
2793     /* Flush all IO requests so they don't interfere with the new state.  */
2794     bdrv_drain_all_begin();
2795 
2796     ret = bdrv_all_goto_snapshot(name, &bs, errp);
2797     if (ret < 0) {
2798         error_prepend(errp, "Could not load snapshot '%s' on '%s': ",
2799                       name, bdrv_get_device_name(bs));
2800         goto err_drain;
2801     }
2802 
2803     /* restore the VM state */
2804     f = qemu_fopen_bdrv(bs_vm_state, 0);
2805     if (!f) {
2806         error_setg(errp, "Could not open VM state file");
2807         ret = -EINVAL;
2808         goto err_drain;
2809     }
2810 
2811     qemu_system_reset(SHUTDOWN_CAUSE_NONE);
2812     mis->from_src_file = f;
2813 
2814     aio_context_acquire(aio_context);
2815     ret = qemu_loadvm_state(f);
2816     migration_incoming_state_destroy();
2817     aio_context_release(aio_context);
2818 
2819     bdrv_drain_all_end();
2820 
2821     if (ret < 0) {
2822         error_setg(errp, "Error %d while loading VM state", ret);
2823         return ret;
2824     }
2825 
2826     return 0;
2827 
2828 err_drain:
2829     bdrv_drain_all_end();
2830     return ret;
2831 }
2832 
2833 void vmstate_register_ram(MemoryRegion *mr, DeviceState *dev)
2834 {
2835     qemu_ram_set_idstr(mr->ram_block,
2836                        memory_region_name(mr), dev);
2837     qemu_ram_set_migratable(mr->ram_block);
2838 }
2839 
2840 void vmstate_unregister_ram(MemoryRegion *mr, DeviceState *dev)
2841 {
2842     qemu_ram_unset_idstr(mr->ram_block);
2843     qemu_ram_unset_migratable(mr->ram_block);
2844 }
2845 
2846 void vmstate_register_ram_global(MemoryRegion *mr)
2847 {
2848     vmstate_register_ram(mr, NULL);
2849 }
2850 
2851 bool vmstate_check_only_migratable(const VMStateDescription *vmsd)
2852 {
2853     /* check needed if --only-migratable is specified */
2854     if (!only_migratable) {
2855         return true;
2856     }
2857 
2858     return !(vmsd && vmsd->unmigratable);
2859 }
2860