Merge commit '70783b9c9be31e98421f17327a1127021abae672' into upstream-merge
[qemu-kvm/markmc.git] / savevm.c
blob1d77d821a0346ec8f818afc4cbb9a7dccbd87416
1 /*
2 * QEMU System Emulator
4 * Copyright (c) 2003-2008 Fabrice Bellard
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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.
24 #include <unistd.h>
25 #include <fcntl.h>
26 #include <signal.h>
27 #include <time.h>
28 #include <errno.h>
29 #include <sys/time.h>
30 #include <zlib.h>
32 /* Needed early for CONFIG_BSD etc. */
33 #include "config-host.h"
35 #ifndef _WIN32
36 #include <sys/times.h>
37 #include <sys/wait.h>
38 #include <termios.h>
39 #include <sys/mman.h>
40 #include <sys/ioctl.h>
41 #include <sys/resource.h>
42 #include <sys/socket.h>
43 #include <netinet/in.h>
44 #include <net/if.h>
45 #include <arpa/inet.h>
46 #include <dirent.h>
47 #include <netdb.h>
48 #include <sys/select.h>
49 #ifdef CONFIG_BSD
50 #include <sys/stat.h>
51 #if defined(__FreeBSD__) || defined(__DragonFly__)
52 #include <libutil.h>
53 #else
54 #include <util.h>
55 #endif
56 #elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
57 #include <freebsd/stdlib.h>
58 #else
59 #ifdef __linux__
60 #include <pty.h>
61 #include <malloc.h>
62 #include <linux/rtc.h>
63 #endif
64 #endif
65 #endif
67 #ifdef _WIN32
68 #include <windows.h>
69 #include <malloc.h>
70 #include <sys/timeb.h>
71 #include <mmsystem.h>
72 #define getopt_long_only getopt_long
73 #define memalign(align, size) malloc(size)
74 #endif
76 #include "qemu-common.h"
77 #include "hw/hw.h"
78 #include "net.h"
79 #include "monitor.h"
80 #include "sysemu.h"
81 #include "qemu-timer.h"
82 #include "qemu-char.h"
83 #include "block.h"
84 #include "audio/audio.h"
85 #include "migration.h"
86 #include "qemu_socket.h"
87 #include "qemu-queue.h"
89 /* point to the block driver where the snapshots are managed */
90 static BlockDriverState *bs_snapshots;
92 #define SELF_ANNOUNCE_ROUNDS 5
94 #ifndef ETH_P_RARP
95 #define ETH_P_RARP 0x0835
96 #endif
97 #define ARP_HTYPE_ETH 0x0001
98 #define ARP_PTYPE_IP 0x0800
99 #define ARP_OP_REQUEST_REV 0x3
101 static int announce_self_create(uint8_t *buf,
102 uint8_t *mac_addr)
104 /* Ethernet header. */
105 memset(buf, 0xff, 6); /* destination MAC addr */
106 memcpy(buf + 6, mac_addr, 6); /* source MAC addr */
107 *(uint16_t *)(buf + 12) = htons(ETH_P_RARP); /* ethertype */
109 /* RARP header. */
110 *(uint16_t *)(buf + 14) = htons(ARP_HTYPE_ETH); /* hardware addr space */
111 *(uint16_t *)(buf + 16) = htons(ARP_PTYPE_IP); /* protocol addr space */
112 *(buf + 18) = 6; /* hardware addr length (ethernet) */
113 *(buf + 19) = 4; /* protocol addr length (IPv4) */
114 *(uint16_t *)(buf + 20) = htons(ARP_OP_REQUEST_REV); /* opcode */
115 memcpy(buf + 22, mac_addr, 6); /* source hw addr */
116 memset(buf + 28, 0x00, 4); /* source protocol addr */
117 memcpy(buf + 32, mac_addr, 6); /* target hw addr */
118 memset(buf + 38, 0x00, 4); /* target protocol addr */
120 /* Padding to get up to 60 bytes (ethernet min packet size, minus FCS). */
121 memset(buf + 42, 0x00, 18);
123 return 60; /* len (FCS will be added by hardware) */
126 static void qemu_announce_self_once(void *opaque)
128 int i, len;
129 VLANState *vlan;
130 VLANClientState *vc;
131 uint8_t buf[60];
132 static int count = SELF_ANNOUNCE_ROUNDS;
133 QEMUTimer *timer = *(QEMUTimer **)opaque;
135 for (i = 0; i < MAX_NICS; i++) {
136 if (!nd_table[i].used)
137 continue;
138 len = announce_self_create(buf, nd_table[i].macaddr);
139 vlan = nd_table[i].vlan;
140 QTAILQ_FOREACH(vc, &vlan->clients, next) {
141 qemu_send_packet_raw(vc, buf, len);
144 if (--count) {
145 /* delay 50ms, 150ms, 250ms, ... */
146 qemu_mod_timer(timer, qemu_get_clock(rt_clock) +
147 50 + (SELF_ANNOUNCE_ROUNDS - count - 1) * 100);
148 } else {
149 qemu_del_timer(timer);
150 qemu_free_timer(timer);
154 void qemu_announce_self(void)
156 static QEMUTimer *timer;
157 timer = qemu_new_timer(rt_clock, qemu_announce_self_once, &timer);
158 qemu_announce_self_once(&timer);
161 /***********************************************************/
162 /* savevm/loadvm support */
164 #define IO_BUF_SIZE 32768
166 struct QEMUFile {
167 QEMUFilePutBufferFunc *put_buffer;
168 QEMUFileGetBufferFunc *get_buffer;
169 QEMUFileCloseFunc *close;
170 QEMUFileRateLimit *rate_limit;
171 QEMUFileSetRateLimit *set_rate_limit;
172 void *opaque;
173 int is_write;
175 int64_t buf_offset; /* start of buffer when writing, end of buffer
176 when reading */
177 int buf_index;
178 int buf_size; /* 0 when writing */
179 uint8_t buf[IO_BUF_SIZE];
181 int has_error;
184 typedef struct QEMUFileStdio
186 FILE *stdio_file;
187 QEMUFile *file;
188 } QEMUFileStdio;
190 typedef struct QEMUFileSocket
192 int fd;
193 QEMUFile *file;
194 } QEMUFileSocket;
196 static int socket_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
198 QEMUFileSocket *s = opaque;
199 ssize_t len;
201 do {
202 len = recv(s->fd, (void *)buf, size, 0);
203 } while (len == -1 && socket_error() == EINTR);
205 if (len == -1)
206 len = -socket_error();
208 return len;
211 static int socket_close(void *opaque)
213 QEMUFileSocket *s = opaque;
214 qemu_free(s);
215 return 0;
218 static int stdio_put_buffer(void *opaque, const uint8_t *buf, int64_t pos, int size)
220 QEMUFileStdio *s = opaque;
221 return fwrite(buf, 1, size, s->stdio_file);
224 static int stdio_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
226 QEMUFileStdio *s = opaque;
227 FILE *fp = s->stdio_file;
228 int bytes;
230 do {
231 clearerr(fp);
232 bytes = fread(buf, 1, size, fp);
233 } while ((bytes == 0) && ferror(fp) && (errno == EINTR));
234 return bytes;
237 static int stdio_pclose(void *opaque)
239 QEMUFileStdio *s = opaque;
240 pclose(s->stdio_file);
241 qemu_free(s);
242 return 0;
245 static int stdio_fclose(void *opaque)
247 QEMUFileStdio *s = opaque;
248 fclose(s->stdio_file);
249 qemu_free(s);
250 return 0;
253 QEMUFile *qemu_popen(FILE *stdio_file, const char *mode)
255 QEMUFileStdio *s;
257 if (stdio_file == NULL || mode == NULL || (mode[0] != 'r' && mode[0] != 'w') || mode[1] != 0) {
258 fprintf(stderr, "qemu_popen: Argument validity check failed\n");
259 return NULL;
262 s = qemu_mallocz(sizeof(QEMUFileStdio));
264 s->stdio_file = stdio_file;
266 if(mode[0] == 'r') {
267 s->file = qemu_fopen_ops(s, NULL, stdio_get_buffer, stdio_pclose, NULL, NULL);
268 } else {
269 s->file = qemu_fopen_ops(s, stdio_put_buffer, NULL, stdio_pclose, NULL, NULL);
271 return s->file;
274 QEMUFile *qemu_popen_cmd(const char *command, const char *mode)
276 FILE *popen_file;
278 popen_file = popen(command, mode);
279 if(popen_file == NULL) {
280 return NULL;
283 return qemu_popen(popen_file, mode);
286 int qemu_stdio_fd(QEMUFile *f)
288 QEMUFileStdio *p;
289 int fd;
291 p = (QEMUFileStdio *)f->opaque;
292 fd = fileno(p->stdio_file);
294 return fd;
297 QEMUFile *qemu_fdopen(int fd, const char *mode)
299 QEMUFileStdio *s;
301 if (mode == NULL ||
302 (mode[0] != 'r' && mode[0] != 'w') ||
303 mode[1] != 'b' || mode[2] != 0) {
304 fprintf(stderr, "qemu_fdopen: Argument validity check failed\n");
305 return NULL;
308 s = qemu_mallocz(sizeof(QEMUFileStdio));
309 s->stdio_file = fdopen(fd, mode);
310 if (!s->stdio_file)
311 goto fail;
313 if(mode[0] == 'r') {
314 s->file = qemu_fopen_ops(s, NULL, stdio_get_buffer, stdio_fclose, NULL, NULL);
315 } else {
316 s->file = qemu_fopen_ops(s, stdio_put_buffer, NULL, stdio_fclose, NULL, NULL);
318 return s->file;
320 fail:
321 qemu_free(s);
322 return NULL;
325 QEMUFile *qemu_fopen_socket(int fd)
327 QEMUFileSocket *s = qemu_mallocz(sizeof(QEMUFileSocket));
329 s->fd = fd;
330 s->file = qemu_fopen_ops(s, NULL, socket_get_buffer, socket_close, NULL, NULL);
331 return s->file;
334 static int file_put_buffer(void *opaque, const uint8_t *buf,
335 int64_t pos, int size)
337 QEMUFileStdio *s = opaque;
338 fseek(s->stdio_file, pos, SEEK_SET);
339 fwrite(buf, 1, size, s->stdio_file);
340 return size;
343 static int file_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
345 QEMUFileStdio *s = opaque;
346 fseek(s->stdio_file, pos, SEEK_SET);
347 return fread(buf, 1, size, s->stdio_file);
350 QEMUFile *qemu_fopen(const char *filename, const char *mode)
352 QEMUFileStdio *s;
354 if (mode == NULL ||
355 (mode[0] != 'r' && mode[0] != 'w') ||
356 mode[1] != 'b' || mode[2] != 0) {
357 fprintf(stderr, "qemu_fdopen: Argument validity check failed\n");
358 return NULL;
361 s = qemu_mallocz(sizeof(QEMUFileStdio));
363 s->stdio_file = fopen(filename, mode);
364 if (!s->stdio_file)
365 goto fail;
367 if(mode[0] == 'w') {
368 s->file = qemu_fopen_ops(s, file_put_buffer, NULL, stdio_fclose, NULL, NULL);
369 } else {
370 s->file = qemu_fopen_ops(s, NULL, file_get_buffer, stdio_fclose, NULL, NULL);
372 return s->file;
373 fail:
374 qemu_free(s);
375 return NULL;
378 static int block_put_buffer(void *opaque, const uint8_t *buf,
379 int64_t pos, int size)
381 bdrv_save_vmstate(opaque, buf, pos, size);
382 return size;
385 static int block_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
387 return bdrv_load_vmstate(opaque, buf, pos, size);
390 static int bdrv_fclose(void *opaque)
392 return 0;
395 static QEMUFile *qemu_fopen_bdrv(BlockDriverState *bs, int is_writable)
397 if (is_writable)
398 return qemu_fopen_ops(bs, block_put_buffer, NULL, bdrv_fclose, NULL, NULL);
399 return qemu_fopen_ops(bs, NULL, block_get_buffer, bdrv_fclose, NULL, NULL);
402 QEMUFile *qemu_fopen_ops(void *opaque, QEMUFilePutBufferFunc *put_buffer,
403 QEMUFileGetBufferFunc *get_buffer,
404 QEMUFileCloseFunc *close,
405 QEMUFileRateLimit *rate_limit,
406 QEMUFileSetRateLimit *set_rate_limit)
408 QEMUFile *f;
410 f = qemu_mallocz(sizeof(QEMUFile));
412 f->opaque = opaque;
413 f->put_buffer = put_buffer;
414 f->get_buffer = get_buffer;
415 f->close = close;
416 f->rate_limit = rate_limit;
417 f->set_rate_limit = set_rate_limit;
418 f->is_write = 0;
420 return f;
423 int qemu_file_has_error(QEMUFile *f)
425 return f->has_error;
428 void qemu_file_set_error(QEMUFile *f)
430 f->has_error = 1;
433 void qemu_fflush(QEMUFile *f)
435 if (!f->put_buffer)
436 return;
438 if (f->is_write && f->buf_index > 0) {
439 int len;
441 len = f->put_buffer(f->opaque, f->buf, f->buf_offset, f->buf_index);
442 if (len > 0)
443 f->buf_offset += f->buf_index;
444 else
445 f->has_error = 1;
446 f->buf_index = 0;
450 static void qemu_fill_buffer(QEMUFile *f)
452 int len;
454 if (!f->get_buffer)
455 return;
457 if (f->is_write)
458 abort();
460 len = f->get_buffer(f->opaque, f->buf, f->buf_offset, IO_BUF_SIZE);
461 if (len > 0) {
462 f->buf_index = 0;
463 f->buf_size = len;
464 f->buf_offset += len;
465 } else if (len != -EAGAIN)
466 f->has_error = 1;
469 int qemu_fclose(QEMUFile *f)
471 int ret = 0;
472 qemu_fflush(f);
473 if (f->close)
474 ret = f->close(f->opaque);
475 qemu_free(f);
476 return ret;
479 void qemu_file_put_notify(QEMUFile *f)
481 f->put_buffer(f->opaque, NULL, 0, 0);
484 void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size)
486 int l;
488 if (!f->has_error && f->is_write == 0 && f->buf_index > 0) {
489 fprintf(stderr,
490 "Attempted to write to buffer while read buffer is not empty\n");
491 abort();
494 while (!f->has_error && size > 0) {
495 l = IO_BUF_SIZE - f->buf_index;
496 if (l > size)
497 l = size;
498 memcpy(f->buf + f->buf_index, buf, l);
499 f->is_write = 1;
500 f->buf_index += l;
501 buf += l;
502 size -= l;
503 if (f->buf_index >= IO_BUF_SIZE)
504 qemu_fflush(f);
508 void qemu_put_byte(QEMUFile *f, int v)
510 if (!f->has_error && f->is_write == 0 && f->buf_index > 0) {
511 fprintf(stderr,
512 "Attempted to write to buffer while read buffer is not empty\n");
513 abort();
516 f->buf[f->buf_index++] = v;
517 f->is_write = 1;
518 if (f->buf_index >= IO_BUF_SIZE)
519 qemu_fflush(f);
522 int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size1)
524 int size, l;
526 if (f->is_write)
527 abort();
529 size = size1;
530 while (size > 0) {
531 l = f->buf_size - f->buf_index;
532 if (l == 0) {
533 qemu_fill_buffer(f);
534 l = f->buf_size - f->buf_index;
535 if (l == 0)
536 break;
538 if (l > size)
539 l = size;
540 memcpy(buf, f->buf + f->buf_index, l);
541 f->buf_index += l;
542 buf += l;
543 size -= l;
545 return size1 - size;
548 int qemu_get_byte(QEMUFile *f)
550 if (f->is_write)
551 abort();
553 if (f->buf_index >= f->buf_size) {
554 qemu_fill_buffer(f);
555 if (f->buf_index >= f->buf_size)
556 return 0;
558 return f->buf[f->buf_index++];
561 int64_t qemu_ftell(QEMUFile *f)
563 return f->buf_offset - f->buf_size + f->buf_index;
566 int64_t qemu_fseek(QEMUFile *f, int64_t pos, int whence)
568 if (whence == SEEK_SET) {
569 /* nothing to do */
570 } else if (whence == SEEK_CUR) {
571 pos += qemu_ftell(f);
572 } else {
573 /* SEEK_END not supported */
574 return -1;
576 if (f->put_buffer) {
577 qemu_fflush(f);
578 f->buf_offset = pos;
579 } else {
580 f->buf_offset = pos;
581 f->buf_index = 0;
582 f->buf_size = 0;
584 return pos;
587 int qemu_file_rate_limit(QEMUFile *f)
589 if (f->rate_limit)
590 return f->rate_limit(f->opaque);
592 return 0;
595 size_t qemu_file_set_rate_limit(QEMUFile *f, size_t new_rate)
597 /* any failed or completed migration keeps its state to allow probing of
598 * migration data, but has no associated file anymore */
599 if (f && f->set_rate_limit)
600 return f->set_rate_limit(f->opaque, new_rate);
602 return 0;
605 void qemu_put_be16(QEMUFile *f, unsigned int v)
607 qemu_put_byte(f, v >> 8);
608 qemu_put_byte(f, v);
611 void qemu_put_be32(QEMUFile *f, unsigned int v)
613 qemu_put_byte(f, v >> 24);
614 qemu_put_byte(f, v >> 16);
615 qemu_put_byte(f, v >> 8);
616 qemu_put_byte(f, v);
619 void qemu_put_be64(QEMUFile *f, uint64_t v)
621 qemu_put_be32(f, v >> 32);
622 qemu_put_be32(f, v);
625 unsigned int qemu_get_be16(QEMUFile *f)
627 unsigned int v;
628 v = qemu_get_byte(f) << 8;
629 v |= qemu_get_byte(f);
630 return v;
633 unsigned int qemu_get_be32(QEMUFile *f)
635 unsigned int v;
636 v = qemu_get_byte(f) << 24;
637 v |= qemu_get_byte(f) << 16;
638 v |= qemu_get_byte(f) << 8;
639 v |= qemu_get_byte(f);
640 return v;
643 uint64_t qemu_get_be64(QEMUFile *f)
645 uint64_t v;
646 v = (uint64_t)qemu_get_be32(f) << 32;
647 v |= qemu_get_be32(f);
648 return v;
651 /* 8 bit int */
653 static int get_int8(QEMUFile *f, void *pv, size_t size)
655 int8_t *v = pv;
656 qemu_get_s8s(f, v);
657 return 0;
660 static void put_int8(QEMUFile *f, void *pv, size_t size)
662 int8_t *v = pv;
663 qemu_put_s8s(f, v);
666 const VMStateInfo vmstate_info_int8 = {
667 .name = "int8",
668 .get = get_int8,
669 .put = put_int8,
672 /* 16 bit int */
674 static int get_int16(QEMUFile *f, void *pv, size_t size)
676 int16_t *v = pv;
677 qemu_get_sbe16s(f, v);
678 return 0;
681 static void put_int16(QEMUFile *f, void *pv, size_t size)
683 int16_t *v = pv;
684 qemu_put_sbe16s(f, v);
687 const VMStateInfo vmstate_info_int16 = {
688 .name = "int16",
689 .get = get_int16,
690 .put = put_int16,
693 /* 32 bit int */
695 static int get_int32(QEMUFile *f, void *pv, size_t size)
697 int32_t *v = pv;
698 qemu_get_sbe32s(f, v);
699 return 0;
702 static void put_int32(QEMUFile *f, void *pv, size_t size)
704 int32_t *v = pv;
705 qemu_put_sbe32s(f, v);
708 const VMStateInfo vmstate_info_int32 = {
709 .name = "int32",
710 .get = get_int32,
711 .put = put_int32,
714 /* 32 bit int. See that the received value is the same than the one
715 in the field */
717 static int get_int32_equal(QEMUFile *f, void *pv, size_t size)
719 int32_t *v = pv;
720 int32_t v2;
721 qemu_get_sbe32s(f, &v2);
723 if (*v == v2)
724 return 0;
725 return -EINVAL;
728 const VMStateInfo vmstate_info_int32_equal = {
729 .name = "int32 equal",
730 .get = get_int32_equal,
731 .put = put_int32,
734 /* 32 bit int. See that the received value is the less or the same
735 than the one in the field */
737 static int get_int32_le(QEMUFile *f, void *pv, size_t size)
739 int32_t *old = pv;
740 int32_t new;
741 qemu_get_sbe32s(f, &new);
743 if (*old <= new)
744 return 0;
745 return -EINVAL;
748 const VMStateInfo vmstate_info_int32_le = {
749 .name = "int32 equal",
750 .get = get_int32_le,
751 .put = put_int32,
754 /* 64 bit int */
756 static int get_int64(QEMUFile *f, void *pv, size_t size)
758 int64_t *v = pv;
759 qemu_get_sbe64s(f, v);
760 return 0;
763 static void put_int64(QEMUFile *f, void *pv, size_t size)
765 int64_t *v = pv;
766 qemu_put_sbe64s(f, v);
769 const VMStateInfo vmstate_info_int64 = {
770 .name = "int64",
771 .get = get_int64,
772 .put = put_int64,
775 /* 8 bit unsigned int */
777 static int get_uint8(QEMUFile *f, void *pv, size_t size)
779 uint8_t *v = pv;
780 qemu_get_8s(f, v);
781 return 0;
784 static void put_uint8(QEMUFile *f, void *pv, size_t size)
786 uint8_t *v = pv;
787 qemu_put_8s(f, v);
790 const VMStateInfo vmstate_info_uint8 = {
791 .name = "uint8",
792 .get = get_uint8,
793 .put = put_uint8,
796 /* 16 bit unsigned int */
798 static int get_uint16(QEMUFile *f, void *pv, size_t size)
800 uint16_t *v = pv;
801 qemu_get_be16s(f, v);
802 return 0;
805 static void put_uint16(QEMUFile *f, void *pv, size_t size)
807 uint16_t *v = pv;
808 qemu_put_be16s(f, v);
811 const VMStateInfo vmstate_info_uint16 = {
812 .name = "uint16",
813 .get = get_uint16,
814 .put = put_uint16,
817 /* 32 bit unsigned int */
819 static int get_uint32(QEMUFile *f, void *pv, size_t size)
821 uint32_t *v = pv;
822 qemu_get_be32s(f, v);
823 return 0;
826 static void put_uint32(QEMUFile *f, void *pv, size_t size)
828 uint32_t *v = pv;
829 qemu_put_be32s(f, v);
832 const VMStateInfo vmstate_info_uint32 = {
833 .name = "uint32",
834 .get = get_uint32,
835 .put = put_uint32,
838 /* 64 bit unsigned int */
840 static int get_uint64(QEMUFile *f, void *pv, size_t size)
842 uint64_t *v = pv;
843 qemu_get_be64s(f, v);
844 return 0;
847 static void put_uint64(QEMUFile *f, void *pv, size_t size)
849 uint64_t *v = pv;
850 qemu_put_be64s(f, v);
853 const VMStateInfo vmstate_info_uint64 = {
854 .name = "uint64",
855 .get = get_uint64,
856 .put = put_uint64,
859 /* 64 bit linux kernel unsigned int */
861 #ifdef __linux__
862 static int get_u64(QEMUFile *f, void *pv, size_t size)
864 __u64 *v = pv;
865 qemu_get_be64s(f, (uint64_t *)v);
866 return 0;
869 static void put_u64(QEMUFile *f, void *pv, size_t size)
871 __u64 *v = pv;
872 qemu_put_be64s(f, (uint64_t *)v);
875 const VMStateInfo vmstate_info_u64 = {
876 .name = "__u64",
877 .get = get_u64,
878 .put = put_u64,
880 #endif /* __linux__ */
882 /* 8 bit int. See that the received value is the same than the one
883 in the field */
885 static int get_uint8_equal(QEMUFile *f, void *pv, size_t size)
887 uint8_t *v = pv;
888 uint8_t v2;
889 qemu_get_8s(f, &v2);
891 if (*v == v2)
892 return 0;
893 return -EINVAL;
896 const VMStateInfo vmstate_info_uint8_equal = {
897 .name = "uint8 equal",
898 .get = get_uint8_equal,
899 .put = put_uint8,
902 /* 16 bit unsigned int int. See that the received value is the same than the one
903 in the field */
905 static int get_uint16_equal(QEMUFile *f, void *pv, size_t size)
907 uint16_t *v = pv;
908 uint16_t v2;
909 qemu_get_be16s(f, &v2);
911 if (*v == v2)
912 return 0;
913 return -EINVAL;
916 const VMStateInfo vmstate_info_uint16_equal = {
917 .name = "uint16 equal",
918 .get = get_uint16_equal,
919 .put = put_uint16,
922 /* timers */
924 static int get_timer(QEMUFile *f, void *pv, size_t size)
926 QEMUTimer *v = pv;
927 qemu_get_timer(f, v);
928 return 0;
931 static void put_timer(QEMUFile *f, void *pv, size_t size)
933 QEMUTimer *v = pv;
934 qemu_put_timer(f, v);
937 const VMStateInfo vmstate_info_timer = {
938 .name = "timer",
939 .get = get_timer,
940 .put = put_timer,
943 /* uint8_t buffers */
945 static int get_buffer(QEMUFile *f, void *pv, size_t size)
947 uint8_t *v = pv;
948 qemu_get_buffer(f, v, size);
949 return 0;
952 static void put_buffer(QEMUFile *f, void *pv, size_t size)
954 uint8_t *v = pv;
955 qemu_put_buffer(f, v, size);
958 const VMStateInfo vmstate_info_buffer = {
959 .name = "buffer",
960 .get = get_buffer,
961 .put = put_buffer,
964 /* unused buffers: space that was used for some fields that are
965 not usefull anymore */
967 static int get_unused_buffer(QEMUFile *f, void *pv, size_t size)
969 qemu_fseek(f, size, SEEK_CUR);
970 return 0;
973 static void put_unused_buffer(QEMUFile *f, void *pv, size_t size)
975 qemu_fseek(f, size, SEEK_CUR);
978 const VMStateInfo vmstate_info_unused_buffer = {
979 .name = "unused_buffer",
980 .get = get_unused_buffer,
981 .put = put_unused_buffer,
984 typedef struct SaveStateEntry {
985 QTAILQ_ENTRY(SaveStateEntry) entry;
986 char idstr[256];
987 int instance_id;
988 int version_id;
989 int section_id;
990 SaveLiveStateHandler *save_live_state;
991 SaveStateHandler *save_state;
992 LoadStateHandler *load_state;
993 const VMStateDescription *vmsd;
994 void *opaque;
995 } SaveStateEntry;
997 static QTAILQ_HEAD(savevm_handlers, SaveStateEntry) savevm_handlers =
998 QTAILQ_HEAD_INITIALIZER(savevm_handlers);
999 static int global_section_id;
1001 static int calculate_new_instance_id(const char *idstr)
1003 SaveStateEntry *se;
1004 int instance_id = 0;
1006 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1007 if (strcmp(idstr, se->idstr) == 0
1008 && instance_id <= se->instance_id) {
1009 instance_id = se->instance_id + 1;
1012 return instance_id;
1015 /* TODO: Individual devices generally have very little idea about the rest
1016 of the system, so instance_id should be removed/replaced.
1017 Meanwhile pass -1 as instance_id if you do not already have a clearly
1018 distinguishing id for all instances of your device class. */
1019 int register_savevm_live(const char *idstr,
1020 int instance_id,
1021 int version_id,
1022 SaveLiveStateHandler *save_live_state,
1023 SaveStateHandler *save_state,
1024 LoadStateHandler *load_state,
1025 void *opaque)
1027 SaveStateEntry *se;
1029 se = qemu_malloc(sizeof(SaveStateEntry));
1030 pstrcpy(se->idstr, sizeof(se->idstr), idstr);
1031 se->version_id = version_id;
1032 se->section_id = global_section_id++;
1033 se->save_live_state = save_live_state;
1034 se->save_state = save_state;
1035 se->load_state = load_state;
1036 se->opaque = opaque;
1037 se->vmsd = NULL;
1039 if (instance_id == -1) {
1040 se->instance_id = calculate_new_instance_id(idstr);
1041 } else {
1042 se->instance_id = instance_id;
1044 /* add at the end of list */
1045 QTAILQ_INSERT_TAIL(&savevm_handlers, se, entry);
1046 return 0;
1049 int register_savevm(const char *idstr,
1050 int instance_id,
1051 int version_id,
1052 SaveStateHandler *save_state,
1053 LoadStateHandler *load_state,
1054 void *opaque)
1056 return register_savevm_live(idstr, instance_id, version_id,
1057 NULL, save_state, load_state, opaque);
1060 void unregister_savevm(const char *idstr, void *opaque)
1062 SaveStateEntry *se, *new_se;
1064 QTAILQ_FOREACH_SAFE(se, &savevm_handlers, entry, new_se) {
1065 if (strcmp(se->idstr, idstr) == 0 && se->opaque == opaque) {
1066 QTAILQ_REMOVE(&savevm_handlers, se, entry);
1067 qemu_free(se);
1072 int vmstate_register(int instance_id, const VMStateDescription *vmsd,
1073 void *opaque)
1075 SaveStateEntry *se;
1077 se = qemu_malloc(sizeof(SaveStateEntry));
1078 pstrcpy(se->idstr, sizeof(se->idstr), vmsd->name);
1079 se->version_id = vmsd->version_id;
1080 se->section_id = global_section_id++;
1081 se->save_live_state = NULL;
1082 se->save_state = NULL;
1083 se->load_state = NULL;
1084 se->opaque = opaque;
1085 se->vmsd = vmsd;
1087 if (instance_id == -1) {
1088 se->instance_id = calculate_new_instance_id(vmsd->name);
1089 } else {
1090 se->instance_id = instance_id;
1092 /* add at the end of list */
1093 QTAILQ_INSERT_TAIL(&savevm_handlers, se, entry);
1094 return 0;
1097 void vmstate_unregister(const VMStateDescription *vmsd, void *opaque)
1099 SaveStateEntry *se, *new_se;
1101 QTAILQ_FOREACH_SAFE(se, &savevm_handlers, entry, new_se) {
1102 if (se->vmsd == vmsd && se->opaque == opaque) {
1103 QTAILQ_REMOVE(&savevm_handlers, se, entry);
1104 qemu_free(se);
1109 int vmstate_load_state(QEMUFile *f, const VMStateDescription *vmsd,
1110 void *opaque, int version_id)
1112 VMStateField *field = vmsd->fields;
1114 if (version_id > vmsd->version_id) {
1115 return -EINVAL;
1117 if (version_id < vmsd->minimum_version_id_old) {
1118 return -EINVAL;
1120 if (version_id < vmsd->minimum_version_id) {
1121 return vmsd->load_state_old(f, opaque, version_id);
1123 if (vmsd->pre_load) {
1124 int ret = vmsd->pre_load(opaque);
1125 if (ret)
1126 return ret;
1128 while(field->name) {
1129 if ((field->field_exists &&
1130 field->field_exists(opaque, version_id)) ||
1131 (!field->field_exists &&
1132 field->version_id <= version_id)) {
1133 void *base_addr = opaque + field->offset;
1134 int ret, i, n_elems = 1;
1136 if (field->flags & VMS_ARRAY) {
1137 n_elems = field->num;
1138 } else if (field->flags & VMS_VARRAY_INT32) {
1139 n_elems = *(int32_t *)(opaque+field->num_offset);
1140 } else if (field->flags & VMS_VARRAY_UINT16) {
1141 n_elems = *(uint16_t *)(opaque+field->num_offset);
1143 if (field->flags & VMS_POINTER) {
1144 base_addr = *(void **)base_addr;
1146 for (i = 0; i < n_elems; i++) {
1147 void *addr = base_addr + field->size * i;
1149 if (field->flags & VMS_ARRAY_OF_POINTER) {
1150 addr = *(void **)addr;
1152 if (field->flags & VMS_STRUCT) {
1153 ret = vmstate_load_state(f, field->vmsd, addr, field->vmsd->version_id);
1154 } else {
1155 ret = field->info->get(f, addr, field->size);
1158 if (ret < 0) {
1159 return ret;
1163 field++;
1165 if (vmsd->post_load) {
1166 return vmsd->post_load(opaque, version_id);
1168 return 0;
1171 void vmstate_save_state(QEMUFile *f, const VMStateDescription *vmsd,
1172 void *opaque)
1174 VMStateField *field = vmsd->fields;
1176 if (vmsd->pre_save) {
1177 vmsd->pre_save(opaque);
1179 while(field->name) {
1180 if (!field->field_exists ||
1181 field->field_exists(opaque, vmsd->version_id)) {
1182 void *base_addr = opaque + field->offset;
1183 int i, n_elems = 1;
1185 if (field->flags & VMS_ARRAY) {
1186 n_elems = field->num;
1187 } else if (field->flags & VMS_VARRAY_INT32) {
1188 n_elems = *(int32_t *)(opaque+field->num_offset);
1189 } else if (field->flags & VMS_VARRAY_UINT16) {
1190 n_elems = *(uint16_t *)(opaque+field->num_offset);
1192 if (field->flags & VMS_POINTER) {
1193 base_addr = *(void **)base_addr;
1195 for (i = 0; i < n_elems; i++) {
1196 void *addr = base_addr + field->size * i;
1198 if (field->flags & VMS_STRUCT) {
1199 vmstate_save_state(f, field->vmsd, addr);
1200 } else {
1201 field->info->put(f, addr, field->size);
1205 field++;
1207 if (vmsd->post_save) {
1208 vmsd->post_save(opaque);
1212 static int vmstate_load(QEMUFile *f, SaveStateEntry *se, int version_id)
1214 if (!se->vmsd) { /* Old style */
1215 return se->load_state(f, se->opaque, version_id);
1217 return vmstate_load_state(f, se->vmsd, se->opaque, version_id);
1220 static void vmstate_save(QEMUFile *f, SaveStateEntry *se)
1222 if (!se->vmsd) { /* Old style */
1223 se->save_state(f, se->opaque);
1224 return;
1226 vmstate_save_state(f,se->vmsd, se->opaque);
1229 #define QEMU_VM_FILE_MAGIC 0x5145564d
1230 #define QEMU_VM_FILE_VERSION_COMPAT 0x00000002
1231 #define QEMU_VM_FILE_VERSION 0x00000003
1233 #define QEMU_VM_EOF 0x00
1234 #define QEMU_VM_SECTION_START 0x01
1235 #define QEMU_VM_SECTION_PART 0x02
1236 #define QEMU_VM_SECTION_END 0x03
1237 #define QEMU_VM_SECTION_FULL 0x04
1239 int qemu_savevm_state_begin(QEMUFile *f)
1241 SaveStateEntry *se;
1243 qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
1244 qemu_put_be32(f, QEMU_VM_FILE_VERSION);
1246 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1247 int len;
1249 if (se->save_live_state == NULL)
1250 continue;
1252 /* Section type */
1253 qemu_put_byte(f, QEMU_VM_SECTION_START);
1254 qemu_put_be32(f, se->section_id);
1256 /* ID string */
1257 len = strlen(se->idstr);
1258 qemu_put_byte(f, len);
1259 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
1261 qemu_put_be32(f, se->instance_id);
1262 qemu_put_be32(f, se->version_id);
1264 se->save_live_state(f, QEMU_VM_SECTION_START, se->opaque);
1267 if (qemu_file_has_error(f))
1268 return -EIO;
1270 return 0;
1273 int qemu_savevm_state_iterate(QEMUFile *f)
1275 SaveStateEntry *se;
1276 int ret = 1;
1278 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1279 if (se->save_live_state == NULL)
1280 continue;
1282 /* Section type */
1283 qemu_put_byte(f, QEMU_VM_SECTION_PART);
1284 qemu_put_be32(f, se->section_id);
1286 ret &= !!se->save_live_state(f, QEMU_VM_SECTION_PART, se->opaque);
1289 if (ret)
1290 return 1;
1292 if (qemu_file_has_error(f))
1293 return -EIO;
1295 return 0;
1298 int qemu_savevm_state_complete(QEMUFile *f)
1300 SaveStateEntry *se;
1302 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1303 if (se->save_live_state == NULL)
1304 continue;
1306 /* Section type */
1307 qemu_put_byte(f, QEMU_VM_SECTION_END);
1308 qemu_put_be32(f, se->section_id);
1310 se->save_live_state(f, QEMU_VM_SECTION_END, se->opaque);
1313 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1314 int len;
1316 if (se->save_state == NULL && se->vmsd == NULL)
1317 continue;
1319 /* Section type */
1320 qemu_put_byte(f, QEMU_VM_SECTION_FULL);
1321 qemu_put_be32(f, se->section_id);
1323 /* ID string */
1324 len = strlen(se->idstr);
1325 qemu_put_byte(f, len);
1326 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
1328 qemu_put_be32(f, se->instance_id);
1329 qemu_put_be32(f, se->version_id);
1331 vmstate_save(f, se);
1334 qemu_put_byte(f, QEMU_VM_EOF);
1336 if (qemu_file_has_error(f))
1337 return -EIO;
1339 return 0;
1342 int qemu_savevm_state(QEMUFile *f)
1344 int saved_vm_running;
1345 int ret;
1347 saved_vm_running = vm_running;
1348 vm_stop(0);
1350 bdrv_flush_all();
1352 ret = qemu_savevm_state_begin(f);
1353 if (ret < 0)
1354 goto out;
1356 do {
1357 ret = qemu_savevm_state_iterate(f);
1358 if (ret < 0)
1359 goto out;
1360 } while (ret == 0);
1362 ret = qemu_savevm_state_complete(f);
1364 out:
1365 if (qemu_file_has_error(f))
1366 ret = -EIO;
1368 if (!ret && saved_vm_running)
1369 vm_start();
1371 return ret;
1374 static SaveStateEntry *find_se(const char *idstr, int instance_id)
1376 SaveStateEntry *se;
1378 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1379 if (!strcmp(se->idstr, idstr) &&
1380 instance_id == se->instance_id)
1381 return se;
1383 return NULL;
1386 typedef struct LoadStateEntry {
1387 QLIST_ENTRY(LoadStateEntry) entry;
1388 SaveStateEntry *se;
1389 int section_id;
1390 int version_id;
1391 } LoadStateEntry;
1393 int qemu_loadvm_state(QEMUFile *f)
1395 QLIST_HEAD(, LoadStateEntry) loadvm_handlers =
1396 QLIST_HEAD_INITIALIZER(loadvm_handlers);
1397 LoadStateEntry *le, *new_le;
1398 uint8_t section_type;
1399 unsigned int v;
1400 int ret;
1402 v = qemu_get_be32(f);
1403 if (v != QEMU_VM_FILE_MAGIC)
1404 return -EINVAL;
1406 v = qemu_get_be32(f);
1407 if (v == QEMU_VM_FILE_VERSION_COMPAT) {
1408 fprintf(stderr, "SaveVM v2 format is obsolete and don't work anymore\n");
1409 return -ENOTSUP;
1411 if (v != QEMU_VM_FILE_VERSION)
1412 return -ENOTSUP;
1414 while ((section_type = qemu_get_byte(f)) != QEMU_VM_EOF) {
1415 uint32_t instance_id, version_id, section_id;
1416 SaveStateEntry *se;
1417 char idstr[257];
1418 int len;
1420 switch (section_type) {
1421 case QEMU_VM_SECTION_START:
1422 case QEMU_VM_SECTION_FULL:
1423 /* Read section start */
1424 section_id = qemu_get_be32(f);
1425 len = qemu_get_byte(f);
1426 qemu_get_buffer(f, (uint8_t *)idstr, len);
1427 idstr[len] = 0;
1428 instance_id = qemu_get_be32(f);
1429 version_id = qemu_get_be32(f);
1431 /* Find savevm section */
1432 se = find_se(idstr, instance_id);
1433 if (se == NULL) {
1434 fprintf(stderr, "Unknown savevm section or instance '%s' %d\n", idstr, instance_id);
1435 ret = -EINVAL;
1436 goto out;
1439 /* Validate version */
1440 if (version_id > se->version_id) {
1441 fprintf(stderr, "savevm: unsupported version %d for '%s' v%d\n",
1442 version_id, idstr, se->version_id);
1443 ret = -EINVAL;
1444 goto out;
1447 /* Add entry */
1448 le = qemu_mallocz(sizeof(*le));
1450 le->se = se;
1451 le->section_id = section_id;
1452 le->version_id = version_id;
1453 QLIST_INSERT_HEAD(&loadvm_handlers, le, entry);
1455 ret = vmstate_load(f, le->se, le->version_id);
1456 if (ret < 0) {
1457 fprintf(stderr, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
1458 instance_id, idstr);
1459 goto out;
1461 break;
1462 case QEMU_VM_SECTION_PART:
1463 case QEMU_VM_SECTION_END:
1464 section_id = qemu_get_be32(f);
1466 QLIST_FOREACH(le, &loadvm_handlers, entry) {
1467 if (le->section_id == section_id) {
1468 break;
1471 if (le == NULL) {
1472 fprintf(stderr, "Unknown savevm section %d\n", section_id);
1473 ret = -EINVAL;
1474 goto out;
1477 ret = vmstate_load(f, le->se, le->version_id);
1478 if (ret < 0) {
1479 fprintf(stderr, "qemu: warning: error while loading state section id %d\n",
1480 section_id);
1481 goto out;
1483 break;
1484 default:
1485 fprintf(stderr, "Unknown savevm section type %d\n", section_type);
1486 ret = -EINVAL;
1487 goto out;
1491 ret = 0;
1493 out:
1494 QLIST_FOREACH_SAFE(le, &loadvm_handlers, entry, new_le) {
1495 QLIST_REMOVE(le, entry);
1496 qemu_free(le);
1499 if (qemu_file_has_error(f))
1500 ret = -EIO;
1502 return ret;
1505 /* device can contain snapshots */
1506 static int bdrv_can_snapshot(BlockDriverState *bs)
1508 return (bs &&
1509 !bdrv_is_removable(bs) &&
1510 !bdrv_is_read_only(bs));
1513 /* device must be snapshots in order to have a reliable snapshot */
1514 static int bdrv_has_snapshot(BlockDriverState *bs)
1516 return (bs &&
1517 !bdrv_is_removable(bs) &&
1518 !bdrv_is_read_only(bs));
1521 static BlockDriverState *get_bs_snapshots(void)
1523 BlockDriverState *bs;
1524 DriveInfo *dinfo;
1526 if (bs_snapshots)
1527 return bs_snapshots;
1528 QTAILQ_FOREACH(dinfo, &drives, next) {
1529 bs = dinfo->bdrv;
1530 if (bdrv_can_snapshot(bs))
1531 goto ok;
1533 return NULL;
1535 bs_snapshots = bs;
1536 return bs;
1539 static int bdrv_snapshot_find(BlockDriverState *bs, QEMUSnapshotInfo *sn_info,
1540 const char *name)
1542 QEMUSnapshotInfo *sn_tab, *sn;
1543 int nb_sns, i, ret;
1545 ret = -ENOENT;
1546 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
1547 if (nb_sns < 0)
1548 return ret;
1549 for(i = 0; i < nb_sns; i++) {
1550 sn = &sn_tab[i];
1551 if (!strcmp(sn->id_str, name) || !strcmp(sn->name, name)) {
1552 *sn_info = *sn;
1553 ret = 0;
1554 break;
1557 qemu_free(sn_tab);
1558 return ret;
1561 void do_savevm(Monitor *mon, const QDict *qdict)
1563 DriveInfo *dinfo;
1564 BlockDriverState *bs, *bs1;
1565 QEMUSnapshotInfo sn1, *sn = &sn1, old_sn1, *old_sn = &old_sn1;
1566 int must_delete, ret;
1567 QEMUFile *f;
1568 int saved_vm_running;
1569 uint32_t vm_state_size;
1570 #ifdef _WIN32
1571 struct _timeb tb;
1572 #else
1573 struct timeval tv;
1574 #endif
1575 const char *name = qdict_get_try_str(qdict, "name");
1577 bs = get_bs_snapshots();
1578 if (!bs) {
1579 monitor_printf(mon, "No block device can accept snapshots\n");
1580 return;
1583 /* ??? Should this occur after vm_stop? */
1584 qemu_aio_flush();
1586 saved_vm_running = vm_running;
1587 vm_stop(0);
1589 must_delete = 0;
1590 if (name) {
1591 ret = bdrv_snapshot_find(bs, old_sn, name);
1592 if (ret >= 0) {
1593 must_delete = 1;
1596 memset(sn, 0, sizeof(*sn));
1597 if (must_delete) {
1598 pstrcpy(sn->name, sizeof(sn->name), old_sn->name);
1599 pstrcpy(sn->id_str, sizeof(sn->id_str), old_sn->id_str);
1600 } else {
1601 if (name)
1602 pstrcpy(sn->name, sizeof(sn->name), name);
1605 /* fill auxiliary fields */
1606 #ifdef _WIN32
1607 _ftime(&tb);
1608 sn->date_sec = tb.time;
1609 sn->date_nsec = tb.millitm * 1000000;
1610 #else
1611 gettimeofday(&tv, NULL);
1612 sn->date_sec = tv.tv_sec;
1613 sn->date_nsec = tv.tv_usec * 1000;
1614 #endif
1615 sn->vm_clock_nsec = qemu_get_clock(vm_clock);
1617 /* save the VM state */
1618 f = qemu_fopen_bdrv(bs, 1);
1619 if (!f) {
1620 monitor_printf(mon, "Could not open VM state file\n");
1621 goto the_end;
1623 ret = qemu_savevm_state(f);
1624 vm_state_size = qemu_ftell(f);
1625 qemu_fclose(f);
1626 if (ret < 0) {
1627 monitor_printf(mon, "Error %d while writing VM\n", ret);
1628 goto the_end;
1631 /* create the snapshots */
1633 QTAILQ_FOREACH(dinfo, &drives, next) {
1634 bs1 = dinfo->bdrv;
1635 if (bdrv_has_snapshot(bs1)) {
1636 if (must_delete) {
1637 ret = bdrv_snapshot_delete(bs1, old_sn->id_str);
1638 if (ret < 0) {
1639 monitor_printf(mon,
1640 "Error while deleting snapshot on '%s'\n",
1641 bdrv_get_device_name(bs1));
1644 /* Write VM state size only to the image that contains the state */
1645 sn->vm_state_size = (bs == bs1 ? vm_state_size : 0);
1646 ret = bdrv_snapshot_create(bs1, sn);
1647 if (ret < 0) {
1648 monitor_printf(mon, "Error while creating snapshot on '%s'\n",
1649 bdrv_get_device_name(bs1));
1654 the_end:
1655 if (saved_vm_running)
1656 vm_start();
1659 int load_vmstate(Monitor *mon, const char *name)
1661 DriveInfo *dinfo;
1662 BlockDriverState *bs, *bs1;
1663 QEMUSnapshotInfo sn;
1664 QEMUFile *f;
1665 int ret;
1667 bs = get_bs_snapshots();
1668 if (!bs) {
1669 monitor_printf(mon, "No block device supports snapshots\n");
1670 return -EINVAL;
1673 /* Flush all IO requests so they don't interfere with the new state. */
1674 qemu_aio_flush();
1676 QTAILQ_FOREACH(dinfo, &drives, next) {
1677 bs1 = dinfo->bdrv;
1678 if (bdrv_has_snapshot(bs1)) {
1679 ret = bdrv_snapshot_goto(bs1, name);
1680 if (ret < 0) {
1681 if (bs != bs1)
1682 monitor_printf(mon, "Warning: ");
1683 switch(ret) {
1684 case -ENOTSUP:
1685 monitor_printf(mon,
1686 "Snapshots not supported on device '%s'\n",
1687 bdrv_get_device_name(bs1));
1688 break;
1689 case -ENOENT:
1690 monitor_printf(mon, "Could not find snapshot '%s' on "
1691 "device '%s'\n",
1692 name, bdrv_get_device_name(bs1));
1693 break;
1694 default:
1695 monitor_printf(mon, "Error %d while activating snapshot on"
1696 " '%s'\n", ret, bdrv_get_device_name(bs1));
1697 break;
1699 /* fatal on snapshot block device */
1700 if (bs == bs1)
1701 return 0;
1706 /* Don't even try to load empty VM states */
1707 ret = bdrv_snapshot_find(bs, &sn, name);
1708 if ((ret >= 0) && (sn.vm_state_size == 0))
1709 return -EINVAL;
1711 /* restore the VM state */
1712 f = qemu_fopen_bdrv(bs, 0);
1713 if (!f) {
1714 monitor_printf(mon, "Could not open VM state file\n");
1715 return -EINVAL;
1717 ret = qemu_loadvm_state(f);
1718 qemu_fclose(f);
1719 if (ret < 0) {
1720 monitor_printf(mon, "Error %d while loading VM state\n", ret);
1721 return ret;
1723 return 0;
1726 void do_delvm(Monitor *mon, const QDict *qdict)
1728 DriveInfo *dinfo;
1729 BlockDriverState *bs, *bs1;
1730 int ret;
1731 const char *name = qdict_get_str(qdict, "name");
1733 bs = get_bs_snapshots();
1734 if (!bs) {
1735 monitor_printf(mon, "No block device supports snapshots\n");
1736 return;
1739 QTAILQ_FOREACH(dinfo, &drives, next) {
1740 bs1 = dinfo->bdrv;
1741 if (bdrv_has_snapshot(bs1)) {
1742 ret = bdrv_snapshot_delete(bs1, name);
1743 if (ret < 0) {
1744 if (ret == -ENOTSUP)
1745 monitor_printf(mon,
1746 "Snapshots not supported on device '%s'\n",
1747 bdrv_get_device_name(bs1));
1748 else
1749 monitor_printf(mon, "Error %d while deleting snapshot on "
1750 "'%s'\n", ret, bdrv_get_device_name(bs1));
1756 void do_info_snapshots(Monitor *mon)
1758 DriveInfo *dinfo;
1759 BlockDriverState *bs, *bs1;
1760 QEMUSnapshotInfo *sn_tab, *sn;
1761 int nb_sns, i;
1762 char buf[256];
1764 bs = get_bs_snapshots();
1765 if (!bs) {
1766 monitor_printf(mon, "No available block device supports snapshots\n");
1767 return;
1769 monitor_printf(mon, "Snapshot devices:");
1770 QTAILQ_FOREACH(dinfo, &drives, next) {
1771 bs1 = dinfo->bdrv;
1772 if (bdrv_has_snapshot(bs1)) {
1773 if (bs == bs1)
1774 monitor_printf(mon, " %s", bdrv_get_device_name(bs1));
1777 monitor_printf(mon, "\n");
1779 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
1780 if (nb_sns < 0) {
1781 monitor_printf(mon, "bdrv_snapshot_list: error %d\n", nb_sns);
1782 return;
1784 monitor_printf(mon, "Snapshot list (from %s):\n",
1785 bdrv_get_device_name(bs));
1786 monitor_printf(mon, "%s\n", bdrv_snapshot_dump(buf, sizeof(buf), NULL));
1787 for(i = 0; i < nb_sns; i++) {
1788 sn = &sn_tab[i];
1789 monitor_printf(mon, "%s\n", bdrv_snapshot_dump(buf, sizeof(buf), sn));
1791 qemu_free(sn_tab);