OSDN Git Service

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