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 QEMUFileHooks *hooks; 392893a288SDaniel P. Berrangé QIOChannel *ioc; 40c0c6e1e2SDaniel P. Berrangé bool is_writable; 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 53154d87b4SDaniel P. Berrangé /* The sum of bytes transferred on the wire */ 5461abf1ebSJuan Quintela uint64_t total_transferred; 55154d87b4SDaniel P. Berrangé 56a24939f2SDaniel P. Berrange int buf_index; 57a24939f2SDaniel P. Berrange int buf_size; /* 0 when writing */ 58a24939f2SDaniel P. Berrange uint8_t buf[IO_BUF_SIZE]; 59a24939f2SDaniel P. Berrange 6053f09a10SPavel Butsykin DECLARE_BITMAP(may_free, MAX_IOV_SIZE); 61a24939f2SDaniel P. Berrange struct iovec iov[MAX_IOV_SIZE]; 62a24939f2SDaniel P. Berrange unsigned int iovcnt; 63a24939f2SDaniel P. Berrange 64a24939f2SDaniel P. Berrange int last_error; 653d661c8aSYury Kotov Error *last_error_obj; 66a24939f2SDaniel P. Berrange }; 67a24939f2SDaniel P. Berrange 68e1a8c9b6SDr. David Alan Gilbert /* 69e1a8c9b6SDr. David Alan Gilbert * Stop a file from being read/written - not all backing files can do this 70e1a8c9b6SDr. David Alan Gilbert * typically only sockets can. 71d3c581b7SDaniel P. Berrangé * 72d3c581b7SDaniel P. Berrangé * TODO: convert to propagate Error objects instead of squashing 73d3c581b7SDaniel P. Berrangé * to a fixed errno value 74e1a8c9b6SDr. David Alan Gilbert */ 75e1a8c9b6SDr. David Alan Gilbert int qemu_file_shutdown(QEMUFile *f) 76e1a8c9b6SDr. David Alan Gilbert { 77d3c581b7SDaniel P. Berrangé int ret = 0; 78a555b809SJuan Quintela 79f5816b5cSPeter Xu /* 80f5816b5cSPeter Xu * We must set qemufile error before the real shutdown(), otherwise 81f5816b5cSPeter Xu * there can be a race window where we thought IO all went though 82f5816b5cSPeter Xu * (because last_error==NULL) but actually IO has already stopped. 83f5816b5cSPeter Xu * 84f5816b5cSPeter Xu * If without correct ordering, the race can happen like this: 85f5816b5cSPeter Xu * 86f5816b5cSPeter Xu * page receiver other thread 87f5816b5cSPeter Xu * ------------- ------------ 88f5816b5cSPeter Xu * qemu_get_buffer() 89f5816b5cSPeter Xu * do shutdown() 90f5816b5cSPeter Xu * returns 0 (buffer all zero) 91f5816b5cSPeter Xu * (we didn't check this retcode) 92f5816b5cSPeter Xu * try to detect IO error 93f5816b5cSPeter Xu * last_error==NULL, IO okay 94f5816b5cSPeter Xu * install ALL-ZERO page 95f5816b5cSPeter Xu * set last_error 96f5816b5cSPeter Xu * --> guest crash! 97f5816b5cSPeter Xu */ 98f5816b5cSPeter Xu if (!f->last_error) { 99f5816b5cSPeter Xu qemu_file_set_error(f, -EIO); 100f5816b5cSPeter Xu } 101f5816b5cSPeter Xu 102d3c581b7SDaniel P. Berrangé if (!qio_channel_has_feature(f->ioc, 103d3c581b7SDaniel P. Berrangé QIO_CHANNEL_FEATURE_SHUTDOWN)) { 104e1a8c9b6SDr. David Alan Gilbert return -ENOSYS; 105e1a8c9b6SDr. David Alan Gilbert } 106d3c581b7SDaniel P. Berrangé 107d3c581b7SDaniel P. Berrangé if (qio_channel_shutdown(f->ioc, QIO_CHANNEL_SHUTDOWN_BOTH, NULL) < 0) { 108d3c581b7SDaniel P. Berrangé ret = -EIO; 109d3c581b7SDaniel P. Berrangé } 110a555b809SJuan Quintela 111a555b809SJuan Quintela return ret; 112e1a8c9b6SDr. David Alan Gilbert } 113e1a8c9b6SDr. David Alan Gilbert 11460fe637bSDr. David Alan Gilbert bool qemu_file_mode_is_not_valid(const char *mode) 11560fe637bSDr. David Alan Gilbert { 11660fe637bSDr. David Alan Gilbert if (mode == NULL || 11760fe637bSDr. David Alan Gilbert (mode[0] != 'r' && mode[0] != 'w') || 11860fe637bSDr. David Alan Gilbert mode[1] != 'b' || mode[2] != 0) { 11960fe637bSDr. David Alan Gilbert fprintf(stderr, "qemu_fopen: Argument validity check failed\n"); 12060fe637bSDr. David Alan Gilbert return true; 12160fe637bSDr. David Alan Gilbert } 12260fe637bSDr. David Alan Gilbert 12360fe637bSDr. David Alan Gilbert return false; 12460fe637bSDr. David Alan Gilbert } 12560fe637bSDr. David Alan Gilbert 12677ef2dc1SDaniel P. Berrangé static QEMUFile *qemu_file_new_impl(QIOChannel *ioc, bool is_writable) 12760fe637bSDr. David Alan Gilbert { 12860fe637bSDr. David Alan Gilbert QEMUFile *f; 12960fe637bSDr. David Alan Gilbert 13097f3ad35SMarkus Armbruster f = g_new0(QEMUFile, 1); 13160fe637bSDr. David Alan Gilbert 13277ef2dc1SDaniel P. Berrangé object_ref(ioc); 1332893a288SDaniel P. Berrangé f->ioc = ioc; 134c0c6e1e2SDaniel P. Berrangé f->is_writable = is_writable; 1352893a288SDaniel P. Berrangé 13660fe637bSDr. David Alan Gilbert return f; 13760fe637bSDr. David Alan Gilbert } 13860fe637bSDr. David Alan Gilbert 13902bdbe17SDaniel P. Berrangé /* 14002bdbe17SDaniel P. Berrangé * Result: QEMUFile* for a 'return path' for comms in the opposite direction 14102bdbe17SDaniel P. Berrangé * NULL if not available 14202bdbe17SDaniel P. Berrangé */ 14302bdbe17SDaniel P. Berrangé QEMUFile *qemu_file_get_return_path(QEMUFile *f) 14402bdbe17SDaniel P. Berrangé { 14577ef2dc1SDaniel P. Berrangé return qemu_file_new_impl(f->ioc, !f->is_writable); 14602bdbe17SDaniel P. Berrangé } 14702bdbe17SDaniel P. Berrangé 14877ef2dc1SDaniel P. Berrangé QEMUFile *qemu_file_new_output(QIOChannel *ioc) 149c0c6e1e2SDaniel P. Berrangé { 15077ef2dc1SDaniel P. Berrangé return qemu_file_new_impl(ioc, true); 151c0c6e1e2SDaniel P. Berrangé } 152c0c6e1e2SDaniel P. Berrangé 15377ef2dc1SDaniel P. Berrangé QEMUFile *qemu_file_new_input(QIOChannel *ioc) 154c0c6e1e2SDaniel P. Berrangé { 15577ef2dc1SDaniel P. Berrangé return qemu_file_new_impl(ioc, false); 156c0c6e1e2SDaniel P. Berrangé } 157c0c6e1e2SDaniel P. Berrangé 1580436e09fSDaniel P. Berrange void qemu_file_set_hooks(QEMUFile *f, const QEMUFileHooks *hooks) 1590436e09fSDaniel P. Berrange { 1600436e09fSDaniel P. Berrange f->hooks = hooks; 1610436e09fSDaniel P. Berrange } 1620436e09fSDaniel P. Berrange 16360fe637bSDr. David Alan Gilbert /* 1643d661c8aSYury Kotov * Get last error for stream f with optional Error* 1653d661c8aSYury Kotov * 1663d661c8aSYury Kotov * Return negative error value if there has been an error on previous 1673d661c8aSYury Kotov * operations, return 0 if no error happened. 1683d661c8aSYury Kotov * Optional, it returns Error* in errp, but it may be NULL even if return value 1693d661c8aSYury Kotov * is not 0. 1703d661c8aSYury Kotov * 1713d661c8aSYury Kotov */ 1723d661c8aSYury Kotov int qemu_file_get_error_obj(QEMUFile *f, Error **errp) 1733d661c8aSYury Kotov { 1743d661c8aSYury Kotov if (errp) { 1753d661c8aSYury Kotov *errp = f->last_error_obj ? error_copy(f->last_error_obj) : NULL; 1763d661c8aSYury Kotov } 1773d661c8aSYury Kotov return f->last_error; 1783d661c8aSYury Kotov } 1793d661c8aSYury Kotov 1803d661c8aSYury Kotov /* 18160bb3c58SPeter Xu * Get last error for either stream f1 or f2 with optional Error*. 18260bb3c58SPeter Xu * The error returned (non-zero) can be either from f1 or f2. 18360bb3c58SPeter Xu * 18460bb3c58SPeter Xu * If any of the qemufile* is NULL, then skip the check on that file. 18560bb3c58SPeter Xu * 18660bb3c58SPeter Xu * When there is no error on both qemufile, zero is returned. 18760bb3c58SPeter Xu */ 18860bb3c58SPeter Xu int qemu_file_get_error_obj_any(QEMUFile *f1, QEMUFile *f2, Error **errp) 18960bb3c58SPeter Xu { 19060bb3c58SPeter Xu int ret = 0; 19160bb3c58SPeter Xu 19260bb3c58SPeter Xu if (f1) { 19360bb3c58SPeter Xu ret = qemu_file_get_error_obj(f1, errp); 19460bb3c58SPeter Xu /* If there's already error detected, return */ 19560bb3c58SPeter Xu if (ret) { 19660bb3c58SPeter Xu return ret; 19760bb3c58SPeter Xu } 19860bb3c58SPeter Xu } 19960bb3c58SPeter Xu 20060bb3c58SPeter Xu if (f2) { 20160bb3c58SPeter Xu ret = qemu_file_get_error_obj(f2, errp); 20260bb3c58SPeter Xu } 20360bb3c58SPeter Xu 20460bb3c58SPeter Xu return ret; 20560bb3c58SPeter Xu } 20660bb3c58SPeter Xu 20760bb3c58SPeter Xu /* 2083d661c8aSYury Kotov * Set the last error for stream f with optional Error* 2093d661c8aSYury Kotov */ 2103d661c8aSYury Kotov void qemu_file_set_error_obj(QEMUFile *f, int ret, Error *err) 2113d661c8aSYury Kotov { 2123d661c8aSYury Kotov if (f->last_error == 0 && ret) { 2133d661c8aSYury Kotov f->last_error = ret; 2143d661c8aSYury Kotov error_propagate(&f->last_error_obj, err); 2153d661c8aSYury Kotov } else if (err) { 2163d661c8aSYury Kotov error_report_err(err); 2173d661c8aSYury Kotov } 2183d661c8aSYury Kotov } 2193d661c8aSYury Kotov 2203d661c8aSYury Kotov /* 22160fe637bSDr. David Alan Gilbert * Get last error for stream f 22260fe637bSDr. David Alan Gilbert * 22360fe637bSDr. David Alan Gilbert * Return negative error value if there has been an error on previous 22460fe637bSDr. David Alan Gilbert * operations, return 0 if no error happened. 22560fe637bSDr. David Alan Gilbert * 22660fe637bSDr. David Alan Gilbert */ 22760fe637bSDr. David Alan Gilbert int qemu_file_get_error(QEMUFile *f) 22860fe637bSDr. David Alan Gilbert { 2293d661c8aSYury Kotov return qemu_file_get_error_obj(f, NULL); 23060fe637bSDr. David Alan Gilbert } 23160fe637bSDr. David Alan Gilbert 2323d661c8aSYury Kotov /* 2333d661c8aSYury Kotov * Set the last error for stream f 2343d661c8aSYury Kotov */ 23560fe637bSDr. David Alan Gilbert void qemu_file_set_error(QEMUFile *f, int ret) 23660fe637bSDr. David Alan Gilbert { 2373d661c8aSYury Kotov qemu_file_set_error_obj(f, ret, NULL); 23860fe637bSDr. David Alan Gilbert } 23960fe637bSDr. David Alan Gilbert 24060fe637bSDr. David Alan Gilbert bool qemu_file_is_writable(QEMUFile *f) 24160fe637bSDr. David Alan Gilbert { 242c0c6e1e2SDaniel P. Berrangé return f->is_writable; 24360fe637bSDr. David Alan Gilbert } 24460fe637bSDr. David Alan Gilbert 24553f09a10SPavel Butsykin static void qemu_iovec_release_ram(QEMUFile *f) 24653f09a10SPavel Butsykin { 24753f09a10SPavel Butsykin struct iovec iov; 24853f09a10SPavel Butsykin unsigned long idx; 24953f09a10SPavel Butsykin 25053f09a10SPavel Butsykin /* Find and release all the contiguous memory ranges marked as may_free. */ 25153f09a10SPavel Butsykin idx = find_next_bit(f->may_free, f->iovcnt, 0); 25253f09a10SPavel Butsykin if (idx >= f->iovcnt) { 25353f09a10SPavel Butsykin return; 25453f09a10SPavel Butsykin } 25553f09a10SPavel Butsykin iov = f->iov[idx]; 25653f09a10SPavel Butsykin 25753f09a10SPavel Butsykin /* The madvise() in the loop is called for iov within a continuous range and 25853f09a10SPavel Butsykin * then reinitialize the iov. And in the end, madvise() is called for the 25953f09a10SPavel Butsykin * last iov. 26053f09a10SPavel Butsykin */ 26153f09a10SPavel Butsykin while ((idx = find_next_bit(f->may_free, f->iovcnt, idx + 1)) < f->iovcnt) { 26253f09a10SPavel Butsykin /* check for adjacent buffer and coalesce them */ 26353f09a10SPavel Butsykin if (iov.iov_base + iov.iov_len == f->iov[idx].iov_base) { 26453f09a10SPavel Butsykin iov.iov_len += f->iov[idx].iov_len; 26553f09a10SPavel Butsykin continue; 26653f09a10SPavel Butsykin } 26753f09a10SPavel Butsykin if (qemu_madvise(iov.iov_base, iov.iov_len, QEMU_MADV_DONTNEED) < 0) { 26853f09a10SPavel Butsykin error_report("migrate: madvise DONTNEED failed %p %zd: %s", 26953f09a10SPavel Butsykin iov.iov_base, iov.iov_len, strerror(errno)); 27053f09a10SPavel Butsykin } 27153f09a10SPavel Butsykin iov = f->iov[idx]; 27253f09a10SPavel Butsykin } 27353f09a10SPavel Butsykin if (qemu_madvise(iov.iov_base, iov.iov_len, QEMU_MADV_DONTNEED) < 0) { 27453f09a10SPavel Butsykin error_report("migrate: madvise DONTNEED failed %p %zd: %s", 27553f09a10SPavel Butsykin iov.iov_base, iov.iov_len, strerror(errno)); 27653f09a10SPavel Butsykin } 27753f09a10SPavel Butsykin memset(f->may_free, 0, sizeof(f->may_free)); 27853f09a10SPavel Butsykin } 27953f09a10SPavel Butsykin 28077ef2dc1SDaniel P. Berrangé 28160fe637bSDr. David Alan Gilbert /** 28260fe637bSDr. David Alan Gilbert * Flushes QEMUFile buffer 28360fe637bSDr. David Alan Gilbert * 2843b348706SDr. David Alan Gilbert * This will flush all pending data. If data was only partially flushed, it 2853b348706SDr. David Alan Gilbert * will set an error state. 28660fe637bSDr. David Alan Gilbert */ 28760fe637bSDr. David Alan Gilbert void qemu_fflush(QEMUFile *f) 28860fe637bSDr. David Alan Gilbert { 28960fe637bSDr. David Alan Gilbert if (!qemu_file_is_writable(f)) { 29060fe637bSDr. David Alan Gilbert return; 29160fe637bSDr. David Alan Gilbert } 29260fe637bSDr. David Alan Gilbert 293ac7d25b8SJuan Quintela if (qemu_file_get_error(f)) { 294a555b809SJuan Quintela return; 295a555b809SJuan Quintela } 29660fe637bSDr. David Alan Gilbert if (f->iovcnt > 0) { 297ec2135eeSDaniel P. Berrangé Error *local_error = NULL; 298ec2135eeSDaniel P. Berrangé if (qio_channel_writev_all(f->ioc, 299ec2135eeSDaniel P. Berrangé f->iov, f->iovcnt, 300ec2135eeSDaniel P. Berrangé &local_error) < 0) { 301ec2135eeSDaniel P. Berrangé qemu_file_set_error_obj(f, -EIO, local_error); 302ec2135eeSDaniel P. Berrangé } else { 303ec2135eeSDaniel P. Berrangé f->total_transferred += iov_size(f->iov, f->iovcnt); 304ec2135eeSDaniel P. Berrangé } 30553f09a10SPavel Butsykin 30653f09a10SPavel Butsykin qemu_iovec_release_ram(f); 30760fe637bSDr. David Alan Gilbert } 308baf51e77SDaniel P. Berrange 30960fe637bSDr. David Alan Gilbert f->buf_index = 0; 31060fe637bSDr. David Alan Gilbert f->iovcnt = 0; 31160fe637bSDr. David Alan Gilbert } 31260fe637bSDr. David Alan Gilbert 31360fe637bSDr. David Alan Gilbert void ram_control_before_iterate(QEMUFile *f, uint64_t flags) 31460fe637bSDr. David Alan Gilbert { 31560fe637bSDr. David Alan Gilbert int ret = 0; 31660fe637bSDr. David Alan Gilbert 3170436e09fSDaniel P. Berrange if (f->hooks && f->hooks->before_ram_iterate) { 318365c0463SDaniel P. Berrangé ret = f->hooks->before_ram_iterate(f, flags, NULL); 31960fe637bSDr. David Alan Gilbert if (ret < 0) { 32060fe637bSDr. David Alan Gilbert qemu_file_set_error(f, ret); 32160fe637bSDr. David Alan Gilbert } 32260fe637bSDr. David Alan Gilbert } 32360fe637bSDr. David Alan Gilbert } 32460fe637bSDr. David Alan Gilbert 32560fe637bSDr. David Alan Gilbert void ram_control_after_iterate(QEMUFile *f, uint64_t flags) 32660fe637bSDr. David Alan Gilbert { 32760fe637bSDr. David Alan Gilbert int ret = 0; 32860fe637bSDr. David Alan Gilbert 3290436e09fSDaniel P. Berrange if (f->hooks && f->hooks->after_ram_iterate) { 330365c0463SDaniel P. Berrangé ret = f->hooks->after_ram_iterate(f, flags, NULL); 33160fe637bSDr. David Alan Gilbert if (ret < 0) { 33260fe637bSDr. David Alan Gilbert qemu_file_set_error(f, ret); 33360fe637bSDr. David Alan Gilbert } 33460fe637bSDr. David Alan Gilbert } 33560fe637bSDr. David Alan Gilbert } 33660fe637bSDr. David Alan Gilbert 337632e3a5cSDr. David Alan Gilbert void ram_control_load_hook(QEMUFile *f, uint64_t flags, void *data) 33860fe637bSDr. David Alan Gilbert { 3390436e09fSDaniel P. Berrange if (f->hooks && f->hooks->hook_ram_load) { 340cf7fe0c5SJuan Quintela int ret = f->hooks->hook_ram_load(f, flags, data); 34160fe637bSDr. David Alan Gilbert if (ret < 0) { 34260fe637bSDr. David Alan Gilbert qemu_file_set_error(f, ret); 34360fe637bSDr. David Alan Gilbert } 34460fe637bSDr. David Alan Gilbert } 345632e3a5cSDr. David Alan Gilbert } 34660fe637bSDr. David Alan Gilbert 34760fe637bSDr. David Alan Gilbert size_t ram_control_save_page(QEMUFile *f, ram_addr_t block_offset, 3486e1dea46SJuan Quintela ram_addr_t offset, size_t size, 3496e1dea46SJuan Quintela uint64_t *bytes_sent) 35060fe637bSDr. David Alan Gilbert { 3510436e09fSDaniel P. Berrange if (f->hooks && f->hooks->save_page) { 352365c0463SDaniel P. Berrangé int ret = f->hooks->save_page(f, block_offset, 35360fe637bSDr. David Alan Gilbert offset, size, bytes_sent); 354ccb7e1b5SLidong Chen if (ret != RAM_SAVE_CONTROL_NOT_SUPP) { 355fae4009fSJuan Quintela qemu_file_acct_rate_limit(f, size); 356ccb7e1b5SLidong Chen } 357ccb7e1b5SLidong Chen 358ccb7e1b5SLidong Chen if (ret != RAM_SAVE_CONTROL_DELAYED && 359ccb7e1b5SLidong Chen ret != RAM_SAVE_CONTROL_NOT_SUPP) { 36060fe637bSDr. David Alan Gilbert if (bytes_sent && *bytes_sent > 0) { 3611a93bd2fSDaniel P. Berrangé qemu_file_credit_transfer(f, *bytes_sent); 36260fe637bSDr. David Alan Gilbert } else if (ret < 0) { 36360fe637bSDr. David Alan Gilbert qemu_file_set_error(f, ret); 36460fe637bSDr. David Alan Gilbert } 36560fe637bSDr. David Alan Gilbert } 36660fe637bSDr. David Alan Gilbert 36760fe637bSDr. David Alan Gilbert return ret; 36860fe637bSDr. David Alan Gilbert } 36960fe637bSDr. David Alan Gilbert 37060fe637bSDr. David Alan Gilbert return RAM_SAVE_CONTROL_NOT_SUPP; 37160fe637bSDr. David Alan Gilbert } 37260fe637bSDr. David Alan Gilbert 37360fe637bSDr. David Alan Gilbert /* 37460fe637bSDr. David Alan Gilbert * Attempt to fill the buffer from the underlying file 37560fe637bSDr. David Alan Gilbert * Returns the number of bytes read, or negative value for an error. 37660fe637bSDr. David Alan Gilbert * 37760fe637bSDr. David Alan Gilbert * Note that it can return a partially full buffer even in a not error/not EOF 37860fe637bSDr. David Alan Gilbert * case if the underlying file descriptor gives a short read, and that can 37960fe637bSDr. David Alan Gilbert * happen even on a blocking fd. 38060fe637bSDr. David Alan Gilbert */ 381394b9407SPaolo Bonzini static ssize_t coroutine_mixed_fn qemu_fill_buffer(QEMUFile *f) 38260fe637bSDr. David Alan Gilbert { 38360fe637bSDr. David Alan Gilbert int len; 38460fe637bSDr. David Alan Gilbert int pending; 3853d661c8aSYury Kotov Error *local_error = NULL; 38660fe637bSDr. David Alan Gilbert 38760fe637bSDr. David Alan Gilbert assert(!qemu_file_is_writable(f)); 38860fe637bSDr. David Alan Gilbert 38960fe637bSDr. David Alan Gilbert pending = f->buf_size - f->buf_index; 39060fe637bSDr. David Alan Gilbert if (pending > 0) { 39160fe637bSDr. David Alan Gilbert memmove(f->buf, f->buf + f->buf_index, pending); 39260fe637bSDr. David Alan Gilbert } 39360fe637bSDr. David Alan Gilbert f->buf_index = 0; 39460fe637bSDr. David Alan Gilbert f->buf_size = pending; 39560fe637bSDr. David Alan Gilbert 396ac7d25b8SJuan Quintela if (qemu_file_get_error(f)) { 397a555b809SJuan Quintela return 0; 398a555b809SJuan Quintela } 399a555b809SJuan Quintela 400f759d705SDaniel P. Berrangé do { 401f759d705SDaniel P. Berrangé len = qio_channel_read(f->ioc, 402f759d705SDaniel P. Berrangé (char *)f->buf + pending, 403f759d705SDaniel P. Berrangé IO_BUF_SIZE - pending, 404f759d705SDaniel P. Berrangé &local_error); 405f759d705SDaniel P. Berrangé if (len == QIO_CHANNEL_ERR_BLOCK) { 406f759d705SDaniel P. Berrangé if (qemu_in_coroutine()) { 407f759d705SDaniel P. Berrangé qio_channel_yield(f->ioc, G_IO_IN); 408f759d705SDaniel P. Berrangé } else { 409f759d705SDaniel P. Berrangé qio_channel_wait(f->ioc, G_IO_IN); 410f759d705SDaniel P. Berrangé } 411f759d705SDaniel P. Berrangé } else if (len < 0) { 412f759d705SDaniel P. Berrangé len = -EIO; 413f759d705SDaniel P. Berrangé } 414f759d705SDaniel P. Berrangé } while (len == QIO_CHANNEL_ERR_BLOCK); 415f759d705SDaniel P. Berrangé 41660fe637bSDr. David Alan Gilbert if (len > 0) { 41760fe637bSDr. David Alan Gilbert f->buf_size += len; 418154d87b4SDaniel P. Berrangé f->total_transferred += len; 41960fe637bSDr. David Alan Gilbert } else if (len == 0) { 4203d661c8aSYury Kotov qemu_file_set_error_obj(f, -EIO, local_error); 4213d661c8aSYury Kotov } else { 4225f87072eSDaniel P. Berrangé qemu_file_set_error_obj(f, len, local_error); 42360fe637bSDr. David Alan Gilbert } 42460fe637bSDr. David Alan Gilbert 42560fe637bSDr. David Alan Gilbert return len; 42660fe637bSDr. David Alan Gilbert } 42760fe637bSDr. David Alan Gilbert 4281a93bd2fSDaniel P. Berrangé void qemu_file_credit_transfer(QEMUFile *f, size_t size) 42960fe637bSDr. David Alan Gilbert { 430154d87b4SDaniel P. Berrangé f->total_transferred += size; 43160fe637bSDr. David Alan Gilbert } 43260fe637bSDr. David Alan Gilbert 43360fe637bSDr. David Alan Gilbert /** Closes the file 43460fe637bSDr. David Alan Gilbert * 43560fe637bSDr. David Alan Gilbert * Returns negative error value if any error happened on previous operations or 43660fe637bSDr. David Alan Gilbert * while closing the file. Returns 0 or positive number on success. 43760fe637bSDr. David Alan Gilbert * 43860fe637bSDr. David Alan Gilbert * The meaning of return value on success depends on the specific backend 43960fe637bSDr. David Alan Gilbert * being used. 44060fe637bSDr. David Alan Gilbert */ 44160fe637bSDr. David Alan Gilbert int qemu_fclose(QEMUFile *f) 44260fe637bSDr. David Alan Gilbert { 4430ae1f7f0SDaniel P. Berrangé int ret, ret2; 44460fe637bSDr. David Alan Gilbert qemu_fflush(f); 44560fe637bSDr. David Alan Gilbert ret = qemu_file_get_error(f); 44660fe637bSDr. David Alan Gilbert 4470ae1f7f0SDaniel P. Berrangé ret2 = qio_channel_close(f->ioc, NULL); 44860fe637bSDr. David Alan Gilbert if (ret >= 0) { 44960fe637bSDr. David Alan Gilbert ret = ret2; 45060fe637bSDr. David Alan Gilbert } 4510ae1f7f0SDaniel P. Berrangé g_clear_pointer(&f->ioc, object_unref); 4520ae1f7f0SDaniel P. Berrangé 45360fe637bSDr. David Alan Gilbert /* If any error was spotted before closing, we should report it 45460fe637bSDr. David Alan Gilbert * instead of the close() return value. 45560fe637bSDr. David Alan Gilbert */ 45660fe637bSDr. David Alan Gilbert if (f->last_error) { 45760fe637bSDr. David Alan Gilbert ret = f->last_error; 45860fe637bSDr. David Alan Gilbert } 4593d661c8aSYury Kotov error_free(f->last_error_obj); 46060fe637bSDr. David Alan Gilbert g_free(f); 46160fe637bSDr. David Alan Gilbert trace_qemu_file_fclose(); 46260fe637bSDr. David Alan Gilbert return ret; 46360fe637bSDr. David Alan Gilbert } 46460fe637bSDr. David Alan Gilbert 4651bf57fb3SWei Yang /* 4661bf57fb3SWei Yang * Add buf to iovec. Do flush if iovec is full. 4671bf57fb3SWei Yang * 4681bf57fb3SWei Yang * Return values: 4691bf57fb3SWei Yang * 1 iovec is full and flushed 4701bf57fb3SWei Yang * 0 iovec is not flushed 4711bf57fb3SWei Yang * 4721bf57fb3SWei Yang */ 4731bf57fb3SWei Yang static int add_to_iovec(QEMUFile *f, const uint8_t *buf, size_t size, 47453f09a10SPavel Butsykin bool may_free) 47560fe637bSDr. David Alan Gilbert { 47660fe637bSDr. David Alan Gilbert /* check for adjacent buffer and coalesce them */ 47760fe637bSDr. David Alan Gilbert if (f->iovcnt > 0 && buf == f->iov[f->iovcnt - 1].iov_base + 47853f09a10SPavel Butsykin f->iov[f->iovcnt - 1].iov_len && 47953f09a10SPavel Butsykin may_free == test_bit(f->iovcnt - 1, f->may_free)) 48053f09a10SPavel Butsykin { 48160fe637bSDr. David Alan Gilbert f->iov[f->iovcnt - 1].iov_len += size; 48260fe637bSDr. David Alan Gilbert } else { 483c00d434aSFeng Lin if (f->iovcnt >= MAX_IOV_SIZE) { 484c00d434aSFeng Lin /* Should only happen if a previous fflush failed */ 485ac7d25b8SJuan Quintela assert(qemu_file_get_error(f) || !qemu_file_is_writable(f)); 486c00d434aSFeng Lin return 1; 487c00d434aSFeng Lin } 48853f09a10SPavel Butsykin if (may_free) { 48953f09a10SPavel Butsykin set_bit(f->iovcnt, f->may_free); 49053f09a10SPavel Butsykin } 49160fe637bSDr. David Alan Gilbert f->iov[f->iovcnt].iov_base = (uint8_t *)buf; 49260fe637bSDr. David Alan Gilbert f->iov[f->iovcnt++].iov_len = size; 49360fe637bSDr. David Alan Gilbert } 49460fe637bSDr. David Alan Gilbert 49560fe637bSDr. David Alan Gilbert if (f->iovcnt >= MAX_IOV_SIZE) { 49660fe637bSDr. David Alan Gilbert qemu_fflush(f); 4971bf57fb3SWei Yang return 1; 4981bf57fb3SWei Yang } 4991bf57fb3SWei Yang 5001bf57fb3SWei Yang return 0; 5011bf57fb3SWei Yang } 5021bf57fb3SWei Yang 5031bf57fb3SWei Yang static void add_buf_to_iovec(QEMUFile *f, size_t len) 5041bf57fb3SWei Yang { 5051bf57fb3SWei Yang if (!add_to_iovec(f, f->buf + f->buf_index, len, false)) { 5061bf57fb3SWei Yang f->buf_index += len; 5071bf57fb3SWei Yang if (f->buf_index == IO_BUF_SIZE) { 5081bf57fb3SWei Yang qemu_fflush(f); 5091bf57fb3SWei Yang } 51060fe637bSDr. David Alan Gilbert } 51160fe637bSDr. David Alan Gilbert } 51260fe637bSDr. David Alan Gilbert 51353f09a10SPavel Butsykin void qemu_put_buffer_async(QEMUFile *f, const uint8_t *buf, size_t size, 51453f09a10SPavel Butsykin bool may_free) 51560fe637bSDr. David Alan Gilbert { 51660fe637bSDr. David Alan Gilbert if (f->last_error) { 51760fe637bSDr. David Alan Gilbert return; 51860fe637bSDr. David Alan Gilbert } 51960fe637bSDr. David Alan Gilbert 520c7fc8d32SDaniel P. Berrangé f->rate_limit_used += size; 52153f09a10SPavel Butsykin add_to_iovec(f, buf, size, may_free); 52260fe637bSDr. David Alan Gilbert } 52360fe637bSDr. David Alan Gilbert 52456f3835fSDr. David Alan Gilbert void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, size_t size) 52560fe637bSDr. David Alan Gilbert { 52656f3835fSDr. David Alan Gilbert size_t l; 52760fe637bSDr. David Alan Gilbert 52860fe637bSDr. David Alan Gilbert if (f->last_error) { 52960fe637bSDr. David Alan Gilbert return; 53060fe637bSDr. David Alan Gilbert } 53160fe637bSDr. David Alan Gilbert 53260fe637bSDr. David Alan Gilbert while (size > 0) { 53360fe637bSDr. David Alan Gilbert l = IO_BUF_SIZE - f->buf_index; 53460fe637bSDr. David Alan Gilbert if (l > size) { 53560fe637bSDr. David Alan Gilbert l = size; 53660fe637bSDr. David Alan Gilbert } 53760fe637bSDr. David Alan Gilbert memcpy(f->buf + f->buf_index, buf, l); 538c7fc8d32SDaniel P. Berrangé f->rate_limit_used += l; 5391bf57fb3SWei Yang add_buf_to_iovec(f, l); 54060fe637bSDr. David Alan Gilbert if (qemu_file_get_error(f)) { 54160fe637bSDr. David Alan Gilbert break; 54260fe637bSDr. David Alan Gilbert } 54360fe637bSDr. David Alan Gilbert buf += l; 54460fe637bSDr. David Alan Gilbert size -= l; 54560fe637bSDr. David Alan Gilbert } 54660fe637bSDr. David Alan Gilbert } 54760fe637bSDr. David Alan Gilbert 54860fe637bSDr. David Alan Gilbert void qemu_put_byte(QEMUFile *f, int v) 54960fe637bSDr. David Alan Gilbert { 55060fe637bSDr. David Alan Gilbert if (f->last_error) { 55160fe637bSDr. David Alan Gilbert return; 55260fe637bSDr. David Alan Gilbert } 55360fe637bSDr. David Alan Gilbert 55460fe637bSDr. David Alan Gilbert f->buf[f->buf_index] = v; 555c7fc8d32SDaniel P. Berrangé f->rate_limit_used++; 5561bf57fb3SWei Yang add_buf_to_iovec(f, 1); 55760fe637bSDr. David Alan Gilbert } 55860fe637bSDr. David Alan Gilbert 55960fe637bSDr. David Alan Gilbert void qemu_file_skip(QEMUFile *f, int size) 56060fe637bSDr. David Alan Gilbert { 56160fe637bSDr. David Alan Gilbert if (f->buf_index + size <= f->buf_size) { 56260fe637bSDr. David Alan Gilbert f->buf_index += size; 56360fe637bSDr. David Alan Gilbert } 56460fe637bSDr. David Alan Gilbert } 56560fe637bSDr. David Alan Gilbert 56660fe637bSDr. David Alan Gilbert /* 5677c1e52baSDr. David Alan Gilbert * Read 'size' bytes from file (at 'offset') without moving the 5687c1e52baSDr. David Alan Gilbert * pointer and set 'buf' to point to that data. 56960fe637bSDr. David Alan Gilbert * 57060fe637bSDr. David Alan Gilbert * It will return size bytes unless there was an error, in which case it will 57160fe637bSDr. David Alan Gilbert * return as many as it managed to read (assuming blocking fd's which 57260fe637bSDr. David Alan Gilbert * all current QEMUFile are) 57360fe637bSDr. David Alan Gilbert */ 574394b9407SPaolo Bonzini size_t coroutine_mixed_fn qemu_peek_buffer(QEMUFile *f, uint8_t **buf, size_t size, size_t offset) 57560fe637bSDr. David Alan Gilbert { 57656f3835fSDr. David Alan Gilbert ssize_t pending; 57756f3835fSDr. David Alan Gilbert size_t index; 57860fe637bSDr. David Alan Gilbert 57960fe637bSDr. David Alan Gilbert assert(!qemu_file_is_writable(f)); 58060fe637bSDr. David Alan Gilbert assert(offset < IO_BUF_SIZE); 58160fe637bSDr. David Alan Gilbert assert(size <= IO_BUF_SIZE - offset); 58260fe637bSDr. David Alan Gilbert 58360fe637bSDr. David Alan Gilbert /* The 1st byte to read from */ 58460fe637bSDr. David Alan Gilbert index = f->buf_index + offset; 58560fe637bSDr. David Alan Gilbert /* The number of available bytes starting at index */ 58660fe637bSDr. David Alan Gilbert pending = f->buf_size - index; 58760fe637bSDr. David Alan Gilbert 58860fe637bSDr. David Alan Gilbert /* 58960fe637bSDr. David Alan Gilbert * qemu_fill_buffer might return just a few bytes, even when there isn't 59060fe637bSDr. David Alan Gilbert * an error, so loop collecting them until we get enough. 59160fe637bSDr. David Alan Gilbert */ 59260fe637bSDr. David Alan Gilbert while (pending < size) { 59360fe637bSDr. David Alan Gilbert int received = qemu_fill_buffer(f); 59460fe637bSDr. David Alan Gilbert 59560fe637bSDr. David Alan Gilbert if (received <= 0) { 59660fe637bSDr. David Alan Gilbert break; 59760fe637bSDr. David Alan Gilbert } 59860fe637bSDr. David Alan Gilbert 59960fe637bSDr. David Alan Gilbert index = f->buf_index + offset; 60060fe637bSDr. David Alan Gilbert pending = f->buf_size - index; 60160fe637bSDr. David Alan Gilbert } 60260fe637bSDr. David Alan Gilbert 60360fe637bSDr. David Alan Gilbert if (pending <= 0) { 60460fe637bSDr. David Alan Gilbert return 0; 60560fe637bSDr. David Alan Gilbert } 60660fe637bSDr. David Alan Gilbert if (size > pending) { 60760fe637bSDr. David Alan Gilbert size = pending; 60860fe637bSDr. David Alan Gilbert } 60960fe637bSDr. David Alan Gilbert 6107c1e52baSDr. David Alan Gilbert *buf = f->buf + index; 61160fe637bSDr. David Alan Gilbert return size; 61260fe637bSDr. David Alan Gilbert } 61360fe637bSDr. David Alan Gilbert 61460fe637bSDr. David Alan Gilbert /* 61560fe637bSDr. David Alan Gilbert * Read 'size' bytes of data from the file into buf. 61660fe637bSDr. David Alan Gilbert * 'size' can be larger than the internal buffer. 61760fe637bSDr. David Alan Gilbert * 61860fe637bSDr. David Alan Gilbert * It will return size bytes unless there was an error, in which case it will 61960fe637bSDr. David Alan Gilbert * return as many as it managed to read (assuming blocking fd's which 62060fe637bSDr. David Alan Gilbert * all current QEMUFile are) 62160fe637bSDr. David Alan Gilbert */ 622394b9407SPaolo Bonzini size_t coroutine_mixed_fn qemu_get_buffer(QEMUFile *f, uint8_t *buf, size_t size) 62360fe637bSDr. David Alan Gilbert { 62456f3835fSDr. David Alan Gilbert size_t pending = size; 62556f3835fSDr. David Alan Gilbert size_t done = 0; 62660fe637bSDr. David Alan Gilbert 62760fe637bSDr. David Alan Gilbert while (pending > 0) { 62856f3835fSDr. David Alan Gilbert size_t res; 6297c1e52baSDr. David Alan Gilbert uint8_t *src; 63060fe637bSDr. David Alan Gilbert 6317c1e52baSDr. David Alan Gilbert res = qemu_peek_buffer(f, &src, MIN(pending, IO_BUF_SIZE), 0); 63260fe637bSDr. David Alan Gilbert if (res == 0) { 63360fe637bSDr. David Alan Gilbert return done; 63460fe637bSDr. David Alan Gilbert } 6357c1e52baSDr. David Alan Gilbert memcpy(buf, src, res); 63660fe637bSDr. David Alan Gilbert qemu_file_skip(f, res); 63760fe637bSDr. David Alan Gilbert buf += res; 63860fe637bSDr. David Alan Gilbert pending -= res; 63960fe637bSDr. David Alan Gilbert done += res; 64060fe637bSDr. David Alan Gilbert } 64160fe637bSDr. David Alan Gilbert return done; 64260fe637bSDr. David Alan Gilbert } 64360fe637bSDr. David Alan Gilbert 64460fe637bSDr. David Alan Gilbert /* 6459504fb51SDr. David Alan Gilbert * Read 'size' bytes of data from the file. 6469504fb51SDr. David Alan Gilbert * 'size' can be larger than the internal buffer. 6479504fb51SDr. David Alan Gilbert * 6489504fb51SDr. David Alan Gilbert * The data: 6499504fb51SDr. David Alan Gilbert * may be held on an internal buffer (in which case *buf is updated 6509504fb51SDr. David Alan Gilbert * to point to it) that is valid until the next qemu_file operation. 6519504fb51SDr. David Alan Gilbert * OR 6529504fb51SDr. David Alan Gilbert * will be copied to the *buf that was passed in. 6539504fb51SDr. David Alan Gilbert * 6549504fb51SDr. David Alan Gilbert * The code tries to avoid the copy if possible. 6559504fb51SDr. David Alan Gilbert * 6569504fb51SDr. David Alan Gilbert * It will return size bytes unless there was an error, in which case it will 6579504fb51SDr. David Alan Gilbert * return as many as it managed to read (assuming blocking fd's which 6589504fb51SDr. David Alan Gilbert * all current QEMUFile are) 6599504fb51SDr. David Alan Gilbert * 6609504fb51SDr. David Alan Gilbert * Note: Since **buf may get changed, the caller should take care to 6619504fb51SDr. David Alan Gilbert * keep a pointer to the original buffer if it needs to deallocate it. 6629504fb51SDr. David Alan Gilbert */ 663394b9407SPaolo Bonzini size_t coroutine_mixed_fn qemu_get_buffer_in_place(QEMUFile *f, uint8_t **buf, size_t size) 6649504fb51SDr. David Alan Gilbert { 6659504fb51SDr. David Alan Gilbert if (size < IO_BUF_SIZE) { 6669504fb51SDr. David Alan Gilbert size_t res; 6671dfafcbdSWainer dos Santos Moschetta uint8_t *src = NULL; 6689504fb51SDr. David Alan Gilbert 6699504fb51SDr. David Alan Gilbert res = qemu_peek_buffer(f, &src, size, 0); 6709504fb51SDr. David Alan Gilbert 6719504fb51SDr. David Alan Gilbert if (res == size) { 6729504fb51SDr. David Alan Gilbert qemu_file_skip(f, res); 6739504fb51SDr. David Alan Gilbert *buf = src; 6749504fb51SDr. David Alan Gilbert return res; 6759504fb51SDr. David Alan Gilbert } 6769504fb51SDr. David Alan Gilbert } 6779504fb51SDr. David Alan Gilbert 6789504fb51SDr. David Alan Gilbert return qemu_get_buffer(f, *buf, size); 6799504fb51SDr. David Alan Gilbert } 6809504fb51SDr. David Alan Gilbert 6819504fb51SDr. David Alan Gilbert /* 68260fe637bSDr. David Alan Gilbert * Peeks a single byte from the buffer; this isn't guaranteed to work if 68360fe637bSDr. David Alan Gilbert * offset leaves a gap after the previous read/peeked data. 68460fe637bSDr. David Alan Gilbert */ 685394b9407SPaolo Bonzini int coroutine_mixed_fn qemu_peek_byte(QEMUFile *f, int offset) 68660fe637bSDr. David Alan Gilbert { 68760fe637bSDr. David Alan Gilbert int index = f->buf_index + offset; 68860fe637bSDr. David Alan Gilbert 68960fe637bSDr. David Alan Gilbert assert(!qemu_file_is_writable(f)); 69060fe637bSDr. David Alan Gilbert assert(offset < IO_BUF_SIZE); 69160fe637bSDr. David Alan Gilbert 69260fe637bSDr. David Alan Gilbert if (index >= f->buf_size) { 69360fe637bSDr. David Alan Gilbert qemu_fill_buffer(f); 69460fe637bSDr. David Alan Gilbert index = f->buf_index + offset; 69560fe637bSDr. David Alan Gilbert if (index >= f->buf_size) { 69660fe637bSDr. David Alan Gilbert return 0; 69760fe637bSDr. David Alan Gilbert } 69860fe637bSDr. David Alan Gilbert } 69960fe637bSDr. David Alan Gilbert return f->buf[index]; 70060fe637bSDr. David Alan Gilbert } 70160fe637bSDr. David Alan Gilbert 702394b9407SPaolo Bonzini int coroutine_mixed_fn qemu_get_byte(QEMUFile *f) 70360fe637bSDr. David Alan Gilbert { 70460fe637bSDr. David Alan Gilbert int result; 70560fe637bSDr. David Alan Gilbert 70660fe637bSDr. David Alan Gilbert result = qemu_peek_byte(f, 0); 70760fe637bSDr. David Alan Gilbert qemu_file_skip(f, 1); 70860fe637bSDr. David Alan Gilbert return result; 70960fe637bSDr. David Alan Gilbert } 71060fe637bSDr. David Alan Gilbert 71161abf1ebSJuan Quintela uint64_t qemu_file_total_transferred_fast(QEMUFile *f) 71297221400SAlexander Graf { 71361abf1ebSJuan Quintela uint64_t ret = f->total_transferred; 71497221400SAlexander Graf int i; 71597221400SAlexander Graf 71697221400SAlexander Graf for (i = 0; i < f->iovcnt; i++) { 71797221400SAlexander Graf ret += f->iov[i].iov_len; 71897221400SAlexander Graf } 71997221400SAlexander Graf 72097221400SAlexander Graf return ret; 72197221400SAlexander Graf } 72297221400SAlexander Graf 72361abf1ebSJuan Quintela uint64_t qemu_file_total_transferred(QEMUFile *f) 72460fe637bSDr. David Alan Gilbert { 72560fe637bSDr. David Alan Gilbert qemu_fflush(f); 726154d87b4SDaniel P. Berrangé return f->total_transferred; 72760fe637bSDr. David Alan Gilbert } 72860fe637bSDr. David Alan Gilbert 72960fe637bSDr. David Alan Gilbert int qemu_file_rate_limit(QEMUFile *f) 73060fe637bSDr. David Alan Gilbert { 73160fe637bSDr. David Alan Gilbert if (qemu_file_get_error(f)) { 73260fe637bSDr. David Alan Gilbert return 1; 73360fe637bSDr. David Alan Gilbert } 734c7fc8d32SDaniel P. Berrangé if (f->rate_limit_max > 0 && f->rate_limit_used > f->rate_limit_max) { 73560fe637bSDr. David Alan Gilbert return 1; 73660fe637bSDr. David Alan Gilbert } 73760fe637bSDr. David Alan Gilbert return 0; 73860fe637bSDr. David Alan Gilbert } 73960fe637bSDr. David Alan Gilbert 74060fe637bSDr. David Alan Gilbert int64_t qemu_file_get_rate_limit(QEMUFile *f) 74160fe637bSDr. David Alan Gilbert { 742c7fc8d32SDaniel P. Berrangé return f->rate_limit_max; 74360fe637bSDr. David Alan Gilbert } 74460fe637bSDr. David Alan Gilbert 74560fe637bSDr. David Alan Gilbert void qemu_file_set_rate_limit(QEMUFile *f, int64_t limit) 74660fe637bSDr. David Alan Gilbert { 747c7fc8d32SDaniel P. Berrangé f->rate_limit_max = limit; 74860fe637bSDr. David Alan Gilbert } 74960fe637bSDr. David Alan Gilbert 75060fe637bSDr. David Alan Gilbert void qemu_file_reset_rate_limit(QEMUFile *f) 75160fe637bSDr. David Alan Gilbert { 752c7fc8d32SDaniel P. Berrangé f->rate_limit_used = 0; 75360fe637bSDr. David Alan Gilbert } 75460fe637bSDr. David Alan Gilbert 755bc698c36SDaniel P. Berrangé void qemu_file_acct_rate_limit(QEMUFile *f, int64_t len) 7565d7d2558SIvan Ren { 757c7fc8d32SDaniel P. Berrangé f->rate_limit_used += len; 7585d7d2558SIvan Ren } 7595d7d2558SIvan Ren 76060fe637bSDr. David Alan Gilbert void qemu_put_be16(QEMUFile *f, unsigned int v) 76160fe637bSDr. David Alan Gilbert { 76260fe637bSDr. David Alan Gilbert qemu_put_byte(f, v >> 8); 76360fe637bSDr. David Alan Gilbert qemu_put_byte(f, v); 76460fe637bSDr. David Alan Gilbert } 76560fe637bSDr. David Alan Gilbert 76660fe637bSDr. David Alan Gilbert void qemu_put_be32(QEMUFile *f, unsigned int v) 76760fe637bSDr. David Alan Gilbert { 76860fe637bSDr. David Alan Gilbert qemu_put_byte(f, v >> 24); 76960fe637bSDr. David Alan Gilbert qemu_put_byte(f, v >> 16); 77060fe637bSDr. David Alan Gilbert qemu_put_byte(f, v >> 8); 77160fe637bSDr. David Alan Gilbert qemu_put_byte(f, v); 77260fe637bSDr. David Alan Gilbert } 77360fe637bSDr. David Alan Gilbert 77460fe637bSDr. David Alan Gilbert void qemu_put_be64(QEMUFile *f, uint64_t v) 77560fe637bSDr. David Alan Gilbert { 77660fe637bSDr. David Alan Gilbert qemu_put_be32(f, v >> 32); 77760fe637bSDr. David Alan Gilbert qemu_put_be32(f, v); 77860fe637bSDr. David Alan Gilbert } 77960fe637bSDr. David Alan Gilbert 78060fe637bSDr. David Alan Gilbert unsigned int qemu_get_be16(QEMUFile *f) 78160fe637bSDr. David Alan Gilbert { 78260fe637bSDr. David Alan Gilbert unsigned int v; 78360fe637bSDr. David Alan Gilbert v = qemu_get_byte(f) << 8; 78460fe637bSDr. David Alan Gilbert v |= qemu_get_byte(f); 78560fe637bSDr. David Alan Gilbert return v; 78660fe637bSDr. David Alan Gilbert } 78760fe637bSDr. David Alan Gilbert 78860fe637bSDr. David Alan Gilbert unsigned int qemu_get_be32(QEMUFile *f) 78960fe637bSDr. David Alan Gilbert { 79060fe637bSDr. David Alan Gilbert unsigned int v; 79190d6a673SPeter Maydell v = (unsigned int)qemu_get_byte(f) << 24; 79260fe637bSDr. David Alan Gilbert v |= qemu_get_byte(f) << 16; 79360fe637bSDr. David Alan Gilbert v |= qemu_get_byte(f) << 8; 79460fe637bSDr. David Alan Gilbert v |= qemu_get_byte(f); 79560fe637bSDr. David Alan Gilbert return v; 79660fe637bSDr. David Alan Gilbert } 79760fe637bSDr. David Alan Gilbert 79860fe637bSDr. David Alan Gilbert uint64_t qemu_get_be64(QEMUFile *f) 79960fe637bSDr. David Alan Gilbert { 80060fe637bSDr. David Alan Gilbert uint64_t v; 80160fe637bSDr. David Alan Gilbert v = (uint64_t)qemu_get_be32(f) << 32; 80260fe637bSDr. David Alan Gilbert v |= qemu_get_be32(f); 80360fe637bSDr. David Alan Gilbert return v; 80460fe637bSDr. David Alan Gilbert } 80544f0eadcSLiang Li 806dcaf446eSXiao Guangrong /* return the size after compression, or negative value on error */ 807dcaf446eSXiao Guangrong static int qemu_compress_data(z_stream *stream, uint8_t *dest, size_t dest_len, 808dcaf446eSXiao Guangrong const uint8_t *source, size_t source_len) 809dcaf446eSXiao Guangrong { 810dcaf446eSXiao Guangrong int err; 811dcaf446eSXiao Guangrong 812dcaf446eSXiao Guangrong err = deflateReset(stream); 813dcaf446eSXiao Guangrong if (err != Z_OK) { 814dcaf446eSXiao Guangrong return -1; 815dcaf446eSXiao Guangrong } 816dcaf446eSXiao Guangrong 817dcaf446eSXiao Guangrong stream->avail_in = source_len; 818dcaf446eSXiao Guangrong stream->next_in = (uint8_t *)source; 819dcaf446eSXiao Guangrong stream->avail_out = dest_len; 820dcaf446eSXiao Guangrong stream->next_out = dest; 821dcaf446eSXiao Guangrong 822dcaf446eSXiao Guangrong err = deflate(stream, Z_FINISH); 823dcaf446eSXiao Guangrong if (err != Z_STREAM_END) { 824dcaf446eSXiao Guangrong return -1; 825dcaf446eSXiao Guangrong } 826dcaf446eSXiao Guangrong 827dcaf446eSXiao Guangrong return stream->next_out - dest; 828dcaf446eSXiao Guangrong } 829dcaf446eSXiao Guangrong 830dcaf446eSXiao Guangrong /* Compress size bytes of data start at p and store the compressed 831dcaf446eSXiao Guangrong * data to the buffer of f. 832b3be2896SLiang Li * 83342d24611SWei Yang * Since the file is dummy file with empty_ops, return -1 if f has no space to 83442d24611SWei Yang * save the compressed data. 83544f0eadcSLiang Li */ 836dcaf446eSXiao Guangrong ssize_t qemu_put_compression_data(QEMUFile *f, z_stream *stream, 837dcaf446eSXiao Guangrong const uint8_t *p, size_t size) 83844f0eadcSLiang Li { 83944f0eadcSLiang Li ssize_t blen = IO_BUF_SIZE - f->buf_index - sizeof(int32_t); 84044f0eadcSLiang Li 84144f0eadcSLiang Li if (blen < compressBound(size)) { 842b3be2896SLiang Li return -1; 843b3be2896SLiang Li } 844dcaf446eSXiao Guangrong 845dcaf446eSXiao Guangrong blen = qemu_compress_data(stream, f->buf + f->buf_index + sizeof(int32_t), 846dcaf446eSXiao Guangrong blen, p, size); 847dcaf446eSXiao Guangrong if (blen < 0) { 84834ab9e97SXiao Guangrong return -1; 84944f0eadcSLiang Li } 85034ab9e97SXiao Guangrong 85144f0eadcSLiang Li qemu_put_be32(f, blen); 8521bf57fb3SWei Yang add_buf_to_iovec(f, blen); 85344f0eadcSLiang Li return blen + sizeof(int32_t); 85444f0eadcSLiang Li } 85544f0eadcSLiang Li 85644f0eadcSLiang Li /* Put the data in the buffer of f_src to the buffer of f_des, and 85744f0eadcSLiang Li * then reset the buf_index of f_src to 0. 85844f0eadcSLiang Li */ 85944f0eadcSLiang Li 86044f0eadcSLiang Li int qemu_put_qemu_file(QEMUFile *f_des, QEMUFile *f_src) 86144f0eadcSLiang Li { 86244f0eadcSLiang Li int len = 0; 86344f0eadcSLiang Li 86444f0eadcSLiang Li if (f_src->buf_index > 0) { 86544f0eadcSLiang Li len = f_src->buf_index; 86644f0eadcSLiang Li qemu_put_buffer(f_des, f_src->buf, f_src->buf_index); 86744f0eadcSLiang Li f_src->buf_index = 0; 868787d134fSLiang Li f_src->iovcnt = 0; 86944f0eadcSLiang Li } 87044f0eadcSLiang Li return len; 87144f0eadcSLiang Li } 872b3af1bc9SDr. David Alan Gilbert 873b3af1bc9SDr. David Alan Gilbert /* 874b3af1bc9SDr. David Alan Gilbert * Get a string whose length is determined by a single preceding byte 875b3af1bc9SDr. David Alan Gilbert * A preallocated 256 byte buffer must be passed in. 876b3af1bc9SDr. David Alan Gilbert * Returns: len on success and a 0 terminated string in the buffer 877b3af1bc9SDr. David Alan Gilbert * else 0 878b3af1bc9SDr. David Alan Gilbert * (Note a 0 length string will return 0 either way) 879b3af1bc9SDr. David Alan Gilbert */ 880394b9407SPaolo Bonzini size_t coroutine_fn qemu_get_counted_string(QEMUFile *f, char buf[256]) 881b3af1bc9SDr. David Alan Gilbert { 882b3af1bc9SDr. David Alan Gilbert size_t len = qemu_get_byte(f); 883b3af1bc9SDr. David Alan Gilbert size_t res = qemu_get_buffer(f, (uint8_t *)buf, len); 884b3af1bc9SDr. David Alan Gilbert 885b3af1bc9SDr. David Alan Gilbert buf[res] = 0; 886b3af1bc9SDr. David Alan Gilbert 887b3af1bc9SDr. David Alan Gilbert return res == len ? res : 0; 888b3af1bc9SDr. David Alan Gilbert } 889a800cd5cSDr. David Alan Gilbert 890a800cd5cSDr. David Alan Gilbert /* 891f0d64cb7SVladimir Sementsov-Ogievskiy * Put a string with one preceding byte containing its length. The length of 892f0d64cb7SVladimir Sementsov-Ogievskiy * the string should be less than 256. 893f0d64cb7SVladimir Sementsov-Ogievskiy */ 894f0d64cb7SVladimir Sementsov-Ogievskiy void qemu_put_counted_string(QEMUFile *f, const char *str) 895f0d64cb7SVladimir Sementsov-Ogievskiy { 896f0d64cb7SVladimir Sementsov-Ogievskiy size_t len = strlen(str); 897f0d64cb7SVladimir Sementsov-Ogievskiy 898f0d64cb7SVladimir Sementsov-Ogievskiy assert(len < 256); 899f0d64cb7SVladimir Sementsov-Ogievskiy qemu_put_byte(f, len); 900f0d64cb7SVladimir Sementsov-Ogievskiy qemu_put_buffer(f, (const uint8_t *)str, len); 901f0d64cb7SVladimir Sementsov-Ogievskiy } 902f0d64cb7SVladimir Sementsov-Ogievskiy 903f0d64cb7SVladimir Sementsov-Ogievskiy /* 904a800cd5cSDr. David Alan Gilbert * Set the blocking state of the QEMUFile. 905a800cd5cSDr. David Alan Gilbert * Note: On some transports the OS only keeps a single blocking state for 906a800cd5cSDr. David Alan Gilbert * both directions, and thus changing the blocking on the main 907a800cd5cSDr. David Alan Gilbert * QEMUFile can also affect the return path. 908a800cd5cSDr. David Alan Gilbert */ 909a800cd5cSDr. David Alan Gilbert void qemu_file_set_blocking(QEMUFile *f, bool block) 910a800cd5cSDr. David Alan Gilbert { 91180ad9706SDaniel P. Berrangé qio_channel_set_blocking(f->ioc, block, NULL); 91206ad5135SDaniel P. Berrange } 913c6ad5be7SPeter Xu 914c6ad5be7SPeter Xu /* 9152893a288SDaniel P. Berrangé * qemu_file_get_ioc: 9162893a288SDaniel P. Berrangé * 9172893a288SDaniel P. Berrangé * Get the ioc object for the file, without incrementing 9182893a288SDaniel P. Berrangé * the reference count. 9192893a288SDaniel P. Berrangé * 9202893a288SDaniel P. Berrangé * Returns: the ioc object 921c6ad5be7SPeter Xu */ 922c6ad5be7SPeter Xu QIOChannel *qemu_file_get_ioc(QEMUFile *file) 923c6ad5be7SPeter Xu { 9242893a288SDaniel P. Berrangé return file->ioc; 925c6ad5be7SPeter Xu } 926c7a7db4bSAvihai Horon 927c7a7db4bSAvihai Horon /* 928c7a7db4bSAvihai Horon * Read size bytes from QEMUFile f and write them to fd. 929c7a7db4bSAvihai Horon */ 930c7a7db4bSAvihai Horon int qemu_file_get_to_fd(QEMUFile *f, int fd, size_t size) 931c7a7db4bSAvihai Horon { 932c7a7db4bSAvihai Horon while (size) { 933c7a7db4bSAvihai Horon size_t pending = f->buf_size - f->buf_index; 934c7a7db4bSAvihai Horon ssize_t rc; 935c7a7db4bSAvihai Horon 936c7a7db4bSAvihai Horon if (!pending) { 937c7a7db4bSAvihai Horon rc = qemu_fill_buffer(f); 938c7a7db4bSAvihai Horon if (rc < 0) { 939c7a7db4bSAvihai Horon return rc; 940c7a7db4bSAvihai Horon } 941c7a7db4bSAvihai Horon if (rc == 0) { 942c7a7db4bSAvihai Horon return -EIO; 943c7a7db4bSAvihai Horon } 944c7a7db4bSAvihai Horon continue; 945c7a7db4bSAvihai Horon } 946c7a7db4bSAvihai Horon 947c7a7db4bSAvihai Horon rc = write(fd, f->buf + f->buf_index, MIN(pending, size)); 948c7a7db4bSAvihai Horon if (rc < 0) { 949c7a7db4bSAvihai Horon return -errno; 950c7a7db4bSAvihai Horon } 951c7a7db4bSAvihai Horon if (rc == 0) { 952c7a7db4bSAvihai Horon return -EIO; 953c7a7db4bSAvihai Horon } 954c7a7db4bSAvihai Horon f->buf_index += rc; 955c7a7db4bSAvihai Horon size -= rc; 956c7a7db4bSAvihai Horon } 957c7a7db4bSAvihai Horon 958c7a7db4bSAvihai Horon return 0; 959c7a7db4bSAvihai Horon } 960