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> 2660fe637bSDr. David Alan Gilbert #include "qemu-common.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" 3260fe637bSDr. David Alan Gilbert 33a24939f2SDaniel P. Berrange #define IO_BUF_SIZE 32768 34a24939f2SDaniel P. Berrange #define MAX_IOV_SIZE MIN(IOV_MAX, 64) 35a24939f2SDaniel P. Berrange 36a24939f2SDaniel P. Berrange struct QEMUFile { 37a24939f2SDaniel P. Berrange const QEMUFileOps *ops; 38a24939f2SDaniel P. Berrange const QEMUFileHooks *hooks; 39a24939f2SDaniel P. Berrange void *opaque; 40a24939f2SDaniel P. Berrange 41a24939f2SDaniel P. Berrange int64_t bytes_xfer; 42a24939f2SDaniel P. Berrange int64_t xfer_limit; 43a24939f2SDaniel P. Berrange 44a24939f2SDaniel P. Berrange int64_t pos; /* start of buffer when writing, end of buffer 45a24939f2SDaniel P. Berrange when reading */ 46a24939f2SDaniel P. Berrange int buf_index; 47a24939f2SDaniel P. Berrange int buf_size; /* 0 when writing */ 48a24939f2SDaniel P. Berrange uint8_t buf[IO_BUF_SIZE]; 49a24939f2SDaniel P. Berrange 5053f09a10SPavel Butsykin DECLARE_BITMAP(may_free, MAX_IOV_SIZE); 51a24939f2SDaniel P. Berrange struct iovec iov[MAX_IOV_SIZE]; 52a24939f2SDaniel P. Berrange unsigned int iovcnt; 53a24939f2SDaniel P. Berrange 54a24939f2SDaniel P. Berrange int last_error; 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. 60e1a8c9b6SDr. David Alan Gilbert */ 61e1a8c9b6SDr. David Alan Gilbert int qemu_file_shutdown(QEMUFile *f) 62e1a8c9b6SDr. David Alan Gilbert { 63e1a8c9b6SDr. David Alan Gilbert if (!f->ops->shut_down) { 64e1a8c9b6SDr. David Alan Gilbert return -ENOSYS; 65e1a8c9b6SDr. David Alan Gilbert } 66e1a8c9b6SDr. David Alan Gilbert return f->ops->shut_down(f->opaque, true, true); 67e1a8c9b6SDr. David Alan Gilbert } 68e1a8c9b6SDr. David Alan Gilbert 69adc468e9SDr. David Alan Gilbert /* 70adc468e9SDr. David Alan Gilbert * Result: QEMUFile* for a 'return path' for comms in the opposite direction 71adc468e9SDr. David Alan Gilbert * NULL if not available 72adc468e9SDr. David Alan Gilbert */ 73adc468e9SDr. David Alan Gilbert QEMUFile *qemu_file_get_return_path(QEMUFile *f) 74adc468e9SDr. David Alan Gilbert { 75adc468e9SDr. David Alan Gilbert if (!f->ops->get_return_path) { 76adc468e9SDr. David Alan Gilbert return NULL; 77adc468e9SDr. David Alan Gilbert } 78adc468e9SDr. David Alan Gilbert return f->ops->get_return_path(f->opaque); 79adc468e9SDr. David Alan Gilbert } 80adc468e9SDr. David Alan Gilbert 8160fe637bSDr. David Alan Gilbert bool qemu_file_mode_is_not_valid(const char *mode) 8260fe637bSDr. David Alan Gilbert { 8360fe637bSDr. David Alan Gilbert if (mode == NULL || 8460fe637bSDr. David Alan Gilbert (mode[0] != 'r' && mode[0] != 'w') || 8560fe637bSDr. David Alan Gilbert mode[1] != 'b' || mode[2] != 0) { 8660fe637bSDr. David Alan Gilbert fprintf(stderr, "qemu_fopen: Argument validity check failed\n"); 8760fe637bSDr. David Alan Gilbert return true; 8860fe637bSDr. David Alan Gilbert } 8960fe637bSDr. David Alan Gilbert 9060fe637bSDr. David Alan Gilbert return false; 9160fe637bSDr. David Alan Gilbert } 9260fe637bSDr. David Alan Gilbert 9360fe637bSDr. David Alan Gilbert QEMUFile *qemu_fopen_ops(void *opaque, const QEMUFileOps *ops) 9460fe637bSDr. David Alan Gilbert { 9560fe637bSDr. David Alan Gilbert QEMUFile *f; 9660fe637bSDr. David Alan Gilbert 9797f3ad35SMarkus Armbruster f = g_new0(QEMUFile, 1); 9860fe637bSDr. David Alan Gilbert 9960fe637bSDr. David Alan Gilbert f->opaque = opaque; 10060fe637bSDr. David Alan Gilbert f->ops = ops; 10160fe637bSDr. David Alan Gilbert return f; 10260fe637bSDr. David Alan Gilbert } 10360fe637bSDr. David Alan Gilbert 1040436e09fSDaniel P. Berrange 1050436e09fSDaniel P. Berrange void qemu_file_set_hooks(QEMUFile *f, const QEMUFileHooks *hooks) 1060436e09fSDaniel P. Berrange { 1070436e09fSDaniel P. Berrange f->hooks = hooks; 1080436e09fSDaniel P. Berrange } 1090436e09fSDaniel P. Berrange 11060fe637bSDr. David Alan Gilbert /* 11160fe637bSDr. David Alan Gilbert * Get last error for stream f 11260fe637bSDr. David Alan Gilbert * 11360fe637bSDr. David Alan Gilbert * Return negative error value if there has been an error on previous 11460fe637bSDr. David Alan Gilbert * operations, return 0 if no error happened. 11560fe637bSDr. David Alan Gilbert * 11660fe637bSDr. David Alan Gilbert */ 11760fe637bSDr. David Alan Gilbert int qemu_file_get_error(QEMUFile *f) 11860fe637bSDr. David Alan Gilbert { 11960fe637bSDr. David Alan Gilbert return f->last_error; 12060fe637bSDr. David Alan Gilbert } 12160fe637bSDr. David Alan Gilbert 12260fe637bSDr. David Alan Gilbert void qemu_file_set_error(QEMUFile *f, int ret) 12360fe637bSDr. David Alan Gilbert { 12460fe637bSDr. David Alan Gilbert if (f->last_error == 0) { 12560fe637bSDr. David Alan Gilbert f->last_error = ret; 12660fe637bSDr. David Alan Gilbert } 12760fe637bSDr. David Alan Gilbert } 12860fe637bSDr. David Alan Gilbert 12960fe637bSDr. David Alan Gilbert bool qemu_file_is_writable(QEMUFile *f) 13060fe637bSDr. David Alan Gilbert { 13111808bb0SDaniel P. Berrange return f->ops->writev_buffer; 13260fe637bSDr. David Alan Gilbert } 13360fe637bSDr. David Alan Gilbert 13453f09a10SPavel Butsykin static void qemu_iovec_release_ram(QEMUFile *f) 13553f09a10SPavel Butsykin { 13653f09a10SPavel Butsykin struct iovec iov; 13753f09a10SPavel Butsykin unsigned long idx; 13853f09a10SPavel Butsykin 13953f09a10SPavel Butsykin /* Find and release all the contiguous memory ranges marked as may_free. */ 14053f09a10SPavel Butsykin idx = find_next_bit(f->may_free, f->iovcnt, 0); 14153f09a10SPavel Butsykin if (idx >= f->iovcnt) { 14253f09a10SPavel Butsykin return; 14353f09a10SPavel Butsykin } 14453f09a10SPavel Butsykin iov = f->iov[idx]; 14553f09a10SPavel Butsykin 14653f09a10SPavel Butsykin /* The madvise() in the loop is called for iov within a continuous range and 14753f09a10SPavel Butsykin * then reinitialize the iov. And in the end, madvise() is called for the 14853f09a10SPavel Butsykin * last iov. 14953f09a10SPavel Butsykin */ 15053f09a10SPavel Butsykin while ((idx = find_next_bit(f->may_free, f->iovcnt, idx + 1)) < f->iovcnt) { 15153f09a10SPavel Butsykin /* check for adjacent buffer and coalesce them */ 15253f09a10SPavel Butsykin if (iov.iov_base + iov.iov_len == f->iov[idx].iov_base) { 15353f09a10SPavel Butsykin iov.iov_len += f->iov[idx].iov_len; 15453f09a10SPavel Butsykin continue; 15553f09a10SPavel Butsykin } 15653f09a10SPavel Butsykin if (qemu_madvise(iov.iov_base, iov.iov_len, QEMU_MADV_DONTNEED) < 0) { 15753f09a10SPavel Butsykin error_report("migrate: madvise DONTNEED failed %p %zd: %s", 15853f09a10SPavel Butsykin iov.iov_base, iov.iov_len, strerror(errno)); 15953f09a10SPavel Butsykin } 16053f09a10SPavel Butsykin iov = f->iov[idx]; 16153f09a10SPavel Butsykin } 16253f09a10SPavel Butsykin if (qemu_madvise(iov.iov_base, iov.iov_len, QEMU_MADV_DONTNEED) < 0) { 16353f09a10SPavel Butsykin error_report("migrate: madvise DONTNEED failed %p %zd: %s", 16453f09a10SPavel Butsykin iov.iov_base, iov.iov_len, strerror(errno)); 16553f09a10SPavel Butsykin } 16653f09a10SPavel Butsykin memset(f->may_free, 0, sizeof(f->may_free)); 16753f09a10SPavel Butsykin } 16853f09a10SPavel Butsykin 16960fe637bSDr. David Alan Gilbert /** 17060fe637bSDr. David Alan Gilbert * Flushes QEMUFile buffer 17160fe637bSDr. David Alan Gilbert * 17260fe637bSDr. David Alan Gilbert * If there is writev_buffer QEMUFileOps it uses it otherwise uses 173baf51e77SDaniel P. Berrange * put_buffer ops. This will flush all pending data. If data was 174baf51e77SDaniel P. Berrange * only partially flushed, it will set an error state. 17560fe637bSDr. David Alan Gilbert */ 17660fe637bSDr. David Alan Gilbert void qemu_fflush(QEMUFile *f) 17760fe637bSDr. David Alan Gilbert { 17860fe637bSDr. David Alan Gilbert ssize_t ret = 0; 179baf51e77SDaniel P. Berrange ssize_t expect = 0; 18060fe637bSDr. David Alan Gilbert 18160fe637bSDr. David Alan Gilbert if (!qemu_file_is_writable(f)) { 18260fe637bSDr. David Alan Gilbert return; 18360fe637bSDr. David Alan Gilbert } 18460fe637bSDr. David Alan Gilbert 18560fe637bSDr. David Alan Gilbert if (f->iovcnt > 0) { 186baf51e77SDaniel P. Berrange expect = iov_size(f->iov, f->iovcnt); 18760fe637bSDr. David Alan Gilbert ret = f->ops->writev_buffer(f->opaque, f->iov, f->iovcnt, f->pos); 18853f09a10SPavel Butsykin 18953f09a10SPavel Butsykin qemu_iovec_release_ram(f); 19060fe637bSDr. David Alan Gilbert } 191baf51e77SDaniel P. Berrange 19260fe637bSDr. David Alan Gilbert if (ret >= 0) { 19360fe637bSDr. David Alan Gilbert f->pos += ret; 19460fe637bSDr. David Alan Gilbert } 195baf51e77SDaniel P. Berrange /* We expect the QEMUFile write impl to send the full 196baf51e77SDaniel P. Berrange * data set we requested, so sanity check that. 197baf51e77SDaniel P. Berrange */ 198baf51e77SDaniel P. Berrange if (ret != expect) { 199baf51e77SDaniel P. Berrange qemu_file_set_error(f, ret < 0 ? ret : -EIO); 200baf51e77SDaniel P. Berrange } 20160fe637bSDr. David Alan Gilbert f->buf_index = 0; 20260fe637bSDr. David Alan Gilbert f->iovcnt = 0; 20360fe637bSDr. David Alan Gilbert } 20460fe637bSDr. David Alan Gilbert 20560fe637bSDr. David Alan Gilbert void ram_control_before_iterate(QEMUFile *f, uint64_t flags) 20660fe637bSDr. David Alan Gilbert { 20760fe637bSDr. David Alan Gilbert int ret = 0; 20860fe637bSDr. David Alan Gilbert 2090436e09fSDaniel P. Berrange if (f->hooks && f->hooks->before_ram_iterate) { 2100436e09fSDaniel P. Berrange ret = f->hooks->before_ram_iterate(f, f->opaque, flags, NULL); 21160fe637bSDr. David Alan Gilbert if (ret < 0) { 21260fe637bSDr. David Alan Gilbert qemu_file_set_error(f, ret); 21360fe637bSDr. David Alan Gilbert } 21460fe637bSDr. David Alan Gilbert } 21560fe637bSDr. David Alan Gilbert } 21660fe637bSDr. David Alan Gilbert 21760fe637bSDr. David Alan Gilbert void ram_control_after_iterate(QEMUFile *f, uint64_t flags) 21860fe637bSDr. David Alan Gilbert { 21960fe637bSDr. David Alan Gilbert int ret = 0; 22060fe637bSDr. David Alan Gilbert 2210436e09fSDaniel P. Berrange if (f->hooks && f->hooks->after_ram_iterate) { 2220436e09fSDaniel P. Berrange ret = f->hooks->after_ram_iterate(f, f->opaque, flags, NULL); 22360fe637bSDr. David Alan Gilbert if (ret < 0) { 22460fe637bSDr. David Alan Gilbert qemu_file_set_error(f, ret); 22560fe637bSDr. David Alan Gilbert } 22660fe637bSDr. David Alan Gilbert } 22760fe637bSDr. David Alan Gilbert } 22860fe637bSDr. David Alan Gilbert 229632e3a5cSDr. David Alan Gilbert void ram_control_load_hook(QEMUFile *f, uint64_t flags, void *data) 23060fe637bSDr. David Alan Gilbert { 23160fe637bSDr. David Alan Gilbert int ret = -EINVAL; 23260fe637bSDr. David Alan Gilbert 2330436e09fSDaniel P. Berrange if (f->hooks && f->hooks->hook_ram_load) { 2340436e09fSDaniel P. Berrange ret = f->hooks->hook_ram_load(f, f->opaque, flags, data); 23560fe637bSDr. David Alan Gilbert if (ret < 0) { 23660fe637bSDr. David Alan Gilbert qemu_file_set_error(f, ret); 23760fe637bSDr. David Alan Gilbert } 23860fe637bSDr. David Alan Gilbert } else { 239632e3a5cSDr. David Alan Gilbert /* 240632e3a5cSDr. David Alan Gilbert * Hook is a hook specifically requested by the source sending a flag 241632e3a5cSDr. David Alan Gilbert * that expects there to be a hook on the destination. 242632e3a5cSDr. David Alan Gilbert */ 243632e3a5cSDr. David Alan Gilbert if (flags == RAM_CONTROL_HOOK) { 24460fe637bSDr. David Alan Gilbert qemu_file_set_error(f, ret); 24560fe637bSDr. David Alan Gilbert } 24660fe637bSDr. David Alan Gilbert } 247632e3a5cSDr. David Alan Gilbert } 24860fe637bSDr. David Alan Gilbert 24960fe637bSDr. David Alan Gilbert size_t ram_control_save_page(QEMUFile *f, ram_addr_t block_offset, 2506e1dea46SJuan Quintela ram_addr_t offset, size_t size, 2516e1dea46SJuan Quintela uint64_t *bytes_sent) 25260fe637bSDr. David Alan Gilbert { 2530436e09fSDaniel P. Berrange if (f->hooks && f->hooks->save_page) { 2540436e09fSDaniel P. Berrange int ret = f->hooks->save_page(f, f->opaque, block_offset, 25560fe637bSDr. David Alan Gilbert offset, size, bytes_sent); 256*ccb7e1b5SLidong Chen if (ret != RAM_SAVE_CONTROL_NOT_SUPP) { 257e8a0f2f9SLidong Chen f->bytes_xfer += size; 258*ccb7e1b5SLidong Chen } 259*ccb7e1b5SLidong Chen 260*ccb7e1b5SLidong Chen if (ret != RAM_SAVE_CONTROL_DELAYED && 261*ccb7e1b5SLidong Chen ret != RAM_SAVE_CONTROL_NOT_SUPP) { 26260fe637bSDr. David Alan Gilbert if (bytes_sent && *bytes_sent > 0) { 26360fe637bSDr. David Alan Gilbert qemu_update_position(f, *bytes_sent); 26460fe637bSDr. David Alan Gilbert } else if (ret < 0) { 26560fe637bSDr. David Alan Gilbert qemu_file_set_error(f, ret); 26660fe637bSDr. David Alan Gilbert } 26760fe637bSDr. David Alan Gilbert } 26860fe637bSDr. David Alan Gilbert 26960fe637bSDr. David Alan Gilbert return ret; 27060fe637bSDr. David Alan Gilbert } 27160fe637bSDr. David Alan Gilbert 27260fe637bSDr. David Alan Gilbert return RAM_SAVE_CONTROL_NOT_SUPP; 27360fe637bSDr. David Alan Gilbert } 27460fe637bSDr. David Alan Gilbert 27560fe637bSDr. David Alan Gilbert /* 27660fe637bSDr. David Alan Gilbert * Attempt to fill the buffer from the underlying file 27760fe637bSDr. David Alan Gilbert * Returns the number of bytes read, or negative value for an error. 27860fe637bSDr. David Alan Gilbert * 27960fe637bSDr. David Alan Gilbert * Note that it can return a partially full buffer even in a not error/not EOF 28060fe637bSDr. David Alan Gilbert * case if the underlying file descriptor gives a short read, and that can 28160fe637bSDr. David Alan Gilbert * happen even on a blocking fd. 28260fe637bSDr. David Alan Gilbert */ 28360fe637bSDr. David Alan Gilbert static ssize_t qemu_fill_buffer(QEMUFile *f) 28460fe637bSDr. David Alan Gilbert { 28560fe637bSDr. David Alan Gilbert int len; 28660fe637bSDr. David Alan Gilbert int pending; 28760fe637bSDr. David Alan Gilbert 28860fe637bSDr. David Alan Gilbert assert(!qemu_file_is_writable(f)); 28960fe637bSDr. David Alan Gilbert 29060fe637bSDr. David Alan Gilbert pending = f->buf_size - f->buf_index; 29160fe637bSDr. David Alan Gilbert if (pending > 0) { 29260fe637bSDr. David Alan Gilbert memmove(f->buf, f->buf + f->buf_index, pending); 29360fe637bSDr. David Alan Gilbert } 29460fe637bSDr. David Alan Gilbert f->buf_index = 0; 29560fe637bSDr. David Alan Gilbert f->buf_size = pending; 29660fe637bSDr. David Alan Gilbert 29760fe637bSDr. David Alan Gilbert len = f->ops->get_buffer(f->opaque, f->buf + pending, f->pos, 29860fe637bSDr. David Alan Gilbert IO_BUF_SIZE - pending); 29960fe637bSDr. David Alan Gilbert if (len > 0) { 30060fe637bSDr. David Alan Gilbert f->buf_size += len; 30160fe637bSDr. David Alan Gilbert f->pos += len; 30260fe637bSDr. David Alan Gilbert } else if (len == 0) { 30360fe637bSDr. David Alan Gilbert qemu_file_set_error(f, -EIO); 30460fe637bSDr. David Alan Gilbert } else if (len != -EAGAIN) { 30560fe637bSDr. David Alan Gilbert qemu_file_set_error(f, len); 30660fe637bSDr. David Alan Gilbert } 30760fe637bSDr. David Alan Gilbert 30860fe637bSDr. David Alan Gilbert return len; 30960fe637bSDr. David Alan Gilbert } 31060fe637bSDr. David Alan Gilbert 31160fe637bSDr. David Alan Gilbert void qemu_update_position(QEMUFile *f, size_t size) 31260fe637bSDr. David Alan Gilbert { 31360fe637bSDr. David Alan Gilbert f->pos += size; 31460fe637bSDr. David Alan Gilbert } 31560fe637bSDr. David Alan Gilbert 31660fe637bSDr. David Alan Gilbert /** Closes the file 31760fe637bSDr. David Alan Gilbert * 31860fe637bSDr. David Alan Gilbert * Returns negative error value if any error happened on previous operations or 31960fe637bSDr. David Alan Gilbert * while closing the file. Returns 0 or positive number on success. 32060fe637bSDr. David Alan Gilbert * 32160fe637bSDr. David Alan Gilbert * The meaning of return value on success depends on the specific backend 32260fe637bSDr. David Alan Gilbert * being used. 32360fe637bSDr. David Alan Gilbert */ 32460fe637bSDr. David Alan Gilbert int qemu_fclose(QEMUFile *f) 32560fe637bSDr. David Alan Gilbert { 32660fe637bSDr. David Alan Gilbert int ret; 32760fe637bSDr. David Alan Gilbert qemu_fflush(f); 32860fe637bSDr. David Alan Gilbert ret = qemu_file_get_error(f); 32960fe637bSDr. David Alan Gilbert 33060fe637bSDr. David Alan Gilbert if (f->ops->close) { 33160fe637bSDr. David Alan Gilbert int ret2 = f->ops->close(f->opaque); 33260fe637bSDr. David Alan Gilbert if (ret >= 0) { 33360fe637bSDr. David Alan Gilbert ret = ret2; 33460fe637bSDr. David Alan Gilbert } 33560fe637bSDr. David Alan Gilbert } 33660fe637bSDr. David Alan Gilbert /* If any error was spotted before closing, we should report it 33760fe637bSDr. David Alan Gilbert * instead of the close() return value. 33860fe637bSDr. David Alan Gilbert */ 33960fe637bSDr. David Alan Gilbert if (f->last_error) { 34060fe637bSDr. David Alan Gilbert ret = f->last_error; 34160fe637bSDr. David Alan Gilbert } 34260fe637bSDr. David Alan Gilbert g_free(f); 34360fe637bSDr. David Alan Gilbert trace_qemu_file_fclose(); 34460fe637bSDr. David Alan Gilbert return ret; 34560fe637bSDr. David Alan Gilbert } 34660fe637bSDr. David Alan Gilbert 34753f09a10SPavel Butsykin static void add_to_iovec(QEMUFile *f, const uint8_t *buf, size_t size, 34853f09a10SPavel Butsykin bool may_free) 34960fe637bSDr. David Alan Gilbert { 35060fe637bSDr. David Alan Gilbert /* check for adjacent buffer and coalesce them */ 35160fe637bSDr. David Alan Gilbert if (f->iovcnt > 0 && buf == f->iov[f->iovcnt - 1].iov_base + 35253f09a10SPavel Butsykin f->iov[f->iovcnt - 1].iov_len && 35353f09a10SPavel Butsykin may_free == test_bit(f->iovcnt - 1, f->may_free)) 35453f09a10SPavel Butsykin { 35560fe637bSDr. David Alan Gilbert f->iov[f->iovcnt - 1].iov_len += size; 35660fe637bSDr. David Alan Gilbert } else { 35753f09a10SPavel Butsykin if (may_free) { 35853f09a10SPavel Butsykin set_bit(f->iovcnt, f->may_free); 35953f09a10SPavel Butsykin } 36060fe637bSDr. David Alan Gilbert f->iov[f->iovcnt].iov_base = (uint8_t *)buf; 36160fe637bSDr. David Alan Gilbert f->iov[f->iovcnt++].iov_len = size; 36260fe637bSDr. David Alan Gilbert } 36360fe637bSDr. David Alan Gilbert 36460fe637bSDr. David Alan Gilbert if (f->iovcnt >= MAX_IOV_SIZE) { 36560fe637bSDr. David Alan Gilbert qemu_fflush(f); 36660fe637bSDr. David Alan Gilbert } 36760fe637bSDr. David Alan Gilbert } 36860fe637bSDr. David Alan Gilbert 36953f09a10SPavel Butsykin void qemu_put_buffer_async(QEMUFile *f, const uint8_t *buf, size_t size, 37053f09a10SPavel Butsykin bool may_free) 37160fe637bSDr. David Alan Gilbert { 37260fe637bSDr. David Alan Gilbert if (f->last_error) { 37360fe637bSDr. David Alan Gilbert return; 37460fe637bSDr. David Alan Gilbert } 37560fe637bSDr. David Alan Gilbert 37660fe637bSDr. David Alan Gilbert f->bytes_xfer += size; 37753f09a10SPavel Butsykin add_to_iovec(f, buf, size, may_free); 37860fe637bSDr. David Alan Gilbert } 37960fe637bSDr. David Alan Gilbert 38056f3835fSDr. David Alan Gilbert void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, size_t size) 38160fe637bSDr. David Alan Gilbert { 38256f3835fSDr. David Alan Gilbert size_t l; 38360fe637bSDr. David Alan Gilbert 38460fe637bSDr. David Alan Gilbert if (f->last_error) { 38560fe637bSDr. David Alan Gilbert return; 38660fe637bSDr. David Alan Gilbert } 38760fe637bSDr. David Alan Gilbert 38860fe637bSDr. David Alan Gilbert while (size > 0) { 38960fe637bSDr. David Alan Gilbert l = IO_BUF_SIZE - f->buf_index; 39060fe637bSDr. David Alan Gilbert if (l > size) { 39160fe637bSDr. David Alan Gilbert l = size; 39260fe637bSDr. David Alan Gilbert } 39360fe637bSDr. David Alan Gilbert memcpy(f->buf + f->buf_index, buf, l); 39460fe637bSDr. David Alan Gilbert f->bytes_xfer += l; 39553f09a10SPavel Butsykin add_to_iovec(f, f->buf + f->buf_index, l, false); 39660fe637bSDr. David Alan Gilbert f->buf_index += l; 39760fe637bSDr. David Alan Gilbert if (f->buf_index == IO_BUF_SIZE) { 39860fe637bSDr. David Alan Gilbert qemu_fflush(f); 39960fe637bSDr. David Alan Gilbert } 40060fe637bSDr. David Alan Gilbert if (qemu_file_get_error(f)) { 40160fe637bSDr. David Alan Gilbert break; 40260fe637bSDr. David Alan Gilbert } 40360fe637bSDr. David Alan Gilbert buf += l; 40460fe637bSDr. David Alan Gilbert size -= l; 40560fe637bSDr. David Alan Gilbert } 40660fe637bSDr. David Alan Gilbert } 40760fe637bSDr. David Alan Gilbert 40860fe637bSDr. David Alan Gilbert void qemu_put_byte(QEMUFile *f, int v) 40960fe637bSDr. David Alan Gilbert { 41060fe637bSDr. David Alan Gilbert if (f->last_error) { 41160fe637bSDr. David Alan Gilbert return; 41260fe637bSDr. David Alan Gilbert } 41360fe637bSDr. David Alan Gilbert 41460fe637bSDr. David Alan Gilbert f->buf[f->buf_index] = v; 41560fe637bSDr. David Alan Gilbert f->bytes_xfer++; 41653f09a10SPavel Butsykin add_to_iovec(f, f->buf + f->buf_index, 1, false); 41760fe637bSDr. David Alan Gilbert f->buf_index++; 41860fe637bSDr. David Alan Gilbert if (f->buf_index == IO_BUF_SIZE) { 41960fe637bSDr. David Alan Gilbert qemu_fflush(f); 42060fe637bSDr. David Alan Gilbert } 42160fe637bSDr. David Alan Gilbert } 42260fe637bSDr. David Alan Gilbert 42360fe637bSDr. David Alan Gilbert void qemu_file_skip(QEMUFile *f, int size) 42460fe637bSDr. David Alan Gilbert { 42560fe637bSDr. David Alan Gilbert if (f->buf_index + size <= f->buf_size) { 42660fe637bSDr. David Alan Gilbert f->buf_index += size; 42760fe637bSDr. David Alan Gilbert } 42860fe637bSDr. David Alan Gilbert } 42960fe637bSDr. David Alan Gilbert 43060fe637bSDr. David Alan Gilbert /* 4317c1e52baSDr. David Alan Gilbert * Read 'size' bytes from file (at 'offset') without moving the 4327c1e52baSDr. David Alan Gilbert * pointer and set 'buf' to point to that data. 43360fe637bSDr. David Alan Gilbert * 43460fe637bSDr. David Alan Gilbert * It will return size bytes unless there was an error, in which case it will 43560fe637bSDr. David Alan Gilbert * return as many as it managed to read (assuming blocking fd's which 43660fe637bSDr. David Alan Gilbert * all current QEMUFile are) 43760fe637bSDr. David Alan Gilbert */ 43856f3835fSDr. David Alan Gilbert size_t qemu_peek_buffer(QEMUFile *f, uint8_t **buf, size_t size, size_t offset) 43960fe637bSDr. David Alan Gilbert { 44056f3835fSDr. David Alan Gilbert ssize_t pending; 44156f3835fSDr. David Alan Gilbert size_t index; 44260fe637bSDr. David Alan Gilbert 44360fe637bSDr. David Alan Gilbert assert(!qemu_file_is_writable(f)); 44460fe637bSDr. David Alan Gilbert assert(offset < IO_BUF_SIZE); 44560fe637bSDr. David Alan Gilbert assert(size <= IO_BUF_SIZE - offset); 44660fe637bSDr. David Alan Gilbert 44760fe637bSDr. David Alan Gilbert /* The 1st byte to read from */ 44860fe637bSDr. David Alan Gilbert index = f->buf_index + offset; 44960fe637bSDr. David Alan Gilbert /* The number of available bytes starting at index */ 45060fe637bSDr. David Alan Gilbert pending = f->buf_size - index; 45160fe637bSDr. David Alan Gilbert 45260fe637bSDr. David Alan Gilbert /* 45360fe637bSDr. David Alan Gilbert * qemu_fill_buffer might return just a few bytes, even when there isn't 45460fe637bSDr. David Alan Gilbert * an error, so loop collecting them until we get enough. 45560fe637bSDr. David Alan Gilbert */ 45660fe637bSDr. David Alan Gilbert while (pending < size) { 45760fe637bSDr. David Alan Gilbert int received = qemu_fill_buffer(f); 45860fe637bSDr. David Alan Gilbert 45960fe637bSDr. David Alan Gilbert if (received <= 0) { 46060fe637bSDr. David Alan Gilbert break; 46160fe637bSDr. David Alan Gilbert } 46260fe637bSDr. David Alan Gilbert 46360fe637bSDr. David Alan Gilbert index = f->buf_index + offset; 46460fe637bSDr. David Alan Gilbert pending = f->buf_size - index; 46560fe637bSDr. David Alan Gilbert } 46660fe637bSDr. David Alan Gilbert 46760fe637bSDr. David Alan Gilbert if (pending <= 0) { 46860fe637bSDr. David Alan Gilbert return 0; 46960fe637bSDr. David Alan Gilbert } 47060fe637bSDr. David Alan Gilbert if (size > pending) { 47160fe637bSDr. David Alan Gilbert size = pending; 47260fe637bSDr. David Alan Gilbert } 47360fe637bSDr. David Alan Gilbert 4747c1e52baSDr. David Alan Gilbert *buf = f->buf + index; 47560fe637bSDr. David Alan Gilbert return size; 47660fe637bSDr. David Alan Gilbert } 47760fe637bSDr. David Alan Gilbert 47860fe637bSDr. David Alan Gilbert /* 47960fe637bSDr. David Alan Gilbert * Read 'size' bytes of data from the file into buf. 48060fe637bSDr. David Alan Gilbert * 'size' can be larger than the internal buffer. 48160fe637bSDr. David Alan Gilbert * 48260fe637bSDr. David Alan Gilbert * It will return size bytes unless there was an error, in which case it will 48360fe637bSDr. David Alan Gilbert * return as many as it managed to read (assuming blocking fd's which 48460fe637bSDr. David Alan Gilbert * all current QEMUFile are) 48560fe637bSDr. David Alan Gilbert */ 48656f3835fSDr. David Alan Gilbert size_t qemu_get_buffer(QEMUFile *f, uint8_t *buf, size_t size) 48760fe637bSDr. David Alan Gilbert { 48856f3835fSDr. David Alan Gilbert size_t pending = size; 48956f3835fSDr. David Alan Gilbert size_t done = 0; 49060fe637bSDr. David Alan Gilbert 49160fe637bSDr. David Alan Gilbert while (pending > 0) { 49256f3835fSDr. David Alan Gilbert size_t res; 4937c1e52baSDr. David Alan Gilbert uint8_t *src; 49460fe637bSDr. David Alan Gilbert 4957c1e52baSDr. David Alan Gilbert res = qemu_peek_buffer(f, &src, MIN(pending, IO_BUF_SIZE), 0); 49660fe637bSDr. David Alan Gilbert if (res == 0) { 49760fe637bSDr. David Alan Gilbert return done; 49860fe637bSDr. David Alan Gilbert } 4997c1e52baSDr. David Alan Gilbert memcpy(buf, src, res); 50060fe637bSDr. David Alan Gilbert qemu_file_skip(f, res); 50160fe637bSDr. David Alan Gilbert buf += res; 50260fe637bSDr. David Alan Gilbert pending -= res; 50360fe637bSDr. David Alan Gilbert done += res; 50460fe637bSDr. David Alan Gilbert } 50560fe637bSDr. David Alan Gilbert return done; 50660fe637bSDr. David Alan Gilbert } 50760fe637bSDr. David Alan Gilbert 50860fe637bSDr. David Alan Gilbert /* 5099504fb51SDr. David Alan Gilbert * Read 'size' bytes of data from the file. 5109504fb51SDr. David Alan Gilbert * 'size' can be larger than the internal buffer. 5119504fb51SDr. David Alan Gilbert * 5129504fb51SDr. David Alan Gilbert * The data: 5139504fb51SDr. David Alan Gilbert * may be held on an internal buffer (in which case *buf is updated 5149504fb51SDr. David Alan Gilbert * to point to it) that is valid until the next qemu_file operation. 5159504fb51SDr. David Alan Gilbert * OR 5169504fb51SDr. David Alan Gilbert * will be copied to the *buf that was passed in. 5179504fb51SDr. David Alan Gilbert * 5189504fb51SDr. David Alan Gilbert * The code tries to avoid the copy if possible. 5199504fb51SDr. David Alan Gilbert * 5209504fb51SDr. David Alan Gilbert * It will return size bytes unless there was an error, in which case it will 5219504fb51SDr. David Alan Gilbert * return as many as it managed to read (assuming blocking fd's which 5229504fb51SDr. David Alan Gilbert * all current QEMUFile are) 5239504fb51SDr. David Alan Gilbert * 5249504fb51SDr. David Alan Gilbert * Note: Since **buf may get changed, the caller should take care to 5259504fb51SDr. David Alan Gilbert * keep a pointer to the original buffer if it needs to deallocate it. 5269504fb51SDr. David Alan Gilbert */ 5279504fb51SDr. David Alan Gilbert size_t qemu_get_buffer_in_place(QEMUFile *f, uint8_t **buf, size_t size) 5289504fb51SDr. David Alan Gilbert { 5299504fb51SDr. David Alan Gilbert if (size < IO_BUF_SIZE) { 5309504fb51SDr. David Alan Gilbert size_t res; 5319504fb51SDr. David Alan Gilbert uint8_t *src; 5329504fb51SDr. David Alan Gilbert 5339504fb51SDr. David Alan Gilbert res = qemu_peek_buffer(f, &src, size, 0); 5349504fb51SDr. David Alan Gilbert 5359504fb51SDr. David Alan Gilbert if (res == size) { 5369504fb51SDr. David Alan Gilbert qemu_file_skip(f, res); 5379504fb51SDr. David Alan Gilbert *buf = src; 5389504fb51SDr. David Alan Gilbert return res; 5399504fb51SDr. David Alan Gilbert } 5409504fb51SDr. David Alan Gilbert } 5419504fb51SDr. David Alan Gilbert 5429504fb51SDr. David Alan Gilbert return qemu_get_buffer(f, *buf, size); 5439504fb51SDr. David Alan Gilbert } 5449504fb51SDr. David Alan Gilbert 5459504fb51SDr. David Alan Gilbert /* 54660fe637bSDr. David Alan Gilbert * Peeks a single byte from the buffer; this isn't guaranteed to work if 54760fe637bSDr. David Alan Gilbert * offset leaves a gap after the previous read/peeked data. 54860fe637bSDr. David Alan Gilbert */ 54960fe637bSDr. David Alan Gilbert int qemu_peek_byte(QEMUFile *f, int offset) 55060fe637bSDr. David Alan Gilbert { 55160fe637bSDr. David Alan Gilbert int index = f->buf_index + offset; 55260fe637bSDr. David Alan Gilbert 55360fe637bSDr. David Alan Gilbert assert(!qemu_file_is_writable(f)); 55460fe637bSDr. David Alan Gilbert assert(offset < IO_BUF_SIZE); 55560fe637bSDr. David Alan Gilbert 55660fe637bSDr. David Alan Gilbert if (index >= f->buf_size) { 55760fe637bSDr. David Alan Gilbert qemu_fill_buffer(f); 55860fe637bSDr. David Alan Gilbert index = f->buf_index + offset; 55960fe637bSDr. David Alan Gilbert if (index >= f->buf_size) { 56060fe637bSDr. David Alan Gilbert return 0; 56160fe637bSDr. David Alan Gilbert } 56260fe637bSDr. David Alan Gilbert } 56360fe637bSDr. David Alan Gilbert return f->buf[index]; 56460fe637bSDr. David Alan Gilbert } 56560fe637bSDr. David Alan Gilbert 56660fe637bSDr. David Alan Gilbert int qemu_get_byte(QEMUFile *f) 56760fe637bSDr. David Alan Gilbert { 56860fe637bSDr. David Alan Gilbert int result; 56960fe637bSDr. David Alan Gilbert 57060fe637bSDr. David Alan Gilbert result = qemu_peek_byte(f, 0); 57160fe637bSDr. David Alan Gilbert qemu_file_skip(f, 1); 57260fe637bSDr. David Alan Gilbert return result; 57360fe637bSDr. David Alan Gilbert } 57460fe637bSDr. David Alan Gilbert 57597221400SAlexander Graf int64_t qemu_ftell_fast(QEMUFile *f) 57697221400SAlexander Graf { 57797221400SAlexander Graf int64_t ret = f->pos; 57897221400SAlexander Graf int i; 57997221400SAlexander Graf 58097221400SAlexander Graf for (i = 0; i < f->iovcnt; i++) { 58197221400SAlexander Graf ret += f->iov[i].iov_len; 58297221400SAlexander Graf } 58397221400SAlexander Graf 58497221400SAlexander Graf return ret; 58597221400SAlexander Graf } 58697221400SAlexander Graf 58760fe637bSDr. David Alan Gilbert int64_t qemu_ftell(QEMUFile *f) 58860fe637bSDr. David Alan Gilbert { 58960fe637bSDr. David Alan Gilbert qemu_fflush(f); 59060fe637bSDr. David Alan Gilbert return f->pos; 59160fe637bSDr. David Alan Gilbert } 59260fe637bSDr. David Alan Gilbert 59360fe637bSDr. David Alan Gilbert int qemu_file_rate_limit(QEMUFile *f) 59460fe637bSDr. David Alan Gilbert { 59560fe637bSDr. David Alan Gilbert if (qemu_file_get_error(f)) { 59660fe637bSDr. David Alan Gilbert return 1; 59760fe637bSDr. David Alan Gilbert } 59860fe637bSDr. David Alan Gilbert if (f->xfer_limit > 0 && f->bytes_xfer > f->xfer_limit) { 59960fe637bSDr. David Alan Gilbert return 1; 60060fe637bSDr. David Alan Gilbert } 60160fe637bSDr. David Alan Gilbert return 0; 60260fe637bSDr. David Alan Gilbert } 60360fe637bSDr. David Alan Gilbert 60460fe637bSDr. David Alan Gilbert int64_t qemu_file_get_rate_limit(QEMUFile *f) 60560fe637bSDr. David Alan Gilbert { 60660fe637bSDr. David Alan Gilbert return f->xfer_limit; 60760fe637bSDr. David Alan Gilbert } 60860fe637bSDr. David Alan Gilbert 60960fe637bSDr. David Alan Gilbert void qemu_file_set_rate_limit(QEMUFile *f, int64_t limit) 61060fe637bSDr. David Alan Gilbert { 61160fe637bSDr. David Alan Gilbert f->xfer_limit = limit; 61260fe637bSDr. David Alan Gilbert } 61360fe637bSDr. David Alan Gilbert 61460fe637bSDr. David Alan Gilbert void qemu_file_reset_rate_limit(QEMUFile *f) 61560fe637bSDr. David Alan Gilbert { 61660fe637bSDr. David Alan Gilbert f->bytes_xfer = 0; 61760fe637bSDr. David Alan Gilbert } 61860fe637bSDr. David Alan Gilbert 61960fe637bSDr. David Alan Gilbert void qemu_put_be16(QEMUFile *f, unsigned int v) 62060fe637bSDr. David Alan Gilbert { 62160fe637bSDr. David Alan Gilbert qemu_put_byte(f, v >> 8); 62260fe637bSDr. David Alan Gilbert qemu_put_byte(f, v); 62360fe637bSDr. David Alan Gilbert } 62460fe637bSDr. David Alan Gilbert 62560fe637bSDr. David Alan Gilbert void qemu_put_be32(QEMUFile *f, unsigned int v) 62660fe637bSDr. David Alan Gilbert { 62760fe637bSDr. David Alan Gilbert qemu_put_byte(f, v >> 24); 62860fe637bSDr. David Alan Gilbert qemu_put_byte(f, v >> 16); 62960fe637bSDr. David Alan Gilbert qemu_put_byte(f, v >> 8); 63060fe637bSDr. David Alan Gilbert qemu_put_byte(f, v); 63160fe637bSDr. David Alan Gilbert } 63260fe637bSDr. David Alan Gilbert 63360fe637bSDr. David Alan Gilbert void qemu_put_be64(QEMUFile *f, uint64_t v) 63460fe637bSDr. David Alan Gilbert { 63560fe637bSDr. David Alan Gilbert qemu_put_be32(f, v >> 32); 63660fe637bSDr. David Alan Gilbert qemu_put_be32(f, v); 63760fe637bSDr. David Alan Gilbert } 63860fe637bSDr. David Alan Gilbert 63960fe637bSDr. David Alan Gilbert unsigned int qemu_get_be16(QEMUFile *f) 64060fe637bSDr. David Alan Gilbert { 64160fe637bSDr. David Alan Gilbert unsigned int v; 64260fe637bSDr. David Alan Gilbert v = qemu_get_byte(f) << 8; 64360fe637bSDr. David Alan Gilbert v |= qemu_get_byte(f); 64460fe637bSDr. David Alan Gilbert return v; 64560fe637bSDr. David Alan Gilbert } 64660fe637bSDr. David Alan Gilbert 64760fe637bSDr. David Alan Gilbert unsigned int qemu_get_be32(QEMUFile *f) 64860fe637bSDr. David Alan Gilbert { 64960fe637bSDr. David Alan Gilbert unsigned int v; 65090d6a673SPeter Maydell v = (unsigned int)qemu_get_byte(f) << 24; 65160fe637bSDr. David Alan Gilbert v |= qemu_get_byte(f) << 16; 65260fe637bSDr. David Alan Gilbert v |= qemu_get_byte(f) << 8; 65360fe637bSDr. David Alan Gilbert v |= qemu_get_byte(f); 65460fe637bSDr. David Alan Gilbert return v; 65560fe637bSDr. David Alan Gilbert } 65660fe637bSDr. David Alan Gilbert 65760fe637bSDr. David Alan Gilbert uint64_t qemu_get_be64(QEMUFile *f) 65860fe637bSDr. David Alan Gilbert { 65960fe637bSDr. David Alan Gilbert uint64_t v; 66060fe637bSDr. David Alan Gilbert v = (uint64_t)qemu_get_be32(f) << 32; 66160fe637bSDr. David Alan Gilbert v |= qemu_get_be32(f); 66260fe637bSDr. David Alan Gilbert return v; 66360fe637bSDr. David Alan Gilbert } 66444f0eadcSLiang Li 665dcaf446eSXiao Guangrong /* return the size after compression, or negative value on error */ 666dcaf446eSXiao Guangrong static int qemu_compress_data(z_stream *stream, uint8_t *dest, size_t dest_len, 667dcaf446eSXiao Guangrong const uint8_t *source, size_t source_len) 668dcaf446eSXiao Guangrong { 669dcaf446eSXiao Guangrong int err; 670dcaf446eSXiao Guangrong 671dcaf446eSXiao Guangrong err = deflateReset(stream); 672dcaf446eSXiao Guangrong if (err != Z_OK) { 673dcaf446eSXiao Guangrong return -1; 674dcaf446eSXiao Guangrong } 675dcaf446eSXiao Guangrong 676dcaf446eSXiao Guangrong stream->avail_in = source_len; 677dcaf446eSXiao Guangrong stream->next_in = (uint8_t *)source; 678dcaf446eSXiao Guangrong stream->avail_out = dest_len; 679dcaf446eSXiao Guangrong stream->next_out = dest; 680dcaf446eSXiao Guangrong 681dcaf446eSXiao Guangrong err = deflate(stream, Z_FINISH); 682dcaf446eSXiao Guangrong if (err != Z_STREAM_END) { 683dcaf446eSXiao Guangrong return -1; 684dcaf446eSXiao Guangrong } 685dcaf446eSXiao Guangrong 686dcaf446eSXiao Guangrong return stream->next_out - dest; 687dcaf446eSXiao Guangrong } 688dcaf446eSXiao Guangrong 689dcaf446eSXiao Guangrong /* Compress size bytes of data start at p and store the compressed 690dcaf446eSXiao Guangrong * data to the buffer of f. 691b3be2896SLiang Li * 692b3be2896SLiang Li * When f is not writable, return -1 if f has no space to save the 693b3be2896SLiang Li * compressed data. 694b3be2896SLiang Li * When f is wirtable and it has no space to save the compressed data, 695b3be2896SLiang Li * do fflush first, if f still has no space to save the compressed 696b3be2896SLiang Li * data, return -1. 69744f0eadcSLiang Li */ 698dcaf446eSXiao Guangrong ssize_t qemu_put_compression_data(QEMUFile *f, z_stream *stream, 699dcaf446eSXiao Guangrong const uint8_t *p, size_t size) 70044f0eadcSLiang Li { 70144f0eadcSLiang Li ssize_t blen = IO_BUF_SIZE - f->buf_index - sizeof(int32_t); 70244f0eadcSLiang Li 70344f0eadcSLiang Li if (blen < compressBound(size)) { 704b3be2896SLiang Li if (!qemu_file_is_writable(f)) { 705b3be2896SLiang Li return -1; 706b3be2896SLiang Li } 707b3be2896SLiang Li qemu_fflush(f); 708b3be2896SLiang Li blen = IO_BUF_SIZE - sizeof(int32_t); 709b3be2896SLiang Li if (blen < compressBound(size)) { 710b3be2896SLiang Li return -1; 711b3be2896SLiang Li } 71244f0eadcSLiang Li } 713dcaf446eSXiao Guangrong 714dcaf446eSXiao Guangrong blen = qemu_compress_data(stream, f->buf + f->buf_index + sizeof(int32_t), 715dcaf446eSXiao Guangrong blen, p, size); 716dcaf446eSXiao Guangrong if (blen < 0) { 71734ab9e97SXiao Guangrong return -1; 71844f0eadcSLiang Li } 71934ab9e97SXiao Guangrong 72044f0eadcSLiang Li qemu_put_be32(f, blen); 721b3be2896SLiang Li if (f->ops->writev_buffer) { 72253f09a10SPavel Butsykin add_to_iovec(f, f->buf + f->buf_index, blen, false); 723b3be2896SLiang Li } 72444f0eadcSLiang Li f->buf_index += blen; 725b3be2896SLiang Li if (f->buf_index == IO_BUF_SIZE) { 726b3be2896SLiang Li qemu_fflush(f); 727b3be2896SLiang Li } 72844f0eadcSLiang Li return blen + sizeof(int32_t); 72944f0eadcSLiang Li } 73044f0eadcSLiang Li 73144f0eadcSLiang Li /* Put the data in the buffer of f_src to the buffer of f_des, and 73244f0eadcSLiang Li * then reset the buf_index of f_src to 0. 73344f0eadcSLiang Li */ 73444f0eadcSLiang Li 73544f0eadcSLiang Li int qemu_put_qemu_file(QEMUFile *f_des, QEMUFile *f_src) 73644f0eadcSLiang Li { 73744f0eadcSLiang Li int len = 0; 73844f0eadcSLiang Li 73944f0eadcSLiang Li if (f_src->buf_index > 0) { 74044f0eadcSLiang Li len = f_src->buf_index; 74144f0eadcSLiang Li qemu_put_buffer(f_des, f_src->buf, f_src->buf_index); 74244f0eadcSLiang Li f_src->buf_index = 0; 743787d134fSLiang Li f_src->iovcnt = 0; 74444f0eadcSLiang Li } 74544f0eadcSLiang Li return len; 74644f0eadcSLiang Li } 747b3af1bc9SDr. David Alan Gilbert 748b3af1bc9SDr. David Alan Gilbert /* 749b3af1bc9SDr. David Alan Gilbert * Get a string whose length is determined by a single preceding byte 750b3af1bc9SDr. David Alan Gilbert * A preallocated 256 byte buffer must be passed in. 751b3af1bc9SDr. David Alan Gilbert * Returns: len on success and a 0 terminated string in the buffer 752b3af1bc9SDr. David Alan Gilbert * else 0 753b3af1bc9SDr. David Alan Gilbert * (Note a 0 length string will return 0 either way) 754b3af1bc9SDr. David Alan Gilbert */ 755b3af1bc9SDr. David Alan Gilbert size_t qemu_get_counted_string(QEMUFile *f, char buf[256]) 756b3af1bc9SDr. David Alan Gilbert { 757b3af1bc9SDr. David Alan Gilbert size_t len = qemu_get_byte(f); 758b3af1bc9SDr. David Alan Gilbert size_t res = qemu_get_buffer(f, (uint8_t *)buf, len); 759b3af1bc9SDr. David Alan Gilbert 760b3af1bc9SDr. David Alan Gilbert buf[res] = 0; 761b3af1bc9SDr. David Alan Gilbert 762b3af1bc9SDr. David Alan Gilbert return res == len ? res : 0; 763b3af1bc9SDr. David Alan Gilbert } 764a800cd5cSDr. David Alan Gilbert 765a800cd5cSDr. David Alan Gilbert /* 766f0d64cb7SVladimir Sementsov-Ogievskiy * Put a string with one preceding byte containing its length. The length of 767f0d64cb7SVladimir Sementsov-Ogievskiy * the string should be less than 256. 768f0d64cb7SVladimir Sementsov-Ogievskiy */ 769f0d64cb7SVladimir Sementsov-Ogievskiy void qemu_put_counted_string(QEMUFile *f, const char *str) 770f0d64cb7SVladimir Sementsov-Ogievskiy { 771f0d64cb7SVladimir Sementsov-Ogievskiy size_t len = strlen(str); 772f0d64cb7SVladimir Sementsov-Ogievskiy 773f0d64cb7SVladimir Sementsov-Ogievskiy assert(len < 256); 774f0d64cb7SVladimir Sementsov-Ogievskiy qemu_put_byte(f, len); 775f0d64cb7SVladimir Sementsov-Ogievskiy qemu_put_buffer(f, (const uint8_t *)str, len); 776f0d64cb7SVladimir Sementsov-Ogievskiy } 777f0d64cb7SVladimir Sementsov-Ogievskiy 778f0d64cb7SVladimir Sementsov-Ogievskiy /* 779a800cd5cSDr. David Alan Gilbert * Set the blocking state of the QEMUFile. 780a800cd5cSDr. David Alan Gilbert * Note: On some transports the OS only keeps a single blocking state for 781a800cd5cSDr. David Alan Gilbert * both directions, and thus changing the blocking on the main 782a800cd5cSDr. David Alan Gilbert * QEMUFile can also affect the return path. 783a800cd5cSDr. David Alan Gilbert */ 784a800cd5cSDr. David Alan Gilbert void qemu_file_set_blocking(QEMUFile *f, bool block) 785a800cd5cSDr. David Alan Gilbert { 78606ad5135SDaniel P. Berrange if (f->ops->set_blocking) { 78706ad5135SDaniel P. Berrange f->ops->set_blocking(f->opaque, block); 788a800cd5cSDr. David Alan Gilbert } 78906ad5135SDaniel P. Berrange } 790