xref: /qemu/migration/qemu-file.c (revision c7fc8d32)
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"
323d661c8aSYury Kotov #include "qapi/error.h"
3360fe637bSDr. David Alan Gilbert 
34a24939f2SDaniel P. Berrange #define IO_BUF_SIZE 32768
35f9919116SEric Blake #define MAX_IOV_SIZE MIN_CONST(IOV_MAX, 64)
36a24939f2SDaniel P. Berrange 
37a24939f2SDaniel P. Berrange struct QEMUFile {
38a24939f2SDaniel P. Berrange     const QEMUFileOps *ops;
39a24939f2SDaniel P. Berrange     const QEMUFileHooks *hooks;
40a24939f2SDaniel P. Berrange     void *opaque;
41a24939f2SDaniel P. Berrange 
42c7fc8d32SDaniel P. Berrangé     /*
43c7fc8d32SDaniel P. Berrangé      * Maximum amount of data in bytes to transfer during one
44c7fc8d32SDaniel P. Berrangé      * rate limiting time window
45c7fc8d32SDaniel P. Berrangé      */
46c7fc8d32SDaniel P. Berrangé     int64_t rate_limit_max;
47c7fc8d32SDaniel P. Berrangé     /*
48c7fc8d32SDaniel P. Berrangé      * Total amount of data in bytes queued for transfer
49c7fc8d32SDaniel P. Berrangé      * during this rate limiting time window
50c7fc8d32SDaniel P. Berrangé      */
51c7fc8d32SDaniel P. Berrangé     int64_t rate_limit_used;
52a24939f2SDaniel P. Berrange 
53a24939f2SDaniel P. Berrange     int64_t pos; /* start of buffer when writing, end of buffer
54a24939f2SDaniel P. Berrange                     when reading */
55a24939f2SDaniel P. Berrange     int buf_index;
56a24939f2SDaniel P. Berrange     int buf_size; /* 0 when writing */
57a24939f2SDaniel P. Berrange     uint8_t buf[IO_BUF_SIZE];
58a24939f2SDaniel P. Berrange 
5953f09a10SPavel Butsykin     DECLARE_BITMAP(may_free, MAX_IOV_SIZE);
60a24939f2SDaniel P. Berrange     struct iovec iov[MAX_IOV_SIZE];
61a24939f2SDaniel P. Berrange     unsigned int iovcnt;
62a24939f2SDaniel P. Berrange 
63a24939f2SDaniel P. Berrange     int last_error;
643d661c8aSYury Kotov     Error *last_error_obj;
65a555b809SJuan Quintela     /* has the file has been shutdown */
66a555b809SJuan Quintela     bool shutdown;
67c6ad5be7SPeter Xu     /* Whether opaque points to a QIOChannel */
68c6ad5be7SPeter Xu     bool has_ioc;
69a24939f2SDaniel P. Berrange };
70a24939f2SDaniel P. Berrange 
71e1a8c9b6SDr. David Alan Gilbert /*
72e1a8c9b6SDr. David Alan Gilbert  * Stop a file from being read/written - not all backing files can do this
73e1a8c9b6SDr. David Alan Gilbert  * typically only sockets can.
74e1a8c9b6SDr. David Alan Gilbert  */
75e1a8c9b6SDr. David Alan Gilbert int qemu_file_shutdown(QEMUFile *f)
76e1a8c9b6SDr. David Alan Gilbert {
77a555b809SJuan Quintela     int ret;
78a555b809SJuan Quintela 
79a555b809SJuan Quintela     f->shutdown = true;
80e1a8c9b6SDr. David Alan Gilbert     if (!f->ops->shut_down) {
81e1a8c9b6SDr. David Alan Gilbert         return -ENOSYS;
82e1a8c9b6SDr. David Alan Gilbert     }
83a555b809SJuan Quintela     ret = f->ops->shut_down(f->opaque, true, true, NULL);
84a555b809SJuan Quintela 
85a555b809SJuan Quintela     if (!f->last_error) {
86a555b809SJuan Quintela         qemu_file_set_error(f, -EIO);
87a555b809SJuan Quintela     }
88a555b809SJuan Quintela     return ret;
89e1a8c9b6SDr. David Alan Gilbert }
90e1a8c9b6SDr. David Alan Gilbert 
91adc468e9SDr. David Alan Gilbert /*
92adc468e9SDr. David Alan Gilbert  * Result: QEMUFile* for a 'return path' for comms in the opposite direction
93adc468e9SDr. David Alan Gilbert  *         NULL if not available
94adc468e9SDr. David Alan Gilbert  */
95adc468e9SDr. David Alan Gilbert QEMUFile *qemu_file_get_return_path(QEMUFile *f)
96adc468e9SDr. David Alan Gilbert {
97adc468e9SDr. David Alan Gilbert     if (!f->ops->get_return_path) {
98adc468e9SDr. David Alan Gilbert         return NULL;
99adc468e9SDr. David Alan Gilbert     }
100adc468e9SDr. David Alan Gilbert     return f->ops->get_return_path(f->opaque);
101adc468e9SDr. David Alan Gilbert }
102adc468e9SDr. David Alan Gilbert 
10360fe637bSDr. David Alan Gilbert bool qemu_file_mode_is_not_valid(const char *mode)
10460fe637bSDr. David Alan Gilbert {
10560fe637bSDr. David Alan Gilbert     if (mode == NULL ||
10660fe637bSDr. David Alan Gilbert         (mode[0] != 'r' && mode[0] != 'w') ||
10760fe637bSDr. David Alan Gilbert         mode[1] != 'b' || mode[2] != 0) {
10860fe637bSDr. David Alan Gilbert         fprintf(stderr, "qemu_fopen: Argument validity check failed\n");
10960fe637bSDr. David Alan Gilbert         return true;
11060fe637bSDr. David Alan Gilbert     }
11160fe637bSDr. David Alan Gilbert 
11260fe637bSDr. David Alan Gilbert     return false;
11360fe637bSDr. David Alan Gilbert }
11460fe637bSDr. David Alan Gilbert 
115c6ad5be7SPeter Xu QEMUFile *qemu_fopen_ops(void *opaque, const QEMUFileOps *ops, bool has_ioc)
11660fe637bSDr. David Alan Gilbert {
11760fe637bSDr. David Alan Gilbert     QEMUFile *f;
11860fe637bSDr. David Alan Gilbert 
11997f3ad35SMarkus Armbruster     f = g_new0(QEMUFile, 1);
12060fe637bSDr. David Alan Gilbert 
12160fe637bSDr. David Alan Gilbert     f->opaque = opaque;
12260fe637bSDr. David Alan Gilbert     f->ops = ops;
123c6ad5be7SPeter Xu     f->has_ioc = has_ioc;
12460fe637bSDr. David Alan Gilbert     return f;
12560fe637bSDr. David Alan Gilbert }
12660fe637bSDr. David Alan Gilbert 
1270436e09fSDaniel P. Berrange 
1280436e09fSDaniel P. Berrange void qemu_file_set_hooks(QEMUFile *f, const QEMUFileHooks *hooks)
1290436e09fSDaniel P. Berrange {
1300436e09fSDaniel P. Berrange     f->hooks = hooks;
1310436e09fSDaniel P. Berrange }
1320436e09fSDaniel P. Berrange 
13360fe637bSDr. David Alan Gilbert /*
1343d661c8aSYury Kotov  * Get last error for stream f with optional Error*
1353d661c8aSYury Kotov  *
1363d661c8aSYury Kotov  * Return negative error value if there has been an error on previous
1373d661c8aSYury Kotov  * operations, return 0 if no error happened.
1383d661c8aSYury Kotov  * Optional, it returns Error* in errp, but it may be NULL even if return value
1393d661c8aSYury Kotov  * is not 0.
1403d661c8aSYury Kotov  *
1413d661c8aSYury Kotov  */
1423d661c8aSYury Kotov int qemu_file_get_error_obj(QEMUFile *f, Error **errp)
1433d661c8aSYury Kotov {
1443d661c8aSYury Kotov     if (errp) {
1453d661c8aSYury Kotov         *errp = f->last_error_obj ? error_copy(f->last_error_obj) : NULL;
1463d661c8aSYury Kotov     }
1473d661c8aSYury Kotov     return f->last_error;
1483d661c8aSYury Kotov }
1493d661c8aSYury Kotov 
1503d661c8aSYury Kotov /*
1513d661c8aSYury Kotov  * Set the last error for stream f with optional Error*
1523d661c8aSYury Kotov  */
1533d661c8aSYury Kotov void qemu_file_set_error_obj(QEMUFile *f, int ret, Error *err)
1543d661c8aSYury Kotov {
1553d661c8aSYury Kotov     if (f->last_error == 0 && ret) {
1563d661c8aSYury Kotov         f->last_error = ret;
1573d661c8aSYury Kotov         error_propagate(&f->last_error_obj, err);
1583d661c8aSYury Kotov     } else if (err) {
1593d661c8aSYury Kotov         error_report_err(err);
1603d661c8aSYury Kotov     }
1613d661c8aSYury Kotov }
1623d661c8aSYury Kotov 
1633d661c8aSYury Kotov /*
16460fe637bSDr. David Alan Gilbert  * Get last error for stream f
16560fe637bSDr. David Alan Gilbert  *
16660fe637bSDr. David Alan Gilbert  * Return negative error value if there has been an error on previous
16760fe637bSDr. David Alan Gilbert  * operations, return 0 if no error happened.
16860fe637bSDr. David Alan Gilbert  *
16960fe637bSDr. David Alan Gilbert  */
17060fe637bSDr. David Alan Gilbert int qemu_file_get_error(QEMUFile *f)
17160fe637bSDr. David Alan Gilbert {
1723d661c8aSYury Kotov     return qemu_file_get_error_obj(f, NULL);
17360fe637bSDr. David Alan Gilbert }
17460fe637bSDr. David Alan Gilbert 
1753d661c8aSYury Kotov /*
1763d661c8aSYury Kotov  * Set the last error for stream f
1773d661c8aSYury Kotov  */
17860fe637bSDr. David Alan Gilbert void qemu_file_set_error(QEMUFile *f, int ret)
17960fe637bSDr. David Alan Gilbert {
1803d661c8aSYury Kotov     qemu_file_set_error_obj(f, ret, NULL);
18160fe637bSDr. David Alan Gilbert }
18260fe637bSDr. David Alan Gilbert 
18360fe637bSDr. David Alan Gilbert bool qemu_file_is_writable(QEMUFile *f)
18460fe637bSDr. David Alan Gilbert {
18511808bb0SDaniel P. Berrange     return f->ops->writev_buffer;
18660fe637bSDr. David Alan Gilbert }
18760fe637bSDr. David Alan Gilbert 
18853f09a10SPavel Butsykin static void qemu_iovec_release_ram(QEMUFile *f)
18953f09a10SPavel Butsykin {
19053f09a10SPavel Butsykin     struct iovec iov;
19153f09a10SPavel Butsykin     unsigned long idx;
19253f09a10SPavel Butsykin 
19353f09a10SPavel Butsykin     /* Find and release all the contiguous memory ranges marked as may_free. */
19453f09a10SPavel Butsykin     idx = find_next_bit(f->may_free, f->iovcnt, 0);
19553f09a10SPavel Butsykin     if (idx >= f->iovcnt) {
19653f09a10SPavel Butsykin         return;
19753f09a10SPavel Butsykin     }
19853f09a10SPavel Butsykin     iov = f->iov[idx];
19953f09a10SPavel Butsykin 
20053f09a10SPavel Butsykin     /* The madvise() in the loop is called for iov within a continuous range and
20153f09a10SPavel Butsykin      * then reinitialize the iov. And in the end, madvise() is called for the
20253f09a10SPavel Butsykin      * last iov.
20353f09a10SPavel Butsykin      */
20453f09a10SPavel Butsykin     while ((idx = find_next_bit(f->may_free, f->iovcnt, idx + 1)) < f->iovcnt) {
20553f09a10SPavel Butsykin         /* check for adjacent buffer and coalesce them */
20653f09a10SPavel Butsykin         if (iov.iov_base + iov.iov_len == f->iov[idx].iov_base) {
20753f09a10SPavel Butsykin             iov.iov_len += f->iov[idx].iov_len;
20853f09a10SPavel Butsykin             continue;
20953f09a10SPavel Butsykin         }
21053f09a10SPavel Butsykin         if (qemu_madvise(iov.iov_base, iov.iov_len, QEMU_MADV_DONTNEED) < 0) {
21153f09a10SPavel Butsykin             error_report("migrate: madvise DONTNEED failed %p %zd: %s",
21253f09a10SPavel Butsykin                          iov.iov_base, iov.iov_len, strerror(errno));
21353f09a10SPavel Butsykin         }
21453f09a10SPavel Butsykin         iov = f->iov[idx];
21553f09a10SPavel Butsykin     }
21653f09a10SPavel Butsykin     if (qemu_madvise(iov.iov_base, iov.iov_len, QEMU_MADV_DONTNEED) < 0) {
21753f09a10SPavel Butsykin             error_report("migrate: madvise DONTNEED failed %p %zd: %s",
21853f09a10SPavel Butsykin                          iov.iov_base, iov.iov_len, strerror(errno));
21953f09a10SPavel Butsykin     }
22053f09a10SPavel Butsykin     memset(f->may_free, 0, sizeof(f->may_free));
22153f09a10SPavel Butsykin }
22253f09a10SPavel Butsykin 
22360fe637bSDr. David Alan Gilbert /**
22460fe637bSDr. David Alan Gilbert  * Flushes QEMUFile buffer
22560fe637bSDr. David Alan Gilbert  *
2263b348706SDr. David Alan Gilbert  * This will flush all pending data. If data was only partially flushed, it
2273b348706SDr. David Alan Gilbert  * will set an error state.
22860fe637bSDr. David Alan Gilbert  */
22960fe637bSDr. David Alan Gilbert void qemu_fflush(QEMUFile *f)
23060fe637bSDr. David Alan Gilbert {
23160fe637bSDr. David Alan Gilbert     ssize_t ret = 0;
232baf51e77SDaniel P. Berrange     ssize_t expect = 0;
2333d661c8aSYury Kotov     Error *local_error = NULL;
23460fe637bSDr. David Alan Gilbert 
23560fe637bSDr. David Alan Gilbert     if (!qemu_file_is_writable(f)) {
23660fe637bSDr. David Alan Gilbert         return;
23760fe637bSDr. David Alan Gilbert     }
23860fe637bSDr. David Alan Gilbert 
239a555b809SJuan Quintela     if (f->shutdown) {
240a555b809SJuan Quintela         return;
241a555b809SJuan Quintela     }
24260fe637bSDr. David Alan Gilbert     if (f->iovcnt > 0) {
243baf51e77SDaniel P. Berrange         expect = iov_size(f->iov, f->iovcnt);
2443d661c8aSYury Kotov         ret = f->ops->writev_buffer(f->opaque, f->iov, f->iovcnt, f->pos,
2453d661c8aSYury Kotov                                     &local_error);
24653f09a10SPavel Butsykin 
24753f09a10SPavel Butsykin         qemu_iovec_release_ram(f);
24860fe637bSDr. David Alan Gilbert     }
249baf51e77SDaniel P. Berrange 
25060fe637bSDr. David Alan Gilbert     if (ret >= 0) {
25160fe637bSDr. David Alan Gilbert         f->pos += ret;
25260fe637bSDr. David Alan Gilbert     }
253baf51e77SDaniel P. Berrange     /* We expect the QEMUFile write impl to send the full
254baf51e77SDaniel P. Berrange      * data set we requested, so sanity check that.
255baf51e77SDaniel P. Berrange      */
256baf51e77SDaniel P. Berrange     if (ret != expect) {
2573d661c8aSYury Kotov         qemu_file_set_error_obj(f, ret < 0 ? ret : -EIO, local_error);
258baf51e77SDaniel P. Berrange     }
25960fe637bSDr. David Alan Gilbert     f->buf_index = 0;
26060fe637bSDr. David Alan Gilbert     f->iovcnt = 0;
26160fe637bSDr. David Alan Gilbert }
26260fe637bSDr. David Alan Gilbert 
26360fe637bSDr. David Alan Gilbert void ram_control_before_iterate(QEMUFile *f, uint64_t flags)
26460fe637bSDr. David Alan Gilbert {
26560fe637bSDr. David Alan Gilbert     int ret = 0;
26660fe637bSDr. David Alan Gilbert 
2670436e09fSDaniel P. Berrange     if (f->hooks && f->hooks->before_ram_iterate) {
2680436e09fSDaniel P. Berrange         ret = f->hooks->before_ram_iterate(f, f->opaque, flags, NULL);
26960fe637bSDr. David Alan Gilbert         if (ret < 0) {
27060fe637bSDr. David Alan Gilbert             qemu_file_set_error(f, ret);
27160fe637bSDr. David Alan Gilbert         }
27260fe637bSDr. David Alan Gilbert     }
27360fe637bSDr. David Alan Gilbert }
27460fe637bSDr. David Alan Gilbert 
27560fe637bSDr. David Alan Gilbert void ram_control_after_iterate(QEMUFile *f, uint64_t flags)
27660fe637bSDr. David Alan Gilbert {
27760fe637bSDr. David Alan Gilbert     int ret = 0;
27860fe637bSDr. David Alan Gilbert 
2790436e09fSDaniel P. Berrange     if (f->hooks && f->hooks->after_ram_iterate) {
2800436e09fSDaniel P. Berrange         ret = f->hooks->after_ram_iterate(f, f->opaque, flags, NULL);
28160fe637bSDr. David Alan Gilbert         if (ret < 0) {
28260fe637bSDr. David Alan Gilbert             qemu_file_set_error(f, ret);
28360fe637bSDr. David Alan Gilbert         }
28460fe637bSDr. David Alan Gilbert     }
28560fe637bSDr. David Alan Gilbert }
28660fe637bSDr. David Alan Gilbert 
287632e3a5cSDr. David Alan Gilbert void ram_control_load_hook(QEMUFile *f, uint64_t flags, void *data)
28860fe637bSDr. David Alan Gilbert {
28960fe637bSDr. David Alan Gilbert     int ret = -EINVAL;
29060fe637bSDr. David Alan Gilbert 
2910436e09fSDaniel P. Berrange     if (f->hooks && f->hooks->hook_ram_load) {
2920436e09fSDaniel P. Berrange         ret = f->hooks->hook_ram_load(f, f->opaque, flags, data);
29360fe637bSDr. David Alan Gilbert         if (ret < 0) {
29460fe637bSDr. David Alan Gilbert             qemu_file_set_error(f, ret);
29560fe637bSDr. David Alan Gilbert         }
29660fe637bSDr. David Alan Gilbert     } else {
297632e3a5cSDr. David Alan Gilbert         /*
298632e3a5cSDr. David Alan Gilbert          * Hook is a hook specifically requested by the source sending a flag
299632e3a5cSDr. David Alan Gilbert          * that expects there to be a hook on the destination.
300632e3a5cSDr. David Alan Gilbert          */
301632e3a5cSDr. David Alan Gilbert         if (flags == RAM_CONTROL_HOOK) {
30260fe637bSDr. David Alan Gilbert             qemu_file_set_error(f, ret);
30360fe637bSDr. David Alan Gilbert         }
30460fe637bSDr. David Alan Gilbert     }
305632e3a5cSDr. David Alan Gilbert }
30660fe637bSDr. David Alan Gilbert 
30760fe637bSDr. David Alan Gilbert size_t ram_control_save_page(QEMUFile *f, ram_addr_t block_offset,
3086e1dea46SJuan Quintela                              ram_addr_t offset, size_t size,
3096e1dea46SJuan Quintela                              uint64_t *bytes_sent)
31060fe637bSDr. David Alan Gilbert {
3110436e09fSDaniel P. Berrange     if (f->hooks && f->hooks->save_page) {
3120436e09fSDaniel P. Berrange         int ret = f->hooks->save_page(f, f->opaque, block_offset,
31360fe637bSDr. David Alan Gilbert                                       offset, size, bytes_sent);
314ccb7e1b5SLidong Chen         if (ret != RAM_SAVE_CONTROL_NOT_SUPP) {
315c7fc8d32SDaniel P. Berrangé             f->rate_limit_used += size;
316ccb7e1b5SLidong Chen         }
317ccb7e1b5SLidong Chen 
318ccb7e1b5SLidong Chen         if (ret != RAM_SAVE_CONTROL_DELAYED &&
319ccb7e1b5SLidong Chen             ret != RAM_SAVE_CONTROL_NOT_SUPP) {
32060fe637bSDr. David Alan Gilbert             if (bytes_sent && *bytes_sent > 0) {
32160fe637bSDr. David Alan Gilbert                 qemu_update_position(f, *bytes_sent);
32260fe637bSDr. David Alan Gilbert             } else if (ret < 0) {
32360fe637bSDr. David Alan Gilbert                 qemu_file_set_error(f, ret);
32460fe637bSDr. David Alan Gilbert             }
32560fe637bSDr. David Alan Gilbert         }
32660fe637bSDr. David Alan Gilbert 
32760fe637bSDr. David Alan Gilbert         return ret;
32860fe637bSDr. David Alan Gilbert     }
32960fe637bSDr. David Alan Gilbert 
33060fe637bSDr. David Alan Gilbert     return RAM_SAVE_CONTROL_NOT_SUPP;
33160fe637bSDr. David Alan Gilbert }
33260fe637bSDr. David Alan Gilbert 
33360fe637bSDr. David Alan Gilbert /*
33460fe637bSDr. David Alan Gilbert  * Attempt to fill the buffer from the underlying file
33560fe637bSDr. David Alan Gilbert  * Returns the number of bytes read, or negative value for an error.
33660fe637bSDr. David Alan Gilbert  *
33760fe637bSDr. David Alan Gilbert  * Note that it can return a partially full buffer even in a not error/not EOF
33860fe637bSDr. David Alan Gilbert  * case if the underlying file descriptor gives a short read, and that can
33960fe637bSDr. David Alan Gilbert  * happen even on a blocking fd.
34060fe637bSDr. David Alan Gilbert  */
34160fe637bSDr. David Alan Gilbert static ssize_t qemu_fill_buffer(QEMUFile *f)
34260fe637bSDr. David Alan Gilbert {
34360fe637bSDr. David Alan Gilbert     int len;
34460fe637bSDr. David Alan Gilbert     int pending;
3453d661c8aSYury Kotov     Error *local_error = NULL;
34660fe637bSDr. David Alan Gilbert 
34760fe637bSDr. David Alan Gilbert     assert(!qemu_file_is_writable(f));
34860fe637bSDr. David Alan Gilbert 
34960fe637bSDr. David Alan Gilbert     pending = f->buf_size - f->buf_index;
35060fe637bSDr. David Alan Gilbert     if (pending > 0) {
35160fe637bSDr. David Alan Gilbert         memmove(f->buf, f->buf + f->buf_index, pending);
35260fe637bSDr. David Alan Gilbert     }
35360fe637bSDr. David Alan Gilbert     f->buf_index = 0;
35460fe637bSDr. David Alan Gilbert     f->buf_size = pending;
35560fe637bSDr. David Alan Gilbert 
356a555b809SJuan Quintela     if (f->shutdown) {
357a555b809SJuan Quintela         return 0;
358a555b809SJuan Quintela     }
359a555b809SJuan Quintela 
36060fe637bSDr. David Alan Gilbert     len = f->ops->get_buffer(f->opaque, f->buf + pending, f->pos,
3613d661c8aSYury Kotov                              IO_BUF_SIZE - pending, &local_error);
36260fe637bSDr. David Alan Gilbert     if (len > 0) {
36360fe637bSDr. David Alan Gilbert         f->buf_size += len;
36460fe637bSDr. David Alan Gilbert         f->pos += len;
36560fe637bSDr. David Alan Gilbert     } else if (len == 0) {
3663d661c8aSYury Kotov         qemu_file_set_error_obj(f, -EIO, local_error);
36760fe637bSDr. David Alan Gilbert     } else if (len != -EAGAIN) {
3683d661c8aSYury Kotov         qemu_file_set_error_obj(f, len, local_error);
3693d661c8aSYury Kotov     } else {
3703d661c8aSYury Kotov         error_free(local_error);
37160fe637bSDr. David Alan Gilbert     }
37260fe637bSDr. David Alan Gilbert 
37360fe637bSDr. David Alan Gilbert     return len;
37460fe637bSDr. David Alan Gilbert }
37560fe637bSDr. David Alan Gilbert 
37660fe637bSDr. David Alan Gilbert void qemu_update_position(QEMUFile *f, size_t size)
37760fe637bSDr. David Alan Gilbert {
37860fe637bSDr. David Alan Gilbert     f->pos += size;
37960fe637bSDr. David Alan Gilbert }
38060fe637bSDr. David Alan Gilbert 
38160fe637bSDr. David Alan Gilbert /** Closes the file
38260fe637bSDr. David Alan Gilbert  *
38360fe637bSDr. David Alan Gilbert  * Returns negative error value if any error happened on previous operations or
38460fe637bSDr. David Alan Gilbert  * while closing the file. Returns 0 or positive number on success.
38560fe637bSDr. David Alan Gilbert  *
38660fe637bSDr. David Alan Gilbert  * The meaning of return value on success depends on the specific backend
38760fe637bSDr. David Alan Gilbert  * being used.
38860fe637bSDr. David Alan Gilbert  */
38960fe637bSDr. David Alan Gilbert int qemu_fclose(QEMUFile *f)
39060fe637bSDr. David Alan Gilbert {
39160fe637bSDr. David Alan Gilbert     int ret;
39260fe637bSDr. David Alan Gilbert     qemu_fflush(f);
39360fe637bSDr. David Alan Gilbert     ret = qemu_file_get_error(f);
39460fe637bSDr. David Alan Gilbert 
39560fe637bSDr. David Alan Gilbert     if (f->ops->close) {
3963d661c8aSYury Kotov         int ret2 = f->ops->close(f->opaque, NULL);
39760fe637bSDr. David Alan Gilbert         if (ret >= 0) {
39860fe637bSDr. David Alan Gilbert             ret = ret2;
39960fe637bSDr. David Alan Gilbert         }
40060fe637bSDr. David Alan Gilbert     }
40160fe637bSDr. David Alan Gilbert     /* If any error was spotted before closing, we should report it
40260fe637bSDr. David Alan Gilbert      * instead of the close() return value.
40360fe637bSDr. David Alan Gilbert      */
40460fe637bSDr. David Alan Gilbert     if (f->last_error) {
40560fe637bSDr. David Alan Gilbert         ret = f->last_error;
40660fe637bSDr. David Alan Gilbert     }
4073d661c8aSYury Kotov     error_free(f->last_error_obj);
40860fe637bSDr. David Alan Gilbert     g_free(f);
40960fe637bSDr. David Alan Gilbert     trace_qemu_file_fclose();
41060fe637bSDr. David Alan Gilbert     return ret;
41160fe637bSDr. David Alan Gilbert }
41260fe637bSDr. David Alan Gilbert 
4131bf57fb3SWei Yang /*
4141bf57fb3SWei Yang  * Add buf to iovec. Do flush if iovec is full.
4151bf57fb3SWei Yang  *
4161bf57fb3SWei Yang  * Return values:
4171bf57fb3SWei Yang  * 1 iovec is full and flushed
4181bf57fb3SWei Yang  * 0 iovec is not flushed
4191bf57fb3SWei Yang  *
4201bf57fb3SWei Yang  */
4211bf57fb3SWei Yang static int add_to_iovec(QEMUFile *f, const uint8_t *buf, size_t size,
42253f09a10SPavel Butsykin                         bool may_free)
42360fe637bSDr. David Alan Gilbert {
42460fe637bSDr. David Alan Gilbert     /* check for adjacent buffer and coalesce them */
42560fe637bSDr. David Alan Gilbert     if (f->iovcnt > 0 && buf == f->iov[f->iovcnt - 1].iov_base +
42653f09a10SPavel Butsykin         f->iov[f->iovcnt - 1].iov_len &&
42753f09a10SPavel Butsykin         may_free == test_bit(f->iovcnt - 1, f->may_free))
42853f09a10SPavel Butsykin     {
42960fe637bSDr. David Alan Gilbert         f->iov[f->iovcnt - 1].iov_len += size;
43060fe637bSDr. David Alan Gilbert     } else {
431c00d434aSFeng Lin         if (f->iovcnt >= MAX_IOV_SIZE) {
432c00d434aSFeng Lin             /* Should only happen if a previous fflush failed */
433c00d434aSFeng Lin             assert(f->shutdown || !qemu_file_is_writable(f));
434c00d434aSFeng Lin             return 1;
435c00d434aSFeng Lin         }
43653f09a10SPavel Butsykin         if (may_free) {
43753f09a10SPavel Butsykin             set_bit(f->iovcnt, f->may_free);
43853f09a10SPavel Butsykin         }
43960fe637bSDr. David Alan Gilbert         f->iov[f->iovcnt].iov_base = (uint8_t *)buf;
44060fe637bSDr. David Alan Gilbert         f->iov[f->iovcnt++].iov_len = size;
44160fe637bSDr. David Alan Gilbert     }
44260fe637bSDr. David Alan Gilbert 
44360fe637bSDr. David Alan Gilbert     if (f->iovcnt >= MAX_IOV_SIZE) {
44460fe637bSDr. David Alan Gilbert         qemu_fflush(f);
4451bf57fb3SWei Yang         return 1;
4461bf57fb3SWei Yang     }
4471bf57fb3SWei Yang 
4481bf57fb3SWei Yang     return 0;
4491bf57fb3SWei Yang }
4501bf57fb3SWei Yang 
4511bf57fb3SWei Yang static void add_buf_to_iovec(QEMUFile *f, size_t len)
4521bf57fb3SWei Yang {
4531bf57fb3SWei Yang     if (!add_to_iovec(f, f->buf + f->buf_index, len, false)) {
4541bf57fb3SWei Yang         f->buf_index += len;
4551bf57fb3SWei Yang         if (f->buf_index == IO_BUF_SIZE) {
4561bf57fb3SWei Yang             qemu_fflush(f);
4571bf57fb3SWei Yang         }
45860fe637bSDr. David Alan Gilbert     }
45960fe637bSDr. David Alan Gilbert }
46060fe637bSDr. David Alan Gilbert 
46153f09a10SPavel Butsykin void qemu_put_buffer_async(QEMUFile *f, const uint8_t *buf, size_t size,
46253f09a10SPavel Butsykin                            bool may_free)
46360fe637bSDr. David Alan Gilbert {
46460fe637bSDr. David Alan Gilbert     if (f->last_error) {
46560fe637bSDr. David Alan Gilbert         return;
46660fe637bSDr. David Alan Gilbert     }
46760fe637bSDr. David Alan Gilbert 
468c7fc8d32SDaniel P. Berrangé     f->rate_limit_used += size;
46953f09a10SPavel Butsykin     add_to_iovec(f, buf, size, may_free);
47060fe637bSDr. David Alan Gilbert }
47160fe637bSDr. David Alan Gilbert 
47256f3835fSDr. David Alan Gilbert void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, size_t size)
47360fe637bSDr. David Alan Gilbert {
47456f3835fSDr. David Alan Gilbert     size_t l;
47560fe637bSDr. David Alan Gilbert 
47660fe637bSDr. David Alan Gilbert     if (f->last_error) {
47760fe637bSDr. David Alan Gilbert         return;
47860fe637bSDr. David Alan Gilbert     }
47960fe637bSDr. David Alan Gilbert 
48060fe637bSDr. David Alan Gilbert     while (size > 0) {
48160fe637bSDr. David Alan Gilbert         l = IO_BUF_SIZE - f->buf_index;
48260fe637bSDr. David Alan Gilbert         if (l > size) {
48360fe637bSDr. David Alan Gilbert             l = size;
48460fe637bSDr. David Alan Gilbert         }
48560fe637bSDr. David Alan Gilbert         memcpy(f->buf + f->buf_index, buf, l);
486c7fc8d32SDaniel P. Berrangé         f->rate_limit_used += l;
4871bf57fb3SWei Yang         add_buf_to_iovec(f, l);
48860fe637bSDr. David Alan Gilbert         if (qemu_file_get_error(f)) {
48960fe637bSDr. David Alan Gilbert             break;
49060fe637bSDr. David Alan Gilbert         }
49160fe637bSDr. David Alan Gilbert         buf += l;
49260fe637bSDr. David Alan Gilbert         size -= l;
49360fe637bSDr. David Alan Gilbert     }
49460fe637bSDr. David Alan Gilbert }
49560fe637bSDr. David Alan Gilbert 
49660fe637bSDr. David Alan Gilbert void qemu_put_byte(QEMUFile *f, int v)
49760fe637bSDr. David Alan Gilbert {
49860fe637bSDr. David Alan Gilbert     if (f->last_error) {
49960fe637bSDr. David Alan Gilbert         return;
50060fe637bSDr. David Alan Gilbert     }
50160fe637bSDr. David Alan Gilbert 
50260fe637bSDr. David Alan Gilbert     f->buf[f->buf_index] = v;
503c7fc8d32SDaniel P. Berrangé     f->rate_limit_used++;
5041bf57fb3SWei Yang     add_buf_to_iovec(f, 1);
50560fe637bSDr. David Alan Gilbert }
50660fe637bSDr. David Alan Gilbert 
50760fe637bSDr. David Alan Gilbert void qemu_file_skip(QEMUFile *f, int size)
50860fe637bSDr. David Alan Gilbert {
50960fe637bSDr. David Alan Gilbert     if (f->buf_index + size <= f->buf_size) {
51060fe637bSDr. David Alan Gilbert         f->buf_index += size;
51160fe637bSDr. David Alan Gilbert     }
51260fe637bSDr. David Alan Gilbert }
51360fe637bSDr. David Alan Gilbert 
51460fe637bSDr. David Alan Gilbert /*
5157c1e52baSDr. David Alan Gilbert  * Read 'size' bytes from file (at 'offset') without moving the
5167c1e52baSDr. David Alan Gilbert  * pointer and set 'buf' to point to that data.
51760fe637bSDr. David Alan Gilbert  *
51860fe637bSDr. David Alan Gilbert  * It will return size bytes unless there was an error, in which case it will
51960fe637bSDr. David Alan Gilbert  * return as many as it managed to read (assuming blocking fd's which
52060fe637bSDr. David Alan Gilbert  * all current QEMUFile are)
52160fe637bSDr. David Alan Gilbert  */
52256f3835fSDr. David Alan Gilbert size_t qemu_peek_buffer(QEMUFile *f, uint8_t **buf, size_t size, size_t offset)
52360fe637bSDr. David Alan Gilbert {
52456f3835fSDr. David Alan Gilbert     ssize_t pending;
52556f3835fSDr. David Alan Gilbert     size_t index;
52660fe637bSDr. David Alan Gilbert 
52760fe637bSDr. David Alan Gilbert     assert(!qemu_file_is_writable(f));
52860fe637bSDr. David Alan Gilbert     assert(offset < IO_BUF_SIZE);
52960fe637bSDr. David Alan Gilbert     assert(size <= IO_BUF_SIZE - offset);
53060fe637bSDr. David Alan Gilbert 
53160fe637bSDr. David Alan Gilbert     /* The 1st byte to read from */
53260fe637bSDr. David Alan Gilbert     index = f->buf_index + offset;
53360fe637bSDr. David Alan Gilbert     /* The number of available bytes starting at index */
53460fe637bSDr. David Alan Gilbert     pending = f->buf_size - index;
53560fe637bSDr. David Alan Gilbert 
53660fe637bSDr. David Alan Gilbert     /*
53760fe637bSDr. David Alan Gilbert      * qemu_fill_buffer might return just a few bytes, even when there isn't
53860fe637bSDr. David Alan Gilbert      * an error, so loop collecting them until we get enough.
53960fe637bSDr. David Alan Gilbert      */
54060fe637bSDr. David Alan Gilbert     while (pending < size) {
54160fe637bSDr. David Alan Gilbert         int received = qemu_fill_buffer(f);
54260fe637bSDr. David Alan Gilbert 
54360fe637bSDr. David Alan Gilbert         if (received <= 0) {
54460fe637bSDr. David Alan Gilbert             break;
54560fe637bSDr. David Alan Gilbert         }
54660fe637bSDr. David Alan Gilbert 
54760fe637bSDr. David Alan Gilbert         index = f->buf_index + offset;
54860fe637bSDr. David Alan Gilbert         pending = f->buf_size - index;
54960fe637bSDr. David Alan Gilbert     }
55060fe637bSDr. David Alan Gilbert 
55160fe637bSDr. David Alan Gilbert     if (pending <= 0) {
55260fe637bSDr. David Alan Gilbert         return 0;
55360fe637bSDr. David Alan Gilbert     }
55460fe637bSDr. David Alan Gilbert     if (size > pending) {
55560fe637bSDr. David Alan Gilbert         size = pending;
55660fe637bSDr. David Alan Gilbert     }
55760fe637bSDr. David Alan Gilbert 
5587c1e52baSDr. David Alan Gilbert     *buf = f->buf + index;
55960fe637bSDr. David Alan Gilbert     return size;
56060fe637bSDr. David Alan Gilbert }
56160fe637bSDr. David Alan Gilbert 
56260fe637bSDr. David Alan Gilbert /*
56360fe637bSDr. David Alan Gilbert  * Read 'size' bytes of data from the file into buf.
56460fe637bSDr. David Alan Gilbert  * 'size' can be larger than the internal buffer.
56560fe637bSDr. David Alan Gilbert  *
56660fe637bSDr. David Alan Gilbert  * It will return size bytes unless there was an error, in which case it will
56760fe637bSDr. David Alan Gilbert  * return as many as it managed to read (assuming blocking fd's which
56860fe637bSDr. David Alan Gilbert  * all current QEMUFile are)
56960fe637bSDr. David Alan Gilbert  */
57056f3835fSDr. David Alan Gilbert size_t qemu_get_buffer(QEMUFile *f, uint8_t *buf, size_t size)
57160fe637bSDr. David Alan Gilbert {
57256f3835fSDr. David Alan Gilbert     size_t pending = size;
57356f3835fSDr. David Alan Gilbert     size_t done = 0;
57460fe637bSDr. David Alan Gilbert 
57560fe637bSDr. David Alan Gilbert     while (pending > 0) {
57656f3835fSDr. David Alan Gilbert         size_t res;
5777c1e52baSDr. David Alan Gilbert         uint8_t *src;
57860fe637bSDr. David Alan Gilbert 
5797c1e52baSDr. David Alan Gilbert         res = qemu_peek_buffer(f, &src, MIN(pending, IO_BUF_SIZE), 0);
58060fe637bSDr. David Alan Gilbert         if (res == 0) {
58160fe637bSDr. David Alan Gilbert             return done;
58260fe637bSDr. David Alan Gilbert         }
5837c1e52baSDr. David Alan Gilbert         memcpy(buf, src, res);
58460fe637bSDr. David Alan Gilbert         qemu_file_skip(f, res);
58560fe637bSDr. David Alan Gilbert         buf += res;
58660fe637bSDr. David Alan Gilbert         pending -= res;
58760fe637bSDr. David Alan Gilbert         done += res;
58860fe637bSDr. David Alan Gilbert     }
58960fe637bSDr. David Alan Gilbert     return done;
59060fe637bSDr. David Alan Gilbert }
59160fe637bSDr. David Alan Gilbert 
59260fe637bSDr. David Alan Gilbert /*
5939504fb51SDr. David Alan Gilbert  * Read 'size' bytes of data from the file.
5949504fb51SDr. David Alan Gilbert  * 'size' can be larger than the internal buffer.
5959504fb51SDr. David Alan Gilbert  *
5969504fb51SDr. David Alan Gilbert  * The data:
5979504fb51SDr. David Alan Gilbert  *   may be held on an internal buffer (in which case *buf is updated
5989504fb51SDr. David Alan Gilbert  *     to point to it) that is valid until the next qemu_file operation.
5999504fb51SDr. David Alan Gilbert  * OR
6009504fb51SDr. David Alan Gilbert  *   will be copied to the *buf that was passed in.
6019504fb51SDr. David Alan Gilbert  *
6029504fb51SDr. David Alan Gilbert  * The code tries to avoid the copy if possible.
6039504fb51SDr. David Alan Gilbert  *
6049504fb51SDr. David Alan Gilbert  * It will return size bytes unless there was an error, in which case it will
6059504fb51SDr. David Alan Gilbert  * return as many as it managed to read (assuming blocking fd's which
6069504fb51SDr. David Alan Gilbert  * all current QEMUFile are)
6079504fb51SDr. David Alan Gilbert  *
6089504fb51SDr. David Alan Gilbert  * Note: Since **buf may get changed, the caller should take care to
6099504fb51SDr. David Alan Gilbert  *       keep a pointer to the original buffer if it needs to deallocate it.
6109504fb51SDr. David Alan Gilbert  */
6119504fb51SDr. David Alan Gilbert size_t qemu_get_buffer_in_place(QEMUFile *f, uint8_t **buf, size_t size)
6129504fb51SDr. David Alan Gilbert {
6139504fb51SDr. David Alan Gilbert     if (size < IO_BUF_SIZE) {
6149504fb51SDr. David Alan Gilbert         size_t res;
6151dfafcbdSWainer dos Santos Moschetta         uint8_t *src = NULL;
6169504fb51SDr. David Alan Gilbert 
6179504fb51SDr. David Alan Gilbert         res = qemu_peek_buffer(f, &src, size, 0);
6189504fb51SDr. David Alan Gilbert 
6199504fb51SDr. David Alan Gilbert         if (res == size) {
6209504fb51SDr. David Alan Gilbert             qemu_file_skip(f, res);
6219504fb51SDr. David Alan Gilbert             *buf = src;
6229504fb51SDr. David Alan Gilbert             return res;
6239504fb51SDr. David Alan Gilbert         }
6249504fb51SDr. David Alan Gilbert     }
6259504fb51SDr. David Alan Gilbert 
6269504fb51SDr. David Alan Gilbert     return qemu_get_buffer(f, *buf, size);
6279504fb51SDr. David Alan Gilbert }
6289504fb51SDr. David Alan Gilbert 
6299504fb51SDr. David Alan Gilbert /*
63060fe637bSDr. David Alan Gilbert  * Peeks a single byte from the buffer; this isn't guaranteed to work if
63160fe637bSDr. David Alan Gilbert  * offset leaves a gap after the previous read/peeked data.
63260fe637bSDr. David Alan Gilbert  */
63360fe637bSDr. David Alan Gilbert int qemu_peek_byte(QEMUFile *f, int offset)
63460fe637bSDr. David Alan Gilbert {
63560fe637bSDr. David Alan Gilbert     int index = f->buf_index + offset;
63660fe637bSDr. David Alan Gilbert 
63760fe637bSDr. David Alan Gilbert     assert(!qemu_file_is_writable(f));
63860fe637bSDr. David Alan Gilbert     assert(offset < IO_BUF_SIZE);
63960fe637bSDr. David Alan Gilbert 
64060fe637bSDr. David Alan Gilbert     if (index >= f->buf_size) {
64160fe637bSDr. David Alan Gilbert         qemu_fill_buffer(f);
64260fe637bSDr. David Alan Gilbert         index = f->buf_index + offset;
64360fe637bSDr. David Alan Gilbert         if (index >= f->buf_size) {
64460fe637bSDr. David Alan Gilbert             return 0;
64560fe637bSDr. David Alan Gilbert         }
64660fe637bSDr. David Alan Gilbert     }
64760fe637bSDr. David Alan Gilbert     return f->buf[index];
64860fe637bSDr. David Alan Gilbert }
64960fe637bSDr. David Alan Gilbert 
65060fe637bSDr. David Alan Gilbert int qemu_get_byte(QEMUFile *f)
65160fe637bSDr. David Alan Gilbert {
65260fe637bSDr. David Alan Gilbert     int result;
65360fe637bSDr. David Alan Gilbert 
65460fe637bSDr. David Alan Gilbert     result = qemu_peek_byte(f, 0);
65560fe637bSDr. David Alan Gilbert     qemu_file_skip(f, 1);
65660fe637bSDr. David Alan Gilbert     return result;
65760fe637bSDr. David Alan Gilbert }
65860fe637bSDr. David Alan Gilbert 
65997221400SAlexander Graf int64_t qemu_ftell_fast(QEMUFile *f)
66097221400SAlexander Graf {
66197221400SAlexander Graf     int64_t ret = f->pos;
66297221400SAlexander Graf     int i;
66397221400SAlexander Graf 
66497221400SAlexander Graf     for (i = 0; i < f->iovcnt; i++) {
66597221400SAlexander Graf         ret += f->iov[i].iov_len;
66697221400SAlexander Graf     }
66797221400SAlexander Graf 
66897221400SAlexander Graf     return ret;
66997221400SAlexander Graf }
67097221400SAlexander Graf 
67160fe637bSDr. David Alan Gilbert int64_t qemu_ftell(QEMUFile *f)
67260fe637bSDr. David Alan Gilbert {
67360fe637bSDr. David Alan Gilbert     qemu_fflush(f);
67460fe637bSDr. David Alan Gilbert     return f->pos;
67560fe637bSDr. David Alan Gilbert }
67660fe637bSDr. David Alan Gilbert 
67760fe637bSDr. David Alan Gilbert int qemu_file_rate_limit(QEMUFile *f)
67860fe637bSDr. David Alan Gilbert {
679a555b809SJuan Quintela     if (f->shutdown) {
680a555b809SJuan Quintela         return 1;
681a555b809SJuan Quintela     }
68260fe637bSDr. David Alan Gilbert     if (qemu_file_get_error(f)) {
68360fe637bSDr. David Alan Gilbert         return 1;
68460fe637bSDr. David Alan Gilbert     }
685c7fc8d32SDaniel P. Berrangé     if (f->rate_limit_max > 0 && f->rate_limit_used > f->rate_limit_max) {
68660fe637bSDr. David Alan Gilbert         return 1;
68760fe637bSDr. David Alan Gilbert     }
68860fe637bSDr. David Alan Gilbert     return 0;
68960fe637bSDr. David Alan Gilbert }
69060fe637bSDr. David Alan Gilbert 
69160fe637bSDr. David Alan Gilbert int64_t qemu_file_get_rate_limit(QEMUFile *f)
69260fe637bSDr. David Alan Gilbert {
693c7fc8d32SDaniel P. Berrangé     return f->rate_limit_max;
69460fe637bSDr. David Alan Gilbert }
69560fe637bSDr. David Alan Gilbert 
69660fe637bSDr. David Alan Gilbert void qemu_file_set_rate_limit(QEMUFile *f, int64_t limit)
69760fe637bSDr. David Alan Gilbert {
698c7fc8d32SDaniel P. Berrangé     f->rate_limit_max = limit;
69960fe637bSDr. David Alan Gilbert }
70060fe637bSDr. David Alan Gilbert 
70160fe637bSDr. David Alan Gilbert void qemu_file_reset_rate_limit(QEMUFile *f)
70260fe637bSDr. David Alan Gilbert {
703c7fc8d32SDaniel P. Berrangé     f->rate_limit_used = 0;
70460fe637bSDr. David Alan Gilbert }
70560fe637bSDr. David Alan Gilbert 
7065d7d2558SIvan Ren void qemu_file_update_transfer(QEMUFile *f, int64_t len)
7075d7d2558SIvan Ren {
708c7fc8d32SDaniel P. Berrangé     f->rate_limit_used += len;
7095d7d2558SIvan Ren }
7105d7d2558SIvan Ren 
71160fe637bSDr. David Alan Gilbert void qemu_put_be16(QEMUFile *f, unsigned int v)
71260fe637bSDr. David Alan Gilbert {
71360fe637bSDr. David Alan Gilbert     qemu_put_byte(f, v >> 8);
71460fe637bSDr. David Alan Gilbert     qemu_put_byte(f, v);
71560fe637bSDr. David Alan Gilbert }
71660fe637bSDr. David Alan Gilbert 
71760fe637bSDr. David Alan Gilbert void qemu_put_be32(QEMUFile *f, unsigned int v)
71860fe637bSDr. David Alan Gilbert {
71960fe637bSDr. David Alan Gilbert     qemu_put_byte(f, v >> 24);
72060fe637bSDr. David Alan Gilbert     qemu_put_byte(f, v >> 16);
72160fe637bSDr. David Alan Gilbert     qemu_put_byte(f, v >> 8);
72260fe637bSDr. David Alan Gilbert     qemu_put_byte(f, v);
72360fe637bSDr. David Alan Gilbert }
72460fe637bSDr. David Alan Gilbert 
72560fe637bSDr. David Alan Gilbert void qemu_put_be64(QEMUFile *f, uint64_t v)
72660fe637bSDr. David Alan Gilbert {
72760fe637bSDr. David Alan Gilbert     qemu_put_be32(f, v >> 32);
72860fe637bSDr. David Alan Gilbert     qemu_put_be32(f, v);
72960fe637bSDr. David Alan Gilbert }
73060fe637bSDr. David Alan Gilbert 
73160fe637bSDr. David Alan Gilbert unsigned int qemu_get_be16(QEMUFile *f)
73260fe637bSDr. David Alan Gilbert {
73360fe637bSDr. David Alan Gilbert     unsigned int v;
73460fe637bSDr. David Alan Gilbert     v = qemu_get_byte(f) << 8;
73560fe637bSDr. David Alan Gilbert     v |= qemu_get_byte(f);
73660fe637bSDr. David Alan Gilbert     return v;
73760fe637bSDr. David Alan Gilbert }
73860fe637bSDr. David Alan Gilbert 
73960fe637bSDr. David Alan Gilbert unsigned int qemu_get_be32(QEMUFile *f)
74060fe637bSDr. David Alan Gilbert {
74160fe637bSDr. David Alan Gilbert     unsigned int v;
74290d6a673SPeter Maydell     v = (unsigned int)qemu_get_byte(f) << 24;
74360fe637bSDr. David Alan Gilbert     v |= qemu_get_byte(f) << 16;
74460fe637bSDr. David Alan Gilbert     v |= qemu_get_byte(f) << 8;
74560fe637bSDr. David Alan Gilbert     v |= qemu_get_byte(f);
74660fe637bSDr. David Alan Gilbert     return v;
74760fe637bSDr. David Alan Gilbert }
74860fe637bSDr. David Alan Gilbert 
74960fe637bSDr. David Alan Gilbert uint64_t qemu_get_be64(QEMUFile *f)
75060fe637bSDr. David Alan Gilbert {
75160fe637bSDr. David Alan Gilbert     uint64_t v;
75260fe637bSDr. David Alan Gilbert     v = (uint64_t)qemu_get_be32(f) << 32;
75360fe637bSDr. David Alan Gilbert     v |= qemu_get_be32(f);
75460fe637bSDr. David Alan Gilbert     return v;
75560fe637bSDr. David Alan Gilbert }
75644f0eadcSLiang Li 
757dcaf446eSXiao Guangrong /* return the size after compression, or negative value on error */
758dcaf446eSXiao Guangrong static int qemu_compress_data(z_stream *stream, uint8_t *dest, size_t dest_len,
759dcaf446eSXiao Guangrong                               const uint8_t *source, size_t source_len)
760dcaf446eSXiao Guangrong {
761dcaf446eSXiao Guangrong     int err;
762dcaf446eSXiao Guangrong 
763dcaf446eSXiao Guangrong     err = deflateReset(stream);
764dcaf446eSXiao Guangrong     if (err != Z_OK) {
765dcaf446eSXiao Guangrong         return -1;
766dcaf446eSXiao Guangrong     }
767dcaf446eSXiao Guangrong 
768dcaf446eSXiao Guangrong     stream->avail_in = source_len;
769dcaf446eSXiao Guangrong     stream->next_in = (uint8_t *)source;
770dcaf446eSXiao Guangrong     stream->avail_out = dest_len;
771dcaf446eSXiao Guangrong     stream->next_out = dest;
772dcaf446eSXiao Guangrong 
773dcaf446eSXiao Guangrong     err = deflate(stream, Z_FINISH);
774dcaf446eSXiao Guangrong     if (err != Z_STREAM_END) {
775dcaf446eSXiao Guangrong         return -1;
776dcaf446eSXiao Guangrong     }
777dcaf446eSXiao Guangrong 
778dcaf446eSXiao Guangrong     return stream->next_out - dest;
779dcaf446eSXiao Guangrong }
780dcaf446eSXiao Guangrong 
781dcaf446eSXiao Guangrong /* Compress size bytes of data start at p and store the compressed
782dcaf446eSXiao Guangrong  * data to the buffer of f.
783b3be2896SLiang Li  *
78442d24611SWei Yang  * Since the file is dummy file with empty_ops, return -1 if f has no space to
78542d24611SWei Yang  * save the compressed data.
78644f0eadcSLiang Li  */
787dcaf446eSXiao Guangrong ssize_t qemu_put_compression_data(QEMUFile *f, z_stream *stream,
788dcaf446eSXiao Guangrong                                   const uint8_t *p, size_t size)
78944f0eadcSLiang Li {
79044f0eadcSLiang Li     ssize_t blen = IO_BUF_SIZE - f->buf_index - sizeof(int32_t);
79144f0eadcSLiang Li 
79244f0eadcSLiang Li     if (blen < compressBound(size)) {
793b3be2896SLiang Li         return -1;
794b3be2896SLiang Li     }
795dcaf446eSXiao Guangrong 
796dcaf446eSXiao Guangrong     blen = qemu_compress_data(stream, f->buf + f->buf_index + sizeof(int32_t),
797dcaf446eSXiao Guangrong                               blen, p, size);
798dcaf446eSXiao Guangrong     if (blen < 0) {
79934ab9e97SXiao Guangrong         return -1;
80044f0eadcSLiang Li     }
80134ab9e97SXiao Guangrong 
80244f0eadcSLiang Li     qemu_put_be32(f, blen);
8031bf57fb3SWei Yang     add_buf_to_iovec(f, blen);
80444f0eadcSLiang Li     return blen + sizeof(int32_t);
80544f0eadcSLiang Li }
80644f0eadcSLiang Li 
80744f0eadcSLiang Li /* Put the data in the buffer of f_src to the buffer of f_des, and
80844f0eadcSLiang Li  * then reset the buf_index of f_src to 0.
80944f0eadcSLiang Li  */
81044f0eadcSLiang Li 
81144f0eadcSLiang Li int qemu_put_qemu_file(QEMUFile *f_des, QEMUFile *f_src)
81244f0eadcSLiang Li {
81344f0eadcSLiang Li     int len = 0;
81444f0eadcSLiang Li 
81544f0eadcSLiang Li     if (f_src->buf_index > 0) {
81644f0eadcSLiang Li         len = f_src->buf_index;
81744f0eadcSLiang Li         qemu_put_buffer(f_des, f_src->buf, f_src->buf_index);
81844f0eadcSLiang Li         f_src->buf_index = 0;
819787d134fSLiang Li         f_src->iovcnt = 0;
82044f0eadcSLiang Li     }
82144f0eadcSLiang Li     return len;
82244f0eadcSLiang Li }
823b3af1bc9SDr. David Alan Gilbert 
824b3af1bc9SDr. David Alan Gilbert /*
825b3af1bc9SDr. David Alan Gilbert  * Get a string whose length is determined by a single preceding byte
826b3af1bc9SDr. David Alan Gilbert  * A preallocated 256 byte buffer must be passed in.
827b3af1bc9SDr. David Alan Gilbert  * Returns: len on success and a 0 terminated string in the buffer
828b3af1bc9SDr. David Alan Gilbert  *          else 0
829b3af1bc9SDr. David Alan Gilbert  *          (Note a 0 length string will return 0 either way)
830b3af1bc9SDr. David Alan Gilbert  */
831b3af1bc9SDr. David Alan Gilbert size_t qemu_get_counted_string(QEMUFile *f, char buf[256])
832b3af1bc9SDr. David Alan Gilbert {
833b3af1bc9SDr. David Alan Gilbert     size_t len = qemu_get_byte(f);
834b3af1bc9SDr. David Alan Gilbert     size_t res = qemu_get_buffer(f, (uint8_t *)buf, len);
835b3af1bc9SDr. David Alan Gilbert 
836b3af1bc9SDr. David Alan Gilbert     buf[res] = 0;
837b3af1bc9SDr. David Alan Gilbert 
838b3af1bc9SDr. David Alan Gilbert     return res == len ? res : 0;
839b3af1bc9SDr. David Alan Gilbert }
840a800cd5cSDr. David Alan Gilbert 
841a800cd5cSDr. David Alan Gilbert /*
842f0d64cb7SVladimir Sementsov-Ogievskiy  * Put a string with one preceding byte containing its length. The length of
843f0d64cb7SVladimir Sementsov-Ogievskiy  * the string should be less than 256.
844f0d64cb7SVladimir Sementsov-Ogievskiy  */
845f0d64cb7SVladimir Sementsov-Ogievskiy void qemu_put_counted_string(QEMUFile *f, const char *str)
846f0d64cb7SVladimir Sementsov-Ogievskiy {
847f0d64cb7SVladimir Sementsov-Ogievskiy     size_t len = strlen(str);
848f0d64cb7SVladimir Sementsov-Ogievskiy 
849f0d64cb7SVladimir Sementsov-Ogievskiy     assert(len < 256);
850f0d64cb7SVladimir Sementsov-Ogievskiy     qemu_put_byte(f, len);
851f0d64cb7SVladimir Sementsov-Ogievskiy     qemu_put_buffer(f, (const uint8_t *)str, len);
852f0d64cb7SVladimir Sementsov-Ogievskiy }
853f0d64cb7SVladimir Sementsov-Ogievskiy 
854f0d64cb7SVladimir Sementsov-Ogievskiy /*
855a800cd5cSDr. David Alan Gilbert  * Set the blocking state of the QEMUFile.
856a800cd5cSDr. David Alan Gilbert  * Note: On some transports the OS only keeps a single blocking state for
857a800cd5cSDr. David Alan Gilbert  *       both directions, and thus changing the blocking on the main
858a800cd5cSDr. David Alan Gilbert  *       QEMUFile can also affect the return path.
859a800cd5cSDr. David Alan Gilbert  */
860a800cd5cSDr. David Alan Gilbert void qemu_file_set_blocking(QEMUFile *f, bool block)
861a800cd5cSDr. David Alan Gilbert {
86206ad5135SDaniel P. Berrange     if (f->ops->set_blocking) {
8633d661c8aSYury Kotov         f->ops->set_blocking(f->opaque, block, NULL);
864a800cd5cSDr. David Alan Gilbert     }
86506ad5135SDaniel P. Berrange }
866c6ad5be7SPeter Xu 
867c6ad5be7SPeter Xu /*
868c6ad5be7SPeter Xu  * Return the ioc object if it's a migration channel.  Note: it can return NULL
869c6ad5be7SPeter Xu  * for callers passing in a non-migration qemufile.  E.g. see qemu_fopen_bdrv()
870c6ad5be7SPeter Xu  * and its usage in e.g. load_snapshot().  So we need to check against NULL
871c6ad5be7SPeter Xu  * before using it.  If without the check, migration_incoming_state_destroy()
872c6ad5be7SPeter Xu  * could fail for load_snapshot().
873c6ad5be7SPeter Xu  */
874c6ad5be7SPeter Xu QIOChannel *qemu_file_get_ioc(QEMUFile *file)
875c6ad5be7SPeter Xu {
876c6ad5be7SPeter Xu     return file->has_ioc ? QIO_CHANNEL(file->opaque) : NULL;
877c6ad5be7SPeter Xu }
878