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