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-common.h"
26 #include "qemu/sockets.h"
27 #include "block/coroutine.h"
28 #include "migration/migration.h"
29 #include "migration/qemu-file.h"
32 #define IO_BUF_SIZE 32768
33 #define MAX_IOV_SIZE MIN(IOV_MAX, 64)
36 const QEMUFileOps
*ops
;
42 int64_t pos
; /* start of buffer when writing, end of buffer
45 int buf_size
; /* 0 when writing */
46 uint8_t buf
[IO_BUF_SIZE
];
48 struct iovec iov
[MAX_IOV_SIZE
];
54 bool qemu_file_mode_is_not_valid(const char *mode
)
57 (mode
[0] != 'r' && mode
[0] != 'w') ||
58 mode
[1] != 'b' || mode
[2] != 0) {
59 fprintf(stderr
, "qemu_fopen: Argument validity check failed\n");
66 QEMUFile
*qemu_fopen_ops(void *opaque
, const QEMUFileOps
*ops
)
70 f
= g_malloc0(sizeof(QEMUFile
));
78 * Get last error for stream f
80 * Return negative error value if there has been an error on previous
81 * operations, return 0 if no error happened.
84 int qemu_file_get_error(QEMUFile
*f
)
89 void qemu_file_set_error(QEMUFile
*f
, int ret
)
91 if (f
->last_error
== 0) {
96 bool qemu_file_is_writable(QEMUFile
*f
)
98 return f
->ops
->writev_buffer
|| f
->ops
->put_buffer
;
102 * Flushes QEMUFile buffer
104 * If there is writev_buffer QEMUFileOps it uses it otherwise uses
107 void qemu_fflush(QEMUFile
*f
)
111 if (!qemu_file_is_writable(f
)) {
115 if (f
->ops
->writev_buffer
) {
117 ret
= f
->ops
->writev_buffer(f
->opaque
, f
->iov
, f
->iovcnt
, f
->pos
);
120 if (f
->buf_index
> 0) {
121 ret
= f
->ops
->put_buffer(f
->opaque
, f
->buf
, f
->pos
, f
->buf_index
);
130 qemu_file_set_error(f
, ret
);
134 void ram_control_before_iterate(QEMUFile
*f
, uint64_t flags
)
138 if (f
->ops
->before_ram_iterate
) {
139 ret
= f
->ops
->before_ram_iterate(f
, f
->opaque
, flags
);
141 qemu_file_set_error(f
, ret
);
146 void ram_control_after_iterate(QEMUFile
*f
, uint64_t flags
)
150 if (f
->ops
->after_ram_iterate
) {
151 ret
= f
->ops
->after_ram_iterate(f
, f
->opaque
, flags
);
153 qemu_file_set_error(f
, ret
);
158 void ram_control_load_hook(QEMUFile
*f
, uint64_t flags
)
162 if (f
->ops
->hook_ram_load
) {
163 ret
= f
->ops
->hook_ram_load(f
, f
->opaque
, flags
);
165 qemu_file_set_error(f
, ret
);
168 qemu_file_set_error(f
, ret
);
172 size_t ram_control_save_page(QEMUFile
*f
, ram_addr_t block_offset
,
173 ram_addr_t offset
, size_t size
, int *bytes_sent
)
175 if (f
->ops
->save_page
) {
176 int ret
= f
->ops
->save_page(f
, f
->opaque
, block_offset
,
177 offset
, size
, bytes_sent
);
179 if (ret
!= RAM_SAVE_CONTROL_DELAYED
) {
180 if (bytes_sent
&& *bytes_sent
> 0) {
181 qemu_update_position(f
, *bytes_sent
);
182 } else if (ret
< 0) {
183 qemu_file_set_error(f
, ret
);
190 return RAM_SAVE_CONTROL_NOT_SUPP
;
194 * Attempt to fill the buffer from the underlying file
195 * Returns the number of bytes read, or negative value for an error.
197 * Note that it can return a partially full buffer even in a not error/not EOF
198 * case if the underlying file descriptor gives a short read, and that can
199 * happen even on a blocking fd.
201 static ssize_t
qemu_fill_buffer(QEMUFile
*f
)
206 assert(!qemu_file_is_writable(f
));
208 pending
= f
->buf_size
- f
->buf_index
;
210 memmove(f
->buf
, f
->buf
+ f
->buf_index
, pending
);
213 f
->buf_size
= pending
;
215 len
= f
->ops
->get_buffer(f
->opaque
, f
->buf
+ pending
, f
->pos
,
216 IO_BUF_SIZE
- pending
);
220 } else if (len
== 0) {
221 qemu_file_set_error(f
, -EIO
);
222 } else if (len
!= -EAGAIN
) {
223 qemu_file_set_error(f
, len
);
229 int qemu_get_fd(QEMUFile
*f
)
231 if (f
->ops
->get_fd
) {
232 return f
->ops
->get_fd(f
->opaque
);
237 void qemu_update_position(QEMUFile
*f
, size_t size
)
244 * Returns negative error value if any error happened on previous operations or
245 * while closing the file. Returns 0 or positive number on success.
247 * The meaning of return value on success depends on the specific backend
250 int qemu_fclose(QEMUFile
*f
)
254 ret
= qemu_file_get_error(f
);
257 int ret2
= f
->ops
->close(f
->opaque
);
262 /* If any error was spotted before closing, we should report it
263 * instead of the close() return value.
269 trace_qemu_file_fclose();
273 static void add_to_iovec(QEMUFile
*f
, const uint8_t *buf
, int size
)
275 /* check for adjacent buffer and coalesce them */
276 if (f
->iovcnt
> 0 && buf
== f
->iov
[f
->iovcnt
- 1].iov_base
+
277 f
->iov
[f
->iovcnt
- 1].iov_len
) {
278 f
->iov
[f
->iovcnt
- 1].iov_len
+= size
;
280 f
->iov
[f
->iovcnt
].iov_base
= (uint8_t *)buf
;
281 f
->iov
[f
->iovcnt
++].iov_len
= size
;
284 if (f
->iovcnt
>= MAX_IOV_SIZE
) {
289 void qemu_put_buffer_async(QEMUFile
*f
, const uint8_t *buf
, int size
)
291 if (!f
->ops
->writev_buffer
) {
292 qemu_put_buffer(f
, buf
, size
);
300 f
->bytes_xfer
+= size
;
301 add_to_iovec(f
, buf
, size
);
304 void qemu_put_buffer(QEMUFile
*f
, const uint8_t *buf
, int size
)
313 l
= IO_BUF_SIZE
- f
->buf_index
;
317 memcpy(f
->buf
+ f
->buf_index
, buf
, l
);
319 if (f
->ops
->writev_buffer
) {
320 add_to_iovec(f
, f
->buf
+ f
->buf_index
, l
);
323 if (f
->buf_index
== IO_BUF_SIZE
) {
326 if (qemu_file_get_error(f
)) {
334 void qemu_put_byte(QEMUFile
*f
, int v
)
340 f
->buf
[f
->buf_index
] = v
;
342 if (f
->ops
->writev_buffer
) {
343 add_to_iovec(f
, f
->buf
+ f
->buf_index
, 1);
346 if (f
->buf_index
== IO_BUF_SIZE
) {
351 void qemu_file_skip(QEMUFile
*f
, int size
)
353 if (f
->buf_index
+ size
<= f
->buf_size
) {
354 f
->buf_index
+= size
;
359 * Read 'size' bytes from file (at 'offset') into buf without moving the
362 * It will return size bytes unless there was an error, in which case it will
363 * return as many as it managed to read (assuming blocking fd's which
364 * all current QEMUFile are)
366 int qemu_peek_buffer(QEMUFile
*f
, uint8_t *buf
, int size
, size_t offset
)
371 assert(!qemu_file_is_writable(f
));
372 assert(offset
< IO_BUF_SIZE
);
373 assert(size
<= IO_BUF_SIZE
- offset
);
375 /* The 1st byte to read from */
376 index
= f
->buf_index
+ offset
;
377 /* The number of available bytes starting at index */
378 pending
= f
->buf_size
- index
;
381 * qemu_fill_buffer might return just a few bytes, even when there isn't
382 * an error, so loop collecting them until we get enough.
384 while (pending
< size
) {
385 int received
= qemu_fill_buffer(f
);
391 index
= f
->buf_index
+ offset
;
392 pending
= f
->buf_size
- index
;
398 if (size
> pending
) {
402 memcpy(buf
, f
->buf
+ index
, size
);
407 * Read 'size' bytes of data from the file into buf.
408 * 'size' can be larger than the internal buffer.
410 * It will return size bytes unless there was an error, in which case it will
411 * return as many as it managed to read (assuming blocking fd's which
412 * all current QEMUFile are)
414 int qemu_get_buffer(QEMUFile
*f
, uint8_t *buf
, int size
)
419 while (pending
> 0) {
422 res
= qemu_peek_buffer(f
, buf
, MIN(pending
, IO_BUF_SIZE
), 0);
426 qemu_file_skip(f
, res
);
435 * Peeks a single byte from the buffer; this isn't guaranteed to work if
436 * offset leaves a gap after the previous read/peeked data.
438 int qemu_peek_byte(QEMUFile
*f
, int offset
)
440 int index
= f
->buf_index
+ offset
;
442 assert(!qemu_file_is_writable(f
));
443 assert(offset
< IO_BUF_SIZE
);
445 if (index
>= f
->buf_size
) {
447 index
= f
->buf_index
+ offset
;
448 if (index
>= f
->buf_size
) {
452 return f
->buf
[index
];
455 int qemu_get_byte(QEMUFile
*f
)
459 result
= qemu_peek_byte(f
, 0);
460 qemu_file_skip(f
, 1);
464 int64_t qemu_ftell(QEMUFile
*f
)
470 int qemu_file_rate_limit(QEMUFile
*f
)
472 if (qemu_file_get_error(f
)) {
475 if (f
->xfer_limit
> 0 && f
->bytes_xfer
> f
->xfer_limit
) {
481 int64_t qemu_file_get_rate_limit(QEMUFile
*f
)
483 return f
->xfer_limit
;
486 void qemu_file_set_rate_limit(QEMUFile
*f
, int64_t limit
)
488 f
->xfer_limit
= limit
;
491 void qemu_file_reset_rate_limit(QEMUFile
*f
)
496 void qemu_put_be16(QEMUFile
*f
, unsigned int v
)
498 qemu_put_byte(f
, v
>> 8);
502 void qemu_put_be32(QEMUFile
*f
, unsigned int v
)
504 qemu_put_byte(f
, v
>> 24);
505 qemu_put_byte(f
, v
>> 16);
506 qemu_put_byte(f
, v
>> 8);
510 void qemu_put_be64(QEMUFile
*f
, uint64_t v
)
512 qemu_put_be32(f
, v
>> 32);
516 unsigned int qemu_get_be16(QEMUFile
*f
)
519 v
= qemu_get_byte(f
) << 8;
520 v
|= qemu_get_byte(f
);
524 unsigned int qemu_get_be32(QEMUFile
*f
)
527 v
= qemu_get_byte(f
) << 24;
528 v
|= qemu_get_byte(f
) << 16;
529 v
|= qemu_get_byte(f
) << 8;
530 v
|= qemu_get_byte(f
);
534 uint64_t qemu_get_be64(QEMUFile
*f
)
537 v
= (uint64_t)qemu_get_be32(f
) << 32;
538 v
|= qemu_get_be32(f
);
542 #define QSB_CHUNK_SIZE (1 << 10)
543 #define QSB_MAX_CHUNK_SIZE (16 * QSB_CHUNK_SIZE)
546 * Create a QEMUSizedBuffer
547 * This type of buffer uses scatter-gather lists internally and
548 * can grow to any size. Any data array in the scatter-gather list
549 * can hold different amount of bytes.
551 * @buffer: Optional buffer to copy into the QSB
552 * @len: size of initial buffer; if @buffer is given, buffer must
553 * hold at least len bytes
555 * Returns a pointer to a QEMUSizedBuffer or NULL on allocation failure
557 QEMUSizedBuffer
*qsb_create(const uint8_t *buffer
, size_t len
)
559 QEMUSizedBuffer
*qsb
;
560 size_t alloc_len
, num_chunks
, i
, to_copy
;
561 size_t chunk_size
= (len
> QSB_MAX_CHUNK_SIZE
)
565 num_chunks
= DIV_ROUND_UP(len
? len
: QSB_CHUNK_SIZE
, chunk_size
);
566 alloc_len
= num_chunks
* chunk_size
;
568 qsb
= g_try_new0(QEMUSizedBuffer
, 1);
573 qsb
->iov
= g_try_new0(struct iovec
, num_chunks
);
579 qsb
->n_iov
= num_chunks
;
581 for (i
= 0; i
< num_chunks
; i
++) {
582 qsb
->iov
[i
].iov_base
= g_try_malloc0(chunk_size
);
583 if (!qsb
->iov
[i
].iov_base
) {
584 /* qsb_free is safe since g_free can cope with NULL */
589 qsb
->iov
[i
].iov_len
= chunk_size
;
591 to_copy
= (len
- qsb
->used
) > chunk_size
592 ? chunk_size
: (len
- qsb
->used
);
593 memcpy(qsb
->iov
[i
].iov_base
, &buffer
[qsb
->used
], to_copy
);
594 qsb
->used
+= to_copy
;
598 qsb
->size
= alloc_len
;
604 * Free the QEMUSizedBuffer
606 * @qsb: The QEMUSizedBuffer to free
608 void qsb_free(QEMUSizedBuffer
*qsb
)
616 for (i
= 0; i
< qsb
->n_iov
; i
++) {
617 g_free(qsb
->iov
[i
].iov_base
);
624 * Get the number of used bytes in the QEMUSizedBuffer
626 * @qsb: A QEMUSizedBuffer
628 * Returns the number of bytes currently used in this buffer
630 size_t qsb_get_length(const QEMUSizedBuffer
*qsb
)
636 * Set the length of the buffer; the primary usage of this
637 * function is to truncate the number of used bytes in the buffer.
638 * The size will not be extended beyond the current number of
639 * allocated bytes in the QEMUSizedBuffer.
641 * @qsb: A QEMUSizedBuffer
642 * @new_len: The new length of bytes in the buffer
644 * Returns the number of bytes the buffer was truncated or extended
647 size_t qsb_set_length(QEMUSizedBuffer
*qsb
, size_t new_len
)
649 if (new_len
<= qsb
->size
) {
652 qsb
->used
= qsb
->size
;
658 * Get the iovec that holds the data for a given position @pos.
660 * @qsb: A QEMUSizedBuffer
661 * @pos: The index of a byte in the buffer
662 * @d_off: Pointer to an offset that this function will indicate
663 * at what position within the returned iovec the byte
666 * Returns the index of the iovec that holds the byte at the given
667 * index @pos in the byte stream; a negative number if the iovec
668 * for the given position @pos does not exist.
670 static ssize_t
qsb_get_iovec(const QEMUSizedBuffer
*qsb
,
671 off_t pos
, off_t
*d_off
)
676 if (pos
> qsb
->used
) {
680 for (i
= 0; i
< qsb
->n_iov
; i
++) {
681 if (curr
+ qsb
->iov
[i
].iov_len
> pos
) {
685 curr
+= qsb
->iov
[i
].iov_len
;
691 * Convert the QEMUSizedBuffer into a flat buffer.
693 * Note: If at all possible, try to avoid this function since it
694 * may unnecessarily copy memory around.
696 * @qsb: pointer to QEMUSizedBuffer
697 * @start: offset to start at
698 * @count: number of bytes to copy
699 * @buf: a pointer to a buffer to write into (at least @count bytes)
701 * Returns the number of bytes copied into the output buffer
703 ssize_t
qsb_get_buffer(const QEMUSizedBuffer
*qsb
, off_t start
,
704 size_t count
, uint8_t *buffer
)
706 const struct iovec
*iov
;
707 size_t to_copy
, all_copy
;
713 if (start
> qsb
->used
) {
717 all_copy
= qsb
->used
- start
;
718 if (all_copy
> count
) {
724 index
= qsb_get_iovec(qsb
, start
, &s_off
);
729 while (all_copy
> 0) {
730 iov
= &qsb
->iov
[index
];
734 to_copy
= iov
->iov_len
- s_off
;
735 if (to_copy
> all_copy
) {
738 memcpy(&buffer
[d_off
], &s
[s_off
], to_copy
);
751 * Grow the QEMUSizedBuffer to the given size and allocate
754 * @qsb: A QEMUSizedBuffer
755 * @new_size: The new size of the buffer
758 * a negative error code in case of memory allocation failure
760 * the new size of the buffer. The returned size may be greater or equal
763 static ssize_t
qsb_grow(QEMUSizedBuffer
*qsb
, size_t new_size
)
765 size_t needed_chunks
, i
;
767 if (qsb
->size
< new_size
) {
768 struct iovec
*new_iov
;
769 size_t size_diff
= new_size
- qsb
->size
;
770 size_t chunk_size
= (size_diff
> QSB_MAX_CHUNK_SIZE
)
771 ? QSB_MAX_CHUNK_SIZE
: QSB_CHUNK_SIZE
;
773 needed_chunks
= DIV_ROUND_UP(size_diff
, chunk_size
);
775 new_iov
= g_try_new(struct iovec
, qsb
->n_iov
+ needed_chunks
);
776 if (new_iov
== NULL
) {
780 /* Allocate new chunks as needed into new_iov */
781 for (i
= qsb
->n_iov
; i
< qsb
->n_iov
+ needed_chunks
; i
++) {
782 new_iov
[i
].iov_base
= g_try_malloc0(chunk_size
);
783 new_iov
[i
].iov_len
= chunk_size
;
784 if (!new_iov
[i
].iov_base
) {
787 /* Free previously allocated new chunks */
788 for (j
= qsb
->n_iov
; j
< i
; j
++) {
789 g_free(new_iov
[j
].iov_base
);
798 * Now we can't get any allocation errors, copy over to new iov
801 for (i
= 0; i
< qsb
->n_iov
; i
++) {
802 new_iov
[i
] = qsb
->iov
[i
];
805 qsb
->n_iov
+= needed_chunks
;
808 qsb
->size
+= (needed_chunks
* chunk_size
);
815 * Write into the QEMUSizedBuffer at a given position and a given
816 * number of bytes. This function will automatically grow the
819 * @qsb: A QEMUSizedBuffer
820 * @source: A byte array to copy data from
821 * @pos: The position within the @qsb to write data to
822 * @size: The number of bytes to copy into the @qsb
824 * Returns @size or a negative error code in case of memory allocation failure,
825 * or with an invalid 'pos'
827 ssize_t
qsb_write_at(QEMUSizedBuffer
*qsb
, const uint8_t *source
,
828 off_t pos
, size_t count
)
830 ssize_t rc
= qsb_grow(qsb
, pos
+ count
);
832 size_t all_copy
= count
;
833 const struct iovec
*iov
;
836 off_t d_off
, s_off
= 0;
842 if (pos
+ count
> qsb
->used
) {
843 qsb
->used
= pos
+ count
;
846 index
= qsb_get_iovec(qsb
, pos
, &d_off
);
851 while (all_copy
> 0) {
852 iov
= &qsb
->iov
[index
];
854 dest
= iov
->iov_base
;
856 to_copy
= iov
->iov_len
- d_off
;
857 if (to_copy
> all_copy
) {
861 memcpy(&dest
[d_off
], &source
[s_off
], to_copy
);
874 * Create a deep copy of the given QEMUSizedBuffer.
876 * @qsb: A QEMUSizedBuffer
878 * Returns a clone of @qsb or NULL on allocation failure
880 QEMUSizedBuffer
*qsb_clone(const QEMUSizedBuffer
*qsb
)
882 QEMUSizedBuffer
*out
= qsb_create(NULL
, qsb_get_length(qsb
));
891 for (i
= 0; i
< qsb
->n_iov
; i
++) {
892 res
= qsb_write_at(out
, qsb
->iov
[i
].iov_base
,
893 pos
, qsb
->iov
[i
].iov_len
);
904 typedef struct QEMUBuffer
{
905 QEMUSizedBuffer
*qsb
;
909 static int buf_get_buffer(void *opaque
, uint8_t *buf
, int64_t pos
, int size
)
911 QEMUBuffer
*s
= opaque
;
912 ssize_t len
= qsb_get_length(s
->qsb
) - pos
;
921 return qsb_get_buffer(s
->qsb
, pos
, len
, buf
);
924 static int buf_put_buffer(void *opaque
, const uint8_t *buf
,
925 int64_t pos
, int size
)
927 QEMUBuffer
*s
= opaque
;
929 return qsb_write_at(s
->qsb
, buf
, pos
, size
);
932 static int buf_close(void *opaque
)
934 QEMUBuffer
*s
= opaque
;
943 const QEMUSizedBuffer
*qemu_buf_get(QEMUFile
*f
)
954 static const QEMUFileOps buf_read_ops
= {
955 .get_buffer
= buf_get_buffer
,
959 static const QEMUFileOps buf_write_ops
= {
960 .put_buffer
= buf_put_buffer
,
964 QEMUFile
*qemu_bufopen(const char *mode
, QEMUSizedBuffer
*input
)
968 if (mode
== NULL
|| (mode
[0] != 'r' && mode
[0] != 'w') ||
970 error_report("qemu_bufopen: Argument validity check failed");
974 s
= g_malloc0(sizeof(QEMUBuffer
));
975 if (mode
[0] == 'r') {
979 if (s
->qsb
== NULL
) {
980 s
->qsb
= qsb_create(NULL
, 0);
984 error_report("qemu_bufopen: qsb_create failed");
989 if (mode
[0] == 'r') {
990 s
->file
= qemu_fopen_ops(s
, &buf_read_ops
);
992 s
->file
= qemu_fopen_ops(s
, &buf_write_ops
);