vhost-net: fix reversed logic in mask notifiers
[qemu/qemu-dev-zwu.git] / savevm.c
blob017695bf020f3e25d3fa1480c1f2e9b9e75ddfc6
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(__FreeBSD_kernel__) || defined(__DragonFly__)
52 #include <libutil.h>
53 #else
54 #include <util.h>
55 #endif
56 #ifdef __linux__
57 #include <pty.h>
58 #include <malloc.h>
59 #include <linux/rtc.h>
60 #endif
61 #endif
62 #endif
64 #ifdef _WIN32
65 #include <windows.h>
66 #include <malloc.h>
67 #include <sys/timeb.h>
68 #include <mmsystem.h>
69 #define getopt_long_only getopt_long
70 #define memalign(align, size) malloc(size)
71 #endif
73 #include "qemu-common.h"
74 #include "hw/hw.h"
75 #include "net.h"
76 #include "monitor.h"
77 #include "sysemu.h"
78 #include "qemu-timer.h"
79 #include "qemu-char.h"
80 #include "block.h"
81 #include "audio/audio.h"
82 #include "migration.h"
83 #include "qemu_socket.h"
84 #include "qemu-queue.h"
86 /* point to the block driver where the snapshots are managed */
87 static BlockDriverState *bs_snapshots;
89 #define SELF_ANNOUNCE_ROUNDS 5
91 #ifndef ETH_P_RARP
92 #define ETH_P_RARP 0x8035
93 #endif
94 #define ARP_HTYPE_ETH 0x0001
95 #define ARP_PTYPE_IP 0x0800
96 #define ARP_OP_REQUEST_REV 0x3
98 static int announce_self_create(uint8_t *buf,
99 uint8_t *mac_addr)
101 /* Ethernet header. */
102 memset(buf, 0xff, 6); /* destination MAC addr */
103 memcpy(buf + 6, mac_addr, 6); /* source MAC addr */
104 *(uint16_t *)(buf + 12) = htons(ETH_P_RARP); /* ethertype */
106 /* RARP header. */
107 *(uint16_t *)(buf + 14) = htons(ARP_HTYPE_ETH); /* hardware addr space */
108 *(uint16_t *)(buf + 16) = htons(ARP_PTYPE_IP); /* protocol addr space */
109 *(buf + 18) = 6; /* hardware addr length (ethernet) */
110 *(buf + 19) = 4; /* protocol addr length (IPv4) */
111 *(uint16_t *)(buf + 20) = htons(ARP_OP_REQUEST_REV); /* opcode */
112 memcpy(buf + 22, mac_addr, 6); /* source hw addr */
113 memset(buf + 28, 0x00, 4); /* source protocol addr */
114 memcpy(buf + 32, mac_addr, 6); /* target hw addr */
115 memset(buf + 38, 0x00, 4); /* target protocol addr */
117 /* Padding to get up to 60 bytes (ethernet min packet size, minus FCS). */
118 memset(buf + 42, 0x00, 18);
120 return 60; /* len (FCS will be added by hardware) */
123 static void qemu_announce_self_iter(NICState *nic, void *opaque)
125 uint8_t buf[60];
126 int len;
128 len = announce_self_create(buf, nic->conf->macaddr.a);
130 qemu_send_packet_raw(&nic->nc, buf, len);
134 static void qemu_announce_self_once(void *opaque)
136 static int count = SELF_ANNOUNCE_ROUNDS;
137 QEMUTimer *timer = *(QEMUTimer **)opaque;
139 qemu_foreach_nic(qemu_announce_self_iter, NULL);
141 if (--count) {
142 /* delay 50ms, 150ms, 250ms, ... */
143 qemu_mod_timer(timer, qemu_get_clock(rt_clock) +
144 50 + (SELF_ANNOUNCE_ROUNDS - count - 1) * 100);
145 } else {
146 qemu_del_timer(timer);
147 qemu_free_timer(timer);
151 void qemu_announce_self(void)
153 static QEMUTimer *timer;
154 timer = qemu_new_timer(rt_clock, qemu_announce_self_once, &timer);
155 qemu_announce_self_once(&timer);
158 /***********************************************************/
159 /* savevm/loadvm support */
161 #define IO_BUF_SIZE 32768
163 struct QEMUFile {
164 QEMUFilePutBufferFunc *put_buffer;
165 QEMUFileGetBufferFunc *get_buffer;
166 QEMUFileCloseFunc *close;
167 QEMUFileRateLimit *rate_limit;
168 QEMUFileSetRateLimit *set_rate_limit;
169 QEMUFileGetRateLimit *get_rate_limit;
170 void *opaque;
171 int is_write;
173 int64_t buf_offset; /* start of buffer when writing, end of buffer
174 when reading */
175 int buf_index;
176 int buf_size; /* 0 when writing */
177 uint8_t buf[IO_BUF_SIZE];
179 int has_error;
182 typedef struct QEMUFileStdio
184 FILE *stdio_file;
185 QEMUFile *file;
186 } QEMUFileStdio;
188 typedef struct QEMUFileSocket
190 int fd;
191 QEMUFile *file;
192 } QEMUFileSocket;
194 static int socket_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
196 QEMUFileSocket *s = opaque;
197 ssize_t len;
199 do {
200 len = recv(s->fd, (void *)buf, size, 0);
201 } while (len == -1 && socket_error() == EINTR);
203 if (len == -1)
204 len = -socket_error();
206 return len;
209 static int socket_close(void *opaque)
211 QEMUFileSocket *s = opaque;
212 qemu_free(s);
213 return 0;
216 static int stdio_put_buffer(void *opaque, const uint8_t *buf, int64_t pos, int size)
218 QEMUFileStdio *s = opaque;
219 return fwrite(buf, 1, size, s->stdio_file);
222 static int stdio_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
224 QEMUFileStdio *s = opaque;
225 FILE *fp = s->stdio_file;
226 int bytes;
228 do {
229 clearerr(fp);
230 bytes = fread(buf, 1, size, fp);
231 } while ((bytes == 0) && ferror(fp) && (errno == EINTR));
232 return bytes;
235 static int stdio_pclose(void *opaque)
237 QEMUFileStdio *s = opaque;
238 pclose(s->stdio_file);
239 qemu_free(s);
240 return 0;
243 static int stdio_fclose(void *opaque)
245 QEMUFileStdio *s = opaque;
246 fclose(s->stdio_file);
247 qemu_free(s);
248 return 0;
251 QEMUFile *qemu_popen(FILE *stdio_file, const char *mode)
253 QEMUFileStdio *s;
255 if (stdio_file == NULL || mode == NULL || (mode[0] != 'r' && mode[0] != 'w') || mode[1] != 0) {
256 fprintf(stderr, "qemu_popen: Argument validity check failed\n");
257 return NULL;
260 s = qemu_mallocz(sizeof(QEMUFileStdio));
262 s->stdio_file = stdio_file;
264 if(mode[0] == 'r') {
265 s->file = qemu_fopen_ops(s, NULL, stdio_get_buffer, stdio_pclose,
266 NULL, NULL, NULL);
267 } else {
268 s->file = qemu_fopen_ops(s, stdio_put_buffer, NULL, stdio_pclose,
269 NULL, 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,
315 NULL, NULL, NULL);
316 } else {
317 s->file = qemu_fopen_ops(s, stdio_put_buffer, NULL, stdio_fclose,
318 NULL, NULL, NULL);
320 return s->file;
322 fail:
323 qemu_free(s);
324 return NULL;
327 QEMUFile *qemu_fopen_socket(int fd)
329 QEMUFileSocket *s = qemu_mallocz(sizeof(QEMUFileSocket));
331 s->fd = fd;
332 s->file = qemu_fopen_ops(s, NULL, socket_get_buffer, socket_close,
333 NULL, NULL, NULL);
334 return s->file;
337 static int file_put_buffer(void *opaque, const uint8_t *buf,
338 int64_t pos, int size)
340 QEMUFileStdio *s = opaque;
341 fseek(s->stdio_file, pos, SEEK_SET);
342 return fwrite(buf, 1, size, s->stdio_file);
345 static int file_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
347 QEMUFileStdio *s = opaque;
348 fseek(s->stdio_file, pos, SEEK_SET);
349 return fread(buf, 1, size, s->stdio_file);
352 QEMUFile *qemu_fopen(const char *filename, const char *mode)
354 QEMUFileStdio *s;
356 if (mode == NULL ||
357 (mode[0] != 'r' && mode[0] != 'w') ||
358 mode[1] != 'b' || mode[2] != 0) {
359 fprintf(stderr, "qemu_fopen: Argument validity check failed\n");
360 return NULL;
363 s = qemu_mallocz(sizeof(QEMUFileStdio));
365 s->stdio_file = fopen(filename, mode);
366 if (!s->stdio_file)
367 goto fail;
369 if(mode[0] == 'w') {
370 s->file = qemu_fopen_ops(s, file_put_buffer, NULL, stdio_fclose,
371 NULL, NULL, NULL);
372 } else {
373 s->file = qemu_fopen_ops(s, NULL, file_get_buffer, stdio_fclose,
374 NULL, NULL, NULL);
376 return s->file;
377 fail:
378 qemu_free(s);
379 return NULL;
382 static int block_put_buffer(void *opaque, const uint8_t *buf,
383 int64_t pos, int size)
385 bdrv_save_vmstate(opaque, buf, pos, size);
386 return size;
389 static int block_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
391 return bdrv_load_vmstate(opaque, buf, pos, size);
394 static int bdrv_fclose(void *opaque)
396 return 0;
399 static QEMUFile *qemu_fopen_bdrv(BlockDriverState *bs, int is_writable)
401 if (is_writable)
402 return qemu_fopen_ops(bs, block_put_buffer, NULL, bdrv_fclose,
403 NULL, NULL, NULL);
404 return qemu_fopen_ops(bs, NULL, block_get_buffer, bdrv_fclose, NULL, NULL, NULL);
407 QEMUFile *qemu_fopen_ops(void *opaque, QEMUFilePutBufferFunc *put_buffer,
408 QEMUFileGetBufferFunc *get_buffer,
409 QEMUFileCloseFunc *close,
410 QEMUFileRateLimit *rate_limit,
411 QEMUFileSetRateLimit *set_rate_limit,
412 QEMUFileGetRateLimit *get_rate_limit)
414 QEMUFile *f;
416 f = qemu_mallocz(sizeof(QEMUFile));
418 f->opaque = opaque;
419 f->put_buffer = put_buffer;
420 f->get_buffer = get_buffer;
421 f->close = close;
422 f->rate_limit = rate_limit;
423 f->set_rate_limit = set_rate_limit;
424 f->get_rate_limit = get_rate_limit;
425 f->is_write = 0;
427 return f;
430 int qemu_file_has_error(QEMUFile *f)
432 return f->has_error;
435 void qemu_file_set_error(QEMUFile *f)
437 f->has_error = 1;
440 void qemu_fflush(QEMUFile *f)
442 if (!f->put_buffer)
443 return;
445 if (f->is_write && f->buf_index > 0) {
446 int len;
448 len = f->put_buffer(f->opaque, f->buf, f->buf_offset, f->buf_index);
449 if (len > 0)
450 f->buf_offset += f->buf_index;
451 else
452 f->has_error = 1;
453 f->buf_index = 0;
457 static void qemu_fill_buffer(QEMUFile *f)
459 int len;
461 if (!f->get_buffer)
462 return;
464 if (f->is_write)
465 abort();
467 len = f->get_buffer(f->opaque, f->buf, f->buf_offset, IO_BUF_SIZE);
468 if (len > 0) {
469 f->buf_index = 0;
470 f->buf_size = len;
471 f->buf_offset += len;
472 } else if (len != -EAGAIN)
473 f->has_error = 1;
476 int qemu_fclose(QEMUFile *f)
478 int ret = 0;
479 qemu_fflush(f);
480 if (f->close)
481 ret = f->close(f->opaque);
482 qemu_free(f);
483 return ret;
486 void qemu_file_put_notify(QEMUFile *f)
488 f->put_buffer(f->opaque, NULL, 0, 0);
491 void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size)
493 int l;
495 if (!f->has_error && f->is_write == 0 && f->buf_index > 0) {
496 fprintf(stderr,
497 "Attempted to write to buffer while read buffer is not empty\n");
498 abort();
501 while (!f->has_error && size > 0) {
502 l = IO_BUF_SIZE - f->buf_index;
503 if (l > size)
504 l = size;
505 memcpy(f->buf + f->buf_index, buf, l);
506 f->is_write = 1;
507 f->buf_index += l;
508 buf += l;
509 size -= l;
510 if (f->buf_index >= IO_BUF_SIZE)
511 qemu_fflush(f);
515 void qemu_put_byte(QEMUFile *f, int v)
517 if (!f->has_error && f->is_write == 0 && f->buf_index > 0) {
518 fprintf(stderr,
519 "Attempted to write to buffer while read buffer is not empty\n");
520 abort();
523 f->buf[f->buf_index++] = v;
524 f->is_write = 1;
525 if (f->buf_index >= IO_BUF_SIZE)
526 qemu_fflush(f);
529 int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size1)
531 int size, l;
533 if (f->is_write)
534 abort();
536 size = size1;
537 while (size > 0) {
538 l = f->buf_size - f->buf_index;
539 if (l == 0) {
540 qemu_fill_buffer(f);
541 l = f->buf_size - f->buf_index;
542 if (l == 0)
543 break;
545 if (l > size)
546 l = size;
547 memcpy(buf, f->buf + f->buf_index, l);
548 f->buf_index += l;
549 buf += l;
550 size -= l;
552 return size1 - size;
555 int qemu_get_byte(QEMUFile *f)
557 if (f->is_write)
558 abort();
560 if (f->buf_index >= f->buf_size) {
561 qemu_fill_buffer(f);
562 if (f->buf_index >= f->buf_size)
563 return 0;
565 return f->buf[f->buf_index++];
568 int64_t qemu_ftell(QEMUFile *f)
570 return f->buf_offset - f->buf_size + f->buf_index;
573 int64_t qemu_fseek(QEMUFile *f, int64_t pos, int whence)
575 if (whence == SEEK_SET) {
576 /* nothing to do */
577 } else if (whence == SEEK_CUR) {
578 pos += qemu_ftell(f);
579 } else {
580 /* SEEK_END not supported */
581 return -1;
583 if (f->put_buffer) {
584 qemu_fflush(f);
585 f->buf_offset = pos;
586 } else {
587 f->buf_offset = pos;
588 f->buf_index = 0;
589 f->buf_size = 0;
591 return pos;
594 int qemu_file_rate_limit(QEMUFile *f)
596 if (f->rate_limit)
597 return f->rate_limit(f->opaque);
599 return 0;
602 size_t qemu_file_get_rate_limit(QEMUFile *f)
604 if (f->get_rate_limit)
605 return f->get_rate_limit(f->opaque);
607 return 0;
610 size_t qemu_file_set_rate_limit(QEMUFile *f, size_t new_rate)
612 /* any failed or completed migration keeps its state to allow probing of
613 * migration data, but has no associated file anymore */
614 if (f && f->set_rate_limit)
615 return f->set_rate_limit(f->opaque, new_rate);
617 return 0;
620 void qemu_put_be16(QEMUFile *f, unsigned int v)
622 qemu_put_byte(f, v >> 8);
623 qemu_put_byte(f, v);
626 void qemu_put_be32(QEMUFile *f, unsigned int v)
628 qemu_put_byte(f, v >> 24);
629 qemu_put_byte(f, v >> 16);
630 qemu_put_byte(f, v >> 8);
631 qemu_put_byte(f, v);
634 void qemu_put_be64(QEMUFile *f, uint64_t v)
636 qemu_put_be32(f, v >> 32);
637 qemu_put_be32(f, v);
640 unsigned int qemu_get_be16(QEMUFile *f)
642 unsigned int v;
643 v = qemu_get_byte(f) << 8;
644 v |= qemu_get_byte(f);
645 return v;
648 unsigned int qemu_get_be32(QEMUFile *f)
650 unsigned int v;
651 v = qemu_get_byte(f) << 24;
652 v |= qemu_get_byte(f) << 16;
653 v |= qemu_get_byte(f) << 8;
654 v |= qemu_get_byte(f);
655 return v;
658 uint64_t qemu_get_be64(QEMUFile *f)
660 uint64_t v;
661 v = (uint64_t)qemu_get_be32(f) << 32;
662 v |= qemu_get_be32(f);
663 return v;
666 /* 8 bit int */
668 static int get_int8(QEMUFile *f, void *pv, size_t size)
670 int8_t *v = pv;
671 qemu_get_s8s(f, v);
672 return 0;
675 static void put_int8(QEMUFile *f, void *pv, size_t size)
677 int8_t *v = pv;
678 qemu_put_s8s(f, v);
681 const VMStateInfo vmstate_info_int8 = {
682 .name = "int8",
683 .get = get_int8,
684 .put = put_int8,
687 /* 16 bit int */
689 static int get_int16(QEMUFile *f, void *pv, size_t size)
691 int16_t *v = pv;
692 qemu_get_sbe16s(f, v);
693 return 0;
696 static void put_int16(QEMUFile *f, void *pv, size_t size)
698 int16_t *v = pv;
699 qemu_put_sbe16s(f, v);
702 const VMStateInfo vmstate_info_int16 = {
703 .name = "int16",
704 .get = get_int16,
705 .put = put_int16,
708 /* 32 bit int */
710 static int get_int32(QEMUFile *f, void *pv, size_t size)
712 int32_t *v = pv;
713 qemu_get_sbe32s(f, v);
714 return 0;
717 static void put_int32(QEMUFile *f, void *pv, size_t size)
719 int32_t *v = pv;
720 qemu_put_sbe32s(f, v);
723 const VMStateInfo vmstate_info_int32 = {
724 .name = "int32",
725 .get = get_int32,
726 .put = put_int32,
729 /* 32 bit int. See that the received value is the same than the one
730 in the field */
732 static int get_int32_equal(QEMUFile *f, void *pv, size_t size)
734 int32_t *v = pv;
735 int32_t v2;
736 qemu_get_sbe32s(f, &v2);
738 if (*v == v2)
739 return 0;
740 return -EINVAL;
743 const VMStateInfo vmstate_info_int32_equal = {
744 .name = "int32 equal",
745 .get = get_int32_equal,
746 .put = put_int32,
749 /* 32 bit int. See that the received value is the less or the same
750 than the one in the field */
752 static int get_int32_le(QEMUFile *f, void *pv, size_t size)
754 int32_t *old = pv;
755 int32_t new;
756 qemu_get_sbe32s(f, &new);
758 if (*old <= new)
759 return 0;
760 return -EINVAL;
763 const VMStateInfo vmstate_info_int32_le = {
764 .name = "int32 equal",
765 .get = get_int32_le,
766 .put = put_int32,
769 /* 64 bit int */
771 static int get_int64(QEMUFile *f, void *pv, size_t size)
773 int64_t *v = pv;
774 qemu_get_sbe64s(f, v);
775 return 0;
778 static void put_int64(QEMUFile *f, void *pv, size_t size)
780 int64_t *v = pv;
781 qemu_put_sbe64s(f, v);
784 const VMStateInfo vmstate_info_int64 = {
785 .name = "int64",
786 .get = get_int64,
787 .put = put_int64,
790 /* 8 bit unsigned int */
792 static int get_uint8(QEMUFile *f, void *pv, size_t size)
794 uint8_t *v = pv;
795 qemu_get_8s(f, v);
796 return 0;
799 static void put_uint8(QEMUFile *f, void *pv, size_t size)
801 uint8_t *v = pv;
802 qemu_put_8s(f, v);
805 const VMStateInfo vmstate_info_uint8 = {
806 .name = "uint8",
807 .get = get_uint8,
808 .put = put_uint8,
811 /* 16 bit unsigned int */
813 static int get_uint16(QEMUFile *f, void *pv, size_t size)
815 uint16_t *v = pv;
816 qemu_get_be16s(f, v);
817 return 0;
820 static void put_uint16(QEMUFile *f, void *pv, size_t size)
822 uint16_t *v = pv;
823 qemu_put_be16s(f, v);
826 const VMStateInfo vmstate_info_uint16 = {
827 .name = "uint16",
828 .get = get_uint16,
829 .put = put_uint16,
832 /* 32 bit unsigned int */
834 static int get_uint32(QEMUFile *f, void *pv, size_t size)
836 uint32_t *v = pv;
837 qemu_get_be32s(f, v);
838 return 0;
841 static void put_uint32(QEMUFile *f, void *pv, size_t size)
843 uint32_t *v = pv;
844 qemu_put_be32s(f, v);
847 const VMStateInfo vmstate_info_uint32 = {
848 .name = "uint32",
849 .get = get_uint32,
850 .put = put_uint32,
853 /* 64 bit unsigned int */
855 static int get_uint64(QEMUFile *f, void *pv, size_t size)
857 uint64_t *v = pv;
858 qemu_get_be64s(f, v);
859 return 0;
862 static void put_uint64(QEMUFile *f, void *pv, size_t size)
864 uint64_t *v = pv;
865 qemu_put_be64s(f, v);
868 const VMStateInfo vmstate_info_uint64 = {
869 .name = "uint64",
870 .get = get_uint64,
871 .put = put_uint64,
874 /* 64 bit linux kernel unsigned int */
876 #ifdef __linux__
877 static int get_u64(QEMUFile *f, void *pv, size_t size)
879 uint64_t *v = pv;
880 qemu_get_be64s(f, v);
881 return 0;
884 static void put_u64(QEMUFile *f, void *pv, size_t size)
886 uint64_t *v = pv;
887 qemu_put_be64s(f, v);
890 const VMStateInfo vmstate_info_u64 = {
891 .name = "__u64",
892 .get = get_u64,
893 .put = put_u64,
895 #endif /* __linux__ */
897 /* 8 bit int. See that the received value is the same than the one
898 in the field */
900 static int get_uint8_equal(QEMUFile *f, void *pv, size_t size)
902 uint8_t *v = pv;
903 uint8_t v2;
904 qemu_get_8s(f, &v2);
906 if (*v == v2)
907 return 0;
908 return -EINVAL;
911 const VMStateInfo vmstate_info_uint8_equal = {
912 .name = "uint8 equal",
913 .get = get_uint8_equal,
914 .put = put_uint8,
917 /* 16 bit unsigned int int. See that the received value is the same than the one
918 in the field */
920 static int get_uint16_equal(QEMUFile *f, void *pv, size_t size)
922 uint16_t *v = pv;
923 uint16_t v2;
924 qemu_get_be16s(f, &v2);
926 if (*v == v2)
927 return 0;
928 return -EINVAL;
931 const VMStateInfo vmstate_info_uint16_equal = {
932 .name = "uint16 equal",
933 .get = get_uint16_equal,
934 .put = put_uint16,
937 /* timers */
939 static int get_timer(QEMUFile *f, void *pv, size_t size)
941 QEMUTimer *v = pv;
942 qemu_get_timer(f, v);
943 return 0;
946 static void put_timer(QEMUFile *f, void *pv, size_t size)
948 QEMUTimer *v = pv;
949 qemu_put_timer(f, v);
952 const VMStateInfo vmstate_info_timer = {
953 .name = "timer",
954 .get = get_timer,
955 .put = put_timer,
958 /* uint8_t buffers */
960 static int get_buffer(QEMUFile *f, void *pv, size_t size)
962 uint8_t *v = pv;
963 qemu_get_buffer(f, v, size);
964 return 0;
967 static void put_buffer(QEMUFile *f, void *pv, size_t size)
969 uint8_t *v = pv;
970 qemu_put_buffer(f, v, size);
973 const VMStateInfo vmstate_info_buffer = {
974 .name = "buffer",
975 .get = get_buffer,
976 .put = put_buffer,
979 /* unused buffers: space that was used for some fields that are
980 not usefull anymore */
982 static int get_unused_buffer(QEMUFile *f, void *pv, size_t size)
984 uint8_t buf[1024];
985 int block_len;
987 while (size > 0) {
988 block_len = MIN(sizeof(buf), size);
989 size -= block_len;
990 qemu_get_buffer(f, buf, block_len);
992 return 0;
995 static void put_unused_buffer(QEMUFile *f, void *pv, size_t size)
997 static const uint8_t buf[1024];
998 int block_len;
1000 while (size > 0) {
1001 block_len = MIN(sizeof(buf), size);
1002 size -= block_len;
1003 qemu_put_buffer(f, buf, block_len);
1007 const VMStateInfo vmstate_info_unused_buffer = {
1008 .name = "unused_buffer",
1009 .get = get_unused_buffer,
1010 .put = put_unused_buffer,
1013 typedef struct SaveStateEntry {
1014 QTAILQ_ENTRY(SaveStateEntry) entry;
1015 char idstr[256];
1016 int instance_id;
1017 int alias_id;
1018 int version_id;
1019 int section_id;
1020 SaveSetParamsHandler *set_params;
1021 SaveLiveStateHandler *save_live_state;
1022 SaveStateHandler *save_state;
1023 LoadStateHandler *load_state;
1024 const VMStateDescription *vmsd;
1025 void *opaque;
1026 } SaveStateEntry;
1029 static QTAILQ_HEAD(savevm_handlers, SaveStateEntry) savevm_handlers =
1030 QTAILQ_HEAD_INITIALIZER(savevm_handlers);
1031 static int global_section_id;
1033 static int calculate_new_instance_id(const char *idstr)
1035 SaveStateEntry *se;
1036 int instance_id = 0;
1038 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1039 if (strcmp(idstr, se->idstr) == 0
1040 && instance_id <= se->instance_id) {
1041 instance_id = se->instance_id + 1;
1044 return instance_id;
1047 /* TODO: Individual devices generally have very little idea about the rest
1048 of the system, so instance_id should be removed/replaced.
1049 Meanwhile pass -1 as instance_id if you do not already have a clearly
1050 distinguishing id for all instances of your device class. */
1051 int register_savevm_live(const char *idstr,
1052 int instance_id,
1053 int version_id,
1054 SaveSetParamsHandler *set_params,
1055 SaveLiveStateHandler *save_live_state,
1056 SaveStateHandler *save_state,
1057 LoadStateHandler *load_state,
1058 void *opaque)
1060 SaveStateEntry *se;
1062 se = qemu_mallocz(sizeof(SaveStateEntry));
1063 pstrcpy(se->idstr, sizeof(se->idstr), idstr);
1064 se->version_id = version_id;
1065 se->section_id = global_section_id++;
1066 se->set_params = set_params;
1067 se->save_live_state = save_live_state;
1068 se->save_state = save_state;
1069 se->load_state = load_state;
1070 se->opaque = opaque;
1071 se->vmsd = NULL;
1073 if (instance_id == -1) {
1074 se->instance_id = calculate_new_instance_id(idstr);
1075 } else {
1076 se->instance_id = instance_id;
1078 /* add at the end of list */
1079 QTAILQ_INSERT_TAIL(&savevm_handlers, se, entry);
1080 return 0;
1083 int register_savevm(const char *idstr,
1084 int instance_id,
1085 int version_id,
1086 SaveStateHandler *save_state,
1087 LoadStateHandler *load_state,
1088 void *opaque)
1090 return register_savevm_live(idstr, instance_id, version_id,
1091 NULL, NULL, save_state, load_state, opaque);
1094 void unregister_savevm(const char *idstr, void *opaque)
1096 SaveStateEntry *se, *new_se;
1098 QTAILQ_FOREACH_SAFE(se, &savevm_handlers, entry, new_se) {
1099 if (strcmp(se->idstr, idstr) == 0 && se->opaque == opaque) {
1100 QTAILQ_REMOVE(&savevm_handlers, se, entry);
1101 qemu_free(se);
1106 int vmstate_register_with_alias_id(int instance_id,
1107 const VMStateDescription *vmsd,
1108 void *opaque, int alias_id,
1109 int required_for_version)
1111 SaveStateEntry *se;
1113 /* If this triggers, alias support can be dropped for the vmsd. */
1114 assert(alias_id == -1 || required_for_version >= vmsd->minimum_version_id);
1116 se = qemu_mallocz(sizeof(SaveStateEntry));
1117 pstrcpy(se->idstr, sizeof(se->idstr), vmsd->name);
1118 se->version_id = vmsd->version_id;
1119 se->section_id = global_section_id++;
1120 se->save_live_state = NULL;
1121 se->save_state = NULL;
1122 se->load_state = NULL;
1123 se->opaque = opaque;
1124 se->vmsd = vmsd;
1125 se->alias_id = alias_id;
1127 if (instance_id == -1) {
1128 se->instance_id = calculate_new_instance_id(vmsd->name);
1129 } else {
1130 se->instance_id = instance_id;
1132 /* add at the end of list */
1133 QTAILQ_INSERT_TAIL(&savevm_handlers, se, entry);
1134 return 0;
1137 int vmstate_register(int instance_id, const VMStateDescription *vmsd,
1138 void *opaque)
1140 return vmstate_register_with_alias_id(instance_id, vmsd, opaque, -1, 0);
1143 void vmstate_unregister(const VMStateDescription *vmsd, void *opaque)
1145 SaveStateEntry *se, *new_se;
1147 QTAILQ_FOREACH_SAFE(se, &savevm_handlers, entry, new_se) {
1148 if (se->vmsd == vmsd && se->opaque == opaque) {
1149 QTAILQ_REMOVE(&savevm_handlers, se, entry);
1150 qemu_free(se);
1155 int vmstate_load_state(QEMUFile *f, const VMStateDescription *vmsd,
1156 void *opaque, int version_id)
1158 VMStateField *field = vmsd->fields;
1160 if (version_id > vmsd->version_id) {
1161 return -EINVAL;
1163 if (version_id < vmsd->minimum_version_id_old) {
1164 return -EINVAL;
1166 if (version_id < vmsd->minimum_version_id) {
1167 return vmsd->load_state_old(f, opaque, version_id);
1169 if (vmsd->pre_load) {
1170 int ret = vmsd->pre_load(opaque);
1171 if (ret)
1172 return ret;
1174 while(field->name) {
1175 if ((field->field_exists &&
1176 field->field_exists(opaque, version_id)) ||
1177 (!field->field_exists &&
1178 field->version_id <= version_id)) {
1179 void *base_addr = opaque + field->offset;
1180 int ret, i, n_elems = 1;
1181 int size = field->size;
1183 if (field->flags & VMS_VBUFFER) {
1184 size = *(int32_t *)(opaque+field->size_offset);
1185 if (field->flags & VMS_MULTIPLY) {
1186 size *= field->size;
1189 if (field->flags & VMS_ARRAY) {
1190 n_elems = field->num;
1191 } else if (field->flags & VMS_VARRAY_INT32) {
1192 n_elems = *(int32_t *)(opaque+field->num_offset);
1193 } else if (field->flags & VMS_VARRAY_UINT16) {
1194 n_elems = *(uint16_t *)(opaque+field->num_offset);
1196 if (field->flags & VMS_POINTER) {
1197 base_addr = *(void **)base_addr + field->start;
1199 for (i = 0; i < n_elems; i++) {
1200 void *addr = base_addr + size * i;
1202 if (field->flags & VMS_ARRAY_OF_POINTER) {
1203 addr = *(void **)addr;
1205 if (field->flags & VMS_STRUCT) {
1206 ret = vmstate_load_state(f, field->vmsd, addr, field->vmsd->version_id);
1207 } else {
1208 ret = field->info->get(f, addr, size);
1211 if (ret < 0) {
1212 return ret;
1216 field++;
1218 if (vmsd->post_load) {
1219 return vmsd->post_load(opaque, version_id);
1221 return 0;
1224 void vmstate_save_state(QEMUFile *f, const VMStateDescription *vmsd,
1225 void *opaque)
1227 VMStateField *field = vmsd->fields;
1229 if (vmsd->pre_save) {
1230 vmsd->pre_save(opaque);
1232 while(field->name) {
1233 if (!field->field_exists ||
1234 field->field_exists(opaque, vmsd->version_id)) {
1235 void *base_addr = opaque + field->offset;
1236 int i, n_elems = 1;
1237 int size = field->size;
1239 if (field->flags & VMS_VBUFFER) {
1240 size = *(int32_t *)(opaque+field->size_offset);
1241 if (field->flags & VMS_MULTIPLY) {
1242 size *= field->size;
1245 if (field->flags & VMS_ARRAY) {
1246 n_elems = field->num;
1247 } else if (field->flags & VMS_VARRAY_INT32) {
1248 n_elems = *(int32_t *)(opaque+field->num_offset);
1249 } else if (field->flags & VMS_VARRAY_UINT16) {
1250 n_elems = *(uint16_t *)(opaque+field->num_offset);
1252 if (field->flags & VMS_POINTER) {
1253 base_addr = *(void **)base_addr + field->start;
1255 for (i = 0; i < n_elems; i++) {
1256 void *addr = base_addr + size * i;
1258 if (field->flags & VMS_ARRAY_OF_POINTER) {
1259 addr = *(void **)addr;
1261 if (field->flags & VMS_STRUCT) {
1262 vmstate_save_state(f, field->vmsd, addr);
1263 } else {
1264 field->info->put(f, addr, size);
1268 field++;
1272 static int vmstate_load(QEMUFile *f, SaveStateEntry *se, int version_id)
1274 if (!se->vmsd) { /* Old style */
1275 return se->load_state(f, se->opaque, version_id);
1277 return vmstate_load_state(f, se->vmsd, se->opaque, version_id);
1280 static void vmstate_save(QEMUFile *f, SaveStateEntry *se)
1282 if (!se->vmsd) { /* Old style */
1283 se->save_state(f, se->opaque);
1284 return;
1286 vmstate_save_state(f,se->vmsd, se->opaque);
1289 #define QEMU_VM_FILE_MAGIC 0x5145564d
1290 #define QEMU_VM_FILE_VERSION_COMPAT 0x00000002
1291 #define QEMU_VM_FILE_VERSION 0x00000003
1293 #define QEMU_VM_EOF 0x00
1294 #define QEMU_VM_SECTION_START 0x01
1295 #define QEMU_VM_SECTION_PART 0x02
1296 #define QEMU_VM_SECTION_END 0x03
1297 #define QEMU_VM_SECTION_FULL 0x04
1299 int qemu_savevm_state_begin(Monitor *mon, QEMUFile *f, int blk_enable,
1300 int shared)
1302 SaveStateEntry *se;
1304 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1305 if(se->set_params == NULL) {
1306 continue;
1308 se->set_params(blk_enable, shared, se->opaque);
1311 qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
1312 qemu_put_be32(f, QEMU_VM_FILE_VERSION);
1314 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1315 int len;
1317 if (se->save_live_state == NULL)
1318 continue;
1320 /* Section type */
1321 qemu_put_byte(f, QEMU_VM_SECTION_START);
1322 qemu_put_be32(f, se->section_id);
1324 /* ID string */
1325 len = strlen(se->idstr);
1326 qemu_put_byte(f, len);
1327 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
1329 qemu_put_be32(f, se->instance_id);
1330 qemu_put_be32(f, se->version_id);
1332 se->save_live_state(mon, f, QEMU_VM_SECTION_START, se->opaque);
1335 if (qemu_file_has_error(f)) {
1336 qemu_savevm_state_cancel(mon, f);
1337 return -EIO;
1340 return 0;
1343 int qemu_savevm_state_iterate(Monitor *mon, QEMUFile *f)
1345 SaveStateEntry *se;
1346 int ret = 1;
1348 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1349 if (se->save_live_state == NULL)
1350 continue;
1352 /* Section type */
1353 qemu_put_byte(f, QEMU_VM_SECTION_PART);
1354 qemu_put_be32(f, se->section_id);
1356 ret = se->save_live_state(mon, f, QEMU_VM_SECTION_PART, se->opaque);
1357 if (!ret) {
1358 /* Do not proceed to the next vmstate before this one reported
1359 completion of the current stage. This serializes the migration
1360 and reduces the probability that a faster changing state is
1361 synchronized over and over again. */
1362 break;
1366 if (ret)
1367 return 1;
1369 if (qemu_file_has_error(f)) {
1370 qemu_savevm_state_cancel(mon, f);
1371 return -EIO;
1374 return 0;
1377 int qemu_savevm_state_complete(Monitor *mon, QEMUFile *f)
1379 SaveStateEntry *se;
1381 cpu_synchronize_all_states();
1383 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1384 if (se->save_live_state == NULL)
1385 continue;
1387 /* Section type */
1388 qemu_put_byte(f, QEMU_VM_SECTION_END);
1389 qemu_put_be32(f, se->section_id);
1391 se->save_live_state(mon, f, QEMU_VM_SECTION_END, se->opaque);
1394 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1395 int len;
1397 if (se->save_state == NULL && se->vmsd == NULL)
1398 continue;
1400 /* Section type */
1401 qemu_put_byte(f, QEMU_VM_SECTION_FULL);
1402 qemu_put_be32(f, se->section_id);
1404 /* ID string */
1405 len = strlen(se->idstr);
1406 qemu_put_byte(f, len);
1407 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
1409 qemu_put_be32(f, se->instance_id);
1410 qemu_put_be32(f, se->version_id);
1412 vmstate_save(f, se);
1415 qemu_put_byte(f, QEMU_VM_EOF);
1417 if (qemu_file_has_error(f))
1418 return -EIO;
1420 return 0;
1423 void qemu_savevm_state_cancel(Monitor *mon, QEMUFile *f)
1425 SaveStateEntry *se;
1427 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1428 if (se->save_live_state) {
1429 se->save_live_state(mon, f, -1, se->opaque);
1434 static int qemu_savevm_state(Monitor *mon, QEMUFile *f)
1436 int saved_vm_running;
1437 int ret;
1439 saved_vm_running = vm_running;
1440 vm_stop(0);
1442 bdrv_flush_all();
1444 ret = qemu_savevm_state_begin(mon, f, 0, 0);
1445 if (ret < 0)
1446 goto out;
1448 do {
1449 ret = qemu_savevm_state_iterate(mon, f);
1450 if (ret < 0)
1451 goto out;
1452 } while (ret == 0);
1454 ret = qemu_savevm_state_complete(mon, f);
1456 out:
1457 if (qemu_file_has_error(f))
1458 ret = -EIO;
1460 if (!ret && saved_vm_running)
1461 vm_start();
1463 return ret;
1466 static SaveStateEntry *find_se(const char *idstr, int instance_id)
1468 SaveStateEntry *se;
1470 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1471 if (!strcmp(se->idstr, idstr) &&
1472 (instance_id == se->instance_id ||
1473 instance_id == se->alias_id))
1474 return se;
1476 return NULL;
1479 typedef struct LoadStateEntry {
1480 QLIST_ENTRY(LoadStateEntry) entry;
1481 SaveStateEntry *se;
1482 int section_id;
1483 int version_id;
1484 } LoadStateEntry;
1486 int qemu_loadvm_state(QEMUFile *f)
1488 QLIST_HEAD(, LoadStateEntry) loadvm_handlers =
1489 QLIST_HEAD_INITIALIZER(loadvm_handlers);
1490 LoadStateEntry *le, *new_le;
1491 uint8_t section_type;
1492 unsigned int v;
1493 int ret;
1495 v = qemu_get_be32(f);
1496 if (v != QEMU_VM_FILE_MAGIC)
1497 return -EINVAL;
1499 v = qemu_get_be32(f);
1500 if (v == QEMU_VM_FILE_VERSION_COMPAT) {
1501 fprintf(stderr, "SaveVM v2 format is obsolete and don't work anymore\n");
1502 return -ENOTSUP;
1504 if (v != QEMU_VM_FILE_VERSION)
1505 return -ENOTSUP;
1507 while ((section_type = qemu_get_byte(f)) != QEMU_VM_EOF) {
1508 uint32_t instance_id, version_id, section_id;
1509 SaveStateEntry *se;
1510 char idstr[257];
1511 int len;
1513 switch (section_type) {
1514 case QEMU_VM_SECTION_START:
1515 case QEMU_VM_SECTION_FULL:
1516 /* Read section start */
1517 section_id = qemu_get_be32(f);
1518 len = qemu_get_byte(f);
1519 qemu_get_buffer(f, (uint8_t *)idstr, len);
1520 idstr[len] = 0;
1521 instance_id = qemu_get_be32(f);
1522 version_id = qemu_get_be32(f);
1524 /* Find savevm section */
1525 se = find_se(idstr, instance_id);
1526 if (se == NULL) {
1527 fprintf(stderr, "Unknown savevm section or instance '%s' %d\n", idstr, instance_id);
1528 ret = -EINVAL;
1529 goto out;
1532 /* Validate version */
1533 if (version_id > se->version_id) {
1534 fprintf(stderr, "savevm: unsupported version %d for '%s' v%d\n",
1535 version_id, idstr, se->version_id);
1536 ret = -EINVAL;
1537 goto out;
1540 /* Add entry */
1541 le = qemu_mallocz(sizeof(*le));
1543 le->se = se;
1544 le->section_id = section_id;
1545 le->version_id = version_id;
1546 QLIST_INSERT_HEAD(&loadvm_handlers, le, entry);
1548 ret = vmstate_load(f, le->se, le->version_id);
1549 if (ret < 0) {
1550 fprintf(stderr, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
1551 instance_id, idstr);
1552 goto out;
1554 break;
1555 case QEMU_VM_SECTION_PART:
1556 case QEMU_VM_SECTION_END:
1557 section_id = qemu_get_be32(f);
1559 QLIST_FOREACH(le, &loadvm_handlers, entry) {
1560 if (le->section_id == section_id) {
1561 break;
1564 if (le == NULL) {
1565 fprintf(stderr, "Unknown savevm section %d\n", section_id);
1566 ret = -EINVAL;
1567 goto out;
1570 ret = vmstate_load(f, le->se, le->version_id);
1571 if (ret < 0) {
1572 fprintf(stderr, "qemu: warning: error while loading state section id %d\n",
1573 section_id);
1574 goto out;
1576 break;
1577 default:
1578 fprintf(stderr, "Unknown savevm section type %d\n", section_type);
1579 ret = -EINVAL;
1580 goto out;
1584 cpu_synchronize_all_post_init();
1586 ret = 0;
1588 out:
1589 QLIST_FOREACH_SAFE(le, &loadvm_handlers, entry, new_le) {
1590 QLIST_REMOVE(le, entry);
1591 qemu_free(le);
1594 if (qemu_file_has_error(f))
1595 ret = -EIO;
1597 return ret;
1600 /* device can contain snapshots */
1601 static int bdrv_can_snapshot(BlockDriverState *bs)
1603 return (bs &&
1604 !bdrv_is_removable(bs) &&
1605 !bdrv_is_read_only(bs));
1608 /* device must be snapshots in order to have a reliable snapshot */
1609 static int bdrv_has_snapshot(BlockDriverState *bs)
1611 return (bs &&
1612 !bdrv_is_removable(bs) &&
1613 !bdrv_is_read_only(bs));
1616 static BlockDriverState *get_bs_snapshots(void)
1618 BlockDriverState *bs;
1619 DriveInfo *dinfo;
1621 if (bs_snapshots)
1622 return bs_snapshots;
1623 QTAILQ_FOREACH(dinfo, &drives, next) {
1624 bs = dinfo->bdrv;
1625 if (bdrv_can_snapshot(bs))
1626 goto ok;
1628 return NULL;
1630 bs_snapshots = bs;
1631 return bs;
1634 static int bdrv_snapshot_find(BlockDriverState *bs, QEMUSnapshotInfo *sn_info,
1635 const char *name)
1637 QEMUSnapshotInfo *sn_tab, *sn;
1638 int nb_sns, i, ret;
1640 ret = -ENOENT;
1641 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
1642 if (nb_sns < 0)
1643 return ret;
1644 for(i = 0; i < nb_sns; i++) {
1645 sn = &sn_tab[i];
1646 if (!strcmp(sn->id_str, name) || !strcmp(sn->name, name)) {
1647 *sn_info = *sn;
1648 ret = 0;
1649 break;
1652 qemu_free(sn_tab);
1653 return ret;
1657 * Deletes snapshots of a given name in all opened images.
1659 static int del_existing_snapshots(Monitor *mon, const char *name)
1661 BlockDriverState *bs;
1662 DriveInfo *dinfo;
1663 QEMUSnapshotInfo sn1, *snapshot = &sn1;
1664 int ret;
1666 QTAILQ_FOREACH(dinfo, &drives, next) {
1667 bs = dinfo->bdrv;
1668 if (bdrv_can_snapshot(bs) &&
1669 bdrv_snapshot_find(bs, snapshot, name) >= 0)
1671 ret = bdrv_snapshot_delete(bs, name);
1672 if (ret < 0) {
1673 monitor_printf(mon,
1674 "Error while deleting snapshot on '%s'\n",
1675 bdrv_get_device_name(bs));
1676 return -1;
1681 return 0;
1684 void do_savevm(Monitor *mon, const QDict *qdict)
1686 DriveInfo *dinfo;
1687 BlockDriverState *bs, *bs1;
1688 QEMUSnapshotInfo sn1, *sn = &sn1, old_sn1, *old_sn = &old_sn1;
1689 int ret;
1690 QEMUFile *f;
1691 int saved_vm_running;
1692 uint32_t vm_state_size;
1693 #ifdef _WIN32
1694 struct _timeb tb;
1695 #else
1696 struct timeval tv;
1697 #endif
1698 const char *name = qdict_get_try_str(qdict, "name");
1700 bs = get_bs_snapshots();
1701 if (!bs) {
1702 monitor_printf(mon, "No block device can accept snapshots\n");
1703 return;
1706 /* ??? Should this occur after vm_stop? */
1707 qemu_aio_flush();
1709 saved_vm_running = vm_running;
1710 vm_stop(0);
1712 memset(sn, 0, sizeof(*sn));
1713 if (name) {
1714 ret = bdrv_snapshot_find(bs, old_sn, name);
1715 if (ret >= 0) {
1716 pstrcpy(sn->name, sizeof(sn->name), old_sn->name);
1717 pstrcpy(sn->id_str, sizeof(sn->id_str), old_sn->id_str);
1718 } else {
1719 pstrcpy(sn->name, sizeof(sn->name), name);
1723 /* fill auxiliary fields */
1724 #ifdef _WIN32
1725 _ftime(&tb);
1726 sn->date_sec = tb.time;
1727 sn->date_nsec = tb.millitm * 1000000;
1728 #else
1729 gettimeofday(&tv, NULL);
1730 sn->date_sec = tv.tv_sec;
1731 sn->date_nsec = tv.tv_usec * 1000;
1732 #endif
1733 sn->vm_clock_nsec = qemu_get_clock(vm_clock);
1735 /* Delete old snapshots of the same name */
1736 if (name && del_existing_snapshots(mon, name) < 0) {
1737 goto the_end;
1740 /* save the VM state */
1741 f = qemu_fopen_bdrv(bs, 1);
1742 if (!f) {
1743 monitor_printf(mon, "Could not open VM state file\n");
1744 goto the_end;
1746 ret = qemu_savevm_state(mon, f);
1747 vm_state_size = qemu_ftell(f);
1748 qemu_fclose(f);
1749 if (ret < 0) {
1750 monitor_printf(mon, "Error %d while writing VM\n", ret);
1751 goto the_end;
1754 /* create the snapshots */
1756 QTAILQ_FOREACH(dinfo, &drives, next) {
1757 bs1 = dinfo->bdrv;
1758 if (bdrv_has_snapshot(bs1)) {
1759 /* Write VM state size only to the image that contains the state */
1760 sn->vm_state_size = (bs == bs1 ? vm_state_size : 0);
1761 ret = bdrv_snapshot_create(bs1, sn);
1762 if (ret < 0) {
1763 monitor_printf(mon, "Error while creating snapshot on '%s'\n",
1764 bdrv_get_device_name(bs1));
1769 the_end:
1770 if (saved_vm_running)
1771 vm_start();
1774 int load_vmstate(const char *name)
1776 DriveInfo *dinfo;
1777 BlockDriverState *bs, *bs1;
1778 QEMUSnapshotInfo sn;
1779 QEMUFile *f;
1780 int ret;
1782 bs = get_bs_snapshots();
1783 if (!bs) {
1784 error_report("No block device supports snapshots");
1785 return -EINVAL;
1788 /* Flush all IO requests so they don't interfere with the new state. */
1789 qemu_aio_flush();
1791 QTAILQ_FOREACH(dinfo, &drives, next) {
1792 bs1 = dinfo->bdrv;
1793 if (bdrv_has_snapshot(bs1)) {
1794 ret = bdrv_snapshot_goto(bs1, name);
1795 if (ret < 0) {
1796 switch(ret) {
1797 case -ENOTSUP:
1798 error_report("%sSnapshots not supported on device '%s'",
1799 bs != bs1 ? "Warning: " : "",
1800 bdrv_get_device_name(bs1));
1801 break;
1802 case -ENOENT:
1803 error_report("%sCould not find snapshot '%s' on device '%s'",
1804 bs != bs1 ? "Warning: " : "",
1805 name, bdrv_get_device_name(bs1));
1806 break;
1807 default:
1808 error_report("%sError %d while activating snapshot on '%s'",
1809 bs != bs1 ? "Warning: " : "",
1810 ret, bdrv_get_device_name(bs1));
1811 break;
1813 /* fatal on snapshot block device */
1814 if (bs == bs1)
1815 return 0;
1820 /* Don't even try to load empty VM states */
1821 ret = bdrv_snapshot_find(bs, &sn, name);
1822 if ((ret >= 0) && (sn.vm_state_size == 0))
1823 return -EINVAL;
1825 /* restore the VM state */
1826 f = qemu_fopen_bdrv(bs, 0);
1827 if (!f) {
1828 error_report("Could not open VM state file");
1829 return -EINVAL;
1831 ret = qemu_loadvm_state(f);
1832 qemu_fclose(f);
1833 if (ret < 0) {
1834 error_report("Error %d while loading VM state", ret);
1835 return ret;
1837 return 0;
1840 void do_delvm(Monitor *mon, const QDict *qdict)
1842 DriveInfo *dinfo;
1843 BlockDriverState *bs, *bs1;
1844 int ret;
1845 const char *name = qdict_get_str(qdict, "name");
1847 bs = get_bs_snapshots();
1848 if (!bs) {
1849 monitor_printf(mon, "No block device supports snapshots\n");
1850 return;
1853 QTAILQ_FOREACH(dinfo, &drives, next) {
1854 bs1 = dinfo->bdrv;
1855 if (bdrv_has_snapshot(bs1)) {
1856 ret = bdrv_snapshot_delete(bs1, name);
1857 if (ret < 0) {
1858 if (ret == -ENOTSUP)
1859 monitor_printf(mon,
1860 "Snapshots not supported on device '%s'\n",
1861 bdrv_get_device_name(bs1));
1862 else
1863 monitor_printf(mon, "Error %d while deleting snapshot on "
1864 "'%s'\n", ret, bdrv_get_device_name(bs1));
1870 void do_info_snapshots(Monitor *mon)
1872 DriveInfo *dinfo;
1873 BlockDriverState *bs, *bs1;
1874 QEMUSnapshotInfo *sn_tab, *sn;
1875 int nb_sns, i;
1876 char buf[256];
1878 bs = get_bs_snapshots();
1879 if (!bs) {
1880 monitor_printf(mon, "No available block device supports snapshots\n");
1881 return;
1883 monitor_printf(mon, "Snapshot devices:");
1884 QTAILQ_FOREACH(dinfo, &drives, next) {
1885 bs1 = dinfo->bdrv;
1886 if (bdrv_has_snapshot(bs1)) {
1887 if (bs == bs1)
1888 monitor_printf(mon, " %s", bdrv_get_device_name(bs1));
1891 monitor_printf(mon, "\n");
1893 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
1894 if (nb_sns < 0) {
1895 monitor_printf(mon, "bdrv_snapshot_list: error %d\n", nb_sns);
1896 return;
1898 monitor_printf(mon, "Snapshot list (from %s):\n",
1899 bdrv_get_device_name(bs));
1900 monitor_printf(mon, "%s\n", bdrv_snapshot_dump(buf, sizeof(buf), NULL));
1901 for(i = 0; i < nb_sns; i++) {
1902 sn = &sn_tab[i];
1903 monitor_printf(mon, "%s\n", bdrv_snapshot_dump(buf, sizeof(buf), sn));
1905 qemu_free(sn_tab);