]> Git Repo - qemu.git/blame - savevm.c
stdio_pclose: return -errno on error (v3)
[qemu.git] / savevm.c
CommitLineData
a672b469
AL
1/*
2 * QEMU System Emulator
3 *
4 * Copyright (c) 2003-2008 Fabrice Bellard
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
a672b469
AL
24#include <unistd.h>
25#include <fcntl.h>
a672b469
AL
26#include <time.h>
27#include <errno.h>
28#include <sys/time.h>
29#include <zlib.h>
30
71e72a19 31/* Needed early for CONFIG_BSD etc. */
d40cdb10
BS
32#include "config-host.h"
33
a672b469
AL
34#ifndef _WIN32
35#include <sys/times.h>
36#include <sys/wait.h>
37#include <termios.h>
38#include <sys/mman.h>
39#include <sys/ioctl.h>
40#include <sys/resource.h>
41#include <sys/socket.h>
42#include <netinet/in.h>
43#include <net/if.h>
a672b469
AL
44#include <arpa/inet.h>
45#include <dirent.h>
46#include <netdb.h>
47#include <sys/select.h>
71e72a19 48#ifdef CONFIG_BSD
a672b469 49#include <sys/stat.h>
a167ba50 50#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
a672b469
AL
51#include <libutil.h>
52#else
53#include <util.h>
54#endif
a672b469
AL
55#ifdef __linux__
56#include <pty.h>
57#include <malloc.h>
58#include <linux/rtc.h>
59#endif
60#endif
61#endif
62
63#ifdef _WIN32
49dc768d 64#include <windows.h>
a672b469
AL
65#include <malloc.h>
66#include <sys/timeb.h>
67#include <mmsystem.h>
68#define getopt_long_only getopt_long
69#define memalign(align, size) malloc(size)
70#endif
71
511d2b14
BS
72#include "qemu-common.h"
73#include "hw/hw.h"
7685ee6a 74#include "hw/qdev.h"
511d2b14
BS
75#include "net.h"
76#include "monitor.h"
77#include "sysemu.h"
78#include "qemu-timer.h"
79#include "qemu-char.h"
511d2b14
BS
80#include "audio/audio.h"
81#include "migration.h"
82#include "qemu_socket.h"
72cf2d4f 83#include "qemu-queue.h"
2ff68d07 84#include "qemu-timer.h"
17a4663e 85#include "cpus.h"
511d2b14 86
a672b469 87#define SELF_ANNOUNCE_ROUNDS 5
a672b469 88
18995b98 89#ifndef ETH_P_RARP
f8778a77 90#define ETH_P_RARP 0x8035
18995b98
N
91#endif
92#define ARP_HTYPE_ETH 0x0001
93#define ARP_PTYPE_IP 0x0800
94#define ARP_OP_REQUEST_REV 0x3
95
96static int announce_self_create(uint8_t *buf,
a672b469
AL
97 uint8_t *mac_addr)
98{
18995b98
N
99 /* Ethernet header. */
100 memset(buf, 0xff, 6); /* destination MAC addr */
101 memcpy(buf + 6, mac_addr, 6); /* source MAC addr */
102 *(uint16_t *)(buf + 12) = htons(ETH_P_RARP); /* ethertype */
103
104 /* RARP header. */
105 *(uint16_t *)(buf + 14) = htons(ARP_HTYPE_ETH); /* hardware addr space */
106 *(uint16_t *)(buf + 16) = htons(ARP_PTYPE_IP); /* protocol addr space */
107 *(buf + 18) = 6; /* hardware addr length (ethernet) */
108 *(buf + 19) = 4; /* protocol addr length (IPv4) */
109 *(uint16_t *)(buf + 20) = htons(ARP_OP_REQUEST_REV); /* opcode */
110 memcpy(buf + 22, mac_addr, 6); /* source hw addr */
111 memset(buf + 28, 0x00, 4); /* source protocol addr */
112 memcpy(buf + 32, mac_addr, 6); /* target hw addr */
113 memset(buf + 38, 0x00, 4); /* target protocol addr */
114
115 /* Padding to get up to 60 bytes (ethernet min packet size, minus FCS). */
116 memset(buf + 42, 0x00, 18);
117
118 return 60; /* len (FCS will be added by hardware) */
a672b469
AL
119}
120
f401ca22 121static void qemu_announce_self_iter(NICState *nic, void *opaque)
a672b469 122{
18995b98 123 uint8_t buf[60];
f401ca22
MM
124 int len;
125
126 len = announce_self_create(buf, nic->conf->macaddr.a);
127
128 qemu_send_packet_raw(&nic->nc, buf, len);
129}
130
131
132static void qemu_announce_self_once(void *opaque)
133{
ed8b330b
GN
134 static int count = SELF_ANNOUNCE_ROUNDS;
135 QEMUTimer *timer = *(QEMUTimer **)opaque;
a672b469 136
f401ca22
MM
137 qemu_foreach_nic(qemu_announce_self_iter, NULL);
138
18995b98
N
139 if (--count) {
140 /* delay 50ms, 150ms, 250ms, ... */
7bd427d8 141 qemu_mod_timer(timer, qemu_get_clock_ms(rt_clock) +
18995b98 142 50 + (SELF_ANNOUNCE_ROUNDS - count - 1) * 100);
ed8b330b
GN
143 } else {
144 qemu_del_timer(timer);
145 qemu_free_timer(timer);
146 }
147}
148
149void qemu_announce_self(void)
150{
151 static QEMUTimer *timer;
7bd427d8 152 timer = qemu_new_timer_ms(rt_clock, qemu_announce_self_once, &timer);
ed8b330b 153 qemu_announce_self_once(&timer);
a672b469
AL
154}
155
156/***********************************************************/
157/* savevm/loadvm support */
158
159#define IO_BUF_SIZE 32768
160
161struct QEMUFile {
162 QEMUFilePutBufferFunc *put_buffer;
163 QEMUFileGetBufferFunc *get_buffer;
164 QEMUFileCloseFunc *close;
165 QEMUFileRateLimit *rate_limit;
19629537 166 QEMUFileSetRateLimit *set_rate_limit;
c163b5ca 167 QEMUFileGetRateLimit *get_rate_limit;
a672b469
AL
168 void *opaque;
169 int is_write;
170
171 int64_t buf_offset; /* start of buffer when writing, end of buffer
172 when reading */
173 int buf_index;
174 int buf_size; /* 0 when writing */
175 uint8_t buf[IO_BUF_SIZE];
176
3961b4dd 177 int last_error;
a672b469
AL
178};
179
7f79dd28 180typedef struct QEMUFileStdio
a672b469 181{
7f79dd28 182 FILE *stdio_file;
a672b469 183 QEMUFile *file;
7f79dd28 184} QEMUFileStdio;
a672b469
AL
185
186typedef struct QEMUFileSocket
187{
188 int fd;
189 QEMUFile *file;
190} QEMUFileSocket;
191
192static int socket_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
193{
194 QEMUFileSocket *s = opaque;
195 ssize_t len;
196
197 do {
00aa0040 198 len = qemu_recv(s->fd, buf, size, 0);
a672b469
AL
199 } while (len == -1 && socket_error() == EINTR);
200
201 if (len == -1)
202 len = -socket_error();
203
204 return len;
205}
206
207static int socket_close(void *opaque)
208{
209 QEMUFileSocket *s = opaque;
7267c094 210 g_free(s);
a672b469
AL
211 return 0;
212}
213
7f79dd28 214static int stdio_put_buffer(void *opaque, const uint8_t *buf, int64_t pos, int size)
a672b469 215{
7f79dd28
PB
216 QEMUFileStdio *s = opaque;
217 return fwrite(buf, 1, size, s->stdio_file);
a672b469
AL
218}
219
7f79dd28 220static int stdio_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
a672b469 221{
7f79dd28
PB
222 QEMUFileStdio *s = opaque;
223 FILE *fp = s->stdio_file;
8a67ec4d
UL
224 int bytes;
225
226 do {
227 clearerr(fp);
228 bytes = fread(buf, 1, size, fp);
229 } while ((bytes == 0) && ferror(fp) && (errno == EINTR));
230 return bytes;
a672b469
AL
231}
232
7f79dd28
PB
233static int stdio_pclose(void *opaque)
234{
235 QEMUFileStdio *s = opaque;
41ef56e6
AL
236 int ret;
237 ret = pclose(s->stdio_file);
26f1af0a
EH
238 if (ret == -1) {
239 ret = -errno;
240 }
7267c094 241 g_free(s);
41ef56e6 242 return ret;
7f79dd28
PB
243}
244
245static int stdio_fclose(void *opaque)
a672b469 246{
7f79dd28
PB
247 QEMUFileStdio *s = opaque;
248 fclose(s->stdio_file);
7267c094 249 g_free(s);
a672b469
AL
250 return 0;
251}
252
7f79dd28 253QEMUFile *qemu_popen(FILE *stdio_file, const char *mode)
a672b469 254{
7f79dd28 255 QEMUFileStdio *s;
a672b469 256
7f79dd28 257 if (stdio_file == NULL || mode == NULL || (mode[0] != 'r' && mode[0] != 'w') || mode[1] != 0) {
a672b469
AL
258 fprintf(stderr, "qemu_popen: Argument validity check failed\n");
259 return NULL;
260 }
261
7267c094 262 s = g_malloc0(sizeof(QEMUFileStdio));
a672b469 263
7f79dd28 264 s->stdio_file = stdio_file;
a672b469
AL
265
266 if(mode[0] == 'r') {
c163b5ca
LS
267 s->file = qemu_fopen_ops(s, NULL, stdio_get_buffer, stdio_pclose,
268 NULL, NULL, NULL);
a672b469 269 } else {
c163b5ca
LS
270 s->file = qemu_fopen_ops(s, stdio_put_buffer, NULL, stdio_pclose,
271 NULL, NULL, NULL);
a672b469 272 }
a672b469
AL
273 return s->file;
274}
275
276QEMUFile *qemu_popen_cmd(const char *command, const char *mode)
277{
278 FILE *popen_file;
279
280 popen_file = popen(command, mode);
281 if(popen_file == NULL) {
282 return NULL;
283 }
284
285 return qemu_popen(popen_file, mode);
286}
287
7f79dd28 288int qemu_stdio_fd(QEMUFile *f)
8a43b1ea 289{
7f79dd28 290 QEMUFileStdio *p;
8a43b1ea
CL
291 int fd;
292
7f79dd28
PB
293 p = (QEMUFileStdio *)f->opaque;
294 fd = fileno(p->stdio_file);
8a43b1ea
CL
295
296 return fd;
297}
298
5ac1fad3
PB
299QEMUFile *qemu_fdopen(int fd, const char *mode)
300{
301 QEMUFileStdio *s;
302
303 if (mode == NULL ||
304 (mode[0] != 'r' && mode[0] != 'w') ||
305 mode[1] != 'b' || mode[2] != 0) {
306 fprintf(stderr, "qemu_fdopen: Argument validity check failed\n");
307 return NULL;
308 }
309
7267c094 310 s = g_malloc0(sizeof(QEMUFileStdio));
5ac1fad3
PB
311 s->stdio_file = fdopen(fd, mode);
312 if (!s->stdio_file)
313 goto fail;
314
315 if(mode[0] == 'r') {
c163b5ca
LS
316 s->file = qemu_fopen_ops(s, NULL, stdio_get_buffer, stdio_fclose,
317 NULL, NULL, NULL);
5ac1fad3 318 } else {
c163b5ca
LS
319 s->file = qemu_fopen_ops(s, stdio_put_buffer, NULL, stdio_fclose,
320 NULL, NULL, NULL);
5ac1fad3
PB
321 }
322 return s->file;
323
324fail:
7267c094 325 g_free(s);
5ac1fad3
PB
326 return NULL;
327}
328
a672b469
AL
329QEMUFile *qemu_fopen_socket(int fd)
330{
7267c094 331 QEMUFileSocket *s = g_malloc0(sizeof(QEMUFileSocket));
a672b469 332
a672b469 333 s->fd = fd;
c163b5ca
LS
334 s->file = qemu_fopen_ops(s, NULL, socket_get_buffer, socket_close,
335 NULL, NULL, NULL);
a672b469
AL
336 return s->file;
337}
338
a672b469
AL
339static int file_put_buffer(void *opaque, const uint8_t *buf,
340 int64_t pos, int size)
341{
342 QEMUFileStdio *s = opaque;
7f79dd28 343 fseek(s->stdio_file, pos, SEEK_SET);
5fdb3aa1 344 return fwrite(buf, 1, size, s->stdio_file);
a672b469
AL
345}
346
347static int file_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
348{
349 QEMUFileStdio *s = opaque;
7f79dd28
PB
350 fseek(s->stdio_file, pos, SEEK_SET);
351 return fread(buf, 1, size, s->stdio_file);
a672b469
AL
352}
353
354QEMUFile *qemu_fopen(const char *filename, const char *mode)
355{
356 QEMUFileStdio *s;
357
7f79dd28
PB
358 if (mode == NULL ||
359 (mode[0] != 'r' && mode[0] != 'w') ||
360 mode[1] != 'b' || mode[2] != 0) {
090414a3 361 fprintf(stderr, "qemu_fopen: Argument validity check failed\n");
7f79dd28
PB
362 return NULL;
363 }
364
7267c094 365 s = g_malloc0(sizeof(QEMUFileStdio));
a672b469 366
7f79dd28
PB
367 s->stdio_file = fopen(filename, mode);
368 if (!s->stdio_file)
a672b469 369 goto fail;
c163b5ca 370
7f79dd28 371 if(mode[0] == 'w') {
c163b5ca
LS
372 s->file = qemu_fopen_ops(s, file_put_buffer, NULL, stdio_fclose,
373 NULL, NULL, NULL);
7f79dd28 374 } else {
c163b5ca
LS
375 s->file = qemu_fopen_ops(s, NULL, file_get_buffer, stdio_fclose,
376 NULL, NULL, NULL);
7f79dd28
PB
377 }
378 return s->file;
a672b469 379fail:
7267c094 380 g_free(s);
a672b469
AL
381 return NULL;
382}
383
178e08a5 384static int block_put_buffer(void *opaque, const uint8_t *buf,
a672b469
AL
385 int64_t pos, int size)
386{
45566e9c 387 bdrv_save_vmstate(opaque, buf, pos, size);
a672b469
AL
388 return size;
389}
390
178e08a5 391static int block_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
a672b469 392{
45566e9c 393 return bdrv_load_vmstate(opaque, buf, pos, size);
a672b469
AL
394}
395
396static int bdrv_fclose(void *opaque)
397{
a672b469
AL
398 return 0;
399}
400
45566e9c 401static QEMUFile *qemu_fopen_bdrv(BlockDriverState *bs, int is_writable)
a672b469 402{
a672b469 403 if (is_writable)
c163b5ca
LS
404 return qemu_fopen_ops(bs, block_put_buffer, NULL, bdrv_fclose,
405 NULL, NULL, NULL);
406 return qemu_fopen_ops(bs, NULL, block_get_buffer, bdrv_fclose, NULL, NULL, NULL);
a672b469
AL
407}
408
409QEMUFile *qemu_fopen_ops(void *opaque, QEMUFilePutBufferFunc *put_buffer,
410 QEMUFileGetBufferFunc *get_buffer,
411 QEMUFileCloseFunc *close,
19629537 412 QEMUFileRateLimit *rate_limit,
c163b5ca
LS
413 QEMUFileSetRateLimit *set_rate_limit,
414 QEMUFileGetRateLimit *get_rate_limit)
a672b469
AL
415{
416 QEMUFile *f;
417
7267c094 418 f = g_malloc0(sizeof(QEMUFile));
a672b469
AL
419
420 f->opaque = opaque;
421 f->put_buffer = put_buffer;
422 f->get_buffer = get_buffer;
423 f->close = close;
424 f->rate_limit = rate_limit;
19629537 425 f->set_rate_limit = set_rate_limit;
c163b5ca 426 f->get_rate_limit = get_rate_limit;
a672b469
AL
427 f->is_write = 0;
428
429 return f;
430}
431
624b9cc2 432int qemu_file_get_error(QEMUFile *f)
a672b469 433{
3961b4dd 434 return f->last_error;
a672b469
AL
435}
436
dcd1d224 437void qemu_file_set_error(QEMUFile *f, int ret)
4dabe248 438{
3961b4dd 439 f->last_error = ret;
4dabe248
AL
440}
441
d82ca915
EH
442/** Sets last_error conditionally
443 *
444 * Sets last_error only if ret is negative _and_ no error
445 * was set before.
446 */
447static void qemu_file_set_if_error(QEMUFile *f, int ret)
448{
449 if (ret < 0 && !f->last_error) {
450 qemu_file_set_error(f, ret);
451 }
452}
453
454/** Flushes QEMUFile buffer
455 *
456 * In case of error, last_error is set.
457 */
a672b469
AL
458void qemu_fflush(QEMUFile *f)
459{
460 if (!f->put_buffer)
461 return;
462
463 if (f->is_write && f->buf_index > 0) {
464 int len;
465
466 len = f->put_buffer(f->opaque, f->buf, f->buf_offset, f->buf_index);
467 if (len > 0)
468 f->buf_offset += f->buf_index;
469 else
c29110d5 470 qemu_file_set_error(f, -EINVAL);
a672b469
AL
471 f->buf_index = 0;
472 }
473}
474
475static void qemu_fill_buffer(QEMUFile *f)
476{
477 int len;
0046c45b 478 int pending;
a672b469
AL
479
480 if (!f->get_buffer)
481 return;
482
483 if (f->is_write)
484 abort();
485
0046c45b
JQ
486 pending = f->buf_size - f->buf_index;
487 if (pending > 0) {
488 memmove(f->buf, f->buf + f->buf_index, pending);
489 }
490 f->buf_index = 0;
491 f->buf_size = pending;
492
493 len = f->get_buffer(f->opaque, f->buf + pending, f->buf_offset,
494 IO_BUF_SIZE - pending);
a672b469 495 if (len > 0) {
0046c45b 496 f->buf_size += len;
a672b469 497 f->buf_offset += len;
fa39a30f
JQ
498 } else if (len == 0) {
499 f->last_error = -EIO;
a672b469 500 } else if (len != -EAGAIN)
c29110d5 501 qemu_file_set_error(f, len);
a672b469
AL
502}
503
d82ca915
EH
504/** Calls close function and set last_error if needed
505 *
506 * Internal function. qemu_fflush() must be called before this.
507 *
508 * Returns f->close() return value, or 0 if close function is not set.
509 */
510static int qemu_close(QEMUFile *f)
a672b469
AL
511{
512 int ret = 0;
d82ca915 513 if (f->close) {
a672b469 514 ret = f->close(f->opaque);
d82ca915
EH
515 qemu_file_set_if_error(f, ret);
516 }
517 return ret;
518}
519
520/** Closes the file
521 *
522 * Returns negative error value if any error happened on previous operations or
523 * while closing the file. Returns 0 or positive number on success.
524 *
525 * The meaning of return value on success depends on the specific backend
526 * being used.
527 */
528int qemu_fclose(QEMUFile *f)
529{
530 int ret;
531 qemu_fflush(f);
532 ret = qemu_close(f);
533 /* If any error was spotted before closing, we should report it
534 * instead of the close() return value.
535 */
536 if (f->last_error) {
537 ret = f->last_error;
538 }
7267c094 539 g_free(f);
a672b469
AL
540 return ret;
541}
542
543void qemu_file_put_notify(QEMUFile *f)
544{
545 f->put_buffer(f->opaque, NULL, 0, 0);
546}
547
548void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size)
549{
550 int l;
551
3961b4dd 552 if (!f->last_error && f->is_write == 0 && f->buf_index > 0) {
a672b469
AL
553 fprintf(stderr,
554 "Attempted to write to buffer while read buffer is not empty\n");
555 abort();
556 }
557
3961b4dd 558 while (!f->last_error && size > 0) {
a672b469
AL
559 l = IO_BUF_SIZE - f->buf_index;
560 if (l > size)
561 l = size;
562 memcpy(f->buf + f->buf_index, buf, l);
563 f->is_write = 1;
564 f->buf_index += l;
565 buf += l;
566 size -= l;
567 if (f->buf_index >= IO_BUF_SIZE)
568 qemu_fflush(f);
569 }
570}
571
572void qemu_put_byte(QEMUFile *f, int v)
573{
3961b4dd 574 if (!f->last_error && f->is_write == 0 && f->buf_index > 0) {
a672b469
AL
575 fprintf(stderr,
576 "Attempted to write to buffer while read buffer is not empty\n");
577 abort();
578 }
579
580 f->buf[f->buf_index++] = v;
581 f->is_write = 1;
582 if (f->buf_index >= IO_BUF_SIZE)
583 qemu_fflush(f);
584}
585
c6380724 586static void qemu_file_skip(QEMUFile *f, int size)
a672b469 587{
c6380724
JQ
588 if (f->buf_index + size <= f->buf_size) {
589 f->buf_index += size;
590 }
591}
592
593static int qemu_peek_buffer(QEMUFile *f, uint8_t *buf, int size, size_t offset)
a672b469 594{
c6380724
JQ
595 int pending;
596 int index;
a672b469 597
b9ce1454 598 if (f->is_write) {
a672b469 599 abort();
b9ce1454 600 }
a672b469 601
c6380724
JQ
602 index = f->buf_index + offset;
603 pending = f->buf_size - index;
604 if (pending < size) {
605 qemu_fill_buffer(f);
606 index = f->buf_index + offset;
607 pending = f->buf_size - index;
608 }
609
610 if (pending <= 0) {
611 return 0;
612 }
613 if (size > pending) {
614 size = pending;
615 }
616
617 memcpy(buf, f->buf + index, size);
618 return size;
619}
620
621int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size)
622{
623 int pending = size;
624 int done = 0;
625
626 while (pending > 0) {
627 int res;
628
629 res = qemu_peek_buffer(f, buf, pending, 0);
630 if (res == 0) {
631 return done;
a672b469 632 }
c6380724
JQ
633 qemu_file_skip(f, res);
634 buf += res;
635 pending -= res;
636 done += res;
a672b469 637 }
c6380724 638 return done;
a672b469
AL
639}
640
c6380724 641static int qemu_peek_byte(QEMUFile *f, int offset)
811814bd 642{
c6380724
JQ
643 int index = f->buf_index + offset;
644
b9ce1454 645 if (f->is_write) {
811814bd 646 abort();
b9ce1454 647 }
811814bd 648
c6380724 649 if (index >= f->buf_size) {
811814bd 650 qemu_fill_buffer(f);
c6380724
JQ
651 index = f->buf_index + offset;
652 if (index >= f->buf_size) {
811814bd 653 return 0;
b9ce1454 654 }
811814bd 655 }
c6380724 656 return f->buf[index];
811814bd
JQ
657}
658
a672b469
AL
659int qemu_get_byte(QEMUFile *f)
660{
65f3bb3d 661 int result;
a672b469 662
c6380724
JQ
663 result = qemu_peek_byte(f, 0);
664 qemu_file_skip(f, 1);
65f3bb3d 665 return result;
a672b469
AL
666}
667
668int64_t qemu_ftell(QEMUFile *f)
669{
670 return f->buf_offset - f->buf_size + f->buf_index;
671}
672
673int64_t qemu_fseek(QEMUFile *f, int64_t pos, int whence)
674{
675 if (whence == SEEK_SET) {
676 /* nothing to do */
677 } else if (whence == SEEK_CUR) {
678 pos += qemu_ftell(f);
679 } else {
680 /* SEEK_END not supported */
681 return -1;
682 }
683 if (f->put_buffer) {
684 qemu_fflush(f);
685 f->buf_offset = pos;
686 } else {
687 f->buf_offset = pos;
688 f->buf_index = 0;
689 f->buf_size = 0;
690 }
691 return pos;
692}
693
694int qemu_file_rate_limit(QEMUFile *f)
695{
696 if (f->rate_limit)
697 return f->rate_limit(f->opaque);
698
699 return 0;
700}
701
3d002df3 702int64_t qemu_file_get_rate_limit(QEMUFile *f)
c163b5ca
LS
703{
704 if (f->get_rate_limit)
705 return f->get_rate_limit(f->opaque);
706
707 return 0;
708}
709
3d002df3 710int64_t qemu_file_set_rate_limit(QEMUFile *f, int64_t new_rate)
19629537 711{
0bb05eaf
GC
712 /* any failed or completed migration keeps its state to allow probing of
713 * migration data, but has no associated file anymore */
714 if (f && f->set_rate_limit)
19629537
GC
715 return f->set_rate_limit(f->opaque, new_rate);
716
717 return 0;
718}
719
a672b469
AL
720void qemu_put_be16(QEMUFile *f, unsigned int v)
721{
722 qemu_put_byte(f, v >> 8);
723 qemu_put_byte(f, v);
724}
725
726void qemu_put_be32(QEMUFile *f, unsigned int v)
727{
728 qemu_put_byte(f, v >> 24);
729 qemu_put_byte(f, v >> 16);
730 qemu_put_byte(f, v >> 8);
731 qemu_put_byte(f, v);
732}
733
734void qemu_put_be64(QEMUFile *f, uint64_t v)
735{
736 qemu_put_be32(f, v >> 32);
737 qemu_put_be32(f, v);
738}
739
740unsigned int qemu_get_be16(QEMUFile *f)
741{
742 unsigned int v;
743 v = qemu_get_byte(f) << 8;
744 v |= qemu_get_byte(f);
745 return v;
746}
747
748unsigned int qemu_get_be32(QEMUFile *f)
749{
750 unsigned int v;
751 v = qemu_get_byte(f) << 24;
752 v |= qemu_get_byte(f) << 16;
753 v |= qemu_get_byte(f) << 8;
754 v |= qemu_get_byte(f);
755 return v;
756}
757
758uint64_t qemu_get_be64(QEMUFile *f)
759{
760 uint64_t v;
761 v = (uint64_t)qemu_get_be32(f) << 32;
762 v |= qemu_get_be32(f);
763 return v;
764}
765
2ff68d07
PB
766
767/* timer */
768
769void qemu_put_timer(QEMUFile *f, QEMUTimer *ts)
770{
771 uint64_t expire_time;
772
773 expire_time = qemu_timer_expire_time_ns(ts);
774 qemu_put_be64(f, expire_time);
775}
776
777void qemu_get_timer(QEMUFile *f, QEMUTimer *ts)
778{
779 uint64_t expire_time;
780
781 expire_time = qemu_get_be64(f);
782 if (expire_time != -1) {
783 qemu_mod_timer_ns(ts, expire_time);
784 } else {
785 qemu_del_timer(ts);
786 }
787}
788
789
cdae5cfb
GH
790/* bool */
791
792static int get_bool(QEMUFile *f, void *pv, size_t size)
793{
794 bool *v = pv;
795 *v = qemu_get_byte(f);
796 return 0;
797}
798
799static void put_bool(QEMUFile *f, void *pv, size_t size)
800{
801 bool *v = pv;
802 qemu_put_byte(f, *v);
803}
804
805const VMStateInfo vmstate_info_bool = {
806 .name = "bool",
807 .get = get_bool,
808 .put = put_bool,
809};
810
9ed7d6ae
JQ
811/* 8 bit int */
812
813static int get_int8(QEMUFile *f, void *pv, size_t size)
814{
815 int8_t *v = pv;
816 qemu_get_s8s(f, v);
817 return 0;
818}
819
84e2e3eb 820static void put_int8(QEMUFile *f, void *pv, size_t size)
9ed7d6ae 821{
84e2e3eb 822 int8_t *v = pv;
9ed7d6ae
JQ
823 qemu_put_s8s(f, v);
824}
825
826const VMStateInfo vmstate_info_int8 = {
827 .name = "int8",
828 .get = get_int8,
829 .put = put_int8,
830};
831
832/* 16 bit int */
833
834static int get_int16(QEMUFile *f, void *pv, size_t size)
835{
836 int16_t *v = pv;
837 qemu_get_sbe16s(f, v);
838 return 0;
839}
840
84e2e3eb 841static void put_int16(QEMUFile *f, void *pv, size_t size)
9ed7d6ae 842{
84e2e3eb 843 int16_t *v = pv;
9ed7d6ae
JQ
844 qemu_put_sbe16s(f, v);
845}
846
847const VMStateInfo vmstate_info_int16 = {
848 .name = "int16",
849 .get = get_int16,
850 .put = put_int16,
851};
852
853/* 32 bit int */
854
855static int get_int32(QEMUFile *f, void *pv, size_t size)
856{
857 int32_t *v = pv;
858 qemu_get_sbe32s(f, v);
859 return 0;
860}
861
84e2e3eb 862static void put_int32(QEMUFile *f, void *pv, size_t size)
9ed7d6ae 863{
84e2e3eb 864 int32_t *v = pv;
9ed7d6ae
JQ
865 qemu_put_sbe32s(f, v);
866}
867
868const VMStateInfo vmstate_info_int32 = {
869 .name = "int32",
870 .get = get_int32,
871 .put = put_int32,
872};
873
82501660
JQ
874/* 32 bit int. See that the received value is the same than the one
875 in the field */
876
877static int get_int32_equal(QEMUFile *f, void *pv, size_t size)
878{
879 int32_t *v = pv;
880 int32_t v2;
881 qemu_get_sbe32s(f, &v2);
882
883 if (*v == v2)
884 return 0;
885 return -EINVAL;
886}
887
888const VMStateInfo vmstate_info_int32_equal = {
889 .name = "int32 equal",
890 .get = get_int32_equal,
891 .put = put_int32,
892};
893
0a031e0a
JQ
894/* 32 bit int. See that the received value is the less or the same
895 than the one in the field */
896
897static int get_int32_le(QEMUFile *f, void *pv, size_t size)
898{
899 int32_t *old = pv;
900 int32_t new;
901 qemu_get_sbe32s(f, &new);
902
903 if (*old <= new)
904 return 0;
905 return -EINVAL;
906}
907
908const VMStateInfo vmstate_info_int32_le = {
909 .name = "int32 equal",
910 .get = get_int32_le,
911 .put = put_int32,
912};
913
9ed7d6ae
JQ
914/* 64 bit int */
915
916static int get_int64(QEMUFile *f, void *pv, size_t size)
917{
918 int64_t *v = pv;
919 qemu_get_sbe64s(f, v);
920 return 0;
921}
922
84e2e3eb 923static void put_int64(QEMUFile *f, void *pv, size_t size)
9ed7d6ae 924{
84e2e3eb 925 int64_t *v = pv;
9ed7d6ae
JQ
926 qemu_put_sbe64s(f, v);
927}
928
929const VMStateInfo vmstate_info_int64 = {
930 .name = "int64",
931 .get = get_int64,
932 .put = put_int64,
933};
934
935/* 8 bit unsigned int */
936
937static int get_uint8(QEMUFile *f, void *pv, size_t size)
938{
939 uint8_t *v = pv;
940 qemu_get_8s(f, v);
941 return 0;
942}
943
84e2e3eb 944static void put_uint8(QEMUFile *f, void *pv, size_t size)
9ed7d6ae 945{
84e2e3eb 946 uint8_t *v = pv;
9ed7d6ae
JQ
947 qemu_put_8s(f, v);
948}
949
950const VMStateInfo vmstate_info_uint8 = {
951 .name = "uint8",
952 .get = get_uint8,
953 .put = put_uint8,
954};
955
956/* 16 bit unsigned int */
957
958static int get_uint16(QEMUFile *f, void *pv, size_t size)
959{
960 uint16_t *v = pv;
961 qemu_get_be16s(f, v);
962 return 0;
963}
964
84e2e3eb 965static void put_uint16(QEMUFile *f, void *pv, size_t size)
9ed7d6ae 966{
84e2e3eb 967 uint16_t *v = pv;
9ed7d6ae
JQ
968 qemu_put_be16s(f, v);
969}
970
971const VMStateInfo vmstate_info_uint16 = {
972 .name = "uint16",
973 .get = get_uint16,
974 .put = put_uint16,
975};
976
977/* 32 bit unsigned int */
978
979static int get_uint32(QEMUFile *f, void *pv, size_t size)
980{
981 uint32_t *v = pv;
982 qemu_get_be32s(f, v);
983 return 0;
984}
985
84e2e3eb 986static void put_uint32(QEMUFile *f, void *pv, size_t size)
9ed7d6ae 987{
84e2e3eb 988 uint32_t *v = pv;
9ed7d6ae
JQ
989 qemu_put_be32s(f, v);
990}
991
992const VMStateInfo vmstate_info_uint32 = {
993 .name = "uint32",
994 .get = get_uint32,
995 .put = put_uint32,
996};
997
9122a8fe
JQ
998/* 32 bit uint. See that the received value is the same than the one
999 in the field */
1000
1001static int get_uint32_equal(QEMUFile *f, void *pv, size_t size)
1002{
1003 uint32_t *v = pv;
1004 uint32_t v2;
1005 qemu_get_be32s(f, &v2);
1006
1007 if (*v == v2) {
1008 return 0;
1009 }
1010 return -EINVAL;
1011}
1012
1013const VMStateInfo vmstate_info_uint32_equal = {
1014 .name = "uint32 equal",
1015 .get = get_uint32_equal,
1016 .put = put_uint32,
1017};
1018
9ed7d6ae
JQ
1019/* 64 bit unsigned int */
1020
1021static int get_uint64(QEMUFile *f, void *pv, size_t size)
1022{
1023 uint64_t *v = pv;
1024 qemu_get_be64s(f, v);
1025 return 0;
1026}
1027
84e2e3eb 1028static void put_uint64(QEMUFile *f, void *pv, size_t size)
9ed7d6ae 1029{
84e2e3eb 1030 uint64_t *v = pv;
9ed7d6ae
JQ
1031 qemu_put_be64s(f, v);
1032}
1033
1034const VMStateInfo vmstate_info_uint64 = {
1035 .name = "uint64",
1036 .get = get_uint64,
1037 .put = put_uint64,
1038};
1039
80cd83e7
JQ
1040/* 8 bit int. See that the received value is the same than the one
1041 in the field */
1042
1043static int get_uint8_equal(QEMUFile *f, void *pv, size_t size)
1044{
1045 uint8_t *v = pv;
1046 uint8_t v2;
1047 qemu_get_8s(f, &v2);
1048
1049 if (*v == v2)
1050 return 0;
1051 return -EINVAL;
1052}
1053
1054const VMStateInfo vmstate_info_uint8_equal = {
aa1cce69 1055 .name = "uint8 equal",
80cd83e7
JQ
1056 .get = get_uint8_equal,
1057 .put = put_uint8,
1058};
1059
dc3b83a0
JQ
1060/* 16 bit unsigned int int. See that the received value is the same than the one
1061 in the field */
1062
1063static int get_uint16_equal(QEMUFile *f, void *pv, size_t size)
1064{
1065 uint16_t *v = pv;
1066 uint16_t v2;
1067 qemu_get_be16s(f, &v2);
1068
1069 if (*v == v2)
1070 return 0;
1071 return -EINVAL;
1072}
1073
1074const VMStateInfo vmstate_info_uint16_equal = {
1075 .name = "uint16 equal",
1076 .get = get_uint16_equal,
1077 .put = put_uint16,
1078};
1079
dde0463b
JQ
1080/* timers */
1081
1082static int get_timer(QEMUFile *f, void *pv, size_t size)
1083{
1084 QEMUTimer *v = pv;
1085 qemu_get_timer(f, v);
1086 return 0;
1087}
1088
84e2e3eb 1089static void put_timer(QEMUFile *f, void *pv, size_t size)
dde0463b 1090{
84e2e3eb 1091 QEMUTimer *v = pv;
dde0463b
JQ
1092 qemu_put_timer(f, v);
1093}
1094
1095const VMStateInfo vmstate_info_timer = {
1096 .name = "timer",
1097 .get = get_timer,
1098 .put = put_timer,
1099};
1100
6f67c50f
JQ
1101/* uint8_t buffers */
1102
1103static int get_buffer(QEMUFile *f, void *pv, size_t size)
1104{
1105 uint8_t *v = pv;
1106 qemu_get_buffer(f, v, size);
1107 return 0;
1108}
1109
84e2e3eb 1110static void put_buffer(QEMUFile *f, void *pv, size_t size)
6f67c50f 1111{
84e2e3eb 1112 uint8_t *v = pv;
6f67c50f
JQ
1113 qemu_put_buffer(f, v, size);
1114}
1115
1116const VMStateInfo vmstate_info_buffer = {
1117 .name = "buffer",
1118 .get = get_buffer,
1119 .put = put_buffer,
1120};
1121
76507c75 1122/* unused buffers: space that was used for some fields that are
61cc8701 1123 not useful anymore */
76507c75
JQ
1124
1125static int get_unused_buffer(QEMUFile *f, void *pv, size_t size)
1126{
21174c34
JK
1127 uint8_t buf[1024];
1128 int block_len;
1129
1130 while (size > 0) {
1131 block_len = MIN(sizeof(buf), size);
1132 size -= block_len;
1133 qemu_get_buffer(f, buf, block_len);
1134 }
1135 return 0;
76507c75
JQ
1136}
1137
1138static void put_unused_buffer(QEMUFile *f, void *pv, size_t size)
1139{
21174c34
JK
1140 static const uint8_t buf[1024];
1141 int block_len;
1142
1143 while (size > 0) {
1144 block_len = MIN(sizeof(buf), size);
1145 size -= block_len;
1146 qemu_put_buffer(f, buf, block_len);
1147 }
76507c75
JQ
1148}
1149
1150const VMStateInfo vmstate_info_unused_buffer = {
1151 .name = "unused_buffer",
1152 .get = get_unused_buffer,
1153 .put = put_unused_buffer,
1154};
1155
7685ee6a
AW
1156typedef struct CompatEntry {
1157 char idstr[256];
1158 int instance_id;
1159} CompatEntry;
1160
a672b469 1161typedef struct SaveStateEntry {
72cf2d4f 1162 QTAILQ_ENTRY(SaveStateEntry) entry;
a672b469
AL
1163 char idstr[256];
1164 int instance_id;
4d2ffa08 1165 int alias_id;
a672b469
AL
1166 int version_id;
1167 int section_id;
c163b5ca 1168 SaveSetParamsHandler *set_params;
a672b469
AL
1169 SaveLiveStateHandler *save_live_state;
1170 SaveStateHandler *save_state;
1171 LoadStateHandler *load_state;
9ed7d6ae 1172 const VMStateDescription *vmsd;
a672b469 1173 void *opaque;
7685ee6a 1174 CompatEntry *compat;
24312968 1175 int no_migrate;
a672b469
AL
1176} SaveStateEntry;
1177
c163b5ca 1178
72cf2d4f
BS
1179static QTAILQ_HEAD(savevm_handlers, SaveStateEntry) savevm_handlers =
1180 QTAILQ_HEAD_INITIALIZER(savevm_handlers);
9ed7d6ae 1181static int global_section_id;
a672b469 1182
8718e999
JQ
1183static int calculate_new_instance_id(const char *idstr)
1184{
1185 SaveStateEntry *se;
1186 int instance_id = 0;
1187
72cf2d4f 1188 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
8718e999
JQ
1189 if (strcmp(idstr, se->idstr) == 0
1190 && instance_id <= se->instance_id) {
1191 instance_id = se->instance_id + 1;
1192 }
1193 }
1194 return instance_id;
1195}
1196
7685ee6a
AW
1197static int calculate_compat_instance_id(const char *idstr)
1198{
1199 SaveStateEntry *se;
1200 int instance_id = 0;
1201
1202 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1203 if (!se->compat)
1204 continue;
1205
1206 if (strcmp(idstr, se->compat->idstr) == 0
1207 && instance_id <= se->compat->instance_id) {
1208 instance_id = se->compat->instance_id + 1;
1209 }
1210 }
1211 return instance_id;
1212}
1213
a672b469
AL
1214/* TODO: Individual devices generally have very little idea about the rest
1215 of the system, so instance_id should be removed/replaced.
1216 Meanwhile pass -1 as instance_id if you do not already have a clearly
1217 distinguishing id for all instances of your device class. */
0be71e32
AW
1218int register_savevm_live(DeviceState *dev,
1219 const char *idstr,
a672b469
AL
1220 int instance_id,
1221 int version_id,
c163b5ca 1222 SaveSetParamsHandler *set_params,
a672b469
AL
1223 SaveLiveStateHandler *save_live_state,
1224 SaveStateHandler *save_state,
1225 LoadStateHandler *load_state,
1226 void *opaque)
1227{
8718e999 1228 SaveStateEntry *se;
a672b469 1229
7267c094 1230 se = g_malloc0(sizeof(SaveStateEntry));
a672b469
AL
1231 se->version_id = version_id;
1232 se->section_id = global_section_id++;
c163b5ca 1233 se->set_params = set_params;
a672b469
AL
1234 se->save_live_state = save_live_state;
1235 se->save_state = save_state;
1236 se->load_state = load_state;
1237 se->opaque = opaque;
9ed7d6ae 1238 se->vmsd = NULL;
24312968 1239 se->no_migrate = 0;
a672b469 1240
7685ee6a
AW
1241 if (dev && dev->parent_bus && dev->parent_bus->info->get_dev_path) {
1242 char *id = dev->parent_bus->info->get_dev_path(dev);
1243 if (id) {
1244 pstrcpy(se->idstr, sizeof(se->idstr), id);
1245 pstrcat(se->idstr, sizeof(se->idstr), "/");
7267c094 1246 g_free(id);
7685ee6a 1247
7267c094 1248 se->compat = g_malloc0(sizeof(CompatEntry));
7685ee6a
AW
1249 pstrcpy(se->compat->idstr, sizeof(se->compat->idstr), idstr);
1250 se->compat->instance_id = instance_id == -1 ?
1251 calculate_compat_instance_id(idstr) : instance_id;
1252 instance_id = -1;
1253 }
1254 }
1255 pstrcat(se->idstr, sizeof(se->idstr), idstr);
1256
8718e999 1257 if (instance_id == -1) {
7685ee6a 1258 se->instance_id = calculate_new_instance_id(se->idstr);
8718e999
JQ
1259 } else {
1260 se->instance_id = instance_id;
a672b469 1261 }
7685ee6a 1262 assert(!se->compat || se->instance_id == 0);
8718e999 1263 /* add at the end of list */
72cf2d4f 1264 QTAILQ_INSERT_TAIL(&savevm_handlers, se, entry);
a672b469
AL
1265 return 0;
1266}
1267
0be71e32
AW
1268int register_savevm(DeviceState *dev,
1269 const char *idstr,
a672b469
AL
1270 int instance_id,
1271 int version_id,
1272 SaveStateHandler *save_state,
1273 LoadStateHandler *load_state,
1274 void *opaque)
1275{
0be71e32 1276 return register_savevm_live(dev, idstr, instance_id, version_id,
c163b5ca 1277 NULL, NULL, save_state, load_state, opaque);
a672b469
AL
1278}
1279
0be71e32 1280void unregister_savevm(DeviceState *dev, const char *idstr, void *opaque)
41bd13af 1281{
8718e999 1282 SaveStateEntry *se, *new_se;
7685ee6a
AW
1283 char id[256] = "";
1284
1285 if (dev && dev->parent_bus && dev->parent_bus->info->get_dev_path) {
1286 char *path = dev->parent_bus->info->get_dev_path(dev);
1287 if (path) {
1288 pstrcpy(id, sizeof(id), path);
1289 pstrcat(id, sizeof(id), "/");
7267c094 1290 g_free(path);
7685ee6a
AW
1291 }
1292 }
1293 pstrcat(id, sizeof(id), idstr);
41bd13af 1294
72cf2d4f 1295 QTAILQ_FOREACH_SAFE(se, &savevm_handlers, entry, new_se) {
7685ee6a 1296 if (strcmp(se->idstr, id) == 0 && se->opaque == opaque) {
72cf2d4f 1297 QTAILQ_REMOVE(&savevm_handlers, se, entry);
69e58af9 1298 if (se->compat) {
7267c094 1299 g_free(se->compat);
69e58af9 1300 }
7267c094 1301 g_free(se);
41bd13af 1302 }
41bd13af
AL
1303 }
1304}
1305
0be71e32 1306int vmstate_register_with_alias_id(DeviceState *dev, int instance_id,
4d2ffa08
JK
1307 const VMStateDescription *vmsd,
1308 void *opaque, int alias_id,
1309 int required_for_version)
9ed7d6ae 1310{
8718e999 1311 SaveStateEntry *se;
9ed7d6ae 1312
4d2ffa08
JK
1313 /* If this triggers, alias support can be dropped for the vmsd. */
1314 assert(alias_id == -1 || required_for_version >= vmsd->minimum_version_id);
1315
7267c094 1316 se = g_malloc0(sizeof(SaveStateEntry));
9ed7d6ae
JQ
1317 se->version_id = vmsd->version_id;
1318 se->section_id = global_section_id++;
1319 se->save_live_state = NULL;
1320 se->save_state = NULL;
1321 se->load_state = NULL;
1322 se->opaque = opaque;
1323 se->vmsd = vmsd;
4d2ffa08 1324 se->alias_id = alias_id;
2837c8ea 1325 se->no_migrate = vmsd->unmigratable;
9ed7d6ae 1326
7685ee6a
AW
1327 if (dev && dev->parent_bus && dev->parent_bus->info->get_dev_path) {
1328 char *id = dev->parent_bus->info->get_dev_path(dev);
1329 if (id) {
1330 pstrcpy(se->idstr, sizeof(se->idstr), id);
1331 pstrcat(se->idstr, sizeof(se->idstr), "/");
7267c094 1332 g_free(id);
7685ee6a 1333
7267c094 1334 se->compat = g_malloc0(sizeof(CompatEntry));
7685ee6a
AW
1335 pstrcpy(se->compat->idstr, sizeof(se->compat->idstr), vmsd->name);
1336 se->compat->instance_id = instance_id == -1 ?
1337 calculate_compat_instance_id(vmsd->name) : instance_id;
1338 instance_id = -1;
1339 }
1340 }
1341 pstrcat(se->idstr, sizeof(se->idstr), vmsd->name);
1342
8718e999 1343 if (instance_id == -1) {
7685ee6a 1344 se->instance_id = calculate_new_instance_id(se->idstr);
8718e999
JQ
1345 } else {
1346 se->instance_id = instance_id;
9ed7d6ae 1347 }
7685ee6a 1348 assert(!se->compat || se->instance_id == 0);
8718e999 1349 /* add at the end of list */
72cf2d4f 1350 QTAILQ_INSERT_TAIL(&savevm_handlers, se, entry);
9ed7d6ae
JQ
1351 return 0;
1352}
1353
0be71e32
AW
1354int vmstate_register(DeviceState *dev, int instance_id,
1355 const VMStateDescription *vmsd, void *opaque)
4d2ffa08 1356{
0be71e32
AW
1357 return vmstate_register_with_alias_id(dev, instance_id, vmsd,
1358 opaque, -1, 0);
4d2ffa08
JK
1359}
1360
0be71e32
AW
1361void vmstate_unregister(DeviceState *dev, const VMStateDescription *vmsd,
1362 void *opaque)
9ed7d6ae 1363{
1eb7538b
JQ
1364 SaveStateEntry *se, *new_se;
1365
72cf2d4f 1366 QTAILQ_FOREACH_SAFE(se, &savevm_handlers, entry, new_se) {
1eb7538b 1367 if (se->vmsd == vmsd && se->opaque == opaque) {
72cf2d4f 1368 QTAILQ_REMOVE(&savevm_handlers, se, entry);
69e58af9 1369 if (se->compat) {
7267c094 1370 g_free(se->compat);
69e58af9 1371 }
7267c094 1372 g_free(se);
1eb7538b
JQ
1373 }
1374 }
9ed7d6ae
JQ
1375}
1376
811814bd
JQ
1377static void vmstate_subsection_save(QEMUFile *f, const VMStateDescription *vmsd,
1378 void *opaque);
1379static int vmstate_subsection_load(QEMUFile *f, const VMStateDescription *vmsd,
1380 void *opaque);
1381
9ed7d6ae
JQ
1382int vmstate_load_state(QEMUFile *f, const VMStateDescription *vmsd,
1383 void *opaque, int version_id)
1384{
1385 VMStateField *field = vmsd->fields;
811814bd 1386 int ret;
9ed7d6ae
JQ
1387
1388 if (version_id > vmsd->version_id) {
1389 return -EINVAL;
1390 }
1391 if (version_id < vmsd->minimum_version_id_old) {
1392 return -EINVAL;
1393 }
1394 if (version_id < vmsd->minimum_version_id) {
1395 return vmsd->load_state_old(f, opaque, version_id);
1396 }
fd4d52de
JQ
1397 if (vmsd->pre_load) {
1398 int ret = vmsd->pre_load(opaque);
1399 if (ret)
1400 return ret;
1401 }
9ed7d6ae 1402 while(field->name) {
f11f6a5f
JQ
1403 if ((field->field_exists &&
1404 field->field_exists(opaque, version_id)) ||
1405 (!field->field_exists &&
1406 field->version_id <= version_id)) {
f752a6aa 1407 void *base_addr = opaque + field->offset;
811814bd 1408 int i, n_elems = 1;
e61a1e0a 1409 int size = field->size;
9ed7d6ae 1410
e61a1e0a
JQ
1411 if (field->flags & VMS_VBUFFER) {
1412 size = *(int32_t *)(opaque+field->size_offset);
33599e2a
JQ
1413 if (field->flags & VMS_MULTIPLY) {
1414 size *= field->size;
1415 }
e61a1e0a 1416 }
f752a6aa
JQ
1417 if (field->flags & VMS_ARRAY) {
1418 n_elems = field->num;
d6698281
JQ
1419 } else if (field->flags & VMS_VARRAY_INT32) {
1420 n_elems = *(int32_t *)(opaque+field->num_offset);
a624b086
JQ
1421 } else if (field->flags & VMS_VARRAY_UINT32) {
1422 n_elems = *(uint32_t *)(opaque+field->num_offset);
bdb4941d
JQ
1423 } else if (field->flags & VMS_VARRAY_UINT16) {
1424 n_elems = *(uint16_t *)(opaque+field->num_offset);
82fa39b7
JQ
1425 } else if (field->flags & VMS_VARRAY_UINT8) {
1426 n_elems = *(uint8_t *)(opaque+field->num_offset);
f752a6aa 1427 }
dde0463b 1428 if (field->flags & VMS_POINTER) {
e61a1e0a 1429 base_addr = *(void **)base_addr + field->start;
dde0463b 1430 }
f752a6aa 1431 for (i = 0; i < n_elems; i++) {
e61a1e0a 1432 void *addr = base_addr + size * i;
ec245e21 1433
19df438b
JQ
1434 if (field->flags & VMS_ARRAY_OF_POINTER) {
1435 addr = *(void **)addr;
1436 }
ec245e21 1437 if (field->flags & VMS_STRUCT) {
fa3aad24 1438 ret = vmstate_load_state(f, field->vmsd, addr, field->vmsd->version_id);
ec245e21 1439 } else {
e61a1e0a 1440 ret = field->info->get(f, addr, size);
ec245e21
JQ
1441
1442 }
f752a6aa
JQ
1443 if (ret < 0) {
1444 return ret;
1445 }
9ed7d6ae
JQ
1446 }
1447 }
1448 field++;
1449 }
811814bd
JQ
1450 ret = vmstate_subsection_load(f, vmsd, opaque);
1451 if (ret != 0) {
1452 return ret;
1453 }
752ff2fa 1454 if (vmsd->post_load) {
e59fb374 1455 return vmsd->post_load(opaque, version_id);
752ff2fa 1456 }
9ed7d6ae
JQ
1457 return 0;
1458}
1459
1460void vmstate_save_state(QEMUFile *f, const VMStateDescription *vmsd,
84e2e3eb 1461 void *opaque)
9ed7d6ae
JQ
1462{
1463 VMStateField *field = vmsd->fields;
1464
8fb0791d
JQ
1465 if (vmsd->pre_save) {
1466 vmsd->pre_save(opaque);
1467 }
9ed7d6ae 1468 while(field->name) {
f11f6a5f
JQ
1469 if (!field->field_exists ||
1470 field->field_exists(opaque, vmsd->version_id)) {
1471 void *base_addr = opaque + field->offset;
1472 int i, n_elems = 1;
e61a1e0a 1473 int size = field->size;
dde0463b 1474
e61a1e0a
JQ
1475 if (field->flags & VMS_VBUFFER) {
1476 size = *(int32_t *)(opaque+field->size_offset);
33599e2a
JQ
1477 if (field->flags & VMS_MULTIPLY) {
1478 size *= field->size;
1479 }
e61a1e0a 1480 }
f11f6a5f
JQ
1481 if (field->flags & VMS_ARRAY) {
1482 n_elems = field->num;
d6698281
JQ
1483 } else if (field->flags & VMS_VARRAY_INT32) {
1484 n_elems = *(int32_t *)(opaque+field->num_offset);
bdb4941d
JQ
1485 } else if (field->flags & VMS_VARRAY_UINT16) {
1486 n_elems = *(uint16_t *)(opaque+field->num_offset);
b784421c
JQ
1487 } else if (field->flags & VMS_VARRAY_UINT8) {
1488 n_elems = *(uint8_t *)(opaque+field->num_offset);
f11f6a5f
JQ
1489 }
1490 if (field->flags & VMS_POINTER) {
e61a1e0a 1491 base_addr = *(void **)base_addr + field->start;
f11f6a5f
JQ
1492 }
1493 for (i = 0; i < n_elems; i++) {
e61a1e0a 1494 void *addr = base_addr + size * i;
ec245e21 1495
8595387e
JQ
1496 if (field->flags & VMS_ARRAY_OF_POINTER) {
1497 addr = *(void **)addr;
1498 }
f11f6a5f
JQ
1499 if (field->flags & VMS_STRUCT) {
1500 vmstate_save_state(f, field->vmsd, addr);
1501 } else {
e61a1e0a 1502 field->info->put(f, addr, size);
f11f6a5f 1503 }
ec245e21 1504 }
dde0463b 1505 }
9ed7d6ae
JQ
1506 field++;
1507 }
811814bd 1508 vmstate_subsection_save(f, vmsd, opaque);
9ed7d6ae
JQ
1509}
1510
4082be4d
JQ
1511static int vmstate_load(QEMUFile *f, SaveStateEntry *se, int version_id)
1512{
9ed7d6ae
JQ
1513 if (!se->vmsd) { /* Old style */
1514 return se->load_state(f, se->opaque, version_id);
1515 }
1516 return vmstate_load_state(f, se->vmsd, se->opaque, version_id);
4082be4d
JQ
1517}
1518
dc912121 1519static void vmstate_save(QEMUFile *f, SaveStateEntry *se)
4082be4d 1520{
9ed7d6ae
JQ
1521 if (!se->vmsd) { /* Old style */
1522 se->save_state(f, se->opaque);
dc912121 1523 return;
9ed7d6ae
JQ
1524 }
1525 vmstate_save_state(f,se->vmsd, se->opaque);
4082be4d
JQ
1526}
1527
a672b469
AL
1528#define QEMU_VM_FILE_MAGIC 0x5145564d
1529#define QEMU_VM_FILE_VERSION_COMPAT 0x00000002
1530#define QEMU_VM_FILE_VERSION 0x00000003
1531
1532#define QEMU_VM_EOF 0x00
1533#define QEMU_VM_SECTION_START 0x01
1534#define QEMU_VM_SECTION_PART 0x02
1535#define QEMU_VM_SECTION_END 0x03
1536#define QEMU_VM_SECTION_FULL 0x04
811814bd 1537#define QEMU_VM_SUBSECTION 0x05
a672b469 1538
dc912121
AW
1539bool qemu_savevm_state_blocked(Monitor *mon)
1540{
1541 SaveStateEntry *se;
1542
1543 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1544 if (se->no_migrate) {
1545 monitor_printf(mon, "state blocked by non-migratable device '%s'\n",
1546 se->idstr);
1547 return true;
1548 }
1549 }
1550 return false;
1551}
1552
f327aa0c
JK
1553int qemu_savevm_state_begin(Monitor *mon, QEMUFile *f, int blk_enable,
1554 int shared)
a672b469
AL
1555{
1556 SaveStateEntry *se;
39346385 1557 int ret;
a672b469 1558
c163b5ca
LS
1559 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1560 if(se->set_params == NULL) {
1561 continue;
1562 }
1563 se->set_params(blk_enable, shared, se->opaque);
1564 }
1565
a672b469
AL
1566 qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
1567 qemu_put_be32(f, QEMU_VM_FILE_VERSION);
1568
72cf2d4f 1569 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
a672b469
AL
1570 int len;
1571
1572 if (se->save_live_state == NULL)
1573 continue;
1574
1575 /* Section type */
1576 qemu_put_byte(f, QEMU_VM_SECTION_START);
1577 qemu_put_be32(f, se->section_id);
1578
1579 /* ID string */
1580 len = strlen(se->idstr);
1581 qemu_put_byte(f, len);
1582 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
1583
1584 qemu_put_be32(f, se->instance_id);
1585 qemu_put_be32(f, se->version_id);
1586
2975725f
JQ
1587 ret = se->save_live_state(mon, f, QEMU_VM_SECTION_START, se->opaque);
1588 if (ret < 0) {
1589 qemu_savevm_state_cancel(mon, f);
1590 return ret;
1591 }
a672b469 1592 }
624b9cc2 1593 ret = qemu_file_get_error(f);
39346385 1594 if (ret != 0) {
f327aa0c 1595 qemu_savevm_state_cancel(mon, f);
4ec7fcc7 1596 }
a672b469 1597
39346385
JQ
1598 return ret;
1599
a672b469
AL
1600}
1601
39346385 1602/*
07f35073 1603 * this function has three return values:
39346385
JQ
1604 * negative: there was one error, and we have -errno.
1605 * 0 : We haven't finished, caller have to go again
1606 * 1 : We have finished, we can go to complete phase
1607 */
f327aa0c 1608int qemu_savevm_state_iterate(Monitor *mon, QEMUFile *f)
a672b469
AL
1609{
1610 SaveStateEntry *se;
1611 int ret = 1;
1612
72cf2d4f 1613 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
a672b469
AL
1614 if (se->save_live_state == NULL)
1615 continue;
1616
1617 /* Section type */
1618 qemu_put_byte(f, QEMU_VM_SECTION_PART);
1619 qemu_put_be32(f, se->section_id);
1620
90697be8 1621 ret = se->save_live_state(mon, f, QEMU_VM_SECTION_PART, se->opaque);
2975725f 1622 if (ret <= 0) {
90697be8
JK
1623 /* Do not proceed to the next vmstate before this one reported
1624 completion of the current stage. This serializes the migration
1625 and reduces the probability that a faster changing state is
1626 synchronized over and over again. */
1627 break;
1628 }
a672b469 1629 }
39346385
JQ
1630 if (ret != 0) {
1631 return ret;
1632 }
624b9cc2 1633 ret = qemu_file_get_error(f);
39346385 1634 if (ret != 0) {
f327aa0c 1635 qemu_savevm_state_cancel(mon, f);
4ec7fcc7 1636 }
39346385 1637 return ret;
a672b469
AL
1638}
1639
f327aa0c 1640int qemu_savevm_state_complete(Monitor *mon, QEMUFile *f)
a672b469
AL
1641{
1642 SaveStateEntry *se;
2975725f 1643 int ret;
a672b469 1644
ea375f9a
JK
1645 cpu_synchronize_all_states();
1646
72cf2d4f 1647 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
a672b469
AL
1648 if (se->save_live_state == NULL)
1649 continue;
1650
1651 /* Section type */
1652 qemu_put_byte(f, QEMU_VM_SECTION_END);
1653 qemu_put_be32(f, se->section_id);
1654
2975725f
JQ
1655 ret = se->save_live_state(mon, f, QEMU_VM_SECTION_END, se->opaque);
1656 if (ret < 0) {
1657 return ret;
1658 }
a672b469
AL
1659 }
1660
72cf2d4f 1661 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
a672b469
AL
1662 int len;
1663
9ed7d6ae 1664 if (se->save_state == NULL && se->vmsd == NULL)
a672b469
AL
1665 continue;
1666
1667 /* Section type */
1668 qemu_put_byte(f, QEMU_VM_SECTION_FULL);
1669 qemu_put_be32(f, se->section_id);
1670
1671 /* ID string */
1672 len = strlen(se->idstr);
1673 qemu_put_byte(f, len);
1674 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
1675
1676 qemu_put_be32(f, se->instance_id);
1677 qemu_put_be32(f, se->version_id);
1678
dc912121 1679 vmstate_save(f, se);
a672b469
AL
1680 }
1681
1682 qemu_put_byte(f, QEMU_VM_EOF);
1683
624b9cc2 1684 return qemu_file_get_error(f);
a672b469
AL
1685}
1686
f327aa0c 1687void qemu_savevm_state_cancel(Monitor *mon, QEMUFile *f)
4ec7fcc7
JK
1688{
1689 SaveStateEntry *se;
1690
1691 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1692 if (se->save_live_state) {
f327aa0c 1693 se->save_live_state(mon, f, -1, se->opaque);
4ec7fcc7
JK
1694 }
1695 }
1696}
1697
f327aa0c 1698static int qemu_savevm_state(Monitor *mon, QEMUFile *f)
a672b469 1699{
a672b469
AL
1700 int ret;
1701
dc912121
AW
1702 if (qemu_savevm_state_blocked(mon)) {
1703 ret = -EINVAL;
1704 goto out;
1705 }
1706
f327aa0c 1707 ret = qemu_savevm_state_begin(mon, f, 0, 0);
a672b469
AL
1708 if (ret < 0)
1709 goto out;
1710
1711 do {
f327aa0c 1712 ret = qemu_savevm_state_iterate(mon, f);
a672b469
AL
1713 if (ret < 0)
1714 goto out;
1715 } while (ret == 0);
1716
f327aa0c 1717 ret = qemu_savevm_state_complete(mon, f);
a672b469
AL
1718
1719out:
39346385 1720 if (ret == 0) {
624b9cc2 1721 ret = qemu_file_get_error(f);
39346385 1722 }
a672b469 1723
a672b469
AL
1724 return ret;
1725}
1726
1727static SaveStateEntry *find_se(const char *idstr, int instance_id)
1728{
1729 SaveStateEntry *se;
1730
72cf2d4f 1731 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
a672b469 1732 if (!strcmp(se->idstr, idstr) &&
4d2ffa08
JK
1733 (instance_id == se->instance_id ||
1734 instance_id == se->alias_id))
a672b469 1735 return se;
7685ee6a
AW
1736 /* Migrating from an older version? */
1737 if (strstr(se->idstr, idstr) && se->compat) {
1738 if (!strcmp(se->compat->idstr, idstr) &&
1739 (instance_id == se->compat->instance_id ||
1740 instance_id == se->alias_id))
1741 return se;
1742 }
a672b469
AL
1743 }
1744 return NULL;
1745}
1746
811814bd
JQ
1747static const VMStateDescription *vmstate_get_subsection(const VMStateSubsection *sub, char *idstr)
1748{
1749 while(sub && sub->needed) {
1750 if (strcmp(idstr, sub->vmsd->name) == 0) {
1751 return sub->vmsd;
1752 }
1753 sub++;
1754 }
1755 return NULL;
1756}
1757
1758static int vmstate_subsection_load(QEMUFile *f, const VMStateDescription *vmsd,
1759 void *opaque)
1760{
c6380724 1761 while (qemu_peek_byte(f, 0) == QEMU_VM_SUBSECTION) {
811814bd
JQ
1762 char idstr[256];
1763 int ret;
c6380724 1764 uint8_t version_id, len, size;
811814bd
JQ
1765 const VMStateDescription *sub_vmsd;
1766
c6380724
JQ
1767 len = qemu_peek_byte(f, 1);
1768 if (len < strlen(vmsd->name) + 1) {
1769 /* subsection name has be be "section_name/a" */
1770 return 0;
1771 }
1772 size = qemu_peek_buffer(f, (uint8_t *)idstr, len, 2);
1773 if (size != len) {
1774 return 0;
1775 }
1776 idstr[size] = 0;
811814bd 1777
c6380724
JQ
1778 if (strncmp(vmsd->name, idstr, strlen(vmsd->name)) != 0) {
1779 /* it don't have a valid subsection name */
1780 return 0;
1781 }
3da9eebd 1782 sub_vmsd = vmstate_get_subsection(vmsd->subsections, idstr);
811814bd
JQ
1783 if (sub_vmsd == NULL) {
1784 return -ENOENT;
1785 }
c6380724
JQ
1786 qemu_file_skip(f, 1); /* subsection */
1787 qemu_file_skip(f, 1); /* len */
1788 qemu_file_skip(f, len); /* idstr */
1789 version_id = qemu_get_be32(f);
1790
811814bd
JQ
1791 ret = vmstate_load_state(f, sub_vmsd, opaque, version_id);
1792 if (ret) {
1793 return ret;
1794 }
1795 }
1796 return 0;
1797}
1798
1799static void vmstate_subsection_save(QEMUFile *f, const VMStateDescription *vmsd,
1800 void *opaque)
1801{
1802 const VMStateSubsection *sub = vmsd->subsections;
1803
1804 while (sub && sub->needed) {
1805 if (sub->needed(opaque)) {
1806 const VMStateDescription *vmsd = sub->vmsd;
1807 uint8_t len;
1808
1809 qemu_put_byte(f, QEMU_VM_SUBSECTION);
1810 len = strlen(vmsd->name);
1811 qemu_put_byte(f, len);
1812 qemu_put_buffer(f, (uint8_t *)vmsd->name, len);
1813 qemu_put_be32(f, vmsd->version_id);
1814 vmstate_save_state(f, vmsd, opaque);
1815 }
1816 sub++;
1817 }
1818}
1819
a672b469 1820typedef struct LoadStateEntry {
72cf2d4f 1821 QLIST_ENTRY(LoadStateEntry) entry;
a672b469
AL
1822 SaveStateEntry *se;
1823 int section_id;
1824 int version_id;
a672b469
AL
1825} LoadStateEntry;
1826
a672b469
AL
1827int qemu_loadvm_state(QEMUFile *f)
1828{
72cf2d4f
BS
1829 QLIST_HEAD(, LoadStateEntry) loadvm_handlers =
1830 QLIST_HEAD_INITIALIZER(loadvm_handlers);
f4dbb8dd 1831 LoadStateEntry *le, *new_le;
a672b469
AL
1832 uint8_t section_type;
1833 unsigned int v;
1834 int ret;
1835
dc912121
AW
1836 if (qemu_savevm_state_blocked(default_mon)) {
1837 return -EINVAL;
1838 }
1839
a672b469
AL
1840 v = qemu_get_be32(f);
1841 if (v != QEMU_VM_FILE_MAGIC)
1842 return -EINVAL;
1843
1844 v = qemu_get_be32(f);
bbfe1408
JQ
1845 if (v == QEMU_VM_FILE_VERSION_COMPAT) {
1846 fprintf(stderr, "SaveVM v2 format is obsolete and don't work anymore\n");
1847 return -ENOTSUP;
1848 }
a672b469
AL
1849 if (v != QEMU_VM_FILE_VERSION)
1850 return -ENOTSUP;
1851
1852 while ((section_type = qemu_get_byte(f)) != QEMU_VM_EOF) {
1853 uint32_t instance_id, version_id, section_id;
a672b469
AL
1854 SaveStateEntry *se;
1855 char idstr[257];
1856 int len;
1857
1858 switch (section_type) {
1859 case QEMU_VM_SECTION_START:
1860 case QEMU_VM_SECTION_FULL:
1861 /* Read section start */
1862 section_id = qemu_get_be32(f);
1863 len = qemu_get_byte(f);
1864 qemu_get_buffer(f, (uint8_t *)idstr, len);
1865 idstr[len] = 0;
1866 instance_id = qemu_get_be32(f);
1867 version_id = qemu_get_be32(f);
1868
1869 /* Find savevm section */
1870 se = find_se(idstr, instance_id);
1871 if (se == NULL) {
1872 fprintf(stderr, "Unknown savevm section or instance '%s' %d\n", idstr, instance_id);
1873 ret = -EINVAL;
1874 goto out;
1875 }
1876
1877 /* Validate version */
1878 if (version_id > se->version_id) {
1879 fprintf(stderr, "savevm: unsupported version %d for '%s' v%d\n",
1880 version_id, idstr, se->version_id);
1881 ret = -EINVAL;
1882 goto out;
1883 }
1884
1885 /* Add entry */
7267c094 1886 le = g_malloc0(sizeof(*le));
a672b469
AL
1887
1888 le->se = se;
1889 le->section_id = section_id;
1890 le->version_id = version_id;
72cf2d4f 1891 QLIST_INSERT_HEAD(&loadvm_handlers, le, entry);
a672b469 1892
4082be4d 1893 ret = vmstate_load(f, le->se, le->version_id);
b5a22e4a
JQ
1894 if (ret < 0) {
1895 fprintf(stderr, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
1896 instance_id, idstr);
1897 goto out;
1898 }
a672b469
AL
1899 break;
1900 case QEMU_VM_SECTION_PART:
1901 case QEMU_VM_SECTION_END:
1902 section_id = qemu_get_be32(f);
1903
72cf2d4f 1904 QLIST_FOREACH(le, &loadvm_handlers, entry) {
f4dbb8dd
JQ
1905 if (le->section_id == section_id) {
1906 break;
1907 }
1908 }
a672b469
AL
1909 if (le == NULL) {
1910 fprintf(stderr, "Unknown savevm section %d\n", section_id);
1911 ret = -EINVAL;
1912 goto out;
1913 }
1914
4082be4d 1915 ret = vmstate_load(f, le->se, le->version_id);
b5a22e4a
JQ
1916 if (ret < 0) {
1917 fprintf(stderr, "qemu: warning: error while loading state section id %d\n",
1918 section_id);
1919 goto out;
1920 }
a672b469
AL
1921 break;
1922 default:
1923 fprintf(stderr, "Unknown savevm section type %d\n", section_type);
1924 ret = -EINVAL;
1925 goto out;
1926 }
1927 }
1928
ea375f9a
JK
1929 cpu_synchronize_all_post_init();
1930
a672b469
AL
1931 ret = 0;
1932
1933out:
72cf2d4f
BS
1934 QLIST_FOREACH_SAFE(le, &loadvm_handlers, entry, new_le) {
1935 QLIST_REMOVE(le, entry);
7267c094 1936 g_free(le);
a672b469
AL
1937 }
1938
42802d47
JQ
1939 if (ret == 0) {
1940 ret = qemu_file_get_error(f);
624b9cc2 1941 }
a672b469
AL
1942
1943 return ret;
1944}
1945
a672b469
AL
1946static int bdrv_snapshot_find(BlockDriverState *bs, QEMUSnapshotInfo *sn_info,
1947 const char *name)
1948{
1949 QEMUSnapshotInfo *sn_tab, *sn;
1950 int nb_sns, i, ret;
1951
1952 ret = -ENOENT;
1953 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
1954 if (nb_sns < 0)
1955 return ret;
1956 for(i = 0; i < nb_sns; i++) {
1957 sn = &sn_tab[i];
1958 if (!strcmp(sn->id_str, name) || !strcmp(sn->name, name)) {
1959 *sn_info = *sn;
1960 ret = 0;
1961 break;
1962 }
1963 }
7267c094 1964 g_free(sn_tab);
a672b469
AL
1965 return ret;
1966}
1967
cb499fb2
KW
1968/*
1969 * Deletes snapshots of a given name in all opened images.
1970 */
1971static int del_existing_snapshots(Monitor *mon, const char *name)
1972{
1973 BlockDriverState *bs;
cb499fb2
KW
1974 QEMUSnapshotInfo sn1, *snapshot = &sn1;
1975 int ret;
1976
dbc13590
MA
1977 bs = NULL;
1978 while ((bs = bdrv_next(bs))) {
cb499fb2
KW
1979 if (bdrv_can_snapshot(bs) &&
1980 bdrv_snapshot_find(bs, snapshot, name) >= 0)
1981 {
1982 ret = bdrv_snapshot_delete(bs, name);
1983 if (ret < 0) {
1984 monitor_printf(mon,
1985 "Error while deleting snapshot on '%s'\n",
1986 bdrv_get_device_name(bs));
1987 return -1;
1988 }
1989 }
1990 }
1991
1992 return 0;
1993}
1994
d54908a5 1995void do_savevm(Monitor *mon, const QDict *qdict)
a672b469
AL
1996{
1997 BlockDriverState *bs, *bs1;
1998 QEMUSnapshotInfo sn1, *sn = &sn1, old_sn1, *old_sn = &old_sn1;
cb499fb2 1999 int ret;
a672b469
AL
2000 QEMUFile *f;
2001 int saved_vm_running;
2d22b18f 2002 uint32_t vm_state_size;
a672b469
AL
2003#ifdef _WIN32
2004 struct _timeb tb;
7d631a11 2005 struct tm *ptm;
a672b469
AL
2006#else
2007 struct timeval tv;
7d631a11 2008 struct tm tm;
a672b469 2009#endif
d54908a5 2010 const char *name = qdict_get_try_str(qdict, "name");
a672b469 2011
feeee5ac 2012 /* Verify if there is a device that doesn't support snapshots and is writable */
dbc13590
MA
2013 bs = NULL;
2014 while ((bs = bdrv_next(bs))) {
feeee5ac 2015
07b70bfb 2016 if (!bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {
feeee5ac
MDCF
2017 continue;
2018 }
2019
2020 if (!bdrv_can_snapshot(bs)) {
2021 monitor_printf(mon, "Device '%s' is writable but does not support snapshots.\n",
2022 bdrv_get_device_name(bs));
2023 return;
2024 }
2025 }
2026
f9092b10 2027 bs = bdrv_snapshots();
a672b469 2028 if (!bs) {
376253ec 2029 monitor_printf(mon, "No block device can accept snapshots\n");
a672b469
AL
2030 return;
2031 }
a672b469 2032
1354869c 2033 saved_vm_running = runstate_is_running();
0461d5a6 2034 vm_stop(RUN_STATE_SAVE_VM);
a672b469 2035
cb499fb2 2036 memset(sn, 0, sizeof(*sn));
a672b469
AL
2037
2038 /* fill auxiliary fields */
2039#ifdef _WIN32
2040 _ftime(&tb);
2041 sn->date_sec = tb.time;
2042 sn->date_nsec = tb.millitm * 1000000;
2043#else
2044 gettimeofday(&tv, NULL);
2045 sn->date_sec = tv.tv_sec;
2046 sn->date_nsec = tv.tv_usec * 1000;
2047#endif
74475455 2048 sn->vm_clock_nsec = qemu_get_clock_ns(vm_clock);
a672b469 2049
7d631a11
MDCF
2050 if (name) {
2051 ret = bdrv_snapshot_find(bs, old_sn, name);
2052 if (ret >= 0) {
2053 pstrcpy(sn->name, sizeof(sn->name), old_sn->name);
2054 pstrcpy(sn->id_str, sizeof(sn->id_str), old_sn->id_str);
2055 } else {
2056 pstrcpy(sn->name, sizeof(sn->name), name);
2057 }
2058 } else {
2059#ifdef _WIN32
2060 ptm = localtime(&tb.time);
2061 strftime(sn->name, sizeof(sn->name), "vm-%Y%m%d%H%M%S", ptm);
2062#else
d7d9b528
BS
2063 /* cast below needed for OpenBSD where tv_sec is still 'long' */
2064 localtime_r((const time_t *)&tv.tv_sec, &tm);
7d631a11
MDCF
2065 strftime(sn->name, sizeof(sn->name), "vm-%Y%m%d%H%M%S", &tm);
2066#endif
2067 }
2068
cb499fb2 2069 /* Delete old snapshots of the same name */
f139a412 2070 if (name && del_existing_snapshots(mon, name) < 0) {
cb499fb2
KW
2071 goto the_end;
2072 }
2073
a672b469 2074 /* save the VM state */
45566e9c 2075 f = qemu_fopen_bdrv(bs, 1);
a672b469 2076 if (!f) {
376253ec 2077 monitor_printf(mon, "Could not open VM state file\n");
a672b469
AL
2078 goto the_end;
2079 }
f327aa0c 2080 ret = qemu_savevm_state(mon, f);
2d22b18f 2081 vm_state_size = qemu_ftell(f);
a672b469
AL
2082 qemu_fclose(f);
2083 if (ret < 0) {
376253ec 2084 monitor_printf(mon, "Error %d while writing VM\n", ret);
a672b469
AL
2085 goto the_end;
2086 }
2087
2088 /* create the snapshots */
2089
dbc13590
MA
2090 bs1 = NULL;
2091 while ((bs1 = bdrv_next(bs1))) {
feeee5ac 2092 if (bdrv_can_snapshot(bs1)) {
2d22b18f
AL
2093 /* Write VM state size only to the image that contains the state */
2094 sn->vm_state_size = (bs == bs1 ? vm_state_size : 0);
a672b469
AL
2095 ret = bdrv_snapshot_create(bs1, sn);
2096 if (ret < 0) {
376253ec
AL
2097 monitor_printf(mon, "Error while creating snapshot on '%s'\n",
2098 bdrv_get_device_name(bs1));
a672b469
AL
2099 }
2100 }
2101 }
2102
2103 the_end:
2104 if (saved_vm_running)
2105 vm_start();
2106}
2107
03cd4655 2108int load_vmstate(const char *name)
a672b469 2109{
f0aa7a8b 2110 BlockDriverState *bs, *bs_vm_state;
2d22b18f 2111 QEMUSnapshotInfo sn;
a672b469 2112 QEMUFile *f;
751c6a17 2113 int ret;
a672b469 2114
f0aa7a8b
MDCF
2115 bs_vm_state = bdrv_snapshots();
2116 if (!bs_vm_state) {
2117 error_report("No block device supports snapshots");
2118 return -ENOTSUP;
2119 }
2120
2121 /* Don't even try to load empty VM states */
2122 ret = bdrv_snapshot_find(bs_vm_state, &sn, name);
2123 if (ret < 0) {
2124 return ret;
2125 } else if (sn.vm_state_size == 0) {
e11480db
KW
2126 error_report("This is a disk-only snapshot. Revert to it offline "
2127 "using qemu-img.");
f0aa7a8b
MDCF
2128 return -EINVAL;
2129 }
2130
2131 /* Verify if there is any device that doesn't support snapshots and is
2132 writable and check if the requested snapshot is available too. */
dbc13590
MA
2133 bs = NULL;
2134 while ((bs = bdrv_next(bs))) {
feeee5ac 2135
07b70bfb 2136 if (!bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {
feeee5ac
MDCF
2137 continue;
2138 }
2139
2140 if (!bdrv_can_snapshot(bs)) {
2141 error_report("Device '%s' is writable but does not support snapshots.",
2142 bdrv_get_device_name(bs));
2143 return -ENOTSUP;
2144 }
feeee5ac 2145
f0aa7a8b
MDCF
2146 ret = bdrv_snapshot_find(bs, &sn, name);
2147 if (ret < 0) {
2148 error_report("Device '%s' does not have the requested snapshot '%s'",
2149 bdrv_get_device_name(bs), name);
2150 return ret;
2151 }
a672b469
AL
2152 }
2153
2154 /* Flush all IO requests so they don't interfere with the new state. */
922453bc 2155 bdrv_drain_all();
a672b469 2156
f0aa7a8b
MDCF
2157 bs = NULL;
2158 while ((bs = bdrv_next(bs))) {
2159 if (bdrv_can_snapshot(bs)) {
2160 ret = bdrv_snapshot_goto(bs, name);
a672b469 2161 if (ret < 0) {
f0aa7a8b
MDCF
2162 error_report("Error %d while activating snapshot '%s' on '%s'",
2163 ret, name, bdrv_get_device_name(bs));
2164 return ret;
a672b469
AL
2165 }
2166 }
2167 }
2168
a672b469 2169 /* restore the VM state */
f0aa7a8b 2170 f = qemu_fopen_bdrv(bs_vm_state, 0);
a672b469 2171 if (!f) {
1ecda02b 2172 error_report("Could not open VM state file");
05f2401e 2173 return -EINVAL;
a672b469 2174 }
f0aa7a8b 2175
5a8a49d7 2176 qemu_system_reset(VMRESET_SILENT);
a672b469 2177 ret = qemu_loadvm_state(f);
f0aa7a8b 2178
a672b469
AL
2179 qemu_fclose(f);
2180 if (ret < 0) {
1ecda02b 2181 error_report("Error %d while loading VM state", ret);
05f2401e 2182 return ret;
a672b469 2183 }
f0aa7a8b 2184
05f2401e 2185 return 0;
7b630349
JQ
2186}
2187
d54908a5 2188void do_delvm(Monitor *mon, const QDict *qdict)
a672b469
AL
2189{
2190 BlockDriverState *bs, *bs1;
751c6a17 2191 int ret;
d54908a5 2192 const char *name = qdict_get_str(qdict, "name");
a672b469 2193
f9092b10 2194 bs = bdrv_snapshots();
a672b469 2195 if (!bs) {
376253ec 2196 monitor_printf(mon, "No block device supports snapshots\n");
a672b469
AL
2197 return;
2198 }
2199
dbc13590
MA
2200 bs1 = NULL;
2201 while ((bs1 = bdrv_next(bs1))) {
feeee5ac 2202 if (bdrv_can_snapshot(bs1)) {
a672b469
AL
2203 ret = bdrv_snapshot_delete(bs1, name);
2204 if (ret < 0) {
2205 if (ret == -ENOTSUP)
376253ec
AL
2206 monitor_printf(mon,
2207 "Snapshots not supported on device '%s'\n",
2208 bdrv_get_device_name(bs1));
a672b469 2209 else
376253ec
AL
2210 monitor_printf(mon, "Error %d while deleting snapshot on "
2211 "'%s'\n", ret, bdrv_get_device_name(bs1));
a672b469
AL
2212 }
2213 }
2214 }
2215}
2216
376253ec 2217void do_info_snapshots(Monitor *mon)
a672b469
AL
2218{
2219 BlockDriverState *bs, *bs1;
f9209915
MDCF
2220 QEMUSnapshotInfo *sn_tab, *sn, s, *sn_info = &s;
2221 int nb_sns, i, ret, available;
2222 int total;
2223 int *available_snapshots;
a672b469
AL
2224 char buf[256];
2225
f9092b10 2226 bs = bdrv_snapshots();
a672b469 2227 if (!bs) {
376253ec 2228 monitor_printf(mon, "No available block device supports snapshots\n");
a672b469
AL
2229 return;
2230 }
a672b469
AL
2231
2232 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
2233 if (nb_sns < 0) {
376253ec 2234 monitor_printf(mon, "bdrv_snapshot_list: error %d\n", nb_sns);
a672b469
AL
2235 return;
2236 }
f9209915
MDCF
2237
2238 if (nb_sns == 0) {
2239 monitor_printf(mon, "There is no snapshot available.\n");
2240 return;
2241 }
2242
7267c094 2243 available_snapshots = g_malloc0(sizeof(int) * nb_sns);
f9209915
MDCF
2244 total = 0;
2245 for (i = 0; i < nb_sns; i++) {
a672b469 2246 sn = &sn_tab[i];
f9209915
MDCF
2247 available = 1;
2248 bs1 = NULL;
2249
2250 while ((bs1 = bdrv_next(bs1))) {
2251 if (bdrv_can_snapshot(bs1) && bs1 != bs) {
2252 ret = bdrv_snapshot_find(bs1, sn_info, sn->id_str);
2253 if (ret < 0) {
2254 available = 0;
2255 break;
2256 }
2257 }
2258 }
2259
2260 if (available) {
2261 available_snapshots[total] = i;
2262 total++;
2263 }
a672b469 2264 }
f9209915
MDCF
2265
2266 if (total > 0) {
2267 monitor_printf(mon, "%s\n", bdrv_snapshot_dump(buf, sizeof(buf), NULL));
2268 for (i = 0; i < total; i++) {
2269 sn = &sn_tab[available_snapshots[i]];
2270 monitor_printf(mon, "%s\n", bdrv_snapshot_dump(buf, sizeof(buf), sn));
2271 }
2272 } else {
2273 monitor_printf(mon, "There is no suitable snapshot available\n");
2274 }
2275
7267c094
AL
2276 g_free(sn_tab);
2277 g_free(available_snapshots);
f9209915 2278
a672b469 2279}
This page took 0.785403 seconds and 4 git commands to generate.