qemu-ga: Building Windows MSI installation with configure/Makefile
[qemu.git] / migration / qemu-file.c
blob965a757772f9018380c6839eaa05476e741f6b60
1 /*
2 * QEMU System Emulator
4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
24 #include <zlib.h>
25 #include "qemu-common.h"
26 #include "qemu/iov.h"
27 #include "qemu/sockets.h"
28 #include "block/coroutine.h"
29 #include "migration/migration.h"
30 #include "migration/qemu-file.h"
31 #include "migration/qemu-file-internal.h"
32 #include "trace.h"
35 * Stop a file from being read/written - not all backing files can do this
36 * typically only sockets can.
38 int qemu_file_shutdown(QEMUFile *f)
40 if (!f->ops->shut_down) {
41 return -ENOSYS;
43 return f->ops->shut_down(f->opaque, true, true);
46 bool qemu_file_mode_is_not_valid(const char *mode)
48 if (mode == NULL ||
49 (mode[0] != 'r' && mode[0] != 'w') ||
50 mode[1] != 'b' || mode[2] != 0) {
51 fprintf(stderr, "qemu_fopen: Argument validity check failed\n");
52 return true;
55 return false;
58 QEMUFile *qemu_fopen_ops(void *opaque, const QEMUFileOps *ops)
60 QEMUFile *f;
62 f = g_malloc0(sizeof(QEMUFile));
64 f->opaque = opaque;
65 f->ops = ops;
66 return f;
70 * Get last error for stream f
72 * Return negative error value if there has been an error on previous
73 * operations, return 0 if no error happened.
76 int qemu_file_get_error(QEMUFile *f)
78 return f->last_error;
81 void qemu_file_set_error(QEMUFile *f, int ret)
83 if (f->last_error == 0) {
84 f->last_error = ret;
88 bool qemu_file_is_writable(QEMUFile *f)
90 return f->ops->writev_buffer || f->ops->put_buffer;
93 /**
94 * Flushes QEMUFile buffer
96 * If there is writev_buffer QEMUFileOps it uses it otherwise uses
97 * put_buffer ops.
99 void qemu_fflush(QEMUFile *f)
101 ssize_t ret = 0;
103 if (!qemu_file_is_writable(f)) {
104 return;
107 if (f->ops->writev_buffer) {
108 if (f->iovcnt > 0) {
109 ret = f->ops->writev_buffer(f->opaque, f->iov, f->iovcnt, f->pos);
111 } else {
112 if (f->buf_index > 0) {
113 ret = f->ops->put_buffer(f->opaque, f->buf, f->pos, f->buf_index);
116 if (ret >= 0) {
117 f->pos += ret;
119 f->buf_index = 0;
120 f->iovcnt = 0;
121 if (ret < 0) {
122 qemu_file_set_error(f, ret);
126 void ram_control_before_iterate(QEMUFile *f, uint64_t flags)
128 int ret = 0;
130 if (f->ops->before_ram_iterate) {
131 ret = f->ops->before_ram_iterate(f, f->opaque, flags);
132 if (ret < 0) {
133 qemu_file_set_error(f, ret);
138 void ram_control_after_iterate(QEMUFile *f, uint64_t flags)
140 int ret = 0;
142 if (f->ops->after_ram_iterate) {
143 ret = f->ops->after_ram_iterate(f, f->opaque, flags);
144 if (ret < 0) {
145 qemu_file_set_error(f, ret);
150 void ram_control_load_hook(QEMUFile *f, uint64_t flags)
152 int ret = -EINVAL;
154 if (f->ops->hook_ram_load) {
155 ret = f->ops->hook_ram_load(f, f->opaque, flags);
156 if (ret < 0) {
157 qemu_file_set_error(f, ret);
159 } else {
160 qemu_file_set_error(f, ret);
164 size_t ram_control_save_page(QEMUFile *f, ram_addr_t block_offset,
165 ram_addr_t offset, size_t size,
166 uint64_t *bytes_sent)
168 if (f->ops->save_page) {
169 int ret = f->ops->save_page(f, f->opaque, block_offset,
170 offset, size, bytes_sent);
172 if (ret != RAM_SAVE_CONTROL_DELAYED) {
173 if (bytes_sent && *bytes_sent > 0) {
174 qemu_update_position(f, *bytes_sent);
175 } else if (ret < 0) {
176 qemu_file_set_error(f, ret);
180 return ret;
183 return RAM_SAVE_CONTROL_NOT_SUPP;
187 * Attempt to fill the buffer from the underlying file
188 * Returns the number of bytes read, or negative value for an error.
190 * Note that it can return a partially full buffer even in a not error/not EOF
191 * case if the underlying file descriptor gives a short read, and that can
192 * happen even on a blocking fd.
194 static ssize_t qemu_fill_buffer(QEMUFile *f)
196 int len;
197 int pending;
199 assert(!qemu_file_is_writable(f));
201 pending = f->buf_size - f->buf_index;
202 if (pending > 0) {
203 memmove(f->buf, f->buf + f->buf_index, pending);
205 f->buf_index = 0;
206 f->buf_size = pending;
208 len = f->ops->get_buffer(f->opaque, f->buf + pending, f->pos,
209 IO_BUF_SIZE - pending);
210 if (len > 0) {
211 f->buf_size += len;
212 f->pos += len;
213 } else if (len == 0) {
214 qemu_file_set_error(f, -EIO);
215 } else if (len != -EAGAIN) {
216 qemu_file_set_error(f, len);
219 return len;
222 int qemu_get_fd(QEMUFile *f)
224 if (f->ops->get_fd) {
225 return f->ops->get_fd(f->opaque);
227 return -1;
230 void qemu_update_position(QEMUFile *f, size_t size)
232 f->pos += size;
235 /** Closes the file
237 * Returns negative error value if any error happened on previous operations or
238 * while closing the file. Returns 0 or positive number on success.
240 * The meaning of return value on success depends on the specific backend
241 * being used.
243 int qemu_fclose(QEMUFile *f)
245 int ret;
246 qemu_fflush(f);
247 ret = qemu_file_get_error(f);
249 if (f->ops->close) {
250 int ret2 = f->ops->close(f->opaque);
251 if (ret >= 0) {
252 ret = ret2;
255 /* If any error was spotted before closing, we should report it
256 * instead of the close() return value.
258 if (f->last_error) {
259 ret = f->last_error;
261 g_free(f);
262 trace_qemu_file_fclose();
263 return ret;
266 static void add_to_iovec(QEMUFile *f, const uint8_t *buf, int size)
268 /* check for adjacent buffer and coalesce them */
269 if (f->iovcnt > 0 && buf == f->iov[f->iovcnt - 1].iov_base +
270 f->iov[f->iovcnt - 1].iov_len) {
271 f->iov[f->iovcnt - 1].iov_len += size;
272 } else {
273 f->iov[f->iovcnt].iov_base = (uint8_t *)buf;
274 f->iov[f->iovcnt++].iov_len = size;
277 if (f->iovcnt >= MAX_IOV_SIZE) {
278 qemu_fflush(f);
282 void qemu_put_buffer_async(QEMUFile *f, const uint8_t *buf, int size)
284 if (!f->ops->writev_buffer) {
285 qemu_put_buffer(f, buf, size);
286 return;
289 if (f->last_error) {
290 return;
293 f->bytes_xfer += size;
294 add_to_iovec(f, buf, size);
297 void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size)
299 int l;
301 if (f->last_error) {
302 return;
305 while (size > 0) {
306 l = IO_BUF_SIZE - f->buf_index;
307 if (l > size) {
308 l = size;
310 memcpy(f->buf + f->buf_index, buf, l);
311 f->bytes_xfer += l;
312 if (f->ops->writev_buffer) {
313 add_to_iovec(f, f->buf + f->buf_index, l);
315 f->buf_index += l;
316 if (f->buf_index == IO_BUF_SIZE) {
317 qemu_fflush(f);
319 if (qemu_file_get_error(f)) {
320 break;
322 buf += l;
323 size -= l;
327 void qemu_put_byte(QEMUFile *f, int v)
329 if (f->last_error) {
330 return;
333 f->buf[f->buf_index] = v;
334 f->bytes_xfer++;
335 if (f->ops->writev_buffer) {
336 add_to_iovec(f, f->buf + f->buf_index, 1);
338 f->buf_index++;
339 if (f->buf_index == IO_BUF_SIZE) {
340 qemu_fflush(f);
344 void qemu_file_skip(QEMUFile *f, int size)
346 if (f->buf_index + size <= f->buf_size) {
347 f->buf_index += size;
352 * Read 'size' bytes from file (at 'offset') without moving the
353 * pointer and set 'buf' to point to that data.
355 * It will return size bytes unless there was an error, in which case it will
356 * return as many as it managed to read (assuming blocking fd's which
357 * all current QEMUFile are)
359 int qemu_peek_buffer(QEMUFile *f, uint8_t **buf, int size, size_t offset)
361 int pending;
362 int index;
364 assert(!qemu_file_is_writable(f));
365 assert(offset < IO_BUF_SIZE);
366 assert(size <= IO_BUF_SIZE - offset);
368 /* The 1st byte to read from */
369 index = f->buf_index + offset;
370 /* The number of available bytes starting at index */
371 pending = f->buf_size - index;
374 * qemu_fill_buffer might return just a few bytes, even when there isn't
375 * an error, so loop collecting them until we get enough.
377 while (pending < size) {
378 int received = qemu_fill_buffer(f);
380 if (received <= 0) {
381 break;
384 index = f->buf_index + offset;
385 pending = f->buf_size - index;
388 if (pending <= 0) {
389 return 0;
391 if (size > pending) {
392 size = pending;
395 *buf = f->buf + index;
396 return size;
400 * Read 'size' bytes of data from the file into buf.
401 * 'size' can be larger than the internal buffer.
403 * It will return size bytes unless there was an error, in which case it will
404 * return as many as it managed to read (assuming blocking fd's which
405 * all current QEMUFile are)
407 int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size)
409 int pending = size;
410 int done = 0;
412 while (pending > 0) {
413 int res;
414 uint8_t *src;
416 res = qemu_peek_buffer(f, &src, MIN(pending, IO_BUF_SIZE), 0);
417 if (res == 0) {
418 return done;
420 memcpy(buf, src, res);
421 qemu_file_skip(f, res);
422 buf += res;
423 pending -= res;
424 done += res;
426 return done;
430 * Peeks a single byte from the buffer; this isn't guaranteed to work if
431 * offset leaves a gap after the previous read/peeked data.
433 int qemu_peek_byte(QEMUFile *f, int offset)
435 int index = f->buf_index + offset;
437 assert(!qemu_file_is_writable(f));
438 assert(offset < IO_BUF_SIZE);
440 if (index >= f->buf_size) {
441 qemu_fill_buffer(f);
442 index = f->buf_index + offset;
443 if (index >= f->buf_size) {
444 return 0;
447 return f->buf[index];
450 int qemu_get_byte(QEMUFile *f)
452 int result;
454 result = qemu_peek_byte(f, 0);
455 qemu_file_skip(f, 1);
456 return result;
459 int64_t qemu_ftell_fast(QEMUFile *f)
461 int64_t ret = f->pos;
462 int i;
464 if (f->ops->writev_buffer) {
465 for (i = 0; i < f->iovcnt; i++) {
466 ret += f->iov[i].iov_len;
468 } else {
469 ret += f->buf_index;
472 return ret;
475 int64_t qemu_ftell(QEMUFile *f)
477 qemu_fflush(f);
478 return f->pos;
481 int qemu_file_rate_limit(QEMUFile *f)
483 if (qemu_file_get_error(f)) {
484 return 1;
486 if (f->xfer_limit > 0 && f->bytes_xfer > f->xfer_limit) {
487 return 1;
489 return 0;
492 int64_t qemu_file_get_rate_limit(QEMUFile *f)
494 return f->xfer_limit;
497 void qemu_file_set_rate_limit(QEMUFile *f, int64_t limit)
499 f->xfer_limit = limit;
502 void qemu_file_reset_rate_limit(QEMUFile *f)
504 f->bytes_xfer = 0;
507 void qemu_put_be16(QEMUFile *f, unsigned int v)
509 qemu_put_byte(f, v >> 8);
510 qemu_put_byte(f, v);
513 void qemu_put_be32(QEMUFile *f, unsigned int v)
515 qemu_put_byte(f, v >> 24);
516 qemu_put_byte(f, v >> 16);
517 qemu_put_byte(f, v >> 8);
518 qemu_put_byte(f, v);
521 void qemu_put_be64(QEMUFile *f, uint64_t v)
523 qemu_put_be32(f, v >> 32);
524 qemu_put_be32(f, v);
527 unsigned int qemu_get_be16(QEMUFile *f)
529 unsigned int v;
530 v = qemu_get_byte(f) << 8;
531 v |= qemu_get_byte(f);
532 return v;
535 unsigned int qemu_get_be32(QEMUFile *f)
537 unsigned int v;
538 v = (unsigned int)qemu_get_byte(f) << 24;
539 v |= qemu_get_byte(f) << 16;
540 v |= qemu_get_byte(f) << 8;
541 v |= qemu_get_byte(f);
542 return v;
545 uint64_t qemu_get_be64(QEMUFile *f)
547 uint64_t v;
548 v = (uint64_t)qemu_get_be32(f) << 32;
549 v |= qemu_get_be32(f);
550 return v;
553 /* compress size bytes of data start at p with specific compression
554 * level and store the compressed data to the buffer of f.
557 ssize_t qemu_put_compression_data(QEMUFile *f, const uint8_t *p, size_t size,
558 int level)
560 ssize_t blen = IO_BUF_SIZE - f->buf_index - sizeof(int32_t);
562 if (blen < compressBound(size)) {
563 return 0;
565 if (compress2(f->buf + f->buf_index + sizeof(int32_t), (uLongf *)&blen,
566 (Bytef *)p, size, level) != Z_OK) {
567 error_report("Compress Failed!");
568 return 0;
570 qemu_put_be32(f, blen);
571 f->buf_index += blen;
572 return blen + sizeof(int32_t);
575 /* Put the data in the buffer of f_src to the buffer of f_des, and
576 * then reset the buf_index of f_src to 0.
579 int qemu_put_qemu_file(QEMUFile *f_des, QEMUFile *f_src)
581 int len = 0;
583 if (f_src->buf_index > 0) {
584 len = f_src->buf_index;
585 qemu_put_buffer(f_des, f_src->buf, f_src->buf_index);
586 f_src->buf_index = 0;
588 return len;
592 * Get a string whose length is determined by a single preceding byte
593 * A preallocated 256 byte buffer must be passed in.
594 * Returns: len on success and a 0 terminated string in the buffer
595 * else 0
596 * (Note a 0 length string will return 0 either way)
598 size_t qemu_get_counted_string(QEMUFile *f, char buf[256])
600 size_t len = qemu_get_byte(f);
601 size_t res = qemu_get_buffer(f, (uint8_t *)buf, len);
603 buf[res] = 0;
605 return res == len ? res : 0;