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"
27 #include "qemu/sockets.h"
28 #include "block/coroutine.h"
29 #include "migration/migration.h"
30 #include "migration/qemu-file.h"
31 #include "migration/qemu-file-internal.h"
35 * Stop a file from being read/written - not all backing files can do this
36 * typically only sockets can.
38 int qemu_file_shutdown(QEMUFile
*f
)
40 if (!f
->ops
->shut_down
) {
43 return f
->ops
->shut_down(f
->opaque
, true, true);
46 bool qemu_file_mode_is_not_valid(const char *mode
)
49 (mode
[0] != 'r' && mode
[0] != 'w') ||
50 mode
[1] != 'b' || mode
[2] != 0) {
51 fprintf(stderr
, "qemu_fopen: Argument validity check failed\n");
58 QEMUFile
*qemu_fopen_ops(void *opaque
, const QEMUFileOps
*ops
)
62 f
= g_malloc0(sizeof(QEMUFile
));
70 * Get last error for stream f
72 * Return negative error value if there has been an error on previous
73 * operations, return 0 if no error happened.
76 int qemu_file_get_error(QEMUFile
*f
)
81 void qemu_file_set_error(QEMUFile
*f
, int ret
)
83 if (f
->last_error
== 0) {
88 bool qemu_file_is_writable(QEMUFile
*f
)
90 return f
->ops
->writev_buffer
|| f
->ops
->put_buffer
;
94 * Flushes QEMUFile buffer
96 * If there is writev_buffer QEMUFileOps it uses it otherwise uses
99 void qemu_fflush(QEMUFile
*f
)
103 if (!qemu_file_is_writable(f
)) {
107 if (f
->ops
->writev_buffer
) {
109 ret
= f
->ops
->writev_buffer(f
->opaque
, f
->iov
, f
->iovcnt
, f
->pos
);
112 if (f
->buf_index
> 0) {
113 ret
= f
->ops
->put_buffer(f
->opaque
, f
->buf
, f
->pos
, f
->buf_index
);
122 qemu_file_set_error(f
, ret
);
126 void ram_control_before_iterate(QEMUFile
*f
, uint64_t flags
)
130 if (f
->ops
->before_ram_iterate
) {
131 ret
= f
->ops
->before_ram_iterate(f
, f
->opaque
, flags
);
133 qemu_file_set_error(f
, ret
);
138 void ram_control_after_iterate(QEMUFile
*f
, uint64_t flags
)
142 if (f
->ops
->after_ram_iterate
) {
143 ret
= f
->ops
->after_ram_iterate(f
, f
->opaque
, flags
);
145 qemu_file_set_error(f
, ret
);
150 void ram_control_load_hook(QEMUFile
*f
, uint64_t flags
)
154 if (f
->ops
->hook_ram_load
) {
155 ret
= f
->ops
->hook_ram_load(f
, f
->opaque
, flags
);
157 qemu_file_set_error(f
, ret
);
160 qemu_file_set_error(f
, ret
);
164 size_t ram_control_save_page(QEMUFile
*f
, ram_addr_t block_offset
,
165 ram_addr_t offset
, size_t size
,
166 uint64_t *bytes_sent
)
168 if (f
->ops
->save_page
) {
169 int ret
= f
->ops
->save_page(f
, f
->opaque
, block_offset
,
170 offset
, size
, bytes_sent
);
172 if (ret
!= RAM_SAVE_CONTROL_DELAYED
) {
173 if (bytes_sent
&& *bytes_sent
> 0) {
174 qemu_update_position(f
, *bytes_sent
);
175 } else if (ret
< 0) {
176 qemu_file_set_error(f
, ret
);
183 return RAM_SAVE_CONTROL_NOT_SUPP
;
187 * Attempt to fill the buffer from the underlying file
188 * Returns the number of bytes read, or negative value for an error.
190 * Note that it can return a partially full buffer even in a not error/not EOF
191 * case if the underlying file descriptor gives a short read, and that can
192 * happen even on a blocking fd.
194 static ssize_t
qemu_fill_buffer(QEMUFile
*f
)
199 assert(!qemu_file_is_writable(f
));
201 pending
= f
->buf_size
- f
->buf_index
;
203 memmove(f
->buf
, f
->buf
+ f
->buf_index
, pending
);
206 f
->buf_size
= pending
;
208 len
= f
->ops
->get_buffer(f
->opaque
, f
->buf
+ pending
, f
->pos
,
209 IO_BUF_SIZE
- pending
);
213 } else if (len
== 0) {
214 qemu_file_set_error(f
, -EIO
);
215 } else if (len
!= -EAGAIN
) {
216 qemu_file_set_error(f
, len
);
222 int qemu_get_fd(QEMUFile
*f
)
224 if (f
->ops
->get_fd
) {
225 return f
->ops
->get_fd(f
->opaque
);
230 void qemu_update_position(QEMUFile
*f
, size_t size
)
237 * Returns negative error value if any error happened on previous operations or
238 * while closing the file. Returns 0 or positive number on success.
240 * The meaning of return value on success depends on the specific backend
243 int qemu_fclose(QEMUFile
*f
)
247 ret
= qemu_file_get_error(f
);
250 int ret2
= f
->ops
->close(f
->opaque
);
255 /* If any error was spotted before closing, we should report it
256 * instead of the close() return value.
262 trace_qemu_file_fclose();
266 static void add_to_iovec(QEMUFile
*f
, const uint8_t *buf
, int size
)
268 /* check for adjacent buffer and coalesce them */
269 if (f
->iovcnt
> 0 && buf
== f
->iov
[f
->iovcnt
- 1].iov_base
+
270 f
->iov
[f
->iovcnt
- 1].iov_len
) {
271 f
->iov
[f
->iovcnt
- 1].iov_len
+= size
;
273 f
->iov
[f
->iovcnt
].iov_base
= (uint8_t *)buf
;
274 f
->iov
[f
->iovcnt
++].iov_len
= size
;
277 if (f
->iovcnt
>= MAX_IOV_SIZE
) {
282 void qemu_put_buffer_async(QEMUFile
*f
, const uint8_t *buf
, int size
)
284 if (!f
->ops
->writev_buffer
) {
285 qemu_put_buffer(f
, buf
, size
);
293 f
->bytes_xfer
+= size
;
294 add_to_iovec(f
, buf
, size
);
297 void qemu_put_buffer(QEMUFile
*f
, const uint8_t *buf
, int size
)
306 l
= IO_BUF_SIZE
- f
->buf_index
;
310 memcpy(f
->buf
+ f
->buf_index
, buf
, l
);
312 if (f
->ops
->writev_buffer
) {
313 add_to_iovec(f
, f
->buf
+ f
->buf_index
, l
);
316 if (f
->buf_index
== IO_BUF_SIZE
) {
319 if (qemu_file_get_error(f
)) {
327 void qemu_put_byte(QEMUFile
*f
, int v
)
333 f
->buf
[f
->buf_index
] = v
;
335 if (f
->ops
->writev_buffer
) {
336 add_to_iovec(f
, f
->buf
+ f
->buf_index
, 1);
339 if (f
->buf_index
== IO_BUF_SIZE
) {
344 void qemu_file_skip(QEMUFile
*f
, int size
)
346 if (f
->buf_index
+ size
<= f
->buf_size
) {
347 f
->buf_index
+= size
;
352 * Read 'size' bytes from file (at 'offset') into buf without moving the
355 * It will return size bytes unless there was an error, in which case it will
356 * return as many as it managed to read (assuming blocking fd's which
357 * all current QEMUFile are)
359 int qemu_peek_buffer(QEMUFile
*f
, uint8_t *buf
, int size
, size_t offset
)
364 assert(!qemu_file_is_writable(f
));
365 assert(offset
< IO_BUF_SIZE
);
366 assert(size
<= IO_BUF_SIZE
- offset
);
368 /* The 1st byte to read from */
369 index
= f
->buf_index
+ offset
;
370 /* The number of available bytes starting at index */
371 pending
= f
->buf_size
- index
;
374 * qemu_fill_buffer might return just a few bytes, even when there isn't
375 * an error, so loop collecting them until we get enough.
377 while (pending
< size
) {
378 int received
= qemu_fill_buffer(f
);
384 index
= f
->buf_index
+ offset
;
385 pending
= f
->buf_size
- index
;
391 if (size
> pending
) {
395 memcpy(buf
, f
->buf
+ index
, size
);
400 * Read 'size' bytes of data from the file into buf.
401 * 'size' can be larger than the internal buffer.
403 * It will return size bytes unless there was an error, in which case it will
404 * return as many as it managed to read (assuming blocking fd's which
405 * all current QEMUFile are)
407 int qemu_get_buffer(QEMUFile
*f
, uint8_t *buf
, int size
)
412 while (pending
> 0) {
415 res
= qemu_peek_buffer(f
, buf
, MIN(pending
, IO_BUF_SIZE
), 0);
419 qemu_file_skip(f
, res
);
428 * Peeks a single byte from the buffer; this isn't guaranteed to work if
429 * offset leaves a gap after the previous read/peeked data.
431 int qemu_peek_byte(QEMUFile
*f
, int offset
)
433 int index
= f
->buf_index
+ offset
;
435 assert(!qemu_file_is_writable(f
));
436 assert(offset
< IO_BUF_SIZE
);
438 if (index
>= f
->buf_size
) {
440 index
= f
->buf_index
+ offset
;
441 if (index
>= f
->buf_size
) {
445 return f
->buf
[index
];
448 int qemu_get_byte(QEMUFile
*f
)
452 result
= qemu_peek_byte(f
, 0);
453 qemu_file_skip(f
, 1);
457 int64_t qemu_ftell_fast(QEMUFile
*f
)
459 int64_t ret
= f
->pos
;
462 if (f
->ops
->writev_buffer
) {
463 for (i
= 0; i
< f
->iovcnt
; i
++) {
464 ret
+= f
->iov
[i
].iov_len
;
473 int64_t qemu_ftell(QEMUFile
*f
)
479 int qemu_file_rate_limit(QEMUFile
*f
)
481 if (qemu_file_get_error(f
)) {
484 if (f
->xfer_limit
> 0 && f
->bytes_xfer
> f
->xfer_limit
) {
490 int64_t qemu_file_get_rate_limit(QEMUFile
*f
)
492 return f
->xfer_limit
;
495 void qemu_file_set_rate_limit(QEMUFile
*f
, int64_t limit
)
497 f
->xfer_limit
= limit
;
500 void qemu_file_reset_rate_limit(QEMUFile
*f
)
505 void qemu_put_be16(QEMUFile
*f
, unsigned int v
)
507 qemu_put_byte(f
, v
>> 8);
511 void qemu_put_be32(QEMUFile
*f
, unsigned int v
)
513 qemu_put_byte(f
, v
>> 24);
514 qemu_put_byte(f
, v
>> 16);
515 qemu_put_byte(f
, v
>> 8);
519 void qemu_put_be64(QEMUFile
*f
, uint64_t v
)
521 qemu_put_be32(f
, v
>> 32);
525 unsigned int qemu_get_be16(QEMUFile
*f
)
528 v
= qemu_get_byte(f
) << 8;
529 v
|= qemu_get_byte(f
);
533 unsigned int qemu_get_be32(QEMUFile
*f
)
536 v
= (unsigned int)qemu_get_byte(f
) << 24;
537 v
|= qemu_get_byte(f
) << 16;
538 v
|= qemu_get_byte(f
) << 8;
539 v
|= qemu_get_byte(f
);
543 uint64_t qemu_get_be64(QEMUFile
*f
)
546 v
= (uint64_t)qemu_get_be32(f
) << 32;
547 v
|= qemu_get_be32(f
);
551 /* compress size bytes of data start at p with specific compression
552 * level and store the compressed data to the buffer of f.
555 ssize_t
qemu_put_compression_data(QEMUFile
*f
, const uint8_t *p
, size_t size
,
558 ssize_t blen
= IO_BUF_SIZE
- f
->buf_index
- sizeof(int32_t);
560 if (blen
< compressBound(size
)) {
563 if (compress2(f
->buf
+ f
->buf_index
+ sizeof(int32_t), (uLongf
*)&blen
,
564 (Bytef
*)p
, size
, level
) != Z_OK
) {
565 error_report("Compress Failed!");
568 qemu_put_be32(f
, blen
);
569 f
->buf_index
+= blen
;
570 return blen
+ sizeof(int32_t);
573 /* Put the data in the buffer of f_src to the buffer of f_des, and
574 * then reset the buf_index of f_src to 0.
577 int qemu_put_qemu_file(QEMUFile
*f_des
, QEMUFile
*f_src
)
581 if (f_src
->buf_index
> 0) {
582 len
= f_src
->buf_index
;
583 qemu_put_buffer(f_des
, f_src
->buf
, f_src
->buf_index
);
584 f_src
->buf_index
= 0;