xref: /qemu/migration/qemu-file.c (revision f87e4d6d)
160fe637bSDr. David Alan Gilbert /*
260fe637bSDr. David Alan Gilbert  * QEMU System Emulator
360fe637bSDr. David Alan Gilbert  *
460fe637bSDr. David Alan Gilbert  * Copyright (c) 2003-2008 Fabrice Bellard
560fe637bSDr. David Alan Gilbert  *
660fe637bSDr. David Alan Gilbert  * Permission is hereby granted, free of charge, to any person obtaining a copy
760fe637bSDr. David Alan Gilbert  * of this software and associated documentation files (the "Software"), to deal
860fe637bSDr. David Alan Gilbert  * in the Software without restriction, including without limitation the rights
960fe637bSDr. David Alan Gilbert  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
1060fe637bSDr. David Alan Gilbert  * copies of the Software, and to permit persons to whom the Software is
1160fe637bSDr. David Alan Gilbert  * furnished to do so, subject to the following conditions:
1260fe637bSDr. David Alan Gilbert  *
1360fe637bSDr. David Alan Gilbert  * The above copyright notice and this permission notice shall be included in
1460fe637bSDr. David Alan Gilbert  * all copies or substantial portions of the Software.
1560fe637bSDr. David Alan Gilbert  *
1660fe637bSDr. David Alan Gilbert  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1760fe637bSDr. David Alan Gilbert  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1860fe637bSDr. David Alan Gilbert  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
1960fe637bSDr. David Alan Gilbert  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
2060fe637bSDr. David Alan Gilbert  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
2160fe637bSDr. David Alan Gilbert  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2260fe637bSDr. David Alan Gilbert  * THE SOFTWARE.
2360fe637bSDr. David Alan Gilbert  */
241393a485SPeter Maydell #include "qemu/osdep.h"
2544f0eadcSLiang Li #include <zlib.h>
26b85ea5faSPeter Maydell #include "qemu/madvise.h"
27d49b6836SMarkus Armbruster #include "qemu/error-report.h"
2860fe637bSDr. David Alan Gilbert #include "qemu/iov.h"
296666c96aSJuan Quintela #include "migration.h"
3008a0aee1SJuan Quintela #include "qemu-file.h"
3160fe637bSDr. David Alan Gilbert #include "trace.h"
329d3ebbe2SJuan Quintela #include "options.h"
333d661c8aSYury Kotov #include "qapi/error.h"
3460fe637bSDr. David Alan Gilbert 
35a24939f2SDaniel P. Berrange #define IO_BUF_SIZE 32768
36f9919116SEric Blake #define MAX_IOV_SIZE MIN_CONST(IOV_MAX, 64)
37a24939f2SDaniel P. Berrange 
38a24939f2SDaniel P. Berrange struct QEMUFile {
39a24939f2SDaniel P. Berrange     const QEMUFileHooks *hooks;
402893a288SDaniel P. Berrangé     QIOChannel *ioc;
41c0c6e1e2SDaniel P. Berrangé     bool is_writable;
42a24939f2SDaniel P. Berrange 
43c7fc8d32SDaniel P. Berrangé     /*
44c7fc8d32SDaniel P. Berrangé      * Maximum amount of data in bytes to transfer during one
45c7fc8d32SDaniel P. Berrangé      * rate limiting time window
46c7fc8d32SDaniel P. Berrangé      */
47bffc0441SJuan Quintela     uint64_t rate_limit_max;
48c7fc8d32SDaniel P. Berrangé     /*
49c7fc8d32SDaniel P. Berrangé      * Total amount of data in bytes queued for transfer
50c7fc8d32SDaniel P. Berrangé      * during this rate limiting time window
51c7fc8d32SDaniel P. Berrangé      */
52f87e4d6dSJuan Quintela     uint64_t rate_limit_used;
53a24939f2SDaniel P. Berrange 
54154d87b4SDaniel P. Berrangé     /* The sum of bytes transferred on the wire */
5561abf1ebSJuan Quintela     uint64_t total_transferred;
56154d87b4SDaniel P. Berrangé 
57a24939f2SDaniel P. Berrange     int buf_index;
58a24939f2SDaniel P. Berrange     int buf_size; /* 0 when writing */
59a24939f2SDaniel P. Berrange     uint8_t buf[IO_BUF_SIZE];
60a24939f2SDaniel P. Berrange 
6153f09a10SPavel Butsykin     DECLARE_BITMAP(may_free, MAX_IOV_SIZE);
62a24939f2SDaniel P. Berrange     struct iovec iov[MAX_IOV_SIZE];
63a24939f2SDaniel P. Berrange     unsigned int iovcnt;
64a24939f2SDaniel P. Berrange 
65a24939f2SDaniel P. Berrange     int last_error;
663d661c8aSYury Kotov     Error *last_error_obj;
67a24939f2SDaniel P. Berrange };
68a24939f2SDaniel P. Berrange 
69e1a8c9b6SDr. David Alan Gilbert /*
70e1a8c9b6SDr. David Alan Gilbert  * Stop a file from being read/written - not all backing files can do this
71e1a8c9b6SDr. David Alan Gilbert  * typically only sockets can.
72d3c581b7SDaniel P. Berrangé  *
73d3c581b7SDaniel P. Berrangé  * TODO: convert to propagate Error objects instead of squashing
74d3c581b7SDaniel P. Berrangé  * to a fixed errno value
75e1a8c9b6SDr. David Alan Gilbert  */
76e1a8c9b6SDr. David Alan Gilbert int qemu_file_shutdown(QEMUFile *f)
77e1a8c9b6SDr. David Alan Gilbert {
78d3c581b7SDaniel P. Berrangé     int ret = 0;
79a555b809SJuan Quintela 
80f5816b5cSPeter Xu     /*
81f5816b5cSPeter Xu      * We must set qemufile error before the real shutdown(), otherwise
82f5816b5cSPeter Xu      * there can be a race window where we thought IO all went though
83f5816b5cSPeter Xu      * (because last_error==NULL) but actually IO has already stopped.
84f5816b5cSPeter Xu      *
85f5816b5cSPeter Xu      * If without correct ordering, the race can happen like this:
86f5816b5cSPeter Xu      *
87f5816b5cSPeter Xu      *      page receiver                     other thread
88f5816b5cSPeter Xu      *      -------------                     ------------
89f5816b5cSPeter Xu      *      qemu_get_buffer()
90f5816b5cSPeter Xu      *                                        do shutdown()
91f5816b5cSPeter Xu      *        returns 0 (buffer all zero)
92f5816b5cSPeter Xu      *        (we didn't check this retcode)
93f5816b5cSPeter Xu      *      try to detect IO error
94f5816b5cSPeter Xu      *        last_error==NULL, IO okay
95f5816b5cSPeter Xu      *      install ALL-ZERO page
96f5816b5cSPeter Xu      *                                        set last_error
97f5816b5cSPeter Xu      *      --> guest crash!
98f5816b5cSPeter Xu      */
99f5816b5cSPeter Xu     if (!f->last_error) {
100f5816b5cSPeter Xu         qemu_file_set_error(f, -EIO);
101f5816b5cSPeter Xu     }
102f5816b5cSPeter Xu 
103d3c581b7SDaniel P. Berrangé     if (!qio_channel_has_feature(f->ioc,
104d3c581b7SDaniel P. Berrangé                                  QIO_CHANNEL_FEATURE_SHUTDOWN)) {
105e1a8c9b6SDr. David Alan Gilbert         return -ENOSYS;
106e1a8c9b6SDr. David Alan Gilbert     }
107d3c581b7SDaniel P. Berrangé 
108d3c581b7SDaniel P. Berrangé     if (qio_channel_shutdown(f->ioc, QIO_CHANNEL_SHUTDOWN_BOTH, NULL) < 0) {
109d3c581b7SDaniel P. Berrangé         ret = -EIO;
110d3c581b7SDaniel P. Berrangé     }
111a555b809SJuan Quintela 
112a555b809SJuan Quintela     return ret;
113e1a8c9b6SDr. David Alan Gilbert }
114e1a8c9b6SDr. David Alan Gilbert 
11560fe637bSDr. David Alan Gilbert bool qemu_file_mode_is_not_valid(const char *mode)
11660fe637bSDr. David Alan Gilbert {
11760fe637bSDr. David Alan Gilbert     if (mode == NULL ||
11860fe637bSDr. David Alan Gilbert         (mode[0] != 'r' && mode[0] != 'w') ||
11960fe637bSDr. David Alan Gilbert         mode[1] != 'b' || mode[2] != 0) {
12060fe637bSDr. David Alan Gilbert         fprintf(stderr, "qemu_fopen: Argument validity check failed\n");
12160fe637bSDr. David Alan Gilbert         return true;
12260fe637bSDr. David Alan Gilbert     }
12360fe637bSDr. David Alan Gilbert 
12460fe637bSDr. David Alan Gilbert     return false;
12560fe637bSDr. David Alan Gilbert }
12660fe637bSDr. David Alan Gilbert 
12777ef2dc1SDaniel P. Berrangé static QEMUFile *qemu_file_new_impl(QIOChannel *ioc, bool is_writable)
12860fe637bSDr. David Alan Gilbert {
12960fe637bSDr. David Alan Gilbert     QEMUFile *f;
13060fe637bSDr. David Alan Gilbert 
13197f3ad35SMarkus Armbruster     f = g_new0(QEMUFile, 1);
13260fe637bSDr. David Alan Gilbert 
13377ef2dc1SDaniel P. Berrangé     object_ref(ioc);
1342893a288SDaniel P. Berrangé     f->ioc = ioc;
135c0c6e1e2SDaniel P. Berrangé     f->is_writable = is_writable;
1362893a288SDaniel P. Berrangé 
13760fe637bSDr. David Alan Gilbert     return f;
13860fe637bSDr. David Alan Gilbert }
13960fe637bSDr. David Alan Gilbert 
14002bdbe17SDaniel P. Berrangé /*
14102bdbe17SDaniel P. Berrangé  * Result: QEMUFile* for a 'return path' for comms in the opposite direction
14202bdbe17SDaniel P. Berrangé  *         NULL if not available
14302bdbe17SDaniel P. Berrangé  */
14402bdbe17SDaniel P. Berrangé QEMUFile *qemu_file_get_return_path(QEMUFile *f)
14502bdbe17SDaniel P. Berrangé {
14677ef2dc1SDaniel P. Berrangé     return qemu_file_new_impl(f->ioc, !f->is_writable);
14702bdbe17SDaniel P. Berrangé }
14802bdbe17SDaniel P. Berrangé 
14977ef2dc1SDaniel P. Berrangé QEMUFile *qemu_file_new_output(QIOChannel *ioc)
150c0c6e1e2SDaniel P. Berrangé {
15177ef2dc1SDaniel P. Berrangé     return qemu_file_new_impl(ioc, true);
152c0c6e1e2SDaniel P. Berrangé }
153c0c6e1e2SDaniel P. Berrangé 
15477ef2dc1SDaniel P. Berrangé QEMUFile *qemu_file_new_input(QIOChannel *ioc)
155c0c6e1e2SDaniel P. Berrangé {
15677ef2dc1SDaniel P. Berrangé     return qemu_file_new_impl(ioc, false);
157c0c6e1e2SDaniel P. Berrangé }
158c0c6e1e2SDaniel P. Berrangé 
1590436e09fSDaniel P. Berrange void qemu_file_set_hooks(QEMUFile *f, const QEMUFileHooks *hooks)
1600436e09fSDaniel P. Berrange {
1610436e09fSDaniel P. Berrange     f->hooks = hooks;
1620436e09fSDaniel P. Berrange }
1630436e09fSDaniel P. Berrange 
16460fe637bSDr. David Alan Gilbert /*
1653d661c8aSYury Kotov  * Get last error for stream f with optional Error*
1663d661c8aSYury Kotov  *
1673d661c8aSYury Kotov  * Return negative error value if there has been an error on previous
1683d661c8aSYury Kotov  * operations, return 0 if no error happened.
1693d661c8aSYury Kotov  * Optional, it returns Error* in errp, but it may be NULL even if return value
1703d661c8aSYury Kotov  * is not 0.
1713d661c8aSYury Kotov  *
1723d661c8aSYury Kotov  */
1733d661c8aSYury Kotov int qemu_file_get_error_obj(QEMUFile *f, Error **errp)
1743d661c8aSYury Kotov {
1753d661c8aSYury Kotov     if (errp) {
1763d661c8aSYury Kotov         *errp = f->last_error_obj ? error_copy(f->last_error_obj) : NULL;
1773d661c8aSYury Kotov     }
1783d661c8aSYury Kotov     return f->last_error;
1793d661c8aSYury Kotov }
1803d661c8aSYury Kotov 
1813d661c8aSYury Kotov /*
18260bb3c58SPeter Xu  * Get last error for either stream f1 or f2 with optional Error*.
18360bb3c58SPeter Xu  * The error returned (non-zero) can be either from f1 or f2.
18460bb3c58SPeter Xu  *
18560bb3c58SPeter Xu  * If any of the qemufile* is NULL, then skip the check on that file.
18660bb3c58SPeter Xu  *
18760bb3c58SPeter Xu  * When there is no error on both qemufile, zero is returned.
18860bb3c58SPeter Xu  */
18960bb3c58SPeter Xu int qemu_file_get_error_obj_any(QEMUFile *f1, QEMUFile *f2, Error **errp)
19060bb3c58SPeter Xu {
19160bb3c58SPeter Xu     int ret = 0;
19260bb3c58SPeter Xu 
19360bb3c58SPeter Xu     if (f1) {
19460bb3c58SPeter Xu         ret = qemu_file_get_error_obj(f1, errp);
19560bb3c58SPeter Xu         /* If there's already error detected, return */
19660bb3c58SPeter Xu         if (ret) {
19760bb3c58SPeter Xu             return ret;
19860bb3c58SPeter Xu         }
19960bb3c58SPeter Xu     }
20060bb3c58SPeter Xu 
20160bb3c58SPeter Xu     if (f2) {
20260bb3c58SPeter Xu         ret = qemu_file_get_error_obj(f2, errp);
20360bb3c58SPeter Xu     }
20460bb3c58SPeter Xu 
20560bb3c58SPeter Xu     return ret;
20660bb3c58SPeter Xu }
20760bb3c58SPeter Xu 
20860bb3c58SPeter Xu /*
2093d661c8aSYury Kotov  * Set the last error for stream f with optional Error*
2103d661c8aSYury Kotov  */
2113d661c8aSYury Kotov void qemu_file_set_error_obj(QEMUFile *f, int ret, Error *err)
2123d661c8aSYury Kotov {
2133d661c8aSYury Kotov     if (f->last_error == 0 && ret) {
2143d661c8aSYury Kotov         f->last_error = ret;
2153d661c8aSYury Kotov         error_propagate(&f->last_error_obj, err);
2163d661c8aSYury Kotov     } else if (err) {
2173d661c8aSYury Kotov         error_report_err(err);
2183d661c8aSYury Kotov     }
2193d661c8aSYury Kotov }
2203d661c8aSYury Kotov 
2213d661c8aSYury Kotov /*
22260fe637bSDr. David Alan Gilbert  * Get last error for stream f
22360fe637bSDr. David Alan Gilbert  *
22460fe637bSDr. David Alan Gilbert  * Return negative error value if there has been an error on previous
22560fe637bSDr. David Alan Gilbert  * operations, return 0 if no error happened.
22660fe637bSDr. David Alan Gilbert  *
22760fe637bSDr. David Alan Gilbert  */
22860fe637bSDr. David Alan Gilbert int qemu_file_get_error(QEMUFile *f)
22960fe637bSDr. David Alan Gilbert {
2303d661c8aSYury Kotov     return qemu_file_get_error_obj(f, NULL);
23160fe637bSDr. David Alan Gilbert }
23260fe637bSDr. David Alan Gilbert 
2333d661c8aSYury Kotov /*
2343d661c8aSYury Kotov  * Set the last error for stream f
2353d661c8aSYury Kotov  */
23660fe637bSDr. David Alan Gilbert void qemu_file_set_error(QEMUFile *f, int ret)
23760fe637bSDr. David Alan Gilbert {
2383d661c8aSYury Kotov     qemu_file_set_error_obj(f, ret, NULL);
23960fe637bSDr. David Alan Gilbert }
24060fe637bSDr. David Alan Gilbert 
24160fe637bSDr. David Alan Gilbert bool qemu_file_is_writable(QEMUFile *f)
24260fe637bSDr. David Alan Gilbert {
243c0c6e1e2SDaniel P. Berrangé     return f->is_writable;
24460fe637bSDr. David Alan Gilbert }
24560fe637bSDr. David Alan Gilbert 
24653f09a10SPavel Butsykin static void qemu_iovec_release_ram(QEMUFile *f)
24753f09a10SPavel Butsykin {
24853f09a10SPavel Butsykin     struct iovec iov;
24953f09a10SPavel Butsykin     unsigned long idx;
25053f09a10SPavel Butsykin 
25153f09a10SPavel Butsykin     /* Find and release all the contiguous memory ranges marked as may_free. */
25253f09a10SPavel Butsykin     idx = find_next_bit(f->may_free, f->iovcnt, 0);
25353f09a10SPavel Butsykin     if (idx >= f->iovcnt) {
25453f09a10SPavel Butsykin         return;
25553f09a10SPavel Butsykin     }
25653f09a10SPavel Butsykin     iov = f->iov[idx];
25753f09a10SPavel Butsykin 
25853f09a10SPavel Butsykin     /* The madvise() in the loop is called for iov within a continuous range and
25953f09a10SPavel Butsykin      * then reinitialize the iov. And in the end, madvise() is called for the
26053f09a10SPavel Butsykin      * last iov.
26153f09a10SPavel Butsykin      */
26253f09a10SPavel Butsykin     while ((idx = find_next_bit(f->may_free, f->iovcnt, idx + 1)) < f->iovcnt) {
26353f09a10SPavel Butsykin         /* check for adjacent buffer and coalesce them */
26453f09a10SPavel Butsykin         if (iov.iov_base + iov.iov_len == f->iov[idx].iov_base) {
26553f09a10SPavel Butsykin             iov.iov_len += f->iov[idx].iov_len;
26653f09a10SPavel Butsykin             continue;
26753f09a10SPavel Butsykin         }
26853f09a10SPavel Butsykin         if (qemu_madvise(iov.iov_base, iov.iov_len, QEMU_MADV_DONTNEED) < 0) {
26953f09a10SPavel Butsykin             error_report("migrate: madvise DONTNEED failed %p %zd: %s",
27053f09a10SPavel Butsykin                          iov.iov_base, iov.iov_len, strerror(errno));
27153f09a10SPavel Butsykin         }
27253f09a10SPavel Butsykin         iov = f->iov[idx];
27353f09a10SPavel Butsykin     }
27453f09a10SPavel Butsykin     if (qemu_madvise(iov.iov_base, iov.iov_len, QEMU_MADV_DONTNEED) < 0) {
27553f09a10SPavel Butsykin             error_report("migrate: madvise DONTNEED failed %p %zd: %s",
27653f09a10SPavel Butsykin                          iov.iov_base, iov.iov_len, strerror(errno));
27753f09a10SPavel Butsykin     }
27853f09a10SPavel Butsykin     memset(f->may_free, 0, sizeof(f->may_free));
27953f09a10SPavel Butsykin }
28053f09a10SPavel Butsykin 
28177ef2dc1SDaniel P. Berrangé 
28260fe637bSDr. David Alan Gilbert /**
28360fe637bSDr. David Alan Gilbert  * Flushes QEMUFile buffer
28460fe637bSDr. David Alan Gilbert  *
2853b348706SDr. David Alan Gilbert  * This will flush all pending data. If data was only partially flushed, it
2863b348706SDr. David Alan Gilbert  * will set an error state.
28760fe637bSDr. David Alan Gilbert  */
28860fe637bSDr. David Alan Gilbert void qemu_fflush(QEMUFile *f)
28960fe637bSDr. David Alan Gilbert {
29060fe637bSDr. David Alan Gilbert     if (!qemu_file_is_writable(f)) {
29160fe637bSDr. David Alan Gilbert         return;
29260fe637bSDr. David Alan Gilbert     }
29360fe637bSDr. David Alan Gilbert 
294ac7d25b8SJuan Quintela     if (qemu_file_get_error(f)) {
295a555b809SJuan Quintela         return;
296a555b809SJuan Quintela     }
29760fe637bSDr. David Alan Gilbert     if (f->iovcnt > 0) {
298ec2135eeSDaniel P. Berrangé         Error *local_error = NULL;
299ec2135eeSDaniel P. Berrangé         if (qio_channel_writev_all(f->ioc,
300ec2135eeSDaniel P. Berrangé                                    f->iov, f->iovcnt,
301ec2135eeSDaniel P. Berrangé                                    &local_error) < 0) {
302ec2135eeSDaniel P. Berrangé             qemu_file_set_error_obj(f, -EIO, local_error);
303ec2135eeSDaniel P. Berrangé         } else {
304ec2135eeSDaniel P. Berrangé             f->total_transferred += iov_size(f->iov, f->iovcnt);
305ec2135eeSDaniel P. Berrangé         }
30653f09a10SPavel Butsykin 
30753f09a10SPavel Butsykin         qemu_iovec_release_ram(f);
30860fe637bSDr. David Alan Gilbert     }
309baf51e77SDaniel P. Berrange 
31060fe637bSDr. David Alan Gilbert     f->buf_index = 0;
31160fe637bSDr. David Alan Gilbert     f->iovcnt = 0;
31260fe637bSDr. David Alan Gilbert }
31360fe637bSDr. David Alan Gilbert 
31460fe637bSDr. David Alan Gilbert void ram_control_before_iterate(QEMUFile *f, uint64_t flags)
31560fe637bSDr. David Alan Gilbert {
31660fe637bSDr. David Alan Gilbert     int ret = 0;
31760fe637bSDr. David Alan Gilbert 
3180436e09fSDaniel P. Berrange     if (f->hooks && f->hooks->before_ram_iterate) {
319365c0463SDaniel P. Berrangé         ret = f->hooks->before_ram_iterate(f, flags, NULL);
32060fe637bSDr. David Alan Gilbert         if (ret < 0) {
32160fe637bSDr. David Alan Gilbert             qemu_file_set_error(f, ret);
32260fe637bSDr. David Alan Gilbert         }
32360fe637bSDr. David Alan Gilbert     }
32460fe637bSDr. David Alan Gilbert }
32560fe637bSDr. David Alan Gilbert 
32660fe637bSDr. David Alan Gilbert void ram_control_after_iterate(QEMUFile *f, uint64_t flags)
32760fe637bSDr. David Alan Gilbert {
32860fe637bSDr. David Alan Gilbert     int ret = 0;
32960fe637bSDr. David Alan Gilbert 
3300436e09fSDaniel P. Berrange     if (f->hooks && f->hooks->after_ram_iterate) {
331365c0463SDaniel P. Berrangé         ret = f->hooks->after_ram_iterate(f, flags, NULL);
33260fe637bSDr. David Alan Gilbert         if (ret < 0) {
33360fe637bSDr. David Alan Gilbert             qemu_file_set_error(f, ret);
33460fe637bSDr. David Alan Gilbert         }
33560fe637bSDr. David Alan Gilbert     }
33660fe637bSDr. David Alan Gilbert }
33760fe637bSDr. David Alan Gilbert 
338632e3a5cSDr. David Alan Gilbert void ram_control_load_hook(QEMUFile *f, uint64_t flags, void *data)
33960fe637bSDr. David Alan Gilbert {
3400436e09fSDaniel P. Berrange     if (f->hooks && f->hooks->hook_ram_load) {
341cf7fe0c5SJuan Quintela         int ret = f->hooks->hook_ram_load(f, flags, data);
34260fe637bSDr. David Alan Gilbert         if (ret < 0) {
34360fe637bSDr. David Alan Gilbert             qemu_file_set_error(f, ret);
34460fe637bSDr. David Alan Gilbert         }
34560fe637bSDr. David Alan Gilbert     }
346632e3a5cSDr. David Alan Gilbert }
34760fe637bSDr. David Alan Gilbert 
34860fe637bSDr. David Alan Gilbert size_t ram_control_save_page(QEMUFile *f, ram_addr_t block_offset,
3496e1dea46SJuan Quintela                              ram_addr_t offset, size_t size,
3506e1dea46SJuan Quintela                              uint64_t *bytes_sent)
35160fe637bSDr. David Alan Gilbert {
3520436e09fSDaniel P. Berrange     if (f->hooks && f->hooks->save_page) {
353365c0463SDaniel P. Berrangé         int ret = f->hooks->save_page(f, block_offset,
35460fe637bSDr. David Alan Gilbert                                       offset, size, bytes_sent);
355ccb7e1b5SLidong Chen         if (ret != RAM_SAVE_CONTROL_NOT_SUPP) {
356fae4009fSJuan Quintela             qemu_file_acct_rate_limit(f, size);
357ccb7e1b5SLidong Chen         }
358ccb7e1b5SLidong Chen 
359ccb7e1b5SLidong Chen         if (ret != RAM_SAVE_CONTROL_DELAYED &&
360ccb7e1b5SLidong Chen             ret != RAM_SAVE_CONTROL_NOT_SUPP) {
36160fe637bSDr. David Alan Gilbert             if (bytes_sent && *bytes_sent > 0) {
3621a93bd2fSDaniel P. Berrangé                 qemu_file_credit_transfer(f, *bytes_sent);
36360fe637bSDr. David Alan Gilbert             } else if (ret < 0) {
36460fe637bSDr. David Alan Gilbert                 qemu_file_set_error(f, ret);
36560fe637bSDr. David Alan Gilbert             }
36660fe637bSDr. David Alan Gilbert         }
36760fe637bSDr. David Alan Gilbert 
36860fe637bSDr. David Alan Gilbert         return ret;
36960fe637bSDr. David Alan Gilbert     }
37060fe637bSDr. David Alan Gilbert 
37160fe637bSDr. David Alan Gilbert     return RAM_SAVE_CONTROL_NOT_SUPP;
37260fe637bSDr. David Alan Gilbert }
37360fe637bSDr. David Alan Gilbert 
37460fe637bSDr. David Alan Gilbert /*
37560fe637bSDr. David Alan Gilbert  * Attempt to fill the buffer from the underlying file
37660fe637bSDr. David Alan Gilbert  * Returns the number of bytes read, or negative value for an error.
37760fe637bSDr. David Alan Gilbert  *
37860fe637bSDr. David Alan Gilbert  * Note that it can return a partially full buffer even in a not error/not EOF
37960fe637bSDr. David Alan Gilbert  * case if the underlying file descriptor gives a short read, and that can
38060fe637bSDr. David Alan Gilbert  * happen even on a blocking fd.
38160fe637bSDr. David Alan Gilbert  */
382394b9407SPaolo Bonzini static ssize_t coroutine_mixed_fn qemu_fill_buffer(QEMUFile *f)
38360fe637bSDr. David Alan Gilbert {
38460fe637bSDr. David Alan Gilbert     int len;
38560fe637bSDr. David Alan Gilbert     int pending;
3863d661c8aSYury Kotov     Error *local_error = NULL;
38760fe637bSDr. David Alan Gilbert 
38860fe637bSDr. David Alan Gilbert     assert(!qemu_file_is_writable(f));
38960fe637bSDr. David Alan Gilbert 
39060fe637bSDr. David Alan Gilbert     pending = f->buf_size - f->buf_index;
39160fe637bSDr. David Alan Gilbert     if (pending > 0) {
39260fe637bSDr. David Alan Gilbert         memmove(f->buf, f->buf + f->buf_index, pending);
39360fe637bSDr. David Alan Gilbert     }
39460fe637bSDr. David Alan Gilbert     f->buf_index = 0;
39560fe637bSDr. David Alan Gilbert     f->buf_size = pending;
39660fe637bSDr. David Alan Gilbert 
397ac7d25b8SJuan Quintela     if (qemu_file_get_error(f)) {
398a555b809SJuan Quintela         return 0;
399a555b809SJuan Quintela     }
400a555b809SJuan Quintela 
401f759d705SDaniel P. Berrangé     do {
402f759d705SDaniel P. Berrangé         len = qio_channel_read(f->ioc,
403f759d705SDaniel P. Berrangé                                (char *)f->buf + pending,
404f759d705SDaniel P. Berrangé                                IO_BUF_SIZE - pending,
405f759d705SDaniel P. Berrangé                                &local_error);
406f759d705SDaniel P. Berrangé         if (len == QIO_CHANNEL_ERR_BLOCK) {
407f759d705SDaniel P. Berrangé             if (qemu_in_coroutine()) {
408f759d705SDaniel P. Berrangé                 qio_channel_yield(f->ioc, G_IO_IN);
409f759d705SDaniel P. Berrangé             } else {
410f759d705SDaniel P. Berrangé                 qio_channel_wait(f->ioc, G_IO_IN);
411f759d705SDaniel P. Berrangé             }
412f759d705SDaniel P. Berrangé         } else if (len < 0) {
413f759d705SDaniel P. Berrangé             len = -EIO;
414f759d705SDaniel P. Berrangé         }
415f759d705SDaniel P. Berrangé     } while (len == QIO_CHANNEL_ERR_BLOCK);
416f759d705SDaniel P. Berrangé 
41760fe637bSDr. David Alan Gilbert     if (len > 0) {
41860fe637bSDr. David Alan Gilbert         f->buf_size += len;
419154d87b4SDaniel P. Berrangé         f->total_transferred += len;
42060fe637bSDr. David Alan Gilbert     } else if (len == 0) {
4213d661c8aSYury Kotov         qemu_file_set_error_obj(f, -EIO, local_error);
4223d661c8aSYury Kotov     } else {
4235f87072eSDaniel P. Berrangé         qemu_file_set_error_obj(f, len, local_error);
42460fe637bSDr. David Alan Gilbert     }
42560fe637bSDr. David Alan Gilbert 
42660fe637bSDr. David Alan Gilbert     return len;
42760fe637bSDr. David Alan Gilbert }
42860fe637bSDr. David Alan Gilbert 
4291a93bd2fSDaniel P. Berrangé void qemu_file_credit_transfer(QEMUFile *f, size_t size)
43060fe637bSDr. David Alan Gilbert {
431154d87b4SDaniel P. Berrangé     f->total_transferred += size;
43260fe637bSDr. David Alan Gilbert }
43360fe637bSDr. David Alan Gilbert 
43460fe637bSDr. David Alan Gilbert /** Closes the file
43560fe637bSDr. David Alan Gilbert  *
43660fe637bSDr. David Alan Gilbert  * Returns negative error value if any error happened on previous operations or
43760fe637bSDr. David Alan Gilbert  * while closing the file. Returns 0 or positive number on success.
43860fe637bSDr. David Alan Gilbert  *
43960fe637bSDr. David Alan Gilbert  * The meaning of return value on success depends on the specific backend
44060fe637bSDr. David Alan Gilbert  * being used.
44160fe637bSDr. David Alan Gilbert  */
44260fe637bSDr. David Alan Gilbert int qemu_fclose(QEMUFile *f)
44360fe637bSDr. David Alan Gilbert {
4440ae1f7f0SDaniel P. Berrangé     int ret, ret2;
44560fe637bSDr. David Alan Gilbert     qemu_fflush(f);
44660fe637bSDr. David Alan Gilbert     ret = qemu_file_get_error(f);
44760fe637bSDr. David Alan Gilbert 
4480ae1f7f0SDaniel P. Berrangé     ret2 = qio_channel_close(f->ioc, NULL);
44960fe637bSDr. David Alan Gilbert     if (ret >= 0) {
45060fe637bSDr. David Alan Gilbert         ret = ret2;
45160fe637bSDr. David Alan Gilbert     }
4520ae1f7f0SDaniel P. Berrangé     g_clear_pointer(&f->ioc, object_unref);
4530ae1f7f0SDaniel P. Berrangé 
45460fe637bSDr. David Alan Gilbert     /* If any error was spotted before closing, we should report it
45560fe637bSDr. David Alan Gilbert      * instead of the close() return value.
45660fe637bSDr. David Alan Gilbert      */
45760fe637bSDr. David Alan Gilbert     if (f->last_error) {
45860fe637bSDr. David Alan Gilbert         ret = f->last_error;
45960fe637bSDr. David Alan Gilbert     }
4603d661c8aSYury Kotov     error_free(f->last_error_obj);
46160fe637bSDr. David Alan Gilbert     g_free(f);
46260fe637bSDr. David Alan Gilbert     trace_qemu_file_fclose();
46360fe637bSDr. David Alan Gilbert     return ret;
46460fe637bSDr. David Alan Gilbert }
46560fe637bSDr. David Alan Gilbert 
4661bf57fb3SWei Yang /*
4671bf57fb3SWei Yang  * Add buf to iovec. Do flush if iovec is full.
4681bf57fb3SWei Yang  *
4691bf57fb3SWei Yang  * Return values:
4701bf57fb3SWei Yang  * 1 iovec is full and flushed
4711bf57fb3SWei Yang  * 0 iovec is not flushed
4721bf57fb3SWei Yang  *
4731bf57fb3SWei Yang  */
4741bf57fb3SWei Yang static int add_to_iovec(QEMUFile *f, const uint8_t *buf, size_t size,
47553f09a10SPavel Butsykin                         bool may_free)
47660fe637bSDr. David Alan Gilbert {
47760fe637bSDr. David Alan Gilbert     /* check for adjacent buffer and coalesce them */
47860fe637bSDr. David Alan Gilbert     if (f->iovcnt > 0 && buf == f->iov[f->iovcnt - 1].iov_base +
47953f09a10SPavel Butsykin         f->iov[f->iovcnt - 1].iov_len &&
48053f09a10SPavel Butsykin         may_free == test_bit(f->iovcnt - 1, f->may_free))
48153f09a10SPavel Butsykin     {
48260fe637bSDr. David Alan Gilbert         f->iov[f->iovcnt - 1].iov_len += size;
48360fe637bSDr. David Alan Gilbert     } else {
484c00d434aSFeng Lin         if (f->iovcnt >= MAX_IOV_SIZE) {
485c00d434aSFeng Lin             /* Should only happen if a previous fflush failed */
486ac7d25b8SJuan Quintela             assert(qemu_file_get_error(f) || !qemu_file_is_writable(f));
487c00d434aSFeng Lin             return 1;
488c00d434aSFeng Lin         }
48953f09a10SPavel Butsykin         if (may_free) {
49053f09a10SPavel Butsykin             set_bit(f->iovcnt, f->may_free);
49153f09a10SPavel Butsykin         }
49260fe637bSDr. David Alan Gilbert         f->iov[f->iovcnt].iov_base = (uint8_t *)buf;
49360fe637bSDr. David Alan Gilbert         f->iov[f->iovcnt++].iov_len = size;
49460fe637bSDr. David Alan Gilbert     }
49560fe637bSDr. David Alan Gilbert 
49660fe637bSDr. David Alan Gilbert     if (f->iovcnt >= MAX_IOV_SIZE) {
49760fe637bSDr. David Alan Gilbert         qemu_fflush(f);
4981bf57fb3SWei Yang         return 1;
4991bf57fb3SWei Yang     }
5001bf57fb3SWei Yang 
5011bf57fb3SWei Yang     return 0;
5021bf57fb3SWei Yang }
5031bf57fb3SWei Yang 
5041bf57fb3SWei Yang static void add_buf_to_iovec(QEMUFile *f, size_t len)
5051bf57fb3SWei Yang {
5061bf57fb3SWei Yang     if (!add_to_iovec(f, f->buf + f->buf_index, len, false)) {
5071bf57fb3SWei Yang         f->buf_index += len;
5081bf57fb3SWei Yang         if (f->buf_index == IO_BUF_SIZE) {
5091bf57fb3SWei Yang             qemu_fflush(f);
5101bf57fb3SWei Yang         }
51160fe637bSDr. David Alan Gilbert     }
51260fe637bSDr. David Alan Gilbert }
51360fe637bSDr. David Alan Gilbert 
51453f09a10SPavel Butsykin void qemu_put_buffer_async(QEMUFile *f, const uint8_t *buf, size_t size,
51553f09a10SPavel Butsykin                            bool may_free)
51660fe637bSDr. David Alan Gilbert {
51760fe637bSDr. David Alan Gilbert     if (f->last_error) {
51860fe637bSDr. David Alan Gilbert         return;
51960fe637bSDr. David Alan Gilbert     }
52060fe637bSDr. David Alan Gilbert 
521c7fc8d32SDaniel P. Berrangé     f->rate_limit_used += size;
52253f09a10SPavel Butsykin     add_to_iovec(f, buf, size, may_free);
52360fe637bSDr. David Alan Gilbert }
52460fe637bSDr. David Alan Gilbert 
52556f3835fSDr. David Alan Gilbert void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, size_t size)
52660fe637bSDr. David Alan Gilbert {
52756f3835fSDr. David Alan Gilbert     size_t l;
52860fe637bSDr. David Alan Gilbert 
52960fe637bSDr. David Alan Gilbert     if (f->last_error) {
53060fe637bSDr. David Alan Gilbert         return;
53160fe637bSDr. David Alan Gilbert     }
53260fe637bSDr. David Alan Gilbert 
53360fe637bSDr. David Alan Gilbert     while (size > 0) {
53460fe637bSDr. David Alan Gilbert         l = IO_BUF_SIZE - f->buf_index;
53560fe637bSDr. David Alan Gilbert         if (l > size) {
53660fe637bSDr. David Alan Gilbert             l = size;
53760fe637bSDr. David Alan Gilbert         }
53860fe637bSDr. David Alan Gilbert         memcpy(f->buf + f->buf_index, buf, l);
539c7fc8d32SDaniel P. Berrangé         f->rate_limit_used += l;
5401bf57fb3SWei Yang         add_buf_to_iovec(f, l);
54160fe637bSDr. David Alan Gilbert         if (qemu_file_get_error(f)) {
54260fe637bSDr. David Alan Gilbert             break;
54360fe637bSDr. David Alan Gilbert         }
54460fe637bSDr. David Alan Gilbert         buf += l;
54560fe637bSDr. David Alan Gilbert         size -= l;
54660fe637bSDr. David Alan Gilbert     }
54760fe637bSDr. David Alan Gilbert }
54860fe637bSDr. David Alan Gilbert 
54960fe637bSDr. David Alan Gilbert void qemu_put_byte(QEMUFile *f, int v)
55060fe637bSDr. David Alan Gilbert {
55160fe637bSDr. David Alan Gilbert     if (f->last_error) {
55260fe637bSDr. David Alan Gilbert         return;
55360fe637bSDr. David Alan Gilbert     }
55460fe637bSDr. David Alan Gilbert 
55560fe637bSDr. David Alan Gilbert     f->buf[f->buf_index] = v;
556c7fc8d32SDaniel P. Berrangé     f->rate_limit_used++;
5571bf57fb3SWei Yang     add_buf_to_iovec(f, 1);
55860fe637bSDr. David Alan Gilbert }
55960fe637bSDr. David Alan Gilbert 
56060fe637bSDr. David Alan Gilbert void qemu_file_skip(QEMUFile *f, int size)
56160fe637bSDr. David Alan Gilbert {
56260fe637bSDr. David Alan Gilbert     if (f->buf_index + size <= f->buf_size) {
56360fe637bSDr. David Alan Gilbert         f->buf_index += size;
56460fe637bSDr. David Alan Gilbert     }
56560fe637bSDr. David Alan Gilbert }
56660fe637bSDr. David Alan Gilbert 
56760fe637bSDr. David Alan Gilbert /*
5687c1e52baSDr. David Alan Gilbert  * Read 'size' bytes from file (at 'offset') without moving the
5697c1e52baSDr. David Alan Gilbert  * pointer and set 'buf' to point to that data.
57060fe637bSDr. David Alan Gilbert  *
57160fe637bSDr. David Alan Gilbert  * It will return size bytes unless there was an error, in which case it will
57260fe637bSDr. David Alan Gilbert  * return as many as it managed to read (assuming blocking fd's which
57360fe637bSDr. David Alan Gilbert  * all current QEMUFile are)
57460fe637bSDr. David Alan Gilbert  */
575394b9407SPaolo Bonzini size_t coroutine_mixed_fn qemu_peek_buffer(QEMUFile *f, uint8_t **buf, size_t size, size_t offset)
57660fe637bSDr. David Alan Gilbert {
57756f3835fSDr. David Alan Gilbert     ssize_t pending;
57856f3835fSDr. David Alan Gilbert     size_t index;
57960fe637bSDr. David Alan Gilbert 
58060fe637bSDr. David Alan Gilbert     assert(!qemu_file_is_writable(f));
58160fe637bSDr. David Alan Gilbert     assert(offset < IO_BUF_SIZE);
58260fe637bSDr. David Alan Gilbert     assert(size <= IO_BUF_SIZE - offset);
58360fe637bSDr. David Alan Gilbert 
58460fe637bSDr. David Alan Gilbert     /* The 1st byte to read from */
58560fe637bSDr. David Alan Gilbert     index = f->buf_index + offset;
58660fe637bSDr. David Alan Gilbert     /* The number of available bytes starting at index */
58760fe637bSDr. David Alan Gilbert     pending = f->buf_size - index;
58860fe637bSDr. David Alan Gilbert 
58960fe637bSDr. David Alan Gilbert     /*
59060fe637bSDr. David Alan Gilbert      * qemu_fill_buffer might return just a few bytes, even when there isn't
59160fe637bSDr. David Alan Gilbert      * an error, so loop collecting them until we get enough.
59260fe637bSDr. David Alan Gilbert      */
59360fe637bSDr. David Alan Gilbert     while (pending < size) {
59460fe637bSDr. David Alan Gilbert         int received = qemu_fill_buffer(f);
59560fe637bSDr. David Alan Gilbert 
59660fe637bSDr. David Alan Gilbert         if (received <= 0) {
59760fe637bSDr. David Alan Gilbert             break;
59860fe637bSDr. David Alan Gilbert         }
59960fe637bSDr. David Alan Gilbert 
60060fe637bSDr. David Alan Gilbert         index = f->buf_index + offset;
60160fe637bSDr. David Alan Gilbert         pending = f->buf_size - index;
60260fe637bSDr. David Alan Gilbert     }
60360fe637bSDr. David Alan Gilbert 
60460fe637bSDr. David Alan Gilbert     if (pending <= 0) {
60560fe637bSDr. David Alan Gilbert         return 0;
60660fe637bSDr. David Alan Gilbert     }
60760fe637bSDr. David Alan Gilbert     if (size > pending) {
60860fe637bSDr. David Alan Gilbert         size = pending;
60960fe637bSDr. David Alan Gilbert     }
61060fe637bSDr. David Alan Gilbert 
6117c1e52baSDr. David Alan Gilbert     *buf = f->buf + index;
61260fe637bSDr. David Alan Gilbert     return size;
61360fe637bSDr. David Alan Gilbert }
61460fe637bSDr. David Alan Gilbert 
61560fe637bSDr. David Alan Gilbert /*
61660fe637bSDr. David Alan Gilbert  * Read 'size' bytes of data from the file into buf.
61760fe637bSDr. David Alan Gilbert  * 'size' can be larger than the internal buffer.
61860fe637bSDr. David Alan Gilbert  *
61960fe637bSDr. David Alan Gilbert  * It will return size bytes unless there was an error, in which case it will
62060fe637bSDr. David Alan Gilbert  * return as many as it managed to read (assuming blocking fd's which
62160fe637bSDr. David Alan Gilbert  * all current QEMUFile are)
62260fe637bSDr. David Alan Gilbert  */
623394b9407SPaolo Bonzini size_t coroutine_mixed_fn qemu_get_buffer(QEMUFile *f, uint8_t *buf, size_t size)
62460fe637bSDr. David Alan Gilbert {
62556f3835fSDr. David Alan Gilbert     size_t pending = size;
62656f3835fSDr. David Alan Gilbert     size_t done = 0;
62760fe637bSDr. David Alan Gilbert 
62860fe637bSDr. David Alan Gilbert     while (pending > 0) {
62956f3835fSDr. David Alan Gilbert         size_t res;
6307c1e52baSDr. David Alan Gilbert         uint8_t *src;
63160fe637bSDr. David Alan Gilbert 
6327c1e52baSDr. David Alan Gilbert         res = qemu_peek_buffer(f, &src, MIN(pending, IO_BUF_SIZE), 0);
63360fe637bSDr. David Alan Gilbert         if (res == 0) {
63460fe637bSDr. David Alan Gilbert             return done;
63560fe637bSDr. David Alan Gilbert         }
6367c1e52baSDr. David Alan Gilbert         memcpy(buf, src, res);
63760fe637bSDr. David Alan Gilbert         qemu_file_skip(f, res);
63860fe637bSDr. David Alan Gilbert         buf += res;
63960fe637bSDr. David Alan Gilbert         pending -= res;
64060fe637bSDr. David Alan Gilbert         done += res;
64160fe637bSDr. David Alan Gilbert     }
64260fe637bSDr. David Alan Gilbert     return done;
64360fe637bSDr. David Alan Gilbert }
64460fe637bSDr. David Alan Gilbert 
64560fe637bSDr. David Alan Gilbert /*
6469504fb51SDr. David Alan Gilbert  * Read 'size' bytes of data from the file.
6479504fb51SDr. David Alan Gilbert  * 'size' can be larger than the internal buffer.
6489504fb51SDr. David Alan Gilbert  *
6499504fb51SDr. David Alan Gilbert  * The data:
6509504fb51SDr. David Alan Gilbert  *   may be held on an internal buffer (in which case *buf is updated
6519504fb51SDr. David Alan Gilbert  *     to point to it) that is valid until the next qemu_file operation.
6529504fb51SDr. David Alan Gilbert  * OR
6539504fb51SDr. David Alan Gilbert  *   will be copied to the *buf that was passed in.
6549504fb51SDr. David Alan Gilbert  *
6559504fb51SDr. David Alan Gilbert  * The code tries to avoid the copy if possible.
6569504fb51SDr. David Alan Gilbert  *
6579504fb51SDr. David Alan Gilbert  * It will return size bytes unless there was an error, in which case it will
6589504fb51SDr. David Alan Gilbert  * return as many as it managed to read (assuming blocking fd's which
6599504fb51SDr. David Alan Gilbert  * all current QEMUFile are)
6609504fb51SDr. David Alan Gilbert  *
6619504fb51SDr. David Alan Gilbert  * Note: Since **buf may get changed, the caller should take care to
6629504fb51SDr. David Alan Gilbert  *       keep a pointer to the original buffer if it needs to deallocate it.
6639504fb51SDr. David Alan Gilbert  */
664394b9407SPaolo Bonzini size_t coroutine_mixed_fn qemu_get_buffer_in_place(QEMUFile *f, uint8_t **buf, size_t size)
6659504fb51SDr. David Alan Gilbert {
6669504fb51SDr. David Alan Gilbert     if (size < IO_BUF_SIZE) {
6679504fb51SDr. David Alan Gilbert         size_t res;
6681dfafcbdSWainer dos Santos Moschetta         uint8_t *src = NULL;
6699504fb51SDr. David Alan Gilbert 
6709504fb51SDr. David Alan Gilbert         res = qemu_peek_buffer(f, &src, size, 0);
6719504fb51SDr. David Alan Gilbert 
6729504fb51SDr. David Alan Gilbert         if (res == size) {
6739504fb51SDr. David Alan Gilbert             qemu_file_skip(f, res);
6749504fb51SDr. David Alan Gilbert             *buf = src;
6759504fb51SDr. David Alan Gilbert             return res;
6769504fb51SDr. David Alan Gilbert         }
6779504fb51SDr. David Alan Gilbert     }
6789504fb51SDr. David Alan Gilbert 
6799504fb51SDr. David Alan Gilbert     return qemu_get_buffer(f, *buf, size);
6809504fb51SDr. David Alan Gilbert }
6819504fb51SDr. David Alan Gilbert 
6829504fb51SDr. David Alan Gilbert /*
68360fe637bSDr. David Alan Gilbert  * Peeks a single byte from the buffer; this isn't guaranteed to work if
68460fe637bSDr. David Alan Gilbert  * offset leaves a gap after the previous read/peeked data.
68560fe637bSDr. David Alan Gilbert  */
686394b9407SPaolo Bonzini int coroutine_mixed_fn qemu_peek_byte(QEMUFile *f, int offset)
68760fe637bSDr. David Alan Gilbert {
68860fe637bSDr. David Alan Gilbert     int index = f->buf_index + offset;
68960fe637bSDr. David Alan Gilbert 
69060fe637bSDr. David Alan Gilbert     assert(!qemu_file_is_writable(f));
69160fe637bSDr. David Alan Gilbert     assert(offset < IO_BUF_SIZE);
69260fe637bSDr. David Alan Gilbert 
69360fe637bSDr. David Alan Gilbert     if (index >= f->buf_size) {
69460fe637bSDr. David Alan Gilbert         qemu_fill_buffer(f);
69560fe637bSDr. David Alan Gilbert         index = f->buf_index + offset;
69660fe637bSDr. David Alan Gilbert         if (index >= f->buf_size) {
69760fe637bSDr. David Alan Gilbert             return 0;
69860fe637bSDr. David Alan Gilbert         }
69960fe637bSDr. David Alan Gilbert     }
70060fe637bSDr. David Alan Gilbert     return f->buf[index];
70160fe637bSDr. David Alan Gilbert }
70260fe637bSDr. David Alan Gilbert 
703394b9407SPaolo Bonzini int coroutine_mixed_fn qemu_get_byte(QEMUFile *f)
70460fe637bSDr. David Alan Gilbert {
70560fe637bSDr. David Alan Gilbert     int result;
70660fe637bSDr. David Alan Gilbert 
70760fe637bSDr. David Alan Gilbert     result = qemu_peek_byte(f, 0);
70860fe637bSDr. David Alan Gilbert     qemu_file_skip(f, 1);
70960fe637bSDr. David Alan Gilbert     return result;
71060fe637bSDr. David Alan Gilbert }
71160fe637bSDr. David Alan Gilbert 
71261abf1ebSJuan Quintela uint64_t qemu_file_total_transferred_fast(QEMUFile *f)
71397221400SAlexander Graf {
71461abf1ebSJuan Quintela     uint64_t ret = f->total_transferred;
71597221400SAlexander Graf     int i;
71697221400SAlexander Graf 
71797221400SAlexander Graf     for (i = 0; i < f->iovcnt; i++) {
71897221400SAlexander Graf         ret += f->iov[i].iov_len;
71997221400SAlexander Graf     }
72097221400SAlexander Graf 
72197221400SAlexander Graf     return ret;
72297221400SAlexander Graf }
72397221400SAlexander Graf 
72461abf1ebSJuan Quintela uint64_t qemu_file_total_transferred(QEMUFile *f)
72560fe637bSDr. David Alan Gilbert {
72660fe637bSDr. David Alan Gilbert     qemu_fflush(f);
727154d87b4SDaniel P. Berrangé     return f->total_transferred;
72860fe637bSDr. David Alan Gilbert }
72960fe637bSDr. David Alan Gilbert 
73060fe637bSDr. David Alan Gilbert int qemu_file_rate_limit(QEMUFile *f)
73160fe637bSDr. David Alan Gilbert {
73260fe637bSDr. David Alan Gilbert     if (qemu_file_get_error(f)) {
73360fe637bSDr. David Alan Gilbert         return 1;
73460fe637bSDr. David Alan Gilbert     }
735c7fc8d32SDaniel P. Berrangé     if (f->rate_limit_max > 0 && f->rate_limit_used > f->rate_limit_max) {
73660fe637bSDr. David Alan Gilbert         return 1;
73760fe637bSDr. David Alan Gilbert     }
73860fe637bSDr. David Alan Gilbert     return 0;
73960fe637bSDr. David Alan Gilbert }
74060fe637bSDr. David Alan Gilbert 
741bffc0441SJuan Quintela uint64_t qemu_file_get_rate_limit(QEMUFile *f)
74260fe637bSDr. David Alan Gilbert {
743c7fc8d32SDaniel P. Berrangé     return f->rate_limit_max;
74460fe637bSDr. David Alan Gilbert }
74560fe637bSDr. David Alan Gilbert 
746bffc0441SJuan Quintela void qemu_file_set_rate_limit(QEMUFile *f, uint64_t limit)
74760fe637bSDr. David Alan Gilbert {
7489d3ebbe2SJuan Quintela     /*
7499d3ebbe2SJuan Quintela      * 'limit' is per second.  But we check it each 100 miliseconds.
7509d3ebbe2SJuan Quintela      */
7519d3ebbe2SJuan Quintela     f->rate_limit_max = limit / XFER_LIMIT_RATIO;
75260fe637bSDr. David Alan Gilbert }
75360fe637bSDr. David Alan Gilbert 
75460fe637bSDr. David Alan Gilbert void qemu_file_reset_rate_limit(QEMUFile *f)
75560fe637bSDr. David Alan Gilbert {
756c7fc8d32SDaniel P. Berrangé     f->rate_limit_used = 0;
75760fe637bSDr. David Alan Gilbert }
75860fe637bSDr. David Alan Gilbert 
759f87e4d6dSJuan Quintela void qemu_file_acct_rate_limit(QEMUFile *f, uint64_t len)
7605d7d2558SIvan Ren {
761c7fc8d32SDaniel P. Berrangé     f->rate_limit_used += len;
7625d7d2558SIvan Ren }
7635d7d2558SIvan Ren 
76460fe637bSDr. David Alan Gilbert void qemu_put_be16(QEMUFile *f, unsigned int v)
76560fe637bSDr. David Alan Gilbert {
76660fe637bSDr. David Alan Gilbert     qemu_put_byte(f, v >> 8);
76760fe637bSDr. David Alan Gilbert     qemu_put_byte(f, v);
76860fe637bSDr. David Alan Gilbert }
76960fe637bSDr. David Alan Gilbert 
77060fe637bSDr. David Alan Gilbert void qemu_put_be32(QEMUFile *f, unsigned int v)
77160fe637bSDr. David Alan Gilbert {
77260fe637bSDr. David Alan Gilbert     qemu_put_byte(f, v >> 24);
77360fe637bSDr. David Alan Gilbert     qemu_put_byte(f, v >> 16);
77460fe637bSDr. David Alan Gilbert     qemu_put_byte(f, v >> 8);
77560fe637bSDr. David Alan Gilbert     qemu_put_byte(f, v);
77660fe637bSDr. David Alan Gilbert }
77760fe637bSDr. David Alan Gilbert 
77860fe637bSDr. David Alan Gilbert void qemu_put_be64(QEMUFile *f, uint64_t v)
77960fe637bSDr. David Alan Gilbert {
78060fe637bSDr. David Alan Gilbert     qemu_put_be32(f, v >> 32);
78160fe637bSDr. David Alan Gilbert     qemu_put_be32(f, v);
78260fe637bSDr. David Alan Gilbert }
78360fe637bSDr. David Alan Gilbert 
78460fe637bSDr. David Alan Gilbert unsigned int qemu_get_be16(QEMUFile *f)
78560fe637bSDr. David Alan Gilbert {
78660fe637bSDr. David Alan Gilbert     unsigned int v;
78760fe637bSDr. David Alan Gilbert     v = qemu_get_byte(f) << 8;
78860fe637bSDr. David Alan Gilbert     v |= qemu_get_byte(f);
78960fe637bSDr. David Alan Gilbert     return v;
79060fe637bSDr. David Alan Gilbert }
79160fe637bSDr. David Alan Gilbert 
79260fe637bSDr. David Alan Gilbert unsigned int qemu_get_be32(QEMUFile *f)
79360fe637bSDr. David Alan Gilbert {
79460fe637bSDr. David Alan Gilbert     unsigned int v;
79590d6a673SPeter Maydell     v = (unsigned int)qemu_get_byte(f) << 24;
79660fe637bSDr. David Alan Gilbert     v |= qemu_get_byte(f) << 16;
79760fe637bSDr. David Alan Gilbert     v |= qemu_get_byte(f) << 8;
79860fe637bSDr. David Alan Gilbert     v |= qemu_get_byte(f);
79960fe637bSDr. David Alan Gilbert     return v;
80060fe637bSDr. David Alan Gilbert }
80160fe637bSDr. David Alan Gilbert 
80260fe637bSDr. David Alan Gilbert uint64_t qemu_get_be64(QEMUFile *f)
80360fe637bSDr. David Alan Gilbert {
80460fe637bSDr. David Alan Gilbert     uint64_t v;
80560fe637bSDr. David Alan Gilbert     v = (uint64_t)qemu_get_be32(f) << 32;
80660fe637bSDr. David Alan Gilbert     v |= qemu_get_be32(f);
80760fe637bSDr. David Alan Gilbert     return v;
80860fe637bSDr. David Alan Gilbert }
80944f0eadcSLiang Li 
810dcaf446eSXiao Guangrong /* return the size after compression, or negative value on error */
811dcaf446eSXiao Guangrong static int qemu_compress_data(z_stream *stream, uint8_t *dest, size_t dest_len,
812dcaf446eSXiao Guangrong                               const uint8_t *source, size_t source_len)
813dcaf446eSXiao Guangrong {
814dcaf446eSXiao Guangrong     int err;
815dcaf446eSXiao Guangrong 
816dcaf446eSXiao Guangrong     err = deflateReset(stream);
817dcaf446eSXiao Guangrong     if (err != Z_OK) {
818dcaf446eSXiao Guangrong         return -1;
819dcaf446eSXiao Guangrong     }
820dcaf446eSXiao Guangrong 
821dcaf446eSXiao Guangrong     stream->avail_in = source_len;
822dcaf446eSXiao Guangrong     stream->next_in = (uint8_t *)source;
823dcaf446eSXiao Guangrong     stream->avail_out = dest_len;
824dcaf446eSXiao Guangrong     stream->next_out = dest;
825dcaf446eSXiao Guangrong 
826dcaf446eSXiao Guangrong     err = deflate(stream, Z_FINISH);
827dcaf446eSXiao Guangrong     if (err != Z_STREAM_END) {
828dcaf446eSXiao Guangrong         return -1;
829dcaf446eSXiao Guangrong     }
830dcaf446eSXiao Guangrong 
831dcaf446eSXiao Guangrong     return stream->next_out - dest;
832dcaf446eSXiao Guangrong }
833dcaf446eSXiao Guangrong 
834dcaf446eSXiao Guangrong /* Compress size bytes of data start at p and store the compressed
835dcaf446eSXiao Guangrong  * data to the buffer of f.
836b3be2896SLiang Li  *
83742d24611SWei Yang  * Since the file is dummy file with empty_ops, return -1 if f has no space to
83842d24611SWei Yang  * save the compressed data.
83944f0eadcSLiang Li  */
840dcaf446eSXiao Guangrong ssize_t qemu_put_compression_data(QEMUFile *f, z_stream *stream,
841dcaf446eSXiao Guangrong                                   const uint8_t *p, size_t size)
84244f0eadcSLiang Li {
84344f0eadcSLiang Li     ssize_t blen = IO_BUF_SIZE - f->buf_index - sizeof(int32_t);
84444f0eadcSLiang Li 
84544f0eadcSLiang Li     if (blen < compressBound(size)) {
846b3be2896SLiang Li         return -1;
847b3be2896SLiang Li     }
848dcaf446eSXiao Guangrong 
849dcaf446eSXiao Guangrong     blen = qemu_compress_data(stream, f->buf + f->buf_index + sizeof(int32_t),
850dcaf446eSXiao Guangrong                               blen, p, size);
851dcaf446eSXiao Guangrong     if (blen < 0) {
85234ab9e97SXiao Guangrong         return -1;
85344f0eadcSLiang Li     }
85434ab9e97SXiao Guangrong 
85544f0eadcSLiang Li     qemu_put_be32(f, blen);
8561bf57fb3SWei Yang     add_buf_to_iovec(f, blen);
85744f0eadcSLiang Li     return blen + sizeof(int32_t);
85844f0eadcSLiang Li }
85944f0eadcSLiang Li 
86044f0eadcSLiang Li /* Put the data in the buffer of f_src to the buffer of f_des, and
86144f0eadcSLiang Li  * then reset the buf_index of f_src to 0.
86244f0eadcSLiang Li  */
86344f0eadcSLiang Li 
86444f0eadcSLiang Li int qemu_put_qemu_file(QEMUFile *f_des, QEMUFile *f_src)
86544f0eadcSLiang Li {
86644f0eadcSLiang Li     int len = 0;
86744f0eadcSLiang Li 
86844f0eadcSLiang Li     if (f_src->buf_index > 0) {
86944f0eadcSLiang Li         len = f_src->buf_index;
87044f0eadcSLiang Li         qemu_put_buffer(f_des, f_src->buf, f_src->buf_index);
87144f0eadcSLiang Li         f_src->buf_index = 0;
872787d134fSLiang Li         f_src->iovcnt = 0;
87344f0eadcSLiang Li     }
87444f0eadcSLiang Li     return len;
87544f0eadcSLiang Li }
876b3af1bc9SDr. David Alan Gilbert 
877b3af1bc9SDr. David Alan Gilbert /*
8784024cc85SLukas Straub  * Check if the writable buffer is empty
8794024cc85SLukas Straub  */
8804024cc85SLukas Straub 
8814024cc85SLukas Straub bool qemu_file_buffer_empty(QEMUFile *file)
8824024cc85SLukas Straub {
8834024cc85SLukas Straub     assert(qemu_file_is_writable(file));
8844024cc85SLukas Straub 
8854024cc85SLukas Straub     return !file->iovcnt;
8864024cc85SLukas Straub }
8874024cc85SLukas Straub 
8884024cc85SLukas Straub /*
889b3af1bc9SDr. David Alan Gilbert  * Get a string whose length is determined by a single preceding byte
890b3af1bc9SDr. David Alan Gilbert  * A preallocated 256 byte buffer must be passed in.
891b3af1bc9SDr. David Alan Gilbert  * Returns: len on success and a 0 terminated string in the buffer
892b3af1bc9SDr. David Alan Gilbert  *          else 0
893b3af1bc9SDr. David Alan Gilbert  *          (Note a 0 length string will return 0 either way)
894b3af1bc9SDr. David Alan Gilbert  */
895394b9407SPaolo Bonzini size_t coroutine_fn qemu_get_counted_string(QEMUFile *f, char buf[256])
896b3af1bc9SDr. David Alan Gilbert {
897b3af1bc9SDr. David Alan Gilbert     size_t len = qemu_get_byte(f);
898b3af1bc9SDr. David Alan Gilbert     size_t res = qemu_get_buffer(f, (uint8_t *)buf, len);
899b3af1bc9SDr. David Alan Gilbert 
900b3af1bc9SDr. David Alan Gilbert     buf[res] = 0;
901b3af1bc9SDr. David Alan Gilbert 
902b3af1bc9SDr. David Alan Gilbert     return res == len ? res : 0;
903b3af1bc9SDr. David Alan Gilbert }
904a800cd5cSDr. David Alan Gilbert 
905a800cd5cSDr. David Alan Gilbert /*
906f0d64cb7SVladimir Sementsov-Ogievskiy  * Put a string with one preceding byte containing its length. The length of
907f0d64cb7SVladimir Sementsov-Ogievskiy  * the string should be less than 256.
908f0d64cb7SVladimir Sementsov-Ogievskiy  */
909f0d64cb7SVladimir Sementsov-Ogievskiy void qemu_put_counted_string(QEMUFile *f, const char *str)
910f0d64cb7SVladimir Sementsov-Ogievskiy {
911f0d64cb7SVladimir Sementsov-Ogievskiy     size_t len = strlen(str);
912f0d64cb7SVladimir Sementsov-Ogievskiy 
913f0d64cb7SVladimir Sementsov-Ogievskiy     assert(len < 256);
914f0d64cb7SVladimir Sementsov-Ogievskiy     qemu_put_byte(f, len);
915f0d64cb7SVladimir Sementsov-Ogievskiy     qemu_put_buffer(f, (const uint8_t *)str, len);
916f0d64cb7SVladimir Sementsov-Ogievskiy }
917f0d64cb7SVladimir Sementsov-Ogievskiy 
918f0d64cb7SVladimir Sementsov-Ogievskiy /*
919a800cd5cSDr. David Alan Gilbert  * Set the blocking state of the QEMUFile.
920a800cd5cSDr. David Alan Gilbert  * Note: On some transports the OS only keeps a single blocking state for
921a800cd5cSDr. David Alan Gilbert  *       both directions, and thus changing the blocking on the main
922a800cd5cSDr. David Alan Gilbert  *       QEMUFile can also affect the return path.
923a800cd5cSDr. David Alan Gilbert  */
924a800cd5cSDr. David Alan Gilbert void qemu_file_set_blocking(QEMUFile *f, bool block)
925a800cd5cSDr. David Alan Gilbert {
92680ad9706SDaniel P. Berrangé     qio_channel_set_blocking(f->ioc, block, NULL);
92706ad5135SDaniel P. Berrange }
928c6ad5be7SPeter Xu 
929c6ad5be7SPeter Xu /*
9302893a288SDaniel P. Berrangé  * qemu_file_get_ioc:
9312893a288SDaniel P. Berrangé  *
9322893a288SDaniel P. Berrangé  * Get the ioc object for the file, without incrementing
9332893a288SDaniel P. Berrangé  * the reference count.
9342893a288SDaniel P. Berrangé  *
9352893a288SDaniel P. Berrangé  * Returns: the ioc object
936c6ad5be7SPeter Xu  */
937c6ad5be7SPeter Xu QIOChannel *qemu_file_get_ioc(QEMUFile *file)
938c6ad5be7SPeter Xu {
9392893a288SDaniel P. Berrangé     return file->ioc;
940c6ad5be7SPeter Xu }
941c7a7db4bSAvihai Horon 
942c7a7db4bSAvihai Horon /*
943c7a7db4bSAvihai Horon  * Read size bytes from QEMUFile f and write them to fd.
944c7a7db4bSAvihai Horon  */
945c7a7db4bSAvihai Horon int qemu_file_get_to_fd(QEMUFile *f, int fd, size_t size)
946c7a7db4bSAvihai Horon {
947c7a7db4bSAvihai Horon     while (size) {
948c7a7db4bSAvihai Horon         size_t pending = f->buf_size - f->buf_index;
949c7a7db4bSAvihai Horon         ssize_t rc;
950c7a7db4bSAvihai Horon 
951c7a7db4bSAvihai Horon         if (!pending) {
952c7a7db4bSAvihai Horon             rc = qemu_fill_buffer(f);
953c7a7db4bSAvihai Horon             if (rc < 0) {
954c7a7db4bSAvihai Horon                 return rc;
955c7a7db4bSAvihai Horon             }
956c7a7db4bSAvihai Horon             if (rc == 0) {
957c7a7db4bSAvihai Horon                 return -EIO;
958c7a7db4bSAvihai Horon             }
959c7a7db4bSAvihai Horon             continue;
960c7a7db4bSAvihai Horon         }
961c7a7db4bSAvihai Horon 
962c7a7db4bSAvihai Horon         rc = write(fd, f->buf + f->buf_index, MIN(pending, size));
963c7a7db4bSAvihai Horon         if (rc < 0) {
964c7a7db4bSAvihai Horon             return -errno;
965c7a7db4bSAvihai Horon         }
966c7a7db4bSAvihai Horon         if (rc == 0) {
967c7a7db4bSAvihai Horon             return -EIO;
968c7a7db4bSAvihai Horon         }
969c7a7db4bSAvihai Horon         f->buf_index += rc;
970c7a7db4bSAvihai Horon         size -= rc;
971c7a7db4bSAvihai Horon     }
972c7a7db4bSAvihai Horon 
973c7a7db4bSAvihai Horon     return 0;
974c7a7db4bSAvihai Horon }
975