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