qcow2 corruption: Fix alloc_cluster_link_l2 (Kevin Wolf)
[qemu-kvm/fedora.git] / savevm.c
blobe3e853cf2213958a2c5812abafa085853b6f2b92
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, 0xff, 6); /* h_dst */
107 memcpy(buf + 6, mac_addr, 6); /* h_src */
108 memcpy(buf + 12, &proto, 2); /* h_proto */
109 memcpy(buf + 14, &magic, 4); /* magic */
111 return 18; /* len */
114 void qemu_announce_self(void)
116 int i, j, len;
117 VLANState *vlan;
118 VLANClientState *vc;
119 uint8_t buf[256];
121 for (i = 0; i < MAX_NICS; i++) {
122 if (!nd_table[i].used)
123 continue;
124 len = announce_self_create(buf, nd_table[i].macaddr);
125 vlan = nd_table[i].vlan;
126 for(vc = vlan->first_client; vc != NULL; vc = vc->next) {
127 for (j=0; j < SELF_ANNOUNCE_ROUNDS; j++)
128 vc->fd_read(vc->opaque, buf, len);
133 /***********************************************************/
134 /* savevm/loadvm support */
136 #define IO_BUF_SIZE 32768
138 struct QEMUFile {
139 QEMUFilePutBufferFunc *put_buffer;
140 QEMUFileGetBufferFunc *get_buffer;
141 QEMUFileCloseFunc *close;
142 QEMUFileRateLimit *rate_limit;
143 void *opaque;
144 int is_write;
146 int64_t buf_offset; /* start of buffer when writing, end of buffer
147 when reading */
148 int buf_index;
149 int buf_size; /* 0 when writing */
150 uint8_t buf[IO_BUF_SIZE];
152 int has_error;
155 typedef struct QEMUFilePopen
157 FILE *popen_file;
158 QEMUFile *file;
159 } QEMUFilePopen;
161 typedef struct QEMUFileSocket
163 int fd;
164 QEMUFile *file;
165 } QEMUFileSocket;
167 static int socket_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
169 QEMUFileSocket *s = opaque;
170 ssize_t len;
172 do {
173 len = recv(s->fd, buf, size, 0);
174 } while (len == -1 && socket_error() == EINTR);
176 if (len == -1)
177 len = -socket_error();
179 return len;
182 static int socket_close(void *opaque)
184 QEMUFileSocket *s = opaque;
185 qemu_free(s);
186 return 0;
189 static int popen_put_buffer(void *opaque, const uint8_t *buf, int64_t pos, int size)
191 QEMUFilePopen *s = opaque;
192 return fwrite(buf, 1, size, s->popen_file);
195 static int popen_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
197 QEMUFilePopen *s = opaque;
198 return fread(buf, 1, size, s->popen_file);
201 static int popen_close(void *opaque)
203 QEMUFilePopen *s = opaque;
204 pclose(s->popen_file);
205 qemu_free(s);
206 return 0;
209 QEMUFile *qemu_popen(FILE *popen_file, const char *mode)
211 QEMUFilePopen *s;
213 if (popen_file == NULL || mode == NULL || (mode[0] != 'r' && mode[0] != 'w') || mode[1] != 0) {
214 fprintf(stderr, "qemu_popen: Argument validity check failed\n");
215 return NULL;
218 s = qemu_mallocz(sizeof(QEMUFilePopen));
220 s->popen_file = popen_file;
222 if(mode[0] == 'r') {
223 s->file = qemu_fopen_ops(s, NULL, popen_get_buffer, popen_close, NULL);
224 } else {
225 s->file = qemu_fopen_ops(s, popen_put_buffer, NULL, popen_close, NULL);
227 fprintf(stderr, "qemu_popen: returning result of qemu_fopen_ops\n");
228 return s->file;
231 QEMUFile *qemu_popen_cmd(const char *command, const char *mode)
233 FILE *popen_file;
235 popen_file = popen(command, mode);
236 if(popen_file == NULL) {
237 return NULL;
240 return qemu_popen(popen_file, mode);
243 QEMUFile *qemu_fopen_socket(int fd)
245 QEMUFileSocket *s = qemu_mallocz(sizeof(QEMUFileSocket));
247 s->fd = fd;
248 s->file = qemu_fopen_ops(s, NULL, socket_get_buffer, socket_close, NULL);
249 return s->file;
252 typedef struct QEMUFileStdio
254 FILE *outfile;
255 } QEMUFileStdio;
257 static int file_put_buffer(void *opaque, const uint8_t *buf,
258 int64_t pos, int size)
260 QEMUFileStdio *s = opaque;
261 fseek(s->outfile, pos, SEEK_SET);
262 fwrite(buf, 1, size, s->outfile);
263 return size;
266 static int file_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
268 QEMUFileStdio *s = opaque;
269 fseek(s->outfile, pos, SEEK_SET);
270 return fread(buf, 1, size, s->outfile);
273 static int file_close(void *opaque)
275 QEMUFileStdio *s = opaque;
276 fclose(s->outfile);
277 qemu_free(s);
278 return 0;
281 QEMUFile *qemu_fopen(const char *filename, const char *mode)
283 QEMUFileStdio *s;
285 s = qemu_mallocz(sizeof(QEMUFileStdio));
287 s->outfile = fopen(filename, mode);
288 if (!s->outfile)
289 goto fail;
291 if (!strcmp(mode, "wb"))
292 return qemu_fopen_ops(s, file_put_buffer, NULL, file_close, NULL);
293 else if (!strcmp(mode, "rb"))
294 return qemu_fopen_ops(s, NULL, file_get_buffer, file_close, NULL);
296 fail:
297 if (s->outfile)
298 fclose(s->outfile);
299 qemu_free(s);
300 return NULL;
303 typedef struct QEMUFileBdrv
305 BlockDriverState *bs;
306 int64_t base_offset;
307 } QEMUFileBdrv;
309 static int block_put_buffer(void *opaque, const uint8_t *buf,
310 int64_t pos, int size)
312 QEMUFileBdrv *s = opaque;
313 bdrv_put_buffer(s->bs, buf, s->base_offset + pos, size);
314 return size;
317 static int block_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
319 QEMUFileBdrv *s = opaque;
320 return bdrv_get_buffer(s->bs, buf, s->base_offset + pos, size);
323 static int bdrv_fclose(void *opaque)
325 QEMUFileBdrv *s = opaque;
326 qemu_free(s);
327 return 0;
330 static QEMUFile *qemu_fopen_bdrv(BlockDriverState *bs, int64_t offset, int is_writable)
332 QEMUFileBdrv *s;
334 s = qemu_mallocz(sizeof(QEMUFileBdrv));
336 s->bs = bs;
337 s->base_offset = offset;
339 if (is_writable)
340 return qemu_fopen_ops(s, block_put_buffer, NULL, bdrv_fclose, NULL);
342 return qemu_fopen_ops(s, NULL, block_get_buffer, bdrv_fclose, NULL);
345 QEMUFile *qemu_fopen_ops(void *opaque, QEMUFilePutBufferFunc *put_buffer,
346 QEMUFileGetBufferFunc *get_buffer,
347 QEMUFileCloseFunc *close,
348 QEMUFileRateLimit *rate_limit)
350 QEMUFile *f;
352 f = qemu_mallocz(sizeof(QEMUFile));
354 f->opaque = opaque;
355 f->put_buffer = put_buffer;
356 f->get_buffer = get_buffer;
357 f->close = close;
358 f->rate_limit = rate_limit;
359 f->is_write = 0;
361 return f;
364 int qemu_file_has_error(QEMUFile *f)
366 return f->has_error;
369 void qemu_file_set_error(QEMUFile *f)
371 f->has_error = 1;
374 void qemu_fflush(QEMUFile *f)
376 if (!f->put_buffer)
377 return;
379 if (f->is_write && f->buf_index > 0) {
380 int len;
382 len = f->put_buffer(f->opaque, f->buf, f->buf_offset, f->buf_index);
383 if (len > 0)
384 f->buf_offset += f->buf_index;
385 else
386 f->has_error = 1;
387 f->buf_index = 0;
391 static void qemu_fill_buffer(QEMUFile *f)
393 int len;
395 if (!f->get_buffer)
396 return;
398 if (f->is_write)
399 abort();
401 len = f->get_buffer(f->opaque, f->buf, f->buf_offset, IO_BUF_SIZE);
402 if (len > 0) {
403 f->buf_index = 0;
404 f->buf_size = len;
405 f->buf_offset += len;
406 } else if (len != -EAGAIN)
407 f->has_error = 1;
410 int qemu_fclose(QEMUFile *f)
412 int ret = 0;
413 qemu_fflush(f);
414 if (f->close)
415 ret = f->close(f->opaque);
416 qemu_free(f);
417 return ret;
420 void qemu_file_put_notify(QEMUFile *f)
422 f->put_buffer(f->opaque, NULL, 0, 0);
425 void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size)
427 int l;
429 if (!f->has_error && f->is_write == 0 && f->buf_index > 0) {
430 fprintf(stderr,
431 "Attempted to write to buffer while read buffer is not empty\n");
432 abort();
435 while (!f->has_error && size > 0) {
436 l = IO_BUF_SIZE - f->buf_index;
437 if (l > size)
438 l = size;
439 memcpy(f->buf + f->buf_index, buf, l);
440 f->is_write = 1;
441 f->buf_index += l;
442 buf += l;
443 size -= l;
444 if (f->buf_index >= IO_BUF_SIZE)
445 qemu_fflush(f);
449 void qemu_put_byte(QEMUFile *f, int v)
451 if (!f->has_error && f->is_write == 0 && f->buf_index > 0) {
452 fprintf(stderr,
453 "Attempted to write to buffer while read buffer is not empty\n");
454 abort();
457 f->buf[f->buf_index++] = v;
458 f->is_write = 1;
459 if (f->buf_index >= IO_BUF_SIZE)
460 qemu_fflush(f);
463 int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size1)
465 int size, l;
467 if (f->is_write)
468 abort();
470 size = size1;
471 while (size > 0) {
472 l = f->buf_size - f->buf_index;
473 if (l == 0) {
474 qemu_fill_buffer(f);
475 l = f->buf_size - f->buf_index;
476 if (l == 0)
477 break;
479 if (l > size)
480 l = size;
481 memcpy(buf, f->buf + f->buf_index, l);
482 f->buf_index += l;
483 buf += l;
484 size -= l;
486 return size1 - size;
489 int qemu_get_byte(QEMUFile *f)
491 if (f->is_write)
492 abort();
494 if (f->buf_index >= f->buf_size) {
495 qemu_fill_buffer(f);
496 if (f->buf_index >= f->buf_size)
497 return 0;
499 return f->buf[f->buf_index++];
502 int64_t qemu_ftell(QEMUFile *f)
504 return f->buf_offset - f->buf_size + f->buf_index;
507 int64_t qemu_fseek(QEMUFile *f, int64_t pos, int whence)
509 if (whence == SEEK_SET) {
510 /* nothing to do */
511 } else if (whence == SEEK_CUR) {
512 pos += qemu_ftell(f);
513 } else {
514 /* SEEK_END not supported */
515 return -1;
517 if (f->put_buffer) {
518 qemu_fflush(f);
519 f->buf_offset = pos;
520 } else {
521 f->buf_offset = pos;
522 f->buf_index = 0;
523 f->buf_size = 0;
525 return pos;
528 int qemu_file_rate_limit(QEMUFile *f)
530 if (f->rate_limit)
531 return f->rate_limit(f->opaque);
533 return 0;
536 void qemu_put_be16(QEMUFile *f, unsigned int v)
538 qemu_put_byte(f, v >> 8);
539 qemu_put_byte(f, v);
542 void qemu_put_be32(QEMUFile *f, unsigned int v)
544 qemu_put_byte(f, v >> 24);
545 qemu_put_byte(f, v >> 16);
546 qemu_put_byte(f, v >> 8);
547 qemu_put_byte(f, v);
550 void qemu_put_be64(QEMUFile *f, uint64_t v)
552 qemu_put_be32(f, v >> 32);
553 qemu_put_be32(f, v);
556 unsigned int qemu_get_be16(QEMUFile *f)
558 unsigned int v;
559 v = qemu_get_byte(f) << 8;
560 v |= qemu_get_byte(f);
561 return v;
564 unsigned int qemu_get_be32(QEMUFile *f)
566 unsigned int v;
567 v = qemu_get_byte(f) << 24;
568 v |= qemu_get_byte(f) << 16;
569 v |= qemu_get_byte(f) << 8;
570 v |= qemu_get_byte(f);
571 return v;
574 uint64_t qemu_get_be64(QEMUFile *f)
576 uint64_t v;
577 v = (uint64_t)qemu_get_be32(f) << 32;
578 v |= qemu_get_be32(f);
579 return v;
582 typedef struct SaveStateEntry {
583 char idstr[256];
584 int instance_id;
585 int version_id;
586 int section_id;
587 SaveLiveStateHandler *save_live_state;
588 SaveStateHandler *save_state;
589 LoadStateHandler *load_state;
590 void *opaque;
591 struct SaveStateEntry *next;
592 } SaveStateEntry;
594 static SaveStateEntry *first_se;
596 /* TODO: Individual devices generally have very little idea about the rest
597 of the system, so instance_id should be removed/replaced.
598 Meanwhile pass -1 as instance_id if you do not already have a clearly
599 distinguishing id for all instances of your device class. */
600 int register_savevm_live(const char *idstr,
601 int instance_id,
602 int version_id,
603 SaveLiveStateHandler *save_live_state,
604 SaveStateHandler *save_state,
605 LoadStateHandler *load_state,
606 void *opaque)
608 SaveStateEntry *se, **pse;
609 static int global_section_id;
611 se = qemu_malloc(sizeof(SaveStateEntry));
612 pstrcpy(se->idstr, sizeof(se->idstr), idstr);
613 se->instance_id = (instance_id == -1) ? 0 : instance_id;
614 se->version_id = version_id;
615 se->section_id = global_section_id++;
616 se->save_live_state = save_live_state;
617 se->save_state = save_state;
618 se->load_state = load_state;
619 se->opaque = opaque;
620 se->next = NULL;
622 /* add at the end of list */
623 pse = &first_se;
624 while (*pse != NULL) {
625 if (instance_id == -1
626 && strcmp(se->idstr, (*pse)->idstr) == 0
627 && se->instance_id <= (*pse)->instance_id)
628 se->instance_id = (*pse)->instance_id + 1;
629 pse = &(*pse)->next;
631 *pse = se;
632 return 0;
635 int register_savevm(const char *idstr,
636 int instance_id,
637 int version_id,
638 SaveStateHandler *save_state,
639 LoadStateHandler *load_state,
640 void *opaque)
642 return register_savevm_live(idstr, instance_id, version_id,
643 NULL, save_state, load_state, opaque);
646 void unregister_savevm(const char *idstr, void *opaque)
648 SaveStateEntry **pse;
650 pse = &first_se;
651 while (*pse != NULL) {
652 if (strcmp((*pse)->idstr, idstr) == 0 && (*pse)->opaque == opaque) {
653 SaveStateEntry *next = (*pse)->next;
654 qemu_free(*pse);
655 *pse = next;
656 continue;
658 pse = &(*pse)->next;
662 #define QEMU_VM_FILE_MAGIC 0x5145564d
663 #define QEMU_VM_FILE_VERSION_COMPAT 0x00000002
664 #define QEMU_VM_FILE_VERSION 0x00000003
666 #define QEMU_VM_EOF 0x00
667 #define QEMU_VM_SECTION_START 0x01
668 #define QEMU_VM_SECTION_PART 0x02
669 #define QEMU_VM_SECTION_END 0x03
670 #define QEMU_VM_SECTION_FULL 0x04
672 int qemu_savevm_state_begin(QEMUFile *f)
674 SaveStateEntry *se;
676 qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
677 qemu_put_be32(f, QEMU_VM_FILE_VERSION);
679 for (se = first_se; se != NULL; se = se->next) {
680 int len;
682 if (se->save_live_state == NULL)
683 continue;
685 /* Section type */
686 qemu_put_byte(f, QEMU_VM_SECTION_START);
687 qemu_put_be32(f, se->section_id);
689 /* ID string */
690 len = strlen(se->idstr);
691 qemu_put_byte(f, len);
692 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
694 qemu_put_be32(f, se->instance_id);
695 qemu_put_be32(f, se->version_id);
697 se->save_live_state(f, QEMU_VM_SECTION_START, se->opaque);
700 if (qemu_file_has_error(f))
701 return -EIO;
703 return 0;
706 int qemu_savevm_state_iterate(QEMUFile *f)
708 SaveStateEntry *se;
709 int ret = 1;
711 for (se = first_se; se != NULL; se = se->next) {
712 if (se->save_live_state == NULL)
713 continue;
715 /* Section type */
716 qemu_put_byte(f, QEMU_VM_SECTION_PART);
717 qemu_put_be32(f, se->section_id);
719 ret &= !!se->save_live_state(f, QEMU_VM_SECTION_PART, se->opaque);
722 if (ret)
723 return 1;
725 if (qemu_file_has_error(f))
726 return -EIO;
728 return 0;
731 int qemu_savevm_state_complete(QEMUFile *f)
733 SaveStateEntry *se;
735 for (se = first_se; se != NULL; se = se->next) {
736 if (se->save_live_state == NULL)
737 continue;
739 /* Section type */
740 qemu_put_byte(f, QEMU_VM_SECTION_END);
741 qemu_put_be32(f, se->section_id);
743 se->save_live_state(f, QEMU_VM_SECTION_END, se->opaque);
746 for(se = first_se; se != NULL; se = se->next) {
747 int len;
749 if (se->save_state == NULL)
750 continue;
752 /* Section type */
753 qemu_put_byte(f, QEMU_VM_SECTION_FULL);
754 qemu_put_be32(f, se->section_id);
756 /* ID string */
757 len = strlen(se->idstr);
758 qemu_put_byte(f, len);
759 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
761 qemu_put_be32(f, se->instance_id);
762 qemu_put_be32(f, se->version_id);
764 se->save_state(f, se->opaque);
767 qemu_put_byte(f, QEMU_VM_EOF);
769 if (qemu_file_has_error(f))
770 return -EIO;
772 return 0;
775 int qemu_savevm_state(QEMUFile *f)
777 int saved_vm_running;
778 int ret;
780 saved_vm_running = vm_running;
781 vm_stop(0);
783 bdrv_flush_all();
785 ret = qemu_savevm_state_begin(f);
786 if (ret < 0)
787 goto out;
789 do {
790 ret = qemu_savevm_state_iterate(f);
791 if (ret < 0)
792 goto out;
793 } while (ret == 0);
795 ret = qemu_savevm_state_complete(f);
797 out:
798 if (qemu_file_has_error(f))
799 ret = -EIO;
801 if (!ret && saved_vm_running)
802 vm_start();
804 return ret;
807 static SaveStateEntry *find_se(const char *idstr, int instance_id)
809 SaveStateEntry *se;
811 for(se = first_se; se != NULL; se = se->next) {
812 if (!strcmp(se->idstr, idstr) &&
813 instance_id == se->instance_id)
814 return se;
816 return NULL;
819 typedef struct LoadStateEntry {
820 SaveStateEntry *se;
821 int section_id;
822 int version_id;
823 struct LoadStateEntry *next;
824 } LoadStateEntry;
826 static int qemu_loadvm_state_v2(QEMUFile *f)
828 SaveStateEntry *se;
829 int len, ret, instance_id, record_len, version_id;
830 int64_t total_len, end_pos, cur_pos;
831 char idstr[256];
833 total_len = qemu_get_be64(f);
834 end_pos = total_len + qemu_ftell(f);
835 for(;;) {
836 if (qemu_ftell(f) >= end_pos)
837 break;
838 len = qemu_get_byte(f);
839 qemu_get_buffer(f, (uint8_t *)idstr, len);
840 idstr[len] = '\0';
841 instance_id = qemu_get_be32(f);
842 version_id = qemu_get_be32(f);
843 record_len = qemu_get_be32(f);
844 cur_pos = qemu_ftell(f);
845 se = find_se(idstr, instance_id);
846 if (!se) {
847 fprintf(stderr, "qemu: warning: instance 0x%x of device '%s' not present in current VM\n",
848 instance_id, idstr);
849 } else {
850 ret = se->load_state(f, se->opaque, version_id);
851 if (ret < 0) {
852 fprintf(stderr, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
853 instance_id, idstr);
856 /* always seek to exact end of record */
857 qemu_fseek(f, cur_pos + record_len, SEEK_SET);
860 if (qemu_file_has_error(f))
861 return -EIO;
863 return 0;
866 int qemu_loadvm_state(QEMUFile *f)
868 LoadStateEntry *first_le = NULL;
869 uint8_t section_type;
870 unsigned int v;
871 int ret;
873 v = qemu_get_be32(f);
874 if (v != QEMU_VM_FILE_MAGIC)
875 return -EINVAL;
877 v = qemu_get_be32(f);
878 if (v == QEMU_VM_FILE_VERSION_COMPAT)
879 return qemu_loadvm_state_v2(f);
880 if (v != QEMU_VM_FILE_VERSION)
881 return -ENOTSUP;
883 while ((section_type = qemu_get_byte(f)) != QEMU_VM_EOF) {
884 uint32_t instance_id, version_id, section_id;
885 LoadStateEntry *le;
886 SaveStateEntry *se;
887 char idstr[257];
888 int len;
890 switch (section_type) {
891 case QEMU_VM_SECTION_START:
892 case QEMU_VM_SECTION_FULL:
893 /* Read section start */
894 section_id = qemu_get_be32(f);
895 len = qemu_get_byte(f);
896 qemu_get_buffer(f, (uint8_t *)idstr, len);
897 idstr[len] = 0;
898 instance_id = qemu_get_be32(f);
899 version_id = qemu_get_be32(f);
901 /* Find savevm section */
902 se = find_se(idstr, instance_id);
903 if (se == NULL) {
904 fprintf(stderr, "Unknown savevm section or instance '%s' %d\n", idstr, instance_id);
905 ret = -EINVAL;
906 goto out;
909 /* Validate version */
910 if (version_id > se->version_id) {
911 fprintf(stderr, "savevm: unsupported version %d for '%s' v%d\n",
912 version_id, idstr, se->version_id);
913 ret = -EINVAL;
914 goto out;
917 /* Add entry */
918 le = qemu_mallocz(sizeof(*le));
920 le->se = se;
921 le->section_id = section_id;
922 le->version_id = version_id;
923 le->next = first_le;
924 first_le = le;
926 le->se->load_state(f, le->se->opaque, le->version_id);
927 break;
928 case QEMU_VM_SECTION_PART:
929 case QEMU_VM_SECTION_END:
930 section_id = qemu_get_be32(f);
932 for (le = first_le; le && le->section_id != section_id; le = le->next);
933 if (le == NULL) {
934 fprintf(stderr, "Unknown savevm section %d\n", section_id);
935 ret = -EINVAL;
936 goto out;
939 le->se->load_state(f, le->se->opaque, le->version_id);
940 break;
941 default:
942 fprintf(stderr, "Unknown savevm section type %d\n", section_type);
943 ret = -EINVAL;
944 goto out;
948 ret = 0;
950 out:
951 while (first_le) {
952 LoadStateEntry *le = first_le;
953 first_le = first_le->next;
954 qemu_free(le);
957 if (qemu_file_has_error(f))
958 ret = -EIO;
960 return ret;
963 /* device can contain snapshots */
964 static int bdrv_can_snapshot(BlockDriverState *bs)
966 return (bs &&
967 !bdrv_is_removable(bs) &&
968 !bdrv_is_read_only(bs));
971 /* device must be snapshots in order to have a reliable snapshot */
972 static int bdrv_has_snapshot(BlockDriverState *bs)
974 return (bs &&
975 !bdrv_is_removable(bs) &&
976 !bdrv_is_read_only(bs));
979 static BlockDriverState *get_bs_snapshots(void)
981 BlockDriverState *bs;
982 int i;
984 if (bs_snapshots)
985 return bs_snapshots;
986 for(i = 0; i <= nb_drives; i++) {
987 bs = drives_table[i].bdrv;
988 if (bdrv_can_snapshot(bs))
989 goto ok;
991 return NULL;
993 bs_snapshots = bs;
994 return bs;
997 static int bdrv_snapshot_find(BlockDriverState *bs, QEMUSnapshotInfo *sn_info,
998 const char *name)
1000 QEMUSnapshotInfo *sn_tab, *sn;
1001 int nb_sns, i, ret;
1003 ret = -ENOENT;
1004 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
1005 if (nb_sns < 0)
1006 return ret;
1007 for(i = 0; i < nb_sns; i++) {
1008 sn = &sn_tab[i];
1009 if (!strcmp(sn->id_str, name) || !strcmp(sn->name, name)) {
1010 *sn_info = *sn;
1011 ret = 0;
1012 break;
1015 qemu_free(sn_tab);
1016 return ret;
1019 void do_savevm(const char *name)
1021 BlockDriverState *bs, *bs1;
1022 QEMUSnapshotInfo sn1, *sn = &sn1, old_sn1, *old_sn = &old_sn1;
1023 int must_delete, ret, i;
1024 BlockDriverInfo bdi1, *bdi = &bdi1;
1025 QEMUFile *f;
1026 int saved_vm_running;
1027 uint32_t vm_state_size;
1028 #ifdef _WIN32
1029 struct _timeb tb;
1030 #else
1031 struct timeval tv;
1032 #endif
1034 bs = get_bs_snapshots();
1035 if (!bs) {
1036 term_printf("No block device can accept snapshots\n");
1037 return;
1040 /* ??? Should this occur after vm_stop? */
1041 qemu_aio_flush();
1043 saved_vm_running = vm_running;
1044 vm_stop(0);
1046 must_delete = 0;
1047 if (name) {
1048 ret = bdrv_snapshot_find(bs, old_sn, name);
1049 if (ret >= 0) {
1050 must_delete = 1;
1053 memset(sn, 0, sizeof(*sn));
1054 if (must_delete) {
1055 pstrcpy(sn->name, sizeof(sn->name), old_sn->name);
1056 pstrcpy(sn->id_str, sizeof(sn->id_str), old_sn->id_str);
1057 } else {
1058 if (name)
1059 pstrcpy(sn->name, sizeof(sn->name), name);
1062 /* fill auxiliary fields */
1063 #ifdef _WIN32
1064 _ftime(&tb);
1065 sn->date_sec = tb.time;
1066 sn->date_nsec = tb.millitm * 1000000;
1067 #else
1068 gettimeofday(&tv, NULL);
1069 sn->date_sec = tv.tv_sec;
1070 sn->date_nsec = tv.tv_usec * 1000;
1071 #endif
1072 sn->vm_clock_nsec = qemu_get_clock(vm_clock);
1074 if (bdrv_get_info(bs, bdi) < 0 || bdi->vm_state_offset <= 0) {
1075 term_printf("Device %s does not support VM state snapshots\n",
1076 bdrv_get_device_name(bs));
1077 goto the_end;
1080 /* save the VM state */
1081 f = qemu_fopen_bdrv(bs, bdi->vm_state_offset, 1);
1082 if (!f) {
1083 term_printf("Could not open VM state file\n");
1084 goto the_end;
1086 ret = qemu_savevm_state(f);
1087 vm_state_size = qemu_ftell(f);
1088 qemu_fclose(f);
1089 if (ret < 0) {
1090 term_printf("Error %d while writing VM\n", ret);
1091 goto the_end;
1094 /* create the snapshots */
1096 for(i = 0; i < nb_drives; i++) {
1097 bs1 = drives_table[i].bdrv;
1098 if (bdrv_has_snapshot(bs1)) {
1099 if (must_delete) {
1100 ret = bdrv_snapshot_delete(bs1, old_sn->id_str);
1101 if (ret < 0) {
1102 term_printf("Error while deleting snapshot on '%s'\n",
1103 bdrv_get_device_name(bs1));
1106 /* Write VM state size only to the image that contains the state */
1107 sn->vm_state_size = (bs == bs1 ? vm_state_size : 0);
1108 ret = bdrv_snapshot_create(bs1, sn);
1109 if (ret < 0) {
1110 term_printf("Error while creating snapshot on '%s'\n",
1111 bdrv_get_device_name(bs1));
1116 the_end:
1117 if (saved_vm_running)
1118 vm_start();
1121 void do_loadvm(const char *name)
1123 BlockDriverState *bs, *bs1;
1124 BlockDriverInfo bdi1, *bdi = &bdi1;
1125 QEMUSnapshotInfo sn;
1126 QEMUFile *f;
1127 int i, ret;
1128 int saved_vm_running;
1130 bs = get_bs_snapshots();
1131 if (!bs) {
1132 term_printf("No block device supports snapshots\n");
1133 return;
1136 /* Flush all IO requests so they don't interfere with the new state. */
1137 qemu_aio_flush();
1139 saved_vm_running = vm_running;
1140 vm_stop(0);
1142 for(i = 0; i <= nb_drives; i++) {
1143 bs1 = drives_table[i].bdrv;
1144 if (bdrv_has_snapshot(bs1)) {
1145 ret = bdrv_snapshot_goto(bs1, name);
1146 if (ret < 0) {
1147 if (bs != bs1)
1148 term_printf("Warning: ");
1149 switch(ret) {
1150 case -ENOTSUP:
1151 term_printf("Snapshots not supported on device '%s'\n",
1152 bdrv_get_device_name(bs1));
1153 break;
1154 case -ENOENT:
1155 term_printf("Could not find snapshot '%s' on device '%s'\n",
1156 name, bdrv_get_device_name(bs1));
1157 break;
1158 default:
1159 term_printf("Error %d while activating snapshot on '%s'\n",
1160 ret, bdrv_get_device_name(bs1));
1161 break;
1163 /* fatal on snapshot block device */
1164 if (bs == bs1)
1165 goto the_end;
1170 if (bdrv_get_info(bs, bdi) < 0 || bdi->vm_state_offset <= 0) {
1171 term_printf("Device %s does not support VM state snapshots\n",
1172 bdrv_get_device_name(bs));
1173 return;
1176 /* Don't even try to load empty VM states */
1177 ret = bdrv_snapshot_find(bs, &sn, name);
1178 if ((ret >= 0) && (sn.vm_state_size == 0))
1179 goto the_end;
1181 /* restore the VM state */
1182 f = qemu_fopen_bdrv(bs, bdi->vm_state_offset, 0);
1183 if (!f) {
1184 term_printf("Could not open VM state file\n");
1185 goto the_end;
1187 ret = qemu_loadvm_state(f);
1188 qemu_fclose(f);
1189 if (ret < 0) {
1190 term_printf("Error %d while loading VM state\n", ret);
1192 the_end:
1193 if (saved_vm_running)
1194 vm_start();
1197 void do_delvm(const char *name)
1199 BlockDriverState *bs, *bs1;
1200 int i, ret;
1202 bs = get_bs_snapshots();
1203 if (!bs) {
1204 term_printf("No block device supports snapshots\n");
1205 return;
1208 for(i = 0; i <= nb_drives; i++) {
1209 bs1 = drives_table[i].bdrv;
1210 if (bdrv_has_snapshot(bs1)) {
1211 ret = bdrv_snapshot_delete(bs1, name);
1212 if (ret < 0) {
1213 if (ret == -ENOTSUP)
1214 term_printf("Snapshots not supported on device '%s'\n",
1215 bdrv_get_device_name(bs1));
1216 else
1217 term_printf("Error %d while deleting snapshot on '%s'\n",
1218 ret, bdrv_get_device_name(bs1));
1224 void do_info_snapshots(void)
1226 BlockDriverState *bs, *bs1;
1227 QEMUSnapshotInfo *sn_tab, *sn;
1228 int nb_sns, i;
1229 char buf[256];
1231 bs = get_bs_snapshots();
1232 if (!bs) {
1233 term_printf("No available block device supports snapshots\n");
1234 return;
1236 term_printf("Snapshot devices:");
1237 for(i = 0; i <= nb_drives; i++) {
1238 bs1 = drives_table[i].bdrv;
1239 if (bdrv_has_snapshot(bs1)) {
1240 if (bs == bs1)
1241 term_printf(" %s", bdrv_get_device_name(bs1));
1244 term_printf("\n");
1246 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
1247 if (nb_sns < 0) {
1248 term_printf("bdrv_snapshot_list: error %d\n", nb_sns);
1249 return;
1251 term_printf("Snapshot list (from %s):\n", bdrv_get_device_name(bs));
1252 term_printf("%s\n", bdrv_snapshot_dump(buf, sizeof(buf), NULL));
1253 for(i = 0; i < nb_sns; i++) {
1254 sn = &sn_tab[i];
1255 term_printf("%s\n", bdrv_snapshot_dump(buf, sizeof(buf), sn));
1257 qemu_free(sn_tab);