vl.c: add HMP help to machine
[qemu/ar7.git] / migration / qemu-file.c
blobd2d40073f04f2edd6777c4b506b6b6eabef9b6e2
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 "qemu-common.h"
25 #include "qemu/iov.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"
31 #include "trace.h"
33 bool qemu_file_mode_is_not_valid(const char *mode)
35 if (mode == NULL ||
36 (mode[0] != 'r' && mode[0] != 'w') ||
37 mode[1] != 'b' || mode[2] != 0) {
38 fprintf(stderr, "qemu_fopen: Argument validity check failed\n");
39 return true;
42 return false;
45 QEMUFile *qemu_fopen_ops(void *opaque, const QEMUFileOps *ops)
47 QEMUFile *f;
49 f = g_malloc0(sizeof(QEMUFile));
51 f->opaque = opaque;
52 f->ops = ops;
53 return f;
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)
65 return f->last_error;
68 void qemu_file_set_error(QEMUFile *f, int ret)
70 if (f->last_error == 0) {
71 f->last_error = ret;
75 bool qemu_file_is_writable(QEMUFile *f)
77 return f->ops->writev_buffer || f->ops->put_buffer;
80 /**
81 * Flushes QEMUFile buffer
83 * If there is writev_buffer QEMUFileOps it uses it otherwise uses
84 * put_buffer ops.
86 void qemu_fflush(QEMUFile *f)
88 ssize_t ret = 0;
90 if (!qemu_file_is_writable(f)) {
91 return;
94 if (f->ops->writev_buffer) {
95 if (f->iovcnt > 0) {
96 ret = f->ops->writev_buffer(f->opaque, f->iov, f->iovcnt, f->pos);
98 } else {
99 if (f->buf_index > 0) {
100 ret = f->ops->put_buffer(f->opaque, f->buf, f->pos, f->buf_index);
103 if (ret >= 0) {
104 f->pos += ret;
106 f->buf_index = 0;
107 f->iovcnt = 0;
108 if (ret < 0) {
109 qemu_file_set_error(f, ret);
113 void ram_control_before_iterate(QEMUFile *f, uint64_t flags)
115 int ret = 0;
117 if (f->ops->before_ram_iterate) {
118 ret = f->ops->before_ram_iterate(f, f->opaque, flags);
119 if (ret < 0) {
120 qemu_file_set_error(f, ret);
125 void ram_control_after_iterate(QEMUFile *f, uint64_t flags)
127 int ret = 0;
129 if (f->ops->after_ram_iterate) {
130 ret = f->ops->after_ram_iterate(f, f->opaque, flags);
131 if (ret < 0) {
132 qemu_file_set_error(f, ret);
137 void ram_control_load_hook(QEMUFile *f, uint64_t flags)
139 int ret = -EINVAL;
141 if (f->ops->hook_ram_load) {
142 ret = f->ops->hook_ram_load(f, f->opaque, flags);
143 if (ret < 0) {
144 qemu_file_set_error(f, ret);
146 } else {
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);
166 return 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)
182 int len;
183 int pending;
185 assert(!qemu_file_is_writable(f));
187 pending = f->buf_size - f->buf_index;
188 if (pending > 0) {
189 memmove(f->buf, f->buf + f->buf_index, pending);
191 f->buf_index = 0;
192 f->buf_size = pending;
194 len = f->ops->get_buffer(f->opaque, f->buf + pending, f->pos,
195 IO_BUF_SIZE - pending);
196 if (len > 0) {
197 f->buf_size += len;
198 f->pos += len;
199 } else if (len == 0) {
200 qemu_file_set_error(f, -EIO);
201 } else if (len != -EAGAIN) {
202 qemu_file_set_error(f, len);
205 return len;
208 int qemu_get_fd(QEMUFile *f)
210 if (f->ops->get_fd) {
211 return f->ops->get_fd(f->opaque);
213 return -1;
216 void qemu_update_position(QEMUFile *f, size_t size)
218 f->pos += size;
221 /** Closes the file
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
227 * being used.
229 int qemu_fclose(QEMUFile *f)
231 int ret;
232 qemu_fflush(f);
233 ret = qemu_file_get_error(f);
235 if (f->ops->close) {
236 int ret2 = f->ops->close(f->opaque);
237 if (ret >= 0) {
238 ret = ret2;
241 /* If any error was spotted before closing, we should report it
242 * instead of the close() return value.
244 if (f->last_error) {
245 ret = f->last_error;
247 g_free(f);
248 trace_qemu_file_fclose();
249 return ret;
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;
258 } else {
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) {
264 qemu_fflush(f);
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);
272 return;
275 if (f->last_error) {
276 return;
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)
285 int l;
287 if (f->last_error) {
288 return;
291 while (size > 0) {
292 l = IO_BUF_SIZE - f->buf_index;
293 if (l > size) {
294 l = size;
296 memcpy(f->buf + f->buf_index, buf, l);
297 f->bytes_xfer += l;
298 if (f->ops->writev_buffer) {
299 add_to_iovec(f, f->buf + f->buf_index, l);
301 f->buf_index += l;
302 if (f->buf_index == IO_BUF_SIZE) {
303 qemu_fflush(f);
305 if (qemu_file_get_error(f)) {
306 break;
308 buf += l;
309 size -= l;
313 void qemu_put_byte(QEMUFile *f, int v)
315 if (f->last_error) {
316 return;
319 f->buf[f->buf_index] = v;
320 f->bytes_xfer++;
321 if (f->ops->writev_buffer) {
322 add_to_iovec(f, f->buf + f->buf_index, 1);
324 f->buf_index++;
325 if (f->buf_index == IO_BUF_SIZE) {
326 qemu_fflush(f);
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
339 * pointer.
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)
347 int pending;
348 int index;
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);
366 if (received <= 0) {
367 break;
370 index = f->buf_index + offset;
371 pending = f->buf_size - index;
374 if (pending <= 0) {
375 return 0;
377 if (size > pending) {
378 size = pending;
381 memcpy(buf, f->buf + index, size);
382 return 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)
395 int pending = size;
396 int done = 0;
398 while (pending > 0) {
399 int res;
401 res = qemu_peek_buffer(f, buf, MIN(pending, IO_BUF_SIZE), 0);
402 if (res == 0) {
403 return done;
405 qemu_file_skip(f, res);
406 buf += res;
407 pending -= res;
408 done += res;
410 return done;
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) {
425 qemu_fill_buffer(f);
426 index = f->buf_index + offset;
427 if (index >= f->buf_size) {
428 return 0;
431 return f->buf[index];
434 int qemu_get_byte(QEMUFile *f)
436 int result;
438 result = qemu_peek_byte(f, 0);
439 qemu_file_skip(f, 1);
440 return result;
443 int64_t qemu_ftell(QEMUFile *f)
445 qemu_fflush(f);
446 return f->pos;
449 int qemu_file_rate_limit(QEMUFile *f)
451 if (qemu_file_get_error(f)) {
452 return 1;
454 if (f->xfer_limit > 0 && f->bytes_xfer > f->xfer_limit) {
455 return 1;
457 return 0;
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)
472 f->bytes_xfer = 0;
475 void qemu_put_be16(QEMUFile *f, unsigned int v)
477 qemu_put_byte(f, v >> 8);
478 qemu_put_byte(f, v);
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);
486 qemu_put_byte(f, v);
489 void qemu_put_be64(QEMUFile *f, uint64_t v)
491 qemu_put_be32(f, v >> 32);
492 qemu_put_be32(f, v);
495 unsigned int qemu_get_be16(QEMUFile *f)
497 unsigned int v;
498 v = qemu_get_byte(f) << 8;
499 v |= qemu_get_byte(f);
500 return v;
503 unsigned int qemu_get_be32(QEMUFile *f)
505 unsigned int v;
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);
510 return v;
513 uint64_t qemu_get_be64(QEMUFile *f)
515 uint64_t v;
516 v = (uint64_t)qemu_get_be32(f) << 32;
517 v |= qemu_get_be32(f);
518 return v;