blob: 70e8a7d3673f15063ad46628060bfef5e69a28fd [file] [log] [blame]
aliguoria672b462008-11-11 21:33:36 +00001/*
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 */
aliguoria672b462008-11-11 21:33:36 +000024#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
Juan Quintela71e72a12009-07-27 16:12:56 +020032/* Needed early for CONFIG_BSD etc. */
blueswir1d40cdb12009-03-07 16:52:02 +000033#include "config-host.h"
34
aliguoria672b462008-11-11 21:33:36 +000035#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>
Juan Quintela71e72a12009-07-27 16:12:56 +020055#ifdef CONFIG_BSD
aliguoria672b462008-11-11 21:33:36 +000056#include <sys/stat.h>
blueswir1c5e97232009-03-07 20:06:23 +000057#if defined(__FreeBSD__) || defined(__DragonFly__)
aliguoria672b462008-11-11 21:33:36 +000058#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
aliguori49dc7682009-03-08 16:26:59 +000074#include <windows.h>
aliguoria672b462008-11-11 21:33:36 +000075#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
blueswir1511d2b12009-03-07 15:32:56 +000082#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"
Blue Swirl72cf2d42009-09-12 07:36:22 +000093#include "qemu-queue.h"
blueswir1511d2b12009-03-07 15:32:56 +000094
aliguoria672b462008-11-11 21:33:36 +000095/* point to the block driver where the snapshots are managed */
96static BlockDriverState *bs_snapshots;
97
98#define SELF_ANNOUNCE_ROUNDS 5
aliguoria672b462008-11-11 21:33:36 +000099
Nolan18995b92009-10-15 16:53:55 -0700100#ifndef ETH_P_RARP
101#define ETH_P_RARP 0x0835
102#endif
103#define ARP_HTYPE_ETH 0x0001
104#define ARP_PTYPE_IP 0x0800
105#define ARP_OP_REQUEST_REV 0x3
106
107static int announce_self_create(uint8_t *buf,
aliguoria672b462008-11-11 21:33:36 +0000108 uint8_t *mac_addr)
109{
Nolan18995b92009-10-15 16:53:55 -0700110 /* Ethernet header. */
111 memset(buf, 0xff, 6); /* destination MAC addr */
112 memcpy(buf + 6, mac_addr, 6); /* source MAC addr */
113 *(uint16_t *)(buf + 12) = htons(ETH_P_RARP); /* ethertype */
aliguoria672b462008-11-11 21:33:36 +0000114
Nolan18995b92009-10-15 16:53:55 -0700115 /* RARP header. */
116 *(uint16_t *)(buf + 14) = htons(ARP_HTYPE_ETH); /* hardware addr space */
117 *(uint16_t *)(buf + 16) = htons(ARP_PTYPE_IP); /* protocol addr space */
118 *(buf + 18) = 6; /* hardware addr length (ethernet) */
119 *(buf + 19) = 4; /* protocol addr length (IPv4) */
120 *(uint16_t *)(buf + 20) = htons(ARP_OP_REQUEST_REV); /* opcode */
121 memcpy(buf + 22, mac_addr, 6); /* source hw addr */
122 memset(buf + 28, 0x00, 4); /* source protocol addr */
123 memcpy(buf + 32, mac_addr, 6); /* target hw addr */
124 memset(buf + 38, 0x00, 4); /* target protocol addr */
aliguoria672b462008-11-11 21:33:36 +0000125
Nolan18995b92009-10-15 16:53:55 -0700126 /* Padding to get up to 60 bytes (ethernet min packet size, minus FCS). */
127 memset(buf + 42, 0x00, 18);
aliguoria672b462008-11-11 21:33:36 +0000128
Nolan18995b92009-10-15 16:53:55 -0700129 return 60; /* len (FCS will be added by hardware) */
aliguoria672b462008-11-11 21:33:36 +0000130}
131
Gleb Natapoved8b3302009-05-21 17:17:44 +0300132static void qemu_announce_self_once(void *opaque)
aliguoria672b462008-11-11 21:33:36 +0000133{
Gleb Natapoved8b3302009-05-21 17:17:44 +0300134 int i, len;
aliguoria672b462008-11-11 21:33:36 +0000135 VLANState *vlan;
136 VLANClientState *vc;
Nolan18995b92009-10-15 16:53:55 -0700137 uint8_t buf[60];
Gleb Natapoved8b3302009-05-21 17:17:44 +0300138 static int count = SELF_ANNOUNCE_ROUNDS;
139 QEMUTimer *timer = *(QEMUTimer **)opaque;
aliguoria672b462008-11-11 21:33:36 +0000140
aliguori3450df32009-03-13 15:03:58 +0000141 for (i = 0; i < MAX_NICS; i++) {
142 if (!nd_table[i].used)
143 continue;
aliguoria672b462008-11-11 21:33:36 +0000144 len = announce_self_create(buf, nd_table[i].macaddr);
145 vlan = nd_table[i].vlan;
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100146 QTAILQ_FOREACH(vc, &vlan->clients, next) {
Mark McLoughline3f5ec22009-05-18 13:33:03 +0100147 vc->receive(vc, buf, len);
aliguoria672b462008-11-11 21:33:36 +0000148 }
149 }
Nolan18995b92009-10-15 16:53:55 -0700150 if (--count) {
151 /* delay 50ms, 150ms, 250ms, ... */
152 qemu_mod_timer(timer, qemu_get_clock(rt_clock) +
153 50 + (SELF_ANNOUNCE_ROUNDS - count - 1) * 100);
Gleb Natapoved8b3302009-05-21 17:17:44 +0300154 } else {
155 qemu_del_timer(timer);
156 qemu_free_timer(timer);
157 }
158}
159
160void qemu_announce_self(void)
161{
162 static QEMUTimer *timer;
163 timer = qemu_new_timer(rt_clock, qemu_announce_self_once, &timer);
164 qemu_announce_self_once(&timer);
aliguoria672b462008-11-11 21:33:36 +0000165}
166
167/***********************************************************/
168/* savevm/loadvm support */
169
170#define IO_BUF_SIZE 32768
171
172struct QEMUFile {
173 QEMUFilePutBufferFunc *put_buffer;
174 QEMUFileGetBufferFunc *get_buffer;
175 QEMUFileCloseFunc *close;
176 QEMUFileRateLimit *rate_limit;
Glauber Costa19629532009-05-20 18:26:57 -0400177 QEMUFileSetRateLimit *set_rate_limit;
aliguoria672b462008-11-11 21:33:36 +0000178 void *opaque;
179 int is_write;
180
181 int64_t buf_offset; /* start of buffer when writing, end of buffer
182 when reading */
183 int buf_index;
184 int buf_size; /* 0 when writing */
185 uint8_t buf[IO_BUF_SIZE];
186
187 int has_error;
188};
189
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200190typedef struct QEMUFileStdio
aliguoria672b462008-11-11 21:33:36 +0000191{
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200192 FILE *stdio_file;
aliguoria672b462008-11-11 21:33:36 +0000193 QEMUFile *file;
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200194} QEMUFileStdio;
aliguoria672b462008-11-11 21:33:36 +0000195
196typedef struct QEMUFileSocket
197{
198 int fd;
199 QEMUFile *file;
200} QEMUFileSocket;
201
202static int socket_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
203{
204 QEMUFileSocket *s = opaque;
205 ssize_t len;
206
207 do {
Blue Swirlc5b76b32009-06-13 08:44:31 +0000208 len = recv(s->fd, (void *)buf, size, 0);
aliguoria672b462008-11-11 21:33:36 +0000209 } while (len == -1 && socket_error() == EINTR);
210
211 if (len == -1)
212 len = -socket_error();
213
214 return len;
215}
216
217static int socket_close(void *opaque)
218{
219 QEMUFileSocket *s = opaque;
220 qemu_free(s);
221 return 0;
222}
223
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200224static int stdio_put_buffer(void *opaque, const uint8_t *buf, int64_t pos, int size)
aliguoria672b462008-11-11 21:33:36 +0000225{
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200226 QEMUFileStdio *s = opaque;
227 return fwrite(buf, 1, size, s->stdio_file);
aliguoria672b462008-11-11 21:33:36 +0000228}
229
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200230static int stdio_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
aliguoria672b462008-11-11 21:33:36 +0000231{
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200232 QEMUFileStdio *s = opaque;
233 FILE *fp = s->stdio_file;
Uri Lublin8a67ec42009-06-08 19:27:21 +0300234 int bytes;
235
236 do {
237 clearerr(fp);
238 bytes = fread(buf, 1, size, fp);
239 } while ((bytes == 0) && ferror(fp) && (errno == EINTR));
240 return bytes;
aliguoria672b462008-11-11 21:33:36 +0000241}
242
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200243static int stdio_pclose(void *opaque)
aliguoria672b462008-11-11 21:33:36 +0000244{
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200245 QEMUFileStdio *s = opaque;
246 pclose(s->stdio_file);
aliguoria672b462008-11-11 21:33:36 +0000247 qemu_free(s);
248 return 0;
249}
250
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200251static int stdio_fclose(void *opaque)
aliguoria672b462008-11-11 21:33:36 +0000252{
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200253 QEMUFileStdio *s = opaque;
254 fclose(s->stdio_file);
255 qemu_free(s);
256 return 0;
257}
aliguoria672b462008-11-11 21:33:36 +0000258
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200259QEMUFile *qemu_popen(FILE *stdio_file, const char *mode)
260{
261 QEMUFileStdio *s;
262
263 if (stdio_file == NULL || mode == NULL || (mode[0] != 'r' && mode[0] != 'w') || mode[1] != 0) {
aliguoria672b462008-11-11 21:33:36 +0000264 fprintf(stderr, "qemu_popen: Argument validity check failed\n");
265 return NULL;
266 }
267
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200268 s = qemu_mallocz(sizeof(QEMUFileStdio));
aliguoria672b462008-11-11 21:33:36 +0000269
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200270 s->stdio_file = stdio_file;
aliguoria672b462008-11-11 21:33:36 +0000271
272 if(mode[0] == 'r') {
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200273 s->file = qemu_fopen_ops(s, NULL, stdio_get_buffer, stdio_pclose, NULL, NULL);
aliguoria672b462008-11-11 21:33:36 +0000274 } else {
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200275 s->file = qemu_fopen_ops(s, stdio_put_buffer, NULL, stdio_pclose, NULL, NULL);
aliguoria672b462008-11-11 21:33:36 +0000276 }
aliguoria672b462008-11-11 21:33:36 +0000277 return s->file;
278}
279
280QEMUFile *qemu_popen_cmd(const char *command, const char *mode)
281{
282 FILE *popen_file;
283
284 popen_file = popen(command, mode);
285 if(popen_file == NULL) {
286 return NULL;
287 }
288
289 return qemu_popen(popen_file, mode);
290}
291
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200292int qemu_stdio_fd(QEMUFile *f)
Chris Lalancette8a43b1e2009-05-25 16:38:23 +0200293{
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200294 QEMUFileStdio *p;
Chris Lalancette8a43b1e2009-05-25 16:38:23 +0200295 int fd;
296
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200297 p = (QEMUFileStdio *)f->opaque;
298 fd = fileno(p->stdio_file);
Chris Lalancette8a43b1e2009-05-25 16:38:23 +0200299
300 return fd;
301}
302
Paolo Bonzini5ac1fad2009-08-18 15:56:25 +0200303QEMUFile *qemu_fdopen(int fd, const char *mode)
304{
305 QEMUFileStdio *s;
306
307 if (mode == NULL ||
308 (mode[0] != 'r' && mode[0] != 'w') ||
309 mode[1] != 'b' || mode[2] != 0) {
310 fprintf(stderr, "qemu_fdopen: Argument validity check failed\n");
311 return NULL;
312 }
313
314 s = qemu_mallocz(sizeof(QEMUFileStdio));
315 s->stdio_file = fdopen(fd, mode);
316 if (!s->stdio_file)
317 goto fail;
318
319 if(mode[0] == 'r') {
320 s->file = qemu_fopen_ops(s, NULL, stdio_get_buffer, stdio_fclose, NULL, NULL);
321 } else {
322 s->file = qemu_fopen_ops(s, stdio_put_buffer, NULL, stdio_fclose, NULL, NULL);
323 }
324 return s->file;
325
326fail:
327 qemu_free(s);
328 return NULL;
329}
330
aliguoria672b462008-11-11 21:33:36 +0000331QEMUFile *qemu_fopen_socket(int fd)
332{
333 QEMUFileSocket *s = qemu_mallocz(sizeof(QEMUFileSocket));
334
aliguoria672b462008-11-11 21:33:36 +0000335 s->fd = fd;
Glauber Costa19629532009-05-20 18:26:57 -0400336 s->file = qemu_fopen_ops(s, NULL, socket_get_buffer, socket_close, NULL, NULL);
aliguoria672b462008-11-11 21:33:36 +0000337 return s->file;
338}
339
aliguoria672b462008-11-11 21:33:36 +0000340static int file_put_buffer(void *opaque, const uint8_t *buf,
341 int64_t pos, int size)
342{
343 QEMUFileStdio *s = opaque;
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200344 fseek(s->stdio_file, pos, SEEK_SET);
345 fwrite(buf, 1, size, s->stdio_file);
aliguoria672b462008-11-11 21:33:36 +0000346 return size;
347}
348
349static int file_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
350{
351 QEMUFileStdio *s = opaque;
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200352 fseek(s->stdio_file, pos, SEEK_SET);
353 return fread(buf, 1, size, s->stdio_file);
aliguoria672b462008-11-11 21:33:36 +0000354}
355
356QEMUFile *qemu_fopen(const char *filename, const char *mode)
357{
358 QEMUFileStdio *s;
359
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200360 if (mode == NULL ||
361 (mode[0] != 'r' && mode[0] != 'w') ||
362 mode[1] != 'b' || mode[2] != 0) {
363 fprintf(stderr, "qemu_fdopen: Argument validity check failed\n");
364 return NULL;
365 }
366
aliguoria672b462008-11-11 21:33:36 +0000367 s = qemu_mallocz(sizeof(QEMUFileStdio));
aliguoria672b462008-11-11 21:33:36 +0000368
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200369 s->stdio_file = fopen(filename, mode);
370 if (!s->stdio_file)
aliguoria672b462008-11-11 21:33:36 +0000371 goto fail;
372
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200373 if(mode[0] == 'w') {
374 s->file = qemu_fopen_ops(s, file_put_buffer, NULL, stdio_fclose, NULL, NULL);
375 } else {
376 s->file = qemu_fopen_ops(s, NULL, file_get_buffer, stdio_fclose, NULL, NULL);
377 }
378 return s->file;
aliguoria672b462008-11-11 21:33:36 +0000379fail:
aliguoria672b462008-11-11 21:33:36 +0000380 qemu_free(s);
381 return NULL;
382}
383
aliguori178e08a2009-04-05 19:10:55 +0000384static int block_put_buffer(void *opaque, const uint8_t *buf,
aliguoria672b462008-11-11 21:33:36 +0000385 int64_t pos, int size)
386{
Christoph Hellwig45566e92009-07-10 23:11:57 +0200387 bdrv_save_vmstate(opaque, buf, pos, size);
aliguoria672b462008-11-11 21:33:36 +0000388 return size;
389}
390
aliguori178e08a2009-04-05 19:10:55 +0000391static int block_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
aliguoria672b462008-11-11 21:33:36 +0000392{
Christoph Hellwig45566e92009-07-10 23:11:57 +0200393 return bdrv_load_vmstate(opaque, buf, pos, size);
aliguoria672b462008-11-11 21:33:36 +0000394}
395
396static int bdrv_fclose(void *opaque)
397{
aliguoria672b462008-11-11 21:33:36 +0000398 return 0;
399}
400
Christoph Hellwig45566e92009-07-10 23:11:57 +0200401static QEMUFile *qemu_fopen_bdrv(BlockDriverState *bs, int is_writable)
aliguoria672b462008-11-11 21:33:36 +0000402{
aliguoria672b462008-11-11 21:33:36 +0000403 if (is_writable)
Christoph Hellwig45566e92009-07-10 23:11:57 +0200404 return qemu_fopen_ops(bs, block_put_buffer, NULL, bdrv_fclose, NULL, NULL);
405 return qemu_fopen_ops(bs, NULL, block_get_buffer, bdrv_fclose, NULL, NULL);
aliguoria672b462008-11-11 21:33:36 +0000406}
407
408QEMUFile *qemu_fopen_ops(void *opaque, QEMUFilePutBufferFunc *put_buffer,
409 QEMUFileGetBufferFunc *get_buffer,
410 QEMUFileCloseFunc *close,
Glauber Costa19629532009-05-20 18:26:57 -0400411 QEMUFileRateLimit *rate_limit,
412 QEMUFileSetRateLimit *set_rate_limit)
aliguoria672b462008-11-11 21:33:36 +0000413{
414 QEMUFile *f;
415
416 f = qemu_mallocz(sizeof(QEMUFile));
aliguoria672b462008-11-11 21:33:36 +0000417
418 f->opaque = opaque;
419 f->put_buffer = put_buffer;
420 f->get_buffer = get_buffer;
421 f->close = close;
422 f->rate_limit = rate_limit;
Glauber Costa19629532009-05-20 18:26:57 -0400423 f->set_rate_limit = set_rate_limit;
aliguoria672b462008-11-11 21:33:36 +0000424 f->is_write = 0;
425
426 return f;
427}
428
429int qemu_file_has_error(QEMUFile *f)
430{
431 return f->has_error;
432}
433
aliguori4dabe242009-04-05 19:30:51 +0000434void qemu_file_set_error(QEMUFile *f)
435{
436 f->has_error = 1;
437}
438
aliguoria672b462008-11-11 21:33:36 +0000439void qemu_fflush(QEMUFile *f)
440{
441 if (!f->put_buffer)
442 return;
443
444 if (f->is_write && f->buf_index > 0) {
445 int len;
446
447 len = f->put_buffer(f->opaque, f->buf, f->buf_offset, f->buf_index);
448 if (len > 0)
449 f->buf_offset += f->buf_index;
450 else
451 f->has_error = 1;
452 f->buf_index = 0;
453 }
454}
455
456static void qemu_fill_buffer(QEMUFile *f)
457{
458 int len;
459
460 if (!f->get_buffer)
461 return;
462
463 if (f->is_write)
464 abort();
465
466 len = f->get_buffer(f->opaque, f->buf, f->buf_offset, IO_BUF_SIZE);
467 if (len > 0) {
468 f->buf_index = 0;
469 f->buf_size = len;
470 f->buf_offset += len;
471 } else if (len != -EAGAIN)
472 f->has_error = 1;
473}
474
475int qemu_fclose(QEMUFile *f)
476{
477 int ret = 0;
478 qemu_fflush(f);
479 if (f->close)
480 ret = f->close(f->opaque);
481 qemu_free(f);
482 return ret;
483}
484
485void qemu_file_put_notify(QEMUFile *f)
486{
487 f->put_buffer(f->opaque, NULL, 0, 0);
488}
489
490void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size)
491{
492 int l;
493
494 if (!f->has_error && f->is_write == 0 && f->buf_index > 0) {
495 fprintf(stderr,
496 "Attempted to write to buffer while read buffer is not empty\n");
497 abort();
498 }
499
500 while (!f->has_error && size > 0) {
501 l = IO_BUF_SIZE - f->buf_index;
502 if (l > size)
503 l = size;
504 memcpy(f->buf + f->buf_index, buf, l);
505 f->is_write = 1;
506 f->buf_index += l;
507 buf += l;
508 size -= l;
509 if (f->buf_index >= IO_BUF_SIZE)
510 qemu_fflush(f);
511 }
512}
513
514void qemu_put_byte(QEMUFile *f, int v)
515{
516 if (!f->has_error && f->is_write == 0 && f->buf_index > 0) {
517 fprintf(stderr,
518 "Attempted to write to buffer while read buffer is not empty\n");
519 abort();
520 }
521
522 f->buf[f->buf_index++] = v;
523 f->is_write = 1;
524 if (f->buf_index >= IO_BUF_SIZE)
525 qemu_fflush(f);
526}
527
528int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size1)
529{
530 int size, l;
531
532 if (f->is_write)
533 abort();
534
535 size = size1;
536 while (size > 0) {
537 l = f->buf_size - f->buf_index;
538 if (l == 0) {
539 qemu_fill_buffer(f);
540 l = f->buf_size - f->buf_index;
541 if (l == 0)
542 break;
543 }
544 if (l > size)
545 l = size;
546 memcpy(buf, f->buf + f->buf_index, l);
547 f->buf_index += l;
548 buf += l;
549 size -= l;
550 }
551 return size1 - size;
552}
553
554int qemu_get_byte(QEMUFile *f)
555{
556 if (f->is_write)
557 abort();
558
559 if (f->buf_index >= f->buf_size) {
560 qemu_fill_buffer(f);
561 if (f->buf_index >= f->buf_size)
562 return 0;
563 }
564 return f->buf[f->buf_index++];
565}
566
567int64_t qemu_ftell(QEMUFile *f)
568{
569 return f->buf_offset - f->buf_size + f->buf_index;
570}
571
572int64_t qemu_fseek(QEMUFile *f, int64_t pos, int whence)
573{
574 if (whence == SEEK_SET) {
575 /* nothing to do */
576 } else if (whence == SEEK_CUR) {
577 pos += qemu_ftell(f);
578 } else {
579 /* SEEK_END not supported */
580 return -1;
581 }
582 if (f->put_buffer) {
583 qemu_fflush(f);
584 f->buf_offset = pos;
585 } else {
586 f->buf_offset = pos;
587 f->buf_index = 0;
588 f->buf_size = 0;
589 }
590 return pos;
591}
592
593int qemu_file_rate_limit(QEMUFile *f)
594{
595 if (f->rate_limit)
596 return f->rate_limit(f->opaque);
597
598 return 0;
599}
600
Glauber Costa19629532009-05-20 18:26:57 -0400601size_t qemu_file_set_rate_limit(QEMUFile *f, size_t new_rate)
602{
Glauber Costa0bb05ea2009-07-14 18:26:51 -0400603 /* any failed or completed migration keeps its state to allow probing of
604 * migration data, but has no associated file anymore */
605 if (f && f->set_rate_limit)
Glauber Costa19629532009-05-20 18:26:57 -0400606 return f->set_rate_limit(f->opaque, new_rate);
607
608 return 0;
609}
610
aliguoria672b462008-11-11 21:33:36 +0000611void qemu_put_be16(QEMUFile *f, unsigned int v)
612{
613 qemu_put_byte(f, v >> 8);
614 qemu_put_byte(f, v);
615}
616
617void qemu_put_be32(QEMUFile *f, unsigned int v)
618{
619 qemu_put_byte(f, v >> 24);
620 qemu_put_byte(f, v >> 16);
621 qemu_put_byte(f, v >> 8);
622 qemu_put_byte(f, v);
623}
624
625void qemu_put_be64(QEMUFile *f, uint64_t v)
626{
627 qemu_put_be32(f, v >> 32);
628 qemu_put_be32(f, v);
629}
630
631unsigned int qemu_get_be16(QEMUFile *f)
632{
633 unsigned int v;
634 v = qemu_get_byte(f) << 8;
635 v |= qemu_get_byte(f);
636 return v;
637}
638
639unsigned int qemu_get_be32(QEMUFile *f)
640{
641 unsigned int v;
642 v = qemu_get_byte(f) << 24;
643 v |= qemu_get_byte(f) << 16;
644 v |= qemu_get_byte(f) << 8;
645 v |= qemu_get_byte(f);
646 return v;
647}
648
649uint64_t qemu_get_be64(QEMUFile *f)
650{
651 uint64_t v;
652 v = (uint64_t)qemu_get_be32(f) << 32;
653 v |= qemu_get_be32(f);
654 return v;
655}
656
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200657/* 8 bit int */
658
659static int get_int8(QEMUFile *f, void *pv, size_t size)
660{
661 int8_t *v = pv;
662 qemu_get_s8s(f, v);
663 return 0;
664}
665
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200666static void put_int8(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200667{
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200668 int8_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200669 qemu_put_s8s(f, v);
670}
671
672const VMStateInfo vmstate_info_int8 = {
673 .name = "int8",
674 .get = get_int8,
675 .put = put_int8,
676};
677
678/* 16 bit int */
679
680static int get_int16(QEMUFile *f, void *pv, size_t size)
681{
682 int16_t *v = pv;
683 qemu_get_sbe16s(f, v);
684 return 0;
685}
686
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200687static void put_int16(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200688{
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200689 int16_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200690 qemu_put_sbe16s(f, v);
691}
692
693const VMStateInfo vmstate_info_int16 = {
694 .name = "int16",
695 .get = get_int16,
696 .put = put_int16,
697};
698
699/* 32 bit int */
700
701static int get_int32(QEMUFile *f, void *pv, size_t size)
702{
703 int32_t *v = pv;
704 qemu_get_sbe32s(f, v);
705 return 0;
706}
707
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200708static void put_int32(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200709{
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200710 int32_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200711 qemu_put_sbe32s(f, v);
712}
713
714const VMStateInfo vmstate_info_int32 = {
715 .name = "int32",
716 .get = get_int32,
717 .put = put_int32,
718};
719
Juan Quintela82501662009-08-20 19:42:32 +0200720/* 32 bit int. See that the received value is the same than the one
721 in the field */
722
723static int get_int32_equal(QEMUFile *f, void *pv, size_t size)
724{
725 int32_t *v = pv;
726 int32_t v2;
727 qemu_get_sbe32s(f, &v2);
728
729 if (*v == v2)
730 return 0;
731 return -EINVAL;
732}
733
734const VMStateInfo vmstate_info_int32_equal = {
735 .name = "int32 equal",
736 .get = get_int32_equal,
737 .put = put_int32,
738};
739
Juan Quintela0a031e02009-08-20 19:42:37 +0200740/* 32 bit int. See that the received value is the less or the same
741 than the one in the field */
742
743static int get_int32_le(QEMUFile *f, void *pv, size_t size)
744{
745 int32_t *old = pv;
746 int32_t new;
747 qemu_get_sbe32s(f, &new);
748
749 if (*old <= new)
750 return 0;
751 return -EINVAL;
752}
753
754const VMStateInfo vmstate_info_int32_le = {
755 .name = "int32 equal",
756 .get = get_int32_le,
757 .put = put_int32,
758};
759
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200760/* 64 bit int */
761
762static int get_int64(QEMUFile *f, void *pv, size_t size)
763{
764 int64_t *v = pv;
765 qemu_get_sbe64s(f, v);
766 return 0;
767}
768
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200769static void put_int64(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200770{
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200771 int64_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200772 qemu_put_sbe64s(f, v);
773}
774
775const VMStateInfo vmstate_info_int64 = {
776 .name = "int64",
777 .get = get_int64,
778 .put = put_int64,
779};
780
781/* 8 bit unsigned int */
782
783static int get_uint8(QEMUFile *f, void *pv, size_t size)
784{
785 uint8_t *v = pv;
786 qemu_get_8s(f, v);
787 return 0;
788}
789
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200790static void put_uint8(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200791{
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200792 uint8_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200793 qemu_put_8s(f, v);
794}
795
796const VMStateInfo vmstate_info_uint8 = {
797 .name = "uint8",
798 .get = get_uint8,
799 .put = put_uint8,
800};
801
802/* 16 bit unsigned int */
803
804static int get_uint16(QEMUFile *f, void *pv, size_t size)
805{
806 uint16_t *v = pv;
807 qemu_get_be16s(f, v);
808 return 0;
809}
810
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200811static void put_uint16(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200812{
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200813 uint16_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200814 qemu_put_be16s(f, v);
815}
816
817const VMStateInfo vmstate_info_uint16 = {
818 .name = "uint16",
819 .get = get_uint16,
820 .put = put_uint16,
821};
822
823/* 32 bit unsigned int */
824
825static int get_uint32(QEMUFile *f, void *pv, size_t size)
826{
827 uint32_t *v = pv;
828 qemu_get_be32s(f, v);
829 return 0;
830}
831
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200832static void put_uint32(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200833{
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200834 uint32_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200835 qemu_put_be32s(f, v);
836}
837
838const VMStateInfo vmstate_info_uint32 = {
839 .name = "uint32",
840 .get = get_uint32,
841 .put = put_uint32,
842};
843
844/* 64 bit unsigned int */
845
846static int get_uint64(QEMUFile *f, void *pv, size_t size)
847{
848 uint64_t *v = pv;
849 qemu_get_be64s(f, v);
850 return 0;
851}
852
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200853static void put_uint64(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200854{
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200855 uint64_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200856 qemu_put_be64s(f, v);
857}
858
859const VMStateInfo vmstate_info_uint64 = {
860 .name = "uint64",
861 .get = get_uint64,
862 .put = put_uint64,
863};
864
Juan Quintela80cd83e2009-09-10 03:04:36 +0200865/* 8 bit int. See that the received value is the same than the one
866 in the field */
867
868static int get_uint8_equal(QEMUFile *f, void *pv, size_t size)
869{
870 uint8_t *v = pv;
871 uint8_t v2;
872 qemu_get_8s(f, &v2);
873
874 if (*v == v2)
875 return 0;
876 return -EINVAL;
877}
878
879const VMStateInfo vmstate_info_uint8_equal = {
Juan Quintelaaa1cce62009-10-15 19:16:06 +0200880 .name = "uint8 equal",
Juan Quintela80cd83e2009-09-10 03:04:36 +0200881 .get = get_uint8_equal,
882 .put = put_uint8,
883};
884
Juan Quinteladc3b83a2009-10-15 23:16:13 +0200885/* 16 bit unsigned int int. See that the received value is the same than the one
886 in the field */
887
888static int get_uint16_equal(QEMUFile *f, void *pv, size_t size)
889{
890 uint16_t *v = pv;
891 uint16_t v2;
892 qemu_get_be16s(f, &v2);
893
894 if (*v == v2)
895 return 0;
896 return -EINVAL;
897}
898
899const VMStateInfo vmstate_info_uint16_equal = {
900 .name = "uint16 equal",
901 .get = get_uint16_equal,
902 .put = put_uint16,
903};
904
Juan Quinteladde04632009-08-20 19:42:26 +0200905/* timers */
906
907static int get_timer(QEMUFile *f, void *pv, size_t size)
908{
909 QEMUTimer *v = pv;
910 qemu_get_timer(f, v);
911 return 0;
912}
913
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200914static void put_timer(QEMUFile *f, void *pv, size_t size)
Juan Quinteladde04632009-08-20 19:42:26 +0200915{
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200916 QEMUTimer *v = pv;
Juan Quinteladde04632009-08-20 19:42:26 +0200917 qemu_put_timer(f, v);
918}
919
920const VMStateInfo vmstate_info_timer = {
921 .name = "timer",
922 .get = get_timer,
923 .put = put_timer,
924};
925
Juan Quintela6f67c502009-08-20 19:42:35 +0200926/* uint8_t buffers */
927
928static int get_buffer(QEMUFile *f, void *pv, size_t size)
929{
930 uint8_t *v = pv;
931 qemu_get_buffer(f, v, size);
932 return 0;
933}
934
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200935static void put_buffer(QEMUFile *f, void *pv, size_t size)
Juan Quintela6f67c502009-08-20 19:42:35 +0200936{
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200937 uint8_t *v = pv;
Juan Quintela6f67c502009-08-20 19:42:35 +0200938 qemu_put_buffer(f, v, size);
939}
940
941const VMStateInfo vmstate_info_buffer = {
942 .name = "buffer",
943 .get = get_buffer,
944 .put = put_buffer,
945};
946
Juan Quintela76507c72009-10-19 15:46:28 +0200947/* unused buffers: space that was used for some fields that are
948 not usefull anymore */
949
950static int get_unused_buffer(QEMUFile *f, void *pv, size_t size)
951{
952 qemu_fseek(f, size, SEEK_CUR);
953 return 0;
954}
955
956static void put_unused_buffer(QEMUFile *f, void *pv, size_t size)
957{
958 qemu_fseek(f, size, SEEK_CUR);
959}
960
961const VMStateInfo vmstate_info_unused_buffer = {
962 .name = "unused_buffer",
963 .get = get_unused_buffer,
964 .put = put_unused_buffer,
965};
966
aliguoria672b462008-11-11 21:33:36 +0000967typedef struct SaveStateEntry {
Blue Swirl72cf2d42009-09-12 07:36:22 +0000968 QTAILQ_ENTRY(SaveStateEntry) entry;
aliguoria672b462008-11-11 21:33:36 +0000969 char idstr[256];
970 int instance_id;
971 int version_id;
972 int section_id;
973 SaveLiveStateHandler *save_live_state;
974 SaveStateHandler *save_state;
975 LoadStateHandler *load_state;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200976 const VMStateDescription *vmsd;
aliguoria672b462008-11-11 21:33:36 +0000977 void *opaque;
aliguoria672b462008-11-11 21:33:36 +0000978} SaveStateEntry;
979
Blue Swirl72cf2d42009-09-12 07:36:22 +0000980static QTAILQ_HEAD(savevm_handlers, SaveStateEntry) savevm_handlers =
981 QTAILQ_HEAD_INITIALIZER(savevm_handlers);
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200982static int global_section_id;
aliguoria672b462008-11-11 21:33:36 +0000983
Juan Quintela8718e992009-09-01 02:12:31 +0200984static int calculate_new_instance_id(const char *idstr)
985{
986 SaveStateEntry *se;
987 int instance_id = 0;
988
Blue Swirl72cf2d42009-09-12 07:36:22 +0000989 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
Juan Quintela8718e992009-09-01 02:12:31 +0200990 if (strcmp(idstr, se->idstr) == 0
991 && instance_id <= se->instance_id) {
992 instance_id = se->instance_id + 1;
993 }
994 }
995 return instance_id;
996}
997
aliguoria672b462008-11-11 21:33:36 +0000998/* TODO: Individual devices generally have very little idea about the rest
999 of the system, so instance_id should be removed/replaced.
1000 Meanwhile pass -1 as instance_id if you do not already have a clearly
1001 distinguishing id for all instances of your device class. */
1002int register_savevm_live(const char *idstr,
1003 int instance_id,
1004 int version_id,
1005 SaveLiveStateHandler *save_live_state,
1006 SaveStateHandler *save_state,
1007 LoadStateHandler *load_state,
1008 void *opaque)
1009{
Juan Quintela8718e992009-09-01 02:12:31 +02001010 SaveStateEntry *se;
aliguoria672b462008-11-11 21:33:36 +00001011
1012 se = qemu_malloc(sizeof(SaveStateEntry));
aliguoria672b462008-11-11 21:33:36 +00001013 pstrcpy(se->idstr, sizeof(se->idstr), idstr);
aliguoria672b462008-11-11 21:33:36 +00001014 se->version_id = version_id;
1015 se->section_id = global_section_id++;
1016 se->save_live_state = save_live_state;
1017 se->save_state = save_state;
1018 se->load_state = load_state;
1019 se->opaque = opaque;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001020 se->vmsd = NULL;
aliguoria672b462008-11-11 21:33:36 +00001021
Juan Quintela8718e992009-09-01 02:12:31 +02001022 if (instance_id == -1) {
1023 se->instance_id = calculate_new_instance_id(idstr);
1024 } else {
1025 se->instance_id = instance_id;
aliguoria672b462008-11-11 21:33:36 +00001026 }
Juan Quintela8718e992009-09-01 02:12:31 +02001027 /* add at the end of list */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001028 QTAILQ_INSERT_TAIL(&savevm_handlers, se, entry);
aliguoria672b462008-11-11 21:33:36 +00001029 return 0;
1030}
1031
1032int register_savevm(const char *idstr,
1033 int instance_id,
1034 int version_id,
1035 SaveStateHandler *save_state,
1036 LoadStateHandler *load_state,
1037 void *opaque)
1038{
1039 return register_savevm_live(idstr, instance_id, version_id,
1040 NULL, save_state, load_state, opaque);
1041}
1042
aliguori41bd13a2009-04-17 17:10:59 +00001043void unregister_savevm(const char *idstr, void *opaque)
1044{
Juan Quintela8718e992009-09-01 02:12:31 +02001045 SaveStateEntry *se, *new_se;
aliguori41bd13a2009-04-17 17:10:59 +00001046
Blue Swirl72cf2d42009-09-12 07:36:22 +00001047 QTAILQ_FOREACH_SAFE(se, &savevm_handlers, entry, new_se) {
Juan Quintela8718e992009-09-01 02:12:31 +02001048 if (strcmp(se->idstr, idstr) == 0 && se->opaque == opaque) {
Blue Swirl72cf2d42009-09-12 07:36:22 +00001049 QTAILQ_REMOVE(&savevm_handlers, se, entry);
Juan Quintela8718e992009-09-01 02:12:31 +02001050 qemu_free(se);
aliguori41bd13a2009-04-17 17:10:59 +00001051 }
aliguori41bd13a2009-04-17 17:10:59 +00001052 }
1053}
1054
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001055int vmstate_register(int instance_id, const VMStateDescription *vmsd,
1056 void *opaque)
1057{
Juan Quintela8718e992009-09-01 02:12:31 +02001058 SaveStateEntry *se;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001059
1060 se = qemu_malloc(sizeof(SaveStateEntry));
1061 pstrcpy(se->idstr, sizeof(se->idstr), vmsd->name);
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001062 se->version_id = vmsd->version_id;
1063 se->section_id = global_section_id++;
1064 se->save_live_state = NULL;
1065 se->save_state = NULL;
1066 se->load_state = NULL;
1067 se->opaque = opaque;
1068 se->vmsd = vmsd;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001069
Juan Quintela8718e992009-09-01 02:12:31 +02001070 if (instance_id == -1) {
1071 se->instance_id = calculate_new_instance_id(vmsd->name);
1072 } else {
1073 se->instance_id = instance_id;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001074 }
Juan Quintela8718e992009-09-01 02:12:31 +02001075 /* add at the end of list */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001076 QTAILQ_INSERT_TAIL(&savevm_handlers, se, entry);
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001077 return 0;
1078}
1079
Juan Quintela1eb75382009-09-10 03:04:29 +02001080void vmstate_unregister(const VMStateDescription *vmsd, void *opaque)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001081{
Juan Quintela1eb75382009-09-10 03:04:29 +02001082 SaveStateEntry *se, *new_se;
1083
Blue Swirl72cf2d42009-09-12 07:36:22 +00001084 QTAILQ_FOREACH_SAFE(se, &savevm_handlers, entry, new_se) {
Juan Quintela1eb75382009-09-10 03:04:29 +02001085 if (se->vmsd == vmsd && se->opaque == opaque) {
Blue Swirl72cf2d42009-09-12 07:36:22 +00001086 QTAILQ_REMOVE(&savevm_handlers, se, entry);
Juan Quintela1eb75382009-09-10 03:04:29 +02001087 qemu_free(se);
1088 }
1089 }
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001090}
1091
1092int vmstate_load_state(QEMUFile *f, const VMStateDescription *vmsd,
1093 void *opaque, int version_id)
1094{
1095 VMStateField *field = vmsd->fields;
1096
1097 if (version_id > vmsd->version_id) {
1098 return -EINVAL;
1099 }
1100 if (version_id < vmsd->minimum_version_id_old) {
1101 return -EINVAL;
1102 }
1103 if (version_id < vmsd->minimum_version_id) {
1104 return vmsd->load_state_old(f, opaque, version_id);
1105 }
Juan Quintelafd4d52d2009-09-10 03:04:31 +02001106 if (vmsd->pre_load) {
1107 int ret = vmsd->pre_load(opaque);
1108 if (ret)
1109 return ret;
1110 }
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001111 while(field->name) {
Juan Quintelaf11f6a52009-09-29 22:49:07 +02001112 if ((field->field_exists &&
1113 field->field_exists(opaque, version_id)) ||
1114 (!field->field_exists &&
1115 field->version_id <= version_id)) {
Juan Quintelaf752a6a2009-08-20 19:42:27 +02001116 void *base_addr = opaque + field->offset;
1117 int ret, i, n_elems = 1;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001118
Juan Quintelaf752a6a2009-08-20 19:42:27 +02001119 if (field->flags & VMS_ARRAY) {
1120 n_elems = field->num;
Juan Quintelad6698282009-10-16 11:27:17 +02001121 } else if (field->flags & VMS_VARRAY_INT32) {
1122 n_elems = *(int32_t *)(opaque+field->num_offset);
Juan Quintelabdb49412009-10-16 11:35:18 +02001123 } else if (field->flags & VMS_VARRAY_UINT16) {
1124 n_elems = *(uint16_t *)(opaque+field->num_offset);
Juan Quinteladde04632009-08-20 19:42:26 +02001125 }
Juan Quintelaf752a6a2009-08-20 19:42:27 +02001126 if (field->flags & VMS_POINTER) {
1127 base_addr = *(void **)base_addr;
1128 }
1129 for (i = 0; i < n_elems; i++) {
1130 void *addr = base_addr + field->size * i;
Juan Quintelaec245e22009-08-20 19:42:29 +02001131
Juan Quintela19df4382009-09-29 22:48:41 +02001132 if (field->flags & VMS_ARRAY_OF_POINTER) {
1133 addr = *(void **)addr;
1134 }
Juan Quintelaec245e22009-08-20 19:42:29 +02001135 if (field->flags & VMS_STRUCT) {
Juan Quintelafa3aad22009-08-28 15:28:25 +02001136 ret = vmstate_load_state(f, field->vmsd, addr, field->vmsd->version_id);
Juan Quintelaec245e22009-08-20 19:42:29 +02001137 } else {
1138 ret = field->info->get(f, addr, field->size);
1139
1140 }
Juan Quintelaf752a6a2009-08-20 19:42:27 +02001141 if (ret < 0) {
1142 return ret;
1143 }
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001144 }
1145 }
1146 field++;
1147 }
Juan Quintela752ff2f2009-09-10 03:04:30 +02001148 if (vmsd->post_load) {
Juan Quintelae59fb372009-09-29 22:48:21 +02001149 return vmsd->post_load(opaque, version_id);
Juan Quintela752ff2f2009-09-10 03:04:30 +02001150 }
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001151 return 0;
1152}
1153
1154void vmstate_save_state(QEMUFile *f, const VMStateDescription *vmsd,
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001155 void *opaque)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001156{
1157 VMStateField *field = vmsd->fields;
1158
Juan Quintela8fb07912009-09-10 03:04:32 +02001159 if (vmsd->pre_save) {
1160 vmsd->pre_save(opaque);
1161 }
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001162 while(field->name) {
Juan Quintelaf11f6a52009-09-29 22:49:07 +02001163 if (!field->field_exists ||
1164 field->field_exists(opaque, vmsd->version_id)) {
1165 void *base_addr = opaque + field->offset;
1166 int i, n_elems = 1;
Juan Quinteladde04632009-08-20 19:42:26 +02001167
Juan Quintelaf11f6a52009-09-29 22:49:07 +02001168 if (field->flags & VMS_ARRAY) {
1169 n_elems = field->num;
Juan Quintelad6698282009-10-16 11:27:17 +02001170 } else if (field->flags & VMS_VARRAY_INT32) {
1171 n_elems = *(int32_t *)(opaque+field->num_offset);
Juan Quintelabdb49412009-10-16 11:35:18 +02001172 } else if (field->flags & VMS_VARRAY_UINT16) {
1173 n_elems = *(uint16_t *)(opaque+field->num_offset);
Juan Quintelaf11f6a52009-09-29 22:49:07 +02001174 }
1175 if (field->flags & VMS_POINTER) {
1176 base_addr = *(void **)base_addr;
1177 }
1178 for (i = 0; i < n_elems; i++) {
1179 void *addr = base_addr + field->size * i;
Juan Quintelaec245e22009-08-20 19:42:29 +02001180
Juan Quintelaf11f6a52009-09-29 22:49:07 +02001181 if (field->flags & VMS_STRUCT) {
1182 vmstate_save_state(f, field->vmsd, addr);
1183 } else {
1184 field->info->put(f, addr, field->size);
1185 }
Juan Quintelaec245e22009-08-20 19:42:29 +02001186 }
Juan Quintelaf752a6a2009-08-20 19:42:27 +02001187 }
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001188 field++;
1189 }
Juan Quintela8fb07912009-09-10 03:04:32 +02001190 if (vmsd->post_save) {
1191 vmsd->post_save(opaque);
1192 }
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001193}
1194
Juan Quintela4082be42009-08-20 19:42:24 +02001195static int vmstate_load(QEMUFile *f, SaveStateEntry *se, int version_id)
1196{
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001197 if (!se->vmsd) { /* Old style */
1198 return se->load_state(f, se->opaque, version_id);
1199 }
1200 return vmstate_load_state(f, se->vmsd, se->opaque, version_id);
Juan Quintela4082be42009-08-20 19:42:24 +02001201}
1202
1203static void vmstate_save(QEMUFile *f, SaveStateEntry *se)
1204{
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001205 if (!se->vmsd) { /* Old style */
1206 se->save_state(f, se->opaque);
1207 return;
1208 }
1209 vmstate_save_state(f,se->vmsd, se->opaque);
Juan Quintela4082be42009-08-20 19:42:24 +02001210}
1211
aliguoria672b462008-11-11 21:33:36 +00001212#define QEMU_VM_FILE_MAGIC 0x5145564d
1213#define QEMU_VM_FILE_VERSION_COMPAT 0x00000002
1214#define QEMU_VM_FILE_VERSION 0x00000003
1215
1216#define QEMU_VM_EOF 0x00
1217#define QEMU_VM_SECTION_START 0x01
1218#define QEMU_VM_SECTION_PART 0x02
1219#define QEMU_VM_SECTION_END 0x03
1220#define QEMU_VM_SECTION_FULL 0x04
1221
1222int qemu_savevm_state_begin(QEMUFile *f)
1223{
1224 SaveStateEntry *se;
1225
1226 qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
1227 qemu_put_be32(f, QEMU_VM_FILE_VERSION);
1228
Blue Swirl72cf2d42009-09-12 07:36:22 +00001229 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
aliguoria672b462008-11-11 21:33:36 +00001230 int len;
1231
1232 if (se->save_live_state == NULL)
1233 continue;
1234
1235 /* Section type */
1236 qemu_put_byte(f, QEMU_VM_SECTION_START);
1237 qemu_put_be32(f, se->section_id);
1238
1239 /* ID string */
1240 len = strlen(se->idstr);
1241 qemu_put_byte(f, len);
1242 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
1243
1244 qemu_put_be32(f, se->instance_id);
1245 qemu_put_be32(f, se->version_id);
1246
1247 se->save_live_state(f, QEMU_VM_SECTION_START, se->opaque);
1248 }
1249
1250 if (qemu_file_has_error(f))
1251 return -EIO;
1252
1253 return 0;
1254}
1255
1256int qemu_savevm_state_iterate(QEMUFile *f)
1257{
1258 SaveStateEntry *se;
1259 int ret = 1;
1260
Blue Swirl72cf2d42009-09-12 07:36:22 +00001261 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
aliguoria672b462008-11-11 21:33:36 +00001262 if (se->save_live_state == NULL)
1263 continue;
1264
1265 /* Section type */
1266 qemu_put_byte(f, QEMU_VM_SECTION_PART);
1267 qemu_put_be32(f, se->section_id);
1268
1269 ret &= !!se->save_live_state(f, QEMU_VM_SECTION_PART, se->opaque);
1270 }
1271
1272 if (ret)
1273 return 1;
1274
1275 if (qemu_file_has_error(f))
1276 return -EIO;
1277
1278 return 0;
1279}
1280
1281int qemu_savevm_state_complete(QEMUFile *f)
1282{
1283 SaveStateEntry *se;
1284
Blue Swirl72cf2d42009-09-12 07:36:22 +00001285 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
aliguoria672b462008-11-11 21:33:36 +00001286 if (se->save_live_state == NULL)
1287 continue;
1288
1289 /* Section type */
1290 qemu_put_byte(f, QEMU_VM_SECTION_END);
1291 qemu_put_be32(f, se->section_id);
1292
1293 se->save_live_state(f, QEMU_VM_SECTION_END, se->opaque);
1294 }
1295
Blue Swirl72cf2d42009-09-12 07:36:22 +00001296 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
aliguoria672b462008-11-11 21:33:36 +00001297 int len;
1298
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001299 if (se->save_state == NULL && se->vmsd == NULL)
aliguoria672b462008-11-11 21:33:36 +00001300 continue;
1301
1302 /* Section type */
1303 qemu_put_byte(f, QEMU_VM_SECTION_FULL);
1304 qemu_put_be32(f, se->section_id);
1305
1306 /* ID string */
1307 len = strlen(se->idstr);
1308 qemu_put_byte(f, len);
1309 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
1310
1311 qemu_put_be32(f, se->instance_id);
1312 qemu_put_be32(f, se->version_id);
1313
Juan Quintela4082be42009-08-20 19:42:24 +02001314 vmstate_save(f, se);
aliguoria672b462008-11-11 21:33:36 +00001315 }
1316
1317 qemu_put_byte(f, QEMU_VM_EOF);
1318
1319 if (qemu_file_has_error(f))
1320 return -EIO;
1321
1322 return 0;
1323}
1324
1325int qemu_savevm_state(QEMUFile *f)
1326{
1327 int saved_vm_running;
1328 int ret;
1329
1330 saved_vm_running = vm_running;
1331 vm_stop(0);
1332
1333 bdrv_flush_all();
1334
1335 ret = qemu_savevm_state_begin(f);
1336 if (ret < 0)
1337 goto out;
1338
1339 do {
1340 ret = qemu_savevm_state_iterate(f);
1341 if (ret < 0)
1342 goto out;
1343 } while (ret == 0);
1344
1345 ret = qemu_savevm_state_complete(f);
1346
1347out:
1348 if (qemu_file_has_error(f))
1349 ret = -EIO;
1350
1351 if (!ret && saved_vm_running)
1352 vm_start();
1353
1354 return ret;
1355}
1356
1357static SaveStateEntry *find_se(const char *idstr, int instance_id)
1358{
1359 SaveStateEntry *se;
1360
Blue Swirl72cf2d42009-09-12 07:36:22 +00001361 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
aliguoria672b462008-11-11 21:33:36 +00001362 if (!strcmp(se->idstr, idstr) &&
1363 instance_id == se->instance_id)
1364 return se;
1365 }
1366 return NULL;
1367}
1368
1369typedef struct LoadStateEntry {
Blue Swirl72cf2d42009-09-12 07:36:22 +00001370 QLIST_ENTRY(LoadStateEntry) entry;
aliguoria672b462008-11-11 21:33:36 +00001371 SaveStateEntry *se;
1372 int section_id;
1373 int version_id;
aliguoria672b462008-11-11 21:33:36 +00001374} LoadStateEntry;
1375
aliguoria672b462008-11-11 21:33:36 +00001376int qemu_loadvm_state(QEMUFile *f)
1377{
Blue Swirl72cf2d42009-09-12 07:36:22 +00001378 QLIST_HEAD(, LoadStateEntry) loadvm_handlers =
1379 QLIST_HEAD_INITIALIZER(loadvm_handlers);
Juan Quintelaf4dbb8d2009-09-01 02:12:33 +02001380 LoadStateEntry *le, *new_le;
aliguoria672b462008-11-11 21:33:36 +00001381 uint8_t section_type;
1382 unsigned int v;
1383 int ret;
1384
1385 v = qemu_get_be32(f);
1386 if (v != QEMU_VM_FILE_MAGIC)
1387 return -EINVAL;
1388
1389 v = qemu_get_be32(f);
Juan Quintelabbfe1402009-09-10 03:04:24 +02001390 if (v == QEMU_VM_FILE_VERSION_COMPAT) {
1391 fprintf(stderr, "SaveVM v2 format is obsolete and don't work anymore\n");
1392 return -ENOTSUP;
1393 }
aliguoria672b462008-11-11 21:33:36 +00001394 if (v != QEMU_VM_FILE_VERSION)
1395 return -ENOTSUP;
1396
1397 while ((section_type = qemu_get_byte(f)) != QEMU_VM_EOF) {
1398 uint32_t instance_id, version_id, section_id;
aliguoria672b462008-11-11 21:33:36 +00001399 SaveStateEntry *se;
1400 char idstr[257];
1401 int len;
1402
1403 switch (section_type) {
1404 case QEMU_VM_SECTION_START:
1405 case QEMU_VM_SECTION_FULL:
1406 /* Read section start */
1407 section_id = qemu_get_be32(f);
1408 len = qemu_get_byte(f);
1409 qemu_get_buffer(f, (uint8_t *)idstr, len);
1410 idstr[len] = 0;
1411 instance_id = qemu_get_be32(f);
1412 version_id = qemu_get_be32(f);
1413
1414 /* Find savevm section */
1415 se = find_se(idstr, instance_id);
1416 if (se == NULL) {
1417 fprintf(stderr, "Unknown savevm section or instance '%s' %d\n", idstr, instance_id);
1418 ret = -EINVAL;
1419 goto out;
1420 }
1421
1422 /* Validate version */
1423 if (version_id > se->version_id) {
1424 fprintf(stderr, "savevm: unsupported version %d for '%s' v%d\n",
1425 version_id, idstr, se->version_id);
1426 ret = -EINVAL;
1427 goto out;
1428 }
1429
1430 /* Add entry */
1431 le = qemu_mallocz(sizeof(*le));
aliguoria672b462008-11-11 21:33:36 +00001432
1433 le->se = se;
1434 le->section_id = section_id;
1435 le->version_id = version_id;
Blue Swirl72cf2d42009-09-12 07:36:22 +00001436 QLIST_INSERT_HEAD(&loadvm_handlers, le, entry);
aliguoria672b462008-11-11 21:33:36 +00001437
Juan Quintela4082be42009-08-20 19:42:24 +02001438 ret = vmstate_load(f, le->se, le->version_id);
Juan Quintelab5a22e42009-08-20 19:42:23 +02001439 if (ret < 0) {
1440 fprintf(stderr, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
1441 instance_id, idstr);
1442 goto out;
1443 }
aliguoria672b462008-11-11 21:33:36 +00001444 break;
1445 case QEMU_VM_SECTION_PART:
1446 case QEMU_VM_SECTION_END:
1447 section_id = qemu_get_be32(f);
1448
Blue Swirl72cf2d42009-09-12 07:36:22 +00001449 QLIST_FOREACH(le, &loadvm_handlers, entry) {
Juan Quintelaf4dbb8d2009-09-01 02:12:33 +02001450 if (le->section_id == section_id) {
1451 break;
1452 }
1453 }
aliguoria672b462008-11-11 21:33:36 +00001454 if (le == NULL) {
1455 fprintf(stderr, "Unknown savevm section %d\n", section_id);
1456 ret = -EINVAL;
1457 goto out;
1458 }
1459
Juan Quintela4082be42009-08-20 19:42:24 +02001460 ret = vmstate_load(f, le->se, le->version_id);
Juan Quintelab5a22e42009-08-20 19:42:23 +02001461 if (ret < 0) {
1462 fprintf(stderr, "qemu: warning: error while loading state section id %d\n",
1463 section_id);
1464 goto out;
1465 }
aliguoria672b462008-11-11 21:33:36 +00001466 break;
1467 default:
1468 fprintf(stderr, "Unknown savevm section type %d\n", section_type);
1469 ret = -EINVAL;
1470 goto out;
1471 }
1472 }
1473
1474 ret = 0;
1475
1476out:
Blue Swirl72cf2d42009-09-12 07:36:22 +00001477 QLIST_FOREACH_SAFE(le, &loadvm_handlers, entry, new_le) {
1478 QLIST_REMOVE(le, entry);
aliguoria672b462008-11-11 21:33:36 +00001479 qemu_free(le);
1480 }
1481
1482 if (qemu_file_has_error(f))
1483 ret = -EIO;
1484
1485 return ret;
1486}
1487
1488/* device can contain snapshots */
1489static int bdrv_can_snapshot(BlockDriverState *bs)
1490{
1491 return (bs &&
1492 !bdrv_is_removable(bs) &&
1493 !bdrv_is_read_only(bs));
1494}
1495
1496/* device must be snapshots in order to have a reliable snapshot */
1497static int bdrv_has_snapshot(BlockDriverState *bs)
1498{
1499 return (bs &&
1500 !bdrv_is_removable(bs) &&
1501 !bdrv_is_read_only(bs));
1502}
1503
1504static BlockDriverState *get_bs_snapshots(void)
1505{
1506 BlockDriverState *bs;
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001507 DriveInfo *dinfo;
aliguoria672b462008-11-11 21:33:36 +00001508
1509 if (bs_snapshots)
1510 return bs_snapshots;
Blue Swirl72cf2d42009-09-12 07:36:22 +00001511 QTAILQ_FOREACH(dinfo, &drives, next) {
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001512 bs = dinfo->bdrv;
aliguoria672b462008-11-11 21:33:36 +00001513 if (bdrv_can_snapshot(bs))
1514 goto ok;
1515 }
1516 return NULL;
1517 ok:
1518 bs_snapshots = bs;
1519 return bs;
1520}
1521
1522static int bdrv_snapshot_find(BlockDriverState *bs, QEMUSnapshotInfo *sn_info,
1523 const char *name)
1524{
1525 QEMUSnapshotInfo *sn_tab, *sn;
1526 int nb_sns, i, ret;
1527
1528 ret = -ENOENT;
1529 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
1530 if (nb_sns < 0)
1531 return ret;
1532 for(i = 0; i < nb_sns; i++) {
1533 sn = &sn_tab[i];
1534 if (!strcmp(sn->id_str, name) || !strcmp(sn->name, name)) {
1535 *sn_info = *sn;
1536 ret = 0;
1537 break;
1538 }
1539 }
1540 qemu_free(sn_tab);
1541 return ret;
1542}
1543
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001544void do_savevm(Monitor *mon, const QDict *qdict)
aliguoria672b462008-11-11 21:33:36 +00001545{
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001546 DriveInfo *dinfo;
aliguoria672b462008-11-11 21:33:36 +00001547 BlockDriverState *bs, *bs1;
1548 QEMUSnapshotInfo sn1, *sn = &sn1, old_sn1, *old_sn = &old_sn1;
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001549 int must_delete, ret;
aliguoria672b462008-11-11 21:33:36 +00001550 QEMUFile *f;
1551 int saved_vm_running;
aliguori2d22b182008-12-11 21:06:49 +00001552 uint32_t vm_state_size;
aliguoria672b462008-11-11 21:33:36 +00001553#ifdef _WIN32
1554 struct _timeb tb;
1555#else
1556 struct timeval tv;
1557#endif
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001558 const char *name = qdict_get_try_str(qdict, "name");
aliguoria672b462008-11-11 21:33:36 +00001559
1560 bs = get_bs_snapshots();
1561 if (!bs) {
aliguori376253e2009-03-05 23:01:23 +00001562 monitor_printf(mon, "No block device can accept snapshots\n");
aliguoria672b462008-11-11 21:33:36 +00001563 return;
1564 }
1565
1566 /* ??? Should this occur after vm_stop? */
1567 qemu_aio_flush();
1568
1569 saved_vm_running = vm_running;
1570 vm_stop(0);
1571
1572 must_delete = 0;
1573 if (name) {
1574 ret = bdrv_snapshot_find(bs, old_sn, name);
1575 if (ret >= 0) {
1576 must_delete = 1;
1577 }
1578 }
1579 memset(sn, 0, sizeof(*sn));
1580 if (must_delete) {
1581 pstrcpy(sn->name, sizeof(sn->name), old_sn->name);
1582 pstrcpy(sn->id_str, sizeof(sn->id_str), old_sn->id_str);
1583 } else {
1584 if (name)
1585 pstrcpy(sn->name, sizeof(sn->name), name);
1586 }
1587
1588 /* fill auxiliary fields */
1589#ifdef _WIN32
1590 _ftime(&tb);
1591 sn->date_sec = tb.time;
1592 sn->date_nsec = tb.millitm * 1000000;
1593#else
1594 gettimeofday(&tv, NULL);
1595 sn->date_sec = tv.tv_sec;
1596 sn->date_nsec = tv.tv_usec * 1000;
1597#endif
1598 sn->vm_clock_nsec = qemu_get_clock(vm_clock);
1599
aliguoria672b462008-11-11 21:33:36 +00001600 /* save the VM state */
Christoph Hellwig45566e92009-07-10 23:11:57 +02001601 f = qemu_fopen_bdrv(bs, 1);
aliguoria672b462008-11-11 21:33:36 +00001602 if (!f) {
aliguori376253e2009-03-05 23:01:23 +00001603 monitor_printf(mon, "Could not open VM state file\n");
aliguoria672b462008-11-11 21:33:36 +00001604 goto the_end;
1605 }
1606 ret = qemu_savevm_state(f);
aliguori2d22b182008-12-11 21:06:49 +00001607 vm_state_size = qemu_ftell(f);
aliguoria672b462008-11-11 21:33:36 +00001608 qemu_fclose(f);
1609 if (ret < 0) {
aliguori376253e2009-03-05 23:01:23 +00001610 monitor_printf(mon, "Error %d while writing VM\n", ret);
aliguoria672b462008-11-11 21:33:36 +00001611 goto the_end;
1612 }
1613
1614 /* create the snapshots */
1615
Blue Swirl72cf2d42009-09-12 07:36:22 +00001616 QTAILQ_FOREACH(dinfo, &drives, next) {
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001617 bs1 = dinfo->bdrv;
aliguoria672b462008-11-11 21:33:36 +00001618 if (bdrv_has_snapshot(bs1)) {
1619 if (must_delete) {
1620 ret = bdrv_snapshot_delete(bs1, old_sn->id_str);
1621 if (ret < 0) {
aliguori376253e2009-03-05 23:01:23 +00001622 monitor_printf(mon,
1623 "Error while deleting snapshot on '%s'\n",
1624 bdrv_get_device_name(bs1));
aliguoria672b462008-11-11 21:33:36 +00001625 }
1626 }
aliguori2d22b182008-12-11 21:06:49 +00001627 /* Write VM state size only to the image that contains the state */
1628 sn->vm_state_size = (bs == bs1 ? vm_state_size : 0);
aliguoria672b462008-11-11 21:33:36 +00001629 ret = bdrv_snapshot_create(bs1, sn);
1630 if (ret < 0) {
aliguori376253e2009-03-05 23:01:23 +00001631 monitor_printf(mon, "Error while creating snapshot on '%s'\n",
1632 bdrv_get_device_name(bs1));
aliguoria672b462008-11-11 21:33:36 +00001633 }
1634 }
1635 }
1636
1637 the_end:
1638 if (saved_vm_running)
1639 vm_start();
1640}
1641
Juan Quintela05f24012009-08-20 19:42:22 +02001642int load_vmstate(Monitor *mon, const char *name)
aliguoria672b462008-11-11 21:33:36 +00001643{
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001644 DriveInfo *dinfo;
aliguoria672b462008-11-11 21:33:36 +00001645 BlockDriverState *bs, *bs1;
aliguori2d22b182008-12-11 21:06:49 +00001646 QEMUSnapshotInfo sn;
aliguoria672b462008-11-11 21:33:36 +00001647 QEMUFile *f;
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001648 int ret;
aliguoria672b462008-11-11 21:33:36 +00001649
1650 bs = get_bs_snapshots();
1651 if (!bs) {
aliguori376253e2009-03-05 23:01:23 +00001652 monitor_printf(mon, "No block device supports snapshots\n");
Juan Quintela05f24012009-08-20 19:42:22 +02001653 return -EINVAL;
aliguoria672b462008-11-11 21:33:36 +00001654 }
1655
1656 /* Flush all IO requests so they don't interfere with the new state. */
1657 qemu_aio_flush();
1658
Blue Swirl72cf2d42009-09-12 07:36:22 +00001659 QTAILQ_FOREACH(dinfo, &drives, next) {
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001660 bs1 = dinfo->bdrv;
aliguoria672b462008-11-11 21:33:36 +00001661 if (bdrv_has_snapshot(bs1)) {
1662 ret = bdrv_snapshot_goto(bs1, name);
1663 if (ret < 0) {
1664 if (bs != bs1)
aliguori376253e2009-03-05 23:01:23 +00001665 monitor_printf(mon, "Warning: ");
aliguoria672b462008-11-11 21:33:36 +00001666 switch(ret) {
1667 case -ENOTSUP:
aliguori376253e2009-03-05 23:01:23 +00001668 monitor_printf(mon,
1669 "Snapshots not supported on device '%s'\n",
1670 bdrv_get_device_name(bs1));
aliguoria672b462008-11-11 21:33:36 +00001671 break;
1672 case -ENOENT:
aliguori376253e2009-03-05 23:01:23 +00001673 monitor_printf(mon, "Could not find snapshot '%s' on "
1674 "device '%s'\n",
1675 name, bdrv_get_device_name(bs1));
aliguoria672b462008-11-11 21:33:36 +00001676 break;
1677 default:
aliguori376253e2009-03-05 23:01:23 +00001678 monitor_printf(mon, "Error %d while activating snapshot on"
1679 " '%s'\n", ret, bdrv_get_device_name(bs1));
aliguoria672b462008-11-11 21:33:36 +00001680 break;
1681 }
1682 /* fatal on snapshot block device */
1683 if (bs == bs1)
Juan Quintela05f24012009-08-20 19:42:22 +02001684 return 0;
aliguoria672b462008-11-11 21:33:36 +00001685 }
1686 }
1687 }
1688
aliguori2d22b182008-12-11 21:06:49 +00001689 /* Don't even try to load empty VM states */
1690 ret = bdrv_snapshot_find(bs, &sn, name);
1691 if ((ret >= 0) && (sn.vm_state_size == 0))
Juan Quintela05f24012009-08-20 19:42:22 +02001692 return -EINVAL;
aliguori2d22b182008-12-11 21:06:49 +00001693
aliguoria672b462008-11-11 21:33:36 +00001694 /* restore the VM state */
Christoph Hellwig45566e92009-07-10 23:11:57 +02001695 f = qemu_fopen_bdrv(bs, 0);
aliguoria672b462008-11-11 21:33:36 +00001696 if (!f) {
aliguori376253e2009-03-05 23:01:23 +00001697 monitor_printf(mon, "Could not open VM state file\n");
Juan Quintela05f24012009-08-20 19:42:22 +02001698 return -EINVAL;
aliguoria672b462008-11-11 21:33:36 +00001699 }
1700 ret = qemu_loadvm_state(f);
1701 qemu_fclose(f);
1702 if (ret < 0) {
aliguori376253e2009-03-05 23:01:23 +00001703 monitor_printf(mon, "Error %d while loading VM state\n", ret);
Juan Quintela05f24012009-08-20 19:42:22 +02001704 return ret;
aliguoria672b462008-11-11 21:33:36 +00001705 }
Juan Quintela05f24012009-08-20 19:42:22 +02001706 return 0;
Juan Quintela7b630342009-08-20 19:42:20 +02001707}
1708
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001709void do_delvm(Monitor *mon, const QDict *qdict)
aliguoria672b462008-11-11 21:33:36 +00001710{
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001711 DriveInfo *dinfo;
aliguoria672b462008-11-11 21:33:36 +00001712 BlockDriverState *bs, *bs1;
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001713 int ret;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001714 const char *name = qdict_get_str(qdict, "name");
aliguoria672b462008-11-11 21:33:36 +00001715
1716 bs = get_bs_snapshots();
1717 if (!bs) {
aliguori376253e2009-03-05 23:01:23 +00001718 monitor_printf(mon, "No block device supports snapshots\n");
aliguoria672b462008-11-11 21:33:36 +00001719 return;
1720 }
1721
Blue Swirl72cf2d42009-09-12 07:36:22 +00001722 QTAILQ_FOREACH(dinfo, &drives, next) {
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001723 bs1 = dinfo->bdrv;
aliguoria672b462008-11-11 21:33:36 +00001724 if (bdrv_has_snapshot(bs1)) {
1725 ret = bdrv_snapshot_delete(bs1, name);
1726 if (ret < 0) {
1727 if (ret == -ENOTSUP)
aliguori376253e2009-03-05 23:01:23 +00001728 monitor_printf(mon,
1729 "Snapshots not supported on device '%s'\n",
1730 bdrv_get_device_name(bs1));
aliguoria672b462008-11-11 21:33:36 +00001731 else
aliguori376253e2009-03-05 23:01:23 +00001732 monitor_printf(mon, "Error %d while deleting snapshot on "
1733 "'%s'\n", ret, bdrv_get_device_name(bs1));
aliguoria672b462008-11-11 21:33:36 +00001734 }
1735 }
1736 }
1737}
1738
aliguori376253e2009-03-05 23:01:23 +00001739void do_info_snapshots(Monitor *mon)
aliguoria672b462008-11-11 21:33:36 +00001740{
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001741 DriveInfo *dinfo;
aliguoria672b462008-11-11 21:33:36 +00001742 BlockDriverState *bs, *bs1;
1743 QEMUSnapshotInfo *sn_tab, *sn;
1744 int nb_sns, i;
1745 char buf[256];
1746
1747 bs = get_bs_snapshots();
1748 if (!bs) {
aliguori376253e2009-03-05 23:01:23 +00001749 monitor_printf(mon, "No available block device supports snapshots\n");
aliguoria672b462008-11-11 21:33:36 +00001750 return;
1751 }
aliguori376253e2009-03-05 23:01:23 +00001752 monitor_printf(mon, "Snapshot devices:");
Blue Swirl72cf2d42009-09-12 07:36:22 +00001753 QTAILQ_FOREACH(dinfo, &drives, next) {
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001754 bs1 = dinfo->bdrv;
aliguoria672b462008-11-11 21:33:36 +00001755 if (bdrv_has_snapshot(bs1)) {
1756 if (bs == bs1)
aliguori376253e2009-03-05 23:01:23 +00001757 monitor_printf(mon, " %s", bdrv_get_device_name(bs1));
aliguoria672b462008-11-11 21:33:36 +00001758 }
1759 }
aliguori376253e2009-03-05 23:01:23 +00001760 monitor_printf(mon, "\n");
aliguoria672b462008-11-11 21:33:36 +00001761
1762 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
1763 if (nb_sns < 0) {
aliguori376253e2009-03-05 23:01:23 +00001764 monitor_printf(mon, "bdrv_snapshot_list: error %d\n", nb_sns);
aliguoria672b462008-11-11 21:33:36 +00001765 return;
1766 }
aliguori376253e2009-03-05 23:01:23 +00001767 monitor_printf(mon, "Snapshot list (from %s):\n",
1768 bdrv_get_device_name(bs));
1769 monitor_printf(mon, "%s\n", bdrv_snapshot_dump(buf, sizeof(buf), NULL));
aliguoria672b462008-11-11 21:33:36 +00001770 for(i = 0; i < nb_sns; i++) {
1771 sn = &sn_tab[i];
aliguori376253e2009-03-05 23:01:23 +00001772 monitor_printf(mon, "%s\n", bdrv_snapshot_dump(buf, sizeof(buf), sn));
aliguoria672b462008-11-11 21:33:36 +00001773 }
1774 qemu_free(sn_tab);
1775}