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