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