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
24 #include "qemu-common.h"
26 #include "qemu/sockets.h"
27 #include "block/coroutine.h"
28 #include "migration/migration.h"
29 #include "migration/qemu-file.h"
30 #include "migration/qemu-file-internal.h"
33 bool qemu_file_mode_is_not_valid(const char *mode)
36 (mode[0] != 'r' && mode[0] != 'w') ||
37 mode[1] != 'b' || mode[2] != 0) {
38 fprintf(stderr, "qemu_fopen: Argument validity check failed\n");
45 QEMUFile *qemu_fopen_ops(void *opaque, const QEMUFileOps *ops)
49 f = g_malloc0(sizeof(QEMUFile));
57 * Get last error for stream f
59 * Return negative error value if there has been an error on previous
60 * operations, return 0 if no error happened.
63 int qemu_file_get_error(QEMUFile *f)
68 void qemu_file_set_error(QEMUFile *f, int ret)
70 if (f->last_error == 0) {
75 bool qemu_file_is_writable(QEMUFile *f)
77 return f->ops->writev_buffer || f->ops->put_buffer;
81 * Flushes QEMUFile buffer
83 * If there is writev_buffer QEMUFileOps it uses it otherwise uses
86 void qemu_fflush(QEMUFile *f)
90 if (!qemu_file_is_writable(f)) {
94 if (f->ops->writev_buffer) {
96 ret = f->ops->writev_buffer(f->opaque, f->iov, f->iovcnt, f->pos);
99 if (f->buf_index > 0) {
100 ret = f->ops->put_buffer(f->opaque, f->buf, f->pos, f->buf_index);
109 qemu_file_set_error(f, ret);
113 void ram_control_before_iterate(QEMUFile *f, uint64_t flags)
117 if (f->ops->before_ram_iterate) {
118 ret = f->ops->before_ram_iterate(f, f->opaque, flags);
120 qemu_file_set_error(f, ret);
125 void ram_control_after_iterate(QEMUFile *f, uint64_t flags)
129 if (f->ops->after_ram_iterate) {
130 ret = f->ops->after_ram_iterate(f, f->opaque, flags);
132 qemu_file_set_error(f, ret);
137 void ram_control_load_hook(QEMUFile *f, uint64_t flags)
141 if (f->ops->hook_ram_load) {
142 ret = f->ops->hook_ram_load(f, f->opaque, flags);
144 qemu_file_set_error(f, ret);
147 qemu_file_set_error(f, ret);
151 size_t ram_control_save_page(QEMUFile *f, ram_addr_t block_offset,
152 ram_addr_t offset, size_t size, int *bytes_sent)
154 if (f->ops->save_page) {
155 int ret = f->ops->save_page(f, f->opaque, block_offset,
156 offset, size, bytes_sent);
158 if (ret != RAM_SAVE_CONTROL_DELAYED) {
159 if (bytes_sent && *bytes_sent > 0) {
160 qemu_update_position(f, *bytes_sent);
161 } else if (ret < 0) {
162 qemu_file_set_error(f, ret);
169 return RAM_SAVE_CONTROL_NOT_SUPP;
173 * Attempt to fill the buffer from the underlying file
174 * Returns the number of bytes read, or negative value for an error.
176 * Note that it can return a partially full buffer even in a not error/not EOF
177 * case if the underlying file descriptor gives a short read, and that can
178 * happen even on a blocking fd.
180 static ssize_t qemu_fill_buffer(QEMUFile *f)
185 assert(!qemu_file_is_writable(f));
187 pending = f->buf_size - f->buf_index;
189 memmove(f->buf, f->buf + f->buf_index, pending);
192 f->buf_size = pending;
194 len = f->ops->get_buffer(f->opaque, f->buf + pending, f->pos,
195 IO_BUF_SIZE - pending);
199 } else if (len == 0) {
200 qemu_file_set_error(f, -EIO);
201 } else if (len != -EAGAIN) {
202 qemu_file_set_error(f, len);
208 int qemu_get_fd(QEMUFile *f)
210 if (f->ops->get_fd) {
211 return f->ops->get_fd(f->opaque);
216 void qemu_update_position(QEMUFile *f, size_t size)
223 * Returns negative error value if any error happened on previous operations or
224 * while closing the file. Returns 0 or positive number on success.
226 * The meaning of return value on success depends on the specific backend
229 int qemu_fclose(QEMUFile *f)
233 ret = qemu_file_get_error(f);
236 int ret2 = f->ops->close(f->opaque);
241 /* If any error was spotted before closing, we should report it
242 * instead of the close() return value.
248 trace_qemu_file_fclose();
252 static void add_to_iovec(QEMUFile *f, const uint8_t *buf, int size)
254 /* check for adjacent buffer and coalesce them */
255 if (f->iovcnt > 0 && buf == f->iov[f->iovcnt - 1].iov_base +
256 f->iov[f->iovcnt - 1].iov_len) {
257 f->iov[f->iovcnt - 1].iov_len += size;
259 f->iov[f->iovcnt].iov_base = (uint8_t *)buf;
260 f->iov[f->iovcnt++].iov_len = size;
263 if (f->iovcnt >= MAX_IOV_SIZE) {
268 void qemu_put_buffer_async(QEMUFile *f, const uint8_t *buf, int size)
270 if (!f->ops->writev_buffer) {
271 qemu_put_buffer(f, buf, size);
279 f->bytes_xfer += size;
280 add_to_iovec(f, buf, size);
283 void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size)
292 l = IO_BUF_SIZE - f->buf_index;
296 memcpy(f->buf + f->buf_index, buf, l);
298 if (f->ops->writev_buffer) {
299 add_to_iovec(f, f->buf + f->buf_index, l);
302 if (f->buf_index == IO_BUF_SIZE) {
305 if (qemu_file_get_error(f)) {
313 void qemu_put_byte(QEMUFile *f, int v)
319 f->buf[f->buf_index] = v;
321 if (f->ops->writev_buffer) {
322 add_to_iovec(f, f->buf + f->buf_index, 1);
325 if (f->buf_index == IO_BUF_SIZE) {
330 void qemu_file_skip(QEMUFile *f, int size)
332 if (f->buf_index + size <= f->buf_size) {
333 f->buf_index += size;
338 * Read 'size' bytes from file (at 'offset') into buf without moving the
341 * It will return size bytes unless there was an error, in which case it will
342 * return as many as it managed to read (assuming blocking fd's which
343 * all current QEMUFile are)
345 int qemu_peek_buffer(QEMUFile *f, uint8_t *buf, int size, size_t offset)
350 assert(!qemu_file_is_writable(f));
351 assert(offset < IO_BUF_SIZE);
352 assert(size <= IO_BUF_SIZE - offset);
354 /* The 1st byte to read from */
355 index = f->buf_index + offset;
356 /* The number of available bytes starting at index */
357 pending = f->buf_size - index;
360 * qemu_fill_buffer might return just a few bytes, even when there isn't
361 * an error, so loop collecting them until we get enough.
363 while (pending < size) {
364 int received = qemu_fill_buffer(f);
370 index = f->buf_index + offset;
371 pending = f->buf_size - index;
377 if (size > pending) {
381 memcpy(buf, f->buf + index, size);
386 * Read 'size' bytes of data from the file into buf.
387 * 'size' can be larger than the internal buffer.
389 * It will return size bytes unless there was an error, in which case it will
390 * return as many as it managed to read (assuming blocking fd's which
391 * all current QEMUFile are)
393 int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size)
398 while (pending > 0) {
401 res = qemu_peek_buffer(f, buf, MIN(pending, IO_BUF_SIZE), 0);
405 qemu_file_skip(f, res);
414 * Peeks a single byte from the buffer; this isn't guaranteed to work if
415 * offset leaves a gap after the previous read/peeked data.
417 int qemu_peek_byte(QEMUFile *f, int offset)
419 int index = f->buf_index + offset;
421 assert(!qemu_file_is_writable(f));
422 assert(offset < IO_BUF_SIZE);
424 if (index >= f->buf_size) {
426 index = f->buf_index + offset;
427 if (index >= f->buf_size) {
431 return f->buf[index];
434 int qemu_get_byte(QEMUFile *f)
438 result = qemu_peek_byte(f, 0);
439 qemu_file_skip(f, 1);
443 int64_t qemu_ftell(QEMUFile *f)
449 int qemu_file_rate_limit(QEMUFile *f)
451 if (qemu_file_get_error(f)) {
454 if (f->xfer_limit > 0 && f->bytes_xfer > f->xfer_limit) {
460 int64_t qemu_file_get_rate_limit(QEMUFile *f)
462 return f->xfer_limit;
465 void qemu_file_set_rate_limit(QEMUFile *f, int64_t limit)
467 f->xfer_limit = limit;
470 void qemu_file_reset_rate_limit(QEMUFile *f)
475 void qemu_put_be16(QEMUFile *f, unsigned int v)
477 qemu_put_byte(f, v >> 8);
481 void qemu_put_be32(QEMUFile *f, unsigned int v)
483 qemu_put_byte(f, v >> 24);
484 qemu_put_byte(f, v >> 16);
485 qemu_put_byte(f, v >> 8);
489 void qemu_put_be64(QEMUFile *f, uint64_t v)
491 qemu_put_be32(f, v >> 32);
495 unsigned int qemu_get_be16(QEMUFile *f)
498 v = qemu_get_byte(f) << 8;
499 v |= qemu_get_byte(f);
503 unsigned int qemu_get_be32(QEMUFile *f)
506 v = qemu_get_byte(f) << 24;
507 v |= qemu_get_byte(f) << 16;
508 v |= qemu_get_byte(f) << 8;
509 v |= qemu_get_byte(f);
513 uint64_t qemu_get_be64(QEMUFile *f)
516 v = (uint64_t)qemu_get_be32(f) << 32;
517 v |= qemu_get_be32(f);