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