ui/cocoa: Add console items to the View menu
[qemu/ar7.git] / migration / qemu-file.c
blob2750365a7e7f478e6c60bf3f6a60894714d03eff
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') into buf without moving the
353 * pointer.
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 memcpy(buf, f->buf + index, size);
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;
415 res = qemu_peek_buffer(f, buf, MIN(pending, IO_BUF_SIZE), 0);
416 if (res == 0) {
417 return done;
419 qemu_file_skip(f, res);
420 buf += res;
421 pending -= res;
422 done += res;
424 return done;
428 * Peeks a single byte from the buffer; this isn't guaranteed to work if
429 * offset leaves a gap after the previous read/peeked data.
431 int qemu_peek_byte(QEMUFile *f, int offset)
433 int index = f->buf_index + offset;
435 assert(!qemu_file_is_writable(f));
436 assert(offset < IO_BUF_SIZE);
438 if (index >= f->buf_size) {
439 qemu_fill_buffer(f);
440 index = f->buf_index + offset;
441 if (index >= f->buf_size) {
442 return 0;
445 return f->buf[index];
448 int qemu_get_byte(QEMUFile *f)
450 int result;
452 result = qemu_peek_byte(f, 0);
453 qemu_file_skip(f, 1);
454 return result;
457 int64_t qemu_ftell_fast(QEMUFile *f)
459 int64_t ret = f->pos;
460 int i;
462 if (f->ops->writev_buffer) {
463 for (i = 0; i < f->iovcnt; i++) {
464 ret += f->iov[i].iov_len;
466 } else {
467 ret += f->buf_index;
470 return ret;
473 int64_t qemu_ftell(QEMUFile *f)
475 qemu_fflush(f);
476 return f->pos;
479 int qemu_file_rate_limit(QEMUFile *f)
481 if (qemu_file_get_error(f)) {
482 return 1;
484 if (f->xfer_limit > 0 && f->bytes_xfer > f->xfer_limit) {
485 return 1;
487 return 0;
490 int64_t qemu_file_get_rate_limit(QEMUFile *f)
492 return f->xfer_limit;
495 void qemu_file_set_rate_limit(QEMUFile *f, int64_t limit)
497 f->xfer_limit = limit;
500 void qemu_file_reset_rate_limit(QEMUFile *f)
502 f->bytes_xfer = 0;
505 void qemu_put_be16(QEMUFile *f, unsigned int v)
507 qemu_put_byte(f, v >> 8);
508 qemu_put_byte(f, v);
511 void qemu_put_be32(QEMUFile *f, unsigned int v)
513 qemu_put_byte(f, v >> 24);
514 qemu_put_byte(f, v >> 16);
515 qemu_put_byte(f, v >> 8);
516 qemu_put_byte(f, v);
519 void qemu_put_be64(QEMUFile *f, uint64_t v)
521 qemu_put_be32(f, v >> 32);
522 qemu_put_be32(f, v);
525 unsigned int qemu_get_be16(QEMUFile *f)
527 unsigned int v;
528 v = qemu_get_byte(f) << 8;
529 v |= qemu_get_byte(f);
530 return v;
533 unsigned int qemu_get_be32(QEMUFile *f)
535 unsigned int v;
536 v = (unsigned int)qemu_get_byte(f) << 24;
537 v |= qemu_get_byte(f) << 16;
538 v |= qemu_get_byte(f) << 8;
539 v |= qemu_get_byte(f);
540 return v;
543 uint64_t qemu_get_be64(QEMUFile *f)
545 uint64_t v;
546 v = (uint64_t)qemu_get_be32(f) << 32;
547 v |= qemu_get_be32(f);
548 return v;
551 /* compress size bytes of data start at p with specific compression
552 * level and store the compressed data to the buffer of f.
555 ssize_t qemu_put_compression_data(QEMUFile *f, const uint8_t *p, size_t size,
556 int level)
558 ssize_t blen = IO_BUF_SIZE - f->buf_index - sizeof(int32_t);
560 if (blen < compressBound(size)) {
561 return 0;
563 if (compress2(f->buf + f->buf_index + sizeof(int32_t), (uLongf *)&blen,
564 (Bytef *)p, size, level) != Z_OK) {
565 error_report("Compress Failed!");
566 return 0;
568 qemu_put_be32(f, blen);
569 f->buf_index += blen;
570 return blen + sizeof(int32_t);
573 /* Put the data in the buffer of f_src to the buffer of f_des, and
574 * then reset the buf_index of f_src to 0.
577 int qemu_put_qemu_file(QEMUFile *f_des, QEMUFile *f_src)
579 int len = 0;
581 if (f_src->buf_index > 0) {
582 len = f_src->buf_index;
583 qemu_put_buffer(f_des, f_src->buf, f_src->buf_index);
584 f_src->buf_index = 0;
586 return len;