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
25 #include "qemu-common.h"
26 #include "qemu/error-report.h"
28 #include "qemu/sockets.h"
29 #include "qemu/coroutine.h"
30 #include "migration/migration.h"
31 #include "migration/qemu-file.h"
32 #include "migration/qemu-file-internal.h"
36 * Stop a file from being read/written - not all backing files can do this
37 * typically only sockets can.
39 int qemu_file_shutdown(QEMUFile
*f
)
41 if (!f
->ops
->shut_down
) {
44 return f
->ops
->shut_down(f
->opaque
, true, true);
48 * Result: QEMUFile* for a 'return path' for comms in the opposite direction
49 * NULL if not available
51 QEMUFile
*qemu_file_get_return_path(QEMUFile
*f
)
53 if (!f
->ops
->get_return_path
) {
56 return f
->ops
->get_return_path(f
->opaque
);
59 bool qemu_file_mode_is_not_valid(const char *mode
)
62 (mode
[0] != 'r' && mode
[0] != 'w') ||
63 mode
[1] != 'b' || mode
[2] != 0) {
64 fprintf(stderr
, "qemu_fopen: Argument validity check failed\n");
71 QEMUFile
*qemu_fopen_ops(void *opaque
, const QEMUFileOps
*ops
)
75 f
= g_new0(QEMUFile
, 1);
83 * Get last error for stream f
85 * Return negative error value if there has been an error on previous
86 * operations, return 0 if no error happened.
89 int qemu_file_get_error(QEMUFile
*f
)
94 void qemu_file_set_error(QEMUFile
*f
, int ret
)
96 if (f
->last_error
== 0) {
101 bool qemu_file_is_writable(QEMUFile
*f
)
103 return f
->ops
->writev_buffer
|| f
->ops
->put_buffer
;
107 * Flushes QEMUFile buffer
109 * If there is writev_buffer QEMUFileOps it uses it otherwise uses
112 void qemu_fflush(QEMUFile
*f
)
116 if (!qemu_file_is_writable(f
)) {
120 if (f
->ops
->writev_buffer
) {
122 ret
= f
->ops
->writev_buffer(f
->opaque
, f
->iov
, f
->iovcnt
, f
->pos
);
125 if (f
->buf_index
> 0) {
126 ret
= f
->ops
->put_buffer(f
->opaque
, f
->buf
, f
->pos
, f
->buf_index
);
135 qemu_file_set_error(f
, ret
);
139 void ram_control_before_iterate(QEMUFile
*f
, uint64_t flags
)
143 if (f
->ops
->before_ram_iterate
) {
144 ret
= f
->ops
->before_ram_iterate(f
, f
->opaque
, flags
, NULL
);
146 qemu_file_set_error(f
, ret
);
151 void ram_control_after_iterate(QEMUFile
*f
, uint64_t flags
)
155 if (f
->ops
->after_ram_iterate
) {
156 ret
= f
->ops
->after_ram_iterate(f
, f
->opaque
, flags
, NULL
);
158 qemu_file_set_error(f
, ret
);
163 void ram_control_load_hook(QEMUFile
*f
, uint64_t flags
, void *data
)
167 if (f
->ops
->hook_ram_load
) {
168 ret
= f
->ops
->hook_ram_load(f
, f
->opaque
, flags
, data
);
170 qemu_file_set_error(f
, ret
);
174 * Hook is a hook specifically requested by the source sending a flag
175 * that expects there to be a hook on the destination.
177 if (flags
== RAM_CONTROL_HOOK
) {
178 qemu_file_set_error(f
, ret
);
183 size_t ram_control_save_page(QEMUFile
*f
, ram_addr_t block_offset
,
184 ram_addr_t offset
, size_t size
,
185 uint64_t *bytes_sent
)
187 if (f
->ops
->save_page
) {
188 int ret
= f
->ops
->save_page(f
, f
->opaque
, block_offset
,
189 offset
, size
, bytes_sent
);
191 if (ret
!= RAM_SAVE_CONTROL_DELAYED
) {
192 if (bytes_sent
&& *bytes_sent
> 0) {
193 qemu_update_position(f
, *bytes_sent
);
194 } else if (ret
< 0) {
195 qemu_file_set_error(f
, ret
);
202 return RAM_SAVE_CONTROL_NOT_SUPP
;
206 * Attempt to fill the buffer from the underlying file
207 * Returns the number of bytes read, or negative value for an error.
209 * Note that it can return a partially full buffer even in a not error/not EOF
210 * case if the underlying file descriptor gives a short read, and that can
211 * happen even on a blocking fd.
213 static ssize_t
qemu_fill_buffer(QEMUFile
*f
)
218 assert(!qemu_file_is_writable(f
));
220 pending
= f
->buf_size
- f
->buf_index
;
222 memmove(f
->buf
, f
->buf
+ f
->buf_index
, pending
);
225 f
->buf_size
= pending
;
227 len
= f
->ops
->get_buffer(f
->opaque
, f
->buf
+ pending
, f
->pos
,
228 IO_BUF_SIZE
- pending
);
232 } else if (len
== 0) {
233 qemu_file_set_error(f
, -EIO
);
234 } else if (len
!= -EAGAIN
) {
235 qemu_file_set_error(f
, len
);
241 int qemu_get_fd(QEMUFile
*f
)
243 if (f
->ops
->get_fd
) {
244 return f
->ops
->get_fd(f
->opaque
);
249 void qemu_update_position(QEMUFile
*f
, size_t size
)
256 * Returns negative error value if any error happened on previous operations or
257 * while closing the file. Returns 0 or positive number on success.
259 * The meaning of return value on success depends on the specific backend
262 int qemu_fclose(QEMUFile
*f
)
266 ret
= qemu_file_get_error(f
);
269 int ret2
= f
->ops
->close(f
->opaque
);
274 /* If any error was spotted before closing, we should report it
275 * instead of the close() return value.
281 trace_qemu_file_fclose();
285 static void add_to_iovec(QEMUFile
*f
, const uint8_t *buf
, size_t size
)
287 /* check for adjacent buffer and coalesce them */
288 if (f
->iovcnt
> 0 && buf
== f
->iov
[f
->iovcnt
- 1].iov_base
+
289 f
->iov
[f
->iovcnt
- 1].iov_len
) {
290 f
->iov
[f
->iovcnt
- 1].iov_len
+= size
;
292 f
->iov
[f
->iovcnt
].iov_base
= (uint8_t *)buf
;
293 f
->iov
[f
->iovcnt
++].iov_len
= size
;
296 if (f
->iovcnt
>= MAX_IOV_SIZE
) {
301 void qemu_put_buffer_async(QEMUFile
*f
, const uint8_t *buf
, size_t size
)
303 if (!f
->ops
->writev_buffer
) {
304 qemu_put_buffer(f
, buf
, size
);
312 f
->bytes_xfer
+= size
;
313 add_to_iovec(f
, buf
, size
);
316 void qemu_put_buffer(QEMUFile
*f
, const uint8_t *buf
, size_t size
)
325 l
= IO_BUF_SIZE
- f
->buf_index
;
329 memcpy(f
->buf
+ f
->buf_index
, buf
, l
);
331 if (f
->ops
->writev_buffer
) {
332 add_to_iovec(f
, f
->buf
+ f
->buf_index
, l
);
335 if (f
->buf_index
== IO_BUF_SIZE
) {
338 if (qemu_file_get_error(f
)) {
346 void qemu_put_byte(QEMUFile
*f
, int v
)
352 f
->buf
[f
->buf_index
] = v
;
354 if (f
->ops
->writev_buffer
) {
355 add_to_iovec(f
, f
->buf
+ f
->buf_index
, 1);
358 if (f
->buf_index
== IO_BUF_SIZE
) {
363 void qemu_file_skip(QEMUFile
*f
, int size
)
365 if (f
->buf_index
+ size
<= f
->buf_size
) {
366 f
->buf_index
+= size
;
371 * Read 'size' bytes from file (at 'offset') without moving the
372 * pointer and set 'buf' to point to that data.
374 * It will return size bytes unless there was an error, in which case it will
375 * return as many as it managed to read (assuming blocking fd's which
376 * all current QEMUFile are)
378 size_t qemu_peek_buffer(QEMUFile
*f
, uint8_t **buf
, size_t size
, size_t offset
)
383 assert(!qemu_file_is_writable(f
));
384 assert(offset
< IO_BUF_SIZE
);
385 assert(size
<= IO_BUF_SIZE
- offset
);
387 /* The 1st byte to read from */
388 index
= f
->buf_index
+ offset
;
389 /* The number of available bytes starting at index */
390 pending
= f
->buf_size
- index
;
393 * qemu_fill_buffer might return just a few bytes, even when there isn't
394 * an error, so loop collecting them until we get enough.
396 while (pending
< size
) {
397 int received
= qemu_fill_buffer(f
);
403 index
= f
->buf_index
+ offset
;
404 pending
= f
->buf_size
- index
;
410 if (size
> pending
) {
414 *buf
= f
->buf
+ index
;
419 * Read 'size' bytes of data from the file into buf.
420 * 'size' can be larger than the internal buffer.
422 * It will return size bytes unless there was an error, in which case it will
423 * return as many as it managed to read (assuming blocking fd's which
424 * all current QEMUFile are)
426 size_t qemu_get_buffer(QEMUFile
*f
, uint8_t *buf
, size_t size
)
428 size_t pending
= size
;
431 while (pending
> 0) {
435 res
= qemu_peek_buffer(f
, &src
, MIN(pending
, IO_BUF_SIZE
), 0);
439 memcpy(buf
, src
, res
);
440 qemu_file_skip(f
, res
);
449 * Read 'size' bytes of data from the file.
450 * 'size' can be larger than the internal buffer.
453 * may be held on an internal buffer (in which case *buf is updated
454 * to point to it) that is valid until the next qemu_file operation.
456 * will be copied to the *buf that was passed in.
458 * The code tries to avoid the copy if possible.
460 * It will return size bytes unless there was an error, in which case it will
461 * return as many as it managed to read (assuming blocking fd's which
462 * all current QEMUFile are)
464 * Note: Since **buf may get changed, the caller should take care to
465 * keep a pointer to the original buffer if it needs to deallocate it.
467 size_t qemu_get_buffer_in_place(QEMUFile
*f
, uint8_t **buf
, size_t size
)
469 if (size
< IO_BUF_SIZE
) {
473 res
= qemu_peek_buffer(f
, &src
, size
, 0);
476 qemu_file_skip(f
, res
);
482 return qemu_get_buffer(f
, *buf
, size
);
486 * Peeks a single byte from the buffer; this isn't guaranteed to work if
487 * offset leaves a gap after the previous read/peeked data.
489 int qemu_peek_byte(QEMUFile
*f
, int offset
)
491 int index
= f
->buf_index
+ offset
;
493 assert(!qemu_file_is_writable(f
));
494 assert(offset
< IO_BUF_SIZE
);
496 if (index
>= f
->buf_size
) {
498 index
= f
->buf_index
+ offset
;
499 if (index
>= f
->buf_size
) {
503 return f
->buf
[index
];
506 int qemu_get_byte(QEMUFile
*f
)
510 result
= qemu_peek_byte(f
, 0);
511 qemu_file_skip(f
, 1);
515 int64_t qemu_ftell_fast(QEMUFile
*f
)
517 int64_t ret
= f
->pos
;
520 if (f
->ops
->writev_buffer
) {
521 for (i
= 0; i
< f
->iovcnt
; i
++) {
522 ret
+= f
->iov
[i
].iov_len
;
531 int64_t qemu_ftell(QEMUFile
*f
)
537 int qemu_file_rate_limit(QEMUFile
*f
)
539 if (qemu_file_get_error(f
)) {
542 if (f
->xfer_limit
> 0 && f
->bytes_xfer
> f
->xfer_limit
) {
548 int64_t qemu_file_get_rate_limit(QEMUFile
*f
)
550 return f
->xfer_limit
;
553 void qemu_file_set_rate_limit(QEMUFile
*f
, int64_t limit
)
555 f
->xfer_limit
= limit
;
558 void qemu_file_reset_rate_limit(QEMUFile
*f
)
563 void qemu_put_be16(QEMUFile
*f
, unsigned int v
)
565 qemu_put_byte(f
, v
>> 8);
569 void qemu_put_be32(QEMUFile
*f
, unsigned int v
)
571 qemu_put_byte(f
, v
>> 24);
572 qemu_put_byte(f
, v
>> 16);
573 qemu_put_byte(f
, v
>> 8);
577 void qemu_put_be64(QEMUFile
*f
, uint64_t v
)
579 qemu_put_be32(f
, v
>> 32);
583 unsigned int qemu_get_be16(QEMUFile
*f
)
586 v
= qemu_get_byte(f
) << 8;
587 v
|= qemu_get_byte(f
);
591 unsigned int qemu_get_be32(QEMUFile
*f
)
594 v
= (unsigned int)qemu_get_byte(f
) << 24;
595 v
|= qemu_get_byte(f
) << 16;
596 v
|= qemu_get_byte(f
) << 8;
597 v
|= qemu_get_byte(f
);
601 uint64_t qemu_get_be64(QEMUFile
*f
)
604 v
= (uint64_t)qemu_get_be32(f
) << 32;
605 v
|= qemu_get_be32(f
);
609 /* compress size bytes of data start at p with specific compression
610 * level and store the compressed data to the buffer of f.
613 ssize_t
qemu_put_compression_data(QEMUFile
*f
, const uint8_t *p
, size_t size
,
616 ssize_t blen
= IO_BUF_SIZE
- f
->buf_index
- sizeof(int32_t);
618 if (blen
< compressBound(size
)) {
621 if (compress2(f
->buf
+ f
->buf_index
+ sizeof(int32_t), (uLongf
*)&blen
,
622 (Bytef
*)p
, size
, level
) != Z_OK
) {
623 error_report("Compress Failed!");
626 qemu_put_be32(f
, blen
);
627 f
->buf_index
+= blen
;
628 return blen
+ sizeof(int32_t);
631 /* Put the data in the buffer of f_src to the buffer of f_des, and
632 * then reset the buf_index of f_src to 0.
635 int qemu_put_qemu_file(QEMUFile
*f_des
, QEMUFile
*f_src
)
639 if (f_src
->buf_index
> 0) {
640 len
= f_src
->buf_index
;
641 qemu_put_buffer(f_des
, f_src
->buf
, f_src
->buf_index
);
642 f_src
->buf_index
= 0;
648 * Get a string whose length is determined by a single preceding byte
649 * A preallocated 256 byte buffer must be passed in.
650 * Returns: len on success and a 0 terminated string in the buffer
652 * (Note a 0 length string will return 0 either way)
654 size_t qemu_get_counted_string(QEMUFile
*f
, char buf
[256])
656 size_t len
= qemu_get_byte(f
);
657 size_t res
= qemu_get_buffer(f
, (uint8_t *)buf
, len
);
661 return res
== len
? res
: 0;
665 * Set the blocking state of the QEMUFile.
666 * Note: On some transports the OS only keeps a single blocking state for
667 * both directions, and thus changing the blocking on the main
668 * QEMUFile can also affect the return path.
670 void qemu_file_set_blocking(QEMUFile
*f
, bool block
)
673 qemu_set_block(qemu_get_fd(f
));
675 qemu_set_nonblock(qemu_get_fd(f
));