]> git.proxmox.com Git - qemu.git/blob - savevm.c
vmstate: add VMS_VARRAY_UINT16_UNSAFE (varrays with uint16 indexes)
[qemu.git] / savevm.c
1 /*
2 * QEMU System Emulator
3 *
4 * Copyright (c) 2003-2008 Fabrice Bellard
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
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>
31
32 /* Needed early for CONFIG_BSD etc. */
33 #include "config-host.h"
34
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
72
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
81
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"
94
95 /* point to the block driver where the snapshots are managed */
96 static BlockDriverState *bs_snapshots;
97
98 #define SELF_ANNOUNCE_ROUNDS 5
99
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
106
107 static int announce_self_create(uint8_t *buf,
108 uint8_t *mac_addr)
109 {
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 */
114
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 */
125
126 /* Padding to get up to 60 bytes (ethernet min packet size, minus FCS). */
127 memset(buf + 42, 0x00, 18);
128
129 return 60; /* len (FCS will be added by hardware) */
130 }
131
132 static void qemu_announce_self_once(void *opaque)
133 {
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;
140
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);
148 }
149 }
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);
157 }
158 }
159
160 void qemu_announce_self(void)
161 {
162 static QEMUTimer *timer;
163 timer = qemu_new_timer(rt_clock, qemu_announce_self_once, &timer);
164 qemu_announce_self_once(&timer);
165 }
166
167 /***********************************************************/
168 /* savevm/loadvm support */
169
170 #define IO_BUF_SIZE 32768
171
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;
180
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];
186
187 int has_error;
188 };
189
190 typedef struct QEMUFileStdio
191 {
192 FILE *stdio_file;
193 QEMUFile *file;
194 } QEMUFileStdio;
195
196 typedef struct QEMUFileSocket
197 {
198 int fd;
199 QEMUFile *file;
200 } QEMUFileSocket;
201
202 static int socket_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
203 {
204 QEMUFileSocket *s = opaque;
205 ssize_t len;
206
207 do {
208 len = recv(s->fd, (void *)buf, size, 0);
209 } while (len == -1 && socket_error() == EINTR);
210
211 if (len == -1)
212 len = -socket_error();
213
214 return len;
215 }
216
217 static int socket_close(void *opaque)
218 {
219 QEMUFileSocket *s = opaque;
220 qemu_free(s);
221 return 0;
222 }
223
224 static int stdio_put_buffer(void *opaque, const uint8_t *buf, int64_t pos, int size)
225 {
226 QEMUFileStdio *s = opaque;
227 return fwrite(buf, 1, size, s->stdio_file);
228 }
229
230 static int stdio_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
231 {
232 QEMUFileStdio *s = opaque;
233 FILE *fp = s->stdio_file;
234 int bytes;
235
236 do {
237 clearerr(fp);
238 bytes = fread(buf, 1, size, fp);
239 } while ((bytes == 0) && ferror(fp) && (errno == EINTR));
240 return bytes;
241 }
242
243 static int stdio_pclose(void *opaque)
244 {
245 QEMUFileStdio *s = opaque;
246 pclose(s->stdio_file);
247 qemu_free(s);
248 return 0;
249 }
250
251 static int stdio_fclose(void *opaque)
252 {
253 QEMUFileStdio *s = opaque;
254 fclose(s->stdio_file);
255 qemu_free(s);
256 return 0;
257 }
258
259 QEMUFile *qemu_popen(FILE *stdio_file, const char *mode)
260 {
261 QEMUFileStdio *s;
262
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;
266 }
267
268 s = qemu_mallocz(sizeof(QEMUFileStdio));
269
270 s->stdio_file = stdio_file;
271
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);
276 }
277 return s->file;
278 }
279
280 QEMUFile *qemu_popen_cmd(const char *command, const char *mode)
281 {
282 FILE *popen_file;
283
284 popen_file = popen(command, mode);
285 if(popen_file == NULL) {
286 return NULL;
287 }
288
289 return qemu_popen(popen_file, mode);
290 }
291
292 int qemu_stdio_fd(QEMUFile *f)
293 {
294 QEMUFileStdio *p;
295 int fd;
296
297 p = (QEMUFileStdio *)f->opaque;
298 fd = fileno(p->stdio_file);
299
300 return fd;
301 }
302
303 QEMUFile *qemu_fdopen(int fd, const char *mode)
304 {
305 QEMUFileStdio *s;
306
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;
312 }
313
314 s = qemu_mallocz(sizeof(QEMUFileStdio));
315 s->stdio_file = fdopen(fd, mode);
316 if (!s->stdio_file)
317 goto fail;
318
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);
323 }
324 return s->file;
325
326 fail:
327 qemu_free(s);
328 return NULL;
329 }
330
331 QEMUFile *qemu_fopen_socket(int fd)
332 {
333 QEMUFileSocket *s = qemu_mallocz(sizeof(QEMUFileSocket));
334
335 s->fd = fd;
336 s->file = qemu_fopen_ops(s, NULL, socket_get_buffer, socket_close, NULL, NULL);
337 return s->file;
338 }
339
340 static int file_put_buffer(void *opaque, const uint8_t *buf,
341 int64_t pos, int size)
342 {
343 QEMUFileStdio *s = opaque;
344 fseek(s->stdio_file, pos, SEEK_SET);
345 fwrite(buf, 1, size, s->stdio_file);
346 return size;
347 }
348
349 static int file_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
350 {
351 QEMUFileStdio *s = opaque;
352 fseek(s->stdio_file, pos, SEEK_SET);
353 return fread(buf, 1, size, s->stdio_file);
354 }
355
356 QEMUFile *qemu_fopen(const char *filename, const char *mode)
357 {
358 QEMUFileStdio *s;
359
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;
365 }
366
367 s = qemu_mallocz(sizeof(QEMUFileStdio));
368
369 s->stdio_file = fopen(filename, mode);
370 if (!s->stdio_file)
371 goto fail;
372
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);
377 }
378 return s->file;
379 fail:
380 qemu_free(s);
381 return NULL;
382 }
383
384 static int block_put_buffer(void *opaque, const uint8_t *buf,
385 int64_t pos, int size)
386 {
387 bdrv_save_vmstate(opaque, buf, pos, size);
388 return size;
389 }
390
391 static int block_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
392 {
393 return bdrv_load_vmstate(opaque, buf, pos, size);
394 }
395
396 static int bdrv_fclose(void *opaque)
397 {
398 return 0;
399 }
400
401 static QEMUFile *qemu_fopen_bdrv(BlockDriverState *bs, int is_writable)
402 {
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);
406 }
407
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)
413 {
414 QEMUFile *f;
415
416 f = qemu_mallocz(sizeof(QEMUFile));
417
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;
425
426 return f;
427 }
428
429 int qemu_file_has_error(QEMUFile *f)
430 {
431 return f->has_error;
432 }
433
434 void qemu_file_set_error(QEMUFile *f)
435 {
436 f->has_error = 1;
437 }
438
439 void qemu_fflush(QEMUFile *f)
440 {
441 if (!f->put_buffer)
442 return;
443
444 if (f->is_write && f->buf_index > 0) {
445 int len;
446
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;
453 }
454 }
455
456 static void qemu_fill_buffer(QEMUFile *f)
457 {
458 int len;
459
460 if (!f->get_buffer)
461 return;
462
463 if (f->is_write)
464 abort();
465
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;
473 }
474
475 int qemu_fclose(QEMUFile *f)
476 {
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;
483 }
484
485 void qemu_file_put_notify(QEMUFile *f)
486 {
487 f->put_buffer(f->opaque, NULL, 0, 0);
488 }
489
490 void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size)
491 {
492 int l;
493
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();
498 }
499
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);
511 }
512 }
513
514 void qemu_put_byte(QEMUFile *f, int v)
515 {
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();
520 }
521
522 f->buf[f->buf_index++] = v;
523 f->is_write = 1;
524 if (f->buf_index >= IO_BUF_SIZE)
525 qemu_fflush(f);
526 }
527
528 int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size1)
529 {
530 int size, l;
531
532 if (f->is_write)
533 abort();
534
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;
543 }
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;
550 }
551 return size1 - size;
552 }
553
554 int qemu_get_byte(QEMUFile *f)
555 {
556 if (f->is_write)
557 abort();
558
559 if (f->buf_index >= f->buf_size) {
560 qemu_fill_buffer(f);
561 if (f->buf_index >= f->buf_size)
562 return 0;
563 }
564 return f->buf[f->buf_index++];
565 }
566
567 int64_t qemu_ftell(QEMUFile *f)
568 {
569 return f->buf_offset - f->buf_size + f->buf_index;
570 }
571
572 int64_t qemu_fseek(QEMUFile *f, int64_t pos, int whence)
573 {
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;
581 }
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;
589 }
590 return pos;
591 }
592
593 int qemu_file_rate_limit(QEMUFile *f)
594 {
595 if (f->rate_limit)
596 return f->rate_limit(f->opaque);
597
598 return 0;
599 }
600
601 size_t qemu_file_set_rate_limit(QEMUFile *f, size_t new_rate)
602 {
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);
607
608 return 0;
609 }
610
611 void qemu_put_be16(QEMUFile *f, unsigned int v)
612 {
613 qemu_put_byte(f, v >> 8);
614 qemu_put_byte(f, v);
615 }
616
617 void qemu_put_be32(QEMUFile *f, unsigned int v)
618 {
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);
623 }
624
625 void qemu_put_be64(QEMUFile *f, uint64_t v)
626 {
627 qemu_put_be32(f, v >> 32);
628 qemu_put_be32(f, v);
629 }
630
631 unsigned int qemu_get_be16(QEMUFile *f)
632 {
633 unsigned int v;
634 v = qemu_get_byte(f) << 8;
635 v |= qemu_get_byte(f);
636 return v;
637 }
638
639 unsigned int qemu_get_be32(QEMUFile *f)
640 {
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;
647 }
648
649 uint64_t qemu_get_be64(QEMUFile *f)
650 {
651 uint64_t v;
652 v = (uint64_t)qemu_get_be32(f) << 32;
653 v |= qemu_get_be32(f);
654 return v;
655 }
656
657 /* 8 bit int */
658
659 static int get_int8(QEMUFile *f, void *pv, size_t size)
660 {
661 int8_t *v = pv;
662 qemu_get_s8s(f, v);
663 return 0;
664 }
665
666 static void put_int8(QEMUFile *f, void *pv, size_t size)
667 {
668 int8_t *v = pv;
669 qemu_put_s8s(f, v);
670 }
671
672 const VMStateInfo vmstate_info_int8 = {
673 .name = "int8",
674 .get = get_int8,
675 .put = put_int8,
676 };
677
678 /* 16 bit int */
679
680 static int get_int16(QEMUFile *f, void *pv, size_t size)
681 {
682 int16_t *v = pv;
683 qemu_get_sbe16s(f, v);
684 return 0;
685 }
686
687 static void put_int16(QEMUFile *f, void *pv, size_t size)
688 {
689 int16_t *v = pv;
690 qemu_put_sbe16s(f, v);
691 }
692
693 const VMStateInfo vmstate_info_int16 = {
694 .name = "int16",
695 .get = get_int16,
696 .put = put_int16,
697 };
698
699 /* 32 bit int */
700
701 static int get_int32(QEMUFile *f, void *pv, size_t size)
702 {
703 int32_t *v = pv;
704 qemu_get_sbe32s(f, v);
705 return 0;
706 }
707
708 static void put_int32(QEMUFile *f, void *pv, size_t size)
709 {
710 int32_t *v = pv;
711 qemu_put_sbe32s(f, v);
712 }
713
714 const VMStateInfo vmstate_info_int32 = {
715 .name = "int32",
716 .get = get_int32,
717 .put = put_int32,
718 };
719
720 /* 32 bit int. See that the received value is the same than the one
721 in the field */
722
723 static int get_int32_equal(QEMUFile *f, void *pv, size_t size)
724 {
725 int32_t *v = pv;
726 int32_t v2;
727 qemu_get_sbe32s(f, &v2);
728
729 if (*v == v2)
730 return 0;
731 return -EINVAL;
732 }
733
734 const VMStateInfo vmstate_info_int32_equal = {
735 .name = "int32 equal",
736 .get = get_int32_equal,
737 .put = put_int32,
738 };
739
740 /* 32 bit int. See that the received value is the less or the same
741 than the one in the field */
742
743 static int get_int32_le(QEMUFile *f, void *pv, size_t size)
744 {
745 int32_t *old = pv;
746 int32_t new;
747 qemu_get_sbe32s(f, &new);
748
749 if (*old <= new)
750 return 0;
751 return -EINVAL;
752 }
753
754 const VMStateInfo vmstate_info_int32_le = {
755 .name = "int32 equal",
756 .get = get_int32_le,
757 .put = put_int32,
758 };
759
760 /* 64 bit int */
761
762 static int get_int64(QEMUFile *f, void *pv, size_t size)
763 {
764 int64_t *v = pv;
765 qemu_get_sbe64s(f, v);
766 return 0;
767 }
768
769 static void put_int64(QEMUFile *f, void *pv, size_t size)
770 {
771 int64_t *v = pv;
772 qemu_put_sbe64s(f, v);
773 }
774
775 const VMStateInfo vmstate_info_int64 = {
776 .name = "int64",
777 .get = get_int64,
778 .put = put_int64,
779 };
780
781 /* 8 bit unsigned int */
782
783 static int get_uint8(QEMUFile *f, void *pv, size_t size)
784 {
785 uint8_t *v = pv;
786 qemu_get_8s(f, v);
787 return 0;
788 }
789
790 static void put_uint8(QEMUFile *f, void *pv, size_t size)
791 {
792 uint8_t *v = pv;
793 qemu_put_8s(f, v);
794 }
795
796 const VMStateInfo vmstate_info_uint8 = {
797 .name = "uint8",
798 .get = get_uint8,
799 .put = put_uint8,
800 };
801
802 /* 16 bit unsigned int */
803
804 static int get_uint16(QEMUFile *f, void *pv, size_t size)
805 {
806 uint16_t *v = pv;
807 qemu_get_be16s(f, v);
808 return 0;
809 }
810
811 static void put_uint16(QEMUFile *f, void *pv, size_t size)
812 {
813 uint16_t *v = pv;
814 qemu_put_be16s(f, v);
815 }
816
817 const VMStateInfo vmstate_info_uint16 = {
818 .name = "uint16",
819 .get = get_uint16,
820 .put = put_uint16,
821 };
822
823 /* 32 bit unsigned int */
824
825 static int get_uint32(QEMUFile *f, void *pv, size_t size)
826 {
827 uint32_t *v = pv;
828 qemu_get_be32s(f, v);
829 return 0;
830 }
831
832 static void put_uint32(QEMUFile *f, void *pv, size_t size)
833 {
834 uint32_t *v = pv;
835 qemu_put_be32s(f, v);
836 }
837
838 const VMStateInfo vmstate_info_uint32 = {
839 .name = "uint32",
840 .get = get_uint32,
841 .put = put_uint32,
842 };
843
844 /* 64 bit unsigned int */
845
846 static int get_uint64(QEMUFile *f, void *pv, size_t size)
847 {
848 uint64_t *v = pv;
849 qemu_get_be64s(f, v);
850 return 0;
851 }
852
853 static void put_uint64(QEMUFile *f, void *pv, size_t size)
854 {
855 uint64_t *v = pv;
856 qemu_put_be64s(f, v);
857 }
858
859 const VMStateInfo vmstate_info_uint64 = {
860 .name = "uint64",
861 .get = get_uint64,
862 .put = put_uint64,
863 };
864
865 /* 8 bit int. See that the received value is the same than the one
866 in the field */
867
868 static int get_uint8_equal(QEMUFile *f, void *pv, size_t size)
869 {
870 uint8_t *v = pv;
871 uint8_t v2;
872 qemu_get_8s(f, &v2);
873
874 if (*v == v2)
875 return 0;
876 return -EINVAL;
877 }
878
879 const VMStateInfo vmstate_info_uint8_equal = {
880 .name = "uint8 equal",
881 .get = get_uint8_equal,
882 .put = put_uint8,
883 };
884
885 /* 16 bit unsigned int int. See that the received value is the same than the one
886 in the field */
887
888 static int get_uint16_equal(QEMUFile *f, void *pv, size_t size)
889 {
890 uint16_t *v = pv;
891 uint16_t v2;
892 qemu_get_be16s(f, &v2);
893
894 if (*v == v2)
895 return 0;
896 return -EINVAL;
897 }
898
899 const VMStateInfo vmstate_info_uint16_equal = {
900 .name = "uint16 equal",
901 .get = get_uint16_equal,
902 .put = put_uint16,
903 };
904
905 /* timers */
906
907 static int get_timer(QEMUFile *f, void *pv, size_t size)
908 {
909 QEMUTimer *v = pv;
910 qemu_get_timer(f, v);
911 return 0;
912 }
913
914 static void put_timer(QEMUFile *f, void *pv, size_t size)
915 {
916 QEMUTimer *v = pv;
917 qemu_put_timer(f, v);
918 }
919
920 const VMStateInfo vmstate_info_timer = {
921 .name = "timer",
922 .get = get_timer,
923 .put = put_timer,
924 };
925
926 /* uint8_t buffers */
927
928 static int get_buffer(QEMUFile *f, void *pv, size_t size)
929 {
930 uint8_t *v = pv;
931 qemu_get_buffer(f, v, size);
932 return 0;
933 }
934
935 static void put_buffer(QEMUFile *f, void *pv, size_t size)
936 {
937 uint8_t *v = pv;
938 qemu_put_buffer(f, v, size);
939 }
940
941 const VMStateInfo vmstate_info_buffer = {
942 .name = "buffer",
943 .get = get_buffer,
944 .put = put_buffer,
945 };
946
947 typedef struct SaveStateEntry {
948 QTAILQ_ENTRY(SaveStateEntry) entry;
949 char idstr[256];
950 int instance_id;
951 int version_id;
952 int section_id;
953 SaveLiveStateHandler *save_live_state;
954 SaveStateHandler *save_state;
955 LoadStateHandler *load_state;
956 const VMStateDescription *vmsd;
957 void *opaque;
958 } SaveStateEntry;
959
960 static QTAILQ_HEAD(savevm_handlers, SaveStateEntry) savevm_handlers =
961 QTAILQ_HEAD_INITIALIZER(savevm_handlers);
962 static int global_section_id;
963
964 static int calculate_new_instance_id(const char *idstr)
965 {
966 SaveStateEntry *se;
967 int instance_id = 0;
968
969 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
970 if (strcmp(idstr, se->idstr) == 0
971 && instance_id <= se->instance_id) {
972 instance_id = se->instance_id + 1;
973 }
974 }
975 return instance_id;
976 }
977
978 /* TODO: Individual devices generally have very little idea about the rest
979 of the system, so instance_id should be removed/replaced.
980 Meanwhile pass -1 as instance_id if you do not already have a clearly
981 distinguishing id for all instances of your device class. */
982 int register_savevm_live(const char *idstr,
983 int instance_id,
984 int version_id,
985 SaveLiveStateHandler *save_live_state,
986 SaveStateHandler *save_state,
987 LoadStateHandler *load_state,
988 void *opaque)
989 {
990 SaveStateEntry *se;
991
992 se = qemu_malloc(sizeof(SaveStateEntry));
993 pstrcpy(se->idstr, sizeof(se->idstr), idstr);
994 se->version_id = version_id;
995 se->section_id = global_section_id++;
996 se->save_live_state = save_live_state;
997 se->save_state = save_state;
998 se->load_state = load_state;
999 se->opaque = opaque;
1000 se->vmsd = NULL;
1001
1002 if (instance_id == -1) {
1003 se->instance_id = calculate_new_instance_id(idstr);
1004 } else {
1005 se->instance_id = instance_id;
1006 }
1007 /* add at the end of list */
1008 QTAILQ_INSERT_TAIL(&savevm_handlers, se, entry);
1009 return 0;
1010 }
1011
1012 int register_savevm(const char *idstr,
1013 int instance_id,
1014 int version_id,
1015 SaveStateHandler *save_state,
1016 LoadStateHandler *load_state,
1017 void *opaque)
1018 {
1019 return register_savevm_live(idstr, instance_id, version_id,
1020 NULL, save_state, load_state, opaque);
1021 }
1022
1023 void unregister_savevm(const char *idstr, void *opaque)
1024 {
1025 SaveStateEntry *se, *new_se;
1026
1027 QTAILQ_FOREACH_SAFE(se, &savevm_handlers, entry, new_se) {
1028 if (strcmp(se->idstr, idstr) == 0 && se->opaque == opaque) {
1029 QTAILQ_REMOVE(&savevm_handlers, se, entry);
1030 qemu_free(se);
1031 }
1032 }
1033 }
1034
1035 int vmstate_register(int instance_id, const VMStateDescription *vmsd,
1036 void *opaque)
1037 {
1038 SaveStateEntry *se;
1039
1040 se = qemu_malloc(sizeof(SaveStateEntry));
1041 pstrcpy(se->idstr, sizeof(se->idstr), vmsd->name);
1042 se->version_id = vmsd->version_id;
1043 se->section_id = global_section_id++;
1044 se->save_live_state = NULL;
1045 se->save_state = NULL;
1046 se->load_state = NULL;
1047 se->opaque = opaque;
1048 se->vmsd = vmsd;
1049
1050 if (instance_id == -1) {
1051 se->instance_id = calculate_new_instance_id(vmsd->name);
1052 } else {
1053 se->instance_id = instance_id;
1054 }
1055 /* add at the end of list */
1056 QTAILQ_INSERT_TAIL(&savevm_handlers, se, entry);
1057 return 0;
1058 }
1059
1060 void vmstate_unregister(const VMStateDescription *vmsd, void *opaque)
1061 {
1062 SaveStateEntry *se, *new_se;
1063
1064 QTAILQ_FOREACH_SAFE(se, &savevm_handlers, entry, new_se) {
1065 if (se->vmsd == vmsd && se->opaque == opaque) {
1066 QTAILQ_REMOVE(&savevm_handlers, se, entry);
1067 qemu_free(se);
1068 }
1069 }
1070 }
1071
1072 int vmstate_load_state(QEMUFile *f, const VMStateDescription *vmsd,
1073 void *opaque, int version_id)
1074 {
1075 VMStateField *field = vmsd->fields;
1076
1077 if (version_id > vmsd->version_id) {
1078 return -EINVAL;
1079 }
1080 if (version_id < vmsd->minimum_version_id_old) {
1081 return -EINVAL;
1082 }
1083 if (version_id < vmsd->minimum_version_id) {
1084 return vmsd->load_state_old(f, opaque, version_id);
1085 }
1086 if (vmsd->pre_load) {
1087 int ret = vmsd->pre_load(opaque);
1088 if (ret)
1089 return ret;
1090 }
1091 while(field->name) {
1092 if ((field->field_exists &&
1093 field->field_exists(opaque, version_id)) ||
1094 (!field->field_exists &&
1095 field->version_id <= version_id)) {
1096 void *base_addr = opaque + field->offset;
1097 int ret, i, n_elems = 1;
1098
1099 if (field->flags & VMS_ARRAY) {
1100 n_elems = field->num;
1101 } else if (field->flags & VMS_VARRAY_INT32) {
1102 n_elems = *(int32_t *)(opaque+field->num_offset);
1103 } else if (field->flags & VMS_VARRAY_UINT16) {
1104 n_elems = *(uint16_t *)(opaque+field->num_offset);
1105 }
1106 if (field->flags & VMS_POINTER) {
1107 base_addr = *(void **)base_addr;
1108 }
1109 for (i = 0; i < n_elems; i++) {
1110 void *addr = base_addr + field->size * i;
1111
1112 if (field->flags & VMS_ARRAY_OF_POINTER) {
1113 addr = *(void **)addr;
1114 }
1115 if (field->flags & VMS_STRUCT) {
1116 ret = vmstate_load_state(f, field->vmsd, addr, field->vmsd->version_id);
1117 } else {
1118 ret = field->info->get(f, addr, field->size);
1119
1120 }
1121 if (ret < 0) {
1122 return ret;
1123 }
1124 }
1125 }
1126 field++;
1127 }
1128 if (vmsd->post_load) {
1129 return vmsd->post_load(opaque, version_id);
1130 }
1131 return 0;
1132 }
1133
1134 void vmstate_save_state(QEMUFile *f, const VMStateDescription *vmsd,
1135 void *opaque)
1136 {
1137 VMStateField *field = vmsd->fields;
1138
1139 if (vmsd->pre_save) {
1140 vmsd->pre_save(opaque);
1141 }
1142 while(field->name) {
1143 if (!field->field_exists ||
1144 field->field_exists(opaque, vmsd->version_id)) {
1145 void *base_addr = opaque + field->offset;
1146 int i, n_elems = 1;
1147
1148 if (field->flags & VMS_ARRAY) {
1149 n_elems = field->num;
1150 } else if (field->flags & VMS_VARRAY_INT32) {
1151 n_elems = *(int32_t *)(opaque+field->num_offset);
1152 } else if (field->flags & VMS_VARRAY_UINT16) {
1153 n_elems = *(uint16_t *)(opaque+field->num_offset);
1154 }
1155 if (field->flags & VMS_POINTER) {
1156 base_addr = *(void **)base_addr;
1157 }
1158 for (i = 0; i < n_elems; i++) {
1159 void *addr = base_addr + field->size * i;
1160
1161 if (field->flags & VMS_STRUCT) {
1162 vmstate_save_state(f, field->vmsd, addr);
1163 } else {
1164 field->info->put(f, addr, field->size);
1165 }
1166 }
1167 }
1168 field++;
1169 }
1170 if (vmsd->post_save) {
1171 vmsd->post_save(opaque);
1172 }
1173 }
1174
1175 static int vmstate_load(QEMUFile *f, SaveStateEntry *se, int version_id)
1176 {
1177 if (!se->vmsd) { /* Old style */
1178 return se->load_state(f, se->opaque, version_id);
1179 }
1180 return vmstate_load_state(f, se->vmsd, se->opaque, version_id);
1181 }
1182
1183 static void vmstate_save(QEMUFile *f, SaveStateEntry *se)
1184 {
1185 if (!se->vmsd) { /* Old style */
1186 se->save_state(f, se->opaque);
1187 return;
1188 }
1189 vmstate_save_state(f,se->vmsd, se->opaque);
1190 }
1191
1192 #define QEMU_VM_FILE_MAGIC 0x5145564d
1193 #define QEMU_VM_FILE_VERSION_COMPAT 0x00000002
1194 #define QEMU_VM_FILE_VERSION 0x00000003
1195
1196 #define QEMU_VM_EOF 0x00
1197 #define QEMU_VM_SECTION_START 0x01
1198 #define QEMU_VM_SECTION_PART 0x02
1199 #define QEMU_VM_SECTION_END 0x03
1200 #define QEMU_VM_SECTION_FULL 0x04
1201
1202 int qemu_savevm_state_begin(QEMUFile *f)
1203 {
1204 SaveStateEntry *se;
1205
1206 qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
1207 qemu_put_be32(f, QEMU_VM_FILE_VERSION);
1208
1209 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1210 int len;
1211
1212 if (se->save_live_state == NULL)
1213 continue;
1214
1215 /* Section type */
1216 qemu_put_byte(f, QEMU_VM_SECTION_START);
1217 qemu_put_be32(f, se->section_id);
1218
1219 /* ID string */
1220 len = strlen(se->idstr);
1221 qemu_put_byte(f, len);
1222 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
1223
1224 qemu_put_be32(f, se->instance_id);
1225 qemu_put_be32(f, se->version_id);
1226
1227 se->save_live_state(f, QEMU_VM_SECTION_START, se->opaque);
1228 }
1229
1230 if (qemu_file_has_error(f))
1231 return -EIO;
1232
1233 return 0;
1234 }
1235
1236 int qemu_savevm_state_iterate(QEMUFile *f)
1237 {
1238 SaveStateEntry *se;
1239 int ret = 1;
1240
1241 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1242 if (se->save_live_state == NULL)
1243 continue;
1244
1245 /* Section type */
1246 qemu_put_byte(f, QEMU_VM_SECTION_PART);
1247 qemu_put_be32(f, se->section_id);
1248
1249 ret &= !!se->save_live_state(f, QEMU_VM_SECTION_PART, se->opaque);
1250 }
1251
1252 if (ret)
1253 return 1;
1254
1255 if (qemu_file_has_error(f))
1256 return -EIO;
1257
1258 return 0;
1259 }
1260
1261 int qemu_savevm_state_complete(QEMUFile *f)
1262 {
1263 SaveStateEntry *se;
1264
1265 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1266 if (se->save_live_state == NULL)
1267 continue;
1268
1269 /* Section type */
1270 qemu_put_byte(f, QEMU_VM_SECTION_END);
1271 qemu_put_be32(f, se->section_id);
1272
1273 se->save_live_state(f, QEMU_VM_SECTION_END, se->opaque);
1274 }
1275
1276 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1277 int len;
1278
1279 if (se->save_state == NULL && se->vmsd == NULL)
1280 continue;
1281
1282 /* Section type */
1283 qemu_put_byte(f, QEMU_VM_SECTION_FULL);
1284 qemu_put_be32(f, se->section_id);
1285
1286 /* ID string */
1287 len = strlen(se->idstr);
1288 qemu_put_byte(f, len);
1289 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
1290
1291 qemu_put_be32(f, se->instance_id);
1292 qemu_put_be32(f, se->version_id);
1293
1294 vmstate_save(f, se);
1295 }
1296
1297 qemu_put_byte(f, QEMU_VM_EOF);
1298
1299 if (qemu_file_has_error(f))
1300 return -EIO;
1301
1302 return 0;
1303 }
1304
1305 int qemu_savevm_state(QEMUFile *f)
1306 {
1307 int saved_vm_running;
1308 int ret;
1309
1310 saved_vm_running = vm_running;
1311 vm_stop(0);
1312
1313 bdrv_flush_all();
1314
1315 ret = qemu_savevm_state_begin(f);
1316 if (ret < 0)
1317 goto out;
1318
1319 do {
1320 ret = qemu_savevm_state_iterate(f);
1321 if (ret < 0)
1322 goto out;
1323 } while (ret == 0);
1324
1325 ret = qemu_savevm_state_complete(f);
1326
1327 out:
1328 if (qemu_file_has_error(f))
1329 ret = -EIO;
1330
1331 if (!ret && saved_vm_running)
1332 vm_start();
1333
1334 return ret;
1335 }
1336
1337 static SaveStateEntry *find_se(const char *idstr, int instance_id)
1338 {
1339 SaveStateEntry *se;
1340
1341 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1342 if (!strcmp(se->idstr, idstr) &&
1343 instance_id == se->instance_id)
1344 return se;
1345 }
1346 return NULL;
1347 }
1348
1349 typedef struct LoadStateEntry {
1350 QLIST_ENTRY(LoadStateEntry) entry;
1351 SaveStateEntry *se;
1352 int section_id;
1353 int version_id;
1354 } LoadStateEntry;
1355
1356 int qemu_loadvm_state(QEMUFile *f)
1357 {
1358 QLIST_HEAD(, LoadStateEntry) loadvm_handlers =
1359 QLIST_HEAD_INITIALIZER(loadvm_handlers);
1360 LoadStateEntry *le, *new_le;
1361 uint8_t section_type;
1362 unsigned int v;
1363 int ret;
1364
1365 v = qemu_get_be32(f);
1366 if (v != QEMU_VM_FILE_MAGIC)
1367 return -EINVAL;
1368
1369 v = qemu_get_be32(f);
1370 if (v == QEMU_VM_FILE_VERSION_COMPAT) {
1371 fprintf(stderr, "SaveVM v2 format is obsolete and don't work anymore\n");
1372 return -ENOTSUP;
1373 }
1374 if (v != QEMU_VM_FILE_VERSION)
1375 return -ENOTSUP;
1376
1377 while ((section_type = qemu_get_byte(f)) != QEMU_VM_EOF) {
1378 uint32_t instance_id, version_id, section_id;
1379 SaveStateEntry *se;
1380 char idstr[257];
1381 int len;
1382
1383 switch (section_type) {
1384 case QEMU_VM_SECTION_START:
1385 case QEMU_VM_SECTION_FULL:
1386 /* Read section start */
1387 section_id = qemu_get_be32(f);
1388 len = qemu_get_byte(f);
1389 qemu_get_buffer(f, (uint8_t *)idstr, len);
1390 idstr[len] = 0;
1391 instance_id = qemu_get_be32(f);
1392 version_id = qemu_get_be32(f);
1393
1394 /* Find savevm section */
1395 se = find_se(idstr, instance_id);
1396 if (se == NULL) {
1397 fprintf(stderr, "Unknown savevm section or instance '%s' %d\n", idstr, instance_id);
1398 ret = -EINVAL;
1399 goto out;
1400 }
1401
1402 /* Validate version */
1403 if (version_id > se->version_id) {
1404 fprintf(stderr, "savevm: unsupported version %d for '%s' v%d\n",
1405 version_id, idstr, se->version_id);
1406 ret = -EINVAL;
1407 goto out;
1408 }
1409
1410 /* Add entry */
1411 le = qemu_mallocz(sizeof(*le));
1412
1413 le->se = se;
1414 le->section_id = section_id;
1415 le->version_id = version_id;
1416 QLIST_INSERT_HEAD(&loadvm_handlers, le, entry);
1417
1418 ret = vmstate_load(f, le->se, le->version_id);
1419 if (ret < 0) {
1420 fprintf(stderr, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
1421 instance_id, idstr);
1422 goto out;
1423 }
1424 break;
1425 case QEMU_VM_SECTION_PART:
1426 case QEMU_VM_SECTION_END:
1427 section_id = qemu_get_be32(f);
1428
1429 QLIST_FOREACH(le, &loadvm_handlers, entry) {
1430 if (le->section_id == section_id) {
1431 break;
1432 }
1433 }
1434 if (le == NULL) {
1435 fprintf(stderr, "Unknown savevm section %d\n", section_id);
1436 ret = -EINVAL;
1437 goto out;
1438 }
1439
1440 ret = vmstate_load(f, le->se, le->version_id);
1441 if (ret < 0) {
1442 fprintf(stderr, "qemu: warning: error while loading state section id %d\n",
1443 section_id);
1444 goto out;
1445 }
1446 break;
1447 default:
1448 fprintf(stderr, "Unknown savevm section type %d\n", section_type);
1449 ret = -EINVAL;
1450 goto out;
1451 }
1452 }
1453
1454 ret = 0;
1455
1456 out:
1457 QLIST_FOREACH_SAFE(le, &loadvm_handlers, entry, new_le) {
1458 QLIST_REMOVE(le, entry);
1459 qemu_free(le);
1460 }
1461
1462 if (qemu_file_has_error(f))
1463 ret = -EIO;
1464
1465 return ret;
1466 }
1467
1468 /* device can contain snapshots */
1469 static int bdrv_can_snapshot(BlockDriverState *bs)
1470 {
1471 return (bs &&
1472 !bdrv_is_removable(bs) &&
1473 !bdrv_is_read_only(bs));
1474 }
1475
1476 /* device must be snapshots in order to have a reliable snapshot */
1477 static int bdrv_has_snapshot(BlockDriverState *bs)
1478 {
1479 return (bs &&
1480 !bdrv_is_removable(bs) &&
1481 !bdrv_is_read_only(bs));
1482 }
1483
1484 static BlockDriverState *get_bs_snapshots(void)
1485 {
1486 BlockDriverState *bs;
1487 DriveInfo *dinfo;
1488
1489 if (bs_snapshots)
1490 return bs_snapshots;
1491 QTAILQ_FOREACH(dinfo, &drives, next) {
1492 bs = dinfo->bdrv;
1493 if (bdrv_can_snapshot(bs))
1494 goto ok;
1495 }
1496 return NULL;
1497 ok:
1498 bs_snapshots = bs;
1499 return bs;
1500 }
1501
1502 static int bdrv_snapshot_find(BlockDriverState *bs, QEMUSnapshotInfo *sn_info,
1503 const char *name)
1504 {
1505 QEMUSnapshotInfo *sn_tab, *sn;
1506 int nb_sns, i, ret;
1507
1508 ret = -ENOENT;
1509 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
1510 if (nb_sns < 0)
1511 return ret;
1512 for(i = 0; i < nb_sns; i++) {
1513 sn = &sn_tab[i];
1514 if (!strcmp(sn->id_str, name) || !strcmp(sn->name, name)) {
1515 *sn_info = *sn;
1516 ret = 0;
1517 break;
1518 }
1519 }
1520 qemu_free(sn_tab);
1521 return ret;
1522 }
1523
1524 void do_savevm(Monitor *mon, const QDict *qdict)
1525 {
1526 DriveInfo *dinfo;
1527 BlockDriverState *bs, *bs1;
1528 QEMUSnapshotInfo sn1, *sn = &sn1, old_sn1, *old_sn = &old_sn1;
1529 int must_delete, ret;
1530 QEMUFile *f;
1531 int saved_vm_running;
1532 uint32_t vm_state_size;
1533 #ifdef _WIN32
1534 struct _timeb tb;
1535 #else
1536 struct timeval tv;
1537 #endif
1538 const char *name = qdict_get_try_str(qdict, "name");
1539
1540 bs = get_bs_snapshots();
1541 if (!bs) {
1542 monitor_printf(mon, "No block device can accept snapshots\n");
1543 return;
1544 }
1545
1546 /* ??? Should this occur after vm_stop? */
1547 qemu_aio_flush();
1548
1549 saved_vm_running = vm_running;
1550 vm_stop(0);
1551
1552 must_delete = 0;
1553 if (name) {
1554 ret = bdrv_snapshot_find(bs, old_sn, name);
1555 if (ret >= 0) {
1556 must_delete = 1;
1557 }
1558 }
1559 memset(sn, 0, sizeof(*sn));
1560 if (must_delete) {
1561 pstrcpy(sn->name, sizeof(sn->name), old_sn->name);
1562 pstrcpy(sn->id_str, sizeof(sn->id_str), old_sn->id_str);
1563 } else {
1564 if (name)
1565 pstrcpy(sn->name, sizeof(sn->name), name);
1566 }
1567
1568 /* fill auxiliary fields */
1569 #ifdef _WIN32
1570 _ftime(&tb);
1571 sn->date_sec = tb.time;
1572 sn->date_nsec = tb.millitm * 1000000;
1573 #else
1574 gettimeofday(&tv, NULL);
1575 sn->date_sec = tv.tv_sec;
1576 sn->date_nsec = tv.tv_usec * 1000;
1577 #endif
1578 sn->vm_clock_nsec = qemu_get_clock(vm_clock);
1579
1580 /* save the VM state */
1581 f = qemu_fopen_bdrv(bs, 1);
1582 if (!f) {
1583 monitor_printf(mon, "Could not open VM state file\n");
1584 goto the_end;
1585 }
1586 ret = qemu_savevm_state(f);
1587 vm_state_size = qemu_ftell(f);
1588 qemu_fclose(f);
1589 if (ret < 0) {
1590 monitor_printf(mon, "Error %d while writing VM\n", ret);
1591 goto the_end;
1592 }
1593
1594 /* create the snapshots */
1595
1596 QTAILQ_FOREACH(dinfo, &drives, next) {
1597 bs1 = dinfo->bdrv;
1598 if (bdrv_has_snapshot(bs1)) {
1599 if (must_delete) {
1600 ret = bdrv_snapshot_delete(bs1, old_sn->id_str);
1601 if (ret < 0) {
1602 monitor_printf(mon,
1603 "Error while deleting snapshot on '%s'\n",
1604 bdrv_get_device_name(bs1));
1605 }
1606 }
1607 /* Write VM state size only to the image that contains the state */
1608 sn->vm_state_size = (bs == bs1 ? vm_state_size : 0);
1609 ret = bdrv_snapshot_create(bs1, sn);
1610 if (ret < 0) {
1611 monitor_printf(mon, "Error while creating snapshot on '%s'\n",
1612 bdrv_get_device_name(bs1));
1613 }
1614 }
1615 }
1616
1617 the_end:
1618 if (saved_vm_running)
1619 vm_start();
1620 }
1621
1622 int load_vmstate(Monitor *mon, const char *name)
1623 {
1624 DriveInfo *dinfo;
1625 BlockDriverState *bs, *bs1;
1626 QEMUSnapshotInfo sn;
1627 QEMUFile *f;
1628 int ret;
1629
1630 bs = get_bs_snapshots();
1631 if (!bs) {
1632 monitor_printf(mon, "No block device supports snapshots\n");
1633 return -EINVAL;
1634 }
1635
1636 /* Flush all IO requests so they don't interfere with the new state. */
1637 qemu_aio_flush();
1638
1639 QTAILQ_FOREACH(dinfo, &drives, next) {
1640 bs1 = dinfo->bdrv;
1641 if (bdrv_has_snapshot(bs1)) {
1642 ret = bdrv_snapshot_goto(bs1, name);
1643 if (ret < 0) {
1644 if (bs != bs1)
1645 monitor_printf(mon, "Warning: ");
1646 switch(ret) {
1647 case -ENOTSUP:
1648 monitor_printf(mon,
1649 "Snapshots not supported on device '%s'\n",
1650 bdrv_get_device_name(bs1));
1651 break;
1652 case -ENOENT:
1653 monitor_printf(mon, "Could not find snapshot '%s' on "
1654 "device '%s'\n",
1655 name, bdrv_get_device_name(bs1));
1656 break;
1657 default:
1658 monitor_printf(mon, "Error %d while activating snapshot on"
1659 " '%s'\n", ret, bdrv_get_device_name(bs1));
1660 break;
1661 }
1662 /* fatal on snapshot block device */
1663 if (bs == bs1)
1664 return 0;
1665 }
1666 }
1667 }
1668
1669 /* Don't even try to load empty VM states */
1670 ret = bdrv_snapshot_find(bs, &sn, name);
1671 if ((ret >= 0) && (sn.vm_state_size == 0))
1672 return -EINVAL;
1673
1674 /* restore the VM state */
1675 f = qemu_fopen_bdrv(bs, 0);
1676 if (!f) {
1677 monitor_printf(mon, "Could not open VM state file\n");
1678 return -EINVAL;
1679 }
1680 ret = qemu_loadvm_state(f);
1681 qemu_fclose(f);
1682 if (ret < 0) {
1683 monitor_printf(mon, "Error %d while loading VM state\n", ret);
1684 return ret;
1685 }
1686 return 0;
1687 }
1688
1689 void do_delvm(Monitor *mon, const QDict *qdict)
1690 {
1691 DriveInfo *dinfo;
1692 BlockDriverState *bs, *bs1;
1693 int ret;
1694 const char *name = qdict_get_str(qdict, "name");
1695
1696 bs = get_bs_snapshots();
1697 if (!bs) {
1698 monitor_printf(mon, "No block device supports snapshots\n");
1699 return;
1700 }
1701
1702 QTAILQ_FOREACH(dinfo, &drives, next) {
1703 bs1 = dinfo->bdrv;
1704 if (bdrv_has_snapshot(bs1)) {
1705 ret = bdrv_snapshot_delete(bs1, name);
1706 if (ret < 0) {
1707 if (ret == -ENOTSUP)
1708 monitor_printf(mon,
1709 "Snapshots not supported on device '%s'\n",
1710 bdrv_get_device_name(bs1));
1711 else
1712 monitor_printf(mon, "Error %d while deleting snapshot on "
1713 "'%s'\n", ret, bdrv_get_device_name(bs1));
1714 }
1715 }
1716 }
1717 }
1718
1719 void do_info_snapshots(Monitor *mon)
1720 {
1721 DriveInfo *dinfo;
1722 BlockDriverState *bs, *bs1;
1723 QEMUSnapshotInfo *sn_tab, *sn;
1724 int nb_sns, i;
1725 char buf[256];
1726
1727 bs = get_bs_snapshots();
1728 if (!bs) {
1729 monitor_printf(mon, "No available block device supports snapshots\n");
1730 return;
1731 }
1732 monitor_printf(mon, "Snapshot devices:");
1733 QTAILQ_FOREACH(dinfo, &drives, next) {
1734 bs1 = dinfo->bdrv;
1735 if (bdrv_has_snapshot(bs1)) {
1736 if (bs == bs1)
1737 monitor_printf(mon, " %s", bdrv_get_device_name(bs1));
1738 }
1739 }
1740 monitor_printf(mon, "\n");
1741
1742 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
1743 if (nb_sns < 0) {
1744 monitor_printf(mon, "bdrv_snapshot_list: error %d\n", nb_sns);
1745 return;
1746 }
1747 monitor_printf(mon, "Snapshot list (from %s):\n",
1748 bdrv_get_device_name(bs));
1749 monitor_printf(mon, "%s\n", bdrv_snapshot_dump(buf, sizeof(buf), NULL));
1750 for(i = 0; i < nb_sns; i++) {
1751 sn = &sn_tab[i];
1752 monitor_printf(mon, "%s\n", bdrv_snapshot_dump(buf, sizeof(buf), sn));
1753 }
1754 qemu_free(sn_tab);
1755 }