xref: /qemu/migration/qemu-file.c (revision e8c44363)
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"
308e4b2a70SJuan Quintela #include "migration-stats.h"
3108a0aee1SJuan Quintela #include "qemu-file.h"
3260fe637bSDr. David Alan Gilbert #include "trace.h"
339d3ebbe2SJuan Quintela #include "options.h"
343d661c8aSYury Kotov #include "qapi/error.h"
3548408174SJuan Quintela #include "rdma.h"
367f5b50a4SFabiano Rosas #include "io/channel-file.h"
3760fe637bSDr. David Alan Gilbert 
38a24939f2SDaniel P. Berrange #define IO_BUF_SIZE 32768
39f9919116SEric Blake #define MAX_IOV_SIZE MIN_CONST(IOV_MAX, 64)
40a24939f2SDaniel P. Berrange 
41a24939f2SDaniel P. Berrange struct QEMUFile {
422893a288SDaniel P. Berrangé     QIOChannel *ioc;
43c0c6e1e2SDaniel P. Berrangé     bool is_writable;
44a24939f2SDaniel P. Berrange 
45a24939f2SDaniel P. Berrange     int buf_index;
46a24939f2SDaniel P. Berrange     int buf_size; /* 0 when writing */
47a24939f2SDaniel P. Berrange     uint8_t buf[IO_BUF_SIZE];
48a24939f2SDaniel P. Berrange 
4953f09a10SPavel Butsykin     DECLARE_BITMAP(may_free, MAX_IOV_SIZE);
50a24939f2SDaniel P. Berrange     struct iovec iov[MAX_IOV_SIZE];
51a24939f2SDaniel P. Berrange     unsigned int iovcnt;
52a24939f2SDaniel P. Berrange 
53a24939f2SDaniel P. Berrange     int last_error;
543d661c8aSYury Kotov     Error *last_error_obj;
55a24939f2SDaniel P. Berrange };
56a24939f2SDaniel P. Berrange 
57e1a8c9b6SDr. David Alan Gilbert /*
58e1a8c9b6SDr. David Alan Gilbert  * Stop a file from being read/written - not all backing files can do this
59e1a8c9b6SDr. David Alan Gilbert  * typically only sockets can.
60d3c581b7SDaniel P. Berrangé  *
61d3c581b7SDaniel P. Berrangé  * TODO: convert to propagate Error objects instead of squashing
62d3c581b7SDaniel P. Berrangé  * to a fixed errno value
63e1a8c9b6SDr. David Alan Gilbert  */
qemu_file_shutdown(QEMUFile * f)64e1a8c9b6SDr. David Alan Gilbert int qemu_file_shutdown(QEMUFile *f)
65e1a8c9b6SDr. David Alan Gilbert {
66e8c44363SCédric Le Goater     Error *err = NULL;
67e8c44363SCédric Le Goater 
68f5816b5cSPeter Xu     /*
69f5816b5cSPeter Xu      * We must set qemufile error before the real shutdown(), otherwise
70f5816b5cSPeter Xu      * there can be a race window where we thought IO all went though
71f5816b5cSPeter Xu      * (because last_error==NULL) but actually IO has already stopped.
72f5816b5cSPeter Xu      *
73f5816b5cSPeter Xu      * If without correct ordering, the race can happen like this:
74f5816b5cSPeter Xu      *
75f5816b5cSPeter Xu      *      page receiver                     other thread
76f5816b5cSPeter Xu      *      -------------                     ------------
77f5816b5cSPeter Xu      *      qemu_get_buffer()
78f5816b5cSPeter Xu      *                                        do shutdown()
79f5816b5cSPeter Xu      *        returns 0 (buffer all zero)
80f5816b5cSPeter Xu      *        (we didn't check this retcode)
81f5816b5cSPeter Xu      *      try to detect IO error
82f5816b5cSPeter Xu      *        last_error==NULL, IO okay
83f5816b5cSPeter Xu      *      install ALL-ZERO page
84f5816b5cSPeter Xu      *                                        set last_error
85f5816b5cSPeter Xu      *      --> guest crash!
86f5816b5cSPeter Xu      */
87f5816b5cSPeter Xu     if (!f->last_error) {
88f5816b5cSPeter Xu         qemu_file_set_error(f, -EIO);
89f5816b5cSPeter Xu     }
90f5816b5cSPeter Xu 
91d3c581b7SDaniel P. Berrangé     if (!qio_channel_has_feature(f->ioc,
92d3c581b7SDaniel P. Berrangé                                  QIO_CHANNEL_FEATURE_SHUTDOWN)) {
93e1a8c9b6SDr. David Alan Gilbert         return -ENOSYS;
94e1a8c9b6SDr. David Alan Gilbert     }
95d3c581b7SDaniel P. Berrangé 
96e8c44363SCédric Le Goater     if (qio_channel_shutdown(f->ioc, QIO_CHANNEL_SHUTDOWN_BOTH, &err) < 0) {
97e8c44363SCédric Le Goater         error_report_err(err);
988c5ee0bfSJuan Quintela         return -EIO;
99d3c581b7SDaniel P. Berrangé     }
100a555b809SJuan Quintela 
1018c5ee0bfSJuan Quintela     return 0;
102e1a8c9b6SDr. David Alan Gilbert }
103e1a8c9b6SDr. David Alan Gilbert 
qemu_file_new_impl(QIOChannel * ioc,bool is_writable)10477ef2dc1SDaniel P. Berrangé static QEMUFile *qemu_file_new_impl(QIOChannel *ioc, bool is_writable)
10560fe637bSDr. David Alan Gilbert {
10660fe637bSDr. David Alan Gilbert     QEMUFile *f;
10760fe637bSDr. David Alan Gilbert 
10897f3ad35SMarkus Armbruster     f = g_new0(QEMUFile, 1);
10960fe637bSDr. David Alan Gilbert 
11077ef2dc1SDaniel P. Berrangé     object_ref(ioc);
1112893a288SDaniel P. Berrangé     f->ioc = ioc;
112c0c6e1e2SDaniel P. Berrangé     f->is_writable = is_writable;
1132893a288SDaniel P. Berrangé 
11460fe637bSDr. David Alan Gilbert     return f;
11560fe637bSDr. David Alan Gilbert }
11660fe637bSDr. David Alan Gilbert 
11702bdbe17SDaniel P. Berrangé /*
11802bdbe17SDaniel P. Berrangé  * Result: QEMUFile* for a 'return path' for comms in the opposite direction
11902bdbe17SDaniel P. Berrangé  *         NULL if not available
12002bdbe17SDaniel P. Berrangé  */
qemu_file_get_return_path(QEMUFile * f)12102bdbe17SDaniel P. Berrangé QEMUFile *qemu_file_get_return_path(QEMUFile *f)
12202bdbe17SDaniel P. Berrangé {
12377ef2dc1SDaniel P. Berrangé     return qemu_file_new_impl(f->ioc, !f->is_writable);
12402bdbe17SDaniel P. Berrangé }
12502bdbe17SDaniel P. Berrangé 
qemu_file_new_output(QIOChannel * ioc)12677ef2dc1SDaniel P. Berrangé QEMUFile *qemu_file_new_output(QIOChannel *ioc)
127c0c6e1e2SDaniel P. Berrangé {
12877ef2dc1SDaniel P. Berrangé     return qemu_file_new_impl(ioc, true);
129c0c6e1e2SDaniel P. Berrangé }
130c0c6e1e2SDaniel P. Berrangé 
qemu_file_new_input(QIOChannel * ioc)13177ef2dc1SDaniel P. Berrangé QEMUFile *qemu_file_new_input(QIOChannel *ioc)
132c0c6e1e2SDaniel P. Berrangé {
13377ef2dc1SDaniel P. Berrangé     return qemu_file_new_impl(ioc, false);
134c0c6e1e2SDaniel P. Berrangé }
135c0c6e1e2SDaniel P. Berrangé 
13660fe637bSDr. David Alan Gilbert /*
1373d661c8aSYury Kotov  * Get last error for stream f with optional Error*
1383d661c8aSYury Kotov  *
1393d661c8aSYury Kotov  * Return negative error value if there has been an error on previous
1403d661c8aSYury Kotov  * operations, return 0 if no error happened.
1413d661c8aSYury Kotov  *
142f4b897f4SPeter Xu  * If errp is specified, a verbose error message will be copied over.
1433d661c8aSYury Kotov  */
qemu_file_get_error_obj(QEMUFile * f,Error ** errp)1447aa6070dSPeter Xu int qemu_file_get_error_obj(QEMUFile *f, Error **errp)
1453d661c8aSYury Kotov {
146f4b897f4SPeter Xu     if (!f->last_error) {
147f4b897f4SPeter Xu         return 0;
1483d661c8aSYury Kotov     }
149f4b897f4SPeter Xu 
150f4b897f4SPeter Xu     /* There is an error */
151f4b897f4SPeter Xu     if (errp) {
152f4b897f4SPeter Xu         if (f->last_error_obj) {
153f4b897f4SPeter Xu             *errp = error_copy(f->last_error_obj);
154f4b897f4SPeter Xu         } else {
155f4b897f4SPeter Xu             error_setg_errno(errp, -f->last_error, "Channel error");
156f4b897f4SPeter Xu         }
157f4b897f4SPeter Xu     }
158f4b897f4SPeter Xu 
1593d661c8aSYury Kotov     return f->last_error;
1603d661c8aSYury Kotov }
1613d661c8aSYury Kotov 
1623d661c8aSYury Kotov /*
16360bb3c58SPeter Xu  * Get last error for either stream f1 or f2 with optional Error*.
16460bb3c58SPeter Xu  * The error returned (non-zero) can be either from f1 or f2.
16560bb3c58SPeter Xu  *
16660bb3c58SPeter Xu  * If any of the qemufile* is NULL, then skip the check on that file.
16760bb3c58SPeter Xu  *
16860bb3c58SPeter Xu  * When there is no error on both qemufile, zero is returned.
16960bb3c58SPeter Xu  */
qemu_file_get_error_obj_any(QEMUFile * f1,QEMUFile * f2,Error ** errp)17060bb3c58SPeter Xu int qemu_file_get_error_obj_any(QEMUFile *f1, QEMUFile *f2, Error **errp)
17160bb3c58SPeter Xu {
17260bb3c58SPeter Xu     int ret = 0;
17360bb3c58SPeter Xu 
17460bb3c58SPeter Xu     if (f1) {
17560bb3c58SPeter Xu         ret = qemu_file_get_error_obj(f1, errp);
17660bb3c58SPeter Xu         /* If there's already error detected, return */
17760bb3c58SPeter Xu         if (ret) {
17860bb3c58SPeter Xu             return ret;
17960bb3c58SPeter Xu         }
18060bb3c58SPeter Xu     }
18160bb3c58SPeter Xu 
18260bb3c58SPeter Xu     if (f2) {
18360bb3c58SPeter Xu         ret = qemu_file_get_error_obj(f2, errp);
18460bb3c58SPeter Xu     }
18560bb3c58SPeter Xu 
18660bb3c58SPeter Xu     return ret;
18760bb3c58SPeter Xu }
18860bb3c58SPeter Xu 
18960bb3c58SPeter Xu /*
1903d661c8aSYury Kotov  * Set the last error for stream f with optional Error*
1913d661c8aSYury Kotov  */
qemu_file_set_error_obj(QEMUFile * f,int ret,Error * err)1923d661c8aSYury Kotov void qemu_file_set_error_obj(QEMUFile *f, int ret, Error *err)
1933d661c8aSYury Kotov {
1943d661c8aSYury Kotov     if (f->last_error == 0 && ret) {
1953d661c8aSYury Kotov         f->last_error = ret;
1963d661c8aSYury Kotov         error_propagate(&f->last_error_obj, err);
1973d661c8aSYury Kotov     } else if (err) {
1983d661c8aSYury Kotov         error_report_err(err);
1993d661c8aSYury Kotov     }
2003d661c8aSYury Kotov }
2013d661c8aSYury Kotov 
2023d661c8aSYury Kotov /*
20360fe637bSDr. David Alan Gilbert  * Get last error for stream f
20460fe637bSDr. David Alan Gilbert  *
20560fe637bSDr. David Alan Gilbert  * Return negative error value if there has been an error on previous
20660fe637bSDr. David Alan Gilbert  * operations, return 0 if no error happened.
20760fe637bSDr. David Alan Gilbert  *
20860fe637bSDr. David Alan Gilbert  */
qemu_file_get_error(QEMUFile * f)20960fe637bSDr. David Alan Gilbert int qemu_file_get_error(QEMUFile *f)
21060fe637bSDr. David Alan Gilbert {
211fc55cf31SJuan Quintela     return f->last_error;
21260fe637bSDr. David Alan Gilbert }
21360fe637bSDr. David Alan Gilbert 
2143d661c8aSYury Kotov /*
2153d661c8aSYury Kotov  * Set the last error for stream f
2163d661c8aSYury Kotov  */
qemu_file_set_error(QEMUFile * f,int ret)21760fe637bSDr. David Alan Gilbert void qemu_file_set_error(QEMUFile *f, int ret)
21860fe637bSDr. David Alan Gilbert {
2193d661c8aSYury Kotov     qemu_file_set_error_obj(f, ret, NULL);
22060fe637bSDr. David Alan Gilbert }
22160fe637bSDr. David Alan Gilbert 
qemu_file_is_writable(QEMUFile * f)2229ccf83f4SJuan Quintela static bool qemu_file_is_writable(QEMUFile *f)
22360fe637bSDr. David Alan Gilbert {
224c0c6e1e2SDaniel P. Berrangé     return f->is_writable;
22560fe637bSDr. David Alan Gilbert }
22660fe637bSDr. David Alan Gilbert 
qemu_iovec_release_ram(QEMUFile * f)22753f09a10SPavel Butsykin static void qemu_iovec_release_ram(QEMUFile *f)
22853f09a10SPavel Butsykin {
22953f09a10SPavel Butsykin     struct iovec iov;
23053f09a10SPavel Butsykin     unsigned long idx;
23153f09a10SPavel Butsykin 
23253f09a10SPavel Butsykin     /* Find and release all the contiguous memory ranges marked as may_free. */
23353f09a10SPavel Butsykin     idx = find_next_bit(f->may_free, f->iovcnt, 0);
23453f09a10SPavel Butsykin     if (idx >= f->iovcnt) {
23553f09a10SPavel Butsykin         return;
23653f09a10SPavel Butsykin     }
23753f09a10SPavel Butsykin     iov = f->iov[idx];
23853f09a10SPavel Butsykin 
23953f09a10SPavel Butsykin     /* The madvise() in the loop is called for iov within a continuous range and
24053f09a10SPavel Butsykin      * then reinitialize the iov. And in the end, madvise() is called for the
24153f09a10SPavel Butsykin      * last iov.
24253f09a10SPavel Butsykin      */
24353f09a10SPavel Butsykin     while ((idx = find_next_bit(f->may_free, f->iovcnt, idx + 1)) < f->iovcnt) {
24453f09a10SPavel Butsykin         /* check for adjacent buffer and coalesce them */
24553f09a10SPavel Butsykin         if (iov.iov_base + iov.iov_len == f->iov[idx].iov_base) {
24653f09a10SPavel Butsykin             iov.iov_len += f->iov[idx].iov_len;
24753f09a10SPavel Butsykin             continue;
24853f09a10SPavel Butsykin         }
24953f09a10SPavel Butsykin         if (qemu_madvise(iov.iov_base, iov.iov_len, QEMU_MADV_DONTNEED) < 0) {
25053f09a10SPavel Butsykin             error_report("migrate: madvise DONTNEED failed %p %zd: %s",
25153f09a10SPavel Butsykin                          iov.iov_base, iov.iov_len, strerror(errno));
25253f09a10SPavel Butsykin         }
25353f09a10SPavel Butsykin         iov = f->iov[idx];
25453f09a10SPavel Butsykin     }
25553f09a10SPavel Butsykin     if (qemu_madvise(iov.iov_base, iov.iov_len, QEMU_MADV_DONTNEED) < 0) {
25653f09a10SPavel Butsykin             error_report("migrate: madvise DONTNEED failed %p %zd: %s",
25753f09a10SPavel Butsykin                          iov.iov_base, iov.iov_len, strerror(errno));
25853f09a10SPavel Butsykin     }
25953f09a10SPavel Butsykin     memset(f->may_free, 0, sizeof(f->may_free));
26053f09a10SPavel Butsykin }
26153f09a10SPavel Butsykin 
qemu_file_is_seekable(QEMUFile * f)2627f5b50a4SFabiano Rosas bool qemu_file_is_seekable(QEMUFile *f)
2637f5b50a4SFabiano Rosas {
2647f5b50a4SFabiano Rosas     return qio_channel_has_feature(f->ioc, QIO_CHANNEL_FEATURE_SEEKABLE);
2657f5b50a4SFabiano Rosas }
26677ef2dc1SDaniel P. Berrangé 
26760fe637bSDr. David Alan Gilbert /**
26860fe637bSDr. David Alan Gilbert  * Flushes QEMUFile buffer
26960fe637bSDr. David Alan Gilbert  *
2703b348706SDr. David Alan Gilbert  * This will flush all pending data. If data was only partially flushed, it
2713b348706SDr. David Alan Gilbert  * will set an error state.
27260fe637bSDr. David Alan Gilbert  */
qemu_fflush(QEMUFile * f)273be07a0edSJuan Quintela int qemu_fflush(QEMUFile *f)
27460fe637bSDr. David Alan Gilbert {
27560fe637bSDr. David Alan Gilbert     if (!qemu_file_is_writable(f)) {
276be07a0edSJuan Quintela         return f->last_error;
27760fe637bSDr. David Alan Gilbert     }
27860fe637bSDr. David Alan Gilbert 
279be07a0edSJuan Quintela     if (f->last_error) {
280be07a0edSJuan Quintela         return f->last_error;
281a555b809SJuan Quintela     }
28260fe637bSDr. David Alan Gilbert     if (f->iovcnt > 0) {
283ec2135eeSDaniel P. Berrangé         Error *local_error = NULL;
284ec2135eeSDaniel P. Berrangé         if (qio_channel_writev_all(f->ioc,
285ec2135eeSDaniel P. Berrangé                                    f->iov, f->iovcnt,
286ec2135eeSDaniel P. Berrangé                                    &local_error) < 0) {
287ec2135eeSDaniel P. Berrangé             qemu_file_set_error_obj(f, -EIO, local_error);
288ec2135eeSDaniel P. Berrangé         } else {
289de37f8b9SJuan Quintela             uint64_t size = iov_size(f->iov, f->iovcnt);
2902d897237SJuan Quintela             stat64_add(&mig_stats.qemu_file_transferred, size);
291ec2135eeSDaniel P. Berrangé         }
29253f09a10SPavel Butsykin 
29353f09a10SPavel Butsykin         qemu_iovec_release_ram(f);
29460fe637bSDr. David Alan Gilbert     }
295baf51e77SDaniel P. Berrange 
29660fe637bSDr. David Alan Gilbert     f->buf_index = 0;
29760fe637bSDr. David Alan Gilbert     f->iovcnt = 0;
298be07a0edSJuan Quintela     return f->last_error;
29960fe637bSDr. David Alan Gilbert }
30060fe637bSDr. David Alan Gilbert 
30160fe637bSDr. David Alan Gilbert /*
30260fe637bSDr. David Alan Gilbert  * Attempt to fill the buffer from the underlying file
30360fe637bSDr. David Alan Gilbert  * Returns the number of bytes read, or negative value for an error.
30460fe637bSDr. David Alan Gilbert  *
30560fe637bSDr. David Alan Gilbert  * Note that it can return a partially full buffer even in a not error/not EOF
30660fe637bSDr. David Alan Gilbert  * case if the underlying file descriptor gives a short read, and that can
30760fe637bSDr. David Alan Gilbert  * happen even on a blocking fd.
30860fe637bSDr. David Alan Gilbert  */
qemu_fill_buffer(QEMUFile * f)309394b9407SPaolo Bonzini static ssize_t coroutine_mixed_fn qemu_fill_buffer(QEMUFile *f)
31060fe637bSDr. David Alan Gilbert {
31160fe637bSDr. David Alan Gilbert     int len;
31260fe637bSDr. David Alan Gilbert     int pending;
3133d661c8aSYury Kotov     Error *local_error = NULL;
31460fe637bSDr. David Alan Gilbert 
31560fe637bSDr. David Alan Gilbert     assert(!qemu_file_is_writable(f));
31660fe637bSDr. David Alan Gilbert 
31760fe637bSDr. David Alan Gilbert     pending = f->buf_size - f->buf_index;
31860fe637bSDr. David Alan Gilbert     if (pending > 0) {
31960fe637bSDr. David Alan Gilbert         memmove(f->buf, f->buf + f->buf_index, pending);
32060fe637bSDr. David Alan Gilbert     }
32160fe637bSDr. David Alan Gilbert     f->buf_index = 0;
32260fe637bSDr. David Alan Gilbert     f->buf_size = pending;
32360fe637bSDr. David Alan Gilbert 
324ac7d25b8SJuan Quintela     if (qemu_file_get_error(f)) {
325a555b809SJuan Quintela         return 0;
326a555b809SJuan Quintela     }
327a555b809SJuan Quintela 
328f759d705SDaniel P. Berrangé     do {
329f759d705SDaniel P. Berrangé         len = qio_channel_read(f->ioc,
330f759d705SDaniel P. Berrangé                                (char *)f->buf + pending,
331f759d705SDaniel P. Berrangé                                IO_BUF_SIZE - pending,
332f759d705SDaniel P. Berrangé                                &local_error);
333f759d705SDaniel P. Berrangé         if (len == QIO_CHANNEL_ERR_BLOCK) {
334f759d705SDaniel P. Berrangé             if (qemu_in_coroutine()) {
335f759d705SDaniel P. Berrangé                 qio_channel_yield(f->ioc, G_IO_IN);
336f759d705SDaniel P. Berrangé             } else {
337f759d705SDaniel P. Berrangé                 qio_channel_wait(f->ioc, G_IO_IN);
338f759d705SDaniel P. Berrangé             }
339f759d705SDaniel P. Berrangé         } else if (len < 0) {
340f759d705SDaniel P. Berrangé             len = -EIO;
341f759d705SDaniel P. Berrangé         }
342f759d705SDaniel P. Berrangé     } while (len == QIO_CHANNEL_ERR_BLOCK);
343f759d705SDaniel P. Berrangé 
34460fe637bSDr. David Alan Gilbert     if (len > 0) {
34560fe637bSDr. David Alan Gilbert         f->buf_size += len;
34660fe637bSDr. David Alan Gilbert     } else if (len == 0) {
3473d661c8aSYury Kotov         qemu_file_set_error_obj(f, -EIO, local_error);
3483d661c8aSYury Kotov     } else {
3495f87072eSDaniel P. Berrangé         qemu_file_set_error_obj(f, len, local_error);
35060fe637bSDr. David Alan Gilbert     }
35160fe637bSDr. David Alan Gilbert 
35260fe637bSDr. David Alan Gilbert     return len;
35360fe637bSDr. David Alan Gilbert }
35460fe637bSDr. David Alan Gilbert 
35560fe637bSDr. David Alan Gilbert /** Closes the file
35660fe637bSDr. David Alan Gilbert  *
35760fe637bSDr. David Alan Gilbert  * Returns negative error value if any error happened on previous operations or
35860fe637bSDr. David Alan Gilbert  * while closing the file. Returns 0 or positive number on success.
35960fe637bSDr. David Alan Gilbert  *
36060fe637bSDr. David Alan Gilbert  * The meaning of return value on success depends on the specific backend
36160fe637bSDr. David Alan Gilbert  * being used.
36260fe637bSDr. David Alan Gilbert  */
qemu_fclose(QEMUFile * f)36360fe637bSDr. David Alan Gilbert int qemu_fclose(QEMUFile *f)
36460fe637bSDr. David Alan Gilbert {
365be07a0edSJuan Quintela     int ret = qemu_fflush(f);
366be07a0edSJuan Quintela     int ret2 = qio_channel_close(f->ioc, NULL);
36760fe637bSDr. David Alan Gilbert     if (ret >= 0) {
36860fe637bSDr. David Alan Gilbert         ret = ret2;
36960fe637bSDr. David Alan Gilbert     }
3700ae1f7f0SDaniel P. Berrangé     g_clear_pointer(&f->ioc, object_unref);
3713d661c8aSYury Kotov     error_free(f->last_error_obj);
37260fe637bSDr. David Alan Gilbert     g_free(f);
37360fe637bSDr. David Alan Gilbert     trace_qemu_file_fclose();
37460fe637bSDr. David Alan Gilbert     return ret;
37560fe637bSDr. David Alan Gilbert }
37660fe637bSDr. David Alan Gilbert 
3771bf57fb3SWei Yang /*
3781bf57fb3SWei Yang  * Add buf to iovec. Do flush if iovec is full.
3791bf57fb3SWei Yang  *
3801bf57fb3SWei Yang  * Return values:
3811bf57fb3SWei Yang  * 1 iovec is full and flushed
3821bf57fb3SWei Yang  * 0 iovec is not flushed
3831bf57fb3SWei Yang  *
3841bf57fb3SWei Yang  */
add_to_iovec(QEMUFile * f,const uint8_t * buf,size_t size,bool may_free)3851bf57fb3SWei Yang static int add_to_iovec(QEMUFile *f, const uint8_t *buf, size_t size,
38653f09a10SPavel Butsykin                         bool may_free)
38760fe637bSDr. David Alan Gilbert {
38860fe637bSDr. David Alan Gilbert     /* check for adjacent buffer and coalesce them */
38960fe637bSDr. David Alan Gilbert     if (f->iovcnt > 0 && buf == f->iov[f->iovcnt - 1].iov_base +
39053f09a10SPavel Butsykin         f->iov[f->iovcnt - 1].iov_len &&
39153f09a10SPavel Butsykin         may_free == test_bit(f->iovcnt - 1, f->may_free))
39253f09a10SPavel Butsykin     {
39360fe637bSDr. David Alan Gilbert         f->iov[f->iovcnt - 1].iov_len += size;
39460fe637bSDr. David Alan Gilbert     } else {
395c00d434aSFeng Lin         if (f->iovcnt >= MAX_IOV_SIZE) {
396c00d434aSFeng Lin             /* Should only happen if a previous fflush failed */
397ac7d25b8SJuan Quintela             assert(qemu_file_get_error(f) || !qemu_file_is_writable(f));
398c00d434aSFeng Lin             return 1;
399c00d434aSFeng Lin         }
40053f09a10SPavel Butsykin         if (may_free) {
40153f09a10SPavel Butsykin             set_bit(f->iovcnt, f->may_free);
40253f09a10SPavel Butsykin         }
40360fe637bSDr. David Alan Gilbert         f->iov[f->iovcnt].iov_base = (uint8_t *)buf;
40460fe637bSDr. David Alan Gilbert         f->iov[f->iovcnt++].iov_len = size;
40560fe637bSDr. David Alan Gilbert     }
40660fe637bSDr. David Alan Gilbert 
40760fe637bSDr. David Alan Gilbert     if (f->iovcnt >= MAX_IOV_SIZE) {
40860fe637bSDr. David Alan Gilbert         qemu_fflush(f);
4091bf57fb3SWei Yang         return 1;
4101bf57fb3SWei Yang     }
4111bf57fb3SWei Yang 
4121bf57fb3SWei Yang     return 0;
4131bf57fb3SWei Yang }
4141bf57fb3SWei Yang 
add_buf_to_iovec(QEMUFile * f,size_t len)4151bf57fb3SWei Yang static void add_buf_to_iovec(QEMUFile *f, size_t len)
4161bf57fb3SWei Yang {
4171bf57fb3SWei Yang     if (!add_to_iovec(f, f->buf + f->buf_index, len, false)) {
4181bf57fb3SWei Yang         f->buf_index += len;
4191bf57fb3SWei Yang         if (f->buf_index == IO_BUF_SIZE) {
4201bf57fb3SWei Yang             qemu_fflush(f);
4211bf57fb3SWei Yang         }
42260fe637bSDr. David Alan Gilbert     }
42360fe637bSDr. David Alan Gilbert }
42460fe637bSDr. David Alan Gilbert 
qemu_put_buffer_async(QEMUFile * f,const uint8_t * buf,size_t size,bool may_free)42553f09a10SPavel Butsykin void qemu_put_buffer_async(QEMUFile *f, const uint8_t *buf, size_t size,
42653f09a10SPavel Butsykin                            bool may_free)
42760fe637bSDr. David Alan Gilbert {
42860fe637bSDr. David Alan Gilbert     if (f->last_error) {
42960fe637bSDr. David Alan Gilbert         return;
43060fe637bSDr. David Alan Gilbert     }
43160fe637bSDr. David Alan Gilbert 
43253f09a10SPavel Butsykin     add_to_iovec(f, buf, size, may_free);
43360fe637bSDr. David Alan Gilbert }
43460fe637bSDr. David Alan Gilbert 
qemu_put_buffer(QEMUFile * f,const uint8_t * buf,size_t size)43556f3835fSDr. David Alan Gilbert void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, size_t size)
43660fe637bSDr. David Alan Gilbert {
43756f3835fSDr. David Alan Gilbert     size_t l;
43860fe637bSDr. David Alan Gilbert 
43960fe637bSDr. David Alan Gilbert     if (f->last_error) {
44060fe637bSDr. David Alan Gilbert         return;
44160fe637bSDr. David Alan Gilbert     }
44260fe637bSDr. David Alan Gilbert 
44360fe637bSDr. David Alan Gilbert     while (size > 0) {
44460fe637bSDr. David Alan Gilbert         l = IO_BUF_SIZE - f->buf_index;
44560fe637bSDr. David Alan Gilbert         if (l > size) {
44660fe637bSDr. David Alan Gilbert             l = size;
44760fe637bSDr. David Alan Gilbert         }
44860fe637bSDr. David Alan Gilbert         memcpy(f->buf + f->buf_index, buf, l);
4491bf57fb3SWei Yang         add_buf_to_iovec(f, l);
45060fe637bSDr. David Alan Gilbert         if (qemu_file_get_error(f)) {
45160fe637bSDr. David Alan Gilbert             break;
45260fe637bSDr. David Alan Gilbert         }
45360fe637bSDr. David Alan Gilbert         buf += l;
45460fe637bSDr. David Alan Gilbert         size -= l;
45560fe637bSDr. David Alan Gilbert     }
45660fe637bSDr. David Alan Gilbert }
45760fe637bSDr. David Alan Gilbert 
qemu_put_buffer_at(QEMUFile * f,const uint8_t * buf,size_t buflen,off_t pos)4587f5b50a4SFabiano Rosas void qemu_put_buffer_at(QEMUFile *f, const uint8_t *buf, size_t buflen,
4597f5b50a4SFabiano Rosas                         off_t pos)
4607f5b50a4SFabiano Rosas {
4617f5b50a4SFabiano Rosas     Error *err = NULL;
4627f5b50a4SFabiano Rosas     size_t ret;
4637f5b50a4SFabiano Rosas 
4647f5b50a4SFabiano Rosas     if (f->last_error) {
4657f5b50a4SFabiano Rosas         return;
4667f5b50a4SFabiano Rosas     }
4677f5b50a4SFabiano Rosas 
4687f5b50a4SFabiano Rosas     qemu_fflush(f);
4697f5b50a4SFabiano Rosas     ret = qio_channel_pwrite(f->ioc, (char *)buf, buflen, pos, &err);
4707f5b50a4SFabiano Rosas 
4717f5b50a4SFabiano Rosas     if (err) {
4727f5b50a4SFabiano Rosas         qemu_file_set_error_obj(f, -EIO, err);
4737f5b50a4SFabiano Rosas         return;
4747f5b50a4SFabiano Rosas     }
4757f5b50a4SFabiano Rosas 
4767f5b50a4SFabiano Rosas     if ((ssize_t)ret == QIO_CHANNEL_ERR_BLOCK) {
4777f5b50a4SFabiano Rosas         qemu_file_set_error_obj(f, -EAGAIN, NULL);
4787f5b50a4SFabiano Rosas         return;
4797f5b50a4SFabiano Rosas     }
4807f5b50a4SFabiano Rosas 
4817f5b50a4SFabiano Rosas     if (ret != buflen) {
4827f5b50a4SFabiano Rosas         error_setg(&err, "Partial write of size %zu, expected %zu", ret,
4837f5b50a4SFabiano Rosas                    buflen);
4847f5b50a4SFabiano Rosas         qemu_file_set_error_obj(f, -EIO, err);
4857f5b50a4SFabiano Rosas         return;
4867f5b50a4SFabiano Rosas     }
4877f5b50a4SFabiano Rosas 
4887f5b50a4SFabiano Rosas     stat64_add(&mig_stats.qemu_file_transferred, buflen);
4897f5b50a4SFabiano Rosas 
4907f5b50a4SFabiano Rosas     return;
4917f5b50a4SFabiano Rosas }
4927f5b50a4SFabiano Rosas 
4937f5b50a4SFabiano Rosas 
qemu_get_buffer_at(QEMUFile * f,const uint8_t * buf,size_t buflen,off_t pos)4947f5b50a4SFabiano Rosas size_t qemu_get_buffer_at(QEMUFile *f, const uint8_t *buf, size_t buflen,
4957f5b50a4SFabiano Rosas                           off_t pos)
4967f5b50a4SFabiano Rosas {
4977f5b50a4SFabiano Rosas     Error *err = NULL;
4987f5b50a4SFabiano Rosas     size_t ret;
4997f5b50a4SFabiano Rosas 
5007f5b50a4SFabiano Rosas     if (f->last_error) {
5017f5b50a4SFabiano Rosas         return 0;
5027f5b50a4SFabiano Rosas     }
5037f5b50a4SFabiano Rosas 
5047f5b50a4SFabiano Rosas     ret = qio_channel_pread(f->ioc, (char *)buf, buflen, pos, &err);
5057f5b50a4SFabiano Rosas 
5067f5b50a4SFabiano Rosas     if ((ssize_t)ret == -1 || err) {
5077f5b50a4SFabiano Rosas         qemu_file_set_error_obj(f, -EIO, err);
5087f5b50a4SFabiano Rosas         return 0;
5097f5b50a4SFabiano Rosas     }
5107f5b50a4SFabiano Rosas 
5117f5b50a4SFabiano Rosas     if ((ssize_t)ret == QIO_CHANNEL_ERR_BLOCK) {
5127f5b50a4SFabiano Rosas         qemu_file_set_error_obj(f, -EAGAIN, NULL);
5137f5b50a4SFabiano Rosas         return 0;
5147f5b50a4SFabiano Rosas     }
5157f5b50a4SFabiano Rosas 
5167f5b50a4SFabiano Rosas     if (ret != buflen) {
5177f5b50a4SFabiano Rosas         error_setg(&err, "Partial read of size %zu, expected %zu", ret, buflen);
5187f5b50a4SFabiano Rosas         qemu_file_set_error_obj(f, -EIO, err);
5197f5b50a4SFabiano Rosas         return 0;
5207f5b50a4SFabiano Rosas     }
5217f5b50a4SFabiano Rosas 
5227f5b50a4SFabiano Rosas     return ret;
5237f5b50a4SFabiano Rosas }
5247f5b50a4SFabiano Rosas 
qemu_set_offset(QEMUFile * f,off_t off,int whence)5257f5b50a4SFabiano Rosas void qemu_set_offset(QEMUFile *f, off_t off, int whence)
5267f5b50a4SFabiano Rosas {
5277f5b50a4SFabiano Rosas     Error *err = NULL;
5287f5b50a4SFabiano Rosas     off_t ret;
5297f5b50a4SFabiano Rosas 
5307f5b50a4SFabiano Rosas     if (qemu_file_is_writable(f)) {
5317f5b50a4SFabiano Rosas         qemu_fflush(f);
5327f5b50a4SFabiano Rosas     } else {
5337f5b50a4SFabiano Rosas         /* Drop all cached buffers if existed; will trigger a re-fill later */
5347f5b50a4SFabiano Rosas         f->buf_index = 0;
5357f5b50a4SFabiano Rosas         f->buf_size = 0;
5367f5b50a4SFabiano Rosas     }
5377f5b50a4SFabiano Rosas 
5387f5b50a4SFabiano Rosas     ret = qio_channel_io_seek(f->ioc, off, whence, &err);
5397f5b50a4SFabiano Rosas     if (ret == (off_t)-1) {
5407f5b50a4SFabiano Rosas         qemu_file_set_error_obj(f, -EIO, err);
5417f5b50a4SFabiano Rosas     }
5427f5b50a4SFabiano Rosas }
5437f5b50a4SFabiano Rosas 
qemu_get_offset(QEMUFile * f)5447f5b50a4SFabiano Rosas off_t qemu_get_offset(QEMUFile *f)
5457f5b50a4SFabiano Rosas {
5467f5b50a4SFabiano Rosas     Error *err = NULL;
5477f5b50a4SFabiano Rosas     off_t ret;
5487f5b50a4SFabiano Rosas 
5497f5b50a4SFabiano Rosas     qemu_fflush(f);
5507f5b50a4SFabiano Rosas 
5517f5b50a4SFabiano Rosas     ret = qio_channel_io_seek(f->ioc, 0, SEEK_CUR, &err);
5527f5b50a4SFabiano Rosas     if (ret == (off_t)-1) {
5537f5b50a4SFabiano Rosas         qemu_file_set_error_obj(f, -EIO, err);
5547f5b50a4SFabiano Rosas     }
5557f5b50a4SFabiano Rosas     return ret;
5567f5b50a4SFabiano Rosas }
5577f5b50a4SFabiano Rosas 
5587f5b50a4SFabiano Rosas 
qemu_put_byte(QEMUFile * f,int v)55960fe637bSDr. David Alan Gilbert void qemu_put_byte(QEMUFile *f, int v)
56060fe637bSDr. David Alan Gilbert {
56160fe637bSDr. David Alan Gilbert     if (f->last_error) {
56260fe637bSDr. David Alan Gilbert         return;
56360fe637bSDr. David Alan Gilbert     }
56460fe637bSDr. David Alan Gilbert 
56560fe637bSDr. David Alan Gilbert     f->buf[f->buf_index] = v;
5661bf57fb3SWei Yang     add_buf_to_iovec(f, 1);
56760fe637bSDr. David Alan Gilbert }
56860fe637bSDr. David Alan Gilbert 
qemu_file_skip(QEMUFile * f,int size)56960fe637bSDr. David Alan Gilbert void qemu_file_skip(QEMUFile *f, int size)
57060fe637bSDr. David Alan Gilbert {
57160fe637bSDr. David Alan Gilbert     if (f->buf_index + size <= f->buf_size) {
57260fe637bSDr. David Alan Gilbert         f->buf_index += size;
57360fe637bSDr. David Alan Gilbert     }
57460fe637bSDr. David Alan Gilbert }
57560fe637bSDr. David Alan Gilbert 
57660fe637bSDr. David Alan Gilbert /*
5777c1e52baSDr. David Alan Gilbert  * Read 'size' bytes from file (at 'offset') without moving the
5787c1e52baSDr. David Alan Gilbert  * pointer and set 'buf' to point to that data.
57960fe637bSDr. David Alan Gilbert  *
58060fe637bSDr. David Alan Gilbert  * It will return size bytes unless there was an error, in which case it will
58160fe637bSDr. David Alan Gilbert  * return as many as it managed to read (assuming blocking fd's which
58260fe637bSDr. David Alan Gilbert  * all current QEMUFile are)
58360fe637bSDr. David Alan Gilbert  */
qemu_peek_buffer(QEMUFile * f,uint8_t ** buf,size_t size,size_t offset)584394b9407SPaolo Bonzini size_t coroutine_mixed_fn qemu_peek_buffer(QEMUFile *f, uint8_t **buf, size_t size, size_t offset)
58560fe637bSDr. David Alan Gilbert {
58656f3835fSDr. David Alan Gilbert     ssize_t pending;
58756f3835fSDr. David Alan Gilbert     size_t index;
58860fe637bSDr. David Alan Gilbert 
58960fe637bSDr. David Alan Gilbert     assert(!qemu_file_is_writable(f));
59060fe637bSDr. David Alan Gilbert     assert(offset < IO_BUF_SIZE);
59160fe637bSDr. David Alan Gilbert     assert(size <= IO_BUF_SIZE - offset);
59260fe637bSDr. David Alan Gilbert 
59360fe637bSDr. David Alan Gilbert     /* The 1st byte to read from */
59460fe637bSDr. David Alan Gilbert     index = f->buf_index + offset;
59560fe637bSDr. David Alan Gilbert     /* The number of available bytes starting at index */
59660fe637bSDr. David Alan Gilbert     pending = f->buf_size - index;
59760fe637bSDr. David Alan Gilbert 
59860fe637bSDr. David Alan Gilbert     /*
59960fe637bSDr. David Alan Gilbert      * qemu_fill_buffer might return just a few bytes, even when there isn't
60060fe637bSDr. David Alan Gilbert      * an error, so loop collecting them until we get enough.
60160fe637bSDr. David Alan Gilbert      */
60260fe637bSDr. David Alan Gilbert     while (pending < size) {
60360fe637bSDr. David Alan Gilbert         int received = qemu_fill_buffer(f);
60460fe637bSDr. David Alan Gilbert 
60560fe637bSDr. David Alan Gilbert         if (received <= 0) {
60660fe637bSDr. David Alan Gilbert             break;
60760fe637bSDr. David Alan Gilbert         }
60860fe637bSDr. David Alan Gilbert 
60960fe637bSDr. David Alan Gilbert         index = f->buf_index + offset;
61060fe637bSDr. David Alan Gilbert         pending = f->buf_size - index;
61160fe637bSDr. David Alan Gilbert     }
61260fe637bSDr. David Alan Gilbert 
61360fe637bSDr. David Alan Gilbert     if (pending <= 0) {
61460fe637bSDr. David Alan Gilbert         return 0;
61560fe637bSDr. David Alan Gilbert     }
61660fe637bSDr. David Alan Gilbert     if (size > pending) {
61760fe637bSDr. David Alan Gilbert         size = pending;
61860fe637bSDr. David Alan Gilbert     }
61960fe637bSDr. David Alan Gilbert 
6207c1e52baSDr. David Alan Gilbert     *buf = f->buf + index;
62160fe637bSDr. David Alan Gilbert     return size;
62260fe637bSDr. David Alan Gilbert }
62360fe637bSDr. David Alan Gilbert 
62460fe637bSDr. David Alan Gilbert /*
62560fe637bSDr. David Alan Gilbert  * Read 'size' bytes of data from the file into buf.
62660fe637bSDr. David Alan Gilbert  * 'size' can be larger than the internal buffer.
62760fe637bSDr. David Alan Gilbert  *
62860fe637bSDr. David Alan Gilbert  * It will return size bytes unless there was an error, in which case it will
62960fe637bSDr. David Alan Gilbert  * return as many as it managed to read (assuming blocking fd's which
63060fe637bSDr. David Alan Gilbert  * all current QEMUFile are)
63160fe637bSDr. David Alan Gilbert  */
qemu_get_buffer(QEMUFile * f,uint8_t * buf,size_t size)632394b9407SPaolo Bonzini size_t coroutine_mixed_fn qemu_get_buffer(QEMUFile *f, uint8_t *buf, size_t size)
63360fe637bSDr. David Alan Gilbert {
63456f3835fSDr. David Alan Gilbert     size_t pending = size;
63556f3835fSDr. David Alan Gilbert     size_t done = 0;
63660fe637bSDr. David Alan Gilbert 
63760fe637bSDr. David Alan Gilbert     while (pending > 0) {
63856f3835fSDr. David Alan Gilbert         size_t res;
6397c1e52baSDr. David Alan Gilbert         uint8_t *src;
64060fe637bSDr. David Alan Gilbert 
6417c1e52baSDr. David Alan Gilbert         res = qemu_peek_buffer(f, &src, MIN(pending, IO_BUF_SIZE), 0);
64260fe637bSDr. David Alan Gilbert         if (res == 0) {
64360fe637bSDr. David Alan Gilbert             return done;
64460fe637bSDr. David Alan Gilbert         }
6457c1e52baSDr. David Alan Gilbert         memcpy(buf, src, res);
64660fe637bSDr. David Alan Gilbert         qemu_file_skip(f, res);
64760fe637bSDr. David Alan Gilbert         buf += res;
64860fe637bSDr. David Alan Gilbert         pending -= res;
64960fe637bSDr. David Alan Gilbert         done += res;
65060fe637bSDr. David Alan Gilbert     }
65160fe637bSDr. David Alan Gilbert     return done;
65260fe637bSDr. David Alan Gilbert }
65360fe637bSDr. David Alan Gilbert 
65460fe637bSDr. David Alan Gilbert /*
6559504fb51SDr. David Alan Gilbert  * Read 'size' bytes of data from the file.
6569504fb51SDr. David Alan Gilbert  * 'size' can be larger than the internal buffer.
6579504fb51SDr. David Alan Gilbert  *
6589504fb51SDr. David Alan Gilbert  * The data:
6599504fb51SDr. David Alan Gilbert  *   may be held on an internal buffer (in which case *buf is updated
6609504fb51SDr. David Alan Gilbert  *     to point to it) that is valid until the next qemu_file operation.
6619504fb51SDr. David Alan Gilbert  * OR
6629504fb51SDr. David Alan Gilbert  *   will be copied to the *buf that was passed in.
6639504fb51SDr. David Alan Gilbert  *
6649504fb51SDr. David Alan Gilbert  * The code tries to avoid the copy if possible.
6659504fb51SDr. David Alan Gilbert  *
6669504fb51SDr. David Alan Gilbert  * It will return size bytes unless there was an error, in which case it will
6679504fb51SDr. David Alan Gilbert  * return as many as it managed to read (assuming blocking fd's which
6689504fb51SDr. David Alan Gilbert  * all current QEMUFile are)
6699504fb51SDr. David Alan Gilbert  *
6709504fb51SDr. David Alan Gilbert  * Note: Since **buf may get changed, the caller should take care to
6719504fb51SDr. David Alan Gilbert  *       keep a pointer to the original buffer if it needs to deallocate it.
6729504fb51SDr. David Alan Gilbert  */
qemu_get_buffer_in_place(QEMUFile * f,uint8_t ** buf,size_t size)673394b9407SPaolo Bonzini size_t coroutine_mixed_fn qemu_get_buffer_in_place(QEMUFile *f, uint8_t **buf, size_t size)
6749504fb51SDr. David Alan Gilbert {
6759504fb51SDr. David Alan Gilbert     if (size < IO_BUF_SIZE) {
6769504fb51SDr. David Alan Gilbert         size_t res;
6771dfafcbdSWainer dos Santos Moschetta         uint8_t *src = NULL;
6789504fb51SDr. David Alan Gilbert 
6799504fb51SDr. David Alan Gilbert         res = qemu_peek_buffer(f, &src, size, 0);
6809504fb51SDr. David Alan Gilbert 
6819504fb51SDr. David Alan Gilbert         if (res == size) {
6829504fb51SDr. David Alan Gilbert             qemu_file_skip(f, res);
6839504fb51SDr. David Alan Gilbert             *buf = src;
6849504fb51SDr. David Alan Gilbert             return res;
6859504fb51SDr. David Alan Gilbert         }
6869504fb51SDr. David Alan Gilbert     }
6879504fb51SDr. David Alan Gilbert 
6889504fb51SDr. David Alan Gilbert     return qemu_get_buffer(f, *buf, size);
6899504fb51SDr. David Alan Gilbert }
6909504fb51SDr. David Alan Gilbert 
6919504fb51SDr. David Alan Gilbert /*
69260fe637bSDr. David Alan Gilbert  * Peeks a single byte from the buffer; this isn't guaranteed to work if
69360fe637bSDr. David Alan Gilbert  * offset leaves a gap after the previous read/peeked data.
69460fe637bSDr. David Alan Gilbert  */
qemu_peek_byte(QEMUFile * f,int offset)695394b9407SPaolo Bonzini int coroutine_mixed_fn qemu_peek_byte(QEMUFile *f, int offset)
69660fe637bSDr. David Alan Gilbert {
69760fe637bSDr. David Alan Gilbert     int index = f->buf_index + offset;
69860fe637bSDr. David Alan Gilbert 
69960fe637bSDr. David Alan Gilbert     assert(!qemu_file_is_writable(f));
70060fe637bSDr. David Alan Gilbert     assert(offset < IO_BUF_SIZE);
70160fe637bSDr. David Alan Gilbert 
70260fe637bSDr. David Alan Gilbert     if (index >= f->buf_size) {
70360fe637bSDr. David Alan Gilbert         qemu_fill_buffer(f);
70460fe637bSDr. David Alan Gilbert         index = f->buf_index + offset;
70560fe637bSDr. David Alan Gilbert         if (index >= f->buf_size) {
70660fe637bSDr. David Alan Gilbert             return 0;
70760fe637bSDr. David Alan Gilbert         }
70860fe637bSDr. David Alan Gilbert     }
70960fe637bSDr. David Alan Gilbert     return f->buf[index];
71060fe637bSDr. David Alan Gilbert }
71160fe637bSDr. David Alan Gilbert 
qemu_get_byte(QEMUFile * f)712394b9407SPaolo Bonzini int coroutine_mixed_fn qemu_get_byte(QEMUFile *f)
71360fe637bSDr. David Alan Gilbert {
71460fe637bSDr. David Alan Gilbert     int result;
71560fe637bSDr. David Alan Gilbert 
71660fe637bSDr. David Alan Gilbert     result = qemu_peek_byte(f, 0);
71760fe637bSDr. David Alan Gilbert     qemu_file_skip(f, 1);
71860fe637bSDr. David Alan Gilbert     return result;
71960fe637bSDr. David Alan Gilbert }
72060fe637bSDr. David Alan Gilbert 
qemu_file_transferred(QEMUFile * f)721e9c0eed7SJuan Quintela uint64_t qemu_file_transferred(QEMUFile *f)
72297221400SAlexander Graf {
7232d897237SJuan Quintela     uint64_t ret = stat64_get(&mig_stats.qemu_file_transferred);
72497221400SAlexander Graf     int i;
72597221400SAlexander Graf 
726cc8bf57dSJuan Quintela     g_assert(qemu_file_is_writable(f));
727cc8bf57dSJuan Quintela 
72897221400SAlexander Graf     for (i = 0; i < f->iovcnt; i++) {
72997221400SAlexander Graf         ret += f->iov[i].iov_len;
73097221400SAlexander Graf     }
73197221400SAlexander Graf 
73297221400SAlexander Graf     return ret;
73397221400SAlexander Graf }
73497221400SAlexander Graf 
qemu_put_be16(QEMUFile * f,unsigned int v)73560fe637bSDr. David Alan Gilbert void qemu_put_be16(QEMUFile *f, unsigned int v)
73660fe637bSDr. David Alan Gilbert {
73760fe637bSDr. David Alan Gilbert     qemu_put_byte(f, v >> 8);
73860fe637bSDr. David Alan Gilbert     qemu_put_byte(f, v);
73960fe637bSDr. David Alan Gilbert }
74060fe637bSDr. David Alan Gilbert 
qemu_put_be32(QEMUFile * f,unsigned int v)74160fe637bSDr. David Alan Gilbert void qemu_put_be32(QEMUFile *f, unsigned int v)
74260fe637bSDr. David Alan Gilbert {
74360fe637bSDr. David Alan Gilbert     qemu_put_byte(f, v >> 24);
74460fe637bSDr. David Alan Gilbert     qemu_put_byte(f, v >> 16);
74560fe637bSDr. David Alan Gilbert     qemu_put_byte(f, v >> 8);
74660fe637bSDr. David Alan Gilbert     qemu_put_byte(f, v);
74760fe637bSDr. David Alan Gilbert }
74860fe637bSDr. David Alan Gilbert 
qemu_put_be64(QEMUFile * f,uint64_t v)74960fe637bSDr. David Alan Gilbert void qemu_put_be64(QEMUFile *f, uint64_t v)
75060fe637bSDr. David Alan Gilbert {
75160fe637bSDr. David Alan Gilbert     qemu_put_be32(f, v >> 32);
75260fe637bSDr. David Alan Gilbert     qemu_put_be32(f, v);
75360fe637bSDr. David Alan Gilbert }
75460fe637bSDr. David Alan Gilbert 
qemu_get_be16(QEMUFile * f)75560fe637bSDr. David Alan Gilbert unsigned int qemu_get_be16(QEMUFile *f)
75660fe637bSDr. David Alan Gilbert {
75760fe637bSDr. David Alan Gilbert     unsigned int v;
75860fe637bSDr. David Alan Gilbert     v = qemu_get_byte(f) << 8;
75960fe637bSDr. David Alan Gilbert     v |= qemu_get_byte(f);
76060fe637bSDr. David Alan Gilbert     return v;
76160fe637bSDr. David Alan Gilbert }
76260fe637bSDr. David Alan Gilbert 
qemu_get_be32(QEMUFile * f)76360fe637bSDr. David Alan Gilbert unsigned int qemu_get_be32(QEMUFile *f)
76460fe637bSDr. David Alan Gilbert {
76560fe637bSDr. David Alan Gilbert     unsigned int v;
76690d6a673SPeter Maydell     v = (unsigned int)qemu_get_byte(f) << 24;
76760fe637bSDr. David Alan Gilbert     v |= qemu_get_byte(f) << 16;
76860fe637bSDr. David Alan Gilbert     v |= qemu_get_byte(f) << 8;
76960fe637bSDr. David Alan Gilbert     v |= qemu_get_byte(f);
77060fe637bSDr. David Alan Gilbert     return v;
77160fe637bSDr. David Alan Gilbert }
77260fe637bSDr. David Alan Gilbert 
qemu_get_be64(QEMUFile * f)77360fe637bSDr. David Alan Gilbert uint64_t qemu_get_be64(QEMUFile *f)
77460fe637bSDr. David Alan Gilbert {
77560fe637bSDr. David Alan Gilbert     uint64_t v;
77660fe637bSDr. David Alan Gilbert     v = (uint64_t)qemu_get_be32(f) << 32;
77760fe637bSDr. David Alan Gilbert     v |= qemu_get_be32(f);
77860fe637bSDr. David Alan Gilbert     return v;
77960fe637bSDr. David Alan Gilbert }
78044f0eadcSLiang Li 
781dcaf446eSXiao Guangrong /* return the size after compression, or negative value on error */
qemu_compress_data(z_stream * stream,uint8_t * dest,size_t dest_len,const uint8_t * source,size_t source_len)782dcaf446eSXiao Guangrong static int qemu_compress_data(z_stream *stream, uint8_t *dest, size_t dest_len,
783dcaf446eSXiao Guangrong                               const uint8_t *source, size_t source_len)
784dcaf446eSXiao Guangrong {
785dcaf446eSXiao Guangrong     int err;
786dcaf446eSXiao Guangrong 
787dcaf446eSXiao Guangrong     err = deflateReset(stream);
788dcaf446eSXiao Guangrong     if (err != Z_OK) {
789dcaf446eSXiao Guangrong         return -1;
790dcaf446eSXiao Guangrong     }
791dcaf446eSXiao Guangrong 
792dcaf446eSXiao Guangrong     stream->avail_in = source_len;
793dcaf446eSXiao Guangrong     stream->next_in = (uint8_t *)source;
794dcaf446eSXiao Guangrong     stream->avail_out = dest_len;
795dcaf446eSXiao Guangrong     stream->next_out = dest;
796dcaf446eSXiao Guangrong 
797dcaf446eSXiao Guangrong     err = deflate(stream, Z_FINISH);
798dcaf446eSXiao Guangrong     if (err != Z_STREAM_END) {
799dcaf446eSXiao Guangrong         return -1;
800dcaf446eSXiao Guangrong     }
801dcaf446eSXiao Guangrong 
802dcaf446eSXiao Guangrong     return stream->next_out - dest;
803dcaf446eSXiao Guangrong }
804dcaf446eSXiao Guangrong 
805dcaf446eSXiao Guangrong /* Compress size bytes of data start at p and store the compressed
806dcaf446eSXiao Guangrong  * data to the buffer of f.
807b3be2896SLiang Li  *
80842d24611SWei Yang  * Since the file is dummy file with empty_ops, return -1 if f has no space to
80942d24611SWei Yang  * save the compressed data.
81044f0eadcSLiang Li  */
qemu_put_compression_data(QEMUFile * f,z_stream * stream,const uint8_t * p,size_t size)811dcaf446eSXiao Guangrong ssize_t qemu_put_compression_data(QEMUFile *f, z_stream *stream,
812dcaf446eSXiao Guangrong                                   const uint8_t *p, size_t size)
81344f0eadcSLiang Li {
81444f0eadcSLiang Li     ssize_t blen = IO_BUF_SIZE - f->buf_index - sizeof(int32_t);
81544f0eadcSLiang Li 
81644f0eadcSLiang Li     if (blen < compressBound(size)) {
817b3be2896SLiang Li         return -1;
818b3be2896SLiang Li     }
819dcaf446eSXiao Guangrong 
820dcaf446eSXiao Guangrong     blen = qemu_compress_data(stream, f->buf + f->buf_index + sizeof(int32_t),
821dcaf446eSXiao Guangrong                               blen, p, size);
822dcaf446eSXiao Guangrong     if (blen < 0) {
82334ab9e97SXiao Guangrong         return -1;
82444f0eadcSLiang Li     }
82534ab9e97SXiao Guangrong 
82644f0eadcSLiang Li     qemu_put_be32(f, blen);
8271bf57fb3SWei Yang     add_buf_to_iovec(f, blen);
82844f0eadcSLiang Li     return blen + sizeof(int32_t);
82944f0eadcSLiang Li }
83044f0eadcSLiang Li 
83144f0eadcSLiang Li /* Put the data in the buffer of f_src to the buffer of f_des, and
83244f0eadcSLiang Li  * then reset the buf_index of f_src to 0.
83344f0eadcSLiang Li  */
83444f0eadcSLiang Li 
qemu_put_qemu_file(QEMUFile * f_des,QEMUFile * f_src)83544f0eadcSLiang Li int qemu_put_qemu_file(QEMUFile *f_des, QEMUFile *f_src)
83644f0eadcSLiang Li {
83744f0eadcSLiang Li     int len = 0;
83844f0eadcSLiang Li 
83944f0eadcSLiang Li     if (f_src->buf_index > 0) {
84044f0eadcSLiang Li         len = f_src->buf_index;
84144f0eadcSLiang Li         qemu_put_buffer(f_des, f_src->buf, f_src->buf_index);
84244f0eadcSLiang Li         f_src->buf_index = 0;
843787d134fSLiang Li         f_src->iovcnt = 0;
84444f0eadcSLiang Li     }
84544f0eadcSLiang Li     return len;
84644f0eadcSLiang Li }
847b3af1bc9SDr. David Alan Gilbert 
848b3af1bc9SDr. David Alan Gilbert /*
8494024cc85SLukas Straub  * Check if the writable buffer is empty
8504024cc85SLukas Straub  */
8514024cc85SLukas Straub 
qemu_file_buffer_empty(QEMUFile * file)8524024cc85SLukas Straub bool qemu_file_buffer_empty(QEMUFile *file)
8534024cc85SLukas Straub {
8544024cc85SLukas Straub     assert(qemu_file_is_writable(file));
8554024cc85SLukas Straub 
8564024cc85SLukas Straub     return !file->iovcnt;
8574024cc85SLukas Straub }
8584024cc85SLukas Straub 
8594024cc85SLukas Straub /*
860b3af1bc9SDr. David Alan Gilbert  * Get a string whose length is determined by a single preceding byte
861b3af1bc9SDr. David Alan Gilbert  * A preallocated 256 byte buffer must be passed in.
862b3af1bc9SDr. David Alan Gilbert  * Returns: len on success and a 0 terminated string in the buffer
863b3af1bc9SDr. David Alan Gilbert  *          else 0
864b3af1bc9SDr. David Alan Gilbert  *          (Note a 0 length string will return 0 either way)
865b3af1bc9SDr. David Alan Gilbert  */
qemu_get_counted_string(QEMUFile * f,char buf[256])866394b9407SPaolo Bonzini size_t coroutine_fn qemu_get_counted_string(QEMUFile *f, char buf[256])
867b3af1bc9SDr. David Alan Gilbert {
868b3af1bc9SDr. David Alan Gilbert     size_t len = qemu_get_byte(f);
869b3af1bc9SDr. David Alan Gilbert     size_t res = qemu_get_buffer(f, (uint8_t *)buf, len);
870b3af1bc9SDr. David Alan Gilbert 
871b3af1bc9SDr. David Alan Gilbert     buf[res] = 0;
872b3af1bc9SDr. David Alan Gilbert 
873b3af1bc9SDr. David Alan Gilbert     return res == len ? res : 0;
874b3af1bc9SDr. David Alan Gilbert }
875a800cd5cSDr. David Alan Gilbert 
876a800cd5cSDr. David Alan Gilbert /*
877f0d64cb7SVladimir Sementsov-Ogievskiy  * Put a string with one preceding byte containing its length. The length of
878f0d64cb7SVladimir Sementsov-Ogievskiy  * the string should be less than 256.
879f0d64cb7SVladimir Sementsov-Ogievskiy  */
qemu_put_counted_string(QEMUFile * f,const char * str)880f0d64cb7SVladimir Sementsov-Ogievskiy void qemu_put_counted_string(QEMUFile *f, const char *str)
881f0d64cb7SVladimir Sementsov-Ogievskiy {
882f0d64cb7SVladimir Sementsov-Ogievskiy     size_t len = strlen(str);
883f0d64cb7SVladimir Sementsov-Ogievskiy 
884f0d64cb7SVladimir Sementsov-Ogievskiy     assert(len < 256);
885f0d64cb7SVladimir Sementsov-Ogievskiy     qemu_put_byte(f, len);
886f0d64cb7SVladimir Sementsov-Ogievskiy     qemu_put_buffer(f, (const uint8_t *)str, len);
887f0d64cb7SVladimir Sementsov-Ogievskiy }
888f0d64cb7SVladimir Sementsov-Ogievskiy 
889f0d64cb7SVladimir Sementsov-Ogievskiy /*
890a800cd5cSDr. David Alan Gilbert  * Set the blocking state of the QEMUFile.
891a800cd5cSDr. David Alan Gilbert  * Note: On some transports the OS only keeps a single blocking state for
892a800cd5cSDr. David Alan Gilbert  *       both directions, and thus changing the blocking on the main
893a800cd5cSDr. David Alan Gilbert  *       QEMUFile can also affect the return path.
894a800cd5cSDr. David Alan Gilbert  */
qemu_file_set_blocking(QEMUFile * f,bool block)895a800cd5cSDr. David Alan Gilbert void qemu_file_set_blocking(QEMUFile *f, bool block)
896a800cd5cSDr. David Alan Gilbert {
89780ad9706SDaniel P. Berrangé     qio_channel_set_blocking(f->ioc, block, NULL);
89806ad5135SDaniel P. Berrange }
899c6ad5be7SPeter Xu 
900c6ad5be7SPeter Xu /*
9012893a288SDaniel P. Berrangé  * qemu_file_get_ioc:
9022893a288SDaniel P. Berrangé  *
9032893a288SDaniel P. Berrangé  * Get the ioc object for the file, without incrementing
9042893a288SDaniel P. Berrangé  * the reference count.
9052893a288SDaniel P. Berrangé  *
9062893a288SDaniel P. Berrangé  * Returns: the ioc object
907c6ad5be7SPeter Xu  */
qemu_file_get_ioc(QEMUFile * file)908c6ad5be7SPeter Xu QIOChannel *qemu_file_get_ioc(QEMUFile *file)
909c6ad5be7SPeter Xu {
9102893a288SDaniel P. Berrangé     return file->ioc;
911c6ad5be7SPeter Xu }
912c7a7db4bSAvihai Horon 
913c7a7db4bSAvihai Horon /*
914c7a7db4bSAvihai Horon  * Read size bytes from QEMUFile f and write them to fd.
915c7a7db4bSAvihai Horon  */
qemu_file_get_to_fd(QEMUFile * f,int fd,size_t size)916c7a7db4bSAvihai Horon int qemu_file_get_to_fd(QEMUFile *f, int fd, size_t size)
917c7a7db4bSAvihai Horon {
918c7a7db4bSAvihai Horon     while (size) {
919c7a7db4bSAvihai Horon         size_t pending = f->buf_size - f->buf_index;
920c7a7db4bSAvihai Horon         ssize_t rc;
921c7a7db4bSAvihai Horon 
922c7a7db4bSAvihai Horon         if (!pending) {
923c7a7db4bSAvihai Horon             rc = qemu_fill_buffer(f);
924c7a7db4bSAvihai Horon             if (rc < 0) {
925c7a7db4bSAvihai Horon                 return rc;
926c7a7db4bSAvihai Horon             }
927c7a7db4bSAvihai Horon             if (rc == 0) {
928c7a7db4bSAvihai Horon                 return -EIO;
929c7a7db4bSAvihai Horon             }
930c7a7db4bSAvihai Horon             continue;
931c7a7db4bSAvihai Horon         }
932c7a7db4bSAvihai Horon 
933c7a7db4bSAvihai Horon         rc = write(fd, f->buf + f->buf_index, MIN(pending, size));
934c7a7db4bSAvihai Horon         if (rc < 0) {
935c7a7db4bSAvihai Horon             return -errno;
936c7a7db4bSAvihai Horon         }
937c7a7db4bSAvihai Horon         if (rc == 0) {
938c7a7db4bSAvihai Horon             return -EIO;
939c7a7db4bSAvihai Horon         }
940c7a7db4bSAvihai Horon         f->buf_index += rc;
941c7a7db4bSAvihai Horon         size -= rc;
942c7a7db4bSAvihai Horon     }
943c7a7db4bSAvihai Horon 
944c7a7db4bSAvihai Horon     return 0;
945c7a7db4bSAvihai Horon }
946