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