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"
34 * Stop a file from being read/written - not all backing files can do this
35 * typically only sockets can.
37 int qemu_file_shutdown(QEMUFile *f)
39 if (!f->ops->shut_down) {
42 return f->ops->shut_down(f->opaque, true, true);
45 bool qemu_file_mode_is_not_valid(const char *mode)
48 (mode[0] != 'r' && mode[0] != 'w') ||
49 mode[1] != 'b' || mode[2] != 0) {
50 fprintf(stderr, "qemu_fopen: Argument validity check failed\n");
57 QEMUFile *qemu_fopen_ops(void *opaque, const QEMUFileOps *ops)
61 f = g_malloc0(sizeof(QEMUFile));
69 * Get last error for stream f
71 * Return negative error value if there has been an error on previous
72 * operations, return 0 if no error happened.
75 int qemu_file_get_error(QEMUFile *f)
80 void qemu_file_set_error(QEMUFile *f, int ret)
82 if (f->last_error == 0) {
87 bool qemu_file_is_writable(QEMUFile *f)
89 return f->ops->writev_buffer || f->ops->put_buffer;
93 * Flushes QEMUFile buffer
95 * If there is writev_buffer QEMUFileOps it uses it otherwise uses
98 void qemu_fflush(QEMUFile *f)
102 if (!qemu_file_is_writable(f)) {
106 if (f->ops->writev_buffer) {
108 ret = f->ops->writev_buffer(f->opaque, f->iov, f->iovcnt, f->pos);
111 if (f->buf_index > 0) {
112 ret = f->ops->put_buffer(f->opaque, f->buf, f->pos, f->buf_index);
121 qemu_file_set_error(f, ret);
125 void ram_control_before_iterate(QEMUFile *f, uint64_t flags)
129 if (f->ops->before_ram_iterate) {
130 ret = f->ops->before_ram_iterate(f, f->opaque, flags);
132 qemu_file_set_error(f, ret);
137 void ram_control_after_iterate(QEMUFile *f, uint64_t flags)
141 if (f->ops->after_ram_iterate) {
142 ret = f->ops->after_ram_iterate(f, f->opaque, flags);
144 qemu_file_set_error(f, ret);
149 void ram_control_load_hook(QEMUFile *f, uint64_t flags)
153 if (f->ops->hook_ram_load) {
154 ret = f->ops->hook_ram_load(f, f->opaque, flags);
156 qemu_file_set_error(f, ret);
159 qemu_file_set_error(f, ret);
163 size_t ram_control_save_page(QEMUFile *f, ram_addr_t block_offset,
164 ram_addr_t offset, size_t size,
165 uint64_t *bytes_sent)
167 if (f->ops->save_page) {
168 int ret = f->ops->save_page(f, f->opaque, block_offset,
169 offset, size, bytes_sent);
171 if (ret != RAM_SAVE_CONTROL_DELAYED) {
172 if (bytes_sent && *bytes_sent > 0) {
173 qemu_update_position(f, *bytes_sent);
174 } else if (ret < 0) {
175 qemu_file_set_error(f, ret);
182 return RAM_SAVE_CONTROL_NOT_SUPP;
186 * Attempt to fill the buffer from the underlying file
187 * Returns the number of bytes read, or negative value for an error.
189 * Note that it can return a partially full buffer even in a not error/not EOF
190 * case if the underlying file descriptor gives a short read, and that can
191 * happen even on a blocking fd.
193 static ssize_t qemu_fill_buffer(QEMUFile *f)
198 assert(!qemu_file_is_writable(f));
200 pending = f->buf_size - f->buf_index;
202 memmove(f->buf, f->buf + f->buf_index, pending);
205 f->buf_size = pending;
207 len = f->ops->get_buffer(f->opaque, f->buf + pending, f->pos,
208 IO_BUF_SIZE - pending);
212 } else if (len == 0) {
213 qemu_file_set_error(f, -EIO);
214 } else if (len != -EAGAIN) {
215 qemu_file_set_error(f, len);
221 int qemu_get_fd(QEMUFile *f)
223 if (f->ops->get_fd) {
224 return f->ops->get_fd(f->opaque);
229 void qemu_update_position(QEMUFile *f, size_t size)
236 * Returns negative error value if any error happened on previous operations or
237 * while closing the file. Returns 0 or positive number on success.
239 * The meaning of return value on success depends on the specific backend
242 int qemu_fclose(QEMUFile *f)
246 ret = qemu_file_get_error(f);
249 int ret2 = f->ops->close(f->opaque);
254 /* If any error was spotted before closing, we should report it
255 * instead of the close() return value.
261 trace_qemu_file_fclose();
265 static void add_to_iovec(QEMUFile *f, const uint8_t *buf, int size)
267 /* check for adjacent buffer and coalesce them */
268 if (f->iovcnt > 0 && buf == f->iov[f->iovcnt - 1].iov_base +
269 f->iov[f->iovcnt - 1].iov_len) {
270 f->iov[f->iovcnt - 1].iov_len += size;
272 f->iov[f->iovcnt].iov_base = (uint8_t *)buf;
273 f->iov[f->iovcnt++].iov_len = size;
276 if (f->iovcnt >= MAX_IOV_SIZE) {
281 void qemu_put_buffer_async(QEMUFile *f, const uint8_t *buf, int size)
283 if (!f->ops->writev_buffer) {
284 qemu_put_buffer(f, buf, size);
292 f->bytes_xfer += size;
293 add_to_iovec(f, buf, size);
296 void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size)
305 l = IO_BUF_SIZE - f->buf_index;
309 memcpy(f->buf + f->buf_index, buf, l);
311 if (f->ops->writev_buffer) {
312 add_to_iovec(f, f->buf + f->buf_index, l);
315 if (f->buf_index == IO_BUF_SIZE) {
318 if (qemu_file_get_error(f)) {
326 void qemu_put_byte(QEMUFile *f, int v)
332 f->buf[f->buf_index] = v;
334 if (f->ops->writev_buffer) {
335 add_to_iovec(f, f->buf + f->buf_index, 1);
338 if (f->buf_index == IO_BUF_SIZE) {
343 void qemu_file_skip(QEMUFile *f, int size)
345 if (f->buf_index + size <= f->buf_size) {
346 f->buf_index += size;
351 * Read 'size' bytes from file (at 'offset') into buf without moving the
354 * It will return size bytes unless there was an error, in which case it will
355 * return as many as it managed to read (assuming blocking fd's which
356 * all current QEMUFile are)
358 int qemu_peek_buffer(QEMUFile *f, uint8_t *buf, int size, size_t offset)
363 assert(!qemu_file_is_writable(f));
364 assert(offset < IO_BUF_SIZE);
365 assert(size <= IO_BUF_SIZE - offset);
367 /* The 1st byte to read from */
368 index = f->buf_index + offset;
369 /* The number of available bytes starting at index */
370 pending = f->buf_size - index;
373 * qemu_fill_buffer might return just a few bytes, even when there isn't
374 * an error, so loop collecting them until we get enough.
376 while (pending < size) {
377 int received = qemu_fill_buffer(f);
383 index = f->buf_index + offset;
384 pending = f->buf_size - index;
390 if (size > pending) {
394 memcpy(buf, f->buf + index, size);
399 * Read 'size' bytes of data from the file into buf.
400 * 'size' can be larger than the internal buffer.
402 * It will return size bytes unless there was an error, in which case it will
403 * return as many as it managed to read (assuming blocking fd's which
404 * all current QEMUFile are)
406 int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size)
411 while (pending > 0) {
414 res = qemu_peek_buffer(f, buf, MIN(pending, IO_BUF_SIZE), 0);
418 qemu_file_skip(f, res);
427 * Peeks a single byte from the buffer; this isn't guaranteed to work if
428 * offset leaves a gap after the previous read/peeked data.
430 int qemu_peek_byte(QEMUFile *f, int offset)
432 int index = f->buf_index + offset;
434 assert(!qemu_file_is_writable(f));
435 assert(offset < IO_BUF_SIZE);
437 if (index >= f->buf_size) {
439 index = f->buf_index + offset;
440 if (index >= f->buf_size) {
444 return f->buf[index];
447 int qemu_get_byte(QEMUFile *f)
451 result = qemu_peek_byte(f, 0);
452 qemu_file_skip(f, 1);
456 int64_t qemu_ftell_fast(QEMUFile *f)
458 int64_t ret = f->pos;
461 if (f->ops->writev_buffer) {
462 for (i = 0; i < f->iovcnt; i++) {
463 ret += f->iov[i].iov_len;
472 int64_t qemu_ftell(QEMUFile *f)
478 int qemu_file_rate_limit(QEMUFile *f)
480 if (qemu_file_get_error(f)) {
483 if (f->xfer_limit > 0 && f->bytes_xfer > f->xfer_limit) {
489 int64_t qemu_file_get_rate_limit(QEMUFile *f)
491 return f->xfer_limit;
494 void qemu_file_set_rate_limit(QEMUFile *f, int64_t limit)
496 f->xfer_limit = limit;
499 void qemu_file_reset_rate_limit(QEMUFile *f)
504 void qemu_put_be16(QEMUFile *f, unsigned int v)
506 qemu_put_byte(f, v >> 8);
510 void qemu_put_be32(QEMUFile *f, unsigned int v)
512 qemu_put_byte(f, v >> 24);
513 qemu_put_byte(f, v >> 16);
514 qemu_put_byte(f, v >> 8);
518 void qemu_put_be64(QEMUFile *f, uint64_t v)
520 qemu_put_be32(f, v >> 32);
524 unsigned int qemu_get_be16(QEMUFile *f)
527 v = qemu_get_byte(f) << 8;
528 v |= qemu_get_byte(f);
532 unsigned int qemu_get_be32(QEMUFile *f)
535 v = (unsigned int)qemu_get_byte(f) << 24;
536 v |= qemu_get_byte(f) << 16;
537 v |= qemu_get_byte(f) << 8;
538 v |= qemu_get_byte(f);
542 uint64_t qemu_get_be64(QEMUFile *f)
545 v = (uint64_t)qemu_get_be32(f) << 32;
546 v |= qemu_get_be32(f);