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
)
82 if (!qio_channel_has_feature(f
->ioc
,
83 QIO_CHANNEL_FEATURE_SHUTDOWN
)) {
87 if (qio_channel_shutdown(f
->ioc
, QIO_CHANNEL_SHUTDOWN_BOTH
, NULL
) < 0) {
92 qemu_file_set_error(f
, -EIO
);
97 bool qemu_file_mode_is_not_valid(const char *mode
)
100 (mode
[0] != 'r' && mode
[0] != 'w') ||
101 mode
[1] != 'b' || mode
[2] != 0) {
102 fprintf(stderr
, "qemu_fopen: Argument validity check failed\n");
109 static QEMUFile
*qemu_file_new_impl(QIOChannel
*ioc
, bool is_writable
)
113 f
= g_new0(QEMUFile
, 1);
117 f
->is_writable
= is_writable
;
123 * Result: QEMUFile* for a 'return path' for comms in the opposite direction
124 * NULL if not available
126 QEMUFile
*qemu_file_get_return_path(QEMUFile
*f
)
128 return qemu_file_new_impl(f
->ioc
, !f
->is_writable
);
131 QEMUFile
*qemu_file_new_output(QIOChannel
*ioc
)
133 return qemu_file_new_impl(ioc
, true);
136 QEMUFile
*qemu_file_new_input(QIOChannel
*ioc
)
138 return qemu_file_new_impl(ioc
, false);
141 void qemu_file_set_hooks(QEMUFile
*f
, const QEMUFileHooks
*hooks
)
147 * Get last error for stream f with optional Error*
149 * Return negative error value if there has been an error on previous
150 * operations, return 0 if no error happened.
151 * Optional, it returns Error* in errp, but it may be NULL even if return value
155 int qemu_file_get_error_obj(QEMUFile
*f
, Error
**errp
)
158 *errp
= f
->last_error_obj
? error_copy(f
->last_error_obj
) : NULL
;
160 return f
->last_error
;
164 * Set the last error for stream f with optional Error*
166 void qemu_file_set_error_obj(QEMUFile
*f
, int ret
, Error
*err
)
168 if (f
->last_error
== 0 && ret
) {
170 error_propagate(&f
->last_error_obj
, err
);
172 error_report_err(err
);
177 * Get last error for stream f
179 * Return negative error value if there has been an error on previous
180 * operations, return 0 if no error happened.
183 int qemu_file_get_error(QEMUFile
*f
)
185 return qemu_file_get_error_obj(f
, NULL
);
189 * Set the last error for stream f
191 void qemu_file_set_error(QEMUFile
*f
, int ret
)
193 qemu_file_set_error_obj(f
, ret
, NULL
);
196 bool qemu_file_is_writable(QEMUFile
*f
)
198 return f
->is_writable
;
201 static void qemu_iovec_release_ram(QEMUFile
*f
)
206 /* Find and release all the contiguous memory ranges marked as may_free. */
207 idx
= find_next_bit(f
->may_free
, f
->iovcnt
, 0);
208 if (idx
>= f
->iovcnt
) {
213 /* The madvise() in the loop is called for iov within a continuous range and
214 * then reinitialize the iov. And in the end, madvise() is called for the
217 while ((idx
= find_next_bit(f
->may_free
, f
->iovcnt
, idx
+ 1)) < f
->iovcnt
) {
218 /* check for adjacent buffer and coalesce them */
219 if (iov
.iov_base
+ iov
.iov_len
== f
->iov
[idx
].iov_base
) {
220 iov
.iov_len
+= f
->iov
[idx
].iov_len
;
223 if (qemu_madvise(iov
.iov_base
, iov
.iov_len
, QEMU_MADV_DONTNEED
) < 0) {
224 error_report("migrate: madvise DONTNEED failed %p %zd: %s",
225 iov
.iov_base
, iov
.iov_len
, strerror(errno
));
229 if (qemu_madvise(iov
.iov_base
, iov
.iov_len
, QEMU_MADV_DONTNEED
) < 0) {
230 error_report("migrate: madvise DONTNEED failed %p %zd: %s",
231 iov
.iov_base
, iov
.iov_len
, strerror(errno
));
233 memset(f
->may_free
, 0, sizeof(f
->may_free
));
238 * Flushes QEMUFile buffer
240 * This will flush all pending data. If data was only partially flushed, it
241 * will set an error state.
243 void qemu_fflush(QEMUFile
*f
)
245 if (!qemu_file_is_writable(f
)) {
253 Error
*local_error
= NULL
;
254 if (qio_channel_writev_all(f
->ioc
,
257 qemu_file_set_error_obj(f
, -EIO
, local_error
);
259 f
->total_transferred
+= iov_size(f
->iov
, f
->iovcnt
);
262 qemu_iovec_release_ram(f
);
269 void ram_control_before_iterate(QEMUFile
*f
, uint64_t flags
)
273 if (f
->hooks
&& f
->hooks
->before_ram_iterate
) {
274 ret
= f
->hooks
->before_ram_iterate(f
, flags
, NULL
);
276 qemu_file_set_error(f
, ret
);
281 void ram_control_after_iterate(QEMUFile
*f
, uint64_t flags
)
285 if (f
->hooks
&& f
->hooks
->after_ram_iterate
) {
286 ret
= f
->hooks
->after_ram_iterate(f
, flags
, NULL
);
288 qemu_file_set_error(f
, ret
);
293 void ram_control_load_hook(QEMUFile
*f
, uint64_t flags
, void *data
)
297 if (f
->hooks
&& f
->hooks
->hook_ram_load
) {
298 ret
= f
->hooks
->hook_ram_load(f
, flags
, data
);
300 qemu_file_set_error(f
, ret
);
304 * Hook is a hook specifically requested by the source sending a flag
305 * that expects there to be a hook on the destination.
307 if (flags
== RAM_CONTROL_HOOK
) {
308 qemu_file_set_error(f
, ret
);
313 size_t ram_control_save_page(QEMUFile
*f
, ram_addr_t block_offset
,
314 ram_addr_t offset
, size_t size
,
315 uint64_t *bytes_sent
)
317 if (f
->hooks
&& f
->hooks
->save_page
) {
318 int ret
= f
->hooks
->save_page(f
, block_offset
,
319 offset
, size
, bytes_sent
);
320 if (ret
!= RAM_SAVE_CONTROL_NOT_SUPP
) {
321 f
->rate_limit_used
+= size
;
324 if (ret
!= RAM_SAVE_CONTROL_DELAYED
&&
325 ret
!= RAM_SAVE_CONTROL_NOT_SUPP
) {
326 if (bytes_sent
&& *bytes_sent
> 0) {
327 qemu_file_credit_transfer(f
, *bytes_sent
);
328 } else if (ret
< 0) {
329 qemu_file_set_error(f
, ret
);
336 return RAM_SAVE_CONTROL_NOT_SUPP
;
340 * Attempt to fill the buffer from the underlying file
341 * Returns the number of bytes read, or negative value for an error.
343 * Note that it can return a partially full buffer even in a not error/not EOF
344 * case if the underlying file descriptor gives a short read, and that can
345 * happen even on a blocking fd.
347 static ssize_t
qemu_fill_buffer(QEMUFile
*f
)
351 Error
*local_error
= NULL
;
353 assert(!qemu_file_is_writable(f
));
355 pending
= f
->buf_size
- f
->buf_index
;
357 memmove(f
->buf
, f
->buf
+ f
->buf_index
, pending
);
360 f
->buf_size
= pending
;
367 len
= qio_channel_read(f
->ioc
,
368 (char *)f
->buf
+ pending
,
369 IO_BUF_SIZE
- pending
,
371 if (len
== QIO_CHANNEL_ERR_BLOCK
) {
372 if (qemu_in_coroutine()) {
373 qio_channel_yield(f
->ioc
, G_IO_IN
);
375 qio_channel_wait(f
->ioc
, G_IO_IN
);
377 } else if (len
< 0) {
380 } while (len
== QIO_CHANNEL_ERR_BLOCK
);
384 f
->total_transferred
+= len
;
385 } else if (len
== 0) {
386 qemu_file_set_error_obj(f
, -EIO
, local_error
);
387 } else if (len
!= -EAGAIN
) {
388 qemu_file_set_error_obj(f
, len
, local_error
);
390 error_free(local_error
);
396 void qemu_file_credit_transfer(QEMUFile
*f
, size_t size
)
398 f
->total_transferred
+= size
;
403 * Returns negative error value if any error happened on previous operations or
404 * while closing the file. Returns 0 or positive number on success.
406 * The meaning of return value on success depends on the specific backend
409 int qemu_fclose(QEMUFile
*f
)
413 ret
= qemu_file_get_error(f
);
415 ret2
= qio_channel_close(f
->ioc
, NULL
);
419 g_clear_pointer(&f
->ioc
, object_unref
);
421 /* If any error was spotted before closing, we should report it
422 * instead of the close() return value.
427 error_free(f
->last_error_obj
);
429 trace_qemu_file_fclose();
434 * Add buf to iovec. Do flush if iovec is full.
437 * 1 iovec is full and flushed
438 * 0 iovec is not flushed
441 static int add_to_iovec(QEMUFile
*f
, const uint8_t *buf
, size_t size
,
444 /* check for adjacent buffer and coalesce them */
445 if (f
->iovcnt
> 0 && buf
== f
->iov
[f
->iovcnt
- 1].iov_base
+
446 f
->iov
[f
->iovcnt
- 1].iov_len
&&
447 may_free
== test_bit(f
->iovcnt
- 1, f
->may_free
))
449 f
->iov
[f
->iovcnt
- 1].iov_len
+= size
;
451 if (f
->iovcnt
>= MAX_IOV_SIZE
) {
452 /* Should only happen if a previous fflush failed */
453 assert(f
->shutdown
|| !qemu_file_is_writable(f
));
457 set_bit(f
->iovcnt
, f
->may_free
);
459 f
->iov
[f
->iovcnt
].iov_base
= (uint8_t *)buf
;
460 f
->iov
[f
->iovcnt
++].iov_len
= size
;
463 if (f
->iovcnt
>= MAX_IOV_SIZE
) {
471 static void add_buf_to_iovec(QEMUFile
*f
, size_t len
)
473 if (!add_to_iovec(f
, f
->buf
+ f
->buf_index
, len
, false)) {
475 if (f
->buf_index
== IO_BUF_SIZE
) {
481 void qemu_put_buffer_async(QEMUFile
*f
, const uint8_t *buf
, size_t size
,
488 f
->rate_limit_used
+= size
;
489 add_to_iovec(f
, buf
, size
, may_free
);
492 void qemu_put_buffer(QEMUFile
*f
, const uint8_t *buf
, size_t size
)
501 l
= IO_BUF_SIZE
- f
->buf_index
;
505 memcpy(f
->buf
+ f
->buf_index
, buf
, l
);
506 f
->rate_limit_used
+= l
;
507 add_buf_to_iovec(f
, l
);
508 if (qemu_file_get_error(f
)) {
516 void qemu_put_byte(QEMUFile
*f
, int v
)
522 f
->buf
[f
->buf_index
] = v
;
523 f
->rate_limit_used
++;
524 add_buf_to_iovec(f
, 1);
527 void qemu_file_skip(QEMUFile
*f
, int size
)
529 if (f
->buf_index
+ size
<= f
->buf_size
) {
530 f
->buf_index
+= size
;
535 * Read 'size' bytes from file (at 'offset') without moving the
536 * pointer and set 'buf' to point to that data.
538 * It will return size bytes unless there was an error, in which case it will
539 * return as many as it managed to read (assuming blocking fd's which
540 * all current QEMUFile are)
542 size_t qemu_peek_buffer(QEMUFile
*f
, uint8_t **buf
, size_t size
, size_t offset
)
547 assert(!qemu_file_is_writable(f
));
548 assert(offset
< IO_BUF_SIZE
);
549 assert(size
<= IO_BUF_SIZE
- offset
);
551 /* The 1st byte to read from */
552 index
= f
->buf_index
+ offset
;
553 /* The number of available bytes starting at index */
554 pending
= f
->buf_size
- index
;
557 * qemu_fill_buffer might return just a few bytes, even when there isn't
558 * an error, so loop collecting them until we get enough.
560 while (pending
< size
) {
561 int received
= qemu_fill_buffer(f
);
567 index
= f
->buf_index
+ offset
;
568 pending
= f
->buf_size
- index
;
574 if (size
> pending
) {
578 *buf
= f
->buf
+ index
;
583 * Read 'size' bytes of data from the file into buf.
584 * 'size' can be larger than the internal buffer.
586 * It will return size bytes unless there was an error, in which case it will
587 * return as many as it managed to read (assuming blocking fd's which
588 * all current QEMUFile are)
590 size_t qemu_get_buffer(QEMUFile
*f
, uint8_t *buf
, size_t size
)
592 size_t pending
= size
;
595 while (pending
> 0) {
599 res
= qemu_peek_buffer(f
, &src
, MIN(pending
, IO_BUF_SIZE
), 0);
603 memcpy(buf
, src
, res
);
604 qemu_file_skip(f
, res
);
613 * Read 'size' bytes of data from the file.
614 * 'size' can be larger than the internal buffer.
617 * may be held on an internal buffer (in which case *buf is updated
618 * to point to it) that is valid until the next qemu_file operation.
620 * will be copied to the *buf that was passed in.
622 * The code tries to avoid the copy if possible.
624 * It will return size bytes unless there was an error, in which case it will
625 * return as many as it managed to read (assuming blocking fd's which
626 * all current QEMUFile are)
628 * Note: Since **buf may get changed, the caller should take care to
629 * keep a pointer to the original buffer if it needs to deallocate it.
631 size_t qemu_get_buffer_in_place(QEMUFile
*f
, uint8_t **buf
, size_t size
)
633 if (size
< IO_BUF_SIZE
) {
637 res
= qemu_peek_buffer(f
, &src
, size
, 0);
640 qemu_file_skip(f
, res
);
646 return qemu_get_buffer(f
, *buf
, size
);
650 * Peeks a single byte from the buffer; this isn't guaranteed to work if
651 * offset leaves a gap after the previous read/peeked data.
653 int qemu_peek_byte(QEMUFile
*f
, int offset
)
655 int index
= f
->buf_index
+ offset
;
657 assert(!qemu_file_is_writable(f
));
658 assert(offset
< IO_BUF_SIZE
);
660 if (index
>= f
->buf_size
) {
662 index
= f
->buf_index
+ offset
;
663 if (index
>= f
->buf_size
) {
667 return f
->buf
[index
];
670 int qemu_get_byte(QEMUFile
*f
)
674 result
= qemu_peek_byte(f
, 0);
675 qemu_file_skip(f
, 1);
679 int64_t qemu_file_total_transferred_fast(QEMUFile
*f
)
681 int64_t ret
= f
->total_transferred
;
684 for (i
= 0; i
< f
->iovcnt
; i
++) {
685 ret
+= f
->iov
[i
].iov_len
;
691 int64_t qemu_file_total_transferred(QEMUFile
*f
)
694 return f
->total_transferred
;
697 int qemu_file_rate_limit(QEMUFile
*f
)
702 if (qemu_file_get_error(f
)) {
705 if (f
->rate_limit_max
> 0 && f
->rate_limit_used
> f
->rate_limit_max
) {
711 int64_t qemu_file_get_rate_limit(QEMUFile
*f
)
713 return f
->rate_limit_max
;
716 void qemu_file_set_rate_limit(QEMUFile
*f
, int64_t limit
)
718 f
->rate_limit_max
= limit
;
721 void qemu_file_reset_rate_limit(QEMUFile
*f
)
723 f
->rate_limit_used
= 0;
726 void qemu_file_acct_rate_limit(QEMUFile
*f
, int64_t len
)
728 f
->rate_limit_used
+= len
;
731 void qemu_put_be16(QEMUFile
*f
, unsigned int v
)
733 qemu_put_byte(f
, v
>> 8);
737 void qemu_put_be32(QEMUFile
*f
, unsigned int v
)
739 qemu_put_byte(f
, v
>> 24);
740 qemu_put_byte(f
, v
>> 16);
741 qemu_put_byte(f
, v
>> 8);
745 void qemu_put_be64(QEMUFile
*f
, uint64_t v
)
747 qemu_put_be32(f
, v
>> 32);
751 unsigned int qemu_get_be16(QEMUFile
*f
)
754 v
= qemu_get_byte(f
) << 8;
755 v
|= qemu_get_byte(f
);
759 unsigned int qemu_get_be32(QEMUFile
*f
)
762 v
= (unsigned int)qemu_get_byte(f
) << 24;
763 v
|= qemu_get_byte(f
) << 16;
764 v
|= qemu_get_byte(f
) << 8;
765 v
|= qemu_get_byte(f
);
769 uint64_t qemu_get_be64(QEMUFile
*f
)
772 v
= (uint64_t)qemu_get_be32(f
) << 32;
773 v
|= qemu_get_be32(f
);
777 /* return the size after compression, or negative value on error */
778 static int qemu_compress_data(z_stream
*stream
, uint8_t *dest
, size_t dest_len
,
779 const uint8_t *source
, size_t source_len
)
783 err
= deflateReset(stream
);
788 stream
->avail_in
= source_len
;
789 stream
->next_in
= (uint8_t *)source
;
790 stream
->avail_out
= dest_len
;
791 stream
->next_out
= dest
;
793 err
= deflate(stream
, Z_FINISH
);
794 if (err
!= Z_STREAM_END
) {
798 return stream
->next_out
- dest
;
801 /* Compress size bytes of data start at p and store the compressed
802 * data to the buffer of f.
804 * Since the file is dummy file with empty_ops, return -1 if f has no space to
805 * save the compressed data.
807 ssize_t
qemu_put_compression_data(QEMUFile
*f
, z_stream
*stream
,
808 const uint8_t *p
, size_t size
)
810 ssize_t blen
= IO_BUF_SIZE
- f
->buf_index
- sizeof(int32_t);
812 if (blen
< compressBound(size
)) {
816 blen
= qemu_compress_data(stream
, f
->buf
+ f
->buf_index
+ sizeof(int32_t),
822 qemu_put_be32(f
, blen
);
823 add_buf_to_iovec(f
, blen
);
824 return blen
+ sizeof(int32_t);
827 /* Put the data in the buffer of f_src to the buffer of f_des, and
828 * then reset the buf_index of f_src to 0.
831 int qemu_put_qemu_file(QEMUFile
*f_des
, QEMUFile
*f_src
)
835 if (f_src
->buf_index
> 0) {
836 len
= f_src
->buf_index
;
837 qemu_put_buffer(f_des
, f_src
->buf
, f_src
->buf_index
);
838 f_src
->buf_index
= 0;
845 * Get a string whose length is determined by a single preceding byte
846 * A preallocated 256 byte buffer must be passed in.
847 * Returns: len on success and a 0 terminated string in the buffer
849 * (Note a 0 length string will return 0 either way)
851 size_t qemu_get_counted_string(QEMUFile
*f
, char buf
[256])
853 size_t len
= qemu_get_byte(f
);
854 size_t res
= qemu_get_buffer(f
, (uint8_t *)buf
, len
);
858 return res
== len
? res
: 0;
862 * Put a string with one preceding byte containing its length. The length of
863 * the string should be less than 256.
865 void qemu_put_counted_string(QEMUFile
*f
, const char *str
)
867 size_t len
= strlen(str
);
870 qemu_put_byte(f
, len
);
871 qemu_put_buffer(f
, (const uint8_t *)str
, len
);
875 * Set the blocking state of the QEMUFile.
876 * Note: On some transports the OS only keeps a single blocking state for
877 * both directions, and thus changing the blocking on the main
878 * QEMUFile can also affect the return path.
880 void qemu_file_set_blocking(QEMUFile
*f
, bool block
)
882 qio_channel_set_blocking(f
->ioc
, block
, NULL
);
888 * Get the ioc object for the file, without incrementing
889 * the reference count.
891 * Returns: the ioc object
893 QIOChannel
*qemu_file_get_ioc(QEMUFile
*file
)