]> git.proxmox.com Git - qemu.git/blame - savevm.c
migration: return real error code
[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 263 s->file = qemu_fopen_ops(s, NULL, stdio_get_buffer, stdio_pclose,
264 NULL, NULL, NULL);
a672b469 265 } else {
c163b5ca 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 312 s->file = qemu_fopen_ops(s, NULL, stdio_get_buffer, stdio_fclose,
313 NULL, NULL, NULL);
5ac1fad3 314 } else {
c163b5ca 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 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 368 s->file = qemu_fopen_ops(s, file_put_buffer, NULL, stdio_fclose,
369 NULL, NULL, NULL);
7f79dd28 370 } else {
c163b5ca 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 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 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
dcd1d224 433void qemu_file_set_error(QEMUFile *f, int ret)
4dabe248 434{
dcd1d224 435 f->has_error = ret;
4dabe248
AL
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
dcd1d224 450 f->has_error = -EINVAL;
a672b469
AL
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)
dcd1d224 479 f->has_error = len;
a672b469
AL
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
c6380724 535static void qemu_file_skip(QEMUFile *f, int size)
a672b469 536{
c6380724
JQ
537 if (f->buf_index + size <= f->buf_size) {
538 f->buf_index += size;
539 }
540}
541
542static int qemu_peek_buffer(QEMUFile *f, uint8_t *buf, int size, size_t offset)
543{
544 int pending;
545 int index;
a672b469 546
b9ce1454 547 if (f->is_write) {
a672b469 548 abort();
b9ce1454 549 }
a672b469 550
c6380724
JQ
551 index = f->buf_index + offset;
552 pending = f->buf_size - index;
553 if (pending < size) {
554 qemu_fill_buffer(f);
555 index = f->buf_index + offset;
556 pending = f->buf_size - index;
557 }
558
559 if (pending <= 0) {
560 return 0;
561 }
562 if (size > pending) {
563 size = pending;
564 }
565
566 memcpy(buf, f->buf + index, size);
567 return size;
568}
569
570int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size)
571{
572 int pending = size;
573 int done = 0;
574
575 while (pending > 0) {
576 int res;
577
578 res = qemu_peek_buffer(f, buf, pending, 0);
579 if (res == 0) {
580 return done;
b9ce1454 581 }
c6380724
JQ
582 qemu_file_skip(f, res);
583 buf += res;
584 pending -= res;
585 done += res;
a672b469 586 }
c6380724 587 return done;
a672b469
AL
588}
589
c6380724 590static int qemu_peek_byte(QEMUFile *f, int offset)
811814bd 591{
c6380724
JQ
592 int index = f->buf_index + offset;
593
b9ce1454 594 if (f->is_write) {
811814bd 595 abort();
b9ce1454 596 }
811814bd 597
c6380724 598 if (index >= f->buf_size) {
811814bd 599 qemu_fill_buffer(f);
c6380724
JQ
600 index = f->buf_index + offset;
601 if (index >= f->buf_size) {
811814bd 602 return 0;
b9ce1454 603 }
811814bd 604 }
c6380724 605 return f->buf[index];
811814bd
JQ
606}
607
a672b469
AL
608int qemu_get_byte(QEMUFile *f)
609{
65f3bb3d 610 int result;
a672b469 611
c6380724
JQ
612 result = qemu_peek_byte(f, 0);
613 qemu_file_skip(f, 1);
65f3bb3d 614 return result;
a672b469
AL
615}
616
617int64_t qemu_ftell(QEMUFile *f)
618{
619 return f->buf_offset - f->buf_size + f->buf_index;
620}
621
622int64_t qemu_fseek(QEMUFile *f, int64_t pos, int whence)
623{
624 if (whence == SEEK_SET) {
625 /* nothing to do */
626 } else if (whence == SEEK_CUR) {
627 pos += qemu_ftell(f);
628 } else {
629 /* SEEK_END not supported */
630 return -1;
631 }
632 if (f->put_buffer) {
633 qemu_fflush(f);
634 f->buf_offset = pos;
635 } else {
636 f->buf_offset = pos;
637 f->buf_index = 0;
638 f->buf_size = 0;
639 }
640 return pos;
641}
642
643int qemu_file_rate_limit(QEMUFile *f)
644{
645 if (f->rate_limit)
646 return f->rate_limit(f->opaque);
647
648 return 0;
649}
650
3d002df3 651int64_t qemu_file_get_rate_limit(QEMUFile *f)
c163b5ca 652{
653 if (f->get_rate_limit)
654 return f->get_rate_limit(f->opaque);
655
656 return 0;
657}
658
3d002df3 659int64_t qemu_file_set_rate_limit(QEMUFile *f, int64_t new_rate)
19629537 660{
0bb05eaf
GC
661 /* any failed or completed migration keeps its state to allow probing of
662 * migration data, but has no associated file anymore */
663 if (f && f->set_rate_limit)
19629537
GC
664 return f->set_rate_limit(f->opaque, new_rate);
665
666 return 0;
667}
668
a672b469
AL
669void qemu_put_be16(QEMUFile *f, unsigned int v)
670{
671 qemu_put_byte(f, v >> 8);
672 qemu_put_byte(f, v);
673}
674
675void qemu_put_be32(QEMUFile *f, unsigned int v)
676{
677 qemu_put_byte(f, v >> 24);
678 qemu_put_byte(f, v >> 16);
679 qemu_put_byte(f, v >> 8);
680 qemu_put_byte(f, v);
681}
682
683void qemu_put_be64(QEMUFile *f, uint64_t v)
684{
685 qemu_put_be32(f, v >> 32);
686 qemu_put_be32(f, v);
687}
688
689unsigned int qemu_get_be16(QEMUFile *f)
690{
691 unsigned int v;
692 v = qemu_get_byte(f) << 8;
693 v |= qemu_get_byte(f);
694 return v;
695}
696
697unsigned int qemu_get_be32(QEMUFile *f)
698{
699 unsigned int v;
700 v = qemu_get_byte(f) << 24;
701 v |= qemu_get_byte(f) << 16;
702 v |= qemu_get_byte(f) << 8;
703 v |= qemu_get_byte(f);
704 return v;
705}
706
707uint64_t qemu_get_be64(QEMUFile *f)
708{
709 uint64_t v;
710 v = (uint64_t)qemu_get_be32(f) << 32;
711 v |= qemu_get_be32(f);
712 return v;
713}
714
cdae5cfb
GH
715/* bool */
716
717static int get_bool(QEMUFile *f, void *pv, size_t size)
718{
719 bool *v = pv;
720 *v = qemu_get_byte(f);
721 return 0;
722}
723
724static void put_bool(QEMUFile *f, void *pv, size_t size)
725{
726 bool *v = pv;
727 qemu_put_byte(f, *v);
728}
729
730const VMStateInfo vmstate_info_bool = {
731 .name = "bool",
732 .get = get_bool,
733 .put = put_bool,
734};
735
9ed7d6ae
JQ
736/* 8 bit int */
737
738static int get_int8(QEMUFile *f, void *pv, size_t size)
739{
740 int8_t *v = pv;
741 qemu_get_s8s(f, v);
742 return 0;
743}
744
84e2e3eb 745static void put_int8(QEMUFile *f, void *pv, size_t size)
9ed7d6ae 746{
84e2e3eb 747 int8_t *v = pv;
9ed7d6ae
JQ
748 qemu_put_s8s(f, v);
749}
750
751const VMStateInfo vmstate_info_int8 = {
752 .name = "int8",
753 .get = get_int8,
754 .put = put_int8,
755};
756
757/* 16 bit int */
758
759static int get_int16(QEMUFile *f, void *pv, size_t size)
760{
761 int16_t *v = pv;
762 qemu_get_sbe16s(f, v);
763 return 0;
764}
765
84e2e3eb 766static void put_int16(QEMUFile *f, void *pv, size_t size)
9ed7d6ae 767{
84e2e3eb 768 int16_t *v = pv;
9ed7d6ae
JQ
769 qemu_put_sbe16s(f, v);
770}
771
772const VMStateInfo vmstate_info_int16 = {
773 .name = "int16",
774 .get = get_int16,
775 .put = put_int16,
776};
777
778/* 32 bit int */
779
780static int get_int32(QEMUFile *f, void *pv, size_t size)
781{
782 int32_t *v = pv;
783 qemu_get_sbe32s(f, v);
784 return 0;
785}
786
84e2e3eb 787static void put_int32(QEMUFile *f, void *pv, size_t size)
9ed7d6ae 788{
84e2e3eb 789 int32_t *v = pv;
9ed7d6ae
JQ
790 qemu_put_sbe32s(f, v);
791}
792
793const VMStateInfo vmstate_info_int32 = {
794 .name = "int32",
795 .get = get_int32,
796 .put = put_int32,
797};
798
82501660
JQ
799/* 32 bit int. See that the received value is the same than the one
800 in the field */
801
802static int get_int32_equal(QEMUFile *f, void *pv, size_t size)
803{
804 int32_t *v = pv;
805 int32_t v2;
806 qemu_get_sbe32s(f, &v2);
807
808 if (*v == v2)
809 return 0;
810 return -EINVAL;
811}
812
813const VMStateInfo vmstate_info_int32_equal = {
814 .name = "int32 equal",
815 .get = get_int32_equal,
816 .put = put_int32,
817};
818
0a031e0a
JQ
819/* 32 bit int. See that the received value is the less or the same
820 than the one in the field */
821
822static int get_int32_le(QEMUFile *f, void *pv, size_t size)
823{
824 int32_t *old = pv;
825 int32_t new;
826 qemu_get_sbe32s(f, &new);
827
828 if (*old <= new)
829 return 0;
830 return -EINVAL;
831}
832
833const VMStateInfo vmstate_info_int32_le = {
834 .name = "int32 equal",
835 .get = get_int32_le,
836 .put = put_int32,
837};
838
9ed7d6ae
JQ
839/* 64 bit int */
840
841static int get_int64(QEMUFile *f, void *pv, size_t size)
842{
843 int64_t *v = pv;
844 qemu_get_sbe64s(f, v);
845 return 0;
846}
847
84e2e3eb 848static void put_int64(QEMUFile *f, void *pv, size_t size)
9ed7d6ae 849{
84e2e3eb 850 int64_t *v = pv;
9ed7d6ae
JQ
851 qemu_put_sbe64s(f, v);
852}
853
854const VMStateInfo vmstate_info_int64 = {
855 .name = "int64",
856 .get = get_int64,
857 .put = put_int64,
858};
859
860/* 8 bit unsigned int */
861
862static int get_uint8(QEMUFile *f, void *pv, size_t size)
863{
864 uint8_t *v = pv;
865 qemu_get_8s(f, v);
866 return 0;
867}
868
84e2e3eb 869static void put_uint8(QEMUFile *f, void *pv, size_t size)
9ed7d6ae 870{
84e2e3eb 871 uint8_t *v = pv;
9ed7d6ae
JQ
872 qemu_put_8s(f, v);
873}
874
875const VMStateInfo vmstate_info_uint8 = {
876 .name = "uint8",
877 .get = get_uint8,
878 .put = put_uint8,
879};
880
881/* 16 bit unsigned int */
882
883static int get_uint16(QEMUFile *f, void *pv, size_t size)
884{
885 uint16_t *v = pv;
886 qemu_get_be16s(f, v);
887 return 0;
888}
889
84e2e3eb 890static void put_uint16(QEMUFile *f, void *pv, size_t size)
9ed7d6ae 891{
84e2e3eb 892 uint16_t *v = pv;
9ed7d6ae
JQ
893 qemu_put_be16s(f, v);
894}
895
896const VMStateInfo vmstate_info_uint16 = {
897 .name = "uint16",
898 .get = get_uint16,
899 .put = put_uint16,
900};
901
902/* 32 bit unsigned int */
903
904static int get_uint32(QEMUFile *f, void *pv, size_t size)
905{
906 uint32_t *v = pv;
907 qemu_get_be32s(f, v);
908 return 0;
909}
910
84e2e3eb 911static void put_uint32(QEMUFile *f, void *pv, size_t size)
9ed7d6ae 912{
84e2e3eb 913 uint32_t *v = pv;
9ed7d6ae
JQ
914 qemu_put_be32s(f, v);
915}
916
917const VMStateInfo vmstate_info_uint32 = {
918 .name = "uint32",
919 .get = get_uint32,
920 .put = put_uint32,
921};
922
9122a8fe
JQ
923/* 32 bit uint. See that the received value is the same than the one
924 in the field */
925
926static int get_uint32_equal(QEMUFile *f, void *pv, size_t size)
927{
928 uint32_t *v = pv;
929 uint32_t v2;
930 qemu_get_be32s(f, &v2);
931
932 if (*v == v2) {
933 return 0;
934 }
935 return -EINVAL;
936}
937
938const VMStateInfo vmstate_info_uint32_equal = {
939 .name = "uint32 equal",
940 .get = get_uint32_equal,
941 .put = put_uint32,
942};
943
9ed7d6ae
JQ
944/* 64 bit unsigned int */
945
946static int get_uint64(QEMUFile *f, void *pv, size_t size)
947{
948 uint64_t *v = pv;
949 qemu_get_be64s(f, v);
950 return 0;
951}
952
84e2e3eb 953static void put_uint64(QEMUFile *f, void *pv, size_t size)
9ed7d6ae 954{
84e2e3eb 955 uint64_t *v = pv;
9ed7d6ae
JQ
956 qemu_put_be64s(f, v);
957}
958
959const VMStateInfo vmstate_info_uint64 = {
960 .name = "uint64",
961 .get = get_uint64,
962 .put = put_uint64,
963};
964
80cd83e7
JQ
965/* 8 bit int. See that the received value is the same than the one
966 in the field */
967
968static int get_uint8_equal(QEMUFile *f, void *pv, size_t size)
969{
970 uint8_t *v = pv;
971 uint8_t v2;
972 qemu_get_8s(f, &v2);
973
974 if (*v == v2)
975 return 0;
976 return -EINVAL;
977}
978
979const VMStateInfo vmstate_info_uint8_equal = {
aa1cce69 980 .name = "uint8 equal",
80cd83e7
JQ
981 .get = get_uint8_equal,
982 .put = put_uint8,
983};
984
dc3b83a0
JQ
985/* 16 bit unsigned int int. See that the received value is the same than the one
986 in the field */
987
988static int get_uint16_equal(QEMUFile *f, void *pv, size_t size)
989{
990 uint16_t *v = pv;
991 uint16_t v2;
992 qemu_get_be16s(f, &v2);
993
994 if (*v == v2)
995 return 0;
996 return -EINVAL;
997}
998
999const VMStateInfo vmstate_info_uint16_equal = {
1000 .name = "uint16 equal",
1001 .get = get_uint16_equal,
1002 .put = put_uint16,
1003};
1004
dde0463b
JQ
1005/* timers */
1006
1007static int get_timer(QEMUFile *f, void *pv, size_t size)
1008{
1009 QEMUTimer *v = pv;
1010 qemu_get_timer(f, v);
1011 return 0;
1012}
1013
84e2e3eb 1014static void put_timer(QEMUFile *f, void *pv, size_t size)
dde0463b 1015{
84e2e3eb 1016 QEMUTimer *v = pv;
dde0463b
JQ
1017 qemu_put_timer(f, v);
1018}
1019
1020const VMStateInfo vmstate_info_timer = {
1021 .name = "timer",
1022 .get = get_timer,
1023 .put = put_timer,
1024};
1025
6f67c50f
JQ
1026/* uint8_t buffers */
1027
1028static int get_buffer(QEMUFile *f, void *pv, size_t size)
1029{
1030 uint8_t *v = pv;
1031 qemu_get_buffer(f, v, size);
1032 return 0;
1033}
1034
84e2e3eb 1035static void put_buffer(QEMUFile *f, void *pv, size_t size)
6f67c50f 1036{
84e2e3eb 1037 uint8_t *v = pv;
6f67c50f
JQ
1038 qemu_put_buffer(f, v, size);
1039}
1040
1041const VMStateInfo vmstate_info_buffer = {
1042 .name = "buffer",
1043 .get = get_buffer,
1044 .put = put_buffer,
1045};
1046
76507c75 1047/* unused buffers: space that was used for some fields that are
61cc8701 1048 not useful anymore */
76507c75
JQ
1049
1050static int get_unused_buffer(QEMUFile *f, void *pv, size_t size)
1051{
21174c34
JK
1052 uint8_t buf[1024];
1053 int block_len;
1054
1055 while (size > 0) {
1056 block_len = MIN(sizeof(buf), size);
1057 size -= block_len;
1058 qemu_get_buffer(f, buf, block_len);
1059 }
1060 return 0;
76507c75
JQ
1061}
1062
1063static void put_unused_buffer(QEMUFile *f, void *pv, size_t size)
1064{
21174c34
JK
1065 static const uint8_t buf[1024];
1066 int block_len;
1067
1068 while (size > 0) {
1069 block_len = MIN(sizeof(buf), size);
1070 size -= block_len;
1071 qemu_put_buffer(f, buf, block_len);
1072 }
76507c75
JQ
1073}
1074
1075const VMStateInfo vmstate_info_unused_buffer = {
1076 .name = "unused_buffer",
1077 .get = get_unused_buffer,
1078 .put = put_unused_buffer,
1079};
1080
7685ee6a
AW
1081typedef struct CompatEntry {
1082 char idstr[256];
1083 int instance_id;
1084} CompatEntry;
1085
a672b469 1086typedef struct SaveStateEntry {
72cf2d4f 1087 QTAILQ_ENTRY(SaveStateEntry) entry;
a672b469
AL
1088 char idstr[256];
1089 int instance_id;
4d2ffa08 1090 int alias_id;
a672b469
AL
1091 int version_id;
1092 int section_id;
c163b5ca 1093 SaveSetParamsHandler *set_params;
a672b469
AL
1094 SaveLiveStateHandler *save_live_state;
1095 SaveStateHandler *save_state;
1096 LoadStateHandler *load_state;
9ed7d6ae 1097 const VMStateDescription *vmsd;
a672b469 1098 void *opaque;
7685ee6a 1099 CompatEntry *compat;
24312968 1100 int no_migrate;
a672b469
AL
1101} SaveStateEntry;
1102
c163b5ca 1103
72cf2d4f
BS
1104static QTAILQ_HEAD(savevm_handlers, SaveStateEntry) savevm_handlers =
1105 QTAILQ_HEAD_INITIALIZER(savevm_handlers);
9ed7d6ae 1106static int global_section_id;
a672b469 1107
8718e999
JQ
1108static int calculate_new_instance_id(const char *idstr)
1109{
1110 SaveStateEntry *se;
1111 int instance_id = 0;
1112
72cf2d4f 1113 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
8718e999
JQ
1114 if (strcmp(idstr, se->idstr) == 0
1115 && instance_id <= se->instance_id) {
1116 instance_id = se->instance_id + 1;
1117 }
1118 }
1119 return instance_id;
1120}
1121
7685ee6a
AW
1122static int calculate_compat_instance_id(const char *idstr)
1123{
1124 SaveStateEntry *se;
1125 int instance_id = 0;
1126
1127 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1128 if (!se->compat)
1129 continue;
1130
1131 if (strcmp(idstr, se->compat->idstr) == 0
1132 && instance_id <= se->compat->instance_id) {
1133 instance_id = se->compat->instance_id + 1;
1134 }
1135 }
1136 return instance_id;
1137}
1138
a672b469
AL
1139/* TODO: Individual devices generally have very little idea about the rest
1140 of the system, so instance_id should be removed/replaced.
1141 Meanwhile pass -1 as instance_id if you do not already have a clearly
1142 distinguishing id for all instances of your device class. */
0be71e32
AW
1143int register_savevm_live(DeviceState *dev,
1144 const char *idstr,
a672b469
AL
1145 int instance_id,
1146 int version_id,
c163b5ca 1147 SaveSetParamsHandler *set_params,
a672b469
AL
1148 SaveLiveStateHandler *save_live_state,
1149 SaveStateHandler *save_state,
1150 LoadStateHandler *load_state,
1151 void *opaque)
1152{
8718e999 1153 SaveStateEntry *se;
a672b469 1154
7267c094 1155 se = g_malloc0(sizeof(SaveStateEntry));
a672b469
AL
1156 se->version_id = version_id;
1157 se->section_id = global_section_id++;
c163b5ca 1158 se->set_params = set_params;
a672b469
AL
1159 se->save_live_state = save_live_state;
1160 se->save_state = save_state;
1161 se->load_state = load_state;
1162 se->opaque = opaque;
9ed7d6ae 1163 se->vmsd = NULL;
24312968 1164 se->no_migrate = 0;
a672b469 1165
7685ee6a
AW
1166 if (dev && dev->parent_bus && dev->parent_bus->info->get_dev_path) {
1167 char *id = dev->parent_bus->info->get_dev_path(dev);
1168 if (id) {
1169 pstrcpy(se->idstr, sizeof(se->idstr), id);
1170 pstrcat(se->idstr, sizeof(se->idstr), "/");
7267c094 1171 g_free(id);
7685ee6a 1172
7267c094 1173 se->compat = g_malloc0(sizeof(CompatEntry));
7685ee6a
AW
1174 pstrcpy(se->compat->idstr, sizeof(se->compat->idstr), idstr);
1175 se->compat->instance_id = instance_id == -1 ?
1176 calculate_compat_instance_id(idstr) : instance_id;
1177 instance_id = -1;
1178 }
1179 }
1180 pstrcat(se->idstr, sizeof(se->idstr), idstr);
1181
8718e999 1182 if (instance_id == -1) {
7685ee6a 1183 se->instance_id = calculate_new_instance_id(se->idstr);
8718e999
JQ
1184 } else {
1185 se->instance_id = instance_id;
a672b469 1186 }
7685ee6a 1187 assert(!se->compat || se->instance_id == 0);
8718e999 1188 /* add at the end of list */
72cf2d4f 1189 QTAILQ_INSERT_TAIL(&savevm_handlers, se, entry);
a672b469
AL
1190 return 0;
1191}
1192
0be71e32
AW
1193int register_savevm(DeviceState *dev,
1194 const char *idstr,
a672b469
AL
1195 int instance_id,
1196 int version_id,
1197 SaveStateHandler *save_state,
1198 LoadStateHandler *load_state,
1199 void *opaque)
1200{
0be71e32 1201 return register_savevm_live(dev, idstr, instance_id, version_id,
c163b5ca 1202 NULL, NULL, save_state, load_state, opaque);
a672b469
AL
1203}
1204
0be71e32 1205void unregister_savevm(DeviceState *dev, const char *idstr, void *opaque)
41bd13af 1206{
8718e999 1207 SaveStateEntry *se, *new_se;
7685ee6a
AW
1208 char id[256] = "";
1209
1210 if (dev && dev->parent_bus && dev->parent_bus->info->get_dev_path) {
1211 char *path = dev->parent_bus->info->get_dev_path(dev);
1212 if (path) {
1213 pstrcpy(id, sizeof(id), path);
1214 pstrcat(id, sizeof(id), "/");
7267c094 1215 g_free(path);
7685ee6a
AW
1216 }
1217 }
1218 pstrcat(id, sizeof(id), idstr);
41bd13af 1219
72cf2d4f 1220 QTAILQ_FOREACH_SAFE(se, &savevm_handlers, entry, new_se) {
7685ee6a 1221 if (strcmp(se->idstr, id) == 0 && se->opaque == opaque) {
72cf2d4f 1222 QTAILQ_REMOVE(&savevm_handlers, se, entry);
69e58af9 1223 if (se->compat) {
7267c094 1224 g_free(se->compat);
69e58af9 1225 }
7267c094 1226 g_free(se);
41bd13af 1227 }
41bd13af
AL
1228 }
1229}
1230
24312968
CM
1231/* mark a device as not to be migrated, that is the device should be
1232 unplugged before migration */
1233void register_device_unmigratable(DeviceState *dev, const char *idstr,
1234 void *opaque)
1235{
1236 SaveStateEntry *se;
1237 char id[256] = "";
1238
1239 if (dev && dev->parent_bus && dev->parent_bus->info->get_dev_path) {
1240 char *path = dev->parent_bus->info->get_dev_path(dev);
1241 if (path) {
1242 pstrcpy(id, sizeof(id), path);
1243 pstrcat(id, sizeof(id), "/");
7267c094 1244 g_free(path);
24312968
CM
1245 }
1246 }
1247 pstrcat(id, sizeof(id), idstr);
1248
1249 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1250 if (strcmp(se->idstr, id) == 0 && se->opaque == opaque) {
1251 se->no_migrate = 1;
1252 }
1253 }
1254}
1255
0be71e32 1256int vmstate_register_with_alias_id(DeviceState *dev, int instance_id,
4d2ffa08
JK
1257 const VMStateDescription *vmsd,
1258 void *opaque, int alias_id,
1259 int required_for_version)
9ed7d6ae 1260{
8718e999 1261 SaveStateEntry *se;
9ed7d6ae 1262
4d2ffa08
JK
1263 /* If this triggers, alias support can be dropped for the vmsd. */
1264 assert(alias_id == -1 || required_for_version >= vmsd->minimum_version_id);
1265
7267c094 1266 se = g_malloc0(sizeof(SaveStateEntry));
9ed7d6ae
JQ
1267 se->version_id = vmsd->version_id;
1268 se->section_id = global_section_id++;
1269 se->save_live_state = NULL;
1270 se->save_state = NULL;
1271 se->load_state = NULL;
1272 se->opaque = opaque;
1273 se->vmsd = vmsd;
4d2ffa08 1274 se->alias_id = alias_id;
2837c8ea 1275 se->no_migrate = vmsd->unmigratable;
9ed7d6ae 1276
7685ee6a
AW
1277 if (dev && dev->parent_bus && dev->parent_bus->info->get_dev_path) {
1278 char *id = dev->parent_bus->info->get_dev_path(dev);
1279 if (id) {
1280 pstrcpy(se->idstr, sizeof(se->idstr), id);
1281 pstrcat(se->idstr, sizeof(se->idstr), "/");
7267c094 1282 g_free(id);
7685ee6a 1283
7267c094 1284 se->compat = g_malloc0(sizeof(CompatEntry));
7685ee6a
AW
1285 pstrcpy(se->compat->idstr, sizeof(se->compat->idstr), vmsd->name);
1286 se->compat->instance_id = instance_id == -1 ?
1287 calculate_compat_instance_id(vmsd->name) : instance_id;
1288 instance_id = -1;
1289 }
1290 }
1291 pstrcat(se->idstr, sizeof(se->idstr), vmsd->name);
1292
8718e999 1293 if (instance_id == -1) {
7685ee6a 1294 se->instance_id = calculate_new_instance_id(se->idstr);
8718e999
JQ
1295 } else {
1296 se->instance_id = instance_id;
9ed7d6ae 1297 }
7685ee6a 1298 assert(!se->compat || se->instance_id == 0);
8718e999 1299 /* add at the end of list */
72cf2d4f 1300 QTAILQ_INSERT_TAIL(&savevm_handlers, se, entry);
9ed7d6ae
JQ
1301 return 0;
1302}
1303
0be71e32
AW
1304int vmstate_register(DeviceState *dev, int instance_id,
1305 const VMStateDescription *vmsd, void *opaque)
4d2ffa08 1306{
0be71e32
AW
1307 return vmstate_register_with_alias_id(dev, instance_id, vmsd,
1308 opaque, -1, 0);
4d2ffa08
JK
1309}
1310
0be71e32
AW
1311void vmstate_unregister(DeviceState *dev, const VMStateDescription *vmsd,
1312 void *opaque)
9ed7d6ae 1313{
1eb7538b
JQ
1314 SaveStateEntry *se, *new_se;
1315
72cf2d4f 1316 QTAILQ_FOREACH_SAFE(se, &savevm_handlers, entry, new_se) {
1eb7538b 1317 if (se->vmsd == vmsd && se->opaque == opaque) {
72cf2d4f 1318 QTAILQ_REMOVE(&savevm_handlers, se, entry);
69e58af9 1319 if (se->compat) {
7267c094 1320 g_free(se->compat);
69e58af9 1321 }
7267c094 1322 g_free(se);
1eb7538b
JQ
1323 }
1324 }
9ed7d6ae
JQ
1325}
1326
811814bd
JQ
1327static void vmstate_subsection_save(QEMUFile *f, const VMStateDescription *vmsd,
1328 void *opaque);
1329static int vmstate_subsection_load(QEMUFile *f, const VMStateDescription *vmsd,
1330 void *opaque);
1331
9ed7d6ae
JQ
1332int vmstate_load_state(QEMUFile *f, const VMStateDescription *vmsd,
1333 void *opaque, int version_id)
1334{
1335 VMStateField *field = vmsd->fields;
811814bd 1336 int ret;
9ed7d6ae
JQ
1337
1338 if (version_id > vmsd->version_id) {
1339 return -EINVAL;
1340 }
1341 if (version_id < vmsd->minimum_version_id_old) {
1342 return -EINVAL;
1343 }
1344 if (version_id < vmsd->minimum_version_id) {
1345 return vmsd->load_state_old(f, opaque, version_id);
1346 }
fd4d52de
JQ
1347 if (vmsd->pre_load) {
1348 int ret = vmsd->pre_load(opaque);
1349 if (ret)
1350 return ret;
1351 }
9ed7d6ae 1352 while(field->name) {
f11f6a5f
JQ
1353 if ((field->field_exists &&
1354 field->field_exists(opaque, version_id)) ||
1355 (!field->field_exists &&
1356 field->version_id <= version_id)) {
f752a6aa 1357 void *base_addr = opaque + field->offset;
811814bd 1358 int i, n_elems = 1;
e61a1e0a 1359 int size = field->size;
9ed7d6ae 1360
e61a1e0a
JQ
1361 if (field->flags & VMS_VBUFFER) {
1362 size = *(int32_t *)(opaque+field->size_offset);
33599e2a
JQ
1363 if (field->flags & VMS_MULTIPLY) {
1364 size *= field->size;
1365 }
e61a1e0a 1366 }
f752a6aa
JQ
1367 if (field->flags & VMS_ARRAY) {
1368 n_elems = field->num;
d6698281
JQ
1369 } else if (field->flags & VMS_VARRAY_INT32) {
1370 n_elems = *(int32_t *)(opaque+field->num_offset);
a624b086
JQ
1371 } else if (field->flags & VMS_VARRAY_UINT32) {
1372 n_elems = *(uint32_t *)(opaque+field->num_offset);
bdb4941d
JQ
1373 } else if (field->flags & VMS_VARRAY_UINT16) {
1374 n_elems = *(uint16_t *)(opaque+field->num_offset);
82fa39b7
JQ
1375 } else if (field->flags & VMS_VARRAY_UINT8) {
1376 n_elems = *(uint8_t *)(opaque+field->num_offset);
f752a6aa 1377 }
dde0463b 1378 if (field->flags & VMS_POINTER) {
e61a1e0a 1379 base_addr = *(void **)base_addr + field->start;
dde0463b 1380 }
f752a6aa 1381 for (i = 0; i < n_elems; i++) {
e61a1e0a 1382 void *addr = base_addr + size * i;
ec245e21 1383
19df438b
JQ
1384 if (field->flags & VMS_ARRAY_OF_POINTER) {
1385 addr = *(void **)addr;
1386 }
ec245e21 1387 if (field->flags & VMS_STRUCT) {
fa3aad24 1388 ret = vmstate_load_state(f, field->vmsd, addr, field->vmsd->version_id);
ec245e21 1389 } else {
e61a1e0a 1390 ret = field->info->get(f, addr, size);
ec245e21
JQ
1391
1392 }
f752a6aa
JQ
1393 if (ret < 0) {
1394 return ret;
1395 }
9ed7d6ae
JQ
1396 }
1397 }
1398 field++;
1399 }
811814bd
JQ
1400 ret = vmstate_subsection_load(f, vmsd, opaque);
1401 if (ret != 0) {
1402 return ret;
1403 }
752ff2fa 1404 if (vmsd->post_load) {
e59fb374 1405 return vmsd->post_load(opaque, version_id);
752ff2fa 1406 }
9ed7d6ae
JQ
1407 return 0;
1408}
1409
1410void vmstate_save_state(QEMUFile *f, const VMStateDescription *vmsd,
84e2e3eb 1411 void *opaque)
9ed7d6ae
JQ
1412{
1413 VMStateField *field = vmsd->fields;
1414
8fb0791d
JQ
1415 if (vmsd->pre_save) {
1416 vmsd->pre_save(opaque);
1417 }
9ed7d6ae 1418 while(field->name) {
f11f6a5f
JQ
1419 if (!field->field_exists ||
1420 field->field_exists(opaque, vmsd->version_id)) {
1421 void *base_addr = opaque + field->offset;
1422 int i, n_elems = 1;
e61a1e0a 1423 int size = field->size;
dde0463b 1424
e61a1e0a
JQ
1425 if (field->flags & VMS_VBUFFER) {
1426 size = *(int32_t *)(opaque+field->size_offset);
33599e2a
JQ
1427 if (field->flags & VMS_MULTIPLY) {
1428 size *= field->size;
1429 }
e61a1e0a 1430 }
f11f6a5f
JQ
1431 if (field->flags & VMS_ARRAY) {
1432 n_elems = field->num;
d6698281
JQ
1433 } else if (field->flags & VMS_VARRAY_INT32) {
1434 n_elems = *(int32_t *)(opaque+field->num_offset);
bdb4941d
JQ
1435 } else if (field->flags & VMS_VARRAY_UINT16) {
1436 n_elems = *(uint16_t *)(opaque+field->num_offset);
b784421c
JQ
1437 } else if (field->flags & VMS_VARRAY_UINT8) {
1438 n_elems = *(uint8_t *)(opaque+field->num_offset);
f11f6a5f
JQ
1439 }
1440 if (field->flags & VMS_POINTER) {
e61a1e0a 1441 base_addr = *(void **)base_addr + field->start;
f11f6a5f
JQ
1442 }
1443 for (i = 0; i < n_elems; i++) {
e61a1e0a 1444 void *addr = base_addr + size * i;
ec245e21 1445
8595387e
JQ
1446 if (field->flags & VMS_ARRAY_OF_POINTER) {
1447 addr = *(void **)addr;
1448 }
f11f6a5f
JQ
1449 if (field->flags & VMS_STRUCT) {
1450 vmstate_save_state(f, field->vmsd, addr);
1451 } else {
e61a1e0a 1452 field->info->put(f, addr, size);
f11f6a5f 1453 }
ec245e21 1454 }
dde0463b 1455 }
9ed7d6ae
JQ
1456 field++;
1457 }
811814bd 1458 vmstate_subsection_save(f, vmsd, opaque);
9ed7d6ae
JQ
1459}
1460
4082be4d
JQ
1461static int vmstate_load(QEMUFile *f, SaveStateEntry *se, int version_id)
1462{
9ed7d6ae
JQ
1463 if (!se->vmsd) { /* Old style */
1464 return se->load_state(f, se->opaque, version_id);
1465 }
1466 return vmstate_load_state(f, se->vmsd, se->opaque, version_id);
4082be4d
JQ
1467}
1468
dc912121 1469static void vmstate_save(QEMUFile *f, SaveStateEntry *se)
4082be4d 1470{
9ed7d6ae
JQ
1471 if (!se->vmsd) { /* Old style */
1472 se->save_state(f, se->opaque);
dc912121 1473 return;
9ed7d6ae
JQ
1474 }
1475 vmstate_save_state(f,se->vmsd, se->opaque);
4082be4d
JQ
1476}
1477
a672b469
AL
1478#define QEMU_VM_FILE_MAGIC 0x5145564d
1479#define QEMU_VM_FILE_VERSION_COMPAT 0x00000002
1480#define QEMU_VM_FILE_VERSION 0x00000003
1481
1482#define QEMU_VM_EOF 0x00
1483#define QEMU_VM_SECTION_START 0x01
1484#define QEMU_VM_SECTION_PART 0x02
1485#define QEMU_VM_SECTION_END 0x03
1486#define QEMU_VM_SECTION_FULL 0x04
811814bd 1487#define QEMU_VM_SUBSECTION 0x05
a672b469 1488
dc912121
AW
1489bool qemu_savevm_state_blocked(Monitor *mon)
1490{
1491 SaveStateEntry *se;
1492
1493 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1494 if (se->no_migrate) {
1495 monitor_printf(mon, "state blocked by non-migratable device '%s'\n",
1496 se->idstr);
1497 return true;
1498 }
1499 }
1500 return false;
1501}
1502
f327aa0c
JK
1503int qemu_savevm_state_begin(Monitor *mon, QEMUFile *f, int blk_enable,
1504 int shared)
a672b469
AL
1505{
1506 SaveStateEntry *se;
39346385 1507 int ret;
a672b469 1508
c163b5ca 1509 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1510 if(se->set_params == NULL) {
1511 continue;
1512 }
1513 se->set_params(blk_enable, shared, se->opaque);
1514 }
1515
a672b469
AL
1516 qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
1517 qemu_put_be32(f, QEMU_VM_FILE_VERSION);
1518
72cf2d4f 1519 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
a672b469
AL
1520 int len;
1521
1522 if (se->save_live_state == NULL)
1523 continue;
1524
1525 /* Section type */
1526 qemu_put_byte(f, QEMU_VM_SECTION_START);
1527 qemu_put_be32(f, se->section_id);
1528
1529 /* ID string */
1530 len = strlen(se->idstr);
1531 qemu_put_byte(f, len);
1532 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
1533
1534 qemu_put_be32(f, se->instance_id);
1535 qemu_put_be32(f, se->version_id);
1536
f327aa0c 1537 se->save_live_state(mon, f, QEMU_VM_SECTION_START, se->opaque);
a672b469 1538 }
39346385
JQ
1539 ret = qemu_file_has_error(f);
1540 if (ret != 0) {
f327aa0c 1541 qemu_savevm_state_cancel(mon, f);
4ec7fcc7 1542 }
a672b469 1543
39346385
JQ
1544 return ret;
1545
a672b469
AL
1546}
1547
39346385
JQ
1548/*
1549 * this funtion has three return values:
1550 * negative: there was one error, and we have -errno.
1551 * 0 : We haven't finished, caller have to go again
1552 * 1 : We have finished, we can go to complete phase
1553 */
f327aa0c 1554int qemu_savevm_state_iterate(Monitor *mon, QEMUFile *f)
a672b469
AL
1555{
1556 SaveStateEntry *se;
1557 int ret = 1;
1558
72cf2d4f 1559 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
a672b469
AL
1560 if (se->save_live_state == NULL)
1561 continue;
1562
1563 /* Section type */
1564 qemu_put_byte(f, QEMU_VM_SECTION_PART);
1565 qemu_put_be32(f, se->section_id);
1566
90697be8
JK
1567 ret = se->save_live_state(mon, f, QEMU_VM_SECTION_PART, se->opaque);
1568 if (!ret) {
1569 /* Do not proceed to the next vmstate before this one reported
1570 completion of the current stage. This serializes the migration
1571 and reduces the probability that a faster changing state is
1572 synchronized over and over again. */
1573 break;
1574 }
a672b469 1575 }
39346385
JQ
1576 if (ret != 0) {
1577 return ret;
1578 }
1579 ret = qemu_file_has_error(f);
1580 if (ret != 0) {
f327aa0c 1581 qemu_savevm_state_cancel(mon, f);
4ec7fcc7 1582 }
39346385 1583 return ret;
a672b469
AL
1584}
1585
f327aa0c 1586int qemu_savevm_state_complete(Monitor *mon, QEMUFile *f)
a672b469
AL
1587{
1588 SaveStateEntry *se;
1589
ea375f9a
JK
1590 cpu_synchronize_all_states();
1591
72cf2d4f 1592 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
a672b469
AL
1593 if (se->save_live_state == NULL)
1594 continue;
1595
1596 /* Section type */
1597 qemu_put_byte(f, QEMU_VM_SECTION_END);
1598 qemu_put_be32(f, se->section_id);
1599
f327aa0c 1600 se->save_live_state(mon, f, QEMU_VM_SECTION_END, se->opaque);
a672b469
AL
1601 }
1602
72cf2d4f 1603 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
a672b469
AL
1604 int len;
1605
9ed7d6ae 1606 if (se->save_state == NULL && se->vmsd == NULL)
a672b469
AL
1607 continue;
1608
1609 /* Section type */
1610 qemu_put_byte(f, QEMU_VM_SECTION_FULL);
1611 qemu_put_be32(f, se->section_id);
1612
1613 /* ID string */
1614 len = strlen(se->idstr);
1615 qemu_put_byte(f, len);
1616 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
1617
1618 qemu_put_be32(f, se->instance_id);
1619 qemu_put_be32(f, se->version_id);
1620
dc912121 1621 vmstate_save(f, se);
a672b469
AL
1622 }
1623
1624 qemu_put_byte(f, QEMU_VM_EOF);
1625
39346385 1626 return qemu_file_has_error(f);
a672b469
AL
1627}
1628
f327aa0c 1629void qemu_savevm_state_cancel(Monitor *mon, QEMUFile *f)
4ec7fcc7
JK
1630{
1631 SaveStateEntry *se;
1632
1633 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1634 if (se->save_live_state) {
f327aa0c 1635 se->save_live_state(mon, f, -1, se->opaque);
4ec7fcc7
JK
1636 }
1637 }
1638}
1639
f327aa0c 1640static int qemu_savevm_state(Monitor *mon, QEMUFile *f)
a672b469
AL
1641{
1642 int saved_vm_running;
1643 int ret;
1644
1354869c 1645 saved_vm_running = runstate_is_running();
0461d5a6 1646 vm_stop(RUN_STATE_SAVE_VM);
a672b469 1647
dc912121
AW
1648 if (qemu_savevm_state_blocked(mon)) {
1649 ret = -EINVAL;
1650 goto out;
1651 }
1652
f327aa0c 1653 ret = qemu_savevm_state_begin(mon, f, 0, 0);
a672b469
AL
1654 if (ret < 0)
1655 goto out;
1656
1657 do {
f327aa0c 1658 ret = qemu_savevm_state_iterate(mon, f);
a672b469
AL
1659 if (ret < 0)
1660 goto out;
1661 } while (ret == 0);
1662
f327aa0c 1663 ret = qemu_savevm_state_complete(mon, f);
a672b469
AL
1664
1665out:
39346385
JQ
1666 if (ret == 0) {
1667 ret = qemu_file_has_error(f);
1668 }
a672b469
AL
1669
1670 if (!ret && saved_vm_running)
1671 vm_start();
1672
1673 return ret;
1674}
1675
1676static SaveStateEntry *find_se(const char *idstr, int instance_id)
1677{
1678 SaveStateEntry *se;
1679
72cf2d4f 1680 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
a672b469 1681 if (!strcmp(se->idstr, idstr) &&
4d2ffa08
JK
1682 (instance_id == se->instance_id ||
1683 instance_id == se->alias_id))
a672b469 1684 return se;
7685ee6a
AW
1685 /* Migrating from an older version? */
1686 if (strstr(se->idstr, idstr) && se->compat) {
1687 if (!strcmp(se->compat->idstr, idstr) &&
1688 (instance_id == se->compat->instance_id ||
1689 instance_id == se->alias_id))
1690 return se;
1691 }
a672b469
AL
1692 }
1693 return NULL;
1694}
1695
811814bd
JQ
1696static const VMStateDescription *vmstate_get_subsection(const VMStateSubsection *sub, char *idstr)
1697{
1698 while(sub && sub->needed) {
1699 if (strcmp(idstr, sub->vmsd->name) == 0) {
1700 return sub->vmsd;
1701 }
1702 sub++;
1703 }
1704 return NULL;
1705}
1706
1707static int vmstate_subsection_load(QEMUFile *f, const VMStateDescription *vmsd,
1708 void *opaque)
1709{
c6380724 1710 while (qemu_peek_byte(f, 0) == QEMU_VM_SUBSECTION) {
811814bd
JQ
1711 char idstr[256];
1712 int ret;
c6380724 1713 uint8_t version_id, len, size;
811814bd
JQ
1714 const VMStateDescription *sub_vmsd;
1715
c6380724
JQ
1716 len = qemu_peek_byte(f, 1);
1717 if (len < strlen(vmsd->name) + 1) {
1718 /* subsection name has be be "section_name/a" */
1719 return 0;
1720 }
1721 size = qemu_peek_buffer(f, (uint8_t *)idstr, len, 2);
1722 if (size != len) {
1723 return 0;
1724 }
1725 idstr[size] = 0;
811814bd 1726
c6380724
JQ
1727 if (strncmp(vmsd->name, idstr, strlen(vmsd->name)) != 0) {
1728 /* it don't have a valid subsection name */
1729 return 0;
1730 }
3da9eebd 1731 sub_vmsd = vmstate_get_subsection(vmsd->subsections, idstr);
811814bd
JQ
1732 if (sub_vmsd == NULL) {
1733 return -ENOENT;
1734 }
c6380724
JQ
1735 qemu_file_skip(f, 1); /* subsection */
1736 qemu_file_skip(f, 1); /* len */
1737 qemu_file_skip(f, len); /* idstr */
1738 version_id = qemu_get_be32(f);
1739
811814bd
JQ
1740 ret = vmstate_load_state(f, sub_vmsd, opaque, version_id);
1741 if (ret) {
1742 return ret;
1743 }
1744 }
1745 return 0;
1746}
1747
1748static void vmstate_subsection_save(QEMUFile *f, const VMStateDescription *vmsd,
1749 void *opaque)
1750{
1751 const VMStateSubsection *sub = vmsd->subsections;
1752
1753 while (sub && sub->needed) {
1754 if (sub->needed(opaque)) {
1755 const VMStateDescription *vmsd = sub->vmsd;
1756 uint8_t len;
1757
1758 qemu_put_byte(f, QEMU_VM_SUBSECTION);
1759 len = strlen(vmsd->name);
1760 qemu_put_byte(f, len);
1761 qemu_put_buffer(f, (uint8_t *)vmsd->name, len);
1762 qemu_put_be32(f, vmsd->version_id);
1763 vmstate_save_state(f, vmsd, opaque);
1764 }
1765 sub++;
1766 }
1767}
1768
a672b469 1769typedef struct LoadStateEntry {
72cf2d4f 1770 QLIST_ENTRY(LoadStateEntry) entry;
a672b469
AL
1771 SaveStateEntry *se;
1772 int section_id;
1773 int version_id;
a672b469
AL
1774} LoadStateEntry;
1775
a672b469
AL
1776int qemu_loadvm_state(QEMUFile *f)
1777{
72cf2d4f
BS
1778 QLIST_HEAD(, LoadStateEntry) loadvm_handlers =
1779 QLIST_HEAD_INITIALIZER(loadvm_handlers);
f4dbb8dd 1780 LoadStateEntry *le, *new_le;
a672b469
AL
1781 uint8_t section_type;
1782 unsigned int v;
1783 int ret;
1784
dc912121
AW
1785 if (qemu_savevm_state_blocked(default_mon)) {
1786 return -EINVAL;
1787 }
1788
a672b469
AL
1789 v = qemu_get_be32(f);
1790 if (v != QEMU_VM_FILE_MAGIC)
1791 return -EINVAL;
1792
1793 v = qemu_get_be32(f);
bbfe1408
JQ
1794 if (v == QEMU_VM_FILE_VERSION_COMPAT) {
1795 fprintf(stderr, "SaveVM v2 format is obsolete and don't work anymore\n");
1796 return -ENOTSUP;
1797 }
a672b469
AL
1798 if (v != QEMU_VM_FILE_VERSION)
1799 return -ENOTSUP;
1800
1801 while ((section_type = qemu_get_byte(f)) != QEMU_VM_EOF) {
1802 uint32_t instance_id, version_id, section_id;
a672b469
AL
1803 SaveStateEntry *se;
1804 char idstr[257];
1805 int len;
1806
1807 switch (section_type) {
1808 case QEMU_VM_SECTION_START:
1809 case QEMU_VM_SECTION_FULL:
1810 /* Read section start */
1811 section_id = qemu_get_be32(f);
1812 len = qemu_get_byte(f);
1813 qemu_get_buffer(f, (uint8_t *)idstr, len);
1814 idstr[len] = 0;
1815 instance_id = qemu_get_be32(f);
1816 version_id = qemu_get_be32(f);
1817
1818 /* Find savevm section */
1819 se = find_se(idstr, instance_id);
1820 if (se == NULL) {
1821 fprintf(stderr, "Unknown savevm section or instance '%s' %d\n", idstr, instance_id);
1822 ret = -EINVAL;
1823 goto out;
1824 }
1825
1826 /* Validate version */
1827 if (version_id > se->version_id) {
1828 fprintf(stderr, "savevm: unsupported version %d for '%s' v%d\n",
1829 version_id, idstr, se->version_id);
1830 ret = -EINVAL;
1831 goto out;
1832 }
1833
1834 /* Add entry */
7267c094 1835 le = g_malloc0(sizeof(*le));
a672b469
AL
1836
1837 le->se = se;
1838 le->section_id = section_id;
1839 le->version_id = version_id;
72cf2d4f 1840 QLIST_INSERT_HEAD(&loadvm_handlers, le, entry);
a672b469 1841
4082be4d 1842 ret = vmstate_load(f, le->se, le->version_id);
b5a22e4a
JQ
1843 if (ret < 0) {
1844 fprintf(stderr, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
1845 instance_id, idstr);
1846 goto out;
1847 }
a672b469
AL
1848 break;
1849 case QEMU_VM_SECTION_PART:
1850 case QEMU_VM_SECTION_END:
1851 section_id = qemu_get_be32(f);
1852
72cf2d4f 1853 QLIST_FOREACH(le, &loadvm_handlers, entry) {
f4dbb8dd
JQ
1854 if (le->section_id == section_id) {
1855 break;
1856 }
1857 }
a672b469
AL
1858 if (le == NULL) {
1859 fprintf(stderr, "Unknown savevm section %d\n", section_id);
1860 ret = -EINVAL;
1861 goto out;
1862 }
1863
4082be4d 1864 ret = vmstate_load(f, le->se, le->version_id);
b5a22e4a
JQ
1865 if (ret < 0) {
1866 fprintf(stderr, "qemu: warning: error while loading state section id %d\n",
1867 section_id);
1868 goto out;
1869 }
a672b469
AL
1870 break;
1871 default:
1872 fprintf(stderr, "Unknown savevm section type %d\n", section_type);
1873 ret = -EINVAL;
1874 goto out;
1875 }
1876 }
1877
ea375f9a
JK
1878 cpu_synchronize_all_post_init();
1879
a672b469
AL
1880 ret = 0;
1881
1882out:
72cf2d4f
BS
1883 QLIST_FOREACH_SAFE(le, &loadvm_handlers, entry, new_le) {
1884 QLIST_REMOVE(le, entry);
7267c094 1885 g_free(le);
a672b469
AL
1886 }
1887
1888 if (qemu_file_has_error(f))
1889 ret = -EIO;
1890
1891 return ret;
1892}
1893
a672b469
AL
1894static int bdrv_snapshot_find(BlockDriverState *bs, QEMUSnapshotInfo *sn_info,
1895 const char *name)
1896{
1897 QEMUSnapshotInfo *sn_tab, *sn;
1898 int nb_sns, i, ret;
1899
1900 ret = -ENOENT;
1901 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
1902 if (nb_sns < 0)
1903 return ret;
1904 for(i = 0; i < nb_sns; i++) {
1905 sn = &sn_tab[i];
1906 if (!strcmp(sn->id_str, name) || !strcmp(sn->name, name)) {
1907 *sn_info = *sn;
1908 ret = 0;
1909 break;
1910 }
1911 }
7267c094 1912 g_free(sn_tab);
a672b469
AL
1913 return ret;
1914}
1915
cb499fb2
KW
1916/*
1917 * Deletes snapshots of a given name in all opened images.
1918 */
1919static int del_existing_snapshots(Monitor *mon, const char *name)
1920{
1921 BlockDriverState *bs;
cb499fb2
KW
1922 QEMUSnapshotInfo sn1, *snapshot = &sn1;
1923 int ret;
1924
dbc13590
MA
1925 bs = NULL;
1926 while ((bs = bdrv_next(bs))) {
cb499fb2
KW
1927 if (bdrv_can_snapshot(bs) &&
1928 bdrv_snapshot_find(bs, snapshot, name) >= 0)
1929 {
1930 ret = bdrv_snapshot_delete(bs, name);
1931 if (ret < 0) {
1932 monitor_printf(mon,
1933 "Error while deleting snapshot on '%s'\n",
1934 bdrv_get_device_name(bs));
1935 return -1;
1936 }
1937 }
1938 }
1939
1940 return 0;
1941}
1942
d54908a5 1943void do_savevm(Monitor *mon, const QDict *qdict)
a672b469
AL
1944{
1945 BlockDriverState *bs, *bs1;
1946 QEMUSnapshotInfo sn1, *sn = &sn1, old_sn1, *old_sn = &old_sn1;
cb499fb2 1947 int ret;
a672b469
AL
1948 QEMUFile *f;
1949 int saved_vm_running;
2d22b18f 1950 uint32_t vm_state_size;
a672b469
AL
1951#ifdef _WIN32
1952 struct _timeb tb;
7d631a11 1953 struct tm *ptm;
a672b469
AL
1954#else
1955 struct timeval tv;
7d631a11 1956 struct tm tm;
a672b469 1957#endif
d54908a5 1958 const char *name = qdict_get_try_str(qdict, "name");
a672b469 1959
feeee5ac 1960 /* Verify if there is a device that doesn't support snapshots and is writable */
dbc13590
MA
1961 bs = NULL;
1962 while ((bs = bdrv_next(bs))) {
feeee5ac 1963
07b70bfb 1964 if (!bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {
feeee5ac
MDCF
1965 continue;
1966 }
1967
1968 if (!bdrv_can_snapshot(bs)) {
1969 monitor_printf(mon, "Device '%s' is writable but does not support snapshots.\n",
1970 bdrv_get_device_name(bs));
1971 return;
1972 }
1973 }
1974
f9092b10 1975 bs = bdrv_snapshots();
a672b469 1976 if (!bs) {
376253ec 1977 monitor_printf(mon, "No block device can accept snapshots\n");
a672b469
AL
1978 return;
1979 }
a672b469 1980
1354869c 1981 saved_vm_running = runstate_is_running();
0461d5a6 1982 vm_stop(RUN_STATE_SAVE_VM);
a672b469 1983
cb499fb2 1984 memset(sn, 0, sizeof(*sn));
a672b469
AL
1985
1986 /* fill auxiliary fields */
1987#ifdef _WIN32
1988 _ftime(&tb);
1989 sn->date_sec = tb.time;
1990 sn->date_nsec = tb.millitm * 1000000;
1991#else
1992 gettimeofday(&tv, NULL);
1993 sn->date_sec = tv.tv_sec;
1994 sn->date_nsec = tv.tv_usec * 1000;
1995#endif
74475455 1996 sn->vm_clock_nsec = qemu_get_clock_ns(vm_clock);
a672b469 1997
7d631a11
MDCF
1998 if (name) {
1999 ret = bdrv_snapshot_find(bs, old_sn, name);
2000 if (ret >= 0) {
2001 pstrcpy(sn->name, sizeof(sn->name), old_sn->name);
2002 pstrcpy(sn->id_str, sizeof(sn->id_str), old_sn->id_str);
2003 } else {
2004 pstrcpy(sn->name, sizeof(sn->name), name);
2005 }
2006 } else {
2007#ifdef _WIN32
2008 ptm = localtime(&tb.time);
2009 strftime(sn->name, sizeof(sn->name), "vm-%Y%m%d%H%M%S", ptm);
2010#else
d7d9b528
BS
2011 /* cast below needed for OpenBSD where tv_sec is still 'long' */
2012 localtime_r((const time_t *)&tv.tv_sec, &tm);
7d631a11
MDCF
2013 strftime(sn->name, sizeof(sn->name), "vm-%Y%m%d%H%M%S", &tm);
2014#endif
2015 }
2016
cb499fb2 2017 /* Delete old snapshots of the same name */
f139a412 2018 if (name && del_existing_snapshots(mon, name) < 0) {
cb499fb2
KW
2019 goto the_end;
2020 }
2021
a672b469 2022 /* save the VM state */
45566e9c 2023 f = qemu_fopen_bdrv(bs, 1);
a672b469 2024 if (!f) {
376253ec 2025 monitor_printf(mon, "Could not open VM state file\n");
a672b469
AL
2026 goto the_end;
2027 }
f327aa0c 2028 ret = qemu_savevm_state(mon, f);
2d22b18f 2029 vm_state_size = qemu_ftell(f);
a672b469
AL
2030 qemu_fclose(f);
2031 if (ret < 0) {
376253ec 2032 monitor_printf(mon, "Error %d while writing VM\n", ret);
a672b469
AL
2033 goto the_end;
2034 }
2035
2036 /* create the snapshots */
2037
dbc13590
MA
2038 bs1 = NULL;
2039 while ((bs1 = bdrv_next(bs1))) {
feeee5ac 2040 if (bdrv_can_snapshot(bs1)) {
2d22b18f
AL
2041 /* Write VM state size only to the image that contains the state */
2042 sn->vm_state_size = (bs == bs1 ? vm_state_size : 0);
a672b469
AL
2043 ret = bdrv_snapshot_create(bs1, sn);
2044 if (ret < 0) {
376253ec
AL
2045 monitor_printf(mon, "Error while creating snapshot on '%s'\n",
2046 bdrv_get_device_name(bs1));
a672b469
AL
2047 }
2048 }
2049 }
2050
2051 the_end:
2052 if (saved_vm_running)
2053 vm_start();
2054}
2055
03cd4655 2056int load_vmstate(const char *name)
a672b469 2057{
f0aa7a8b 2058 BlockDriverState *bs, *bs_vm_state;
2d22b18f 2059 QEMUSnapshotInfo sn;
a672b469 2060 QEMUFile *f;
751c6a17 2061 int ret;
a672b469 2062
f0aa7a8b
MDCF
2063 bs_vm_state = bdrv_snapshots();
2064 if (!bs_vm_state) {
2065 error_report("No block device supports snapshots");
2066 return -ENOTSUP;
2067 }
2068
2069 /* Don't even try to load empty VM states */
2070 ret = bdrv_snapshot_find(bs_vm_state, &sn, name);
2071 if (ret < 0) {
2072 return ret;
2073 } else if (sn.vm_state_size == 0) {
e11480db
KW
2074 error_report("This is a disk-only snapshot. Revert to it offline "
2075 "using qemu-img.");
f0aa7a8b
MDCF
2076 return -EINVAL;
2077 }
2078
2079 /* Verify if there is any device that doesn't support snapshots and is
2080 writable and check if the requested snapshot is available too. */
dbc13590
MA
2081 bs = NULL;
2082 while ((bs = bdrv_next(bs))) {
feeee5ac 2083
07b70bfb 2084 if (!bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {
feeee5ac
MDCF
2085 continue;
2086 }
2087
2088 if (!bdrv_can_snapshot(bs)) {
2089 error_report("Device '%s' is writable but does not support snapshots.",
2090 bdrv_get_device_name(bs));
2091 return -ENOTSUP;
2092 }
feeee5ac 2093
f0aa7a8b
MDCF
2094 ret = bdrv_snapshot_find(bs, &sn, name);
2095 if (ret < 0) {
2096 error_report("Device '%s' does not have the requested snapshot '%s'",
2097 bdrv_get_device_name(bs), name);
2098 return ret;
2099 }
a672b469
AL
2100 }
2101
2102 /* Flush all IO requests so they don't interfere with the new state. */
2103 qemu_aio_flush();
2104
f0aa7a8b
MDCF
2105 bs = NULL;
2106 while ((bs = bdrv_next(bs))) {
2107 if (bdrv_can_snapshot(bs)) {
2108 ret = bdrv_snapshot_goto(bs, name);
a672b469 2109 if (ret < 0) {
f0aa7a8b
MDCF
2110 error_report("Error %d while activating snapshot '%s' on '%s'",
2111 ret, name, bdrv_get_device_name(bs));
2112 return ret;
a672b469
AL
2113 }
2114 }
2115 }
2116
a672b469 2117 /* restore the VM state */
f0aa7a8b 2118 f = qemu_fopen_bdrv(bs_vm_state, 0);
a672b469 2119 if (!f) {
1ecda02b 2120 error_report("Could not open VM state file");
05f2401e 2121 return -EINVAL;
a672b469 2122 }
f0aa7a8b 2123
5a8a49d7 2124 qemu_system_reset(VMRESET_SILENT);
a672b469 2125 ret = qemu_loadvm_state(f);
f0aa7a8b 2126
a672b469
AL
2127 qemu_fclose(f);
2128 if (ret < 0) {
1ecda02b 2129 error_report("Error %d while loading VM state", ret);
05f2401e 2130 return ret;
a672b469 2131 }
f0aa7a8b 2132
05f2401e 2133 return 0;
7b630349
JQ
2134}
2135
d54908a5 2136void do_delvm(Monitor *mon, const QDict *qdict)
a672b469
AL
2137{
2138 BlockDriverState *bs, *bs1;
751c6a17 2139 int ret;
d54908a5 2140 const char *name = qdict_get_str(qdict, "name");
a672b469 2141
f9092b10 2142 bs = bdrv_snapshots();
a672b469 2143 if (!bs) {
376253ec 2144 monitor_printf(mon, "No block device supports snapshots\n");
a672b469
AL
2145 return;
2146 }
2147
dbc13590
MA
2148 bs1 = NULL;
2149 while ((bs1 = bdrv_next(bs1))) {
feeee5ac 2150 if (bdrv_can_snapshot(bs1)) {
a672b469
AL
2151 ret = bdrv_snapshot_delete(bs1, name);
2152 if (ret < 0) {
2153 if (ret == -ENOTSUP)
376253ec
AL
2154 monitor_printf(mon,
2155 "Snapshots not supported on device '%s'\n",
2156 bdrv_get_device_name(bs1));
a672b469 2157 else
376253ec
AL
2158 monitor_printf(mon, "Error %d while deleting snapshot on "
2159 "'%s'\n", ret, bdrv_get_device_name(bs1));
a672b469
AL
2160 }
2161 }
2162 }
2163}
2164
376253ec 2165void do_info_snapshots(Monitor *mon)
a672b469
AL
2166{
2167 BlockDriverState *bs, *bs1;
f9209915
MDCF
2168 QEMUSnapshotInfo *sn_tab, *sn, s, *sn_info = &s;
2169 int nb_sns, i, ret, available;
2170 int total;
2171 int *available_snapshots;
a672b469
AL
2172 char buf[256];
2173
f9092b10 2174 bs = bdrv_snapshots();
a672b469 2175 if (!bs) {
376253ec 2176 monitor_printf(mon, "No available block device supports snapshots\n");
a672b469
AL
2177 return;
2178 }
a672b469
AL
2179
2180 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
2181 if (nb_sns < 0) {
376253ec 2182 monitor_printf(mon, "bdrv_snapshot_list: error %d\n", nb_sns);
a672b469
AL
2183 return;
2184 }
f9209915
MDCF
2185
2186 if (nb_sns == 0) {
2187 monitor_printf(mon, "There is no snapshot available.\n");
2188 return;
2189 }
2190
7267c094 2191 available_snapshots = g_malloc0(sizeof(int) * nb_sns);
f9209915
MDCF
2192 total = 0;
2193 for (i = 0; i < nb_sns; i++) {
a672b469 2194 sn = &sn_tab[i];
f9209915
MDCF
2195 available = 1;
2196 bs1 = NULL;
2197
2198 while ((bs1 = bdrv_next(bs1))) {
2199 if (bdrv_can_snapshot(bs1) && bs1 != bs) {
2200 ret = bdrv_snapshot_find(bs1, sn_info, sn->id_str);
2201 if (ret < 0) {
2202 available = 0;
2203 break;
2204 }
2205 }
2206 }
2207
2208 if (available) {
2209 available_snapshots[total] = i;
2210 total++;
2211 }
a672b469 2212 }
f9209915
MDCF
2213
2214 if (total > 0) {
2215 monitor_printf(mon, "%s\n", bdrv_snapshot_dump(buf, sizeof(buf), NULL));
2216 for (i = 0; i < total; i++) {
2217 sn = &sn_tab[available_snapshots[i]];
2218 monitor_printf(mon, "%s\n", bdrv_snapshot_dump(buf, sizeof(buf), sn));
2219 }
2220 } else {
2221 monitor_printf(mon, "There is no suitable snapshot available\n");
2222 }
2223
7267c094
AL
2224 g_free(sn_tab);
2225 g_free(available_snapshots);
f9209915 2226
a672b469 2227}