]> git.proxmox.com Git - mirror_qemu.git/blame - migration/qemu-file.c
include/hw/core: Move do_interrupt in TCGCPUOps
[mirror_qemu.git] / migration / qemu-file.c
CommitLineData
9a4ac51f
EH
1/*
2 * QEMU System Emulator
3 *
4 * Copyright (c) 2003-2008 Fabrice Bellard
5 *
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:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
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
22 * THE SOFTWARE.
23 */
1393a485 24#include "qemu/osdep.h"
44f0eadc 25#include <zlib.h>
b85ea5fa 26#include "qemu/madvise.h"
d49b6836 27#include "qemu/error-report.h"
093c455a 28#include "qemu/iov.h"
6666c96a 29#include "migration.h"
8e4b2a70 30#include "migration-stats.h"
08a0aee1 31#include "qemu-file.h"
9013dca5 32#include "trace.h"
9d3ebbe2 33#include "options.h"
3d661c8a 34#include "qapi/error.h"
48408174 35#include "rdma.h"
093c455a 36
a24939f2 37#define IO_BUF_SIZE 32768
f9919116 38#define MAX_IOV_SIZE MIN_CONST(IOV_MAX, 64)
a24939f2
DB
39
40struct QEMUFile {
2893a288 41 QIOChannel *ioc;
c0c6e1e2 42 bool is_writable;
a24939f2 43
a24939f2
DB
44 int buf_index;
45 int buf_size; /* 0 when writing */
46 uint8_t buf[IO_BUF_SIZE];
47
53f09a10 48 DECLARE_BITMAP(may_free, MAX_IOV_SIZE);
a24939f2
DB
49 struct iovec iov[MAX_IOV_SIZE];
50 unsigned int iovcnt;
51
52 int last_error;
3d661c8a 53 Error *last_error_obj;
a24939f2
DB
54};
55
e1a8c9b6
DDAG
56/*
57 * Stop a file from being read/written - not all backing files can do this
58 * typically only sockets can.
d3c581b7
DB
59 *
60 * TODO: convert to propagate Error objects instead of squashing
61 * to a fixed errno value
e1a8c9b6
DDAG
62 */
63int qemu_file_shutdown(QEMUFile *f)
64{
f5816b5c
PX
65 /*
66 * We must set qemufile error before the real shutdown(), otherwise
67 * there can be a race window where we thought IO all went though
68 * (because last_error==NULL) but actually IO has already stopped.
69 *
70 * If without correct ordering, the race can happen like this:
71 *
72 * page receiver other thread
73 * ------------- ------------
74 * qemu_get_buffer()
75 * do shutdown()
76 * returns 0 (buffer all zero)
77 * (we didn't check this retcode)
78 * try to detect IO error
79 * last_error==NULL, IO okay
80 * install ALL-ZERO page
81 * set last_error
82 * --> guest crash!
83 */
84 if (!f->last_error) {
85 qemu_file_set_error(f, -EIO);
86 }
87
d3c581b7
DB
88 if (!qio_channel_has_feature(f->ioc,
89 QIO_CHANNEL_FEATURE_SHUTDOWN)) {
e1a8c9b6
DDAG
90 return -ENOSYS;
91 }
d3c581b7
DB
92
93 if (qio_channel_shutdown(f->ioc, QIO_CHANNEL_SHUTDOWN_BOTH, NULL) < 0) {
8c5ee0bf 94 return -EIO;
d3c581b7 95 }
a555b809 96
8c5ee0bf 97 return 0;
e1a8c9b6
DDAG
98}
99
77ef2dc1 100static QEMUFile *qemu_file_new_impl(QIOChannel *ioc, bool is_writable)
093c455a
EH
101{
102 QEMUFile *f;
103
97f3ad35 104 f = g_new0(QEMUFile, 1);
093c455a 105
77ef2dc1 106 object_ref(ioc);
2893a288 107 f->ioc = ioc;
c0c6e1e2 108 f->is_writable = is_writable;
2893a288 109
093c455a
EH
110 return f;
111}
112
02bdbe17
DB
113/*
114 * Result: QEMUFile* for a 'return path' for comms in the opposite direction
115 * NULL if not available
116 */
117QEMUFile *qemu_file_get_return_path(QEMUFile *f)
118{
77ef2dc1 119 return qemu_file_new_impl(f->ioc, !f->is_writable);
02bdbe17
DB
120}
121
77ef2dc1 122QEMUFile *qemu_file_new_output(QIOChannel *ioc)
c0c6e1e2 123{
77ef2dc1 124 return qemu_file_new_impl(ioc, true);
c0c6e1e2
DB
125}
126
77ef2dc1 127QEMUFile *qemu_file_new_input(QIOChannel *ioc)
c0c6e1e2 128{
77ef2dc1 129 return qemu_file_new_impl(ioc, false);
c0c6e1e2
DB
130}
131
093c455a 132/*
3d661c8a 133 * Get last error for stream f with optional Error*
093c455a
EH
134 *
135 * Return negative error value if there has been an error on previous
136 * operations, return 0 if no error happened.
137 *
f4b897f4 138 * If errp is specified, a verbose error message will be copied over.
093c455a 139 */
7aa6070d 140int qemu_file_get_error_obj(QEMUFile *f, Error **errp)
093c455a 141{
f4b897f4
PX
142 if (!f->last_error) {
143 return 0;
144 }
145
146 /* There is an error */
3d661c8a 147 if (errp) {
f4b897f4
PX
148 if (f->last_error_obj) {
149 *errp = error_copy(f->last_error_obj);
150 } else {
151 error_setg_errno(errp, -f->last_error, "Channel error");
152 }
3d661c8a 153 }
f4b897f4 154
093c455a
EH
155 return f->last_error;
156}
157
60bb3c58
PX
158/*
159 * Get last error for either stream f1 or f2 with optional Error*.
160 * The error returned (non-zero) can be either from f1 or f2.
161 *
162 * If any of the qemufile* is NULL, then skip the check on that file.
163 *
164 * When there is no error on both qemufile, zero is returned.
165 */
166int qemu_file_get_error_obj_any(QEMUFile *f1, QEMUFile *f2, Error **errp)
167{
168 int ret = 0;
169
170 if (f1) {
171 ret = qemu_file_get_error_obj(f1, errp);
172 /* If there's already error detected, return */
173 if (ret) {
174 return ret;
175 }
176 }
177
178 if (f2) {
179 ret = qemu_file_get_error_obj(f2, errp);
180 }
181
182 return ret;
183}
184
3d661c8a
YK
185/*
186 * Set the last error for stream f with optional Error*
187 */
188void qemu_file_set_error_obj(QEMUFile *f, int ret, Error *err)
093c455a 189{
3d661c8a 190 if (f->last_error == 0 && ret) {
093c455a 191 f->last_error = ret;
3d661c8a
YK
192 error_propagate(&f->last_error_obj, err);
193 } else if (err) {
194 error_report_err(err);
093c455a
EH
195 }
196}
197
3d661c8a
YK
198/*
199 * Get last error for stream f
200 *
201 * Return negative error value if there has been an error on previous
202 * operations, return 0 if no error happened.
203 *
204 */
205int qemu_file_get_error(QEMUFile *f)
206{
fc55cf31 207 return f->last_error;
3d661c8a
YK
208}
209
210/*
211 * Set the last error for stream f
212 */
213void qemu_file_set_error(QEMUFile *f, int ret)
214{
215 qemu_file_set_error_obj(f, ret, NULL);
216}
217
9ccf83f4 218static bool qemu_file_is_writable(QEMUFile *f)
093c455a 219{
c0c6e1e2 220 return f->is_writable;
093c455a
EH
221}
222
53f09a10
PB
223static void qemu_iovec_release_ram(QEMUFile *f)
224{
225 struct iovec iov;
226 unsigned long idx;
227
228 /* Find and release all the contiguous memory ranges marked as may_free. */
229 idx = find_next_bit(f->may_free, f->iovcnt, 0);
230 if (idx >= f->iovcnt) {
231 return;
232 }
233 iov = f->iov[idx];
234
235 /* The madvise() in the loop is called for iov within a continuous range and
236 * then reinitialize the iov. And in the end, madvise() is called for the
237 * last iov.
238 */
239 while ((idx = find_next_bit(f->may_free, f->iovcnt, idx + 1)) < f->iovcnt) {
240 /* check for adjacent buffer and coalesce them */
241 if (iov.iov_base + iov.iov_len == f->iov[idx].iov_base) {
242 iov.iov_len += f->iov[idx].iov_len;
243 continue;
244 }
245 if (qemu_madvise(iov.iov_base, iov.iov_len, QEMU_MADV_DONTNEED) < 0) {
246 error_report("migrate: madvise DONTNEED failed %p %zd: %s",
247 iov.iov_base, iov.iov_len, strerror(errno));
248 }
249 iov = f->iov[idx];
250 }
251 if (qemu_madvise(iov.iov_base, iov.iov_len, QEMU_MADV_DONTNEED) < 0) {
252 error_report("migrate: madvise DONTNEED failed %p %zd: %s",
253 iov.iov_base, iov.iov_len, strerror(errno));
254 }
255 memset(f->may_free, 0, sizeof(f->may_free));
256}
257
77ef2dc1 258
093c455a
EH
259/**
260 * Flushes QEMUFile buffer
261 *
3b348706
DDAG
262 * This will flush all pending data. If data was only partially flushed, it
263 * will set an error state.
093c455a 264 */
be07a0ed 265int qemu_fflush(QEMUFile *f)
093c455a 266{
093c455a 267 if (!qemu_file_is_writable(f)) {
be07a0ed 268 return f->last_error;
093c455a
EH
269 }
270
be07a0ed
JQ
271 if (f->last_error) {
272 return f->last_error;
a555b809 273 }
11808bb0 274 if (f->iovcnt > 0) {
ec2135ee
DB
275 Error *local_error = NULL;
276 if (qio_channel_writev_all(f->ioc,
277 f->iov, f->iovcnt,
278 &local_error) < 0) {
279 qemu_file_set_error_obj(f, -EIO, local_error);
280 } else {
de37f8b9 281 uint64_t size = iov_size(f->iov, f->iovcnt);
2d897237 282 stat64_add(&mig_stats.qemu_file_transferred, size);
ec2135ee 283 }
53f09a10
PB
284
285 qemu_iovec_release_ram(f);
093c455a 286 }
baf51e77 287
093c455a
EH
288 f->buf_index = 0;
289 f->iovcnt = 0;
be07a0ed 290 return f->last_error;
093c455a
EH
291}
292
548f52ea
DDAG
293/*
294 * Attempt to fill the buffer from the underlying file
295 * Returns the number of bytes read, or negative value for an error.
296 *
297 * Note that it can return a partially full buffer even in a not error/not EOF
298 * case if the underlying file descriptor gives a short read, and that can
299 * happen even on a blocking fd.
300 */
394b9407 301static ssize_t coroutine_mixed_fn qemu_fill_buffer(QEMUFile *f)
093c455a
EH
302{
303 int len;
304 int pending;
3d661c8a 305 Error *local_error = NULL;
093c455a
EH
306
307 assert(!qemu_file_is_writable(f));
308
309 pending = f->buf_size - f->buf_index;
310 if (pending > 0) {
311 memmove(f->buf, f->buf + f->buf_index, pending);
312 }
313 f->buf_index = 0;
314 f->buf_size = pending;
315
ac7d25b8 316 if (qemu_file_get_error(f)) {
a555b809
JQ
317 return 0;
318 }
319
f759d705
DB
320 do {
321 len = qio_channel_read(f->ioc,
322 (char *)f->buf + pending,
323 IO_BUF_SIZE - pending,
324 &local_error);
325 if (len == QIO_CHANNEL_ERR_BLOCK) {
326 if (qemu_in_coroutine()) {
327 qio_channel_yield(f->ioc, G_IO_IN);
328 } else {
329 qio_channel_wait(f->ioc, G_IO_IN);
330 }
331 } else if (len < 0) {
332 len = -EIO;
333 }
334 } while (len == QIO_CHANNEL_ERR_BLOCK);
335
093c455a
EH
336 if (len > 0) {
337 f->buf_size += len;
093c455a 338 } else if (len == 0) {
3d661c8a 339 qemu_file_set_error_obj(f, -EIO, local_error);
3d661c8a 340 } else {
5f87072e 341 qemu_file_set_error_obj(f, len, local_error);
093c455a 342 }
548f52ea
DDAG
343
344 return len;
093c455a
EH
345}
346
093c455a
EH
347/** Closes the file
348 *
349 * Returns negative error value if any error happened on previous operations or
350 * while closing the file. Returns 0 or positive number on success.
351 *
352 * The meaning of return value on success depends on the specific backend
353 * being used.
354 */
355int qemu_fclose(QEMUFile *f)
356{
be07a0ed
JQ
357 int ret = qemu_fflush(f);
358 int ret2 = qio_channel_close(f->ioc, NULL);
0ae1f7f0
DB
359 if (ret >= 0) {
360 ret = ret2;
093c455a 361 }
0ae1f7f0 362 g_clear_pointer(&f->ioc, object_unref);
3d661c8a 363 error_free(f->last_error_obj);
093c455a 364 g_free(f);
9013dca5 365 trace_qemu_file_fclose();
093c455a
EH
366 return ret;
367}
368
1bf57fb3
WY
369/*
370 * Add buf to iovec. Do flush if iovec is full.
371 *
372 * Return values:
373 * 1 iovec is full and flushed
374 * 0 iovec is not flushed
375 *
376 */
377static int add_to_iovec(QEMUFile *f, const uint8_t *buf, size_t size,
378 bool may_free)
093c455a
EH
379{
380 /* check for adjacent buffer and coalesce them */
381 if (f->iovcnt > 0 && buf == f->iov[f->iovcnt - 1].iov_base +
53f09a10
PB
382 f->iov[f->iovcnt - 1].iov_len &&
383 may_free == test_bit(f->iovcnt - 1, f->may_free))
384 {
093c455a
EH
385 f->iov[f->iovcnt - 1].iov_len += size;
386 } else {
c00d434a
FL
387 if (f->iovcnt >= MAX_IOV_SIZE) {
388 /* Should only happen if a previous fflush failed */
ac7d25b8 389 assert(qemu_file_get_error(f) || !qemu_file_is_writable(f));
c00d434a
FL
390 return 1;
391 }
53f09a10
PB
392 if (may_free) {
393 set_bit(f->iovcnt, f->may_free);
394 }
093c455a
EH
395 f->iov[f->iovcnt].iov_base = (uint8_t *)buf;
396 f->iov[f->iovcnt++].iov_len = size;
397 }
398
399 if (f->iovcnt >= MAX_IOV_SIZE) {
400 qemu_fflush(f);
1bf57fb3
WY
401 return 1;
402 }
403
404 return 0;
405}
406
407static void add_buf_to_iovec(QEMUFile *f, size_t len)
408{
409 if (!add_to_iovec(f, f->buf + f->buf_index, len, false)) {
410 f->buf_index += len;
411 if (f->buf_index == IO_BUF_SIZE) {
412 qemu_fflush(f);
413 }
093c455a
EH
414 }
415}
416
53f09a10
PB
417void qemu_put_buffer_async(QEMUFile *f, const uint8_t *buf, size_t size,
418 bool may_free)
093c455a 419{
093c455a
EH
420 if (f->last_error) {
421 return;
422 }
423
53f09a10 424 add_to_iovec(f, buf, size, may_free);
093c455a
EH
425}
426
56f3835f 427void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, size_t size)
093c455a 428{
56f3835f 429 size_t l;
093c455a
EH
430
431 if (f->last_error) {
432 return;
433 }
434
435 while (size > 0) {
436 l = IO_BUF_SIZE - f->buf_index;
437 if (l > size) {
438 l = size;
439 }
440 memcpy(f->buf + f->buf_index, buf, l);
1bf57fb3 441 add_buf_to_iovec(f, l);
093c455a
EH
442 if (qemu_file_get_error(f)) {
443 break;
444 }
445 buf += l;
446 size -= l;
447 }
448}
449
450void qemu_put_byte(QEMUFile *f, int v)
451{
452 if (f->last_error) {
453 return;
454 }
455
456 f->buf[f->buf_index] = v;
1bf57fb3 457 add_buf_to_iovec(f, 1);
093c455a
EH
458}
459
460void qemu_file_skip(QEMUFile *f, int size)
461{
462 if (f->buf_index + size <= f->buf_size) {
463 f->buf_index += size;
464 }
465}
466
548f52ea 467/*
7c1e52ba
DDAG
468 * Read 'size' bytes from file (at 'offset') without moving the
469 * pointer and set 'buf' to point to that data.
548f52ea
DDAG
470 *
471 * It will return size bytes unless there was an error, in which case it will
472 * return as many as it managed to read (assuming blocking fd's which
473 * all current QEMUFile are)
474 */
394b9407 475size_t coroutine_mixed_fn qemu_peek_buffer(QEMUFile *f, uint8_t **buf, size_t size, size_t offset)
093c455a 476{
56f3835f
DDAG
477 ssize_t pending;
478 size_t index;
093c455a
EH
479
480 assert(!qemu_file_is_writable(f));
548f52ea
DDAG
481 assert(offset < IO_BUF_SIZE);
482 assert(size <= IO_BUF_SIZE - offset);
093c455a 483
548f52ea 484 /* The 1st byte to read from */
093c455a 485 index = f->buf_index + offset;
548f52ea 486 /* The number of available bytes starting at index */
093c455a 487 pending = f->buf_size - index;
548f52ea
DDAG
488
489 /*
490 * qemu_fill_buffer might return just a few bytes, even when there isn't
491 * an error, so loop collecting them until we get enough.
492 */
493 while (pending < size) {
494 int received = qemu_fill_buffer(f);
495
496 if (received <= 0) {
497 break;
498 }
499
093c455a
EH
500 index = f->buf_index + offset;
501 pending = f->buf_size - index;
502 }
503
504 if (pending <= 0) {
505 return 0;
506 }
507 if (size > pending) {
508 size = pending;
509 }
510
7c1e52ba 511 *buf = f->buf + index;
093c455a
EH
512 return size;
513}
514
548f52ea
DDAG
515/*
516 * Read 'size' bytes of data from the file into buf.
517 * 'size' can be larger than the internal buffer.
518 *
519 * It will return size bytes unless there was an error, in which case it will
520 * return as many as it managed to read (assuming blocking fd's which
521 * all current QEMUFile are)
522 */
394b9407 523size_t coroutine_mixed_fn qemu_get_buffer(QEMUFile *f, uint8_t *buf, size_t size)
093c455a 524{
56f3835f
DDAG
525 size_t pending = size;
526 size_t done = 0;
093c455a
EH
527
528 while (pending > 0) {
56f3835f 529 size_t res;
7c1e52ba 530 uint8_t *src;
093c455a 531
7c1e52ba 532 res = qemu_peek_buffer(f, &src, MIN(pending, IO_BUF_SIZE), 0);
093c455a
EH
533 if (res == 0) {
534 return done;
535 }
7c1e52ba 536 memcpy(buf, src, res);
093c455a
EH
537 qemu_file_skip(f, res);
538 buf += res;
539 pending -= res;
540 done += res;
541 }
542 return done;
543}
544
9504fb51
DDAG
545/*
546 * Read 'size' bytes of data from the file.
547 * 'size' can be larger than the internal buffer.
548 *
549 * The data:
550 * may be held on an internal buffer (in which case *buf is updated
551 * to point to it) that is valid until the next qemu_file operation.
552 * OR
553 * will be copied to the *buf that was passed in.
554 *
555 * The code tries to avoid the copy if possible.
556 *
557 * It will return size bytes unless there was an error, in which case it will
558 * return as many as it managed to read (assuming blocking fd's which
559 * all current QEMUFile are)
560 *
561 * Note: Since **buf may get changed, the caller should take care to
562 * keep a pointer to the original buffer if it needs to deallocate it.
563 */
394b9407 564size_t coroutine_mixed_fn qemu_get_buffer_in_place(QEMUFile *f, uint8_t **buf, size_t size)
9504fb51
DDAG
565{
566 if (size < IO_BUF_SIZE) {
567 size_t res;
1dfafcbd 568 uint8_t *src = NULL;
9504fb51
DDAG
569
570 res = qemu_peek_buffer(f, &src, size, 0);
571
572 if (res == size) {
573 qemu_file_skip(f, res);
574 *buf = src;
575 return res;
576 }
577 }
578
579 return qemu_get_buffer(f, *buf, size);
580}
581
548f52ea
DDAG
582/*
583 * Peeks a single byte from the buffer; this isn't guaranteed to work if
584 * offset leaves a gap after the previous read/peeked data.
585 */
394b9407 586int coroutine_mixed_fn qemu_peek_byte(QEMUFile *f, int offset)
093c455a
EH
587{
588 int index = f->buf_index + offset;
589
590 assert(!qemu_file_is_writable(f));
548f52ea 591 assert(offset < IO_BUF_SIZE);
093c455a
EH
592
593 if (index >= f->buf_size) {
594 qemu_fill_buffer(f);
595 index = f->buf_index + offset;
596 if (index >= f->buf_size) {
597 return 0;
598 }
599 }
600 return f->buf[index];
601}
602
394b9407 603int coroutine_mixed_fn qemu_get_byte(QEMUFile *f)
093c455a
EH
604{
605 int result;
606
607 result = qemu_peek_byte(f, 0);
608 qemu_file_skip(f, 1);
609 return result;
610}
611
e9c0eed7 612uint64_t qemu_file_transferred(QEMUFile *f)
97221400 613{
2d897237 614 uint64_t ret = stat64_get(&mig_stats.qemu_file_transferred);
97221400
AG
615 int i;
616
cc8bf57d
JQ
617 g_assert(qemu_file_is_writable(f));
618
11808bb0
DB
619 for (i = 0; i < f->iovcnt; i++) {
620 ret += f->iov[i].iov_len;
97221400
AG
621 }
622
623 return ret;
624}
625
093c455a
EH
626void qemu_put_be16(QEMUFile *f, unsigned int v)
627{
628 qemu_put_byte(f, v >> 8);
629 qemu_put_byte(f, v);
630}
631
632void qemu_put_be32(QEMUFile *f, unsigned int v)
633{
634 qemu_put_byte(f, v >> 24);
635 qemu_put_byte(f, v >> 16);
636 qemu_put_byte(f, v >> 8);
637 qemu_put_byte(f, v);
638}
639
640void qemu_put_be64(QEMUFile *f, uint64_t v)
641{
642 qemu_put_be32(f, v >> 32);
643 qemu_put_be32(f, v);
644}
645
646unsigned int qemu_get_be16(QEMUFile *f)
647{
648 unsigned int v;
649 v = qemu_get_byte(f) << 8;
650 v |= qemu_get_byte(f);
651 return v;
652}
653
654unsigned int qemu_get_be32(QEMUFile *f)
655{
656 unsigned int v;
90d6a673 657 v = (unsigned int)qemu_get_byte(f) << 24;
093c455a
EH
658 v |= qemu_get_byte(f) << 16;
659 v |= qemu_get_byte(f) << 8;
660 v |= qemu_get_byte(f);
661 return v;
662}
663
664uint64_t qemu_get_be64(QEMUFile *f)
665{
666 uint64_t v;
667 v = (uint64_t)qemu_get_be32(f) << 32;
668 v |= qemu_get_be32(f);
669 return v;
670}
44f0eadc 671
dcaf446e
XG
672/* return the size after compression, or negative value on error */
673static int qemu_compress_data(z_stream *stream, uint8_t *dest, size_t dest_len,
674 const uint8_t *source, size_t source_len)
675{
676 int err;
677
678 err = deflateReset(stream);
679 if (err != Z_OK) {
680 return -1;
681 }
682
683 stream->avail_in = source_len;
684 stream->next_in = (uint8_t *)source;
685 stream->avail_out = dest_len;
686 stream->next_out = dest;
687
688 err = deflate(stream, Z_FINISH);
689 if (err != Z_STREAM_END) {
690 return -1;
691 }
692
693 return stream->next_out - dest;
694}
695
696/* Compress size bytes of data start at p and store the compressed
697 * data to the buffer of f.
b3be2896 698 *
42d24611
WY
699 * Since the file is dummy file with empty_ops, return -1 if f has no space to
700 * save the compressed data.
44f0eadc 701 */
dcaf446e
XG
702ssize_t qemu_put_compression_data(QEMUFile *f, z_stream *stream,
703 const uint8_t *p, size_t size)
44f0eadc
LL
704{
705 ssize_t blen = IO_BUF_SIZE - f->buf_index - sizeof(int32_t);
706
707 if (blen < compressBound(size)) {
42d24611 708 return -1;
44f0eadc 709 }
dcaf446e
XG
710
711 blen = qemu_compress_data(stream, f->buf + f->buf_index + sizeof(int32_t),
712 blen, p, size);
713 if (blen < 0) {
34ab9e97 714 return -1;
44f0eadc 715 }
34ab9e97 716
44f0eadc 717 qemu_put_be32(f, blen);
1bf57fb3 718 add_buf_to_iovec(f, blen);
44f0eadc
LL
719 return blen + sizeof(int32_t);
720}
721
722/* Put the data in the buffer of f_src to the buffer of f_des, and
723 * then reset the buf_index of f_src to 0.
724 */
725
726int qemu_put_qemu_file(QEMUFile *f_des, QEMUFile *f_src)
727{
728 int len = 0;
729
730 if (f_src->buf_index > 0) {
731 len = f_src->buf_index;
732 qemu_put_buffer(f_des, f_src->buf, f_src->buf_index);
733 f_src->buf_index = 0;
787d134f 734 f_src->iovcnt = 0;
44f0eadc
LL
735 }
736 return len;
737}
b3af1bc9 738
4024cc85
LS
739/*
740 * Check if the writable buffer is empty
741 */
742
743bool qemu_file_buffer_empty(QEMUFile *file)
744{
745 assert(qemu_file_is_writable(file));
746
747 return !file->iovcnt;
748}
749
b3af1bc9
DDAG
750/*
751 * Get a string whose length is determined by a single preceding byte
752 * A preallocated 256 byte buffer must be passed in.
753 * Returns: len on success and a 0 terminated string in the buffer
754 * else 0
755 * (Note a 0 length string will return 0 either way)
756 */
394b9407 757size_t coroutine_fn qemu_get_counted_string(QEMUFile *f, char buf[256])
b3af1bc9
DDAG
758{
759 size_t len = qemu_get_byte(f);
760 size_t res = qemu_get_buffer(f, (uint8_t *)buf, len);
761
762 buf[res] = 0;
763
764 return res == len ? res : 0;
765}
a800cd5c 766
f0d64cb7
VSO
767/*
768 * Put a string with one preceding byte containing its length. The length of
769 * the string should be less than 256.
770 */
771void qemu_put_counted_string(QEMUFile *f, const char *str)
772{
773 size_t len = strlen(str);
774
775 assert(len < 256);
776 qemu_put_byte(f, len);
777 qemu_put_buffer(f, (const uint8_t *)str, len);
778}
779
a800cd5c
DDAG
780/*
781 * Set the blocking state of the QEMUFile.
782 * Note: On some transports the OS only keeps a single blocking state for
783 * both directions, and thus changing the blocking on the main
784 * QEMUFile can also affect the return path.
785 */
786void qemu_file_set_blocking(QEMUFile *f, bool block)
787{
80ad9706 788 qio_channel_set_blocking(f->ioc, block, NULL);
a800cd5c 789}
c6ad5be7
PX
790
791/*
2893a288
DB
792 * qemu_file_get_ioc:
793 *
794 * Get the ioc object for the file, without incrementing
795 * the reference count.
796 *
797 * Returns: the ioc object
c6ad5be7
PX
798 */
799QIOChannel *qemu_file_get_ioc(QEMUFile *file)
800{
2893a288 801 return file->ioc;
c6ad5be7 802}
c7a7db4b
AH
803
804/*
805 * Read size bytes from QEMUFile f and write them to fd.
806 */
807int qemu_file_get_to_fd(QEMUFile *f, int fd, size_t size)
808{
809 while (size) {
810 size_t pending = f->buf_size - f->buf_index;
811 ssize_t rc;
812
813 if (!pending) {
814 rc = qemu_fill_buffer(f);
815 if (rc < 0) {
816 return rc;
817 }
818 if (rc == 0) {
819 return -EIO;
820 }
821 continue;
822 }
823
824 rc = write(fd, f->buf + f->buf_index, MIN(pending, size));
825 if (rc < 0) {
826 return -errno;
827 }
828 if (rc == 0) {
829 return -EIO;
830 }
831 f->buf_index += rc;
832 size -= rc;
833 }
834
835 return 0;
836}