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/osdep.h"
26 #include "qemu/madvise.h"
27 #include "qemu/error-report.h"
29 #include "migration.h"
30 #include "qemu-file.h"
32 #include "qapi/error.h"
34 #define IO_BUF_SIZE 32768
35 #define MAX_IOV_SIZE MIN_CONST(IOV_MAX, 64)
38 const QEMUFileHooks
*hooks
;
43 * Maximum amount of data in bytes to transfer during one
44 * rate limiting time window
46 int64_t rate_limit_max
;
48 * Total amount of data in bytes queued for transfer
49 * during this rate limiting time window
51 int64_t rate_limit_used
;
53 /* The sum of bytes transferred on the wire */
54 int64_t total_transferred
;
57 int buf_size
; /* 0 when writing */
58 uint8_t buf
[IO_BUF_SIZE
];
60 DECLARE_BITMAP(may_free
, MAX_IOV_SIZE
);
61 struct iovec iov
[MAX_IOV_SIZE
];
65 Error
*last_error_obj
;
66 /* has the file has been shutdown */
71 * Stop a file from being read/written - not all backing files can do this
72 * typically only sockets can.
74 * TODO: convert to propagate Error objects instead of squashing
75 * to a fixed errno value
77 int qemu_file_shutdown(QEMUFile
*f
)
84 * We must set qemufile error before the real shutdown(), otherwise
85 * there can be a race window where we thought IO all went though
86 * (because last_error==NULL) but actually IO has already stopped.
88 * If without correct ordering, the race can happen like this:
90 * page receiver other thread
91 * ------------- ------------
94 * returns 0 (buffer all zero)
95 * (we didn't check this retcode)
96 * try to detect IO error
97 * last_error==NULL, IO okay
98 * install ALL-ZERO page
102 if (!f
->last_error
) {
103 qemu_file_set_error(f
, -EIO
);
106 if (!qio_channel_has_feature(f
->ioc
,
107 QIO_CHANNEL_FEATURE_SHUTDOWN
)) {
111 if (qio_channel_shutdown(f
->ioc
, QIO_CHANNEL_SHUTDOWN_BOTH
, NULL
) < 0) {
118 bool qemu_file_mode_is_not_valid(const char *mode
)
121 (mode
[0] != 'r' && mode
[0] != 'w') ||
122 mode
[1] != 'b' || mode
[2] != 0) {
123 fprintf(stderr
, "qemu_fopen: Argument validity check failed\n");
130 static QEMUFile
*qemu_file_new_impl(QIOChannel
*ioc
, bool is_writable
)
134 f
= g_new0(QEMUFile
, 1);
138 f
->is_writable
= is_writable
;
144 * Result: QEMUFile* for a 'return path' for comms in the opposite direction
145 * NULL if not available
147 QEMUFile
*qemu_file_get_return_path(QEMUFile
*f
)
149 return qemu_file_new_impl(f
->ioc
, !f
->is_writable
);
152 QEMUFile
*qemu_file_new_output(QIOChannel
*ioc
)
154 return qemu_file_new_impl(ioc
, true);
157 QEMUFile
*qemu_file_new_input(QIOChannel
*ioc
)
159 return qemu_file_new_impl(ioc
, false);
162 void qemu_file_set_hooks(QEMUFile
*f
, const QEMUFileHooks
*hooks
)
168 * Get last error for stream f with optional Error*
170 * Return negative error value if there has been an error on previous
171 * operations, return 0 if no error happened.
172 * Optional, it returns Error* in errp, but it may be NULL even if return value
176 int qemu_file_get_error_obj(QEMUFile
*f
, Error
**errp
)
179 *errp
= f
->last_error_obj
? error_copy(f
->last_error_obj
) : NULL
;
181 return f
->last_error
;
185 * Get last error for either stream f1 or f2 with optional Error*.
186 * The error returned (non-zero) can be either from f1 or f2.
188 * If any of the qemufile* is NULL, then skip the check on that file.
190 * When there is no error on both qemufile, zero is returned.
192 int qemu_file_get_error_obj_any(QEMUFile
*f1
, QEMUFile
*f2
, Error
**errp
)
197 ret
= qemu_file_get_error_obj(f1
, errp
);
198 /* If there's already error detected, return */
205 ret
= qemu_file_get_error_obj(f2
, errp
);
212 * Set the last error for stream f with optional Error*
214 void qemu_file_set_error_obj(QEMUFile
*f
, int ret
, Error
*err
)
216 if (f
->last_error
== 0 && ret
) {
218 error_propagate(&f
->last_error_obj
, err
);
220 error_report_err(err
);
225 * Get last error for stream f
227 * Return negative error value if there has been an error on previous
228 * operations, return 0 if no error happened.
231 int qemu_file_get_error(QEMUFile
*f
)
233 return qemu_file_get_error_obj(f
, NULL
);
237 * Set the last error for stream f
239 void qemu_file_set_error(QEMUFile
*f
, int ret
)
241 qemu_file_set_error_obj(f
, ret
, NULL
);
244 bool qemu_file_is_writable(QEMUFile
*f
)
246 return f
->is_writable
;
249 static void qemu_iovec_release_ram(QEMUFile
*f
)
254 /* Find and release all the contiguous memory ranges marked as may_free. */
255 idx
= find_next_bit(f
->may_free
, f
->iovcnt
, 0);
256 if (idx
>= f
->iovcnt
) {
261 /* The madvise() in the loop is called for iov within a continuous range and
262 * then reinitialize the iov. And in the end, madvise() is called for the
265 while ((idx
= find_next_bit(f
->may_free
, f
->iovcnt
, idx
+ 1)) < f
->iovcnt
) {
266 /* check for adjacent buffer and coalesce them */
267 if (iov
.iov_base
+ iov
.iov_len
== f
->iov
[idx
].iov_base
) {
268 iov
.iov_len
+= f
->iov
[idx
].iov_len
;
271 if (qemu_madvise(iov
.iov_base
, iov
.iov_len
, QEMU_MADV_DONTNEED
) < 0) {
272 error_report("migrate: madvise DONTNEED failed %p %zd: %s",
273 iov
.iov_base
, iov
.iov_len
, strerror(errno
));
277 if (qemu_madvise(iov
.iov_base
, iov
.iov_len
, QEMU_MADV_DONTNEED
) < 0) {
278 error_report("migrate: madvise DONTNEED failed %p %zd: %s",
279 iov
.iov_base
, iov
.iov_len
, strerror(errno
));
281 memset(f
->may_free
, 0, sizeof(f
->may_free
));
286 * Flushes QEMUFile buffer
288 * This will flush all pending data. If data was only partially flushed, it
289 * will set an error state.
291 void qemu_fflush(QEMUFile
*f
)
293 if (!qemu_file_is_writable(f
)) {
301 Error
*local_error
= NULL
;
302 if (qio_channel_writev_all(f
->ioc
,
305 qemu_file_set_error_obj(f
, -EIO
, local_error
);
307 f
->total_transferred
+= iov_size(f
->iov
, f
->iovcnt
);
310 qemu_iovec_release_ram(f
);
317 void ram_control_before_iterate(QEMUFile
*f
, uint64_t flags
)
321 if (f
->hooks
&& f
->hooks
->before_ram_iterate
) {
322 ret
= f
->hooks
->before_ram_iterate(f
, flags
, NULL
);
324 qemu_file_set_error(f
, ret
);
329 void ram_control_after_iterate(QEMUFile
*f
, uint64_t flags
)
333 if (f
->hooks
&& f
->hooks
->after_ram_iterate
) {
334 ret
= f
->hooks
->after_ram_iterate(f
, flags
, NULL
);
336 qemu_file_set_error(f
, ret
);
341 void ram_control_load_hook(QEMUFile
*f
, uint64_t flags
, void *data
)
345 if (f
->hooks
&& f
->hooks
->hook_ram_load
) {
346 ret
= f
->hooks
->hook_ram_load(f
, flags
, data
);
348 qemu_file_set_error(f
, ret
);
352 * Hook is a hook specifically requested by the source sending a flag
353 * that expects there to be a hook on the destination.
355 if (flags
== RAM_CONTROL_HOOK
) {
356 qemu_file_set_error(f
, ret
);
361 size_t ram_control_save_page(QEMUFile
*f
, ram_addr_t block_offset
,
362 ram_addr_t offset
, size_t size
,
363 uint64_t *bytes_sent
)
365 if (f
->hooks
&& f
->hooks
->save_page
) {
366 int ret
= f
->hooks
->save_page(f
, block_offset
,
367 offset
, size
, bytes_sent
);
368 if (ret
!= RAM_SAVE_CONTROL_NOT_SUPP
) {
369 f
->rate_limit_used
+= size
;
372 if (ret
!= RAM_SAVE_CONTROL_DELAYED
&&
373 ret
!= RAM_SAVE_CONTROL_NOT_SUPP
) {
374 if (bytes_sent
&& *bytes_sent
> 0) {
375 qemu_file_credit_transfer(f
, *bytes_sent
);
376 } else if (ret
< 0) {
377 qemu_file_set_error(f
, ret
);
384 return RAM_SAVE_CONTROL_NOT_SUPP
;
388 * Attempt to fill the buffer from the underlying file
389 * Returns the number of bytes read, or negative value for an error.
391 * Note that it can return a partially full buffer even in a not error/not EOF
392 * case if the underlying file descriptor gives a short read, and that can
393 * happen even on a blocking fd.
395 static ssize_t
qemu_fill_buffer(QEMUFile
*f
)
399 Error
*local_error
= NULL
;
401 assert(!qemu_file_is_writable(f
));
403 pending
= f
->buf_size
- f
->buf_index
;
405 memmove(f
->buf
, f
->buf
+ f
->buf_index
, pending
);
408 f
->buf_size
= pending
;
415 len
= qio_channel_read(f
->ioc
,
416 (char *)f
->buf
+ pending
,
417 IO_BUF_SIZE
- pending
,
419 if (len
== QIO_CHANNEL_ERR_BLOCK
) {
420 if (qemu_in_coroutine()) {
421 qio_channel_yield(f
->ioc
, G_IO_IN
);
423 qio_channel_wait(f
->ioc
, G_IO_IN
);
425 } else if (len
< 0) {
428 } while (len
== QIO_CHANNEL_ERR_BLOCK
);
432 f
->total_transferred
+= len
;
433 } else if (len
== 0) {
434 qemu_file_set_error_obj(f
, -EIO
, local_error
);
436 qemu_file_set_error_obj(f
, len
, local_error
);
442 void qemu_file_credit_transfer(QEMUFile
*f
, size_t size
)
444 f
->total_transferred
+= size
;
449 * Returns negative error value if any error happened on previous operations or
450 * while closing the file. Returns 0 or positive number on success.
452 * The meaning of return value on success depends on the specific backend
455 int qemu_fclose(QEMUFile
*f
)
459 ret
= qemu_file_get_error(f
);
461 ret2
= qio_channel_close(f
->ioc
, NULL
);
465 g_clear_pointer(&f
->ioc
, object_unref
);
467 /* If any error was spotted before closing, we should report it
468 * instead of the close() return value.
473 error_free(f
->last_error_obj
);
475 trace_qemu_file_fclose();
480 * Add buf to iovec. Do flush if iovec is full.
483 * 1 iovec is full and flushed
484 * 0 iovec is not flushed
487 static int add_to_iovec(QEMUFile
*f
, const uint8_t *buf
, size_t size
,
490 /* check for adjacent buffer and coalesce them */
491 if (f
->iovcnt
> 0 && buf
== f
->iov
[f
->iovcnt
- 1].iov_base
+
492 f
->iov
[f
->iovcnt
- 1].iov_len
&&
493 may_free
== test_bit(f
->iovcnt
- 1, f
->may_free
))
495 f
->iov
[f
->iovcnt
- 1].iov_len
+= size
;
497 if (f
->iovcnt
>= MAX_IOV_SIZE
) {
498 /* Should only happen if a previous fflush failed */
499 assert(f
->shutdown
|| !qemu_file_is_writable(f
));
503 set_bit(f
->iovcnt
, f
->may_free
);
505 f
->iov
[f
->iovcnt
].iov_base
= (uint8_t *)buf
;
506 f
->iov
[f
->iovcnt
++].iov_len
= size
;
509 if (f
->iovcnt
>= MAX_IOV_SIZE
) {
517 static void add_buf_to_iovec(QEMUFile
*f
, size_t len
)
519 if (!add_to_iovec(f
, f
->buf
+ f
->buf_index
, len
, false)) {
521 if (f
->buf_index
== IO_BUF_SIZE
) {
527 void qemu_put_buffer_async(QEMUFile
*f
, const uint8_t *buf
, size_t size
,
534 f
->rate_limit_used
+= size
;
535 add_to_iovec(f
, buf
, size
, may_free
);
538 void qemu_put_buffer(QEMUFile
*f
, const uint8_t *buf
, size_t size
)
547 l
= IO_BUF_SIZE
- f
->buf_index
;
551 memcpy(f
->buf
+ f
->buf_index
, buf
, l
);
552 f
->rate_limit_used
+= l
;
553 add_buf_to_iovec(f
, l
);
554 if (qemu_file_get_error(f
)) {
562 void qemu_put_byte(QEMUFile
*f
, int v
)
568 f
->buf
[f
->buf_index
] = v
;
569 f
->rate_limit_used
++;
570 add_buf_to_iovec(f
, 1);
573 void qemu_file_skip(QEMUFile
*f
, int size
)
575 if (f
->buf_index
+ size
<= f
->buf_size
) {
576 f
->buf_index
+= size
;
581 * Read 'size' bytes from file (at 'offset') without moving the
582 * pointer and set 'buf' to point to that data.
584 * It will return size bytes unless there was an error, in which case it will
585 * return as many as it managed to read (assuming blocking fd's which
586 * all current QEMUFile are)
588 size_t qemu_peek_buffer(QEMUFile
*f
, uint8_t **buf
, size_t size
, size_t offset
)
593 assert(!qemu_file_is_writable(f
));
594 assert(offset
< IO_BUF_SIZE
);
595 assert(size
<= IO_BUF_SIZE
- offset
);
597 /* The 1st byte to read from */
598 index
= f
->buf_index
+ offset
;
599 /* The number of available bytes starting at index */
600 pending
= f
->buf_size
- index
;
603 * qemu_fill_buffer might return just a few bytes, even when there isn't
604 * an error, so loop collecting them until we get enough.
606 while (pending
< size
) {
607 int received
= qemu_fill_buffer(f
);
613 index
= f
->buf_index
+ offset
;
614 pending
= f
->buf_size
- index
;
620 if (size
> pending
) {
624 *buf
= f
->buf
+ index
;
629 * Read 'size' bytes of data from the file into buf.
630 * 'size' can be larger than the internal buffer.
632 * It will return size bytes unless there was an error, in which case it will
633 * return as many as it managed to read (assuming blocking fd's which
634 * all current QEMUFile are)
636 size_t qemu_get_buffer(QEMUFile
*f
, uint8_t *buf
, size_t size
)
638 size_t pending
= size
;
641 while (pending
> 0) {
645 res
= qemu_peek_buffer(f
, &src
, MIN(pending
, IO_BUF_SIZE
), 0);
649 memcpy(buf
, src
, res
);
650 qemu_file_skip(f
, res
);
659 * Read 'size' bytes of data from the file.
660 * 'size' can be larger than the internal buffer.
663 * may be held on an internal buffer (in which case *buf is updated
664 * to point to it) that is valid until the next qemu_file operation.
666 * will be copied to the *buf that was passed in.
668 * The code tries to avoid the copy if possible.
670 * It will return size bytes unless there was an error, in which case it will
671 * return as many as it managed to read (assuming blocking fd's which
672 * all current QEMUFile are)
674 * Note: Since **buf may get changed, the caller should take care to
675 * keep a pointer to the original buffer if it needs to deallocate it.
677 size_t qemu_get_buffer_in_place(QEMUFile
*f
, uint8_t **buf
, size_t size
)
679 if (size
< IO_BUF_SIZE
) {
683 res
= qemu_peek_buffer(f
, &src
, size
, 0);
686 qemu_file_skip(f
, res
);
692 return qemu_get_buffer(f
, *buf
, size
);
696 * Peeks a single byte from the buffer; this isn't guaranteed to work if
697 * offset leaves a gap after the previous read/peeked data.
699 int qemu_peek_byte(QEMUFile
*f
, int offset
)
701 int index
= f
->buf_index
+ offset
;
703 assert(!qemu_file_is_writable(f
));
704 assert(offset
< IO_BUF_SIZE
);
706 if (index
>= f
->buf_size
) {
708 index
= f
->buf_index
+ offset
;
709 if (index
>= f
->buf_size
) {
713 return f
->buf
[index
];
716 int qemu_get_byte(QEMUFile
*f
)
720 result
= qemu_peek_byte(f
, 0);
721 qemu_file_skip(f
, 1);
725 int64_t qemu_file_total_transferred_fast(QEMUFile
*f
)
727 int64_t ret
= f
->total_transferred
;
730 for (i
= 0; i
< f
->iovcnt
; i
++) {
731 ret
+= f
->iov
[i
].iov_len
;
737 int64_t qemu_file_total_transferred(QEMUFile
*f
)
740 return f
->total_transferred
;
743 int qemu_file_rate_limit(QEMUFile
*f
)
748 if (qemu_file_get_error(f
)) {
751 if (f
->rate_limit_max
> 0 && f
->rate_limit_used
> f
->rate_limit_max
) {
757 int64_t qemu_file_get_rate_limit(QEMUFile
*f
)
759 return f
->rate_limit_max
;
762 void qemu_file_set_rate_limit(QEMUFile
*f
, int64_t limit
)
764 f
->rate_limit_max
= limit
;
767 void qemu_file_reset_rate_limit(QEMUFile
*f
)
769 f
->rate_limit_used
= 0;
772 void qemu_file_acct_rate_limit(QEMUFile
*f
, int64_t len
)
774 f
->rate_limit_used
+= len
;
777 void qemu_put_be16(QEMUFile
*f
, unsigned int v
)
779 qemu_put_byte(f
, v
>> 8);
783 void qemu_put_be32(QEMUFile
*f
, unsigned int v
)
785 qemu_put_byte(f
, v
>> 24);
786 qemu_put_byte(f
, v
>> 16);
787 qemu_put_byte(f
, v
>> 8);
791 void qemu_put_be64(QEMUFile
*f
, uint64_t v
)
793 qemu_put_be32(f
, v
>> 32);
797 unsigned int qemu_get_be16(QEMUFile
*f
)
800 v
= qemu_get_byte(f
) << 8;
801 v
|= qemu_get_byte(f
);
805 unsigned int qemu_get_be32(QEMUFile
*f
)
808 v
= (unsigned int)qemu_get_byte(f
) << 24;
809 v
|= qemu_get_byte(f
) << 16;
810 v
|= qemu_get_byte(f
) << 8;
811 v
|= qemu_get_byte(f
);
815 uint64_t qemu_get_be64(QEMUFile
*f
)
818 v
= (uint64_t)qemu_get_be32(f
) << 32;
819 v
|= qemu_get_be32(f
);
823 /* return the size after compression, or negative value on error */
824 static int qemu_compress_data(z_stream
*stream
, uint8_t *dest
, size_t dest_len
,
825 const uint8_t *source
, size_t source_len
)
829 err
= deflateReset(stream
);
834 stream
->avail_in
= source_len
;
835 stream
->next_in
= (uint8_t *)source
;
836 stream
->avail_out
= dest_len
;
837 stream
->next_out
= dest
;
839 err
= deflate(stream
, Z_FINISH
);
840 if (err
!= Z_STREAM_END
) {
844 return stream
->next_out
- dest
;
847 /* Compress size bytes of data start at p and store the compressed
848 * data to the buffer of f.
850 * Since the file is dummy file with empty_ops, return -1 if f has no space to
851 * save the compressed data.
853 ssize_t
qemu_put_compression_data(QEMUFile
*f
, z_stream
*stream
,
854 const uint8_t *p
, size_t size
)
856 ssize_t blen
= IO_BUF_SIZE
- f
->buf_index
- sizeof(int32_t);
858 if (blen
< compressBound(size
)) {
862 blen
= qemu_compress_data(stream
, f
->buf
+ f
->buf_index
+ sizeof(int32_t),
868 qemu_put_be32(f
, blen
);
869 add_buf_to_iovec(f
, blen
);
870 return blen
+ sizeof(int32_t);
873 /* Put the data in the buffer of f_src to the buffer of f_des, and
874 * then reset the buf_index of f_src to 0.
877 int qemu_put_qemu_file(QEMUFile
*f_des
, QEMUFile
*f_src
)
881 if (f_src
->buf_index
> 0) {
882 len
= f_src
->buf_index
;
883 qemu_put_buffer(f_des
, f_src
->buf
, f_src
->buf_index
);
884 f_src
->buf_index
= 0;
891 * Get a string whose length is determined by a single preceding byte
892 * A preallocated 256 byte buffer must be passed in.
893 * Returns: len on success and a 0 terminated string in the buffer
895 * (Note a 0 length string will return 0 either way)
897 size_t qemu_get_counted_string(QEMUFile
*f
, char buf
[256])
899 size_t len
= qemu_get_byte(f
);
900 size_t res
= qemu_get_buffer(f
, (uint8_t *)buf
, len
);
904 return res
== len
? res
: 0;
908 * Put a string with one preceding byte containing its length. The length of
909 * the string should be less than 256.
911 void qemu_put_counted_string(QEMUFile
*f
, const char *str
)
913 size_t len
= strlen(str
);
916 qemu_put_byte(f
, len
);
917 qemu_put_buffer(f
, (const uint8_t *)str
, len
);
921 * Set the blocking state of the QEMUFile.
922 * Note: On some transports the OS only keeps a single blocking state for
923 * both directions, and thus changing the blocking on the main
924 * QEMUFile can also affect the return path.
926 void qemu_file_set_blocking(QEMUFile
*f
, bool block
)
928 qio_channel_set_blocking(f
->ioc
, block
, NULL
);
934 * Get the ioc object for the file, without incrementing
935 * the reference count.
937 * Returns: the ioc object
939 QIOChannel
*qemu_file_get_ioc(QEMUFile
*file
)