Fix prototype of function zfree.
[qemu-kvm/fedora.git] / savevm.c
blob54137f87f4a5c74fad89ab9c3b22f73cb61b84a1
1 /*
2 * QEMU System Emulator
4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
24 #include "qemu-common.h"
25 #include "hw/hw.h"
26 #include "net.h"
27 #include "console.h"
28 #include "sysemu.h"
29 #include "qemu-timer.h"
30 #include "qemu-char.h"
31 #include "block.h"
32 #include "audio/audio.h"
33 #include "migration.h"
34 #include "qemu_socket.h"
36 #include <unistd.h>
37 #include <fcntl.h>
38 #include <signal.h>
39 #include <time.h>
40 #include <errno.h>
41 #include <sys/time.h>
42 #include <zlib.h>
44 #ifndef _WIN32
45 #include <sys/times.h>
46 #include <sys/wait.h>
47 #include <termios.h>
48 #include <sys/mman.h>
49 #include <sys/ioctl.h>
50 #include <sys/resource.h>
51 #include <sys/socket.h>
52 #include <netinet/in.h>
53 #include <net/if.h>
54 #if defined(__NetBSD__)
55 #include <net/if_tap.h>
56 #endif
57 #ifdef __linux__
58 #include <linux/if_tun.h>
59 #endif
60 #include <arpa/inet.h>
61 #include <dirent.h>
62 #include <netdb.h>
63 #include <sys/select.h>
64 #ifdef _BSD
65 #include <sys/stat.h>
66 #ifdef __FreeBSD__
67 #include <libutil.h>
68 #else
69 #include <util.h>
70 #endif
71 #elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
72 #include <freebsd/stdlib.h>
73 #else
74 #ifdef __linux__
75 #include <pty.h>
76 #include <malloc.h>
77 #include <linux/rtc.h>
78 #endif
79 #endif
80 #endif
82 #ifdef _WIN32
83 #include <malloc.h>
84 #include <sys/timeb.h>
85 #include <mmsystem.h>
86 #define getopt_long_only getopt_long
87 #define memalign(align, size) malloc(size)
88 #endif
90 /* point to the block driver where the snapshots are managed */
91 static BlockDriverState *bs_snapshots;
93 #define SELF_ANNOUNCE_ROUNDS 5
94 #define ETH_P_EXPERIMENTAL 0x01F1 /* just a number */
95 //#define ETH_P_EXPERIMENTAL 0x0012 /* make it the size of the packet */
96 #define EXPERIMENTAL_MAGIC 0xf1f23f4f
98 static int announce_self_create(uint8_t *buf,
99 uint8_t *mac_addr)
101 uint32_t magic = EXPERIMENTAL_MAGIC;
102 uint16_t proto = htons(ETH_P_EXPERIMENTAL);
104 /* FIXME: should we send a different packet (arp/rarp/ping)? */
106 memset(buf, 0, 64);
107 memset(buf, 0xff, 6); /* h_dst */
108 memcpy(buf + 6, mac_addr, 6); /* h_src */
109 memcpy(buf + 12, &proto, 2); /* h_proto */
110 memcpy(buf + 14, &magic, 4); /* magic */
112 return 64; /* len */
115 static void qemu_announce_self_once(void *opaque)
117 int i, len;
118 VLANState *vlan;
119 VLANClientState *vc;
120 uint8_t buf[256];
121 static int count = SELF_ANNOUNCE_ROUNDS;
122 QEMUTimer *timer = *(QEMUTimer **)opaque;
124 for (i = 0; i < MAX_NICS; i++) {
125 if (!nd_table[i].used)
126 continue;
127 len = announce_self_create(buf, nd_table[i].macaddr);
128 vlan = nd_table[i].vlan;
129 for(vc = vlan->first_client; vc != NULL; vc = vc->next) {
130 vc->fd_read(vc->opaque, buf, len);
133 if (count--) {
134 qemu_mod_timer(timer, qemu_get_clock(rt_clock) + 100);
135 } else {
136 qemu_del_timer(timer);
137 qemu_free_timer(timer);
141 void qemu_announce_self(void)
143 static QEMUTimer *timer;
144 timer = qemu_new_timer(rt_clock, qemu_announce_self_once, &timer);
145 qemu_announce_self_once(&timer);
148 /***********************************************************/
149 /* savevm/loadvm support */
151 #define IO_BUF_SIZE 32768
153 struct QEMUFile {
154 QEMUFilePutBufferFunc *put_buffer;
155 QEMUFileGetBufferFunc *get_buffer;
156 QEMUFileCloseFunc *close;
157 QEMUFileRateLimit *rate_limit;
158 void *opaque;
159 int is_write;
161 int64_t buf_offset; /* start of buffer when writing, end of buffer
162 when reading */
163 int buf_index;
164 int buf_size; /* 0 when writing */
165 uint8_t buf[IO_BUF_SIZE];
167 int has_error;
170 typedef struct QEMUFilePopen
172 FILE *popen_file;
173 QEMUFile *file;
174 } QEMUFilePopen;
176 typedef struct QEMUFileSocket
178 int fd;
179 QEMUFile *file;
180 } QEMUFileSocket;
182 static int socket_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
184 QEMUFileSocket *s = opaque;
185 ssize_t len;
187 do {
188 len = recv(s->fd, buf, size, 0);
189 } while (len == -1 && socket_error() == EINTR);
191 if (len == -1)
192 len = -socket_error();
194 return len;
197 static int socket_close(void *opaque)
199 QEMUFileSocket *s = opaque;
200 qemu_free(s);
201 return 0;
204 static int popen_put_buffer(void *opaque, const uint8_t *buf, int64_t pos, int size)
206 QEMUFilePopen *s = opaque;
207 return fwrite(buf, 1, size, s->popen_file);
210 static int popen_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
212 QEMUFilePopen *s = opaque;
213 return fread(buf, 1, size, s->popen_file);
216 static int popen_close(void *opaque)
218 QEMUFilePopen *s = opaque;
219 pclose(s->popen_file);
220 qemu_free(s);
221 return 0;
224 QEMUFile *qemu_popen(FILE *popen_file, const char *mode)
226 QEMUFilePopen *s;
228 if (popen_file == NULL || mode == NULL || (mode[0] != 'r' && mode[0] != 'w') || mode[1] != 0) {
229 fprintf(stderr, "qemu_popen: Argument validity check failed\n");
230 return NULL;
233 s = qemu_mallocz(sizeof(QEMUFilePopen));
235 s->popen_file = popen_file;
237 if(mode[0] == 'r') {
238 s->file = qemu_fopen_ops(s, NULL, popen_get_buffer, popen_close, NULL);
239 } else {
240 s->file = qemu_fopen_ops(s, popen_put_buffer, NULL, popen_close, NULL);
242 return s->file;
245 QEMUFile *qemu_popen_cmd(const char *command, const char *mode)
247 FILE *popen_file;
249 popen_file = popen(command, mode);
250 if(popen_file == NULL) {
251 return NULL;
254 return qemu_popen(popen_file, mode);
257 int qemu_popen_fd(QEMUFile *f)
259 QEMUFilePopen *p;
260 int fd;
262 p = (QEMUFilePopen *)f->opaque;
263 fd = fileno(p->popen_file);
265 return fd;
268 QEMUFile *qemu_fopen_socket(int fd)
270 QEMUFileSocket *s = qemu_mallocz(sizeof(QEMUFileSocket));
272 s->fd = fd;
273 s->file = qemu_fopen_ops(s, NULL, socket_get_buffer, socket_close, NULL);
274 return s->file;
277 typedef struct QEMUFileStdio
279 FILE *outfile;
280 } QEMUFileStdio;
282 static int file_put_buffer(void *opaque, const uint8_t *buf,
283 int64_t pos, int size)
285 QEMUFileStdio *s = opaque;
286 fseek(s->outfile, pos, SEEK_SET);
287 fwrite(buf, 1, size, s->outfile);
288 return size;
291 static int file_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
293 QEMUFileStdio *s = opaque;
294 fseek(s->outfile, pos, SEEK_SET);
295 return fread(buf, 1, size, s->outfile);
298 static int file_close(void *opaque)
300 QEMUFileStdio *s = opaque;
301 fclose(s->outfile);
302 qemu_free(s);
303 return 0;
306 QEMUFile *qemu_fopen(const char *filename, const char *mode)
308 QEMUFileStdio *s;
310 s = qemu_mallocz(sizeof(QEMUFileStdio));
312 s->outfile = fopen(filename, mode);
313 if (!s->outfile)
314 goto fail;
316 if (!strcmp(mode, "wb"))
317 return qemu_fopen_ops(s, file_put_buffer, NULL, file_close, NULL);
318 else if (!strcmp(mode, "rb"))
319 return qemu_fopen_ops(s, NULL, file_get_buffer, file_close, NULL);
321 fail:
322 if (s->outfile)
323 fclose(s->outfile);
324 qemu_free(s);
325 return NULL;
328 typedef struct QEMUFileBdrv
330 BlockDriverState *bs;
331 int64_t base_offset;
332 } QEMUFileBdrv;
334 static int block_put_buffer(void *opaque, const uint8_t *buf,
335 int64_t pos, int size)
337 QEMUFileBdrv *s = opaque;
338 bdrv_put_buffer(s->bs, buf, s->base_offset + pos, size);
339 return size;
342 static int block_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
344 QEMUFileBdrv *s = opaque;
345 return bdrv_get_buffer(s->bs, buf, s->base_offset + pos, size);
348 static int bdrv_fclose(void *opaque)
350 QEMUFileBdrv *s = opaque;
351 qemu_free(s);
352 return 0;
355 static QEMUFile *qemu_fopen_bdrv(BlockDriverState *bs, int64_t offset, int is_writable)
357 QEMUFileBdrv *s;
359 s = qemu_mallocz(sizeof(QEMUFileBdrv));
361 s->bs = bs;
362 s->base_offset = offset;
364 if (is_writable)
365 return qemu_fopen_ops(s, block_put_buffer, NULL, bdrv_fclose, NULL);
367 return qemu_fopen_ops(s, NULL, block_get_buffer, bdrv_fclose, NULL);
370 QEMUFile *qemu_fopen_ops(void *opaque, QEMUFilePutBufferFunc *put_buffer,
371 QEMUFileGetBufferFunc *get_buffer,
372 QEMUFileCloseFunc *close,
373 QEMUFileRateLimit *rate_limit)
375 QEMUFile *f;
377 f = qemu_mallocz(sizeof(QEMUFile));
379 f->opaque = opaque;
380 f->put_buffer = put_buffer;
381 f->get_buffer = get_buffer;
382 f->close = close;
383 f->rate_limit = rate_limit;
384 f->is_write = 0;
386 return f;
389 int qemu_file_has_error(QEMUFile *f)
391 return f->has_error;
394 void qemu_file_set_error(QEMUFile *f)
396 f->has_error = 1;
399 void qemu_fflush(QEMUFile *f)
401 if (!f->put_buffer)
402 return;
404 if (f->is_write && f->buf_index > 0) {
405 int len;
407 len = f->put_buffer(f->opaque, f->buf, f->buf_offset, f->buf_index);
408 if (len > 0)
409 f->buf_offset += f->buf_index;
410 else
411 f->has_error = 1;
412 f->buf_index = 0;
416 static void qemu_fill_buffer(QEMUFile *f)
418 int len;
420 if (!f->get_buffer)
421 return;
423 if (f->is_write)
424 abort();
426 len = f->get_buffer(f->opaque, f->buf, f->buf_offset, IO_BUF_SIZE);
427 if (len > 0) {
428 f->buf_index = 0;
429 f->buf_size = len;
430 f->buf_offset += len;
431 } else if (len != -EAGAIN)
432 f->has_error = 1;
435 int qemu_fclose(QEMUFile *f)
437 int ret = 0;
438 qemu_fflush(f);
439 if (f->close)
440 ret = f->close(f->opaque);
441 qemu_free(f);
442 return ret;
445 void qemu_file_put_notify(QEMUFile *f)
447 f->put_buffer(f->opaque, NULL, 0, 0);
450 void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size)
452 int l;
454 if (!f->has_error && f->is_write == 0 && f->buf_index > 0) {
455 fprintf(stderr,
456 "Attempted to write to buffer while read buffer is not empty\n");
457 abort();
460 while (!f->has_error && size > 0) {
461 l = IO_BUF_SIZE - f->buf_index;
462 if (l > size)
463 l = size;
464 memcpy(f->buf + f->buf_index, buf, l);
465 f->is_write = 1;
466 f->buf_index += l;
467 buf += l;
468 size -= l;
469 if (f->buf_index >= IO_BUF_SIZE)
470 qemu_fflush(f);
474 void qemu_put_byte(QEMUFile *f, int v)
476 if (!f->has_error && f->is_write == 0 && f->buf_index > 0) {
477 fprintf(stderr,
478 "Attempted to write to buffer while read buffer is not empty\n");
479 abort();
482 f->buf[f->buf_index++] = v;
483 f->is_write = 1;
484 if (f->buf_index >= IO_BUF_SIZE)
485 qemu_fflush(f);
488 int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size1)
490 int size, l;
492 if (f->is_write)
493 abort();
495 size = size1;
496 while (size > 0) {
497 l = f->buf_size - f->buf_index;
498 if (l == 0) {
499 qemu_fill_buffer(f);
500 l = f->buf_size - f->buf_index;
501 if (l == 0)
502 break;
504 if (l > size)
505 l = size;
506 memcpy(buf, f->buf + f->buf_index, l);
507 f->buf_index += l;
508 buf += l;
509 size -= l;
511 return size1 - size;
514 int qemu_get_byte(QEMUFile *f)
516 if (f->is_write)
517 abort();
519 if (f->buf_index >= f->buf_size) {
520 qemu_fill_buffer(f);
521 if (f->buf_index >= f->buf_size)
522 return 0;
524 return f->buf[f->buf_index++];
527 int64_t qemu_ftell(QEMUFile *f)
529 return f->buf_offset - f->buf_size + f->buf_index;
532 int64_t qemu_fseek(QEMUFile *f, int64_t pos, int whence)
534 if (whence == SEEK_SET) {
535 /* nothing to do */
536 } else if (whence == SEEK_CUR) {
537 pos += qemu_ftell(f);
538 } else {
539 /* SEEK_END not supported */
540 return -1;
542 if (f->put_buffer) {
543 qemu_fflush(f);
544 f->buf_offset = pos;
545 } else {
546 f->buf_offset = pos;
547 f->buf_index = 0;
548 f->buf_size = 0;
550 return pos;
553 int qemu_file_rate_limit(QEMUFile *f)
555 if (f->rate_limit)
556 return f->rate_limit(f->opaque);
558 return 0;
561 void qemu_put_be16(QEMUFile *f, unsigned int v)
563 qemu_put_byte(f, v >> 8);
564 qemu_put_byte(f, v);
567 void qemu_put_be32(QEMUFile *f, unsigned int v)
569 qemu_put_byte(f, v >> 24);
570 qemu_put_byte(f, v >> 16);
571 qemu_put_byte(f, v >> 8);
572 qemu_put_byte(f, v);
575 void qemu_put_be64(QEMUFile *f, uint64_t v)
577 qemu_put_be32(f, v >> 32);
578 qemu_put_be32(f, v);
581 unsigned int qemu_get_be16(QEMUFile *f)
583 unsigned int v;
584 v = qemu_get_byte(f) << 8;
585 v |= qemu_get_byte(f);
586 return v;
589 unsigned int qemu_get_be32(QEMUFile *f)
591 unsigned int v;
592 v = qemu_get_byte(f) << 24;
593 v |= qemu_get_byte(f) << 16;
594 v |= qemu_get_byte(f) << 8;
595 v |= qemu_get_byte(f);
596 return v;
599 uint64_t qemu_get_be64(QEMUFile *f)
601 uint64_t v;
602 v = (uint64_t)qemu_get_be32(f) << 32;
603 v |= qemu_get_be32(f);
604 return v;
607 typedef struct SaveStateEntry {
608 char idstr[256];
609 int instance_id;
610 int version_id;
611 int section_id;
612 SaveLiveStateHandler *save_live_state;
613 SaveStateHandler *save_state;
614 LoadStateHandler *load_state;
615 void *opaque;
616 struct SaveStateEntry *next;
617 } SaveStateEntry;
619 static SaveStateEntry *first_se;
621 /* TODO: Individual devices generally have very little idea about the rest
622 of the system, so instance_id should be removed/replaced.
623 Meanwhile pass -1 as instance_id if you do not already have a clearly
624 distinguishing id for all instances of your device class. */
625 int register_savevm_live(const char *idstr,
626 int instance_id,
627 int version_id,
628 SaveLiveStateHandler *save_live_state,
629 SaveStateHandler *save_state,
630 LoadStateHandler *load_state,
631 void *opaque)
633 SaveStateEntry *se, **pse;
634 static int global_section_id;
636 se = qemu_malloc(sizeof(SaveStateEntry));
637 pstrcpy(se->idstr, sizeof(se->idstr), idstr);
638 se->instance_id = (instance_id == -1) ? 0 : instance_id;
639 se->version_id = version_id;
640 se->section_id = global_section_id++;
641 se->save_live_state = save_live_state;
642 se->save_state = save_state;
643 se->load_state = load_state;
644 se->opaque = opaque;
645 se->next = NULL;
647 /* add at the end of list */
648 pse = &first_se;
649 while (*pse != NULL) {
650 if (instance_id == -1
651 && strcmp(se->idstr, (*pse)->idstr) == 0
652 && se->instance_id <= (*pse)->instance_id)
653 se->instance_id = (*pse)->instance_id + 1;
654 pse = &(*pse)->next;
656 *pse = se;
657 return 0;
660 int register_savevm(const char *idstr,
661 int instance_id,
662 int version_id,
663 SaveStateHandler *save_state,
664 LoadStateHandler *load_state,
665 void *opaque)
667 return register_savevm_live(idstr, instance_id, version_id,
668 NULL, save_state, load_state, opaque);
671 void unregister_savevm(const char *idstr, void *opaque)
673 SaveStateEntry **pse;
675 pse = &first_se;
676 while (*pse != NULL) {
677 if (strcmp((*pse)->idstr, idstr) == 0 && (*pse)->opaque == opaque) {
678 SaveStateEntry *next = (*pse)->next;
679 qemu_free(*pse);
680 *pse = next;
681 continue;
683 pse = &(*pse)->next;
687 #define QEMU_VM_FILE_MAGIC 0x5145564d
688 #define QEMU_VM_FILE_VERSION_COMPAT 0x00000002
689 #define QEMU_VM_FILE_VERSION 0x00000003
691 #define QEMU_VM_EOF 0x00
692 #define QEMU_VM_SECTION_START 0x01
693 #define QEMU_VM_SECTION_PART 0x02
694 #define QEMU_VM_SECTION_END 0x03
695 #define QEMU_VM_SECTION_FULL 0x04
697 int qemu_savevm_state_begin(QEMUFile *f)
699 SaveStateEntry *se;
701 qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
702 qemu_put_be32(f, QEMU_VM_FILE_VERSION);
704 for (se = first_se; se != NULL; se = se->next) {
705 int len;
707 if (se->save_live_state == NULL)
708 continue;
710 /* Section type */
711 qemu_put_byte(f, QEMU_VM_SECTION_START);
712 qemu_put_be32(f, se->section_id);
714 /* ID string */
715 len = strlen(se->idstr);
716 qemu_put_byte(f, len);
717 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
719 qemu_put_be32(f, se->instance_id);
720 qemu_put_be32(f, se->version_id);
722 se->save_live_state(f, QEMU_VM_SECTION_START, se->opaque);
725 if (qemu_file_has_error(f))
726 return -EIO;
728 return 0;
731 int qemu_savevm_state_iterate(QEMUFile *f)
733 SaveStateEntry *se;
734 int ret = 1;
736 for (se = first_se; se != NULL; se = se->next) {
737 if (se->save_live_state == NULL)
738 continue;
740 /* Section type */
741 qemu_put_byte(f, QEMU_VM_SECTION_PART);
742 qemu_put_be32(f, se->section_id);
744 ret &= !!se->save_live_state(f, QEMU_VM_SECTION_PART, se->opaque);
747 if (ret)
748 return 1;
750 if (qemu_file_has_error(f))
751 return -EIO;
753 return 0;
756 int qemu_savevm_state_complete(QEMUFile *f)
758 SaveStateEntry *se;
760 for (se = first_se; se != NULL; se = se->next) {
761 if (se->save_live_state == NULL)
762 continue;
764 /* Section type */
765 qemu_put_byte(f, QEMU_VM_SECTION_END);
766 qemu_put_be32(f, se->section_id);
768 se->save_live_state(f, QEMU_VM_SECTION_END, se->opaque);
771 for(se = first_se; se != NULL; se = se->next) {
772 int len;
774 if (se->save_state == NULL)
775 continue;
777 /* Section type */
778 qemu_put_byte(f, QEMU_VM_SECTION_FULL);
779 qemu_put_be32(f, se->section_id);
781 /* ID string */
782 len = strlen(se->idstr);
783 qemu_put_byte(f, len);
784 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
786 qemu_put_be32(f, se->instance_id);
787 qemu_put_be32(f, se->version_id);
789 se->save_state(f, se->opaque);
792 qemu_put_byte(f, QEMU_VM_EOF);
794 if (qemu_file_has_error(f))
795 return -EIO;
797 return 0;
800 int qemu_savevm_state(QEMUFile *f)
802 int saved_vm_running;
803 int ret;
805 saved_vm_running = vm_running;
806 vm_stop(0);
808 bdrv_flush_all();
810 ret = qemu_savevm_state_begin(f);
811 if (ret < 0)
812 goto out;
814 do {
815 ret = qemu_savevm_state_iterate(f);
816 if (ret < 0)
817 goto out;
818 } while (ret == 0);
820 ret = qemu_savevm_state_complete(f);
822 out:
823 if (qemu_file_has_error(f))
824 ret = -EIO;
826 if (!ret && saved_vm_running)
827 vm_start();
829 return ret;
832 static SaveStateEntry *find_se(const char *idstr, int instance_id)
834 SaveStateEntry *se;
836 for(se = first_se; se != NULL; se = se->next) {
837 if (!strcmp(se->idstr, idstr) &&
838 instance_id == se->instance_id)
839 return se;
841 return NULL;
844 typedef struct LoadStateEntry {
845 SaveStateEntry *se;
846 int section_id;
847 int version_id;
848 struct LoadStateEntry *next;
849 } LoadStateEntry;
851 static int qemu_loadvm_state_v2(QEMUFile *f)
853 SaveStateEntry *se;
854 int len, ret, instance_id, record_len, version_id;
855 int64_t total_len, end_pos, cur_pos;
856 char idstr[256];
858 total_len = qemu_get_be64(f);
859 end_pos = total_len + qemu_ftell(f);
860 for(;;) {
861 if (qemu_ftell(f) >= end_pos)
862 break;
863 len = qemu_get_byte(f);
864 qemu_get_buffer(f, (uint8_t *)idstr, len);
865 idstr[len] = '\0';
866 instance_id = qemu_get_be32(f);
867 version_id = qemu_get_be32(f);
868 record_len = qemu_get_be32(f);
869 cur_pos = qemu_ftell(f);
870 se = find_se(idstr, instance_id);
871 if (!se) {
872 fprintf(stderr, "qemu: warning: instance 0x%x of device '%s' not present in current VM\n",
873 instance_id, idstr);
874 } else {
875 ret = se->load_state(f, se->opaque, version_id);
876 if (ret < 0) {
877 fprintf(stderr, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
878 instance_id, idstr);
879 return ret;
882 /* always seek to exact end of record */
883 qemu_fseek(f, cur_pos + record_len, SEEK_SET);
886 if (qemu_file_has_error(f))
887 return -EIO;
889 return 0;
892 int qemu_loadvm_state(QEMUFile *f)
894 LoadStateEntry *first_le = NULL;
895 uint8_t section_type;
896 unsigned int v;
897 int ret;
899 v = qemu_get_be32(f);
900 if (v != QEMU_VM_FILE_MAGIC)
901 return -EINVAL;
903 v = qemu_get_be32(f);
904 if (v == QEMU_VM_FILE_VERSION_COMPAT)
905 return qemu_loadvm_state_v2(f);
906 if (v != QEMU_VM_FILE_VERSION)
907 return -ENOTSUP;
909 while ((section_type = qemu_get_byte(f)) != QEMU_VM_EOF) {
910 uint32_t instance_id, version_id, section_id;
911 LoadStateEntry *le;
912 SaveStateEntry *se;
913 char idstr[257];
914 int len;
916 switch (section_type) {
917 case QEMU_VM_SECTION_START:
918 case QEMU_VM_SECTION_FULL:
919 /* Read section start */
920 section_id = qemu_get_be32(f);
921 len = qemu_get_byte(f);
922 qemu_get_buffer(f, (uint8_t *)idstr, len);
923 idstr[len] = 0;
924 instance_id = qemu_get_be32(f);
925 version_id = qemu_get_be32(f);
927 /* Find savevm section */
928 se = find_se(idstr, instance_id);
929 if (se == NULL) {
930 fprintf(stderr, "Unknown savevm section or instance '%s' %d\n", idstr, instance_id);
931 ret = -EINVAL;
932 goto out;
935 /* Validate version */
936 if (version_id > se->version_id) {
937 fprintf(stderr, "savevm: unsupported version %d for '%s' v%d\n",
938 version_id, idstr, se->version_id);
939 ret = -EINVAL;
940 goto out;
943 /* Add entry */
944 le = qemu_mallocz(sizeof(*le));
946 le->se = se;
947 le->section_id = section_id;
948 le->version_id = version_id;
949 le->next = first_le;
950 first_le = le;
952 le->se->load_state(f, le->se->opaque, le->version_id);
953 break;
954 case QEMU_VM_SECTION_PART:
955 case QEMU_VM_SECTION_END:
956 section_id = qemu_get_be32(f);
958 for (le = first_le; le && le->section_id != section_id; le = le->next);
959 if (le == NULL) {
960 fprintf(stderr, "Unknown savevm section %d\n", section_id);
961 ret = -EINVAL;
962 goto out;
965 le->se->load_state(f, le->se->opaque, le->version_id);
966 break;
967 default:
968 fprintf(stderr, "Unknown savevm section type %d\n", section_type);
969 ret = -EINVAL;
970 goto out;
974 ret = 0;
976 out:
977 while (first_le) {
978 LoadStateEntry *le = first_le;
979 first_le = first_le->next;
980 qemu_free(le);
983 if (qemu_file_has_error(f))
984 ret = -EIO;
986 return ret;
989 /* device can contain snapshots */
990 static int bdrv_can_snapshot(BlockDriverState *bs)
992 return (bs &&
993 !bdrv_is_removable(bs) &&
994 !bdrv_is_read_only(bs));
997 /* device must be snapshots in order to have a reliable snapshot */
998 static int bdrv_has_snapshot(BlockDriverState *bs)
1000 return (bs &&
1001 !bdrv_is_removable(bs) &&
1002 !bdrv_is_read_only(bs));
1005 static BlockDriverState *get_bs_snapshots(void)
1007 BlockDriverState *bs;
1008 int i;
1010 if (bs_snapshots)
1011 return bs_snapshots;
1012 for(i = 0; i <= nb_drives; i++) {
1013 bs = drives_table[i].bdrv;
1014 if (bdrv_can_snapshot(bs))
1015 goto ok;
1017 return NULL;
1019 bs_snapshots = bs;
1020 return bs;
1023 static int bdrv_snapshot_find(BlockDriverState *bs, QEMUSnapshotInfo *sn_info,
1024 const char *name)
1026 QEMUSnapshotInfo *sn_tab, *sn;
1027 int nb_sns, i, ret;
1029 ret = -ENOENT;
1030 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
1031 if (nb_sns < 0)
1032 return ret;
1033 for(i = 0; i < nb_sns; i++) {
1034 sn = &sn_tab[i];
1035 if (!strcmp(sn->id_str, name) || !strcmp(sn->name, name)) {
1036 *sn_info = *sn;
1037 ret = 0;
1038 break;
1041 qemu_free(sn_tab);
1042 return ret;
1045 void do_savevm(const char *name)
1047 BlockDriverState *bs, *bs1;
1048 QEMUSnapshotInfo sn1, *sn = &sn1, old_sn1, *old_sn = &old_sn1;
1049 int must_delete, ret, i;
1050 BlockDriverInfo bdi1, *bdi = &bdi1;
1051 QEMUFile *f;
1052 int saved_vm_running;
1053 uint32_t vm_state_size;
1054 #ifdef _WIN32
1055 struct _timeb tb;
1056 #else
1057 struct timeval tv;
1058 #endif
1060 bs = get_bs_snapshots();
1061 if (!bs) {
1062 term_printf("No block device can accept snapshots\n");
1063 return;
1066 /* ??? Should this occur after vm_stop? */
1067 qemu_aio_flush();
1069 saved_vm_running = vm_running;
1070 vm_stop(0);
1072 must_delete = 0;
1073 if (name) {
1074 ret = bdrv_snapshot_find(bs, old_sn, name);
1075 if (ret >= 0) {
1076 must_delete = 1;
1079 memset(sn, 0, sizeof(*sn));
1080 if (must_delete) {
1081 pstrcpy(sn->name, sizeof(sn->name), old_sn->name);
1082 pstrcpy(sn->id_str, sizeof(sn->id_str), old_sn->id_str);
1083 } else {
1084 if (name)
1085 pstrcpy(sn->name, sizeof(sn->name), name);
1088 /* fill auxiliary fields */
1089 #ifdef _WIN32
1090 _ftime(&tb);
1091 sn->date_sec = tb.time;
1092 sn->date_nsec = tb.millitm * 1000000;
1093 #else
1094 gettimeofday(&tv, NULL);
1095 sn->date_sec = tv.tv_sec;
1096 sn->date_nsec = tv.tv_usec * 1000;
1097 #endif
1098 sn->vm_clock_nsec = qemu_get_clock(vm_clock);
1100 if (bdrv_get_info(bs, bdi) < 0 || bdi->vm_state_offset <= 0) {
1101 term_printf("Device %s does not support VM state snapshots\n",
1102 bdrv_get_device_name(bs));
1103 goto the_end;
1106 /* save the VM state */
1107 f = qemu_fopen_bdrv(bs, bdi->vm_state_offset, 1);
1108 if (!f) {
1109 term_printf("Could not open VM state file\n");
1110 goto the_end;
1112 ret = qemu_savevm_state(f);
1113 vm_state_size = qemu_ftell(f);
1114 qemu_fclose(f);
1115 if (ret < 0) {
1116 term_printf("Error %d while writing VM\n", ret);
1117 goto the_end;
1120 /* create the snapshots */
1122 for(i = 0; i < nb_drives; i++) {
1123 bs1 = drives_table[i].bdrv;
1124 if (bdrv_has_snapshot(bs1)) {
1125 if (must_delete) {
1126 ret = bdrv_snapshot_delete(bs1, old_sn->id_str);
1127 if (ret < 0) {
1128 term_printf("Error while deleting snapshot on '%s'\n",
1129 bdrv_get_device_name(bs1));
1132 /* Write VM state size only to the image that contains the state */
1133 sn->vm_state_size = (bs == bs1 ? vm_state_size : 0);
1134 ret = bdrv_snapshot_create(bs1, sn);
1135 if (ret < 0) {
1136 term_printf("Error while creating snapshot on '%s'\n",
1137 bdrv_get_device_name(bs1));
1142 the_end:
1143 if (saved_vm_running)
1144 vm_start();
1147 void do_loadvm(const char *name)
1149 BlockDriverState *bs, *bs1;
1150 BlockDriverInfo bdi1, *bdi = &bdi1;
1151 QEMUSnapshotInfo sn;
1152 QEMUFile *f;
1153 int i, ret;
1154 int saved_vm_running;
1156 bs = get_bs_snapshots();
1157 if (!bs) {
1158 term_printf("No block device supports snapshots\n");
1159 return;
1162 /* Flush all IO requests so they don't interfere with the new state. */
1163 qemu_aio_flush();
1165 saved_vm_running = vm_running;
1166 vm_stop(0);
1168 for(i = 0; i <= nb_drives; i++) {
1169 bs1 = drives_table[i].bdrv;
1170 if (bdrv_has_snapshot(bs1)) {
1171 ret = bdrv_snapshot_goto(bs1, name);
1172 if (ret < 0) {
1173 if (bs != bs1)
1174 term_printf("Warning: ");
1175 switch(ret) {
1176 case -ENOTSUP:
1177 term_printf("Snapshots not supported on device '%s'\n",
1178 bdrv_get_device_name(bs1));
1179 break;
1180 case -ENOENT:
1181 term_printf("Could not find snapshot '%s' on device '%s'\n",
1182 name, bdrv_get_device_name(bs1));
1183 break;
1184 default:
1185 term_printf("Error %d while activating snapshot on '%s'\n",
1186 ret, bdrv_get_device_name(bs1));
1187 break;
1189 /* fatal on snapshot block device */
1190 if (bs == bs1)
1191 goto the_end;
1196 if (bdrv_get_info(bs, bdi) < 0 || bdi->vm_state_offset <= 0) {
1197 term_printf("Device %s does not support VM state snapshots\n",
1198 bdrv_get_device_name(bs));
1199 return;
1202 /* Don't even try to load empty VM states */
1203 ret = bdrv_snapshot_find(bs, &sn, name);
1204 if ((ret >= 0) && (sn.vm_state_size == 0))
1205 goto the_end;
1207 /* restore the VM state */
1208 f = qemu_fopen_bdrv(bs, bdi->vm_state_offset, 0);
1209 if (!f) {
1210 term_printf("Could not open VM state file\n");
1211 goto the_end;
1213 ret = qemu_loadvm_state(f);
1214 qemu_fclose(f);
1215 if (ret < 0) {
1216 term_printf("Error %d while loading VM state\n", ret);
1218 the_end:
1219 if (saved_vm_running)
1220 vm_start();
1223 void do_delvm(const char *name)
1225 BlockDriverState *bs, *bs1;
1226 int i, ret;
1228 bs = get_bs_snapshots();
1229 if (!bs) {
1230 term_printf("No block device supports snapshots\n");
1231 return;
1234 for(i = 0; i <= nb_drives; i++) {
1235 bs1 = drives_table[i].bdrv;
1236 if (bdrv_has_snapshot(bs1)) {
1237 ret = bdrv_snapshot_delete(bs1, name);
1238 if (ret < 0) {
1239 if (ret == -ENOTSUP)
1240 term_printf("Snapshots not supported on device '%s'\n",
1241 bdrv_get_device_name(bs1));
1242 else
1243 term_printf("Error %d while deleting snapshot on '%s'\n",
1244 ret, bdrv_get_device_name(bs1));
1250 void do_info_snapshots(void)
1252 BlockDriverState *bs, *bs1;
1253 QEMUSnapshotInfo *sn_tab, *sn;
1254 int nb_sns, i;
1255 char buf[256];
1257 bs = get_bs_snapshots();
1258 if (!bs) {
1259 term_printf("No available block device supports snapshots\n");
1260 return;
1262 term_printf("Snapshot devices:");
1263 for(i = 0; i <= nb_drives; i++) {
1264 bs1 = drives_table[i].bdrv;
1265 if (bdrv_has_snapshot(bs1)) {
1266 if (bs == bs1)
1267 term_printf(" %s", bdrv_get_device_name(bs1));
1270 term_printf("\n");
1272 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
1273 if (nb_sns < 0) {
1274 term_printf("bdrv_snapshot_list: error %d\n", nb_sns);
1275 return;
1277 term_printf("Snapshot list (from %s):\n", bdrv_get_device_name(bs));
1278 term_printf("%s\n", bdrv_snapshot_dump(buf, sizeof(buf), NULL));
1279 for(i = 0; i < nb_sns; i++) {
1280 sn = &sn_tab[i];
1281 term_printf("%s\n", bdrv_snapshot_dump(buf, sizeof(buf), sn));
1283 qemu_free(sn_tab);