]> Git Repo - qemu.git/blame - savevm.c
savevm: some coding style cleanups
[qemu.git] / savevm.c
CommitLineData
a672b469
AL
1/*
2 * QEMU System Emulator
3 *
4 * Copyright (c) 2003-2008 Fabrice Bellard
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
a672b469
AL
24#include <unistd.h>
25#include <fcntl.h>
a672b469
AL
26#include <time.h>
27#include <errno.h>
28#include <sys/time.h>
29#include <zlib.h>
30
71e72a19 31/* Needed early for CONFIG_BSD etc. */
d40cdb10
BS
32#include "config-host.h"
33
a672b469
AL
34#ifndef _WIN32
35#include <sys/times.h>
36#include <sys/wait.h>
37#include <termios.h>
38#include <sys/mman.h>
39#include <sys/ioctl.h>
40#include <sys/resource.h>
41#include <sys/socket.h>
42#include <netinet/in.h>
43#include <net/if.h>
a672b469
AL
44#include <arpa/inet.h>
45#include <dirent.h>
46#include <netdb.h>
47#include <sys/select.h>
71e72a19 48#ifdef CONFIG_BSD
a672b469 49#include <sys/stat.h>
a167ba50 50#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
a672b469
AL
51#include <libutil.h>
52#else
53#include <util.h>
54#endif
a672b469
AL
55#ifdef __linux__
56#include <pty.h>
57#include <malloc.h>
58#include <linux/rtc.h>
59#endif
60#endif
61#endif
62
63#ifdef _WIN32
49dc768d 64#include <windows.h>
a672b469
AL
65#include <malloc.h>
66#include <sys/timeb.h>
67#include <mmsystem.h>
68#define getopt_long_only getopt_long
69#define memalign(align, size) malloc(size)
70#endif
71
511d2b14
BS
72#include "qemu-common.h"
73#include "hw/hw.h"
7685ee6a 74#include "hw/qdev.h"
511d2b14
BS
75#include "net.h"
76#include "monitor.h"
77#include "sysemu.h"
78#include "qemu-timer.h"
79#include "qemu-char.h"
511d2b14
BS
80#include "audio/audio.h"
81#include "migration.h"
82#include "qemu_socket.h"
72cf2d4f 83#include "qemu-queue.h"
17a4663e 84#include "cpus.h"
511d2b14 85
a672b469 86#define SELF_ANNOUNCE_ROUNDS 5
a672b469 87
18995b98 88#ifndef ETH_P_RARP
f8778a77 89#define ETH_P_RARP 0x8035
18995b98
N
90#endif
91#define ARP_HTYPE_ETH 0x0001
92#define ARP_PTYPE_IP 0x0800
93#define ARP_OP_REQUEST_REV 0x3
94
95static int announce_self_create(uint8_t *buf,
a672b469
AL
96 uint8_t *mac_addr)
97{
18995b98
N
98 /* Ethernet header. */
99 memset(buf, 0xff, 6); /* destination MAC addr */
100 memcpy(buf + 6, mac_addr, 6); /* source MAC addr */
101 *(uint16_t *)(buf + 12) = htons(ETH_P_RARP); /* ethertype */
102
103 /* RARP header. */
104 *(uint16_t *)(buf + 14) = htons(ARP_HTYPE_ETH); /* hardware addr space */
105 *(uint16_t *)(buf + 16) = htons(ARP_PTYPE_IP); /* protocol addr space */
106 *(buf + 18) = 6; /* hardware addr length (ethernet) */
107 *(buf + 19) = 4; /* protocol addr length (IPv4) */
108 *(uint16_t *)(buf + 20) = htons(ARP_OP_REQUEST_REV); /* opcode */
109 memcpy(buf + 22, mac_addr, 6); /* source hw addr */
110 memset(buf + 28, 0x00, 4); /* source protocol addr */
111 memcpy(buf + 32, mac_addr, 6); /* target hw addr */
112 memset(buf + 38, 0x00, 4); /* target protocol addr */
113
114 /* Padding to get up to 60 bytes (ethernet min packet size, minus FCS). */
115 memset(buf + 42, 0x00, 18);
116
117 return 60; /* len (FCS will be added by hardware) */
a672b469
AL
118}
119
f401ca22 120static void qemu_announce_self_iter(NICState *nic, void *opaque)
a672b469 121{
18995b98 122 uint8_t buf[60];
f401ca22
MM
123 int len;
124
125 len = announce_self_create(buf, nic->conf->macaddr.a);
126
127 qemu_send_packet_raw(&nic->nc, buf, len);
128}
129
130
131static void qemu_announce_self_once(void *opaque)
132{
ed8b330b
GN
133 static int count = SELF_ANNOUNCE_ROUNDS;
134 QEMUTimer *timer = *(QEMUTimer **)opaque;
a672b469 135
f401ca22
MM
136 qemu_foreach_nic(qemu_announce_self_iter, NULL);
137
18995b98
N
138 if (--count) {
139 /* delay 50ms, 150ms, 250ms, ... */
7bd427d8 140 qemu_mod_timer(timer, qemu_get_clock_ms(rt_clock) +
18995b98 141 50 + (SELF_ANNOUNCE_ROUNDS - count - 1) * 100);
ed8b330b
GN
142 } else {
143 qemu_del_timer(timer);
144 qemu_free_timer(timer);
145 }
146}
147
148void qemu_announce_self(void)
149{
150 static QEMUTimer *timer;
7bd427d8 151 timer = qemu_new_timer_ms(rt_clock, qemu_announce_self_once, &timer);
ed8b330b 152 qemu_announce_self_once(&timer);
a672b469
AL
153}
154
155/***********************************************************/
156/* savevm/loadvm support */
157
158#define IO_BUF_SIZE 32768
159
160struct QEMUFile {
161 QEMUFilePutBufferFunc *put_buffer;
162 QEMUFileGetBufferFunc *get_buffer;
163 QEMUFileCloseFunc *close;
164 QEMUFileRateLimit *rate_limit;
19629537 165 QEMUFileSetRateLimit *set_rate_limit;
c163b5ca 166 QEMUFileGetRateLimit *get_rate_limit;
a672b469
AL
167 void *opaque;
168 int is_write;
169
170 int64_t buf_offset; /* start of buffer when writing, end of buffer
171 when reading */
172 int buf_index;
173 int buf_size; /* 0 when writing */
174 uint8_t buf[IO_BUF_SIZE];
175
176 int has_error;
177};
178
7f79dd28 179typedef struct QEMUFileStdio
a672b469 180{
7f79dd28 181 FILE *stdio_file;
a672b469 182 QEMUFile *file;
7f79dd28 183} QEMUFileStdio;
a672b469
AL
184
185typedef struct QEMUFileSocket
186{
187 int fd;
188 QEMUFile *file;
189} QEMUFileSocket;
190
191static int socket_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
192{
193 QEMUFileSocket *s = opaque;
194 ssize_t len;
195
196 do {
00aa0040 197 len = qemu_recv(s->fd, buf, size, 0);
a672b469
AL
198 } while (len == -1 && socket_error() == EINTR);
199
200 if (len == -1)
201 len = -socket_error();
202
203 return len;
204}
205
206static int socket_close(void *opaque)
207{
208 QEMUFileSocket *s = opaque;
7267c094 209 g_free(s);
a672b469
AL
210 return 0;
211}
212
7f79dd28 213static int stdio_put_buffer(void *opaque, const uint8_t *buf, int64_t pos, int size)
a672b469 214{
7f79dd28
PB
215 QEMUFileStdio *s = opaque;
216 return fwrite(buf, 1, size, s->stdio_file);
a672b469
AL
217}
218
7f79dd28 219static int stdio_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
a672b469 220{
7f79dd28
PB
221 QEMUFileStdio *s = opaque;
222 FILE *fp = s->stdio_file;
8a67ec4d
UL
223 int bytes;
224
225 do {
226 clearerr(fp);
227 bytes = fread(buf, 1, size, fp);
228 } while ((bytes == 0) && ferror(fp) && (errno == EINTR));
229 return bytes;
a672b469
AL
230}
231
7f79dd28
PB
232static int stdio_pclose(void *opaque)
233{
234 QEMUFileStdio *s = opaque;
41ef56e6
AL
235 int ret;
236 ret = pclose(s->stdio_file);
7267c094 237 g_free(s);
41ef56e6 238 return ret;
7f79dd28
PB
239}
240
241static int stdio_fclose(void *opaque)
a672b469 242{
7f79dd28
PB
243 QEMUFileStdio *s = opaque;
244 fclose(s->stdio_file);
7267c094 245 g_free(s);
a672b469
AL
246 return 0;
247}
248
7f79dd28 249QEMUFile *qemu_popen(FILE *stdio_file, const char *mode)
a672b469 250{
7f79dd28 251 QEMUFileStdio *s;
a672b469 252
7f79dd28 253 if (stdio_file == NULL || mode == NULL || (mode[0] != 'r' && mode[0] != 'w') || mode[1] != 0) {
a672b469
AL
254 fprintf(stderr, "qemu_popen: Argument validity check failed\n");
255 return NULL;
256 }
257
7267c094 258 s = g_malloc0(sizeof(QEMUFileStdio));
a672b469 259
7f79dd28 260 s->stdio_file = stdio_file;
a672b469
AL
261
262 if(mode[0] == 'r') {
c163b5ca
LS
263 s->file = qemu_fopen_ops(s, NULL, stdio_get_buffer, stdio_pclose,
264 NULL, NULL, NULL);
a672b469 265 } else {
c163b5ca
LS
266 s->file = qemu_fopen_ops(s, stdio_put_buffer, NULL, stdio_pclose,
267 NULL, NULL, NULL);
a672b469 268 }
a672b469
AL
269 return s->file;
270}
271
272QEMUFile *qemu_popen_cmd(const char *command, const char *mode)
273{
274 FILE *popen_file;
275
276 popen_file = popen(command, mode);
277 if(popen_file == NULL) {
278 return NULL;
279 }
280
281 return qemu_popen(popen_file, mode);
282}
283
7f79dd28 284int qemu_stdio_fd(QEMUFile *f)
8a43b1ea 285{
7f79dd28 286 QEMUFileStdio *p;
8a43b1ea
CL
287 int fd;
288
7f79dd28
PB
289 p = (QEMUFileStdio *)f->opaque;
290 fd = fileno(p->stdio_file);
8a43b1ea
CL
291
292 return fd;
293}
294
5ac1fad3
PB
295QEMUFile *qemu_fdopen(int fd, const char *mode)
296{
297 QEMUFileStdio *s;
298
299 if (mode == NULL ||
300 (mode[0] != 'r' && mode[0] != 'w') ||
301 mode[1] != 'b' || mode[2] != 0) {
302 fprintf(stderr, "qemu_fdopen: Argument validity check failed\n");
303 return NULL;
304 }
305
7267c094 306 s = g_malloc0(sizeof(QEMUFileStdio));
5ac1fad3
PB
307 s->stdio_file = fdopen(fd, mode);
308 if (!s->stdio_file)
309 goto fail;
310
311 if(mode[0] == 'r') {
c163b5ca
LS
312 s->file = qemu_fopen_ops(s, NULL, stdio_get_buffer, stdio_fclose,
313 NULL, NULL, NULL);
5ac1fad3 314 } else {
c163b5ca
LS
315 s->file = qemu_fopen_ops(s, stdio_put_buffer, NULL, stdio_fclose,
316 NULL, NULL, NULL);
5ac1fad3
PB
317 }
318 return s->file;
319
320fail:
7267c094 321 g_free(s);
5ac1fad3
PB
322 return NULL;
323}
324
a672b469
AL
325QEMUFile *qemu_fopen_socket(int fd)
326{
7267c094 327 QEMUFileSocket *s = g_malloc0(sizeof(QEMUFileSocket));
a672b469 328
a672b469 329 s->fd = fd;
c163b5ca
LS
330 s->file = qemu_fopen_ops(s, NULL, socket_get_buffer, socket_close,
331 NULL, NULL, NULL);
a672b469
AL
332 return s->file;
333}
334
a672b469
AL
335static int file_put_buffer(void *opaque, const uint8_t *buf,
336 int64_t pos, int size)
337{
338 QEMUFileStdio *s = opaque;
7f79dd28 339 fseek(s->stdio_file, pos, SEEK_SET);
5fdb3aa1 340 return fwrite(buf, 1, size, s->stdio_file);
a672b469
AL
341}
342
343static int file_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
344{
345 QEMUFileStdio *s = opaque;
7f79dd28
PB
346 fseek(s->stdio_file, pos, SEEK_SET);
347 return fread(buf, 1, size, s->stdio_file);
a672b469
AL
348}
349
350QEMUFile *qemu_fopen(const char *filename, const char *mode)
351{
352 QEMUFileStdio *s;
353
7f79dd28
PB
354 if (mode == NULL ||
355 (mode[0] != 'r' && mode[0] != 'w') ||
356 mode[1] != 'b' || mode[2] != 0) {
090414a3 357 fprintf(stderr, "qemu_fopen: Argument validity check failed\n");
7f79dd28
PB
358 return NULL;
359 }
360
7267c094 361 s = g_malloc0(sizeof(QEMUFileStdio));
a672b469 362
7f79dd28
PB
363 s->stdio_file = fopen(filename, mode);
364 if (!s->stdio_file)
a672b469 365 goto fail;
c163b5ca 366
7f79dd28 367 if(mode[0] == 'w') {
c163b5ca
LS
368 s->file = qemu_fopen_ops(s, file_put_buffer, NULL, stdio_fclose,
369 NULL, NULL, NULL);
7f79dd28 370 } else {
c163b5ca
LS
371 s->file = qemu_fopen_ops(s, NULL, file_get_buffer, stdio_fclose,
372 NULL, NULL, NULL);
7f79dd28
PB
373 }
374 return s->file;
a672b469 375fail:
7267c094 376 g_free(s);
a672b469
AL
377 return NULL;
378}
379
178e08a5 380static int block_put_buffer(void *opaque, const uint8_t *buf,
a672b469
AL
381 int64_t pos, int size)
382{
45566e9c 383 bdrv_save_vmstate(opaque, buf, pos, size);
a672b469
AL
384 return size;
385}
386
178e08a5 387static int block_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
a672b469 388{
45566e9c 389 return bdrv_load_vmstate(opaque, buf, pos, size);
a672b469
AL
390}
391
392static int bdrv_fclose(void *opaque)
393{
a672b469
AL
394 return 0;
395}
396
45566e9c 397static QEMUFile *qemu_fopen_bdrv(BlockDriverState *bs, int is_writable)
a672b469 398{
a672b469 399 if (is_writable)
c163b5ca
LS
400 return qemu_fopen_ops(bs, block_put_buffer, NULL, bdrv_fclose,
401 NULL, NULL, NULL);
402 return qemu_fopen_ops(bs, NULL, block_get_buffer, bdrv_fclose, NULL, NULL, NULL);
a672b469
AL
403}
404
405QEMUFile *qemu_fopen_ops(void *opaque, QEMUFilePutBufferFunc *put_buffer,
406 QEMUFileGetBufferFunc *get_buffer,
407 QEMUFileCloseFunc *close,
19629537 408 QEMUFileRateLimit *rate_limit,
c163b5ca
LS
409 QEMUFileSetRateLimit *set_rate_limit,
410 QEMUFileGetRateLimit *get_rate_limit)
a672b469
AL
411{
412 QEMUFile *f;
413
7267c094 414 f = g_malloc0(sizeof(QEMUFile));
a672b469
AL
415
416 f->opaque = opaque;
417 f->put_buffer = put_buffer;
418 f->get_buffer = get_buffer;
419 f->close = close;
420 f->rate_limit = rate_limit;
19629537 421 f->set_rate_limit = set_rate_limit;
c163b5ca 422 f->get_rate_limit = get_rate_limit;
a672b469
AL
423 f->is_write = 0;
424
425 return f;
426}
427
428int qemu_file_has_error(QEMUFile *f)
429{
430 return f->has_error;
431}
432
4dabe248
AL
433void qemu_file_set_error(QEMUFile *f)
434{
435 f->has_error = 1;
436}
437
a672b469
AL
438void qemu_fflush(QEMUFile *f)
439{
440 if (!f->put_buffer)
441 return;
442
443 if (f->is_write && f->buf_index > 0) {
444 int len;
445
446 len = f->put_buffer(f->opaque, f->buf, f->buf_offset, f->buf_index);
447 if (len > 0)
448 f->buf_offset += f->buf_index;
449 else
450 f->has_error = 1;
451 f->buf_index = 0;
452 }
453}
454
455static void qemu_fill_buffer(QEMUFile *f)
456{
457 int len;
0046c45b 458 int pending;
a672b469
AL
459
460 if (!f->get_buffer)
461 return;
462
463 if (f->is_write)
464 abort();
465
0046c45b
JQ
466 pending = f->buf_size - f->buf_index;
467 if (pending > 0) {
468 memmove(f->buf, f->buf + f->buf_index, pending);
469 }
470 f->buf_index = 0;
471 f->buf_size = pending;
472
473 len = f->get_buffer(f->opaque, f->buf + pending, f->buf_offset,
474 IO_BUF_SIZE - pending);
a672b469 475 if (len > 0) {
0046c45b 476 f->buf_size += len;
a672b469
AL
477 f->buf_offset += len;
478 } else if (len != -EAGAIN)
479 f->has_error = 1;
480}
481
482int qemu_fclose(QEMUFile *f)
483{
484 int ret = 0;
485 qemu_fflush(f);
486 if (f->close)
487 ret = f->close(f->opaque);
7267c094 488 g_free(f);
a672b469
AL
489 return ret;
490}
491
492void qemu_file_put_notify(QEMUFile *f)
493{
494 f->put_buffer(f->opaque, NULL, 0, 0);
495}
496
497void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size)
498{
499 int l;
500
501 if (!f->has_error && f->is_write == 0 && f->buf_index > 0) {
502 fprintf(stderr,
503 "Attempted to write to buffer while read buffer is not empty\n");
504 abort();
505 }
506
507 while (!f->has_error && size > 0) {
508 l = IO_BUF_SIZE - f->buf_index;
509 if (l > size)
510 l = size;
511 memcpy(f->buf + f->buf_index, buf, l);
512 f->is_write = 1;
513 f->buf_index += l;
514 buf += l;
515 size -= l;
516 if (f->buf_index >= IO_BUF_SIZE)
517 qemu_fflush(f);
518 }
519}
520
521void qemu_put_byte(QEMUFile *f, int v)
522{
523 if (!f->has_error && f->is_write == 0 && f->buf_index > 0) {
524 fprintf(stderr,
525 "Attempted to write to buffer while read buffer is not empty\n");
526 abort();
527 }
528
529 f->buf[f->buf_index++] = v;
530 f->is_write = 1;
531 if (f->buf_index >= IO_BUF_SIZE)
532 qemu_fflush(f);
533}
534
535int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size1)
536{
537 int size, l;
538
b9ce1454 539 if (f->is_write) {
a672b469 540 abort();
b9ce1454 541 }
a672b469
AL
542
543 size = size1;
544 while (size > 0) {
545 l = f->buf_size - f->buf_index;
546 if (l == 0) {
547 qemu_fill_buffer(f);
548 l = f->buf_size - f->buf_index;
b9ce1454 549 if (l == 0) {
a672b469 550 break;
b9ce1454 551 }
a672b469 552 }
b9ce1454 553 if (l > size) {
a672b469 554 l = size;
b9ce1454 555 }
a672b469
AL
556 memcpy(buf, f->buf + f->buf_index, l);
557 f->buf_index += l;
558 buf += l;
559 size -= l;
560 }
561 return size1 - size;
562}
563
811814bd
JQ
564static int qemu_peek_byte(QEMUFile *f)
565{
b9ce1454 566 if (f->is_write) {
811814bd 567 abort();
b9ce1454 568 }
811814bd
JQ
569
570 if (f->buf_index >= f->buf_size) {
571 qemu_fill_buffer(f);
b9ce1454 572 if (f->buf_index >= f->buf_size) {
811814bd 573 return 0;
b9ce1454 574 }
811814bd
JQ
575 }
576 return f->buf[f->buf_index];
577}
578
a672b469
AL
579int qemu_get_byte(QEMUFile *f)
580{
b9ce1454 581 if (f->is_write) {
a672b469 582 abort();
b9ce1454 583 }
a672b469
AL
584
585 if (f->buf_index >= f->buf_size) {
586 qemu_fill_buffer(f);
b9ce1454 587 if (f->buf_index >= f->buf_size) {
a672b469 588 return 0;
b9ce1454 589 }
a672b469
AL
590 }
591 return f->buf[f->buf_index++];
592}
593
594int64_t qemu_ftell(QEMUFile *f)
595{
596 return f->buf_offset - f->buf_size + f->buf_index;
597}
598
599int64_t qemu_fseek(QEMUFile *f, int64_t pos, int whence)
600{
601 if (whence == SEEK_SET) {
602 /* nothing to do */
603 } else if (whence == SEEK_CUR) {
604 pos += qemu_ftell(f);
605 } else {
606 /* SEEK_END not supported */
607 return -1;
608 }
609 if (f->put_buffer) {
610 qemu_fflush(f);
611 f->buf_offset = pos;
612 } else {
613 f->buf_offset = pos;
614 f->buf_index = 0;
615 f->buf_size = 0;
616 }
617 return pos;
618}
619
620int qemu_file_rate_limit(QEMUFile *f)
621{
622 if (f->rate_limit)
623 return f->rate_limit(f->opaque);
624
625 return 0;
626}
627
3d002df3 628int64_t qemu_file_get_rate_limit(QEMUFile *f)
c163b5ca
LS
629{
630 if (f->get_rate_limit)
631 return f->get_rate_limit(f->opaque);
632
633 return 0;
634}
635
3d002df3 636int64_t qemu_file_set_rate_limit(QEMUFile *f, int64_t new_rate)
19629537 637{
0bb05eaf
GC
638 /* any failed or completed migration keeps its state to allow probing of
639 * migration data, but has no associated file anymore */
640 if (f && f->set_rate_limit)
19629537
GC
641 return f->set_rate_limit(f->opaque, new_rate);
642
643 return 0;
644}
645
a672b469
AL
646void qemu_put_be16(QEMUFile *f, unsigned int v)
647{
648 qemu_put_byte(f, v >> 8);
649 qemu_put_byte(f, v);
650}
651
652void qemu_put_be32(QEMUFile *f, unsigned int v)
653{
654 qemu_put_byte(f, v >> 24);
655 qemu_put_byte(f, v >> 16);
656 qemu_put_byte(f, v >> 8);
657 qemu_put_byte(f, v);
658}
659
660void qemu_put_be64(QEMUFile *f, uint64_t v)
661{
662 qemu_put_be32(f, v >> 32);
663 qemu_put_be32(f, v);
664}
665
666unsigned int qemu_get_be16(QEMUFile *f)
667{
668 unsigned int v;
669 v = qemu_get_byte(f) << 8;
670 v |= qemu_get_byte(f);
671 return v;
672}
673
674unsigned int qemu_get_be32(QEMUFile *f)
675{
676 unsigned int v;
677 v = qemu_get_byte(f) << 24;
678 v |= qemu_get_byte(f) << 16;
679 v |= qemu_get_byte(f) << 8;
680 v |= qemu_get_byte(f);
681 return v;
682}
683
684uint64_t qemu_get_be64(QEMUFile *f)
685{
686 uint64_t v;
687 v = (uint64_t)qemu_get_be32(f) << 32;
688 v |= qemu_get_be32(f);
689 return v;
690}
691
cdae5cfb
GH
692/* bool */
693
694static int get_bool(QEMUFile *f, void *pv, size_t size)
695{
696 bool *v = pv;
697 *v = qemu_get_byte(f);
698 return 0;
699}
700
701static void put_bool(QEMUFile *f, void *pv, size_t size)
702{
703 bool *v = pv;
704 qemu_put_byte(f, *v);
705}
706
707const VMStateInfo vmstate_info_bool = {
708 .name = "bool",
709 .get = get_bool,
710 .put = put_bool,
711};
712
9ed7d6ae
JQ
713/* 8 bit int */
714
715static int get_int8(QEMUFile *f, void *pv, size_t size)
716{
717 int8_t *v = pv;
718 qemu_get_s8s(f, v);
719 return 0;
720}
721
84e2e3eb 722static void put_int8(QEMUFile *f, void *pv, size_t size)
9ed7d6ae 723{
84e2e3eb 724 int8_t *v = pv;
9ed7d6ae
JQ
725 qemu_put_s8s(f, v);
726}
727
728const VMStateInfo vmstate_info_int8 = {
729 .name = "int8",
730 .get = get_int8,
731 .put = put_int8,
732};
733
734/* 16 bit int */
735
736static int get_int16(QEMUFile *f, void *pv, size_t size)
737{
738 int16_t *v = pv;
739 qemu_get_sbe16s(f, v);
740 return 0;
741}
742
84e2e3eb 743static void put_int16(QEMUFile *f, void *pv, size_t size)
9ed7d6ae 744{
84e2e3eb 745 int16_t *v = pv;
9ed7d6ae
JQ
746 qemu_put_sbe16s(f, v);
747}
748
749const VMStateInfo vmstate_info_int16 = {
750 .name = "int16",
751 .get = get_int16,
752 .put = put_int16,
753};
754
755/* 32 bit int */
756
757static int get_int32(QEMUFile *f, void *pv, size_t size)
758{
759 int32_t *v = pv;
760 qemu_get_sbe32s(f, v);
761 return 0;
762}
763
84e2e3eb 764static void put_int32(QEMUFile *f, void *pv, size_t size)
9ed7d6ae 765{
84e2e3eb 766 int32_t *v = pv;
9ed7d6ae
JQ
767 qemu_put_sbe32s(f, v);
768}
769
770const VMStateInfo vmstate_info_int32 = {
771 .name = "int32",
772 .get = get_int32,
773 .put = put_int32,
774};
775
82501660
JQ
776/* 32 bit int. See that the received value is the same than the one
777 in the field */
778
779static int get_int32_equal(QEMUFile *f, void *pv, size_t size)
780{
781 int32_t *v = pv;
782 int32_t v2;
783 qemu_get_sbe32s(f, &v2);
784
785 if (*v == v2)
786 return 0;
787 return -EINVAL;
788}
789
790const VMStateInfo vmstate_info_int32_equal = {
791 .name = "int32 equal",
792 .get = get_int32_equal,
793 .put = put_int32,
794};
795
0a031e0a
JQ
796/* 32 bit int. See that the received value is the less or the same
797 than the one in the field */
798
799static int get_int32_le(QEMUFile *f, void *pv, size_t size)
800{
801 int32_t *old = pv;
802 int32_t new;
803 qemu_get_sbe32s(f, &new);
804
805 if (*old <= new)
806 return 0;
807 return -EINVAL;
808}
809
810const VMStateInfo vmstate_info_int32_le = {
811 .name = "int32 equal",
812 .get = get_int32_le,
813 .put = put_int32,
814};
815
9ed7d6ae
JQ
816/* 64 bit int */
817
818static int get_int64(QEMUFile *f, void *pv, size_t size)
819{
820 int64_t *v = pv;
821 qemu_get_sbe64s(f, v);
822 return 0;
823}
824
84e2e3eb 825static void put_int64(QEMUFile *f, void *pv, size_t size)
9ed7d6ae 826{
84e2e3eb 827 int64_t *v = pv;
9ed7d6ae
JQ
828 qemu_put_sbe64s(f, v);
829}
830
831const VMStateInfo vmstate_info_int64 = {
832 .name = "int64",
833 .get = get_int64,
834 .put = put_int64,
835};
836
837/* 8 bit unsigned int */
838
839static int get_uint8(QEMUFile *f, void *pv, size_t size)
840{
841 uint8_t *v = pv;
842 qemu_get_8s(f, v);
843 return 0;
844}
845
84e2e3eb 846static void put_uint8(QEMUFile *f, void *pv, size_t size)
9ed7d6ae 847{
84e2e3eb 848 uint8_t *v = pv;
9ed7d6ae
JQ
849 qemu_put_8s(f, v);
850}
851
852const VMStateInfo vmstate_info_uint8 = {
853 .name = "uint8",
854 .get = get_uint8,
855 .put = put_uint8,
856};
857
858/* 16 bit unsigned int */
859
860static int get_uint16(QEMUFile *f, void *pv, size_t size)
861{
862 uint16_t *v = pv;
863 qemu_get_be16s(f, v);
864 return 0;
865}
866
84e2e3eb 867static void put_uint16(QEMUFile *f, void *pv, size_t size)
9ed7d6ae 868{
84e2e3eb 869 uint16_t *v = pv;
9ed7d6ae
JQ
870 qemu_put_be16s(f, v);
871}
872
873const VMStateInfo vmstate_info_uint16 = {
874 .name = "uint16",
875 .get = get_uint16,
876 .put = put_uint16,
877};
878
879/* 32 bit unsigned int */
880
881static int get_uint32(QEMUFile *f, void *pv, size_t size)
882{
883 uint32_t *v = pv;
884 qemu_get_be32s(f, v);
885 return 0;
886}
887
84e2e3eb 888static void put_uint32(QEMUFile *f, void *pv, size_t size)
9ed7d6ae 889{
84e2e3eb 890 uint32_t *v = pv;
9ed7d6ae
JQ
891 qemu_put_be32s(f, v);
892}
893
894const VMStateInfo vmstate_info_uint32 = {
895 .name = "uint32",
896 .get = get_uint32,
897 .put = put_uint32,
898};
899
9122a8fe
JQ
900/* 32 bit uint. See that the received value is the same than the one
901 in the field */
902
903static int get_uint32_equal(QEMUFile *f, void *pv, size_t size)
904{
905 uint32_t *v = pv;
906 uint32_t v2;
907 qemu_get_be32s(f, &v2);
908
909 if (*v == v2) {
910 return 0;
911 }
912 return -EINVAL;
913}
914
915const VMStateInfo vmstate_info_uint32_equal = {
916 .name = "uint32 equal",
917 .get = get_uint32_equal,
918 .put = put_uint32,
919};
920
9ed7d6ae
JQ
921/* 64 bit unsigned int */
922
923static int get_uint64(QEMUFile *f, void *pv, size_t size)
924{
925 uint64_t *v = pv;
926 qemu_get_be64s(f, v);
927 return 0;
928}
929
84e2e3eb 930static void put_uint64(QEMUFile *f, void *pv, size_t size)
9ed7d6ae 931{
84e2e3eb 932 uint64_t *v = pv;
9ed7d6ae
JQ
933 qemu_put_be64s(f, v);
934}
935
936const VMStateInfo vmstate_info_uint64 = {
937 .name = "uint64",
938 .get = get_uint64,
939 .put = put_uint64,
940};
941
80cd83e7
JQ
942/* 8 bit int. See that the received value is the same than the one
943 in the field */
944
945static int get_uint8_equal(QEMUFile *f, void *pv, size_t size)
946{
947 uint8_t *v = pv;
948 uint8_t v2;
949 qemu_get_8s(f, &v2);
950
951 if (*v == v2)
952 return 0;
953 return -EINVAL;
954}
955
956const VMStateInfo vmstate_info_uint8_equal = {
aa1cce69 957 .name = "uint8 equal",
80cd83e7
JQ
958 .get = get_uint8_equal,
959 .put = put_uint8,
960};
961
dc3b83a0
JQ
962/* 16 bit unsigned int int. See that the received value is the same than the one
963 in the field */
964
965static int get_uint16_equal(QEMUFile *f, void *pv, size_t size)
966{
967 uint16_t *v = pv;
968 uint16_t v2;
969 qemu_get_be16s(f, &v2);
970
971 if (*v == v2)
972 return 0;
973 return -EINVAL;
974}
975
976const VMStateInfo vmstate_info_uint16_equal = {
977 .name = "uint16 equal",
978 .get = get_uint16_equal,
979 .put = put_uint16,
980};
981
dde0463b
JQ
982/* timers */
983
984static int get_timer(QEMUFile *f, void *pv, size_t size)
985{
986 QEMUTimer *v = pv;
987 qemu_get_timer(f, v);
988 return 0;
989}
990
84e2e3eb 991static void put_timer(QEMUFile *f, void *pv, size_t size)
dde0463b 992{
84e2e3eb 993 QEMUTimer *v = pv;
dde0463b
JQ
994 qemu_put_timer(f, v);
995}
996
997const VMStateInfo vmstate_info_timer = {
998 .name = "timer",
999 .get = get_timer,
1000 .put = put_timer,
1001};
1002
6f67c50f
JQ
1003/* uint8_t buffers */
1004
1005static int get_buffer(QEMUFile *f, void *pv, size_t size)
1006{
1007 uint8_t *v = pv;
1008 qemu_get_buffer(f, v, size);
1009 return 0;
1010}
1011
84e2e3eb 1012static void put_buffer(QEMUFile *f, void *pv, size_t size)
6f67c50f 1013{
84e2e3eb 1014 uint8_t *v = pv;
6f67c50f
JQ
1015 qemu_put_buffer(f, v, size);
1016}
1017
1018const VMStateInfo vmstate_info_buffer = {
1019 .name = "buffer",
1020 .get = get_buffer,
1021 .put = put_buffer,
1022};
1023
76507c75 1024/* unused buffers: space that was used for some fields that are
61cc8701 1025 not useful anymore */
76507c75
JQ
1026
1027static int get_unused_buffer(QEMUFile *f, void *pv, size_t size)
1028{
21174c34
JK
1029 uint8_t buf[1024];
1030 int block_len;
1031
1032 while (size > 0) {
1033 block_len = MIN(sizeof(buf), size);
1034 size -= block_len;
1035 qemu_get_buffer(f, buf, block_len);
1036 }
1037 return 0;
76507c75
JQ
1038}
1039
1040static void put_unused_buffer(QEMUFile *f, void *pv, size_t size)
1041{
21174c34
JK
1042 static const uint8_t buf[1024];
1043 int block_len;
1044
1045 while (size > 0) {
1046 block_len = MIN(sizeof(buf), size);
1047 size -= block_len;
1048 qemu_put_buffer(f, buf, block_len);
1049 }
76507c75
JQ
1050}
1051
1052const VMStateInfo vmstate_info_unused_buffer = {
1053 .name = "unused_buffer",
1054 .get = get_unused_buffer,
1055 .put = put_unused_buffer,
1056};
1057
7685ee6a
AW
1058typedef struct CompatEntry {
1059 char idstr[256];
1060 int instance_id;
1061} CompatEntry;
1062
a672b469 1063typedef struct SaveStateEntry {
72cf2d4f 1064 QTAILQ_ENTRY(SaveStateEntry) entry;
a672b469
AL
1065 char idstr[256];
1066 int instance_id;
4d2ffa08 1067 int alias_id;
a672b469
AL
1068 int version_id;
1069 int section_id;
c163b5ca 1070 SaveSetParamsHandler *set_params;
a672b469
AL
1071 SaveLiveStateHandler *save_live_state;
1072 SaveStateHandler *save_state;
1073 LoadStateHandler *load_state;
9ed7d6ae 1074 const VMStateDescription *vmsd;
a672b469 1075 void *opaque;
7685ee6a 1076 CompatEntry *compat;
24312968 1077 int no_migrate;
a672b469
AL
1078} SaveStateEntry;
1079
c163b5ca 1080
72cf2d4f
BS
1081static QTAILQ_HEAD(savevm_handlers, SaveStateEntry) savevm_handlers =
1082 QTAILQ_HEAD_INITIALIZER(savevm_handlers);
9ed7d6ae 1083static int global_section_id;
a672b469 1084
8718e999
JQ
1085static int calculate_new_instance_id(const char *idstr)
1086{
1087 SaveStateEntry *se;
1088 int instance_id = 0;
1089
72cf2d4f 1090 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
8718e999
JQ
1091 if (strcmp(idstr, se->idstr) == 0
1092 && instance_id <= se->instance_id) {
1093 instance_id = se->instance_id + 1;
1094 }
1095 }
1096 return instance_id;
1097}
1098
7685ee6a
AW
1099static int calculate_compat_instance_id(const char *idstr)
1100{
1101 SaveStateEntry *se;
1102 int instance_id = 0;
1103
1104 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1105 if (!se->compat)
1106 continue;
1107
1108 if (strcmp(idstr, se->compat->idstr) == 0
1109 && instance_id <= se->compat->instance_id) {
1110 instance_id = se->compat->instance_id + 1;
1111 }
1112 }
1113 return instance_id;
1114}
1115
a672b469
AL
1116/* TODO: Individual devices generally have very little idea about the rest
1117 of the system, so instance_id should be removed/replaced.
1118 Meanwhile pass -1 as instance_id if you do not already have a clearly
1119 distinguishing id for all instances of your device class. */
0be71e32
AW
1120int register_savevm_live(DeviceState *dev,
1121 const char *idstr,
a672b469
AL
1122 int instance_id,
1123 int version_id,
c163b5ca 1124 SaveSetParamsHandler *set_params,
a672b469
AL
1125 SaveLiveStateHandler *save_live_state,
1126 SaveStateHandler *save_state,
1127 LoadStateHandler *load_state,
1128 void *opaque)
1129{
8718e999 1130 SaveStateEntry *se;
a672b469 1131
7267c094 1132 se = g_malloc0(sizeof(SaveStateEntry));
a672b469
AL
1133 se->version_id = version_id;
1134 se->section_id = global_section_id++;
c163b5ca 1135 se->set_params = set_params;
a672b469
AL
1136 se->save_live_state = save_live_state;
1137 se->save_state = save_state;
1138 se->load_state = load_state;
1139 se->opaque = opaque;
9ed7d6ae 1140 se->vmsd = NULL;
24312968 1141 se->no_migrate = 0;
a672b469 1142
7685ee6a
AW
1143 if (dev && dev->parent_bus && dev->parent_bus->info->get_dev_path) {
1144 char *id = dev->parent_bus->info->get_dev_path(dev);
1145 if (id) {
1146 pstrcpy(se->idstr, sizeof(se->idstr), id);
1147 pstrcat(se->idstr, sizeof(se->idstr), "/");
7267c094 1148 g_free(id);
7685ee6a 1149
7267c094 1150 se->compat = g_malloc0(sizeof(CompatEntry));
7685ee6a
AW
1151 pstrcpy(se->compat->idstr, sizeof(se->compat->idstr), idstr);
1152 se->compat->instance_id = instance_id == -1 ?
1153 calculate_compat_instance_id(idstr) : instance_id;
1154 instance_id = -1;
1155 }
1156 }
1157 pstrcat(se->idstr, sizeof(se->idstr), idstr);
1158
8718e999 1159 if (instance_id == -1) {
7685ee6a 1160 se->instance_id = calculate_new_instance_id(se->idstr);
8718e999
JQ
1161 } else {
1162 se->instance_id = instance_id;
a672b469 1163 }
7685ee6a 1164 assert(!se->compat || se->instance_id == 0);
8718e999 1165 /* add at the end of list */
72cf2d4f 1166 QTAILQ_INSERT_TAIL(&savevm_handlers, se, entry);
a672b469
AL
1167 return 0;
1168}
1169
0be71e32
AW
1170int register_savevm(DeviceState *dev,
1171 const char *idstr,
a672b469
AL
1172 int instance_id,
1173 int version_id,
1174 SaveStateHandler *save_state,
1175 LoadStateHandler *load_state,
1176 void *opaque)
1177{
0be71e32 1178 return register_savevm_live(dev, idstr, instance_id, version_id,
c163b5ca 1179 NULL, NULL, save_state, load_state, opaque);
a672b469
AL
1180}
1181
0be71e32 1182void unregister_savevm(DeviceState *dev, const char *idstr, void *opaque)
41bd13af 1183{
8718e999 1184 SaveStateEntry *se, *new_se;
7685ee6a
AW
1185 char id[256] = "";
1186
1187 if (dev && dev->parent_bus && dev->parent_bus->info->get_dev_path) {
1188 char *path = dev->parent_bus->info->get_dev_path(dev);
1189 if (path) {
1190 pstrcpy(id, sizeof(id), path);
1191 pstrcat(id, sizeof(id), "/");
7267c094 1192 g_free(path);
7685ee6a
AW
1193 }
1194 }
1195 pstrcat(id, sizeof(id), idstr);
41bd13af 1196
72cf2d4f 1197 QTAILQ_FOREACH_SAFE(se, &savevm_handlers, entry, new_se) {
7685ee6a 1198 if (strcmp(se->idstr, id) == 0 && se->opaque == opaque) {
72cf2d4f 1199 QTAILQ_REMOVE(&savevm_handlers, se, entry);
69e58af9 1200 if (se->compat) {
7267c094 1201 g_free(se->compat);
69e58af9 1202 }
7267c094 1203 g_free(se);
41bd13af 1204 }
41bd13af
AL
1205 }
1206}
1207
24312968
CM
1208/* mark a device as not to be migrated, that is the device should be
1209 unplugged before migration */
1210void register_device_unmigratable(DeviceState *dev, const char *idstr,
1211 void *opaque)
1212{
1213 SaveStateEntry *se;
1214 char id[256] = "";
1215
1216 if (dev && dev->parent_bus && dev->parent_bus->info->get_dev_path) {
1217 char *path = dev->parent_bus->info->get_dev_path(dev);
1218 if (path) {
1219 pstrcpy(id, sizeof(id), path);
1220 pstrcat(id, sizeof(id), "/");
7267c094 1221 g_free(path);
24312968
CM
1222 }
1223 }
1224 pstrcat(id, sizeof(id), idstr);
1225
1226 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1227 if (strcmp(se->idstr, id) == 0 && se->opaque == opaque) {
1228 se->no_migrate = 1;
1229 }
1230 }
1231}
1232
0be71e32 1233int vmstate_register_with_alias_id(DeviceState *dev, int instance_id,
4d2ffa08
JK
1234 const VMStateDescription *vmsd,
1235 void *opaque, int alias_id,
1236 int required_for_version)
9ed7d6ae 1237{
8718e999 1238 SaveStateEntry *se;
9ed7d6ae 1239
4d2ffa08
JK
1240 /* If this triggers, alias support can be dropped for the vmsd. */
1241 assert(alias_id == -1 || required_for_version >= vmsd->minimum_version_id);
1242
7267c094 1243 se = g_malloc0(sizeof(SaveStateEntry));
9ed7d6ae
JQ
1244 se->version_id = vmsd->version_id;
1245 se->section_id = global_section_id++;
1246 se->save_live_state = NULL;
1247 se->save_state = NULL;
1248 se->load_state = NULL;
1249 se->opaque = opaque;
1250 se->vmsd = vmsd;
4d2ffa08 1251 se->alias_id = alias_id;
2837c8ea 1252 se->no_migrate = vmsd->unmigratable;
9ed7d6ae 1253
7685ee6a
AW
1254 if (dev && dev->parent_bus && dev->parent_bus->info->get_dev_path) {
1255 char *id = dev->parent_bus->info->get_dev_path(dev);
1256 if (id) {
1257 pstrcpy(se->idstr, sizeof(se->idstr), id);
1258 pstrcat(se->idstr, sizeof(se->idstr), "/");
7267c094 1259 g_free(id);
7685ee6a 1260
7267c094 1261 se->compat = g_malloc0(sizeof(CompatEntry));
7685ee6a
AW
1262 pstrcpy(se->compat->idstr, sizeof(se->compat->idstr), vmsd->name);
1263 se->compat->instance_id = instance_id == -1 ?
1264 calculate_compat_instance_id(vmsd->name) : instance_id;
1265 instance_id = -1;
1266 }
1267 }
1268 pstrcat(se->idstr, sizeof(se->idstr), vmsd->name);
1269
8718e999 1270 if (instance_id == -1) {
7685ee6a 1271 se->instance_id = calculate_new_instance_id(se->idstr);
8718e999
JQ
1272 } else {
1273 se->instance_id = instance_id;
9ed7d6ae 1274 }
7685ee6a 1275 assert(!se->compat || se->instance_id == 0);
8718e999 1276 /* add at the end of list */
72cf2d4f 1277 QTAILQ_INSERT_TAIL(&savevm_handlers, se, entry);
9ed7d6ae
JQ
1278 return 0;
1279}
1280
0be71e32
AW
1281int vmstate_register(DeviceState *dev, int instance_id,
1282 const VMStateDescription *vmsd, void *opaque)
4d2ffa08 1283{
0be71e32
AW
1284 return vmstate_register_with_alias_id(dev, instance_id, vmsd,
1285 opaque, -1, 0);
4d2ffa08
JK
1286}
1287
0be71e32
AW
1288void vmstate_unregister(DeviceState *dev, const VMStateDescription *vmsd,
1289 void *opaque)
9ed7d6ae 1290{
1eb7538b
JQ
1291 SaveStateEntry *se, *new_se;
1292
72cf2d4f 1293 QTAILQ_FOREACH_SAFE(se, &savevm_handlers, entry, new_se) {
1eb7538b 1294 if (se->vmsd == vmsd && se->opaque == opaque) {
72cf2d4f 1295 QTAILQ_REMOVE(&savevm_handlers, se, entry);
69e58af9 1296 if (se->compat) {
7267c094 1297 g_free(se->compat);
69e58af9 1298 }
7267c094 1299 g_free(se);
1eb7538b
JQ
1300 }
1301 }
9ed7d6ae
JQ
1302}
1303
811814bd
JQ
1304static void vmstate_subsection_save(QEMUFile *f, const VMStateDescription *vmsd,
1305 void *opaque);
1306static int vmstate_subsection_load(QEMUFile *f, const VMStateDescription *vmsd,
1307 void *opaque);
1308
9ed7d6ae
JQ
1309int vmstate_load_state(QEMUFile *f, const VMStateDescription *vmsd,
1310 void *opaque, int version_id)
1311{
1312 VMStateField *field = vmsd->fields;
811814bd 1313 int ret;
9ed7d6ae
JQ
1314
1315 if (version_id > vmsd->version_id) {
1316 return -EINVAL;
1317 }
1318 if (version_id < vmsd->minimum_version_id_old) {
1319 return -EINVAL;
1320 }
1321 if (version_id < vmsd->minimum_version_id) {
1322 return vmsd->load_state_old(f, opaque, version_id);
1323 }
fd4d52de
JQ
1324 if (vmsd->pre_load) {
1325 int ret = vmsd->pre_load(opaque);
1326 if (ret)
1327 return ret;
1328 }
9ed7d6ae 1329 while(field->name) {
f11f6a5f
JQ
1330 if ((field->field_exists &&
1331 field->field_exists(opaque, version_id)) ||
1332 (!field->field_exists &&
1333 field->version_id <= version_id)) {
f752a6aa 1334 void *base_addr = opaque + field->offset;
811814bd 1335 int i, n_elems = 1;
e61a1e0a 1336 int size = field->size;
9ed7d6ae 1337
e61a1e0a
JQ
1338 if (field->flags & VMS_VBUFFER) {
1339 size = *(int32_t *)(opaque+field->size_offset);
33599e2a
JQ
1340 if (field->flags & VMS_MULTIPLY) {
1341 size *= field->size;
1342 }
e61a1e0a 1343 }
f752a6aa
JQ
1344 if (field->flags & VMS_ARRAY) {
1345 n_elems = field->num;
d6698281
JQ
1346 } else if (field->flags & VMS_VARRAY_INT32) {
1347 n_elems = *(int32_t *)(opaque+field->num_offset);
a624b086
JQ
1348 } else if (field->flags & VMS_VARRAY_UINT32) {
1349 n_elems = *(uint32_t *)(opaque+field->num_offset);
bdb4941d
JQ
1350 } else if (field->flags & VMS_VARRAY_UINT16) {
1351 n_elems = *(uint16_t *)(opaque+field->num_offset);
82fa39b7
JQ
1352 } else if (field->flags & VMS_VARRAY_UINT8) {
1353 n_elems = *(uint8_t *)(opaque+field->num_offset);
f752a6aa 1354 }
dde0463b 1355 if (field->flags & VMS_POINTER) {
e61a1e0a 1356 base_addr = *(void **)base_addr + field->start;
dde0463b 1357 }
f752a6aa 1358 for (i = 0; i < n_elems; i++) {
e61a1e0a 1359 void *addr = base_addr + size * i;
ec245e21 1360
19df438b
JQ
1361 if (field->flags & VMS_ARRAY_OF_POINTER) {
1362 addr = *(void **)addr;
1363 }
ec245e21 1364 if (field->flags & VMS_STRUCT) {
fa3aad24 1365 ret = vmstate_load_state(f, field->vmsd, addr, field->vmsd->version_id);
ec245e21 1366 } else {
e61a1e0a 1367 ret = field->info->get(f, addr, size);
ec245e21
JQ
1368
1369 }
f752a6aa
JQ
1370 if (ret < 0) {
1371 return ret;
1372 }
9ed7d6ae
JQ
1373 }
1374 }
1375 field++;
1376 }
811814bd
JQ
1377 ret = vmstate_subsection_load(f, vmsd, opaque);
1378 if (ret != 0) {
1379 return ret;
1380 }
752ff2fa 1381 if (vmsd->post_load) {
e59fb374 1382 return vmsd->post_load(opaque, version_id);
752ff2fa 1383 }
9ed7d6ae
JQ
1384 return 0;
1385}
1386
1387void vmstate_save_state(QEMUFile *f, const VMStateDescription *vmsd,
84e2e3eb 1388 void *opaque)
9ed7d6ae
JQ
1389{
1390 VMStateField *field = vmsd->fields;
1391
8fb0791d
JQ
1392 if (vmsd->pre_save) {
1393 vmsd->pre_save(opaque);
1394 }
9ed7d6ae 1395 while(field->name) {
f11f6a5f
JQ
1396 if (!field->field_exists ||
1397 field->field_exists(opaque, vmsd->version_id)) {
1398 void *base_addr = opaque + field->offset;
1399 int i, n_elems = 1;
e61a1e0a 1400 int size = field->size;
dde0463b 1401
e61a1e0a
JQ
1402 if (field->flags & VMS_VBUFFER) {
1403 size = *(int32_t *)(opaque+field->size_offset);
33599e2a
JQ
1404 if (field->flags & VMS_MULTIPLY) {
1405 size *= field->size;
1406 }
e61a1e0a 1407 }
f11f6a5f
JQ
1408 if (field->flags & VMS_ARRAY) {
1409 n_elems = field->num;
d6698281
JQ
1410 } else if (field->flags & VMS_VARRAY_INT32) {
1411 n_elems = *(int32_t *)(opaque+field->num_offset);
bdb4941d
JQ
1412 } else if (field->flags & VMS_VARRAY_UINT16) {
1413 n_elems = *(uint16_t *)(opaque+field->num_offset);
b784421c
JQ
1414 } else if (field->flags & VMS_VARRAY_UINT8) {
1415 n_elems = *(uint8_t *)(opaque+field->num_offset);
f11f6a5f
JQ
1416 }
1417 if (field->flags & VMS_POINTER) {
e61a1e0a 1418 base_addr = *(void **)base_addr + field->start;
f11f6a5f
JQ
1419 }
1420 for (i = 0; i < n_elems; i++) {
e61a1e0a 1421 void *addr = base_addr + size * i;
ec245e21 1422
8595387e
JQ
1423 if (field->flags & VMS_ARRAY_OF_POINTER) {
1424 addr = *(void **)addr;
1425 }
f11f6a5f
JQ
1426 if (field->flags & VMS_STRUCT) {
1427 vmstate_save_state(f, field->vmsd, addr);
1428 } else {
e61a1e0a 1429 field->info->put(f, addr, size);
f11f6a5f 1430 }
ec245e21 1431 }
dde0463b 1432 }
9ed7d6ae
JQ
1433 field++;
1434 }
811814bd 1435 vmstate_subsection_save(f, vmsd, opaque);
9ed7d6ae
JQ
1436}
1437
4082be4d
JQ
1438static int vmstate_load(QEMUFile *f, SaveStateEntry *se, int version_id)
1439{
9ed7d6ae
JQ
1440 if (!se->vmsd) { /* Old style */
1441 return se->load_state(f, se->opaque, version_id);
1442 }
1443 return vmstate_load_state(f, se->vmsd, se->opaque, version_id);
4082be4d
JQ
1444}
1445
dc912121 1446static void vmstate_save(QEMUFile *f, SaveStateEntry *se)
4082be4d 1447{
9ed7d6ae
JQ
1448 if (!se->vmsd) { /* Old style */
1449 se->save_state(f, se->opaque);
dc912121 1450 return;
9ed7d6ae
JQ
1451 }
1452 vmstate_save_state(f,se->vmsd, se->opaque);
4082be4d
JQ
1453}
1454
a672b469
AL
1455#define QEMU_VM_FILE_MAGIC 0x5145564d
1456#define QEMU_VM_FILE_VERSION_COMPAT 0x00000002
1457#define QEMU_VM_FILE_VERSION 0x00000003
1458
1459#define QEMU_VM_EOF 0x00
1460#define QEMU_VM_SECTION_START 0x01
1461#define QEMU_VM_SECTION_PART 0x02
1462#define QEMU_VM_SECTION_END 0x03
1463#define QEMU_VM_SECTION_FULL 0x04
811814bd 1464#define QEMU_VM_SUBSECTION 0x05
a672b469 1465
dc912121
AW
1466bool qemu_savevm_state_blocked(Monitor *mon)
1467{
1468 SaveStateEntry *se;
1469
1470 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1471 if (se->no_migrate) {
1472 monitor_printf(mon, "state blocked by non-migratable device '%s'\n",
1473 se->idstr);
1474 return true;
1475 }
1476 }
1477 return false;
1478}
1479
f327aa0c
JK
1480int qemu_savevm_state_begin(Monitor *mon, QEMUFile *f, int blk_enable,
1481 int shared)
a672b469
AL
1482{
1483 SaveStateEntry *se;
1484
c163b5ca
LS
1485 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1486 if(se->set_params == NULL) {
1487 continue;
1488 }
1489 se->set_params(blk_enable, shared, se->opaque);
1490 }
1491
a672b469
AL
1492 qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
1493 qemu_put_be32(f, QEMU_VM_FILE_VERSION);
1494
72cf2d4f 1495 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
a672b469
AL
1496 int len;
1497
1498 if (se->save_live_state == NULL)
1499 continue;
1500
1501 /* Section type */
1502 qemu_put_byte(f, QEMU_VM_SECTION_START);
1503 qemu_put_be32(f, se->section_id);
1504
1505 /* ID string */
1506 len = strlen(se->idstr);
1507 qemu_put_byte(f, len);
1508 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
1509
1510 qemu_put_be32(f, se->instance_id);
1511 qemu_put_be32(f, se->version_id);
1512
f327aa0c 1513 se->save_live_state(mon, f, QEMU_VM_SECTION_START, se->opaque);
a672b469
AL
1514 }
1515
4ec7fcc7 1516 if (qemu_file_has_error(f)) {
f327aa0c 1517 qemu_savevm_state_cancel(mon, f);
a672b469 1518 return -EIO;
4ec7fcc7 1519 }
a672b469
AL
1520
1521 return 0;
1522}
1523
f327aa0c 1524int qemu_savevm_state_iterate(Monitor *mon, QEMUFile *f)
a672b469
AL
1525{
1526 SaveStateEntry *se;
1527 int ret = 1;
1528
72cf2d4f 1529 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
a672b469
AL
1530 if (se->save_live_state == NULL)
1531 continue;
1532
1533 /* Section type */
1534 qemu_put_byte(f, QEMU_VM_SECTION_PART);
1535 qemu_put_be32(f, se->section_id);
1536
90697be8
JK
1537 ret = se->save_live_state(mon, f, QEMU_VM_SECTION_PART, se->opaque);
1538 if (!ret) {
1539 /* Do not proceed to the next vmstate before this one reported
1540 completion of the current stage. This serializes the migration
1541 and reduces the probability that a faster changing state is
1542 synchronized over and over again. */
1543 break;
1544 }
a672b469
AL
1545 }
1546
1547 if (ret)
1548 return 1;
1549
4ec7fcc7 1550 if (qemu_file_has_error(f)) {
f327aa0c 1551 qemu_savevm_state_cancel(mon, f);
a672b469 1552 return -EIO;
4ec7fcc7 1553 }
a672b469
AL
1554
1555 return 0;
1556}
1557
f327aa0c 1558int qemu_savevm_state_complete(Monitor *mon, QEMUFile *f)
a672b469
AL
1559{
1560 SaveStateEntry *se;
1561
ea375f9a
JK
1562 cpu_synchronize_all_states();
1563
72cf2d4f 1564 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
a672b469
AL
1565 if (se->save_live_state == NULL)
1566 continue;
1567
1568 /* Section type */
1569 qemu_put_byte(f, QEMU_VM_SECTION_END);
1570 qemu_put_be32(f, se->section_id);
1571
f327aa0c 1572 se->save_live_state(mon, f, QEMU_VM_SECTION_END, se->opaque);
a672b469
AL
1573 }
1574
72cf2d4f 1575 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
a672b469
AL
1576 int len;
1577
9ed7d6ae 1578 if (se->save_state == NULL && se->vmsd == NULL)
a672b469
AL
1579 continue;
1580
1581 /* Section type */
1582 qemu_put_byte(f, QEMU_VM_SECTION_FULL);
1583 qemu_put_be32(f, se->section_id);
1584
1585 /* ID string */
1586 len = strlen(se->idstr);
1587 qemu_put_byte(f, len);
1588 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
1589
1590 qemu_put_be32(f, se->instance_id);
1591 qemu_put_be32(f, se->version_id);
1592
dc912121 1593 vmstate_save(f, se);
a672b469
AL
1594 }
1595
1596 qemu_put_byte(f, QEMU_VM_EOF);
1597
1598 if (qemu_file_has_error(f))
1599 return -EIO;
1600
1601 return 0;
1602}
1603
f327aa0c 1604void qemu_savevm_state_cancel(Monitor *mon, QEMUFile *f)
4ec7fcc7
JK
1605{
1606 SaveStateEntry *se;
1607
1608 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1609 if (se->save_live_state) {
f327aa0c 1610 se->save_live_state(mon, f, -1, se->opaque);
4ec7fcc7
JK
1611 }
1612 }
1613}
1614
f327aa0c 1615static int qemu_savevm_state(Monitor *mon, QEMUFile *f)
a672b469
AL
1616{
1617 int saved_vm_running;
1618 int ret;
1619
1354869c 1620 saved_vm_running = runstate_is_running();
0461d5a6 1621 vm_stop(RUN_STATE_SAVE_VM);
a672b469 1622
dc912121
AW
1623 if (qemu_savevm_state_blocked(mon)) {
1624 ret = -EINVAL;
1625 goto out;
1626 }
1627
f327aa0c 1628 ret = qemu_savevm_state_begin(mon, f, 0, 0);
a672b469
AL
1629 if (ret < 0)
1630 goto out;
1631
1632 do {
f327aa0c 1633 ret = qemu_savevm_state_iterate(mon, f);
a672b469
AL
1634 if (ret < 0)
1635 goto out;
1636 } while (ret == 0);
1637
f327aa0c 1638 ret = qemu_savevm_state_complete(mon, f);
a672b469
AL
1639
1640out:
1641 if (qemu_file_has_error(f))
1642 ret = -EIO;
1643
1644 if (!ret && saved_vm_running)
1645 vm_start();
1646
1647 return ret;
1648}
1649
1650static SaveStateEntry *find_se(const char *idstr, int instance_id)
1651{
1652 SaveStateEntry *se;
1653
72cf2d4f 1654 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
a672b469 1655 if (!strcmp(se->idstr, idstr) &&
4d2ffa08
JK
1656 (instance_id == se->instance_id ||
1657 instance_id == se->alias_id))
a672b469 1658 return se;
7685ee6a
AW
1659 /* Migrating from an older version? */
1660 if (strstr(se->idstr, idstr) && se->compat) {
1661 if (!strcmp(se->compat->idstr, idstr) &&
1662 (instance_id == se->compat->instance_id ||
1663 instance_id == se->alias_id))
1664 return se;
1665 }
a672b469
AL
1666 }
1667 return NULL;
1668}
1669
811814bd
JQ
1670static const VMStateDescription *vmstate_get_subsection(const VMStateSubsection *sub, char *idstr)
1671{
1672 while(sub && sub->needed) {
1673 if (strcmp(idstr, sub->vmsd->name) == 0) {
1674 return sub->vmsd;
1675 }
1676 sub++;
1677 }
1678 return NULL;
1679}
1680
1681static int vmstate_subsection_load(QEMUFile *f, const VMStateDescription *vmsd,
1682 void *opaque)
1683{
eb60260d
YT
1684 const VMStateSubsection *sub = vmsd->subsections;
1685
1686 if (!sub || !sub->needed) {
1687 return 0;
1688 }
1689
811814bd
JQ
1690 while (qemu_peek_byte(f) == QEMU_VM_SUBSECTION) {
1691 char idstr[256];
1692 int ret;
49a2942d 1693 uint8_t version_id, len;
811814bd
JQ
1694 const VMStateDescription *sub_vmsd;
1695
49a2942d 1696 qemu_get_byte(f); /* subsection */
811814bd
JQ
1697 len = qemu_get_byte(f);
1698 qemu_get_buffer(f, (uint8_t *)idstr, len);
1699 idstr[len] = 0;
1700 version_id = qemu_get_be32(f);
1701
eb60260d 1702 sub_vmsd = vmstate_get_subsection(sub, idstr);
811814bd
JQ
1703 if (sub_vmsd == NULL) {
1704 return -ENOENT;
1705 }
eb60260d 1706 assert(!sub_vmsd->subsections);
811814bd
JQ
1707 ret = vmstate_load_state(f, sub_vmsd, opaque, version_id);
1708 if (ret) {
1709 return ret;
1710 }
1711 }
1712 return 0;
1713}
1714
1715static void vmstate_subsection_save(QEMUFile *f, const VMStateDescription *vmsd,
1716 void *opaque)
1717{
1718 const VMStateSubsection *sub = vmsd->subsections;
1719
1720 while (sub && sub->needed) {
1721 if (sub->needed(opaque)) {
1722 const VMStateDescription *vmsd = sub->vmsd;
1723 uint8_t len;
1724
1725 qemu_put_byte(f, QEMU_VM_SUBSECTION);
1726 len = strlen(vmsd->name);
1727 qemu_put_byte(f, len);
1728 qemu_put_buffer(f, (uint8_t *)vmsd->name, len);
1729 qemu_put_be32(f, vmsd->version_id);
eb60260d 1730 assert(!vmsd->subsections);
811814bd
JQ
1731 vmstate_save_state(f, vmsd, opaque);
1732 }
1733 sub++;
1734 }
1735}
1736
a672b469 1737typedef struct LoadStateEntry {
72cf2d4f 1738 QLIST_ENTRY(LoadStateEntry) entry;
a672b469
AL
1739 SaveStateEntry *se;
1740 int section_id;
1741 int version_id;
a672b469
AL
1742} LoadStateEntry;
1743
a672b469
AL
1744int qemu_loadvm_state(QEMUFile *f)
1745{
72cf2d4f
BS
1746 QLIST_HEAD(, LoadStateEntry) loadvm_handlers =
1747 QLIST_HEAD_INITIALIZER(loadvm_handlers);
f4dbb8dd 1748 LoadStateEntry *le, *new_le;
a672b469
AL
1749 uint8_t section_type;
1750 unsigned int v;
1751 int ret;
1752
dc912121
AW
1753 if (qemu_savevm_state_blocked(default_mon)) {
1754 return -EINVAL;
1755 }
1756
a672b469
AL
1757 v = qemu_get_be32(f);
1758 if (v != QEMU_VM_FILE_MAGIC)
1759 return -EINVAL;
1760
1761 v = qemu_get_be32(f);
bbfe1408
JQ
1762 if (v == QEMU_VM_FILE_VERSION_COMPAT) {
1763 fprintf(stderr, "SaveVM v2 format is obsolete and don't work anymore\n");
1764 return -ENOTSUP;
1765 }
a672b469
AL
1766 if (v != QEMU_VM_FILE_VERSION)
1767 return -ENOTSUP;
1768
1769 while ((section_type = qemu_get_byte(f)) != QEMU_VM_EOF) {
1770 uint32_t instance_id, version_id, section_id;
a672b469
AL
1771 SaveStateEntry *se;
1772 char idstr[257];
1773 int len;
1774
1775 switch (section_type) {
1776 case QEMU_VM_SECTION_START:
1777 case QEMU_VM_SECTION_FULL:
1778 /* Read section start */
1779 section_id = qemu_get_be32(f);
1780 len = qemu_get_byte(f);
1781 qemu_get_buffer(f, (uint8_t *)idstr, len);
1782 idstr[len] = 0;
1783 instance_id = qemu_get_be32(f);
1784 version_id = qemu_get_be32(f);
1785
1786 /* Find savevm section */
1787 se = find_se(idstr, instance_id);
1788 if (se == NULL) {
1789 fprintf(stderr, "Unknown savevm section or instance '%s' %d\n", idstr, instance_id);
1790 ret = -EINVAL;
1791 goto out;
1792 }
1793
1794 /* Validate version */
1795 if (version_id > se->version_id) {
1796 fprintf(stderr, "savevm: unsupported version %d for '%s' v%d\n",
1797 version_id, idstr, se->version_id);
1798 ret = -EINVAL;
1799 goto out;
1800 }
1801
1802 /* Add entry */
7267c094 1803 le = g_malloc0(sizeof(*le));
a672b469
AL
1804
1805 le->se = se;
1806 le->section_id = section_id;
1807 le->version_id = version_id;
72cf2d4f 1808 QLIST_INSERT_HEAD(&loadvm_handlers, le, entry);
a672b469 1809
4082be4d 1810 ret = vmstate_load(f, le->se, le->version_id);
b5a22e4a
JQ
1811 if (ret < 0) {
1812 fprintf(stderr, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
1813 instance_id, idstr);
1814 goto out;
1815 }
a672b469
AL
1816 break;
1817 case QEMU_VM_SECTION_PART:
1818 case QEMU_VM_SECTION_END:
1819 section_id = qemu_get_be32(f);
1820
72cf2d4f 1821 QLIST_FOREACH(le, &loadvm_handlers, entry) {
f4dbb8dd
JQ
1822 if (le->section_id == section_id) {
1823 break;
1824 }
1825 }
a672b469
AL
1826 if (le == NULL) {
1827 fprintf(stderr, "Unknown savevm section %d\n", section_id);
1828 ret = -EINVAL;
1829 goto out;
1830 }
1831
4082be4d 1832 ret = vmstate_load(f, le->se, le->version_id);
b5a22e4a
JQ
1833 if (ret < 0) {
1834 fprintf(stderr, "qemu: warning: error while loading state section id %d\n",
1835 section_id);
1836 goto out;
1837 }
a672b469
AL
1838 break;
1839 default:
1840 fprintf(stderr, "Unknown savevm section type %d\n", section_type);
1841 ret = -EINVAL;
1842 goto out;
1843 }
1844 }
1845
ea375f9a
JK
1846 cpu_synchronize_all_post_init();
1847
a672b469
AL
1848 ret = 0;
1849
1850out:
72cf2d4f
BS
1851 QLIST_FOREACH_SAFE(le, &loadvm_handlers, entry, new_le) {
1852 QLIST_REMOVE(le, entry);
7267c094 1853 g_free(le);
a672b469
AL
1854 }
1855
1856 if (qemu_file_has_error(f))
1857 ret = -EIO;
1858
1859 return ret;
1860}
1861
a672b469
AL
1862static int bdrv_snapshot_find(BlockDriverState *bs, QEMUSnapshotInfo *sn_info,
1863 const char *name)
1864{
1865 QEMUSnapshotInfo *sn_tab, *sn;
1866 int nb_sns, i, ret;
1867
1868 ret = -ENOENT;
1869 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
1870 if (nb_sns < 0)
1871 return ret;
1872 for(i = 0; i < nb_sns; i++) {
1873 sn = &sn_tab[i];
1874 if (!strcmp(sn->id_str, name) || !strcmp(sn->name, name)) {
1875 *sn_info = *sn;
1876 ret = 0;
1877 break;
1878 }
1879 }
7267c094 1880 g_free(sn_tab);
a672b469
AL
1881 return ret;
1882}
1883
cb499fb2
KW
1884/*
1885 * Deletes snapshots of a given name in all opened images.
1886 */
1887static int del_existing_snapshots(Monitor *mon, const char *name)
1888{
1889 BlockDriverState *bs;
cb499fb2
KW
1890 QEMUSnapshotInfo sn1, *snapshot = &sn1;
1891 int ret;
1892
dbc13590
MA
1893 bs = NULL;
1894 while ((bs = bdrv_next(bs))) {
cb499fb2
KW
1895 if (bdrv_can_snapshot(bs) &&
1896 bdrv_snapshot_find(bs, snapshot, name) >= 0)
1897 {
1898 ret = bdrv_snapshot_delete(bs, name);
1899 if (ret < 0) {
1900 monitor_printf(mon,
1901 "Error while deleting snapshot on '%s'\n",
1902 bdrv_get_device_name(bs));
1903 return -1;
1904 }
1905 }
1906 }
1907
1908 return 0;
1909}
1910
d54908a5 1911void do_savevm(Monitor *mon, const QDict *qdict)
a672b469
AL
1912{
1913 BlockDriverState *bs, *bs1;
1914 QEMUSnapshotInfo sn1, *sn = &sn1, old_sn1, *old_sn = &old_sn1;
cb499fb2 1915 int ret;
a672b469
AL
1916 QEMUFile *f;
1917 int saved_vm_running;
2d22b18f 1918 uint32_t vm_state_size;
a672b469
AL
1919#ifdef _WIN32
1920 struct _timeb tb;
7d631a11 1921 struct tm *ptm;
a672b469
AL
1922#else
1923 struct timeval tv;
7d631a11 1924 struct tm tm;
a672b469 1925#endif
d54908a5 1926 const char *name = qdict_get_try_str(qdict, "name");
a672b469 1927
feeee5ac 1928 /* Verify if there is a device that doesn't support snapshots and is writable */
dbc13590
MA
1929 bs = NULL;
1930 while ((bs = bdrv_next(bs))) {
feeee5ac 1931
07b70bfb 1932 if (!bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {
feeee5ac
MDCF
1933 continue;
1934 }
1935
1936 if (!bdrv_can_snapshot(bs)) {
1937 monitor_printf(mon, "Device '%s' is writable but does not support snapshots.\n",
1938 bdrv_get_device_name(bs));
1939 return;
1940 }
1941 }
1942
f9092b10 1943 bs = bdrv_snapshots();
a672b469 1944 if (!bs) {
376253ec 1945 monitor_printf(mon, "No block device can accept snapshots\n");
a672b469
AL
1946 return;
1947 }
a672b469 1948
1354869c 1949 saved_vm_running = runstate_is_running();
0461d5a6 1950 vm_stop(RUN_STATE_SAVE_VM);
a672b469 1951
cb499fb2 1952 memset(sn, 0, sizeof(*sn));
a672b469
AL
1953
1954 /* fill auxiliary fields */
1955#ifdef _WIN32
1956 _ftime(&tb);
1957 sn->date_sec = tb.time;
1958 sn->date_nsec = tb.millitm * 1000000;
1959#else
1960 gettimeofday(&tv, NULL);
1961 sn->date_sec = tv.tv_sec;
1962 sn->date_nsec = tv.tv_usec * 1000;
1963#endif
74475455 1964 sn->vm_clock_nsec = qemu_get_clock_ns(vm_clock);
a672b469 1965
7d631a11
MDCF
1966 if (name) {
1967 ret = bdrv_snapshot_find(bs, old_sn, name);
1968 if (ret >= 0) {
1969 pstrcpy(sn->name, sizeof(sn->name), old_sn->name);
1970 pstrcpy(sn->id_str, sizeof(sn->id_str), old_sn->id_str);
1971 } else {
1972 pstrcpy(sn->name, sizeof(sn->name), name);
1973 }
1974 } else {
1975#ifdef _WIN32
1976 ptm = localtime(&tb.time);
1977 strftime(sn->name, sizeof(sn->name), "vm-%Y%m%d%H%M%S", ptm);
1978#else
d7d9b528
BS
1979 /* cast below needed for OpenBSD where tv_sec is still 'long' */
1980 localtime_r((const time_t *)&tv.tv_sec, &tm);
7d631a11
MDCF
1981 strftime(sn->name, sizeof(sn->name), "vm-%Y%m%d%H%M%S", &tm);
1982#endif
1983 }
1984
cb499fb2 1985 /* Delete old snapshots of the same name */
f139a412 1986 if (name && del_existing_snapshots(mon, name) < 0) {
cb499fb2
KW
1987 goto the_end;
1988 }
1989
a672b469 1990 /* save the VM state */
45566e9c 1991 f = qemu_fopen_bdrv(bs, 1);
a672b469 1992 if (!f) {
376253ec 1993 monitor_printf(mon, "Could not open VM state file\n");
a672b469
AL
1994 goto the_end;
1995 }
f327aa0c 1996 ret = qemu_savevm_state(mon, f);
2d22b18f 1997 vm_state_size = qemu_ftell(f);
a672b469
AL
1998 qemu_fclose(f);
1999 if (ret < 0) {
376253ec 2000 monitor_printf(mon, "Error %d while writing VM\n", ret);
a672b469
AL
2001 goto the_end;
2002 }
2003
2004 /* create the snapshots */
2005
dbc13590
MA
2006 bs1 = NULL;
2007 while ((bs1 = bdrv_next(bs1))) {
feeee5ac 2008 if (bdrv_can_snapshot(bs1)) {
2d22b18f
AL
2009 /* Write VM state size only to the image that contains the state */
2010 sn->vm_state_size = (bs == bs1 ? vm_state_size : 0);
a672b469
AL
2011 ret = bdrv_snapshot_create(bs1, sn);
2012 if (ret < 0) {
376253ec
AL
2013 monitor_printf(mon, "Error while creating snapshot on '%s'\n",
2014 bdrv_get_device_name(bs1));
a672b469
AL
2015 }
2016 }
2017 }
2018
2019 the_end:
2020 if (saved_vm_running)
2021 vm_start();
2022}
2023
03cd4655 2024int load_vmstate(const char *name)
a672b469 2025{
f0aa7a8b 2026 BlockDriverState *bs, *bs_vm_state;
2d22b18f 2027 QEMUSnapshotInfo sn;
a672b469 2028 QEMUFile *f;
751c6a17 2029 int ret;
a672b469 2030
f0aa7a8b
MDCF
2031 bs_vm_state = bdrv_snapshots();
2032 if (!bs_vm_state) {
2033 error_report("No block device supports snapshots");
2034 return -ENOTSUP;
2035 }
2036
2037 /* Don't even try to load empty VM states */
2038 ret = bdrv_snapshot_find(bs_vm_state, &sn, name);
2039 if (ret < 0) {
2040 return ret;
2041 } else if (sn.vm_state_size == 0) {
e11480db
KW
2042 error_report("This is a disk-only snapshot. Revert to it offline "
2043 "using qemu-img.");
f0aa7a8b
MDCF
2044 return -EINVAL;
2045 }
2046
2047 /* Verify if there is any device that doesn't support snapshots and is
2048 writable and check if the requested snapshot is available too. */
dbc13590
MA
2049 bs = NULL;
2050 while ((bs = bdrv_next(bs))) {
feeee5ac 2051
07b70bfb 2052 if (!bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {
feeee5ac
MDCF
2053 continue;
2054 }
2055
2056 if (!bdrv_can_snapshot(bs)) {
2057 error_report("Device '%s' is writable but does not support snapshots.",
2058 bdrv_get_device_name(bs));
2059 return -ENOTSUP;
2060 }
feeee5ac 2061
f0aa7a8b
MDCF
2062 ret = bdrv_snapshot_find(bs, &sn, name);
2063 if (ret < 0) {
2064 error_report("Device '%s' does not have the requested snapshot '%s'",
2065 bdrv_get_device_name(bs), name);
2066 return ret;
2067 }
a672b469
AL
2068 }
2069
2070 /* Flush all IO requests so they don't interfere with the new state. */
2071 qemu_aio_flush();
2072
f0aa7a8b
MDCF
2073 bs = NULL;
2074 while ((bs = bdrv_next(bs))) {
2075 if (bdrv_can_snapshot(bs)) {
2076 ret = bdrv_snapshot_goto(bs, name);
a672b469 2077 if (ret < 0) {
f0aa7a8b
MDCF
2078 error_report("Error %d while activating snapshot '%s' on '%s'",
2079 ret, name, bdrv_get_device_name(bs));
2080 return ret;
a672b469
AL
2081 }
2082 }
2083 }
2084
a672b469 2085 /* restore the VM state */
f0aa7a8b 2086 f = qemu_fopen_bdrv(bs_vm_state, 0);
a672b469 2087 if (!f) {
1ecda02b 2088 error_report("Could not open VM state file");
05f2401e 2089 return -EINVAL;
a672b469 2090 }
f0aa7a8b 2091
5a8a49d7 2092 qemu_system_reset(VMRESET_SILENT);
a672b469 2093 ret = qemu_loadvm_state(f);
f0aa7a8b 2094
a672b469
AL
2095 qemu_fclose(f);
2096 if (ret < 0) {
1ecda02b 2097 error_report("Error %d while loading VM state", ret);
05f2401e 2098 return ret;
a672b469 2099 }
f0aa7a8b 2100
05f2401e 2101 return 0;
7b630349
JQ
2102}
2103
d54908a5 2104void do_delvm(Monitor *mon, const QDict *qdict)
a672b469
AL
2105{
2106 BlockDriverState *bs, *bs1;
751c6a17 2107 int ret;
d54908a5 2108 const char *name = qdict_get_str(qdict, "name");
a672b469 2109
f9092b10 2110 bs = bdrv_snapshots();
a672b469 2111 if (!bs) {
376253ec 2112 monitor_printf(mon, "No block device supports snapshots\n");
a672b469
AL
2113 return;
2114 }
2115
dbc13590
MA
2116 bs1 = NULL;
2117 while ((bs1 = bdrv_next(bs1))) {
feeee5ac 2118 if (bdrv_can_snapshot(bs1)) {
a672b469
AL
2119 ret = bdrv_snapshot_delete(bs1, name);
2120 if (ret < 0) {
2121 if (ret == -ENOTSUP)
376253ec
AL
2122 monitor_printf(mon,
2123 "Snapshots not supported on device '%s'\n",
2124 bdrv_get_device_name(bs1));
a672b469 2125 else
376253ec
AL
2126 monitor_printf(mon, "Error %d while deleting snapshot on "
2127 "'%s'\n", ret, bdrv_get_device_name(bs1));
a672b469
AL
2128 }
2129 }
2130 }
2131}
2132
376253ec 2133void do_info_snapshots(Monitor *mon)
a672b469
AL
2134{
2135 BlockDriverState *bs, *bs1;
f9209915
MDCF
2136 QEMUSnapshotInfo *sn_tab, *sn, s, *sn_info = &s;
2137 int nb_sns, i, ret, available;
2138 int total;
2139 int *available_snapshots;
a672b469
AL
2140 char buf[256];
2141
f9092b10 2142 bs = bdrv_snapshots();
a672b469 2143 if (!bs) {
376253ec 2144 monitor_printf(mon, "No available block device supports snapshots\n");
a672b469
AL
2145 return;
2146 }
a672b469
AL
2147
2148 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
2149 if (nb_sns < 0) {
376253ec 2150 monitor_printf(mon, "bdrv_snapshot_list: error %d\n", nb_sns);
a672b469
AL
2151 return;
2152 }
f9209915
MDCF
2153
2154 if (nb_sns == 0) {
2155 monitor_printf(mon, "There is no snapshot available.\n");
2156 return;
2157 }
2158
7267c094 2159 available_snapshots = g_malloc0(sizeof(int) * nb_sns);
f9209915
MDCF
2160 total = 0;
2161 for (i = 0; i < nb_sns; i++) {
a672b469 2162 sn = &sn_tab[i];
f9209915
MDCF
2163 available = 1;
2164 bs1 = NULL;
2165
2166 while ((bs1 = bdrv_next(bs1))) {
2167 if (bdrv_can_snapshot(bs1) && bs1 != bs) {
2168 ret = bdrv_snapshot_find(bs1, sn_info, sn->id_str);
2169 if (ret < 0) {
2170 available = 0;
2171 break;
2172 }
2173 }
2174 }
2175
2176 if (available) {
2177 available_snapshots[total] = i;
2178 total++;
2179 }
a672b469 2180 }
f9209915
MDCF
2181
2182 if (total > 0) {
2183 monitor_printf(mon, "%s\n", bdrv_snapshot_dump(buf, sizeof(buf), NULL));
2184 for (i = 0; i < total; i++) {
2185 sn = &sn_tab[available_snapshots[i]];
2186 monitor_printf(mon, "%s\n", bdrv_snapshot_dump(buf, sizeof(buf), sn));
2187 }
2188 } else {
2189 monitor_printf(mon, "There is no suitable snapshot available\n");
2190 }
2191
7267c094
AL
2192 g_free(sn_tab);
2193 g_free(available_snapshots);
f9209915 2194
a672b469 2195}
This page took 0.747886 seconds and 4 git commands to generate.