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 typedef struct QEMUFileStdio
{
59 typedef struct QEMUFileSocket
{
64 static ssize_t
socket_writev_buffer(void *opaque
, struct iovec
*iov
, int iovcnt
,
67 QEMUFileSocket
*s
= opaque
;
69 ssize_t size
= iov_size(iov
, iovcnt
);
71 len
= iov_send(s
->fd
, iov
, iovcnt
, 0, size
);
73 len
= -socket_error();
78 static int socket_get_fd(void *opaque
)
80 QEMUFileSocket
*s
= opaque
;
85 static int socket_get_buffer(void *opaque
, uint8_t *buf
, int64_t pos
, int size
)
87 QEMUFileSocket
*s
= opaque
;
91 len
= qemu_recv(s
->fd
, buf
, size
, 0);
95 if (socket_error() == EAGAIN
) {
96 yield_until_fd_readable(s
->fd
);
97 } else if (socket_error() != EINTR
) {
103 len
= -socket_error();
108 static int socket_close(void *opaque
)
110 QEMUFileSocket
*s
= opaque
;
116 static int stdio_get_fd(void *opaque
)
118 QEMUFileStdio
*s
= opaque
;
120 return fileno(s
->stdio_file
);
123 static int stdio_put_buffer(void *opaque
, const uint8_t *buf
, int64_t pos
,
126 QEMUFileStdio
*s
= opaque
;
129 res
= fwrite(buf
, 1, size
, s
->stdio_file
);
137 static int stdio_get_buffer(void *opaque
, uint8_t *buf
, int64_t pos
, int size
)
139 QEMUFileStdio
*s
= opaque
;
140 FILE *fp
= s
->stdio_file
;
145 bytes
= fread(buf
, 1, size
, fp
);
146 if (bytes
!= 0 || !ferror(fp
)) {
149 if (errno
== EAGAIN
) {
150 yield_until_fd_readable(fileno(fp
));
151 } else if (errno
!= EINTR
) {
158 static int stdio_pclose(void *opaque
)
160 QEMUFileStdio
*s
= opaque
;
162 ret
= pclose(s
->stdio_file
);
165 } else if (!WIFEXITED(ret
) || WEXITSTATUS(ret
) != 0) {
166 /* close succeeded, but non-zero exit code: */
167 ret
= -EIO
; /* fake errno value */
173 static int stdio_fclose(void *opaque
)
175 QEMUFileStdio
*s
= opaque
;
178 if (s
->file
->ops
->put_buffer
|| s
->file
->ops
->writev_buffer
) {
179 int fd
= fileno(s
->stdio_file
);
182 ret
= fstat(fd
, &st
);
183 if (ret
== 0 && S_ISREG(st
.st_mode
)) {
185 * If the file handle is a regular file make sure the
186 * data is flushed to disk before signaling success.
195 if (fclose(s
->stdio_file
) == EOF
) {
202 static const QEMUFileOps stdio_pipe_read_ops
= {
203 .get_fd
= stdio_get_fd
,
204 .get_buffer
= stdio_get_buffer
,
205 .close
= stdio_pclose
208 static const QEMUFileOps stdio_pipe_write_ops
= {
209 .get_fd
= stdio_get_fd
,
210 .put_buffer
= stdio_put_buffer
,
211 .close
= stdio_pclose
214 QEMUFile
*qemu_popen_cmd(const char *command
, const char *mode
)
219 if (mode
== NULL
|| (mode
[0] != 'r' && mode
[0] != 'w') || mode
[1] != 0) {
220 fprintf(stderr
, "qemu_popen: Argument validity check failed\n");
224 stdio_file
= popen(command
, mode
);
225 if (stdio_file
== NULL
) {
229 s
= g_malloc0(sizeof(QEMUFileStdio
));
231 s
->stdio_file
= stdio_file
;
233 if (mode
[0] == 'r') {
234 s
->file
= qemu_fopen_ops(s
, &stdio_pipe_read_ops
);
236 s
->file
= qemu_fopen_ops(s
, &stdio_pipe_write_ops
);
241 static const QEMUFileOps stdio_file_read_ops
= {
242 .get_fd
= stdio_get_fd
,
243 .get_buffer
= stdio_get_buffer
,
244 .close
= stdio_fclose
247 static const QEMUFileOps stdio_file_write_ops
= {
248 .get_fd
= stdio_get_fd
,
249 .put_buffer
= stdio_put_buffer
,
250 .close
= stdio_fclose
253 static ssize_t
unix_writev_buffer(void *opaque
, struct iovec
*iov
, int iovcnt
,
256 QEMUFileSocket
*s
= opaque
;
258 ssize_t size
= iov_size(iov
, iovcnt
);
264 /* Find the next start position; skip all full-sized vector elements */
265 while (offset
>= iov
[0].iov_len
) {
266 offset
-= iov
[0].iov_len
;
270 /* skip `offset' bytes from the (now) first element, undo it on exit */
272 iov
[0].iov_base
+= offset
;
273 iov
[0].iov_len
-= offset
;
276 len
= writev(s
->fd
, iov
, iovcnt
);
277 } while (len
== -1 && errno
== EINTR
);
282 /* Undo the changes above */
283 iov
[0].iov_base
-= offset
;
284 iov
[0].iov_len
+= offset
;
286 /* Prepare for the next iteration */
295 static int unix_get_buffer(void *opaque
, uint8_t *buf
, int64_t pos
, int size
)
297 QEMUFileSocket
*s
= opaque
;
301 len
= read(s
->fd
, buf
, size
);
305 if (errno
== EAGAIN
) {
306 yield_until_fd_readable(s
->fd
);
307 } else if (errno
!= EINTR
) {
318 static int unix_close(void *opaque
)
320 QEMUFileSocket
*s
= opaque
;
326 static const QEMUFileOps unix_read_ops
= {
327 .get_fd
= socket_get_fd
,
328 .get_buffer
= unix_get_buffer
,
332 static const QEMUFileOps unix_write_ops
= {
333 .get_fd
= socket_get_fd
,
334 .writev_buffer
= unix_writev_buffer
,
338 QEMUFile
*qemu_fdopen(int fd
, const char *mode
)
343 (mode
[0] != 'r' && mode
[0] != 'w') ||
344 mode
[1] != 'b' || mode
[2] != 0) {
345 fprintf(stderr
, "qemu_fdopen: Argument validity check failed\n");
349 s
= g_malloc0(sizeof(QEMUFileSocket
));
352 if (mode
[0] == 'r') {
353 s
->file
= qemu_fopen_ops(s
, &unix_read_ops
);
355 s
->file
= qemu_fopen_ops(s
, &unix_write_ops
);
360 static const QEMUFileOps socket_read_ops
= {
361 .get_fd
= socket_get_fd
,
362 .get_buffer
= socket_get_buffer
,
363 .close
= socket_close
366 static const QEMUFileOps socket_write_ops
= {
367 .get_fd
= socket_get_fd
,
368 .writev_buffer
= socket_writev_buffer
,
369 .close
= socket_close
372 bool qemu_file_mode_is_not_valid(const char *mode
)
375 (mode
[0] != 'r' && mode
[0] != 'w') ||
376 mode
[1] != 'b' || mode
[2] != 0) {
377 fprintf(stderr
, "qemu_fopen: Argument validity check failed\n");
384 QEMUFile
*qemu_fopen_socket(int fd
, const char *mode
)
388 if (qemu_file_mode_is_not_valid(mode
)) {
392 s
= g_malloc0(sizeof(QEMUFileSocket
));
394 if (mode
[0] == 'w') {
395 qemu_set_block(s
->fd
);
396 s
->file
= qemu_fopen_ops(s
, &socket_write_ops
);
398 s
->file
= qemu_fopen_ops(s
, &socket_read_ops
);
403 QEMUFile
*qemu_fopen(const char *filename
, const char *mode
)
407 if (qemu_file_mode_is_not_valid(mode
)) {
411 s
= g_malloc0(sizeof(QEMUFileStdio
));
413 s
->stdio_file
= fopen(filename
, mode
);
414 if (!s
->stdio_file
) {
418 if (mode
[0] == 'w') {
419 s
->file
= qemu_fopen_ops(s
, &stdio_file_write_ops
);
421 s
->file
= qemu_fopen_ops(s
, &stdio_file_read_ops
);
429 QEMUFile
*qemu_fopen_ops(void *opaque
, const QEMUFileOps
*ops
)
433 f
= g_malloc0(sizeof(QEMUFile
));
441 * Get last error for stream f
443 * Return negative error value if there has been an error on previous
444 * operations, return 0 if no error happened.
447 int qemu_file_get_error(QEMUFile
*f
)
449 return f
->last_error
;
452 void qemu_file_set_error(QEMUFile
*f
, int ret
)
454 if (f
->last_error
== 0) {
459 bool qemu_file_is_writable(QEMUFile
*f
)
461 return f
->ops
->writev_buffer
|| f
->ops
->put_buffer
;
465 * Flushes QEMUFile buffer
467 * If there is writev_buffer QEMUFileOps it uses it otherwise uses
470 void qemu_fflush(QEMUFile
*f
)
474 if (!qemu_file_is_writable(f
)) {
478 if (f
->ops
->writev_buffer
) {
480 ret
= f
->ops
->writev_buffer(f
->opaque
, f
->iov
, f
->iovcnt
, f
->pos
);
483 if (f
->buf_index
> 0) {
484 ret
= f
->ops
->put_buffer(f
->opaque
, f
->buf
, f
->pos
, f
->buf_index
);
493 qemu_file_set_error(f
, ret
);
497 void ram_control_before_iterate(QEMUFile
*f
, uint64_t flags
)
501 if (f
->ops
->before_ram_iterate
) {
502 ret
= f
->ops
->before_ram_iterate(f
, f
->opaque
, flags
);
504 qemu_file_set_error(f
, ret
);
509 void ram_control_after_iterate(QEMUFile
*f
, uint64_t flags
)
513 if (f
->ops
->after_ram_iterate
) {
514 ret
= f
->ops
->after_ram_iterate(f
, f
->opaque
, flags
);
516 qemu_file_set_error(f
, ret
);
521 void ram_control_load_hook(QEMUFile
*f
, uint64_t flags
)
525 if (f
->ops
->hook_ram_load
) {
526 ret
= f
->ops
->hook_ram_load(f
, f
->opaque
, flags
);
528 qemu_file_set_error(f
, ret
);
531 qemu_file_set_error(f
, ret
);
535 size_t ram_control_save_page(QEMUFile
*f
, ram_addr_t block_offset
,
536 ram_addr_t offset
, size_t size
, int *bytes_sent
)
538 if (f
->ops
->save_page
) {
539 int ret
= f
->ops
->save_page(f
, f
->opaque
, block_offset
,
540 offset
, size
, bytes_sent
);
542 if (ret
!= RAM_SAVE_CONTROL_DELAYED
) {
543 if (bytes_sent
&& *bytes_sent
> 0) {
544 qemu_update_position(f
, *bytes_sent
);
545 } else if (ret
< 0) {
546 qemu_file_set_error(f
, ret
);
553 return RAM_SAVE_CONTROL_NOT_SUPP
;
557 * Attempt to fill the buffer from the underlying file
558 * Returns the number of bytes read, or negative value for an error.
560 * Note that it can return a partially full buffer even in a not error/not EOF
561 * case if the underlying file descriptor gives a short read, and that can
562 * happen even on a blocking fd.
564 static ssize_t
qemu_fill_buffer(QEMUFile
*f
)
569 assert(!qemu_file_is_writable(f
));
571 pending
= f
->buf_size
- f
->buf_index
;
573 memmove(f
->buf
, f
->buf
+ f
->buf_index
, pending
);
576 f
->buf_size
= pending
;
578 len
= f
->ops
->get_buffer(f
->opaque
, f
->buf
+ pending
, f
->pos
,
579 IO_BUF_SIZE
- pending
);
583 } else if (len
== 0) {
584 qemu_file_set_error(f
, -EIO
);
585 } else if (len
!= -EAGAIN
) {
586 qemu_file_set_error(f
, len
);
592 int qemu_get_fd(QEMUFile
*f
)
594 if (f
->ops
->get_fd
) {
595 return f
->ops
->get_fd(f
->opaque
);
600 void qemu_update_position(QEMUFile
*f
, size_t size
)
607 * Returns negative error value if any error happened on previous operations or
608 * while closing the file. Returns 0 or positive number on success.
610 * The meaning of return value on success depends on the specific backend
613 int qemu_fclose(QEMUFile
*f
)
617 ret
= qemu_file_get_error(f
);
620 int ret2
= f
->ops
->close(f
->opaque
);
625 /* If any error was spotted before closing, we should report it
626 * instead of the close() return value.
632 trace_qemu_file_fclose();
636 static void add_to_iovec(QEMUFile
*f
, const uint8_t *buf
, int size
)
638 /* check for adjacent buffer and coalesce them */
639 if (f
->iovcnt
> 0 && buf
== f
->iov
[f
->iovcnt
- 1].iov_base
+
640 f
->iov
[f
->iovcnt
- 1].iov_len
) {
641 f
->iov
[f
->iovcnt
- 1].iov_len
+= size
;
643 f
->iov
[f
->iovcnt
].iov_base
= (uint8_t *)buf
;
644 f
->iov
[f
->iovcnt
++].iov_len
= size
;
647 if (f
->iovcnt
>= MAX_IOV_SIZE
) {
652 void qemu_put_buffer_async(QEMUFile
*f
, const uint8_t *buf
, int size
)
654 if (!f
->ops
->writev_buffer
) {
655 qemu_put_buffer(f
, buf
, size
);
663 f
->bytes_xfer
+= size
;
664 add_to_iovec(f
, buf
, size
);
667 void qemu_put_buffer(QEMUFile
*f
, const uint8_t *buf
, int size
)
676 l
= IO_BUF_SIZE
- f
->buf_index
;
680 memcpy(f
->buf
+ f
->buf_index
, buf
, l
);
682 if (f
->ops
->writev_buffer
) {
683 add_to_iovec(f
, f
->buf
+ f
->buf_index
, l
);
686 if (f
->buf_index
== IO_BUF_SIZE
) {
689 if (qemu_file_get_error(f
)) {
697 void qemu_put_byte(QEMUFile
*f
, int v
)
703 f
->buf
[f
->buf_index
] = v
;
705 if (f
->ops
->writev_buffer
) {
706 add_to_iovec(f
, f
->buf
+ f
->buf_index
, 1);
709 if (f
->buf_index
== IO_BUF_SIZE
) {
714 void qemu_file_skip(QEMUFile
*f
, int size
)
716 if (f
->buf_index
+ size
<= f
->buf_size
) {
717 f
->buf_index
+= size
;
722 * Read 'size' bytes from file (at 'offset') into buf without moving the
725 * It will return size bytes unless there was an error, in which case it will
726 * return as many as it managed to read (assuming blocking fd's which
727 * all current QEMUFile are)
729 int qemu_peek_buffer(QEMUFile
*f
, uint8_t *buf
, int size
, size_t offset
)
734 assert(!qemu_file_is_writable(f
));
735 assert(offset
< IO_BUF_SIZE
);
736 assert(size
<= IO_BUF_SIZE
- offset
);
738 /* The 1st byte to read from */
739 index
= f
->buf_index
+ offset
;
740 /* The number of available bytes starting at index */
741 pending
= f
->buf_size
- index
;
744 * qemu_fill_buffer might return just a few bytes, even when there isn't
745 * an error, so loop collecting them until we get enough.
747 while (pending
< size
) {
748 int received
= qemu_fill_buffer(f
);
754 index
= f
->buf_index
+ offset
;
755 pending
= f
->buf_size
- index
;
761 if (size
> pending
) {
765 memcpy(buf
, f
->buf
+ index
, size
);
770 * Read 'size' bytes of data from the file into buf.
771 * 'size' can be larger than the internal buffer.
773 * It will return size bytes unless there was an error, in which case it will
774 * return as many as it managed to read (assuming blocking fd's which
775 * all current QEMUFile are)
777 int qemu_get_buffer(QEMUFile
*f
, uint8_t *buf
, int size
)
782 while (pending
> 0) {
785 res
= qemu_peek_buffer(f
, buf
, MIN(pending
, IO_BUF_SIZE
), 0);
789 qemu_file_skip(f
, res
);
798 * Peeks a single byte from the buffer; this isn't guaranteed to work if
799 * offset leaves a gap after the previous read/peeked data.
801 int qemu_peek_byte(QEMUFile
*f
, int offset
)
803 int index
= f
->buf_index
+ offset
;
805 assert(!qemu_file_is_writable(f
));
806 assert(offset
< IO_BUF_SIZE
);
808 if (index
>= f
->buf_size
) {
810 index
= f
->buf_index
+ offset
;
811 if (index
>= f
->buf_size
) {
815 return f
->buf
[index
];
818 int qemu_get_byte(QEMUFile
*f
)
822 result
= qemu_peek_byte(f
, 0);
823 qemu_file_skip(f
, 1);
827 int64_t qemu_ftell(QEMUFile
*f
)
833 int qemu_file_rate_limit(QEMUFile
*f
)
835 if (qemu_file_get_error(f
)) {
838 if (f
->xfer_limit
> 0 && f
->bytes_xfer
> f
->xfer_limit
) {
844 int64_t qemu_file_get_rate_limit(QEMUFile
*f
)
846 return f
->xfer_limit
;
849 void qemu_file_set_rate_limit(QEMUFile
*f
, int64_t limit
)
851 f
->xfer_limit
= limit
;
854 void qemu_file_reset_rate_limit(QEMUFile
*f
)
859 void qemu_put_be16(QEMUFile
*f
, unsigned int v
)
861 qemu_put_byte(f
, v
>> 8);
865 void qemu_put_be32(QEMUFile
*f
, unsigned int v
)
867 qemu_put_byte(f
, v
>> 24);
868 qemu_put_byte(f
, v
>> 16);
869 qemu_put_byte(f
, v
>> 8);
873 void qemu_put_be64(QEMUFile
*f
, uint64_t v
)
875 qemu_put_be32(f
, v
>> 32);
879 unsigned int qemu_get_be16(QEMUFile
*f
)
882 v
= qemu_get_byte(f
) << 8;
883 v
|= qemu_get_byte(f
);
887 unsigned int qemu_get_be32(QEMUFile
*f
)
890 v
= qemu_get_byte(f
) << 24;
891 v
|= qemu_get_byte(f
) << 16;
892 v
|= qemu_get_byte(f
) << 8;
893 v
|= qemu_get_byte(f
);
897 uint64_t qemu_get_be64(QEMUFile
*f
)
900 v
= (uint64_t)qemu_get_be32(f
) << 32;
901 v
|= qemu_get_be32(f
);
905 #define QSB_CHUNK_SIZE (1 << 10)
906 #define QSB_MAX_CHUNK_SIZE (16 * QSB_CHUNK_SIZE)
909 * Create a QEMUSizedBuffer
910 * This type of buffer uses scatter-gather lists internally and
911 * can grow to any size. Any data array in the scatter-gather list
912 * can hold different amount of bytes.
914 * @buffer: Optional buffer to copy into the QSB
915 * @len: size of initial buffer; if @buffer is given, buffer must
916 * hold at least len bytes
918 * Returns a pointer to a QEMUSizedBuffer or NULL on allocation failure
920 QEMUSizedBuffer
*qsb_create(const uint8_t *buffer
, size_t len
)
922 QEMUSizedBuffer
*qsb
;
923 size_t alloc_len
, num_chunks
, i
, to_copy
;
924 size_t chunk_size
= (len
> QSB_MAX_CHUNK_SIZE
)
928 num_chunks
= DIV_ROUND_UP(len
? len
: QSB_CHUNK_SIZE
, chunk_size
);
929 alloc_len
= num_chunks
* chunk_size
;
931 qsb
= g_try_new0(QEMUSizedBuffer
, 1);
936 qsb
->iov
= g_try_new0(struct iovec
, num_chunks
);
942 qsb
->n_iov
= num_chunks
;
944 for (i
= 0; i
< num_chunks
; i
++) {
945 qsb
->iov
[i
].iov_base
= g_try_malloc0(chunk_size
);
946 if (!qsb
->iov
[i
].iov_base
) {
947 /* qsb_free is safe since g_free can cope with NULL */
952 qsb
->iov
[i
].iov_len
= chunk_size
;
954 to_copy
= (len
- qsb
->used
) > chunk_size
955 ? chunk_size
: (len
- qsb
->used
);
956 memcpy(qsb
->iov
[i
].iov_base
, &buffer
[qsb
->used
], to_copy
);
957 qsb
->used
+= to_copy
;
961 qsb
->size
= alloc_len
;
967 * Free the QEMUSizedBuffer
969 * @qsb: The QEMUSizedBuffer to free
971 void qsb_free(QEMUSizedBuffer
*qsb
)
979 for (i
= 0; i
< qsb
->n_iov
; i
++) {
980 g_free(qsb
->iov
[i
].iov_base
);
987 * Get the number of used bytes in the QEMUSizedBuffer
989 * @qsb: A QEMUSizedBuffer
991 * Returns the number of bytes currently used in this buffer
993 size_t qsb_get_length(const QEMUSizedBuffer
*qsb
)
999 * Set the length of the buffer; the primary usage of this
1000 * function is to truncate the number of used bytes in the buffer.
1001 * The size will not be extended beyond the current number of
1002 * allocated bytes in the QEMUSizedBuffer.
1004 * @qsb: A QEMUSizedBuffer
1005 * @new_len: The new length of bytes in the buffer
1007 * Returns the number of bytes the buffer was truncated or extended
1010 size_t qsb_set_length(QEMUSizedBuffer
*qsb
, size_t new_len
)
1012 if (new_len
<= qsb
->size
) {
1013 qsb
->used
= new_len
;
1015 qsb
->used
= qsb
->size
;
1021 * Get the iovec that holds the data for a given position @pos.
1023 * @qsb: A QEMUSizedBuffer
1024 * @pos: The index of a byte in the buffer
1025 * @d_off: Pointer to an offset that this function will indicate
1026 * at what position within the returned iovec the byte
1029 * Returns the index of the iovec that holds the byte at the given
1030 * index @pos in the byte stream; a negative number if the iovec
1031 * for the given position @pos does not exist.
1033 static ssize_t
qsb_get_iovec(const QEMUSizedBuffer
*qsb
,
1034 off_t pos
, off_t
*d_off
)
1039 if (pos
> qsb
->used
) {
1043 for (i
= 0; i
< qsb
->n_iov
; i
++) {
1044 if (curr
+ qsb
->iov
[i
].iov_len
> pos
) {
1045 *d_off
= pos
- curr
;
1048 curr
+= qsb
->iov
[i
].iov_len
;
1054 * Convert the QEMUSizedBuffer into a flat buffer.
1056 * Note: If at all possible, try to avoid this function since it
1057 * may unnecessarily copy memory around.
1059 * @qsb: pointer to QEMUSizedBuffer
1060 * @start: offset to start at
1061 * @count: number of bytes to copy
1062 * @buf: a pointer to a buffer to write into (at least @count bytes)
1064 * Returns the number of bytes copied into the output buffer
1066 ssize_t
qsb_get_buffer(const QEMUSizedBuffer
*qsb
, off_t start
,
1067 size_t count
, uint8_t *buffer
)
1069 const struct iovec
*iov
;
1070 size_t to_copy
, all_copy
;
1076 if (start
> qsb
->used
) {
1080 all_copy
= qsb
->used
- start
;
1081 if (all_copy
> count
) {
1087 index
= qsb_get_iovec(qsb
, start
, &s_off
);
1092 while (all_copy
> 0) {
1093 iov
= &qsb
->iov
[index
];
1097 to_copy
= iov
->iov_len
- s_off
;
1098 if (to_copy
> all_copy
) {
1101 memcpy(&buffer
[d_off
], &s
[s_off
], to_copy
);
1104 all_copy
-= to_copy
;
1114 * Grow the QEMUSizedBuffer to the given size and allocate
1117 * @qsb: A QEMUSizedBuffer
1118 * @new_size: The new size of the buffer
1121 * a negative error code in case of memory allocation failure
1123 * the new size of the buffer. The returned size may be greater or equal
1126 static ssize_t
qsb_grow(QEMUSizedBuffer
*qsb
, size_t new_size
)
1128 size_t needed_chunks
, i
;
1130 if (qsb
->size
< new_size
) {
1131 struct iovec
*new_iov
;
1132 size_t size_diff
= new_size
- qsb
->size
;
1133 size_t chunk_size
= (size_diff
> QSB_MAX_CHUNK_SIZE
)
1134 ? QSB_MAX_CHUNK_SIZE
: QSB_CHUNK_SIZE
;
1136 needed_chunks
= DIV_ROUND_UP(size_diff
, chunk_size
);
1138 new_iov
= g_try_new(struct iovec
, qsb
->n_iov
+ needed_chunks
);
1139 if (new_iov
== NULL
) {
1143 /* Allocate new chunks as needed into new_iov */
1144 for (i
= qsb
->n_iov
; i
< qsb
->n_iov
+ needed_chunks
; i
++) {
1145 new_iov
[i
].iov_base
= g_try_malloc0(chunk_size
);
1146 new_iov
[i
].iov_len
= chunk_size
;
1147 if (!new_iov
[i
].iov_base
) {
1150 /* Free previously allocated new chunks */
1151 for (j
= qsb
->n_iov
; j
< i
; j
++) {
1152 g_free(new_iov
[j
].iov_base
);
1161 * Now we can't get any allocation errors, copy over to new iov
1164 for (i
= 0; i
< qsb
->n_iov
; i
++) {
1165 new_iov
[i
] = qsb
->iov
[i
];
1168 qsb
->n_iov
+= needed_chunks
;
1171 qsb
->size
+= (needed_chunks
* chunk_size
);
1178 * Write into the QEMUSizedBuffer at a given position and a given
1179 * number of bytes. This function will automatically grow the
1182 * @qsb: A QEMUSizedBuffer
1183 * @source: A byte array to copy data from
1184 * @pos: The position within the @qsb to write data to
1185 * @size: The number of bytes to copy into the @qsb
1187 * Returns @size or a negative error code in case of memory allocation failure,
1188 * or with an invalid 'pos'
1190 ssize_t
qsb_write_at(QEMUSizedBuffer
*qsb
, const uint8_t *source
,
1191 off_t pos
, size_t count
)
1193 ssize_t rc
= qsb_grow(qsb
, pos
+ count
);
1195 size_t all_copy
= count
;
1196 const struct iovec
*iov
;
1199 off_t d_off
, s_off
= 0;
1205 if (pos
+ count
> qsb
->used
) {
1206 qsb
->used
= pos
+ count
;
1209 index
= qsb_get_iovec(qsb
, pos
, &d_off
);
1214 while (all_copy
> 0) {
1215 iov
= &qsb
->iov
[index
];
1217 dest
= iov
->iov_base
;
1219 to_copy
= iov
->iov_len
- d_off
;
1220 if (to_copy
> all_copy
) {
1224 memcpy(&dest
[d_off
], &source
[s_off
], to_copy
);
1227 all_copy
-= to_copy
;
1237 * Create a deep copy of the given QEMUSizedBuffer.
1239 * @qsb: A QEMUSizedBuffer
1241 * Returns a clone of @qsb or NULL on allocation failure
1243 QEMUSizedBuffer
*qsb_clone(const QEMUSizedBuffer
*qsb
)
1245 QEMUSizedBuffer
*out
= qsb_create(NULL
, qsb_get_length(qsb
));
1254 for (i
= 0; i
< qsb
->n_iov
; i
++) {
1255 res
= qsb_write_at(out
, qsb
->iov
[i
].iov_base
,
1256 pos
, qsb
->iov
[i
].iov_len
);
1267 typedef struct QEMUBuffer
{
1268 QEMUSizedBuffer
*qsb
;
1272 static int buf_get_buffer(void *opaque
, uint8_t *buf
, int64_t pos
, int size
)
1274 QEMUBuffer
*s
= opaque
;
1275 ssize_t len
= qsb_get_length(s
->qsb
) - pos
;
1284 return qsb_get_buffer(s
->qsb
, pos
, len
, buf
);
1287 static int buf_put_buffer(void *opaque
, const uint8_t *buf
,
1288 int64_t pos
, int size
)
1290 QEMUBuffer
*s
= opaque
;
1292 return qsb_write_at(s
->qsb
, buf
, pos
, size
);
1295 static int buf_close(void *opaque
)
1297 QEMUBuffer
*s
= opaque
;
1306 const QEMUSizedBuffer
*qemu_buf_get(QEMUFile
*f
)
1317 static const QEMUFileOps buf_read_ops
= {
1318 .get_buffer
= buf_get_buffer
,
1322 static const QEMUFileOps buf_write_ops
= {
1323 .put_buffer
= buf_put_buffer
,
1327 QEMUFile
*qemu_bufopen(const char *mode
, QEMUSizedBuffer
*input
)
1331 if (mode
== NULL
|| (mode
[0] != 'r' && mode
[0] != 'w') ||
1333 error_report("qemu_bufopen: Argument validity check failed");
1337 s
= g_malloc0(sizeof(QEMUBuffer
));
1338 if (mode
[0] == 'r') {
1342 if (s
->qsb
== NULL
) {
1343 s
->qsb
= qsb_create(NULL
, 0);
1347 error_report("qemu_bufopen: qsb_create failed");
1352 if (mode
[0] == 'r') {
1353 s
->file
= qemu_fopen_ops(s
, &buf_read_ops
);
1355 s
->file
= qemu_fopen_ops(s
, &buf_write_ops
);