]> git.proxmox.com Git - mirror_qemu.git/blame - migration/vmstate.c
migration: check pre_save return in vmstate_save_state
[mirror_qemu.git] / migration / vmstate.c
CommitLineData
576d1abc
JQ
1/*
2 * VMState interpreter
3 *
4 * Copyright (c) 2009-2017 Red Hat Inc
5 *
6 * Authors:
7 * Juan Quintela <quintela@redhat.com>
8 *
9 * This work is licensed under the terms of the GNU GPL, version 2 or later.
10 * See the COPYING file in the top-level directory.
11 */
12
1393a485 13#include "qemu/osdep.h"
b6fcfa59 14#include "qemu-common.h"
6666c96a 15#include "migration.h"
b6fcfa59 16#include "migration/vmstate.h"
c3d2e2e7 17#include "migration/savevm.h"
08a0aee1 18#include "qemu-file.h"
b6fcfa59 19#include "qemu/bitops.h"
6a64b644 20#include "qemu/error-report.h"
9013dca5 21#include "trace.h"
05b98c22 22#include "qjson.h"
b6fcfa59
EH
23
24static void vmstate_subsection_save(QEMUFile *f, const VMStateDescription *vmsd,
8118f095 25 void *opaque, QJSON *vmdesc);
b6fcfa59
EH
26static int vmstate_subsection_load(QEMUFile *f, const VMStateDescription *vmsd,
27 void *opaque);
28
35fc1f71
MT
29static int vmstate_n_elems(void *opaque, VMStateField *field)
30{
31 int n_elems = 1;
32
33 if (field->flags & VMS_ARRAY) {
34 n_elems = field->num;
35 } else if (field->flags & VMS_VARRAY_INT32) {
36 n_elems = *(int32_t *)(opaque+field->num_offset);
37 } else if (field->flags & VMS_VARRAY_UINT32) {
38 n_elems = *(uint32_t *)(opaque+field->num_offset);
39 } else if (field->flags & VMS_VARRAY_UINT16) {
40 n_elems = *(uint16_t *)(opaque+field->num_offset);
41 } else if (field->flags & VMS_VARRAY_UINT8) {
42 n_elems = *(uint8_t *)(opaque+field->num_offset);
43 }
44
b47d3af7
JQ
45 if (field->flags & VMS_MULTIPLY_ELEMENTS) {
46 n_elems *= field->num;
47 }
48
023ad1a6 49 trace_vmstate_n_elems(field->name, n_elems);
35fc1f71
MT
50 return n_elems;
51}
52
53static int vmstate_size(void *opaque, VMStateField *field)
54{
55 int size = field->size;
56
57 if (field->flags & VMS_VBUFFER) {
58 size = *(int32_t *)(opaque+field->size_offset);
59 if (field->flags & VMS_MULTIPLY) {
60 size *= field->size;
61 }
62 }
63
64 return size;
65}
66
cbfda0e6 67static void vmstate_handle_alloc(void *ptr, VMStateField *field, void *opaque)
35fc1f71 68{
cbfda0e6
HP
69 if (field->flags & VMS_POINTER && field->flags & VMS_ALLOC) {
70 gsize size = vmstate_size(opaque, field);
71 size *= vmstate_n_elems(opaque, field);
72 if (size) {
73 *(void **)ptr = g_malloc(size);
f32935ea 74 }
35fc1f71 75 }
35fc1f71
MT
76}
77
b6fcfa59
EH
78int vmstate_load_state(QEMUFile *f, const VMStateDescription *vmsd,
79 void *opaque, int version_id)
80{
81 VMStateField *field = vmsd->fields;
a5df2a02 82 int ret = 0;
b6fcfa59 83
a5df2a02 84 trace_vmstate_load_state(vmsd->name, version_id);
b6fcfa59 85 if (version_id > vmsd->version_id) {
cde7ee59
JD
86 error_report("%s: incoming version_id %d is too new "
87 "for local version_id %d",
88 vmsd->name, version_id, vmsd->version_id);
a5df2a02 89 trace_vmstate_load_state_end(vmsd->name, "too new", -EINVAL);
b6fcfa59
EH
90 return -EINVAL;
91 }
b6fcfa59 92 if (version_id < vmsd->minimum_version_id) {
767adce2
PM
93 if (vmsd->load_state_old &&
94 version_id >= vmsd->minimum_version_id_old) {
a5df2a02
DDAG
95 ret = vmsd->load_state_old(f, opaque, version_id);
96 trace_vmstate_load_state_end(vmsd->name, "old path", ret);
97 return ret;
767adce2 98 }
cde7ee59
JD
99 error_report("%s: incoming version_id %d is too old "
100 "for local minimum version_id %d",
101 vmsd->name, version_id, vmsd->minimum_version_id);
a5df2a02 102 trace_vmstate_load_state_end(vmsd->name, "too old", -EINVAL);
767adce2 103 return -EINVAL;
b6fcfa59
EH
104 }
105 if (vmsd->pre_load) {
106 int ret = vmsd->pre_load(opaque);
107 if (ret) {
108 return ret;
109 }
110 }
111 while (field->name) {
a5df2a02 112 trace_vmstate_load_state_field(vmsd->name, field->name);
b6fcfa59
EH
113 if ((field->field_exists &&
114 field->field_exists(opaque, version_id)) ||
115 (!field->field_exists &&
116 field->version_id <= version_id)) {
cbfda0e6 117 void *first_elem = opaque + field->offset;
35fc1f71
MT
118 int i, n_elems = vmstate_n_elems(opaque, field);
119 int size = vmstate_size(opaque, field);
120
cbfda0e6
HP
121 vmstate_handle_alloc(first_elem, field, opaque);
122 if (field->flags & VMS_POINTER) {
123 first_elem = *(void **)first_elem;
e1e686c1 124 assert(first_elem || !n_elems || !size);
cbfda0e6 125 }
b6fcfa59 126 for (i = 0; i < n_elems; i++) {
e84641f7 127 void *curr_elem = first_elem + size * i;
b6fcfa59
EH
128
129 if (field->flags & VMS_ARRAY_OF_POINTER) {
e84641f7 130 curr_elem = *(void **)curr_elem;
b6fcfa59 131 }
e1e686c1 132 if (!curr_elem && size) {
07d4e691
HP
133 /* if null pointer check placeholder and do not follow */
134 assert(field->flags & VMS_ARRAY_OF_POINTER);
135 ret = vmstate_info_nullptr.get(f, curr_elem, size, NULL);
136 } else if (field->flags & VMS_STRUCT) {
e84641f7 137 ret = vmstate_load_state(f, field->vmsd, curr_elem,
b6fcfa59
EH
138 field->vmsd->version_id);
139 } else {
e84641f7 140 ret = field->info->get(f, curr_elem, size, field);
b6fcfa59 141 }
13cde508
JQ
142 if (ret >= 0) {
143 ret = qemu_file_get_error(f);
144 }
b6fcfa59 145 if (ret < 0) {
13cde508 146 qemu_file_set_error(f, ret);
a1771070
DDAG
147 error_report("Failed to load %s:%s", vmsd->name,
148 field->name);
9013dca5 149 trace_vmstate_load_field_error(field->name, ret);
b6fcfa59
EH
150 return ret;
151 }
152 }
5bf81c8d 153 } else if (field->flags & VMS_MUST_EXIST) {
6a64b644
DDAG
154 error_report("Input validation failed: %s/%s",
155 vmsd->name, field->name);
5bf81c8d 156 return -1;
b6fcfa59
EH
157 }
158 field++;
159 }
160 ret = vmstate_subsection_load(f, vmsd, opaque);
161 if (ret != 0) {
162 return ret;
163 }
164 if (vmsd->post_load) {
a5df2a02 165 ret = vmsd->post_load(opaque, version_id);
b6fcfa59 166 }
a5df2a02
DDAG
167 trace_vmstate_load_state_end(vmsd->name, "end", ret);
168 return ret;
b6fcfa59
EH
169}
170
8118f095
AG
171static int vmfield_name_num(VMStateField *start, VMStateField *search)
172{
173 VMStateField *field;
174 int found = 0;
175
176 for (field = start; field->name; field++) {
177 if (!strcmp(field->name, search->name)) {
178 if (field == search) {
179 return found;
180 }
181 found++;
182 }
183 }
184
185 return -1;
186}
187
188static bool vmfield_name_is_unique(VMStateField *start, VMStateField *search)
189{
190 VMStateField *field;
191 int found = 0;
192
193 for (field = start; field->name; field++) {
194 if (!strcmp(field->name, search->name)) {
195 found++;
196 /* name found more than once, so it's not unique */
197 if (found > 1) {
198 return false;
199 }
200 }
201 }
202
203 return true;
204}
205
206static const char *vmfield_get_type_name(VMStateField *field)
207{
208 const char *type = "unknown";
209
210 if (field->flags & VMS_STRUCT) {
211 type = "struct";
212 } else if (field->info->name) {
213 type = field->info->name;
214 }
215
216 return type;
217}
218
219static bool vmsd_can_compress(VMStateField *field)
220{
221 if (field->field_exists) {
222 /* Dynamically existing fields mess up compression */
223 return false;
224 }
225
226 if (field->flags & VMS_STRUCT) {
227 VMStateField *sfield = field->vmsd->fields;
228 while (sfield->name) {
229 if (!vmsd_can_compress(sfield)) {
230 /* Child elements can't compress, so can't we */
231 return false;
232 }
233 sfield++;
234 }
235
236 if (field->vmsd->subsections) {
237 /* Subsections may come and go, better don't compress */
238 return false;
239 }
240 }
241
242 return true;
243}
244
245static void vmsd_desc_field_start(const VMStateDescription *vmsd, QJSON *vmdesc,
246 VMStateField *field, int i, int max)
247{
248 char *name, *old_name;
249 bool is_array = max > 1;
250 bool can_compress = vmsd_can_compress(field);
251
252 if (!vmdesc) {
253 return;
254 }
255
256 name = g_strdup(field->name);
257
258 /* Field name is not unique, need to make it unique */
259 if (!vmfield_name_is_unique(vmsd->fields, field)) {
260 int num = vmfield_name_num(vmsd->fields, field);
261 old_name = name;
262 name = g_strdup_printf("%s[%d]", name, num);
263 g_free(old_name);
264 }
265
266 json_start_object(vmdesc, NULL);
267 json_prop_str(vmdesc, "name", name);
268 if (is_array) {
269 if (can_compress) {
270 json_prop_int(vmdesc, "array_len", max);
271 } else {
272 json_prop_int(vmdesc, "index", i);
273 }
274 }
275 json_prop_str(vmdesc, "type", vmfield_get_type_name(field));
276
277 if (field->flags & VMS_STRUCT) {
278 json_start_object(vmdesc, "struct");
279 }
280
281 g_free(name);
282}
283
284static void vmsd_desc_field_end(const VMStateDescription *vmsd, QJSON *vmdesc,
285 VMStateField *field, size_t size, int i)
286{
287 if (!vmdesc) {
288 return;
289 }
290
291 if (field->flags & VMS_STRUCT) {
292 /* We printed a struct in between, close its child object */
293 json_end_object(vmdesc);
294 }
295
296 json_prop_int(vmdesc, "size", size);
297 json_end_object(vmdesc);
298}
299
df896152
JQ
300
301bool vmstate_save_needed(const VMStateDescription *vmsd, void *opaque)
302{
303 if (vmsd->needed && !vmsd->needed(opaque)) {
304 /* optional section not needed */
305 return false;
306 }
307 return true;
308}
309
310
551dbd08 311int vmstate_save_state(QEMUFile *f, const VMStateDescription *vmsd,
8118f095 312 void *opaque, QJSON *vmdesc)
b6fcfa59 313{
551dbd08 314 int ret = 0;
b6fcfa59
EH
315 VMStateField *field = vmsd->fields;
316
46a02a74
DDAG
317 trace_vmstate_save_state_top(vmsd->name);
318
b6fcfa59 319 if (vmsd->pre_save) {
551dbd08
DDAG
320 ret = vmsd->pre_save(opaque);
321 trace_vmstate_save_state_pre_save_res(vmsd->name, ret);
322 if (ret) {
323 error_report("pre-save failed: %s", vmsd->name);
324 return ret;
325 }
b6fcfa59 326 }
8118f095
AG
327
328 if (vmdesc) {
329 json_prop_str(vmdesc, "vmsd_name", vmsd->name);
330 json_prop_int(vmdesc, "version", vmsd->version_id);
331 json_start_array(vmdesc, "fields");
332 }
333
b6fcfa59
EH
334 while (field->name) {
335 if (!field->field_exists ||
336 field->field_exists(opaque, vmsd->version_id)) {
cbfda0e6 337 void *first_elem = opaque + field->offset;
35fc1f71
MT
338 int i, n_elems = vmstate_n_elems(opaque, field);
339 int size = vmstate_size(opaque, field);
8118f095
AG
340 int64_t old_offset, written_bytes;
341 QJSON *vmdesc_loop = vmdesc;
35fc1f71 342
46a02a74 343 trace_vmstate_save_state_loop(vmsd->name, field->name, n_elems);
cbfda0e6
HP
344 if (field->flags & VMS_POINTER) {
345 first_elem = *(void **)first_elem;
e1e686c1 346 assert(first_elem || !n_elems || !size);
cbfda0e6 347 }
b6fcfa59 348 for (i = 0; i < n_elems; i++) {
e84641f7 349 void *curr_elem = first_elem + size * i;
b6fcfa59 350
8118f095
AG
351 vmsd_desc_field_start(vmsd, vmdesc_loop, field, i, n_elems);
352 old_offset = qemu_ftell_fast(f);
b6fcfa59 353 if (field->flags & VMS_ARRAY_OF_POINTER) {
e84641f7
HP
354 assert(curr_elem);
355 curr_elem = *(void **)curr_elem;
b6fcfa59 356 }
e1e686c1 357 if (!curr_elem && size) {
07d4e691
HP
358 /* if null pointer write placeholder and do not follow */
359 assert(field->flags & VMS_ARRAY_OF_POINTER);
360 vmstate_info_nullptr.put(f, curr_elem, size, NULL, NULL);
361 } else if (field->flags & VMS_STRUCT) {
e84641f7 362 vmstate_save_state(f, field->vmsd, curr_elem, vmdesc_loop);
b6fcfa59 363 } else {
e84641f7 364 field->info->put(f, curr_elem, size, field, vmdesc_loop);
b6fcfa59 365 }
8118f095
AG
366
367 written_bytes = qemu_ftell_fast(f) - old_offset;
368 vmsd_desc_field_end(vmsd, vmdesc_loop, field, written_bytes, i);
369
370 /* Compressed arrays only care about the first element */
371 if (vmdesc_loop && vmsd_can_compress(field)) {
372 vmdesc_loop = NULL;
373 }
b6fcfa59 374 }
5bf81c8d
MT
375 } else {
376 if (field->flags & VMS_MUST_EXIST) {
6a64b644 377 error_report("Output state validation failed: %s/%s",
5bf81c8d
MT
378 vmsd->name, field->name);
379 assert(!(field->flags & VMS_MUST_EXIST));
380 }
b6fcfa59
EH
381 }
382 field++;
383 }
8118f095
AG
384
385 if (vmdesc) {
386 json_end_array(vmdesc);
387 }
388
389 vmstate_subsection_save(f, vmsd, opaque, vmdesc);
551dbd08
DDAG
390
391 return 0;
b6fcfa59
EH
392}
393
394static const VMStateDescription *
5cd8cada 395vmstate_get_subsection(const VMStateDescription **sub, char *idstr)
b6fcfa59 396{
5cd8cada
JQ
397 while (sub && *sub && (*sub)->needed) {
398 if (strcmp(idstr, (*sub)->name) == 0) {
399 return *sub;
b6fcfa59
EH
400 }
401 sub++;
402 }
403 return NULL;
404}
405
406static int vmstate_subsection_load(QEMUFile *f, const VMStateDescription *vmsd,
407 void *opaque)
408{
a5df2a02
DDAG
409 trace_vmstate_subsection_load(vmsd->name);
410
b6fcfa59 411 while (qemu_peek_byte(f, 0) == QEMU_VM_SUBSECTION) {
7c1e52ba 412 char idstr[256], *idstr_ret;
b6fcfa59
EH
413 int ret;
414 uint8_t version_id, len, size;
415 const VMStateDescription *sub_vmsd;
416
417 len = qemu_peek_byte(f, 1);
418 if (len < strlen(vmsd->name) + 1) {
419 /* subsection name has be be "section_name/a" */
023ad1a6 420 trace_vmstate_subsection_load_bad(vmsd->name, "(short)", "");
b6fcfa59
EH
421 return 0;
422 }
7c1e52ba 423 size = qemu_peek_buffer(f, (uint8_t **)&idstr_ret, len, 2);
b6fcfa59 424 if (size != len) {
023ad1a6 425 trace_vmstate_subsection_load_bad(vmsd->name, "(peek fail)", "");
b6fcfa59
EH
426 return 0;
427 }
7c1e52ba 428 memcpy(idstr, idstr_ret, size);
b6fcfa59
EH
429 idstr[size] = 0;
430
431 if (strncmp(vmsd->name, idstr, strlen(vmsd->name)) != 0) {
023ad1a6
DDAG
432 trace_vmstate_subsection_load_bad(vmsd->name, idstr, "(prefix)");
433 /* it doesn't have a valid subsection name */
b6fcfa59
EH
434 return 0;
435 }
436 sub_vmsd = vmstate_get_subsection(vmsd->subsections, idstr);
437 if (sub_vmsd == NULL) {
023ad1a6 438 trace_vmstate_subsection_load_bad(vmsd->name, idstr, "(lookup)");
b6fcfa59
EH
439 return -ENOENT;
440 }
441 qemu_file_skip(f, 1); /* subsection */
442 qemu_file_skip(f, 1); /* len */
443 qemu_file_skip(f, len); /* idstr */
444 version_id = qemu_get_be32(f);
445
446 ret = vmstate_load_state(f, sub_vmsd, opaque, version_id);
447 if (ret) {
023ad1a6 448 trace_vmstate_subsection_load_bad(vmsd->name, idstr, "(child)");
b6fcfa59
EH
449 return ret;
450 }
451 }
a5df2a02
DDAG
452
453 trace_vmstate_subsection_load_good(vmsd->name);
b6fcfa59
EH
454 return 0;
455}
456
457static void vmstate_subsection_save(QEMUFile *f, const VMStateDescription *vmsd,
8118f095 458 void *opaque, QJSON *vmdesc)
b6fcfa59 459{
5cd8cada 460 const VMStateDescription **sub = vmsd->subsections;
8118f095 461 bool subsection_found = false;
b6fcfa59 462
46a02a74 463 trace_vmstate_subsection_save_top(vmsd->name);
5cd8cada
JQ
464 while (sub && *sub && (*sub)->needed) {
465 if ((*sub)->needed(opaque)) {
46a02a74 466 const VMStateDescription *vmsdsub = *sub;
b6fcfa59
EH
467 uint8_t len;
468
46a02a74 469 trace_vmstate_subsection_save_loop(vmsd->name, vmsdsub->name);
8118f095
AG
470 if (vmdesc) {
471 /* Only create subsection array when we have any */
472 if (!subsection_found) {
473 json_start_array(vmdesc, "subsections");
474 subsection_found = true;
475 }
476
477 json_start_object(vmdesc, NULL);
478 }
479
b6fcfa59 480 qemu_put_byte(f, QEMU_VM_SUBSECTION);
46a02a74 481 len = strlen(vmsdsub->name);
b6fcfa59 482 qemu_put_byte(f, len);
46a02a74
DDAG
483 qemu_put_buffer(f, (uint8_t *)vmsdsub->name, len);
484 qemu_put_be32(f, vmsdsub->version_id);
485 vmstate_save_state(f, vmsdsub, opaque, vmdesc);
8118f095
AG
486
487 if (vmdesc) {
488 json_end_object(vmdesc);
489 }
b6fcfa59
EH
490 }
491 sub++;
492 }
8118f095
AG
493
494 if (vmdesc && subsection_found) {
495 json_end_array(vmdesc);
496 }
b6fcfa59 497}