qdev/net: common nic property bits
[qemu.git] / savevm.c
blob61af8136fb10985846162ac2d05ab453bebc6d4b
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 #if defined(__NetBSD__)
46 #include <net/if_tap.h>
47 #endif
48 #ifdef __linux__
49 #include <linux/if_tun.h>
50 #endif
51 #include <arpa/inet.h>
52 #include <dirent.h>
53 #include <netdb.h>
54 #include <sys/select.h>
55 #ifdef CONFIG_BSD
56 #include <sys/stat.h>
57 #if defined(__FreeBSD__) || defined(__DragonFly__)
58 #include <libutil.h>
59 #else
60 #include <util.h>
61 #endif
62 #elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
63 #include <freebsd/stdlib.h>
64 #else
65 #ifdef __linux__
66 #include <pty.h>
67 #include <malloc.h>
68 #include <linux/rtc.h>
69 #endif
70 #endif
71 #endif
73 #ifdef _WIN32
74 #include <windows.h>
75 #include <malloc.h>
76 #include <sys/timeb.h>
77 #include <mmsystem.h>
78 #define getopt_long_only getopt_long
79 #define memalign(align, size) malloc(size)
80 #endif
82 #include "qemu-common.h"
83 #include "hw/hw.h"
84 #include "net.h"
85 #include "monitor.h"
86 #include "sysemu.h"
87 #include "qemu-timer.h"
88 #include "qemu-char.h"
89 #include "block.h"
90 #include "audio/audio.h"
91 #include "migration.h"
92 #include "qemu_socket.h"
93 #include "qemu-queue.h"
95 /* point to the block driver where the snapshots are managed */
96 static BlockDriverState *bs_snapshots;
98 #define SELF_ANNOUNCE_ROUNDS 5
100 #ifndef ETH_P_RARP
101 #define ETH_P_RARP 0x0835
102 #endif
103 #define ARP_HTYPE_ETH 0x0001
104 #define ARP_PTYPE_IP 0x0800
105 #define ARP_OP_REQUEST_REV 0x3
107 static int announce_self_create(uint8_t *buf,
108 uint8_t *mac_addr)
110 /* Ethernet header. */
111 memset(buf, 0xff, 6); /* destination MAC addr */
112 memcpy(buf + 6, mac_addr, 6); /* source MAC addr */
113 *(uint16_t *)(buf + 12) = htons(ETH_P_RARP); /* ethertype */
115 /* RARP header. */
116 *(uint16_t *)(buf + 14) = htons(ARP_HTYPE_ETH); /* hardware addr space */
117 *(uint16_t *)(buf + 16) = htons(ARP_PTYPE_IP); /* protocol addr space */
118 *(buf + 18) = 6; /* hardware addr length (ethernet) */
119 *(buf + 19) = 4; /* protocol addr length (IPv4) */
120 *(uint16_t *)(buf + 20) = htons(ARP_OP_REQUEST_REV); /* opcode */
121 memcpy(buf + 22, mac_addr, 6); /* source hw addr */
122 memset(buf + 28, 0x00, 4); /* source protocol addr */
123 memcpy(buf + 32, mac_addr, 6); /* target hw addr */
124 memset(buf + 38, 0x00, 4); /* target protocol addr */
126 /* Padding to get up to 60 bytes (ethernet min packet size, minus FCS). */
127 memset(buf + 42, 0x00, 18);
129 return 60; /* len (FCS will be added by hardware) */
132 static void qemu_announce_self_once(void *opaque)
134 int i, len;
135 VLANState *vlan;
136 VLANClientState *vc;
137 uint8_t buf[60];
138 static int count = SELF_ANNOUNCE_ROUNDS;
139 QEMUTimer *timer = *(QEMUTimer **)opaque;
141 for (i = 0; i < MAX_NICS; i++) {
142 if (!nd_table[i].used)
143 continue;
144 len = announce_self_create(buf, nd_table[i].macaddr);
145 vlan = nd_table[i].vlan;
146 QTAILQ_FOREACH(vc, &vlan->clients, next) {
147 vc->receive(vc, buf, len);
150 if (--count) {
151 /* delay 50ms, 150ms, 250ms, ... */
152 qemu_mod_timer(timer, qemu_get_clock(rt_clock) +
153 50 + (SELF_ANNOUNCE_ROUNDS - count - 1) * 100);
154 } else {
155 qemu_del_timer(timer);
156 qemu_free_timer(timer);
160 void qemu_announce_self(void)
162 static QEMUTimer *timer;
163 timer = qemu_new_timer(rt_clock, qemu_announce_self_once, &timer);
164 qemu_announce_self_once(&timer);
167 /***********************************************************/
168 /* savevm/loadvm support */
170 #define IO_BUF_SIZE 32768
172 struct QEMUFile {
173 QEMUFilePutBufferFunc *put_buffer;
174 QEMUFileGetBufferFunc *get_buffer;
175 QEMUFileCloseFunc *close;
176 QEMUFileRateLimit *rate_limit;
177 QEMUFileSetRateLimit *set_rate_limit;
178 void *opaque;
179 int is_write;
181 int64_t buf_offset; /* start of buffer when writing, end of buffer
182 when reading */
183 int buf_index;
184 int buf_size; /* 0 when writing */
185 uint8_t buf[IO_BUF_SIZE];
187 int has_error;
190 typedef struct QEMUFileStdio
192 FILE *stdio_file;
193 QEMUFile *file;
194 } QEMUFileStdio;
196 typedef struct QEMUFileSocket
198 int fd;
199 QEMUFile *file;
200 } QEMUFileSocket;
202 static int socket_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
204 QEMUFileSocket *s = opaque;
205 ssize_t len;
207 do {
208 len = recv(s->fd, (void *)buf, size, 0);
209 } while (len == -1 && socket_error() == EINTR);
211 if (len == -1)
212 len = -socket_error();
214 return len;
217 static int socket_close(void *opaque)
219 QEMUFileSocket *s = opaque;
220 qemu_free(s);
221 return 0;
224 static int stdio_put_buffer(void *opaque, const uint8_t *buf, int64_t pos, int size)
226 QEMUFileStdio *s = opaque;
227 return fwrite(buf, 1, size, s->stdio_file);
230 static int stdio_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
232 QEMUFileStdio *s = opaque;
233 FILE *fp = s->stdio_file;
234 int bytes;
236 do {
237 clearerr(fp);
238 bytes = fread(buf, 1, size, fp);
239 } while ((bytes == 0) && ferror(fp) && (errno == EINTR));
240 return bytes;
243 static int stdio_pclose(void *opaque)
245 QEMUFileStdio *s = opaque;
246 pclose(s->stdio_file);
247 qemu_free(s);
248 return 0;
251 static int stdio_fclose(void *opaque)
253 QEMUFileStdio *s = opaque;
254 fclose(s->stdio_file);
255 qemu_free(s);
256 return 0;
259 QEMUFile *qemu_popen(FILE *stdio_file, const char *mode)
261 QEMUFileStdio *s;
263 if (stdio_file == NULL || mode == NULL || (mode[0] != 'r' && mode[0] != 'w') || mode[1] != 0) {
264 fprintf(stderr, "qemu_popen: Argument validity check failed\n");
265 return NULL;
268 s = qemu_mallocz(sizeof(QEMUFileStdio));
270 s->stdio_file = stdio_file;
272 if(mode[0] == 'r') {
273 s->file = qemu_fopen_ops(s, NULL, stdio_get_buffer, stdio_pclose, NULL, NULL);
274 } else {
275 s->file = qemu_fopen_ops(s, stdio_put_buffer, NULL, stdio_pclose, NULL, NULL);
277 return s->file;
280 QEMUFile *qemu_popen_cmd(const char *command, const char *mode)
282 FILE *popen_file;
284 popen_file = popen(command, mode);
285 if(popen_file == NULL) {
286 return NULL;
289 return qemu_popen(popen_file, mode);
292 int qemu_stdio_fd(QEMUFile *f)
294 QEMUFileStdio *p;
295 int fd;
297 p = (QEMUFileStdio *)f->opaque;
298 fd = fileno(p->stdio_file);
300 return fd;
303 QEMUFile *qemu_fdopen(int fd, const char *mode)
305 QEMUFileStdio *s;
307 if (mode == NULL ||
308 (mode[0] != 'r' && mode[0] != 'w') ||
309 mode[1] != 'b' || mode[2] != 0) {
310 fprintf(stderr, "qemu_fdopen: Argument validity check failed\n");
311 return NULL;
314 s = qemu_mallocz(sizeof(QEMUFileStdio));
315 s->stdio_file = fdopen(fd, mode);
316 if (!s->stdio_file)
317 goto fail;
319 if(mode[0] == 'r') {
320 s->file = qemu_fopen_ops(s, NULL, stdio_get_buffer, stdio_fclose, NULL, NULL);
321 } else {
322 s->file = qemu_fopen_ops(s, stdio_put_buffer, NULL, stdio_fclose, NULL, NULL);
324 return s->file;
326 fail:
327 qemu_free(s);
328 return NULL;
331 QEMUFile *qemu_fopen_socket(int fd)
333 QEMUFileSocket *s = qemu_mallocz(sizeof(QEMUFileSocket));
335 s->fd = fd;
336 s->file = qemu_fopen_ops(s, NULL, socket_get_buffer, socket_close, 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, NULL, NULL);
375 } else {
376 s->file = qemu_fopen_ops(s, NULL, file_get_buffer, stdio_fclose, NULL, NULL);
378 return s->file;
379 fail:
380 qemu_free(s);
381 return NULL;
384 static int block_put_buffer(void *opaque, const uint8_t *buf,
385 int64_t pos, int size)
387 bdrv_save_vmstate(opaque, buf, pos, size);
388 return size;
391 static int block_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
393 return bdrv_load_vmstate(opaque, buf, pos, size);
396 static int bdrv_fclose(void *opaque)
398 return 0;
401 static QEMUFile *qemu_fopen_bdrv(BlockDriverState *bs, int is_writable)
403 if (is_writable)
404 return qemu_fopen_ops(bs, block_put_buffer, NULL, bdrv_fclose, NULL, NULL);
405 return qemu_fopen_ops(bs, NULL, block_get_buffer, bdrv_fclose, NULL, NULL);
408 QEMUFile *qemu_fopen_ops(void *opaque, QEMUFilePutBufferFunc *put_buffer,
409 QEMUFileGetBufferFunc *get_buffer,
410 QEMUFileCloseFunc *close,
411 QEMUFileRateLimit *rate_limit,
412 QEMUFileSetRateLimit *set_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->is_write = 0;
426 return f;
429 int qemu_file_has_error(QEMUFile *f)
431 return f->has_error;
434 void qemu_file_set_error(QEMUFile *f)
436 f->has_error = 1;
439 void qemu_fflush(QEMUFile *f)
441 if (!f->put_buffer)
442 return;
444 if (f->is_write && f->buf_index > 0) {
445 int len;
447 len = f->put_buffer(f->opaque, f->buf, f->buf_offset, f->buf_index);
448 if (len > 0)
449 f->buf_offset += f->buf_index;
450 else
451 f->has_error = 1;
452 f->buf_index = 0;
456 static void qemu_fill_buffer(QEMUFile *f)
458 int len;
460 if (!f->get_buffer)
461 return;
463 if (f->is_write)
464 abort();
466 len = f->get_buffer(f->opaque, f->buf, f->buf_offset, IO_BUF_SIZE);
467 if (len > 0) {
468 f->buf_index = 0;
469 f->buf_size = len;
470 f->buf_offset += len;
471 } else if (len != -EAGAIN)
472 f->has_error = 1;
475 int qemu_fclose(QEMUFile *f)
477 int ret = 0;
478 qemu_fflush(f);
479 if (f->close)
480 ret = f->close(f->opaque);
481 qemu_free(f);
482 return ret;
485 void qemu_file_put_notify(QEMUFile *f)
487 f->put_buffer(f->opaque, NULL, 0, 0);
490 void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size)
492 int l;
494 if (!f->has_error && f->is_write == 0 && f->buf_index > 0) {
495 fprintf(stderr,
496 "Attempted to write to buffer while read buffer is not empty\n");
497 abort();
500 while (!f->has_error && size > 0) {
501 l = IO_BUF_SIZE - f->buf_index;
502 if (l > size)
503 l = size;
504 memcpy(f->buf + f->buf_index, buf, l);
505 f->is_write = 1;
506 f->buf_index += l;
507 buf += l;
508 size -= l;
509 if (f->buf_index >= IO_BUF_SIZE)
510 qemu_fflush(f);
514 void qemu_put_byte(QEMUFile *f, int v)
516 if (!f->has_error && f->is_write == 0 && f->buf_index > 0) {
517 fprintf(stderr,
518 "Attempted to write to buffer while read buffer is not empty\n");
519 abort();
522 f->buf[f->buf_index++] = v;
523 f->is_write = 1;
524 if (f->buf_index >= IO_BUF_SIZE)
525 qemu_fflush(f);
528 int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size1)
530 int size, l;
532 if (f->is_write)
533 abort();
535 size = size1;
536 while (size > 0) {
537 l = f->buf_size - f->buf_index;
538 if (l == 0) {
539 qemu_fill_buffer(f);
540 l = f->buf_size - f->buf_index;
541 if (l == 0)
542 break;
544 if (l > size)
545 l = size;
546 memcpy(buf, f->buf + f->buf_index, l);
547 f->buf_index += l;
548 buf += l;
549 size -= l;
551 return size1 - size;
554 int qemu_get_byte(QEMUFile *f)
556 if (f->is_write)
557 abort();
559 if (f->buf_index >= f->buf_size) {
560 qemu_fill_buffer(f);
561 if (f->buf_index >= f->buf_size)
562 return 0;
564 return f->buf[f->buf_index++];
567 int64_t qemu_ftell(QEMUFile *f)
569 return f->buf_offset - f->buf_size + f->buf_index;
572 int64_t qemu_fseek(QEMUFile *f, int64_t pos, int whence)
574 if (whence == SEEK_SET) {
575 /* nothing to do */
576 } else if (whence == SEEK_CUR) {
577 pos += qemu_ftell(f);
578 } else {
579 /* SEEK_END not supported */
580 return -1;
582 if (f->put_buffer) {
583 qemu_fflush(f);
584 f->buf_offset = pos;
585 } else {
586 f->buf_offset = pos;
587 f->buf_index = 0;
588 f->buf_size = 0;
590 return pos;
593 int qemu_file_rate_limit(QEMUFile *f)
595 if (f->rate_limit)
596 return f->rate_limit(f->opaque);
598 return 0;
601 size_t qemu_file_set_rate_limit(QEMUFile *f, size_t new_rate)
603 /* any failed or completed migration keeps its state to allow probing of
604 * migration data, but has no associated file anymore */
605 if (f && f->set_rate_limit)
606 return f->set_rate_limit(f->opaque, new_rate);
608 return 0;
611 void qemu_put_be16(QEMUFile *f, unsigned int v)
613 qemu_put_byte(f, v >> 8);
614 qemu_put_byte(f, v);
617 void qemu_put_be32(QEMUFile *f, unsigned int v)
619 qemu_put_byte(f, v >> 24);
620 qemu_put_byte(f, v >> 16);
621 qemu_put_byte(f, v >> 8);
622 qemu_put_byte(f, v);
625 void qemu_put_be64(QEMUFile *f, uint64_t v)
627 qemu_put_be32(f, v >> 32);
628 qemu_put_be32(f, v);
631 unsigned int qemu_get_be16(QEMUFile *f)
633 unsigned int v;
634 v = qemu_get_byte(f) << 8;
635 v |= qemu_get_byte(f);
636 return v;
639 unsigned int qemu_get_be32(QEMUFile *f)
641 unsigned int v;
642 v = qemu_get_byte(f) << 24;
643 v |= qemu_get_byte(f) << 16;
644 v |= qemu_get_byte(f) << 8;
645 v |= qemu_get_byte(f);
646 return v;
649 uint64_t qemu_get_be64(QEMUFile *f)
651 uint64_t v;
652 v = (uint64_t)qemu_get_be32(f) << 32;
653 v |= qemu_get_be32(f);
654 return v;
657 /* 8 bit int */
659 static int get_int8(QEMUFile *f, void *pv, size_t size)
661 int8_t *v = pv;
662 qemu_get_s8s(f, v);
663 return 0;
666 static void put_int8(QEMUFile *f, void *pv, size_t size)
668 int8_t *v = pv;
669 qemu_put_s8s(f, v);
672 const VMStateInfo vmstate_info_int8 = {
673 .name = "int8",
674 .get = get_int8,
675 .put = put_int8,
678 /* 16 bit int */
680 static int get_int16(QEMUFile *f, void *pv, size_t size)
682 int16_t *v = pv;
683 qemu_get_sbe16s(f, v);
684 return 0;
687 static void put_int16(QEMUFile *f, void *pv, size_t size)
689 int16_t *v = pv;
690 qemu_put_sbe16s(f, v);
693 const VMStateInfo vmstate_info_int16 = {
694 .name = "int16",
695 .get = get_int16,
696 .put = put_int16,
699 /* 32 bit int */
701 static int get_int32(QEMUFile *f, void *pv, size_t size)
703 int32_t *v = pv;
704 qemu_get_sbe32s(f, v);
705 return 0;
708 static void put_int32(QEMUFile *f, void *pv, size_t size)
710 int32_t *v = pv;
711 qemu_put_sbe32s(f, v);
714 const VMStateInfo vmstate_info_int32 = {
715 .name = "int32",
716 .get = get_int32,
717 .put = put_int32,
720 /* 32 bit int. See that the received value is the same than the one
721 in the field */
723 static int get_int32_equal(QEMUFile *f, void *pv, size_t size)
725 int32_t *v = pv;
726 int32_t v2;
727 qemu_get_sbe32s(f, &v2);
729 if (*v == v2)
730 return 0;
731 return -EINVAL;
734 const VMStateInfo vmstate_info_int32_equal = {
735 .name = "int32 equal",
736 .get = get_int32_equal,
737 .put = put_int32,
740 /* 32 bit int. See that the received value is the less or the same
741 than the one in the field */
743 static int get_int32_le(QEMUFile *f, void *pv, size_t size)
745 int32_t *old = pv;
746 int32_t new;
747 qemu_get_sbe32s(f, &new);
749 if (*old <= new)
750 return 0;
751 return -EINVAL;
754 const VMStateInfo vmstate_info_int32_le = {
755 .name = "int32 equal",
756 .get = get_int32_le,
757 .put = put_int32,
760 /* 64 bit int */
762 static int get_int64(QEMUFile *f, void *pv, size_t size)
764 int64_t *v = pv;
765 qemu_get_sbe64s(f, v);
766 return 0;
769 static void put_int64(QEMUFile *f, void *pv, size_t size)
771 int64_t *v = pv;
772 qemu_put_sbe64s(f, v);
775 const VMStateInfo vmstate_info_int64 = {
776 .name = "int64",
777 .get = get_int64,
778 .put = put_int64,
781 /* 8 bit unsigned int */
783 static int get_uint8(QEMUFile *f, void *pv, size_t size)
785 uint8_t *v = pv;
786 qemu_get_8s(f, v);
787 return 0;
790 static void put_uint8(QEMUFile *f, void *pv, size_t size)
792 uint8_t *v = pv;
793 qemu_put_8s(f, v);
796 const VMStateInfo vmstate_info_uint8 = {
797 .name = "uint8",
798 .get = get_uint8,
799 .put = put_uint8,
802 /* 16 bit unsigned int */
804 static int get_uint16(QEMUFile *f, void *pv, size_t size)
806 uint16_t *v = pv;
807 qemu_get_be16s(f, v);
808 return 0;
811 static void put_uint16(QEMUFile *f, void *pv, size_t size)
813 uint16_t *v = pv;
814 qemu_put_be16s(f, v);
817 const VMStateInfo vmstate_info_uint16 = {
818 .name = "uint16",
819 .get = get_uint16,
820 .put = put_uint16,
823 /* 32 bit unsigned int */
825 static int get_uint32(QEMUFile *f, void *pv, size_t size)
827 uint32_t *v = pv;
828 qemu_get_be32s(f, v);
829 return 0;
832 static void put_uint32(QEMUFile *f, void *pv, size_t size)
834 uint32_t *v = pv;
835 qemu_put_be32s(f, v);
838 const VMStateInfo vmstate_info_uint32 = {
839 .name = "uint32",
840 .get = get_uint32,
841 .put = put_uint32,
844 /* 64 bit unsigned int */
846 static int get_uint64(QEMUFile *f, void *pv, size_t size)
848 uint64_t *v = pv;
849 qemu_get_be64s(f, v);
850 return 0;
853 static void put_uint64(QEMUFile *f, void *pv, size_t size)
855 uint64_t *v = pv;
856 qemu_put_be64s(f, v);
859 const VMStateInfo vmstate_info_uint64 = {
860 .name = "uint64",
861 .get = get_uint64,
862 .put = put_uint64,
865 /* 8 bit int. See that the received value is the same than the one
866 in the field */
868 static int get_uint8_equal(QEMUFile *f, void *pv, size_t size)
870 uint8_t *v = pv;
871 uint8_t v2;
872 qemu_get_8s(f, &v2);
874 if (*v == v2)
875 return 0;
876 return -EINVAL;
879 const VMStateInfo vmstate_info_uint8_equal = {
880 .name = "int32 equal",
881 .get = get_uint8_equal,
882 .put = put_uint8,
885 /* timers */
887 static int get_timer(QEMUFile *f, void *pv, size_t size)
889 QEMUTimer *v = pv;
890 qemu_get_timer(f, v);
891 return 0;
894 static void put_timer(QEMUFile *f, void *pv, size_t size)
896 QEMUTimer *v = pv;
897 qemu_put_timer(f, v);
900 const VMStateInfo vmstate_info_timer = {
901 .name = "timer",
902 .get = get_timer,
903 .put = put_timer,
906 /* uint8_t buffers */
908 static int get_buffer(QEMUFile *f, void *pv, size_t size)
910 uint8_t *v = pv;
911 qemu_get_buffer(f, v, size);
912 return 0;
915 static void put_buffer(QEMUFile *f, void *pv, size_t size)
917 uint8_t *v = pv;
918 qemu_put_buffer(f, v, size);
921 const VMStateInfo vmstate_info_buffer = {
922 .name = "buffer",
923 .get = get_buffer,
924 .put = put_buffer,
927 typedef struct SaveStateEntry {
928 QTAILQ_ENTRY(SaveStateEntry) entry;
929 char idstr[256];
930 int instance_id;
931 int version_id;
932 int section_id;
933 SaveLiveStateHandler *save_live_state;
934 SaveStateHandler *save_state;
935 LoadStateHandler *load_state;
936 const VMStateDescription *vmsd;
937 void *opaque;
938 } SaveStateEntry;
940 static QTAILQ_HEAD(savevm_handlers, SaveStateEntry) savevm_handlers =
941 QTAILQ_HEAD_INITIALIZER(savevm_handlers);
942 static int global_section_id;
944 static int calculate_new_instance_id(const char *idstr)
946 SaveStateEntry *se;
947 int instance_id = 0;
949 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
950 if (strcmp(idstr, se->idstr) == 0
951 && instance_id <= se->instance_id) {
952 instance_id = se->instance_id + 1;
955 return instance_id;
958 /* TODO: Individual devices generally have very little idea about the rest
959 of the system, so instance_id should be removed/replaced.
960 Meanwhile pass -1 as instance_id if you do not already have a clearly
961 distinguishing id for all instances of your device class. */
962 int register_savevm_live(const char *idstr,
963 int instance_id,
964 int version_id,
965 SaveLiveStateHandler *save_live_state,
966 SaveStateHandler *save_state,
967 LoadStateHandler *load_state,
968 void *opaque)
970 SaveStateEntry *se;
972 se = qemu_malloc(sizeof(SaveStateEntry));
973 pstrcpy(se->idstr, sizeof(se->idstr), idstr);
974 se->version_id = version_id;
975 se->section_id = global_section_id++;
976 se->save_live_state = save_live_state;
977 se->save_state = save_state;
978 se->load_state = load_state;
979 se->opaque = opaque;
980 se->vmsd = NULL;
982 if (instance_id == -1) {
983 se->instance_id = calculate_new_instance_id(idstr);
984 } else {
985 se->instance_id = instance_id;
987 /* add at the end of list */
988 QTAILQ_INSERT_TAIL(&savevm_handlers, se, entry);
989 return 0;
992 int register_savevm(const char *idstr,
993 int instance_id,
994 int version_id,
995 SaveStateHandler *save_state,
996 LoadStateHandler *load_state,
997 void *opaque)
999 return register_savevm_live(idstr, instance_id, version_id,
1000 NULL, save_state, load_state, opaque);
1003 void unregister_savevm(const char *idstr, void *opaque)
1005 SaveStateEntry *se, *new_se;
1007 QTAILQ_FOREACH_SAFE(se, &savevm_handlers, entry, new_se) {
1008 if (strcmp(se->idstr, idstr) == 0 && se->opaque == opaque) {
1009 QTAILQ_REMOVE(&savevm_handlers, se, entry);
1010 qemu_free(se);
1015 int vmstate_register(int instance_id, const VMStateDescription *vmsd,
1016 void *opaque)
1018 SaveStateEntry *se;
1020 se = qemu_malloc(sizeof(SaveStateEntry));
1021 pstrcpy(se->idstr, sizeof(se->idstr), vmsd->name);
1022 se->version_id = vmsd->version_id;
1023 se->section_id = global_section_id++;
1024 se->save_live_state = NULL;
1025 se->save_state = NULL;
1026 se->load_state = NULL;
1027 se->opaque = opaque;
1028 se->vmsd = vmsd;
1030 if (instance_id == -1) {
1031 se->instance_id = calculate_new_instance_id(vmsd->name);
1032 } else {
1033 se->instance_id = instance_id;
1035 /* add at the end of list */
1036 QTAILQ_INSERT_TAIL(&savevm_handlers, se, entry);
1037 return 0;
1040 void vmstate_unregister(const VMStateDescription *vmsd, void *opaque)
1042 SaveStateEntry *se, *new_se;
1044 QTAILQ_FOREACH_SAFE(se, &savevm_handlers, entry, new_se) {
1045 if (se->vmsd == vmsd && se->opaque == opaque) {
1046 QTAILQ_REMOVE(&savevm_handlers, se, entry);
1047 qemu_free(se);
1052 int vmstate_load_state(QEMUFile *f, const VMStateDescription *vmsd,
1053 void *opaque, int version_id)
1055 VMStateField *field = vmsd->fields;
1057 if (version_id > vmsd->version_id) {
1058 return -EINVAL;
1060 if (version_id < vmsd->minimum_version_id_old) {
1061 return -EINVAL;
1063 if (version_id < vmsd->minimum_version_id) {
1064 return vmsd->load_state_old(f, opaque, version_id);
1066 if (vmsd->pre_load) {
1067 int ret = vmsd->pre_load(opaque);
1068 if (ret)
1069 return ret;
1071 while(field->name) {
1072 if ((field->field_exists &&
1073 field->field_exists(opaque, version_id)) ||
1074 (!field->field_exists &&
1075 field->version_id <= version_id)) {
1076 void *base_addr = opaque + field->offset;
1077 int ret, i, n_elems = 1;
1079 if (field->flags & VMS_ARRAY) {
1080 n_elems = field->num;
1081 } else if (field->flags & VMS_VARRAY) {
1082 n_elems = *(size_t *)(opaque+field->num_offset);
1084 if (field->flags & VMS_POINTER) {
1085 base_addr = *(void **)base_addr;
1087 for (i = 0; i < n_elems; i++) {
1088 void *addr = base_addr + field->size * i;
1090 if (field->flags & VMS_ARRAY_OF_POINTER) {
1091 addr = *(void **)addr;
1093 if (field->flags & VMS_STRUCT) {
1094 ret = vmstate_load_state(f, field->vmsd, addr, field->vmsd->version_id);
1095 } else {
1096 ret = field->info->get(f, addr, field->size);
1099 if (ret < 0) {
1100 return ret;
1104 field++;
1106 if (vmsd->post_load) {
1107 return vmsd->post_load(opaque, version_id);
1109 return 0;
1112 void vmstate_save_state(QEMUFile *f, const VMStateDescription *vmsd,
1113 void *opaque)
1115 VMStateField *field = vmsd->fields;
1117 if (vmsd->pre_save) {
1118 vmsd->pre_save(opaque);
1120 while(field->name) {
1121 if (!field->field_exists ||
1122 field->field_exists(opaque, vmsd->version_id)) {
1123 void *base_addr = opaque + field->offset;
1124 int i, n_elems = 1;
1126 if (field->flags & VMS_ARRAY) {
1127 n_elems = field->num;
1128 } else if (field->flags & VMS_VARRAY) {
1129 n_elems = *(size_t *)(opaque+field->num_offset);
1131 if (field->flags & VMS_POINTER) {
1132 base_addr = *(void **)base_addr;
1134 for (i = 0; i < n_elems; i++) {
1135 void *addr = base_addr + field->size * i;
1137 if (field->flags & VMS_STRUCT) {
1138 vmstate_save_state(f, field->vmsd, addr);
1139 } else {
1140 field->info->put(f, addr, field->size);
1144 field++;
1146 if (vmsd->post_save) {
1147 vmsd->post_save(opaque);
1151 static int vmstate_load(QEMUFile *f, SaveStateEntry *se, int version_id)
1153 if (!se->vmsd) { /* Old style */
1154 return se->load_state(f, se->opaque, version_id);
1156 return vmstate_load_state(f, se->vmsd, se->opaque, version_id);
1159 static void vmstate_save(QEMUFile *f, SaveStateEntry *se)
1161 if (!se->vmsd) { /* Old style */
1162 se->save_state(f, se->opaque);
1163 return;
1165 vmstate_save_state(f,se->vmsd, se->opaque);
1168 #define QEMU_VM_FILE_MAGIC 0x5145564d
1169 #define QEMU_VM_FILE_VERSION_COMPAT 0x00000002
1170 #define QEMU_VM_FILE_VERSION 0x00000003
1172 #define QEMU_VM_EOF 0x00
1173 #define QEMU_VM_SECTION_START 0x01
1174 #define QEMU_VM_SECTION_PART 0x02
1175 #define QEMU_VM_SECTION_END 0x03
1176 #define QEMU_VM_SECTION_FULL 0x04
1178 int qemu_savevm_state_begin(QEMUFile *f)
1180 SaveStateEntry *se;
1182 qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
1183 qemu_put_be32(f, QEMU_VM_FILE_VERSION);
1185 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1186 int len;
1188 if (se->save_live_state == NULL)
1189 continue;
1191 /* Section type */
1192 qemu_put_byte(f, QEMU_VM_SECTION_START);
1193 qemu_put_be32(f, se->section_id);
1195 /* ID string */
1196 len = strlen(se->idstr);
1197 qemu_put_byte(f, len);
1198 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
1200 qemu_put_be32(f, se->instance_id);
1201 qemu_put_be32(f, se->version_id);
1203 se->save_live_state(f, QEMU_VM_SECTION_START, se->opaque);
1206 if (qemu_file_has_error(f))
1207 return -EIO;
1209 return 0;
1212 int qemu_savevm_state_iterate(QEMUFile *f)
1214 SaveStateEntry *se;
1215 int ret = 1;
1217 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1218 if (se->save_live_state == NULL)
1219 continue;
1221 /* Section type */
1222 qemu_put_byte(f, QEMU_VM_SECTION_PART);
1223 qemu_put_be32(f, se->section_id);
1225 ret &= !!se->save_live_state(f, QEMU_VM_SECTION_PART, se->opaque);
1228 if (ret)
1229 return 1;
1231 if (qemu_file_has_error(f))
1232 return -EIO;
1234 return 0;
1237 int qemu_savevm_state_complete(QEMUFile *f)
1239 SaveStateEntry *se;
1241 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1242 if (se->save_live_state == NULL)
1243 continue;
1245 /* Section type */
1246 qemu_put_byte(f, QEMU_VM_SECTION_END);
1247 qemu_put_be32(f, se->section_id);
1249 se->save_live_state(f, QEMU_VM_SECTION_END, se->opaque);
1252 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1253 int len;
1255 if (se->save_state == NULL && se->vmsd == NULL)
1256 continue;
1258 /* Section type */
1259 qemu_put_byte(f, QEMU_VM_SECTION_FULL);
1260 qemu_put_be32(f, se->section_id);
1262 /* ID string */
1263 len = strlen(se->idstr);
1264 qemu_put_byte(f, len);
1265 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
1267 qemu_put_be32(f, se->instance_id);
1268 qemu_put_be32(f, se->version_id);
1270 vmstate_save(f, se);
1273 qemu_put_byte(f, QEMU_VM_EOF);
1275 if (qemu_file_has_error(f))
1276 return -EIO;
1278 return 0;
1281 int qemu_savevm_state(QEMUFile *f)
1283 int saved_vm_running;
1284 int ret;
1286 saved_vm_running = vm_running;
1287 vm_stop(0);
1289 bdrv_flush_all();
1291 ret = qemu_savevm_state_begin(f);
1292 if (ret < 0)
1293 goto out;
1295 do {
1296 ret = qemu_savevm_state_iterate(f);
1297 if (ret < 0)
1298 goto out;
1299 } while (ret == 0);
1301 ret = qemu_savevm_state_complete(f);
1303 out:
1304 if (qemu_file_has_error(f))
1305 ret = -EIO;
1307 if (!ret && saved_vm_running)
1308 vm_start();
1310 return ret;
1313 static SaveStateEntry *find_se(const char *idstr, int instance_id)
1315 SaveStateEntry *se;
1317 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1318 if (!strcmp(se->idstr, idstr) &&
1319 instance_id == se->instance_id)
1320 return se;
1322 return NULL;
1325 typedef struct LoadStateEntry {
1326 QLIST_ENTRY(LoadStateEntry) entry;
1327 SaveStateEntry *se;
1328 int section_id;
1329 int version_id;
1330 } LoadStateEntry;
1332 int qemu_loadvm_state(QEMUFile *f)
1334 QLIST_HEAD(, LoadStateEntry) loadvm_handlers =
1335 QLIST_HEAD_INITIALIZER(loadvm_handlers);
1336 LoadStateEntry *le, *new_le;
1337 uint8_t section_type;
1338 unsigned int v;
1339 int ret;
1341 v = qemu_get_be32(f);
1342 if (v != QEMU_VM_FILE_MAGIC)
1343 return -EINVAL;
1345 v = qemu_get_be32(f);
1346 if (v == QEMU_VM_FILE_VERSION_COMPAT) {
1347 fprintf(stderr, "SaveVM v2 format is obsolete and don't work anymore\n");
1348 return -ENOTSUP;
1350 if (v != QEMU_VM_FILE_VERSION)
1351 return -ENOTSUP;
1353 while ((section_type = qemu_get_byte(f)) != QEMU_VM_EOF) {
1354 uint32_t instance_id, version_id, section_id;
1355 SaveStateEntry *se;
1356 char idstr[257];
1357 int len;
1359 switch (section_type) {
1360 case QEMU_VM_SECTION_START:
1361 case QEMU_VM_SECTION_FULL:
1362 /* Read section start */
1363 section_id = qemu_get_be32(f);
1364 len = qemu_get_byte(f);
1365 qemu_get_buffer(f, (uint8_t *)idstr, len);
1366 idstr[len] = 0;
1367 instance_id = qemu_get_be32(f);
1368 version_id = qemu_get_be32(f);
1370 /* Find savevm section */
1371 se = find_se(idstr, instance_id);
1372 if (se == NULL) {
1373 fprintf(stderr, "Unknown savevm section or instance '%s' %d\n", idstr, instance_id);
1374 ret = -EINVAL;
1375 goto out;
1378 /* Validate version */
1379 if (version_id > se->version_id) {
1380 fprintf(stderr, "savevm: unsupported version %d for '%s' v%d\n",
1381 version_id, idstr, se->version_id);
1382 ret = -EINVAL;
1383 goto out;
1386 /* Add entry */
1387 le = qemu_mallocz(sizeof(*le));
1389 le->se = se;
1390 le->section_id = section_id;
1391 le->version_id = version_id;
1392 QLIST_INSERT_HEAD(&loadvm_handlers, le, entry);
1394 ret = vmstate_load(f, le->se, le->version_id);
1395 if (ret < 0) {
1396 fprintf(stderr, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
1397 instance_id, idstr);
1398 goto out;
1400 break;
1401 case QEMU_VM_SECTION_PART:
1402 case QEMU_VM_SECTION_END:
1403 section_id = qemu_get_be32(f);
1405 QLIST_FOREACH(le, &loadvm_handlers, entry) {
1406 if (le->section_id == section_id) {
1407 break;
1410 if (le == NULL) {
1411 fprintf(stderr, "Unknown savevm section %d\n", section_id);
1412 ret = -EINVAL;
1413 goto out;
1416 ret = vmstate_load(f, le->se, le->version_id);
1417 if (ret < 0) {
1418 fprintf(stderr, "qemu: warning: error while loading state section id %d\n",
1419 section_id);
1420 goto out;
1422 break;
1423 default:
1424 fprintf(stderr, "Unknown savevm section type %d\n", section_type);
1425 ret = -EINVAL;
1426 goto out;
1430 ret = 0;
1432 out:
1433 QLIST_FOREACH_SAFE(le, &loadvm_handlers, entry, new_le) {
1434 QLIST_REMOVE(le, entry);
1435 qemu_free(le);
1438 if (qemu_file_has_error(f))
1439 ret = -EIO;
1441 return ret;
1444 /* device can contain snapshots */
1445 static int bdrv_can_snapshot(BlockDriverState *bs)
1447 return (bs &&
1448 !bdrv_is_removable(bs) &&
1449 !bdrv_is_read_only(bs));
1452 /* device must be snapshots in order to have a reliable snapshot */
1453 static int bdrv_has_snapshot(BlockDriverState *bs)
1455 return (bs &&
1456 !bdrv_is_removable(bs) &&
1457 !bdrv_is_read_only(bs));
1460 static BlockDriverState *get_bs_snapshots(void)
1462 BlockDriverState *bs;
1463 DriveInfo *dinfo;
1465 if (bs_snapshots)
1466 return bs_snapshots;
1467 QTAILQ_FOREACH(dinfo, &drives, next) {
1468 bs = dinfo->bdrv;
1469 if (bdrv_can_snapshot(bs))
1470 goto ok;
1472 return NULL;
1474 bs_snapshots = bs;
1475 return bs;
1478 static int bdrv_snapshot_find(BlockDriverState *bs, QEMUSnapshotInfo *sn_info,
1479 const char *name)
1481 QEMUSnapshotInfo *sn_tab, *sn;
1482 int nb_sns, i, ret;
1484 ret = -ENOENT;
1485 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
1486 if (nb_sns < 0)
1487 return ret;
1488 for(i = 0; i < nb_sns; i++) {
1489 sn = &sn_tab[i];
1490 if (!strcmp(sn->id_str, name) || !strcmp(sn->name, name)) {
1491 *sn_info = *sn;
1492 ret = 0;
1493 break;
1496 qemu_free(sn_tab);
1497 return ret;
1500 void do_savevm(Monitor *mon, const QDict *qdict)
1502 DriveInfo *dinfo;
1503 BlockDriverState *bs, *bs1;
1504 QEMUSnapshotInfo sn1, *sn = &sn1, old_sn1, *old_sn = &old_sn1;
1505 int must_delete, ret;
1506 QEMUFile *f;
1507 int saved_vm_running;
1508 uint32_t vm_state_size;
1509 #ifdef _WIN32
1510 struct _timeb tb;
1511 #else
1512 struct timeval tv;
1513 #endif
1514 const char *name = qdict_get_try_str(qdict, "name");
1516 bs = get_bs_snapshots();
1517 if (!bs) {
1518 monitor_printf(mon, "No block device can accept snapshots\n");
1519 return;
1522 /* ??? Should this occur after vm_stop? */
1523 qemu_aio_flush();
1525 saved_vm_running = vm_running;
1526 vm_stop(0);
1528 must_delete = 0;
1529 if (name) {
1530 ret = bdrv_snapshot_find(bs, old_sn, name);
1531 if (ret >= 0) {
1532 must_delete = 1;
1535 memset(sn, 0, sizeof(*sn));
1536 if (must_delete) {
1537 pstrcpy(sn->name, sizeof(sn->name), old_sn->name);
1538 pstrcpy(sn->id_str, sizeof(sn->id_str), old_sn->id_str);
1539 } else {
1540 if (name)
1541 pstrcpy(sn->name, sizeof(sn->name), name);
1544 /* fill auxiliary fields */
1545 #ifdef _WIN32
1546 _ftime(&tb);
1547 sn->date_sec = tb.time;
1548 sn->date_nsec = tb.millitm * 1000000;
1549 #else
1550 gettimeofday(&tv, NULL);
1551 sn->date_sec = tv.tv_sec;
1552 sn->date_nsec = tv.tv_usec * 1000;
1553 #endif
1554 sn->vm_clock_nsec = qemu_get_clock(vm_clock);
1556 /* save the VM state */
1557 f = qemu_fopen_bdrv(bs, 1);
1558 if (!f) {
1559 monitor_printf(mon, "Could not open VM state file\n");
1560 goto the_end;
1562 ret = qemu_savevm_state(f);
1563 vm_state_size = qemu_ftell(f);
1564 qemu_fclose(f);
1565 if (ret < 0) {
1566 monitor_printf(mon, "Error %d while writing VM\n", ret);
1567 goto the_end;
1570 /* create the snapshots */
1572 QTAILQ_FOREACH(dinfo, &drives, next) {
1573 bs1 = dinfo->bdrv;
1574 if (bdrv_has_snapshot(bs1)) {
1575 if (must_delete) {
1576 ret = bdrv_snapshot_delete(bs1, old_sn->id_str);
1577 if (ret < 0) {
1578 monitor_printf(mon,
1579 "Error while deleting snapshot on '%s'\n",
1580 bdrv_get_device_name(bs1));
1583 /* Write VM state size only to the image that contains the state */
1584 sn->vm_state_size = (bs == bs1 ? vm_state_size : 0);
1585 ret = bdrv_snapshot_create(bs1, sn);
1586 if (ret < 0) {
1587 monitor_printf(mon, "Error while creating snapshot on '%s'\n",
1588 bdrv_get_device_name(bs1));
1593 the_end:
1594 if (saved_vm_running)
1595 vm_start();
1598 int load_vmstate(Monitor *mon, const char *name)
1600 DriveInfo *dinfo;
1601 BlockDriverState *bs, *bs1;
1602 QEMUSnapshotInfo sn;
1603 QEMUFile *f;
1604 int ret;
1606 bs = get_bs_snapshots();
1607 if (!bs) {
1608 monitor_printf(mon, "No block device supports snapshots\n");
1609 return -EINVAL;
1612 /* Flush all IO requests so they don't interfere with the new state. */
1613 qemu_aio_flush();
1615 QTAILQ_FOREACH(dinfo, &drives, next) {
1616 bs1 = dinfo->bdrv;
1617 if (bdrv_has_snapshot(bs1)) {
1618 ret = bdrv_snapshot_goto(bs1, name);
1619 if (ret < 0) {
1620 if (bs != bs1)
1621 monitor_printf(mon, "Warning: ");
1622 switch(ret) {
1623 case -ENOTSUP:
1624 monitor_printf(mon,
1625 "Snapshots not supported on device '%s'\n",
1626 bdrv_get_device_name(bs1));
1627 break;
1628 case -ENOENT:
1629 monitor_printf(mon, "Could not find snapshot '%s' on "
1630 "device '%s'\n",
1631 name, bdrv_get_device_name(bs1));
1632 break;
1633 default:
1634 monitor_printf(mon, "Error %d while activating snapshot on"
1635 " '%s'\n", ret, bdrv_get_device_name(bs1));
1636 break;
1638 /* fatal on snapshot block device */
1639 if (bs == bs1)
1640 return 0;
1645 /* Don't even try to load empty VM states */
1646 ret = bdrv_snapshot_find(bs, &sn, name);
1647 if ((ret >= 0) && (sn.vm_state_size == 0))
1648 return -EINVAL;
1650 /* restore the VM state */
1651 f = qemu_fopen_bdrv(bs, 0);
1652 if (!f) {
1653 monitor_printf(mon, "Could not open VM state file\n");
1654 return -EINVAL;
1656 ret = qemu_loadvm_state(f);
1657 qemu_fclose(f);
1658 if (ret < 0) {
1659 monitor_printf(mon, "Error %d while loading VM state\n", ret);
1660 return ret;
1662 return 0;
1665 void do_delvm(Monitor *mon, const QDict *qdict)
1667 DriveInfo *dinfo;
1668 BlockDriverState *bs, *bs1;
1669 int ret;
1670 const char *name = qdict_get_str(qdict, "name");
1672 bs = get_bs_snapshots();
1673 if (!bs) {
1674 monitor_printf(mon, "No block device supports snapshots\n");
1675 return;
1678 QTAILQ_FOREACH(dinfo, &drives, next) {
1679 bs1 = dinfo->bdrv;
1680 if (bdrv_has_snapshot(bs1)) {
1681 ret = bdrv_snapshot_delete(bs1, name);
1682 if (ret < 0) {
1683 if (ret == -ENOTSUP)
1684 monitor_printf(mon,
1685 "Snapshots not supported on device '%s'\n",
1686 bdrv_get_device_name(bs1));
1687 else
1688 monitor_printf(mon, "Error %d while deleting snapshot on "
1689 "'%s'\n", ret, bdrv_get_device_name(bs1));
1695 void do_info_snapshots(Monitor *mon)
1697 DriveInfo *dinfo;
1698 BlockDriverState *bs, *bs1;
1699 QEMUSnapshotInfo *sn_tab, *sn;
1700 int nb_sns, i;
1701 char buf[256];
1703 bs = get_bs_snapshots();
1704 if (!bs) {
1705 monitor_printf(mon, "No available block device supports snapshots\n");
1706 return;
1708 monitor_printf(mon, "Snapshot devices:");
1709 QTAILQ_FOREACH(dinfo, &drives, next) {
1710 bs1 = dinfo->bdrv;
1711 if (bdrv_has_snapshot(bs1)) {
1712 if (bs == bs1)
1713 monitor_printf(mon, " %s", bdrv_get_device_name(bs1));
1716 monitor_printf(mon, "\n");
1718 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
1719 if (nb_sns < 0) {
1720 monitor_printf(mon, "bdrv_snapshot_list: error %d\n", nb_sns);
1721 return;
1723 monitor_printf(mon, "Snapshot list (from %s):\n",
1724 bdrv_get_device_name(bs));
1725 monitor_printf(mon, "%s\n", bdrv_snapshot_dump(buf, sizeof(buf), NULL));
1726 for(i = 0; i < nb_sns; i++) {
1727 sn = &sn_tab[i];
1728 monitor_printf(mon, "%s\n", bdrv_snapshot_dump(buf, sizeof(buf), sn));
1730 qemu_free(sn_tab);