Add operations to qlist to allow it to be used as a stack
[qemu.git] / savevm.c
blob4668843c3f2ad12d52d1cbf30c0fafeeaa932085
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 QEMUFileGetRateLimit *get_rate_limit;
173 void *opaque;
174 int is_write;
176 int64_t buf_offset; /* start of buffer when writing, end of buffer
177 when reading */
178 int buf_index;
179 int buf_size; /* 0 when writing */
180 uint8_t buf[IO_BUF_SIZE];
182 int has_error;
185 typedef struct QEMUFileStdio
187 FILE *stdio_file;
188 QEMUFile *file;
189 } QEMUFileStdio;
191 typedef struct QEMUFileSocket
193 int fd;
194 QEMUFile *file;
195 } QEMUFileSocket;
197 static int socket_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
199 QEMUFileSocket *s = opaque;
200 ssize_t len;
202 do {
203 len = recv(s->fd, (void *)buf, size, 0);
204 } while (len == -1 && socket_error() == EINTR);
206 if (len == -1)
207 len = -socket_error();
209 return len;
212 static int socket_close(void *opaque)
214 QEMUFileSocket *s = opaque;
215 qemu_free(s);
216 return 0;
219 static int stdio_put_buffer(void *opaque, const uint8_t *buf, int64_t pos, int size)
221 QEMUFileStdio *s = opaque;
222 return fwrite(buf, 1, size, s->stdio_file);
225 static int stdio_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
227 QEMUFileStdio *s = opaque;
228 FILE *fp = s->stdio_file;
229 int bytes;
231 do {
232 clearerr(fp);
233 bytes = fread(buf, 1, size, fp);
234 } while ((bytes == 0) && ferror(fp) && (errno == EINTR));
235 return bytes;
238 static int stdio_pclose(void *opaque)
240 QEMUFileStdio *s = opaque;
241 pclose(s->stdio_file);
242 qemu_free(s);
243 return 0;
246 static int stdio_fclose(void *opaque)
248 QEMUFileStdio *s = opaque;
249 fclose(s->stdio_file);
250 qemu_free(s);
251 return 0;
254 QEMUFile *qemu_popen(FILE *stdio_file, const char *mode)
256 QEMUFileStdio *s;
258 if (stdio_file == NULL || mode == NULL || (mode[0] != 'r' && mode[0] != 'w') || mode[1] != 0) {
259 fprintf(stderr, "qemu_popen: Argument validity check failed\n");
260 return NULL;
263 s = qemu_mallocz(sizeof(QEMUFileStdio));
265 s->stdio_file = stdio_file;
267 if(mode[0] == 'r') {
268 s->file = qemu_fopen_ops(s, NULL, stdio_get_buffer, stdio_pclose,
269 NULL, NULL, NULL);
270 } else {
271 s->file = qemu_fopen_ops(s, stdio_put_buffer, NULL, stdio_pclose,
272 NULL, NULL, NULL);
274 return s->file;
277 QEMUFile *qemu_popen_cmd(const char *command, const char *mode)
279 FILE *popen_file;
281 popen_file = popen(command, mode);
282 if(popen_file == NULL) {
283 return NULL;
286 return qemu_popen(popen_file, mode);
289 int qemu_stdio_fd(QEMUFile *f)
291 QEMUFileStdio *p;
292 int fd;
294 p = (QEMUFileStdio *)f->opaque;
295 fd = fileno(p->stdio_file);
297 return fd;
300 QEMUFile *qemu_fdopen(int fd, const char *mode)
302 QEMUFileStdio *s;
304 if (mode == NULL ||
305 (mode[0] != 'r' && mode[0] != 'w') ||
306 mode[1] != 'b' || mode[2] != 0) {
307 fprintf(stderr, "qemu_fdopen: Argument validity check failed\n");
308 return NULL;
311 s = qemu_mallocz(sizeof(QEMUFileStdio));
312 s->stdio_file = fdopen(fd, mode);
313 if (!s->stdio_file)
314 goto fail;
316 if(mode[0] == 'r') {
317 s->file = qemu_fopen_ops(s, NULL, stdio_get_buffer, stdio_fclose,
318 NULL, NULL, NULL);
319 } else {
320 s->file = qemu_fopen_ops(s, stdio_put_buffer, NULL, stdio_fclose,
321 NULL, NULL, NULL);
323 return s->file;
325 fail:
326 qemu_free(s);
327 return NULL;
330 QEMUFile *qemu_fopen_socket(int fd)
332 QEMUFileSocket *s = qemu_mallocz(sizeof(QEMUFileSocket));
334 s->fd = fd;
335 s->file = qemu_fopen_ops(s, NULL, socket_get_buffer, socket_close,
336 NULL, NULL, NULL);
337 return s->file;
340 static int file_put_buffer(void *opaque, const uint8_t *buf,
341 int64_t pos, int size)
343 QEMUFileStdio *s = opaque;
344 fseek(s->stdio_file, pos, SEEK_SET);
345 fwrite(buf, 1, size, s->stdio_file);
346 return size;
349 static int file_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
351 QEMUFileStdio *s = opaque;
352 fseek(s->stdio_file, pos, SEEK_SET);
353 return fread(buf, 1, size, s->stdio_file);
356 QEMUFile *qemu_fopen(const char *filename, const char *mode)
358 QEMUFileStdio *s;
360 if (mode == NULL ||
361 (mode[0] != 'r' && mode[0] != 'w') ||
362 mode[1] != 'b' || mode[2] != 0) {
363 fprintf(stderr, "qemu_fdopen: Argument validity check failed\n");
364 return NULL;
367 s = qemu_mallocz(sizeof(QEMUFileStdio));
369 s->stdio_file = fopen(filename, mode);
370 if (!s->stdio_file)
371 goto fail;
373 if(mode[0] == 'w') {
374 s->file = qemu_fopen_ops(s, file_put_buffer, NULL, stdio_fclose,
375 NULL, NULL, NULL);
376 } else {
377 s->file = qemu_fopen_ops(s, NULL, file_get_buffer, stdio_fclose,
378 NULL, NULL, NULL);
380 return s->file;
381 fail:
382 qemu_free(s);
383 return NULL;
386 static int block_put_buffer(void *opaque, const uint8_t *buf,
387 int64_t pos, int size)
389 bdrv_save_vmstate(opaque, buf, pos, size);
390 return size;
393 static int block_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
395 return bdrv_load_vmstate(opaque, buf, pos, size);
398 static int bdrv_fclose(void *opaque)
400 return 0;
403 static QEMUFile *qemu_fopen_bdrv(BlockDriverState *bs, int is_writable)
405 if (is_writable)
406 return qemu_fopen_ops(bs, block_put_buffer, NULL, bdrv_fclose,
407 NULL, NULL, NULL);
408 return qemu_fopen_ops(bs, NULL, block_get_buffer, bdrv_fclose, NULL, NULL, NULL);
411 QEMUFile *qemu_fopen_ops(void *opaque, QEMUFilePutBufferFunc *put_buffer,
412 QEMUFileGetBufferFunc *get_buffer,
413 QEMUFileCloseFunc *close,
414 QEMUFileRateLimit *rate_limit,
415 QEMUFileSetRateLimit *set_rate_limit,
416 QEMUFileGetRateLimit *get_rate_limit)
418 QEMUFile *f;
420 f = qemu_mallocz(sizeof(QEMUFile));
422 f->opaque = opaque;
423 f->put_buffer = put_buffer;
424 f->get_buffer = get_buffer;
425 f->close = close;
426 f->rate_limit = rate_limit;
427 f->set_rate_limit = set_rate_limit;
428 f->get_rate_limit = get_rate_limit;
429 f->is_write = 0;
431 return f;
434 int qemu_file_has_error(QEMUFile *f)
436 return f->has_error;
439 void qemu_file_set_error(QEMUFile *f)
441 f->has_error = 1;
444 void qemu_fflush(QEMUFile *f)
446 if (!f->put_buffer)
447 return;
449 if (f->is_write && f->buf_index > 0) {
450 int len;
452 len = f->put_buffer(f->opaque, f->buf, f->buf_offset, f->buf_index);
453 if (len > 0)
454 f->buf_offset += f->buf_index;
455 else
456 f->has_error = 1;
457 f->buf_index = 0;
461 static void qemu_fill_buffer(QEMUFile *f)
463 int len;
465 if (!f->get_buffer)
466 return;
468 if (f->is_write)
469 abort();
471 len = f->get_buffer(f->opaque, f->buf, f->buf_offset, IO_BUF_SIZE);
472 if (len > 0) {
473 f->buf_index = 0;
474 f->buf_size = len;
475 f->buf_offset += len;
476 } else if (len != -EAGAIN)
477 f->has_error = 1;
480 int qemu_fclose(QEMUFile *f)
482 int ret = 0;
483 qemu_fflush(f);
484 if (f->close)
485 ret = f->close(f->opaque);
486 qemu_free(f);
487 return ret;
490 void qemu_file_put_notify(QEMUFile *f)
492 f->put_buffer(f->opaque, NULL, 0, 0);
495 void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size)
497 int l;
499 if (!f->has_error && f->is_write == 0 && f->buf_index > 0) {
500 fprintf(stderr,
501 "Attempted to write to buffer while read buffer is not empty\n");
502 abort();
505 while (!f->has_error && size > 0) {
506 l = IO_BUF_SIZE - f->buf_index;
507 if (l > size)
508 l = size;
509 memcpy(f->buf + f->buf_index, buf, l);
510 f->is_write = 1;
511 f->buf_index += l;
512 buf += l;
513 size -= l;
514 if (f->buf_index >= IO_BUF_SIZE)
515 qemu_fflush(f);
519 void qemu_put_byte(QEMUFile *f, int v)
521 if (!f->has_error && f->is_write == 0 && f->buf_index > 0) {
522 fprintf(stderr,
523 "Attempted to write to buffer while read buffer is not empty\n");
524 abort();
527 f->buf[f->buf_index++] = v;
528 f->is_write = 1;
529 if (f->buf_index >= IO_BUF_SIZE)
530 qemu_fflush(f);
533 int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size1)
535 int size, l;
537 if (f->is_write)
538 abort();
540 size = size1;
541 while (size > 0) {
542 l = f->buf_size - f->buf_index;
543 if (l == 0) {
544 qemu_fill_buffer(f);
545 l = f->buf_size - f->buf_index;
546 if (l == 0)
547 break;
549 if (l > size)
550 l = size;
551 memcpy(buf, f->buf + f->buf_index, l);
552 f->buf_index += l;
553 buf += l;
554 size -= l;
556 return size1 - size;
559 int qemu_get_byte(QEMUFile *f)
561 if (f->is_write)
562 abort();
564 if (f->buf_index >= f->buf_size) {
565 qemu_fill_buffer(f);
566 if (f->buf_index >= f->buf_size)
567 return 0;
569 return f->buf[f->buf_index++];
572 int64_t qemu_ftell(QEMUFile *f)
574 return f->buf_offset - f->buf_size + f->buf_index;
577 int64_t qemu_fseek(QEMUFile *f, int64_t pos, int whence)
579 if (whence == SEEK_SET) {
580 /* nothing to do */
581 } else if (whence == SEEK_CUR) {
582 pos += qemu_ftell(f);
583 } else {
584 /* SEEK_END not supported */
585 return -1;
587 if (f->put_buffer) {
588 qemu_fflush(f);
589 f->buf_offset = pos;
590 } else {
591 f->buf_offset = pos;
592 f->buf_index = 0;
593 f->buf_size = 0;
595 return pos;
598 int qemu_file_rate_limit(QEMUFile *f)
600 if (f->rate_limit)
601 return f->rate_limit(f->opaque);
603 return 0;
606 size_t qemu_file_get_rate_limit(QEMUFile *f)
608 if (f->get_rate_limit)
609 return f->get_rate_limit(f->opaque);
611 return 0;
614 size_t qemu_file_set_rate_limit(QEMUFile *f, size_t new_rate)
616 /* any failed or completed migration keeps its state to allow probing of
617 * migration data, but has no associated file anymore */
618 if (f && f->set_rate_limit)
619 return f->set_rate_limit(f->opaque, new_rate);
621 return 0;
624 void qemu_put_be16(QEMUFile *f, unsigned int v)
626 qemu_put_byte(f, v >> 8);
627 qemu_put_byte(f, v);
630 void qemu_put_be32(QEMUFile *f, unsigned int v)
632 qemu_put_byte(f, v >> 24);
633 qemu_put_byte(f, v >> 16);
634 qemu_put_byte(f, v >> 8);
635 qemu_put_byte(f, v);
638 void qemu_put_be64(QEMUFile *f, uint64_t v)
640 qemu_put_be32(f, v >> 32);
641 qemu_put_be32(f, v);
644 unsigned int qemu_get_be16(QEMUFile *f)
646 unsigned int v;
647 v = qemu_get_byte(f) << 8;
648 v |= qemu_get_byte(f);
649 return v;
652 unsigned int qemu_get_be32(QEMUFile *f)
654 unsigned int v;
655 v = qemu_get_byte(f) << 24;
656 v |= qemu_get_byte(f) << 16;
657 v |= qemu_get_byte(f) << 8;
658 v |= qemu_get_byte(f);
659 return v;
662 uint64_t qemu_get_be64(QEMUFile *f)
664 uint64_t v;
665 v = (uint64_t)qemu_get_be32(f) << 32;
666 v |= qemu_get_be32(f);
667 return v;
670 /* 8 bit int */
672 static int get_int8(QEMUFile *f, void *pv, size_t size)
674 int8_t *v = pv;
675 qemu_get_s8s(f, v);
676 return 0;
679 static void put_int8(QEMUFile *f, void *pv, size_t size)
681 int8_t *v = pv;
682 qemu_put_s8s(f, v);
685 const VMStateInfo vmstate_info_int8 = {
686 .name = "int8",
687 .get = get_int8,
688 .put = put_int8,
691 /* 16 bit int */
693 static int get_int16(QEMUFile *f, void *pv, size_t size)
695 int16_t *v = pv;
696 qemu_get_sbe16s(f, v);
697 return 0;
700 static void put_int16(QEMUFile *f, void *pv, size_t size)
702 int16_t *v = pv;
703 qemu_put_sbe16s(f, v);
706 const VMStateInfo vmstate_info_int16 = {
707 .name = "int16",
708 .get = get_int16,
709 .put = put_int16,
712 /* 32 bit int */
714 static int get_int32(QEMUFile *f, void *pv, size_t size)
716 int32_t *v = pv;
717 qemu_get_sbe32s(f, v);
718 return 0;
721 static void put_int32(QEMUFile *f, void *pv, size_t size)
723 int32_t *v = pv;
724 qemu_put_sbe32s(f, v);
727 const VMStateInfo vmstate_info_int32 = {
728 .name = "int32",
729 .get = get_int32,
730 .put = put_int32,
733 /* 32 bit int. See that the received value is the same than the one
734 in the field */
736 static int get_int32_equal(QEMUFile *f, void *pv, size_t size)
738 int32_t *v = pv;
739 int32_t v2;
740 qemu_get_sbe32s(f, &v2);
742 if (*v == v2)
743 return 0;
744 return -EINVAL;
747 const VMStateInfo vmstate_info_int32_equal = {
748 .name = "int32 equal",
749 .get = get_int32_equal,
750 .put = put_int32,
753 /* 32 bit int. See that the received value is the less or the same
754 than the one in the field */
756 static int get_int32_le(QEMUFile *f, void *pv, size_t size)
758 int32_t *old = pv;
759 int32_t new;
760 qemu_get_sbe32s(f, &new);
762 if (*old <= new)
763 return 0;
764 return -EINVAL;
767 const VMStateInfo vmstate_info_int32_le = {
768 .name = "int32 equal",
769 .get = get_int32_le,
770 .put = put_int32,
773 /* 64 bit int */
775 static int get_int64(QEMUFile *f, void *pv, size_t size)
777 int64_t *v = pv;
778 qemu_get_sbe64s(f, v);
779 return 0;
782 static void put_int64(QEMUFile *f, void *pv, size_t size)
784 int64_t *v = pv;
785 qemu_put_sbe64s(f, v);
788 const VMStateInfo vmstate_info_int64 = {
789 .name = "int64",
790 .get = get_int64,
791 .put = put_int64,
794 /* 8 bit unsigned int */
796 static int get_uint8(QEMUFile *f, void *pv, size_t size)
798 uint8_t *v = pv;
799 qemu_get_8s(f, v);
800 return 0;
803 static void put_uint8(QEMUFile *f, void *pv, size_t size)
805 uint8_t *v = pv;
806 qemu_put_8s(f, v);
809 const VMStateInfo vmstate_info_uint8 = {
810 .name = "uint8",
811 .get = get_uint8,
812 .put = put_uint8,
815 /* 16 bit unsigned int */
817 static int get_uint16(QEMUFile *f, void *pv, size_t size)
819 uint16_t *v = pv;
820 qemu_get_be16s(f, v);
821 return 0;
824 static void put_uint16(QEMUFile *f, void *pv, size_t size)
826 uint16_t *v = pv;
827 qemu_put_be16s(f, v);
830 const VMStateInfo vmstate_info_uint16 = {
831 .name = "uint16",
832 .get = get_uint16,
833 .put = put_uint16,
836 /* 32 bit unsigned int */
838 static int get_uint32(QEMUFile *f, void *pv, size_t size)
840 uint32_t *v = pv;
841 qemu_get_be32s(f, v);
842 return 0;
845 static void put_uint32(QEMUFile *f, void *pv, size_t size)
847 uint32_t *v = pv;
848 qemu_put_be32s(f, v);
851 const VMStateInfo vmstate_info_uint32 = {
852 .name = "uint32",
853 .get = get_uint32,
854 .put = put_uint32,
857 /* 64 bit unsigned int */
859 static int get_uint64(QEMUFile *f, void *pv, size_t size)
861 uint64_t *v = pv;
862 qemu_get_be64s(f, v);
863 return 0;
866 static void put_uint64(QEMUFile *f, void *pv, size_t size)
868 uint64_t *v = pv;
869 qemu_put_be64s(f, v);
872 const VMStateInfo vmstate_info_uint64 = {
873 .name = "uint64",
874 .get = get_uint64,
875 .put = put_uint64,
878 /* 8 bit int. See that the received value is the same than the one
879 in the field */
881 static int get_uint8_equal(QEMUFile *f, void *pv, size_t size)
883 uint8_t *v = pv;
884 uint8_t v2;
885 qemu_get_8s(f, &v2);
887 if (*v == v2)
888 return 0;
889 return -EINVAL;
892 const VMStateInfo vmstate_info_uint8_equal = {
893 .name = "uint8 equal",
894 .get = get_uint8_equal,
895 .put = put_uint8,
898 /* 16 bit unsigned int int. See that the received value is the same than the one
899 in the field */
901 static int get_uint16_equal(QEMUFile *f, void *pv, size_t size)
903 uint16_t *v = pv;
904 uint16_t v2;
905 qemu_get_be16s(f, &v2);
907 if (*v == v2)
908 return 0;
909 return -EINVAL;
912 const VMStateInfo vmstate_info_uint16_equal = {
913 .name = "uint16 equal",
914 .get = get_uint16_equal,
915 .put = put_uint16,
918 /* timers */
920 static int get_timer(QEMUFile *f, void *pv, size_t size)
922 QEMUTimer *v = pv;
923 qemu_get_timer(f, v);
924 return 0;
927 static void put_timer(QEMUFile *f, void *pv, size_t size)
929 QEMUTimer *v = pv;
930 qemu_put_timer(f, v);
933 const VMStateInfo vmstate_info_timer = {
934 .name = "timer",
935 .get = get_timer,
936 .put = put_timer,
939 /* uint8_t buffers */
941 static int get_buffer(QEMUFile *f, void *pv, size_t size)
943 uint8_t *v = pv;
944 qemu_get_buffer(f, v, size);
945 return 0;
948 static void put_buffer(QEMUFile *f, void *pv, size_t size)
950 uint8_t *v = pv;
951 qemu_put_buffer(f, v, size);
954 const VMStateInfo vmstate_info_buffer = {
955 .name = "buffer",
956 .get = get_buffer,
957 .put = put_buffer,
960 /* unused buffers: space that was used for some fields that are
961 not usefull anymore */
963 static int get_unused_buffer(QEMUFile *f, void *pv, size_t size)
965 qemu_fseek(f, size, SEEK_CUR);
966 return 0;
969 static void put_unused_buffer(QEMUFile *f, void *pv, size_t size)
971 qemu_fseek(f, size, SEEK_CUR);
974 const VMStateInfo vmstate_info_unused_buffer = {
975 .name = "unused_buffer",
976 .get = get_unused_buffer,
977 .put = put_unused_buffer,
980 typedef struct SaveStateEntry {
981 QTAILQ_ENTRY(SaveStateEntry) entry;
982 char idstr[256];
983 int instance_id;
984 int version_id;
985 int section_id;
986 SaveSetParamsHandler *set_params;
987 SaveLiveStateHandler *save_live_state;
988 SaveStateHandler *save_state;
989 LoadStateHandler *load_state;
990 const VMStateDescription *vmsd;
991 void *opaque;
992 } SaveStateEntry;
995 static QTAILQ_HEAD(savevm_handlers, SaveStateEntry) savevm_handlers =
996 QTAILQ_HEAD_INITIALIZER(savevm_handlers);
997 static int global_section_id;
999 static int calculate_new_instance_id(const char *idstr)
1001 SaveStateEntry *se;
1002 int instance_id = 0;
1004 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1005 if (strcmp(idstr, se->idstr) == 0
1006 && instance_id <= se->instance_id) {
1007 instance_id = se->instance_id + 1;
1010 return instance_id;
1013 /* TODO: Individual devices generally have very little idea about the rest
1014 of the system, so instance_id should be removed/replaced.
1015 Meanwhile pass -1 as instance_id if you do not already have a clearly
1016 distinguishing id for all instances of your device class. */
1017 int register_savevm_live(const char *idstr,
1018 int instance_id,
1019 int version_id,
1020 SaveSetParamsHandler *set_params,
1021 SaveLiveStateHandler *save_live_state,
1022 SaveStateHandler *save_state,
1023 LoadStateHandler *load_state,
1024 void *opaque)
1026 SaveStateEntry *se;
1028 se = qemu_mallocz(sizeof(SaveStateEntry));
1029 pstrcpy(se->idstr, sizeof(se->idstr), idstr);
1030 se->version_id = version_id;
1031 se->section_id = global_section_id++;
1032 se->set_params = set_params;
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, 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_mallocz(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, int blk_enable, int shared)
1241 SaveStateEntry *se;
1243 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1244 if(se->set_params == NULL) {
1245 continue;
1247 se->set_params(blk_enable, shared, se->opaque);
1250 qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
1251 qemu_put_be32(f, QEMU_VM_FILE_VERSION);
1253 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1254 int len;
1256 if (se->save_live_state == NULL)
1257 continue;
1259 /* Section type */
1260 qemu_put_byte(f, QEMU_VM_SECTION_START);
1261 qemu_put_be32(f, se->section_id);
1263 /* ID string */
1264 len = strlen(se->idstr);
1265 qemu_put_byte(f, len);
1266 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
1268 qemu_put_be32(f, se->instance_id);
1269 qemu_put_be32(f, se->version_id);
1271 se->save_live_state(f, QEMU_VM_SECTION_START, se->opaque);
1274 if (qemu_file_has_error(f))
1275 return -EIO;
1277 return 0;
1280 int qemu_savevm_state_iterate(QEMUFile *f)
1282 SaveStateEntry *se;
1283 int ret = 1;
1285 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1286 if (se->save_live_state == NULL)
1287 continue;
1289 /* Section type */
1290 qemu_put_byte(f, QEMU_VM_SECTION_PART);
1291 qemu_put_be32(f, se->section_id);
1293 ret &= !!se->save_live_state(f, QEMU_VM_SECTION_PART, se->opaque);
1296 if (ret)
1297 return 1;
1299 if (qemu_file_has_error(f))
1300 return -EIO;
1302 return 0;
1305 int qemu_savevm_state_complete(QEMUFile *f)
1307 SaveStateEntry *se;
1309 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1310 if (se->save_live_state == NULL)
1311 continue;
1313 /* Section type */
1314 qemu_put_byte(f, QEMU_VM_SECTION_END);
1315 qemu_put_be32(f, se->section_id);
1317 se->save_live_state(f, QEMU_VM_SECTION_END, se->opaque);
1320 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1321 int len;
1323 if (se->save_state == NULL && se->vmsd == NULL)
1324 continue;
1326 /* Section type */
1327 qemu_put_byte(f, QEMU_VM_SECTION_FULL);
1328 qemu_put_be32(f, se->section_id);
1330 /* ID string */
1331 len = strlen(se->idstr);
1332 qemu_put_byte(f, len);
1333 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
1335 qemu_put_be32(f, se->instance_id);
1336 qemu_put_be32(f, se->version_id);
1338 vmstate_save(f, se);
1341 qemu_put_byte(f, QEMU_VM_EOF);
1343 if (qemu_file_has_error(f))
1344 return -EIO;
1346 return 0;
1349 int qemu_savevm_state(QEMUFile *f)
1351 int saved_vm_running;
1352 int ret;
1354 saved_vm_running = vm_running;
1355 vm_stop(0);
1357 bdrv_flush_all();
1359 ret = qemu_savevm_state_begin(f, 0, 0);
1360 if (ret < 0)
1361 goto out;
1363 do {
1364 ret = qemu_savevm_state_iterate(f);
1365 if (ret < 0)
1366 goto out;
1367 } while (ret == 0);
1369 ret = qemu_savevm_state_complete(f);
1371 out:
1372 if (qemu_file_has_error(f))
1373 ret = -EIO;
1375 if (!ret && saved_vm_running)
1376 vm_start();
1378 return ret;
1381 static SaveStateEntry *find_se(const char *idstr, int instance_id)
1383 SaveStateEntry *se;
1385 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1386 if (!strcmp(se->idstr, idstr) &&
1387 instance_id == se->instance_id)
1388 return se;
1390 return NULL;
1393 typedef struct LoadStateEntry {
1394 QLIST_ENTRY(LoadStateEntry) entry;
1395 SaveStateEntry *se;
1396 int section_id;
1397 int version_id;
1398 } LoadStateEntry;
1400 int qemu_loadvm_state(QEMUFile *f)
1402 QLIST_HEAD(, LoadStateEntry) loadvm_handlers =
1403 QLIST_HEAD_INITIALIZER(loadvm_handlers);
1404 LoadStateEntry *le, *new_le;
1405 uint8_t section_type;
1406 unsigned int v;
1407 int ret;
1409 v = qemu_get_be32(f);
1410 if (v != QEMU_VM_FILE_MAGIC)
1411 return -EINVAL;
1413 v = qemu_get_be32(f);
1414 if (v == QEMU_VM_FILE_VERSION_COMPAT) {
1415 fprintf(stderr, "SaveVM v2 format is obsolete and don't work anymore\n");
1416 return -ENOTSUP;
1418 if (v != QEMU_VM_FILE_VERSION)
1419 return -ENOTSUP;
1421 while ((section_type = qemu_get_byte(f)) != QEMU_VM_EOF) {
1422 uint32_t instance_id, version_id, section_id;
1423 SaveStateEntry *se;
1424 char idstr[257];
1425 int len;
1427 switch (section_type) {
1428 case QEMU_VM_SECTION_START:
1429 case QEMU_VM_SECTION_FULL:
1430 /* Read section start */
1431 section_id = qemu_get_be32(f);
1432 len = qemu_get_byte(f);
1433 qemu_get_buffer(f, (uint8_t *)idstr, len);
1434 idstr[len] = 0;
1435 instance_id = qemu_get_be32(f);
1436 version_id = qemu_get_be32(f);
1438 /* Find savevm section */
1439 se = find_se(idstr, instance_id);
1440 if (se == NULL) {
1441 fprintf(stderr, "Unknown savevm section or instance '%s' %d\n", idstr, instance_id);
1442 ret = -EINVAL;
1443 goto out;
1446 /* Validate version */
1447 if (version_id > se->version_id) {
1448 fprintf(stderr, "savevm: unsupported version %d for '%s' v%d\n",
1449 version_id, idstr, se->version_id);
1450 ret = -EINVAL;
1451 goto out;
1454 /* Add entry */
1455 le = qemu_mallocz(sizeof(*le));
1457 le->se = se;
1458 le->section_id = section_id;
1459 le->version_id = version_id;
1460 QLIST_INSERT_HEAD(&loadvm_handlers, le, entry);
1462 ret = vmstate_load(f, le->se, le->version_id);
1463 if (ret < 0) {
1464 fprintf(stderr, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
1465 instance_id, idstr);
1466 goto out;
1468 break;
1469 case QEMU_VM_SECTION_PART:
1470 case QEMU_VM_SECTION_END:
1471 section_id = qemu_get_be32(f);
1473 QLIST_FOREACH(le, &loadvm_handlers, entry) {
1474 if (le->section_id == section_id) {
1475 break;
1478 if (le == NULL) {
1479 fprintf(stderr, "Unknown savevm section %d\n", section_id);
1480 ret = -EINVAL;
1481 goto out;
1484 ret = vmstate_load(f, le->se, le->version_id);
1485 if (ret < 0) {
1486 fprintf(stderr, "qemu: warning: error while loading state section id %d\n",
1487 section_id);
1488 goto out;
1490 break;
1491 default:
1492 fprintf(stderr, "Unknown savevm section type %d\n", section_type);
1493 ret = -EINVAL;
1494 goto out;
1498 ret = 0;
1500 out:
1501 QLIST_FOREACH_SAFE(le, &loadvm_handlers, entry, new_le) {
1502 QLIST_REMOVE(le, entry);
1503 qemu_free(le);
1506 if (qemu_file_has_error(f))
1507 ret = -EIO;
1509 return ret;
1512 /* device can contain snapshots */
1513 static int bdrv_can_snapshot(BlockDriverState *bs)
1515 return (bs &&
1516 !bdrv_is_removable(bs) &&
1517 !bdrv_is_read_only(bs));
1520 /* device must be snapshots in order to have a reliable snapshot */
1521 static int bdrv_has_snapshot(BlockDriverState *bs)
1523 return (bs &&
1524 !bdrv_is_removable(bs) &&
1525 !bdrv_is_read_only(bs));
1528 static BlockDriverState *get_bs_snapshots(void)
1530 BlockDriverState *bs;
1531 DriveInfo *dinfo;
1533 if (bs_snapshots)
1534 return bs_snapshots;
1535 QTAILQ_FOREACH(dinfo, &drives, next) {
1536 bs = dinfo->bdrv;
1537 if (bdrv_can_snapshot(bs))
1538 goto ok;
1540 return NULL;
1542 bs_snapshots = bs;
1543 return bs;
1546 static int bdrv_snapshot_find(BlockDriverState *bs, QEMUSnapshotInfo *sn_info,
1547 const char *name)
1549 QEMUSnapshotInfo *sn_tab, *sn;
1550 int nb_sns, i, ret;
1552 ret = -ENOENT;
1553 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
1554 if (nb_sns < 0)
1555 return ret;
1556 for(i = 0; i < nb_sns; i++) {
1557 sn = &sn_tab[i];
1558 if (!strcmp(sn->id_str, name) || !strcmp(sn->name, name)) {
1559 *sn_info = *sn;
1560 ret = 0;
1561 break;
1564 qemu_free(sn_tab);
1565 return ret;
1569 * Deletes snapshots of a given name in all opened images.
1571 static int del_existing_snapshots(Monitor *mon, const char *name)
1573 BlockDriverState *bs;
1574 DriveInfo *dinfo;
1575 QEMUSnapshotInfo sn1, *snapshot = &sn1;
1576 int ret;
1578 QTAILQ_FOREACH(dinfo, &drives, next) {
1579 bs = dinfo->bdrv;
1580 if (bdrv_can_snapshot(bs) &&
1581 bdrv_snapshot_find(bs, snapshot, name) >= 0)
1583 ret = bdrv_snapshot_delete(bs, name);
1584 if (ret < 0) {
1585 monitor_printf(mon,
1586 "Error while deleting snapshot on '%s'\n",
1587 bdrv_get_device_name(bs));
1588 return -1;
1593 return 0;
1596 void do_savevm(Monitor *mon, const QDict *qdict)
1598 DriveInfo *dinfo;
1599 BlockDriverState *bs, *bs1;
1600 QEMUSnapshotInfo sn1, *sn = &sn1, old_sn1, *old_sn = &old_sn1;
1601 int ret;
1602 QEMUFile *f;
1603 int saved_vm_running;
1604 uint32_t vm_state_size;
1605 #ifdef _WIN32
1606 struct _timeb tb;
1607 #else
1608 struct timeval tv;
1609 #endif
1610 const char *name = qdict_get_try_str(qdict, "name");
1612 bs = get_bs_snapshots();
1613 if (!bs) {
1614 monitor_printf(mon, "No block device can accept snapshots\n");
1615 return;
1618 /* ??? Should this occur after vm_stop? */
1619 qemu_aio_flush();
1621 saved_vm_running = vm_running;
1622 vm_stop(0);
1624 memset(sn, 0, sizeof(*sn));
1625 if (name) {
1626 ret = bdrv_snapshot_find(bs, old_sn, name);
1627 if (ret >= 0) {
1628 pstrcpy(sn->name, sizeof(sn->name), old_sn->name);
1629 pstrcpy(sn->id_str, sizeof(sn->id_str), old_sn->id_str);
1630 } else {
1631 pstrcpy(sn->name, sizeof(sn->name), name);
1635 /* fill auxiliary fields */
1636 #ifdef _WIN32
1637 _ftime(&tb);
1638 sn->date_sec = tb.time;
1639 sn->date_nsec = tb.millitm * 1000000;
1640 #else
1641 gettimeofday(&tv, NULL);
1642 sn->date_sec = tv.tv_sec;
1643 sn->date_nsec = tv.tv_usec * 1000;
1644 #endif
1645 sn->vm_clock_nsec = qemu_get_clock(vm_clock);
1647 /* Delete old snapshots of the same name */
1648 if (del_existing_snapshots(mon, name) < 0) {
1649 goto the_end;
1652 /* save the VM state */
1653 f = qemu_fopen_bdrv(bs, 1);
1654 if (!f) {
1655 monitor_printf(mon, "Could not open VM state file\n");
1656 goto the_end;
1658 ret = qemu_savevm_state(f);
1659 vm_state_size = qemu_ftell(f);
1660 qemu_fclose(f);
1661 if (ret < 0) {
1662 monitor_printf(mon, "Error %d while writing VM\n", ret);
1663 goto the_end;
1666 /* create the snapshots */
1668 QTAILQ_FOREACH(dinfo, &drives, next) {
1669 bs1 = dinfo->bdrv;
1670 if (bdrv_has_snapshot(bs1)) {
1671 /* Write VM state size only to the image that contains the state */
1672 sn->vm_state_size = (bs == bs1 ? vm_state_size : 0);
1673 ret = bdrv_snapshot_create(bs1, sn);
1674 if (ret < 0) {
1675 monitor_printf(mon, "Error while creating snapshot on '%s'\n",
1676 bdrv_get_device_name(bs1));
1681 the_end:
1682 if (saved_vm_running)
1683 vm_start();
1686 int load_vmstate(Monitor *mon, const char *name)
1688 DriveInfo *dinfo;
1689 BlockDriverState *bs, *bs1;
1690 QEMUSnapshotInfo sn;
1691 QEMUFile *f;
1692 int ret;
1694 bs = get_bs_snapshots();
1695 if (!bs) {
1696 monitor_printf(mon, "No block device supports snapshots\n");
1697 return -EINVAL;
1700 /* Flush all IO requests so they don't interfere with the new state. */
1701 qemu_aio_flush();
1703 QTAILQ_FOREACH(dinfo, &drives, next) {
1704 bs1 = dinfo->bdrv;
1705 if (bdrv_has_snapshot(bs1)) {
1706 ret = bdrv_snapshot_goto(bs1, name);
1707 if (ret < 0) {
1708 if (bs != bs1)
1709 monitor_printf(mon, "Warning: ");
1710 switch(ret) {
1711 case -ENOTSUP:
1712 monitor_printf(mon,
1713 "Snapshots not supported on device '%s'\n",
1714 bdrv_get_device_name(bs1));
1715 break;
1716 case -ENOENT:
1717 monitor_printf(mon, "Could not find snapshot '%s' on "
1718 "device '%s'\n",
1719 name, bdrv_get_device_name(bs1));
1720 break;
1721 default:
1722 monitor_printf(mon, "Error %d while activating snapshot on"
1723 " '%s'\n", ret, bdrv_get_device_name(bs1));
1724 break;
1726 /* fatal on snapshot block device */
1727 if (bs == bs1)
1728 return 0;
1733 /* Don't even try to load empty VM states */
1734 ret = bdrv_snapshot_find(bs, &sn, name);
1735 if ((ret >= 0) && (sn.vm_state_size == 0))
1736 return -EINVAL;
1738 /* restore the VM state */
1739 f = qemu_fopen_bdrv(bs, 0);
1740 if (!f) {
1741 monitor_printf(mon, "Could not open VM state file\n");
1742 return -EINVAL;
1744 ret = qemu_loadvm_state(f);
1745 qemu_fclose(f);
1746 if (ret < 0) {
1747 monitor_printf(mon, "Error %d while loading VM state\n", ret);
1748 return ret;
1750 return 0;
1753 void do_delvm(Monitor *mon, const QDict *qdict)
1755 DriveInfo *dinfo;
1756 BlockDriverState *bs, *bs1;
1757 int ret;
1758 const char *name = qdict_get_str(qdict, "name");
1760 bs = get_bs_snapshots();
1761 if (!bs) {
1762 monitor_printf(mon, "No block device supports snapshots\n");
1763 return;
1766 QTAILQ_FOREACH(dinfo, &drives, next) {
1767 bs1 = dinfo->bdrv;
1768 if (bdrv_has_snapshot(bs1)) {
1769 ret = bdrv_snapshot_delete(bs1, name);
1770 if (ret < 0) {
1771 if (ret == -ENOTSUP)
1772 monitor_printf(mon,
1773 "Snapshots not supported on device '%s'\n",
1774 bdrv_get_device_name(bs1));
1775 else
1776 monitor_printf(mon, "Error %d while deleting snapshot on "
1777 "'%s'\n", ret, bdrv_get_device_name(bs1));
1783 void do_info_snapshots(Monitor *mon)
1785 DriveInfo *dinfo;
1786 BlockDriverState *bs, *bs1;
1787 QEMUSnapshotInfo *sn_tab, *sn;
1788 int nb_sns, i;
1789 char buf[256];
1791 bs = get_bs_snapshots();
1792 if (!bs) {
1793 monitor_printf(mon, "No available block device supports snapshots\n");
1794 return;
1796 monitor_printf(mon, "Snapshot devices:");
1797 QTAILQ_FOREACH(dinfo, &drives, next) {
1798 bs1 = dinfo->bdrv;
1799 if (bdrv_has_snapshot(bs1)) {
1800 if (bs == bs1)
1801 monitor_printf(mon, " %s", bdrv_get_device_name(bs1));
1804 monitor_printf(mon, "\n");
1806 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
1807 if (nb_sns < 0) {
1808 monitor_printf(mon, "bdrv_snapshot_list: error %d\n", nb_sns);
1809 return;
1811 monitor_printf(mon, "Snapshot list (from %s):\n",
1812 bdrv_get_device_name(bs));
1813 monitor_printf(mon, "%s\n", bdrv_snapshot_dump(buf, sizeof(buf), NULL));
1814 for(i = 0; i < nb_sns; i++) {
1815 sn = &sn_tab[i];
1816 monitor_printf(mon, "%s\n", bdrv_snapshot_dump(buf, sizeof(buf), sn));
1818 qemu_free(sn_tab);