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> 26d49b6836SMarkus Armbruster #include "qemu/error-report.h" 2760fe637bSDr. David Alan Gilbert #include "qemu/iov.h" 286666c96aSJuan Quintela #include "migration.h" 2908a0aee1SJuan Quintela #include "qemu-file.h" 3060fe637bSDr. David Alan Gilbert #include "trace.h" 313d661c8aSYury Kotov #include "qapi/error.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; 553d661c8aSYury Kotov Error *last_error_obj; 56a24939f2SDaniel P. Berrange }; 57a24939f2SDaniel P. Berrange 58e1a8c9b6SDr. David Alan Gilbert /* 59e1a8c9b6SDr. David Alan Gilbert * Stop a file from being read/written - not all backing files can do this 60e1a8c9b6SDr. David Alan Gilbert * typically only sockets can. 61e1a8c9b6SDr. David Alan Gilbert */ 62e1a8c9b6SDr. David Alan Gilbert int qemu_file_shutdown(QEMUFile *f) 63e1a8c9b6SDr. David Alan Gilbert { 64e1a8c9b6SDr. David Alan Gilbert if (!f->ops->shut_down) { 65e1a8c9b6SDr. David Alan Gilbert return -ENOSYS; 66e1a8c9b6SDr. David Alan Gilbert } 673d661c8aSYury Kotov return f->ops->shut_down(f->opaque, true, true, NULL); 68e1a8c9b6SDr. David Alan Gilbert } 69e1a8c9b6SDr. David Alan Gilbert 70adc468e9SDr. David Alan Gilbert /* 71adc468e9SDr. David Alan Gilbert * Result: QEMUFile* for a 'return path' for comms in the opposite direction 72adc468e9SDr. David Alan Gilbert * NULL if not available 73adc468e9SDr. David Alan Gilbert */ 74adc468e9SDr. David Alan Gilbert QEMUFile *qemu_file_get_return_path(QEMUFile *f) 75adc468e9SDr. David Alan Gilbert { 76adc468e9SDr. David Alan Gilbert if (!f->ops->get_return_path) { 77adc468e9SDr. David Alan Gilbert return NULL; 78adc468e9SDr. David Alan Gilbert } 79adc468e9SDr. David Alan Gilbert return f->ops->get_return_path(f->opaque); 80adc468e9SDr. David Alan Gilbert } 81adc468e9SDr. David Alan Gilbert 8260fe637bSDr. David Alan Gilbert bool qemu_file_mode_is_not_valid(const char *mode) 8360fe637bSDr. David Alan Gilbert { 8460fe637bSDr. David Alan Gilbert if (mode == NULL || 8560fe637bSDr. David Alan Gilbert (mode[0] != 'r' && mode[0] != 'w') || 8660fe637bSDr. David Alan Gilbert mode[1] != 'b' || mode[2] != 0) { 8760fe637bSDr. David Alan Gilbert fprintf(stderr, "qemu_fopen: Argument validity check failed\n"); 8860fe637bSDr. David Alan Gilbert return true; 8960fe637bSDr. David Alan Gilbert } 9060fe637bSDr. David Alan Gilbert 9160fe637bSDr. David Alan Gilbert return false; 9260fe637bSDr. David Alan Gilbert } 9360fe637bSDr. David Alan Gilbert 9460fe637bSDr. David Alan Gilbert QEMUFile *qemu_fopen_ops(void *opaque, const QEMUFileOps *ops) 9560fe637bSDr. David Alan Gilbert { 9660fe637bSDr. David Alan Gilbert QEMUFile *f; 9760fe637bSDr. David Alan Gilbert 9897f3ad35SMarkus Armbruster f = g_new0(QEMUFile, 1); 9960fe637bSDr. David Alan Gilbert 10060fe637bSDr. David Alan Gilbert f->opaque = opaque; 10160fe637bSDr. David Alan Gilbert f->ops = ops; 10260fe637bSDr. David Alan Gilbert return f; 10360fe637bSDr. David Alan Gilbert } 10460fe637bSDr. David Alan Gilbert 1050436e09fSDaniel P. Berrange 1060436e09fSDaniel P. Berrange void qemu_file_set_hooks(QEMUFile *f, const QEMUFileHooks *hooks) 1070436e09fSDaniel P. Berrange { 1080436e09fSDaniel P. Berrange f->hooks = hooks; 1090436e09fSDaniel P. Berrange } 1100436e09fSDaniel P. Berrange 11160fe637bSDr. David Alan Gilbert /* 1123d661c8aSYury Kotov * Get last error for stream f with optional Error* 1133d661c8aSYury Kotov * 1143d661c8aSYury Kotov * Return negative error value if there has been an error on previous 1153d661c8aSYury Kotov * operations, return 0 if no error happened. 1163d661c8aSYury Kotov * Optional, it returns Error* in errp, but it may be NULL even if return value 1173d661c8aSYury Kotov * is not 0. 1183d661c8aSYury Kotov * 1193d661c8aSYury Kotov */ 1203d661c8aSYury Kotov int qemu_file_get_error_obj(QEMUFile *f, Error **errp) 1213d661c8aSYury Kotov { 1223d661c8aSYury Kotov if (errp) { 1233d661c8aSYury Kotov *errp = f->last_error_obj ? error_copy(f->last_error_obj) : NULL; 1243d661c8aSYury Kotov } 1253d661c8aSYury Kotov return f->last_error; 1263d661c8aSYury Kotov } 1273d661c8aSYury Kotov 1283d661c8aSYury Kotov /* 1293d661c8aSYury Kotov * Set the last error for stream f with optional Error* 1303d661c8aSYury Kotov */ 1313d661c8aSYury Kotov void qemu_file_set_error_obj(QEMUFile *f, int ret, Error *err) 1323d661c8aSYury Kotov { 1333d661c8aSYury Kotov if (f->last_error == 0 && ret) { 1343d661c8aSYury Kotov f->last_error = ret; 1353d661c8aSYury Kotov error_propagate(&f->last_error_obj, err); 1363d661c8aSYury Kotov } else if (err) { 1373d661c8aSYury Kotov error_report_err(err); 1383d661c8aSYury Kotov } 1393d661c8aSYury Kotov } 1403d661c8aSYury Kotov 1413d661c8aSYury Kotov /* 14260fe637bSDr. David Alan Gilbert * Get last error for stream f 14360fe637bSDr. David Alan Gilbert * 14460fe637bSDr. David Alan Gilbert * Return negative error value if there has been an error on previous 14560fe637bSDr. David Alan Gilbert * operations, return 0 if no error happened. 14660fe637bSDr. David Alan Gilbert * 14760fe637bSDr. David Alan Gilbert */ 14860fe637bSDr. David Alan Gilbert int qemu_file_get_error(QEMUFile *f) 14960fe637bSDr. David Alan Gilbert { 1503d661c8aSYury Kotov return qemu_file_get_error_obj(f, NULL); 15160fe637bSDr. David Alan Gilbert } 15260fe637bSDr. David Alan Gilbert 1533d661c8aSYury Kotov /* 1543d661c8aSYury Kotov * Set the last error for stream f 1553d661c8aSYury Kotov */ 15660fe637bSDr. David Alan Gilbert void qemu_file_set_error(QEMUFile *f, int ret) 15760fe637bSDr. David Alan Gilbert { 1583d661c8aSYury Kotov qemu_file_set_error_obj(f, ret, NULL); 15960fe637bSDr. David Alan Gilbert } 16060fe637bSDr. David Alan Gilbert 16160fe637bSDr. David Alan Gilbert bool qemu_file_is_writable(QEMUFile *f) 16260fe637bSDr. David Alan Gilbert { 16311808bb0SDaniel P. Berrange return f->ops->writev_buffer; 16460fe637bSDr. David Alan Gilbert } 16560fe637bSDr. David Alan Gilbert 16653f09a10SPavel Butsykin static void qemu_iovec_release_ram(QEMUFile *f) 16753f09a10SPavel Butsykin { 16853f09a10SPavel Butsykin struct iovec iov; 16953f09a10SPavel Butsykin unsigned long idx; 17053f09a10SPavel Butsykin 17153f09a10SPavel Butsykin /* Find and release all the contiguous memory ranges marked as may_free. */ 17253f09a10SPavel Butsykin idx = find_next_bit(f->may_free, f->iovcnt, 0); 17353f09a10SPavel Butsykin if (idx >= f->iovcnt) { 17453f09a10SPavel Butsykin return; 17553f09a10SPavel Butsykin } 17653f09a10SPavel Butsykin iov = f->iov[idx]; 17753f09a10SPavel Butsykin 17853f09a10SPavel Butsykin /* The madvise() in the loop is called for iov within a continuous range and 17953f09a10SPavel Butsykin * then reinitialize the iov. And in the end, madvise() is called for the 18053f09a10SPavel Butsykin * last iov. 18153f09a10SPavel Butsykin */ 18253f09a10SPavel Butsykin while ((idx = find_next_bit(f->may_free, f->iovcnt, idx + 1)) < f->iovcnt) { 18353f09a10SPavel Butsykin /* check for adjacent buffer and coalesce them */ 18453f09a10SPavel Butsykin if (iov.iov_base + iov.iov_len == f->iov[idx].iov_base) { 18553f09a10SPavel Butsykin iov.iov_len += f->iov[idx].iov_len; 18653f09a10SPavel Butsykin continue; 18753f09a10SPavel Butsykin } 18853f09a10SPavel Butsykin if (qemu_madvise(iov.iov_base, iov.iov_len, QEMU_MADV_DONTNEED) < 0) { 18953f09a10SPavel Butsykin error_report("migrate: madvise DONTNEED failed %p %zd: %s", 19053f09a10SPavel Butsykin iov.iov_base, iov.iov_len, strerror(errno)); 19153f09a10SPavel Butsykin } 19253f09a10SPavel Butsykin iov = f->iov[idx]; 19353f09a10SPavel Butsykin } 19453f09a10SPavel Butsykin if (qemu_madvise(iov.iov_base, iov.iov_len, QEMU_MADV_DONTNEED) < 0) { 19553f09a10SPavel Butsykin error_report("migrate: madvise DONTNEED failed %p %zd: %s", 19653f09a10SPavel Butsykin iov.iov_base, iov.iov_len, strerror(errno)); 19753f09a10SPavel Butsykin } 19853f09a10SPavel Butsykin memset(f->may_free, 0, sizeof(f->may_free)); 19953f09a10SPavel Butsykin } 20053f09a10SPavel Butsykin 20160fe637bSDr. David Alan Gilbert /** 20260fe637bSDr. David Alan Gilbert * Flushes QEMUFile buffer 20360fe637bSDr. David Alan Gilbert * 20460fe637bSDr. David Alan Gilbert * If there is writev_buffer QEMUFileOps it uses it otherwise uses 205baf51e77SDaniel P. Berrange * put_buffer ops. This will flush all pending data. If data was 206baf51e77SDaniel P. Berrange * only partially flushed, it will set an error state. 20760fe637bSDr. David Alan Gilbert */ 20860fe637bSDr. David Alan Gilbert void qemu_fflush(QEMUFile *f) 20960fe637bSDr. David Alan Gilbert { 21060fe637bSDr. David Alan Gilbert ssize_t ret = 0; 211baf51e77SDaniel P. Berrange ssize_t expect = 0; 2123d661c8aSYury Kotov Error *local_error = NULL; 21360fe637bSDr. David Alan Gilbert 21460fe637bSDr. David Alan Gilbert if (!qemu_file_is_writable(f)) { 21560fe637bSDr. David Alan Gilbert return; 21660fe637bSDr. David Alan Gilbert } 21760fe637bSDr. David Alan Gilbert 21860fe637bSDr. David Alan Gilbert if (f->iovcnt > 0) { 219baf51e77SDaniel P. Berrange expect = iov_size(f->iov, f->iovcnt); 2203d661c8aSYury Kotov ret = f->ops->writev_buffer(f->opaque, f->iov, f->iovcnt, f->pos, 2213d661c8aSYury Kotov &local_error); 22253f09a10SPavel Butsykin 22353f09a10SPavel Butsykin qemu_iovec_release_ram(f); 22460fe637bSDr. David Alan Gilbert } 225baf51e77SDaniel P. Berrange 22660fe637bSDr. David Alan Gilbert if (ret >= 0) { 22760fe637bSDr. David Alan Gilbert f->pos += ret; 22860fe637bSDr. David Alan Gilbert } 229baf51e77SDaniel P. Berrange /* We expect the QEMUFile write impl to send the full 230baf51e77SDaniel P. Berrange * data set we requested, so sanity check that. 231baf51e77SDaniel P. Berrange */ 232baf51e77SDaniel P. Berrange if (ret != expect) { 2333d661c8aSYury Kotov qemu_file_set_error_obj(f, ret < 0 ? ret : -EIO, local_error); 234baf51e77SDaniel P. Berrange } 23560fe637bSDr. David Alan Gilbert f->buf_index = 0; 23660fe637bSDr. David Alan Gilbert f->iovcnt = 0; 23760fe637bSDr. David Alan Gilbert } 23860fe637bSDr. David Alan Gilbert 23960fe637bSDr. David Alan Gilbert void ram_control_before_iterate(QEMUFile *f, uint64_t flags) 24060fe637bSDr. David Alan Gilbert { 24160fe637bSDr. David Alan Gilbert int ret = 0; 24260fe637bSDr. David Alan Gilbert 2430436e09fSDaniel P. Berrange if (f->hooks && f->hooks->before_ram_iterate) { 2440436e09fSDaniel P. Berrange ret = f->hooks->before_ram_iterate(f, f->opaque, flags, NULL); 24560fe637bSDr. David Alan Gilbert if (ret < 0) { 24660fe637bSDr. David Alan Gilbert qemu_file_set_error(f, ret); 24760fe637bSDr. David Alan Gilbert } 24860fe637bSDr. David Alan Gilbert } 24960fe637bSDr. David Alan Gilbert } 25060fe637bSDr. David Alan Gilbert 25160fe637bSDr. David Alan Gilbert void ram_control_after_iterate(QEMUFile *f, uint64_t flags) 25260fe637bSDr. David Alan Gilbert { 25360fe637bSDr. David Alan Gilbert int ret = 0; 25460fe637bSDr. David Alan Gilbert 2550436e09fSDaniel P. Berrange if (f->hooks && f->hooks->after_ram_iterate) { 2560436e09fSDaniel P. Berrange ret = f->hooks->after_ram_iterate(f, f->opaque, flags, NULL); 25760fe637bSDr. David Alan Gilbert if (ret < 0) { 25860fe637bSDr. David Alan Gilbert qemu_file_set_error(f, ret); 25960fe637bSDr. David Alan Gilbert } 26060fe637bSDr. David Alan Gilbert } 26160fe637bSDr. David Alan Gilbert } 26260fe637bSDr. David Alan Gilbert 263632e3a5cSDr. David Alan Gilbert void ram_control_load_hook(QEMUFile *f, uint64_t flags, void *data) 26460fe637bSDr. David Alan Gilbert { 26560fe637bSDr. David Alan Gilbert int ret = -EINVAL; 26660fe637bSDr. David Alan Gilbert 2670436e09fSDaniel P. Berrange if (f->hooks && f->hooks->hook_ram_load) { 2680436e09fSDaniel P. Berrange ret = f->hooks->hook_ram_load(f, f->opaque, flags, data); 26960fe637bSDr. David Alan Gilbert if (ret < 0) { 27060fe637bSDr. David Alan Gilbert qemu_file_set_error(f, ret); 27160fe637bSDr. David Alan Gilbert } 27260fe637bSDr. David Alan Gilbert } else { 273632e3a5cSDr. David Alan Gilbert /* 274632e3a5cSDr. David Alan Gilbert * Hook is a hook specifically requested by the source sending a flag 275632e3a5cSDr. David Alan Gilbert * that expects there to be a hook on the destination. 276632e3a5cSDr. David Alan Gilbert */ 277632e3a5cSDr. David Alan Gilbert if (flags == RAM_CONTROL_HOOK) { 27860fe637bSDr. David Alan Gilbert qemu_file_set_error(f, ret); 27960fe637bSDr. David Alan Gilbert } 28060fe637bSDr. David Alan Gilbert } 281632e3a5cSDr. David Alan Gilbert } 28260fe637bSDr. David Alan Gilbert 28360fe637bSDr. David Alan Gilbert size_t ram_control_save_page(QEMUFile *f, ram_addr_t block_offset, 2846e1dea46SJuan Quintela ram_addr_t offset, size_t size, 2856e1dea46SJuan Quintela uint64_t *bytes_sent) 28660fe637bSDr. David Alan Gilbert { 2870436e09fSDaniel P. Berrange if (f->hooks && f->hooks->save_page) { 2880436e09fSDaniel P. Berrange int ret = f->hooks->save_page(f, f->opaque, block_offset, 28960fe637bSDr. David Alan Gilbert offset, size, bytes_sent); 290ccb7e1b5SLidong Chen if (ret != RAM_SAVE_CONTROL_NOT_SUPP) { 291e8a0f2f9SLidong Chen f->bytes_xfer += size; 292ccb7e1b5SLidong Chen } 293ccb7e1b5SLidong Chen 294ccb7e1b5SLidong Chen if (ret != RAM_SAVE_CONTROL_DELAYED && 295ccb7e1b5SLidong Chen ret != RAM_SAVE_CONTROL_NOT_SUPP) { 29660fe637bSDr. David Alan Gilbert if (bytes_sent && *bytes_sent > 0) { 29760fe637bSDr. David Alan Gilbert qemu_update_position(f, *bytes_sent); 29860fe637bSDr. David Alan Gilbert } else if (ret < 0) { 29960fe637bSDr. David Alan Gilbert qemu_file_set_error(f, ret); 30060fe637bSDr. David Alan Gilbert } 30160fe637bSDr. David Alan Gilbert } 30260fe637bSDr. David Alan Gilbert 30360fe637bSDr. David Alan Gilbert return ret; 30460fe637bSDr. David Alan Gilbert } 30560fe637bSDr. David Alan Gilbert 30660fe637bSDr. David Alan Gilbert return RAM_SAVE_CONTROL_NOT_SUPP; 30760fe637bSDr. David Alan Gilbert } 30860fe637bSDr. David Alan Gilbert 30960fe637bSDr. David Alan Gilbert /* 31060fe637bSDr. David Alan Gilbert * Attempt to fill the buffer from the underlying file 31160fe637bSDr. David Alan Gilbert * Returns the number of bytes read, or negative value for an error. 31260fe637bSDr. David Alan Gilbert * 31360fe637bSDr. David Alan Gilbert * Note that it can return a partially full buffer even in a not error/not EOF 31460fe637bSDr. David Alan Gilbert * case if the underlying file descriptor gives a short read, and that can 31560fe637bSDr. David Alan Gilbert * happen even on a blocking fd. 31660fe637bSDr. David Alan Gilbert */ 31760fe637bSDr. David Alan Gilbert static ssize_t qemu_fill_buffer(QEMUFile *f) 31860fe637bSDr. David Alan Gilbert { 31960fe637bSDr. David Alan Gilbert int len; 32060fe637bSDr. David Alan Gilbert int pending; 3213d661c8aSYury Kotov Error *local_error = NULL; 32260fe637bSDr. David Alan Gilbert 32360fe637bSDr. David Alan Gilbert assert(!qemu_file_is_writable(f)); 32460fe637bSDr. David Alan Gilbert 32560fe637bSDr. David Alan Gilbert pending = f->buf_size - f->buf_index; 32660fe637bSDr. David Alan Gilbert if (pending > 0) { 32760fe637bSDr. David Alan Gilbert memmove(f->buf, f->buf + f->buf_index, pending); 32860fe637bSDr. David Alan Gilbert } 32960fe637bSDr. David Alan Gilbert f->buf_index = 0; 33060fe637bSDr. David Alan Gilbert f->buf_size = pending; 33160fe637bSDr. David Alan Gilbert 33260fe637bSDr. David Alan Gilbert len = f->ops->get_buffer(f->opaque, f->buf + pending, f->pos, 3333d661c8aSYury Kotov IO_BUF_SIZE - pending, &local_error); 33460fe637bSDr. David Alan Gilbert if (len > 0) { 33560fe637bSDr. David Alan Gilbert f->buf_size += len; 33660fe637bSDr. David Alan Gilbert f->pos += len; 33760fe637bSDr. David Alan Gilbert } else if (len == 0) { 3383d661c8aSYury Kotov qemu_file_set_error_obj(f, -EIO, local_error); 33960fe637bSDr. David Alan Gilbert } else if (len != -EAGAIN) { 3403d661c8aSYury Kotov qemu_file_set_error_obj(f, len, local_error); 3413d661c8aSYury Kotov } else { 3423d661c8aSYury Kotov error_free(local_error); 34360fe637bSDr. David Alan Gilbert } 34460fe637bSDr. David Alan Gilbert 34560fe637bSDr. David Alan Gilbert return len; 34660fe637bSDr. David Alan Gilbert } 34760fe637bSDr. David Alan Gilbert 34860fe637bSDr. David Alan Gilbert void qemu_update_position(QEMUFile *f, size_t size) 34960fe637bSDr. David Alan Gilbert { 35060fe637bSDr. David Alan Gilbert f->pos += size; 35160fe637bSDr. David Alan Gilbert } 35260fe637bSDr. David Alan Gilbert 35360fe637bSDr. David Alan Gilbert /** Closes the file 35460fe637bSDr. David Alan Gilbert * 35560fe637bSDr. David Alan Gilbert * Returns negative error value if any error happened on previous operations or 35660fe637bSDr. David Alan Gilbert * while closing the file. Returns 0 or positive number on success. 35760fe637bSDr. David Alan Gilbert * 35860fe637bSDr. David Alan Gilbert * The meaning of return value on success depends on the specific backend 35960fe637bSDr. David Alan Gilbert * being used. 36060fe637bSDr. David Alan Gilbert */ 36160fe637bSDr. David Alan Gilbert int qemu_fclose(QEMUFile *f) 36260fe637bSDr. David Alan Gilbert { 36360fe637bSDr. David Alan Gilbert int ret; 36460fe637bSDr. David Alan Gilbert qemu_fflush(f); 36560fe637bSDr. David Alan Gilbert ret = qemu_file_get_error(f); 36660fe637bSDr. David Alan Gilbert 36760fe637bSDr. David Alan Gilbert if (f->ops->close) { 3683d661c8aSYury Kotov int ret2 = f->ops->close(f->opaque, NULL); 36960fe637bSDr. David Alan Gilbert if (ret >= 0) { 37060fe637bSDr. David Alan Gilbert ret = ret2; 37160fe637bSDr. David Alan Gilbert } 37260fe637bSDr. David Alan Gilbert } 37360fe637bSDr. David Alan Gilbert /* If any error was spotted before closing, we should report it 37460fe637bSDr. David Alan Gilbert * instead of the close() return value. 37560fe637bSDr. David Alan Gilbert */ 37660fe637bSDr. David Alan Gilbert if (f->last_error) { 37760fe637bSDr. David Alan Gilbert ret = f->last_error; 37860fe637bSDr. David Alan Gilbert } 3793d661c8aSYury Kotov error_free(f->last_error_obj); 38060fe637bSDr. David Alan Gilbert g_free(f); 38160fe637bSDr. David Alan Gilbert trace_qemu_file_fclose(); 38260fe637bSDr. David Alan Gilbert return ret; 38360fe637bSDr. David Alan Gilbert } 38460fe637bSDr. David Alan Gilbert 38553f09a10SPavel Butsykin static void add_to_iovec(QEMUFile *f, const uint8_t *buf, size_t size, 38653f09a10SPavel Butsykin bool may_free) 38760fe637bSDr. David Alan Gilbert { 38860fe637bSDr. David Alan Gilbert /* check for adjacent buffer and coalesce them */ 38960fe637bSDr. David Alan Gilbert if (f->iovcnt > 0 && buf == f->iov[f->iovcnt - 1].iov_base + 39053f09a10SPavel Butsykin f->iov[f->iovcnt - 1].iov_len && 39153f09a10SPavel Butsykin may_free == test_bit(f->iovcnt - 1, f->may_free)) 39253f09a10SPavel Butsykin { 39360fe637bSDr. David Alan Gilbert f->iov[f->iovcnt - 1].iov_len += size; 39460fe637bSDr. David Alan Gilbert } else { 39553f09a10SPavel Butsykin if (may_free) { 39653f09a10SPavel Butsykin set_bit(f->iovcnt, f->may_free); 39753f09a10SPavel Butsykin } 39860fe637bSDr. David Alan Gilbert f->iov[f->iovcnt].iov_base = (uint8_t *)buf; 39960fe637bSDr. David Alan Gilbert f->iov[f->iovcnt++].iov_len = size; 40060fe637bSDr. David Alan Gilbert } 40160fe637bSDr. David Alan Gilbert 40260fe637bSDr. David Alan Gilbert if (f->iovcnt >= MAX_IOV_SIZE) { 40360fe637bSDr. David Alan Gilbert qemu_fflush(f); 40460fe637bSDr. David Alan Gilbert } 40560fe637bSDr. David Alan Gilbert } 40660fe637bSDr. David Alan Gilbert 40753f09a10SPavel Butsykin void qemu_put_buffer_async(QEMUFile *f, const uint8_t *buf, size_t size, 40853f09a10SPavel Butsykin bool may_free) 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->bytes_xfer += size; 41553f09a10SPavel Butsykin add_to_iovec(f, buf, size, may_free); 41660fe637bSDr. David Alan Gilbert } 41760fe637bSDr. David Alan Gilbert 41856f3835fSDr. David Alan Gilbert void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, size_t size) 41960fe637bSDr. David Alan Gilbert { 42056f3835fSDr. David Alan Gilbert size_t l; 42160fe637bSDr. David Alan Gilbert 42260fe637bSDr. David Alan Gilbert if (f->last_error) { 42360fe637bSDr. David Alan Gilbert return; 42460fe637bSDr. David Alan Gilbert } 42560fe637bSDr. David Alan Gilbert 42660fe637bSDr. David Alan Gilbert while (size > 0) { 42760fe637bSDr. David Alan Gilbert l = IO_BUF_SIZE - f->buf_index; 42860fe637bSDr. David Alan Gilbert if (l > size) { 42960fe637bSDr. David Alan Gilbert l = size; 43060fe637bSDr. David Alan Gilbert } 43160fe637bSDr. David Alan Gilbert memcpy(f->buf + f->buf_index, buf, l); 43260fe637bSDr. David Alan Gilbert f->bytes_xfer += l; 43353f09a10SPavel Butsykin add_to_iovec(f, f->buf + f->buf_index, l, false); 43460fe637bSDr. David Alan Gilbert f->buf_index += l; 43560fe637bSDr. David Alan Gilbert if (f->buf_index == IO_BUF_SIZE) { 43660fe637bSDr. David Alan Gilbert qemu_fflush(f); 43760fe637bSDr. David Alan Gilbert } 43860fe637bSDr. David Alan Gilbert if (qemu_file_get_error(f)) { 43960fe637bSDr. David Alan Gilbert break; 44060fe637bSDr. David Alan Gilbert } 44160fe637bSDr. David Alan Gilbert buf += l; 44260fe637bSDr. David Alan Gilbert size -= l; 44360fe637bSDr. David Alan Gilbert } 44460fe637bSDr. David Alan Gilbert } 44560fe637bSDr. David Alan Gilbert 44660fe637bSDr. David Alan Gilbert void qemu_put_byte(QEMUFile *f, int v) 44760fe637bSDr. David Alan Gilbert { 44860fe637bSDr. David Alan Gilbert if (f->last_error) { 44960fe637bSDr. David Alan Gilbert return; 45060fe637bSDr. David Alan Gilbert } 45160fe637bSDr. David Alan Gilbert 45260fe637bSDr. David Alan Gilbert f->buf[f->buf_index] = v; 45360fe637bSDr. David Alan Gilbert f->bytes_xfer++; 45453f09a10SPavel Butsykin add_to_iovec(f, f->buf + f->buf_index, 1, false); 45560fe637bSDr. David Alan Gilbert f->buf_index++; 45660fe637bSDr. David Alan Gilbert if (f->buf_index == IO_BUF_SIZE) { 45760fe637bSDr. David Alan Gilbert qemu_fflush(f); 45860fe637bSDr. David Alan Gilbert } 45960fe637bSDr. David Alan Gilbert } 46060fe637bSDr. David Alan Gilbert 46160fe637bSDr. David Alan Gilbert void qemu_file_skip(QEMUFile *f, int size) 46260fe637bSDr. David Alan Gilbert { 46360fe637bSDr. David Alan Gilbert if (f->buf_index + size <= f->buf_size) { 46460fe637bSDr. David Alan Gilbert f->buf_index += size; 46560fe637bSDr. David Alan Gilbert } 46660fe637bSDr. David Alan Gilbert } 46760fe637bSDr. David Alan Gilbert 46860fe637bSDr. David Alan Gilbert /* 4697c1e52baSDr. David Alan Gilbert * Read 'size' bytes from file (at 'offset') without moving the 4707c1e52baSDr. David Alan Gilbert * pointer and set 'buf' to point to that data. 47160fe637bSDr. David Alan Gilbert * 47260fe637bSDr. David Alan Gilbert * It will return size bytes unless there was an error, in which case it will 47360fe637bSDr. David Alan Gilbert * return as many as it managed to read (assuming blocking fd's which 47460fe637bSDr. David Alan Gilbert * all current QEMUFile are) 47560fe637bSDr. David Alan Gilbert */ 47656f3835fSDr. David Alan Gilbert size_t qemu_peek_buffer(QEMUFile *f, uint8_t **buf, size_t size, size_t offset) 47760fe637bSDr. David Alan Gilbert { 47856f3835fSDr. David Alan Gilbert ssize_t pending; 47956f3835fSDr. David Alan Gilbert size_t index; 48060fe637bSDr. David Alan Gilbert 48160fe637bSDr. David Alan Gilbert assert(!qemu_file_is_writable(f)); 48260fe637bSDr. David Alan Gilbert assert(offset < IO_BUF_SIZE); 48360fe637bSDr. David Alan Gilbert assert(size <= IO_BUF_SIZE - offset); 48460fe637bSDr. David Alan Gilbert 48560fe637bSDr. David Alan Gilbert /* The 1st byte to read from */ 48660fe637bSDr. David Alan Gilbert index = f->buf_index + offset; 48760fe637bSDr. David Alan Gilbert /* The number of available bytes starting at index */ 48860fe637bSDr. David Alan Gilbert pending = f->buf_size - index; 48960fe637bSDr. David Alan Gilbert 49060fe637bSDr. David Alan Gilbert /* 49160fe637bSDr. David Alan Gilbert * qemu_fill_buffer might return just a few bytes, even when there isn't 49260fe637bSDr. David Alan Gilbert * an error, so loop collecting them until we get enough. 49360fe637bSDr. David Alan Gilbert */ 49460fe637bSDr. David Alan Gilbert while (pending < size) { 49560fe637bSDr. David Alan Gilbert int received = qemu_fill_buffer(f); 49660fe637bSDr. David Alan Gilbert 49760fe637bSDr. David Alan Gilbert if (received <= 0) { 49860fe637bSDr. David Alan Gilbert break; 49960fe637bSDr. David Alan Gilbert } 50060fe637bSDr. David Alan Gilbert 50160fe637bSDr. David Alan Gilbert index = f->buf_index + offset; 50260fe637bSDr. David Alan Gilbert pending = f->buf_size - index; 50360fe637bSDr. David Alan Gilbert } 50460fe637bSDr. David Alan Gilbert 50560fe637bSDr. David Alan Gilbert if (pending <= 0) { 50660fe637bSDr. David Alan Gilbert return 0; 50760fe637bSDr. David Alan Gilbert } 50860fe637bSDr. David Alan Gilbert if (size > pending) { 50960fe637bSDr. David Alan Gilbert size = pending; 51060fe637bSDr. David Alan Gilbert } 51160fe637bSDr. David Alan Gilbert 5127c1e52baSDr. David Alan Gilbert *buf = f->buf + index; 51360fe637bSDr. David Alan Gilbert return size; 51460fe637bSDr. David Alan Gilbert } 51560fe637bSDr. David Alan Gilbert 51660fe637bSDr. David Alan Gilbert /* 51760fe637bSDr. David Alan Gilbert * Read 'size' bytes of data from the file into buf. 51860fe637bSDr. David Alan Gilbert * 'size' can be larger than the internal buffer. 51960fe637bSDr. David Alan Gilbert * 52060fe637bSDr. David Alan Gilbert * It will return size bytes unless there was an error, in which case it will 52160fe637bSDr. David Alan Gilbert * return as many as it managed to read (assuming blocking fd's which 52260fe637bSDr. David Alan Gilbert * all current QEMUFile are) 52360fe637bSDr. David Alan Gilbert */ 52456f3835fSDr. David Alan Gilbert size_t qemu_get_buffer(QEMUFile *f, uint8_t *buf, size_t size) 52560fe637bSDr. David Alan Gilbert { 52656f3835fSDr. David Alan Gilbert size_t pending = size; 52756f3835fSDr. David Alan Gilbert size_t done = 0; 52860fe637bSDr. David Alan Gilbert 52960fe637bSDr. David Alan Gilbert while (pending > 0) { 53056f3835fSDr. David Alan Gilbert size_t res; 5317c1e52baSDr. David Alan Gilbert uint8_t *src; 53260fe637bSDr. David Alan Gilbert 5337c1e52baSDr. David Alan Gilbert res = qemu_peek_buffer(f, &src, MIN(pending, IO_BUF_SIZE), 0); 53460fe637bSDr. David Alan Gilbert if (res == 0) { 53560fe637bSDr. David Alan Gilbert return done; 53660fe637bSDr. David Alan Gilbert } 5377c1e52baSDr. David Alan Gilbert memcpy(buf, src, res); 53860fe637bSDr. David Alan Gilbert qemu_file_skip(f, res); 53960fe637bSDr. David Alan Gilbert buf += res; 54060fe637bSDr. David Alan Gilbert pending -= res; 54160fe637bSDr. David Alan Gilbert done += res; 54260fe637bSDr. David Alan Gilbert } 54360fe637bSDr. David Alan Gilbert return done; 54460fe637bSDr. David Alan Gilbert } 54560fe637bSDr. David Alan Gilbert 54660fe637bSDr. David Alan Gilbert /* 5479504fb51SDr. David Alan Gilbert * Read 'size' bytes of data from the file. 5489504fb51SDr. David Alan Gilbert * 'size' can be larger than the internal buffer. 5499504fb51SDr. David Alan Gilbert * 5509504fb51SDr. David Alan Gilbert * The data: 5519504fb51SDr. David Alan Gilbert * may be held on an internal buffer (in which case *buf is updated 5529504fb51SDr. David Alan Gilbert * to point to it) that is valid until the next qemu_file operation. 5539504fb51SDr. David Alan Gilbert * OR 5549504fb51SDr. David Alan Gilbert * will be copied to the *buf that was passed in. 5559504fb51SDr. David Alan Gilbert * 5569504fb51SDr. David Alan Gilbert * The code tries to avoid the copy if possible. 5579504fb51SDr. David Alan Gilbert * 5589504fb51SDr. David Alan Gilbert * It will return size bytes unless there was an error, in which case it will 5599504fb51SDr. David Alan Gilbert * return as many as it managed to read (assuming blocking fd's which 5609504fb51SDr. David Alan Gilbert * all current QEMUFile are) 5619504fb51SDr. David Alan Gilbert * 5629504fb51SDr. David Alan Gilbert * Note: Since **buf may get changed, the caller should take care to 5639504fb51SDr. David Alan Gilbert * keep a pointer to the original buffer if it needs to deallocate it. 5649504fb51SDr. David Alan Gilbert */ 5659504fb51SDr. David Alan Gilbert size_t qemu_get_buffer_in_place(QEMUFile *f, uint8_t **buf, size_t size) 5669504fb51SDr. David Alan Gilbert { 5679504fb51SDr. David Alan Gilbert if (size < IO_BUF_SIZE) { 5689504fb51SDr. David Alan Gilbert size_t res; 5699504fb51SDr. David Alan Gilbert uint8_t *src; 5709504fb51SDr. David Alan Gilbert 5719504fb51SDr. David Alan Gilbert res = qemu_peek_buffer(f, &src, size, 0); 5729504fb51SDr. David Alan Gilbert 5739504fb51SDr. David Alan Gilbert if (res == size) { 5749504fb51SDr. David Alan Gilbert qemu_file_skip(f, res); 5759504fb51SDr. David Alan Gilbert *buf = src; 5769504fb51SDr. David Alan Gilbert return res; 5779504fb51SDr. David Alan Gilbert } 5789504fb51SDr. David Alan Gilbert } 5799504fb51SDr. David Alan Gilbert 5809504fb51SDr. David Alan Gilbert return qemu_get_buffer(f, *buf, size); 5819504fb51SDr. David Alan Gilbert } 5829504fb51SDr. David Alan Gilbert 5839504fb51SDr. David Alan Gilbert /* 58460fe637bSDr. David Alan Gilbert * Peeks a single byte from the buffer; this isn't guaranteed to work if 58560fe637bSDr. David Alan Gilbert * offset leaves a gap after the previous read/peeked data. 58660fe637bSDr. David Alan Gilbert */ 58760fe637bSDr. David Alan Gilbert int qemu_peek_byte(QEMUFile *f, int offset) 58860fe637bSDr. David Alan Gilbert { 58960fe637bSDr. David Alan Gilbert int index = f->buf_index + offset; 59060fe637bSDr. David Alan Gilbert 59160fe637bSDr. David Alan Gilbert assert(!qemu_file_is_writable(f)); 59260fe637bSDr. David Alan Gilbert assert(offset < IO_BUF_SIZE); 59360fe637bSDr. David Alan Gilbert 59460fe637bSDr. David Alan Gilbert if (index >= f->buf_size) { 59560fe637bSDr. David Alan Gilbert qemu_fill_buffer(f); 59660fe637bSDr. David Alan Gilbert index = f->buf_index + offset; 59760fe637bSDr. David Alan Gilbert if (index >= f->buf_size) { 59860fe637bSDr. David Alan Gilbert return 0; 59960fe637bSDr. David Alan Gilbert } 60060fe637bSDr. David Alan Gilbert } 60160fe637bSDr. David Alan Gilbert return f->buf[index]; 60260fe637bSDr. David Alan Gilbert } 60360fe637bSDr. David Alan Gilbert 60460fe637bSDr. David Alan Gilbert int qemu_get_byte(QEMUFile *f) 60560fe637bSDr. David Alan Gilbert { 60660fe637bSDr. David Alan Gilbert int result; 60760fe637bSDr. David Alan Gilbert 60860fe637bSDr. David Alan Gilbert result = qemu_peek_byte(f, 0); 60960fe637bSDr. David Alan Gilbert qemu_file_skip(f, 1); 61060fe637bSDr. David Alan Gilbert return result; 61160fe637bSDr. David Alan Gilbert } 61260fe637bSDr. David Alan Gilbert 61397221400SAlexander Graf int64_t qemu_ftell_fast(QEMUFile *f) 61497221400SAlexander Graf { 61597221400SAlexander Graf int64_t ret = f->pos; 61697221400SAlexander Graf int i; 61797221400SAlexander Graf 61897221400SAlexander Graf for (i = 0; i < f->iovcnt; i++) { 61997221400SAlexander Graf ret += f->iov[i].iov_len; 62097221400SAlexander Graf } 62197221400SAlexander Graf 62297221400SAlexander Graf return ret; 62397221400SAlexander Graf } 62497221400SAlexander Graf 62560fe637bSDr. David Alan Gilbert int64_t qemu_ftell(QEMUFile *f) 62660fe637bSDr. David Alan Gilbert { 62760fe637bSDr. David Alan Gilbert qemu_fflush(f); 62860fe637bSDr. David Alan Gilbert return f->pos; 62960fe637bSDr. David Alan Gilbert } 63060fe637bSDr. David Alan Gilbert 63160fe637bSDr. David Alan Gilbert int qemu_file_rate_limit(QEMUFile *f) 63260fe637bSDr. David Alan Gilbert { 63360fe637bSDr. David Alan Gilbert if (qemu_file_get_error(f)) { 63460fe637bSDr. David Alan Gilbert return 1; 63560fe637bSDr. David Alan Gilbert } 63660fe637bSDr. David Alan Gilbert if (f->xfer_limit > 0 && f->bytes_xfer > f->xfer_limit) { 63760fe637bSDr. David Alan Gilbert return 1; 63860fe637bSDr. David Alan Gilbert } 63960fe637bSDr. David Alan Gilbert return 0; 64060fe637bSDr. David Alan Gilbert } 64160fe637bSDr. David Alan Gilbert 64260fe637bSDr. David Alan Gilbert int64_t qemu_file_get_rate_limit(QEMUFile *f) 64360fe637bSDr. David Alan Gilbert { 64460fe637bSDr. David Alan Gilbert return f->xfer_limit; 64560fe637bSDr. David Alan Gilbert } 64660fe637bSDr. David Alan Gilbert 64760fe637bSDr. David Alan Gilbert void qemu_file_set_rate_limit(QEMUFile *f, int64_t limit) 64860fe637bSDr. David Alan Gilbert { 64960fe637bSDr. David Alan Gilbert f->xfer_limit = limit; 65060fe637bSDr. David Alan Gilbert } 65160fe637bSDr. David Alan Gilbert 65260fe637bSDr. David Alan Gilbert void qemu_file_reset_rate_limit(QEMUFile *f) 65360fe637bSDr. David Alan Gilbert { 65460fe637bSDr. David Alan Gilbert f->bytes_xfer = 0; 65560fe637bSDr. David Alan Gilbert } 65660fe637bSDr. David Alan Gilbert 657*5d7d2558SIvan Ren void qemu_file_update_transfer(QEMUFile *f, int64_t len) 658*5d7d2558SIvan Ren { 659*5d7d2558SIvan Ren f->bytes_xfer += len; 660*5d7d2558SIvan Ren } 661*5d7d2558SIvan Ren 66260fe637bSDr. David Alan Gilbert void qemu_put_be16(QEMUFile *f, unsigned int v) 66360fe637bSDr. David Alan Gilbert { 66460fe637bSDr. David Alan Gilbert qemu_put_byte(f, v >> 8); 66560fe637bSDr. David Alan Gilbert qemu_put_byte(f, v); 66660fe637bSDr. David Alan Gilbert } 66760fe637bSDr. David Alan Gilbert 66860fe637bSDr. David Alan Gilbert void qemu_put_be32(QEMUFile *f, unsigned int v) 66960fe637bSDr. David Alan Gilbert { 67060fe637bSDr. David Alan Gilbert qemu_put_byte(f, v >> 24); 67160fe637bSDr. David Alan Gilbert qemu_put_byte(f, v >> 16); 67260fe637bSDr. David Alan Gilbert qemu_put_byte(f, v >> 8); 67360fe637bSDr. David Alan Gilbert qemu_put_byte(f, v); 67460fe637bSDr. David Alan Gilbert } 67560fe637bSDr. David Alan Gilbert 67660fe637bSDr. David Alan Gilbert void qemu_put_be64(QEMUFile *f, uint64_t v) 67760fe637bSDr. David Alan Gilbert { 67860fe637bSDr. David Alan Gilbert qemu_put_be32(f, v >> 32); 67960fe637bSDr. David Alan Gilbert qemu_put_be32(f, v); 68060fe637bSDr. David Alan Gilbert } 68160fe637bSDr. David Alan Gilbert 68260fe637bSDr. David Alan Gilbert unsigned int qemu_get_be16(QEMUFile *f) 68360fe637bSDr. David Alan Gilbert { 68460fe637bSDr. David Alan Gilbert unsigned int v; 68560fe637bSDr. David Alan Gilbert v = qemu_get_byte(f) << 8; 68660fe637bSDr. David Alan Gilbert v |= qemu_get_byte(f); 68760fe637bSDr. David Alan Gilbert return v; 68860fe637bSDr. David Alan Gilbert } 68960fe637bSDr. David Alan Gilbert 69060fe637bSDr. David Alan Gilbert unsigned int qemu_get_be32(QEMUFile *f) 69160fe637bSDr. David Alan Gilbert { 69260fe637bSDr. David Alan Gilbert unsigned int v; 69390d6a673SPeter Maydell v = (unsigned int)qemu_get_byte(f) << 24; 69460fe637bSDr. David Alan Gilbert v |= qemu_get_byte(f) << 16; 69560fe637bSDr. David Alan Gilbert v |= qemu_get_byte(f) << 8; 69660fe637bSDr. David Alan Gilbert v |= qemu_get_byte(f); 69760fe637bSDr. David Alan Gilbert return v; 69860fe637bSDr. David Alan Gilbert } 69960fe637bSDr. David Alan Gilbert 70060fe637bSDr. David Alan Gilbert uint64_t qemu_get_be64(QEMUFile *f) 70160fe637bSDr. David Alan Gilbert { 70260fe637bSDr. David Alan Gilbert uint64_t v; 70360fe637bSDr. David Alan Gilbert v = (uint64_t)qemu_get_be32(f) << 32; 70460fe637bSDr. David Alan Gilbert v |= qemu_get_be32(f); 70560fe637bSDr. David Alan Gilbert return v; 70660fe637bSDr. David Alan Gilbert } 70744f0eadcSLiang Li 708dcaf446eSXiao Guangrong /* return the size after compression, or negative value on error */ 709dcaf446eSXiao Guangrong static int qemu_compress_data(z_stream *stream, uint8_t *dest, size_t dest_len, 710dcaf446eSXiao Guangrong const uint8_t *source, size_t source_len) 711dcaf446eSXiao Guangrong { 712dcaf446eSXiao Guangrong int err; 713dcaf446eSXiao Guangrong 714dcaf446eSXiao Guangrong err = deflateReset(stream); 715dcaf446eSXiao Guangrong if (err != Z_OK) { 716dcaf446eSXiao Guangrong return -1; 717dcaf446eSXiao Guangrong } 718dcaf446eSXiao Guangrong 719dcaf446eSXiao Guangrong stream->avail_in = source_len; 720dcaf446eSXiao Guangrong stream->next_in = (uint8_t *)source; 721dcaf446eSXiao Guangrong stream->avail_out = dest_len; 722dcaf446eSXiao Guangrong stream->next_out = dest; 723dcaf446eSXiao Guangrong 724dcaf446eSXiao Guangrong err = deflate(stream, Z_FINISH); 725dcaf446eSXiao Guangrong if (err != Z_STREAM_END) { 726dcaf446eSXiao Guangrong return -1; 727dcaf446eSXiao Guangrong } 728dcaf446eSXiao Guangrong 729dcaf446eSXiao Guangrong return stream->next_out - dest; 730dcaf446eSXiao Guangrong } 731dcaf446eSXiao Guangrong 732dcaf446eSXiao Guangrong /* Compress size bytes of data start at p and store the compressed 733dcaf446eSXiao Guangrong * data to the buffer of f. 734b3be2896SLiang Li * 735b3be2896SLiang Li * When f is not writable, return -1 if f has no space to save the 736b3be2896SLiang Li * compressed data. 737b3be2896SLiang Li * When f is wirtable and it has no space to save the compressed data, 738b3be2896SLiang Li * do fflush first, if f still has no space to save the compressed 739b3be2896SLiang Li * data, return -1. 74044f0eadcSLiang Li */ 741dcaf446eSXiao Guangrong ssize_t qemu_put_compression_data(QEMUFile *f, z_stream *stream, 742dcaf446eSXiao Guangrong const uint8_t *p, size_t size) 74344f0eadcSLiang Li { 74444f0eadcSLiang Li ssize_t blen = IO_BUF_SIZE - f->buf_index - sizeof(int32_t); 74544f0eadcSLiang Li 74644f0eadcSLiang Li if (blen < compressBound(size)) { 747b3be2896SLiang Li if (!qemu_file_is_writable(f)) { 748b3be2896SLiang Li return -1; 749b3be2896SLiang Li } 750b3be2896SLiang Li qemu_fflush(f); 751b3be2896SLiang Li blen = IO_BUF_SIZE - sizeof(int32_t); 752b3be2896SLiang Li if (blen < compressBound(size)) { 753b3be2896SLiang Li return -1; 754b3be2896SLiang Li } 75544f0eadcSLiang Li } 756dcaf446eSXiao Guangrong 757dcaf446eSXiao Guangrong blen = qemu_compress_data(stream, f->buf + f->buf_index + sizeof(int32_t), 758dcaf446eSXiao Guangrong blen, p, size); 759dcaf446eSXiao Guangrong if (blen < 0) { 76034ab9e97SXiao Guangrong return -1; 76144f0eadcSLiang Li } 76234ab9e97SXiao Guangrong 76344f0eadcSLiang Li qemu_put_be32(f, blen); 764b3be2896SLiang Li if (f->ops->writev_buffer) { 76553f09a10SPavel Butsykin add_to_iovec(f, f->buf + f->buf_index, blen, false); 766b3be2896SLiang Li } 76744f0eadcSLiang Li f->buf_index += blen; 768b3be2896SLiang Li if (f->buf_index == IO_BUF_SIZE) { 769b3be2896SLiang Li qemu_fflush(f); 770b3be2896SLiang Li } 77144f0eadcSLiang Li return blen + sizeof(int32_t); 77244f0eadcSLiang Li } 77344f0eadcSLiang Li 77444f0eadcSLiang Li /* Put the data in the buffer of f_src to the buffer of f_des, and 77544f0eadcSLiang Li * then reset the buf_index of f_src to 0. 77644f0eadcSLiang Li */ 77744f0eadcSLiang Li 77844f0eadcSLiang Li int qemu_put_qemu_file(QEMUFile *f_des, QEMUFile *f_src) 77944f0eadcSLiang Li { 78044f0eadcSLiang Li int len = 0; 78144f0eadcSLiang Li 78244f0eadcSLiang Li if (f_src->buf_index > 0) { 78344f0eadcSLiang Li len = f_src->buf_index; 78444f0eadcSLiang Li qemu_put_buffer(f_des, f_src->buf, f_src->buf_index); 78544f0eadcSLiang Li f_src->buf_index = 0; 786787d134fSLiang Li f_src->iovcnt = 0; 78744f0eadcSLiang Li } 78844f0eadcSLiang Li return len; 78944f0eadcSLiang Li } 790b3af1bc9SDr. David Alan Gilbert 791b3af1bc9SDr. David Alan Gilbert /* 792b3af1bc9SDr. David Alan Gilbert * Get a string whose length is determined by a single preceding byte 793b3af1bc9SDr. David Alan Gilbert * A preallocated 256 byte buffer must be passed in. 794b3af1bc9SDr. David Alan Gilbert * Returns: len on success and a 0 terminated string in the buffer 795b3af1bc9SDr. David Alan Gilbert * else 0 796b3af1bc9SDr. David Alan Gilbert * (Note a 0 length string will return 0 either way) 797b3af1bc9SDr. David Alan Gilbert */ 798b3af1bc9SDr. David Alan Gilbert size_t qemu_get_counted_string(QEMUFile *f, char buf[256]) 799b3af1bc9SDr. David Alan Gilbert { 800b3af1bc9SDr. David Alan Gilbert size_t len = qemu_get_byte(f); 801b3af1bc9SDr. David Alan Gilbert size_t res = qemu_get_buffer(f, (uint8_t *)buf, len); 802b3af1bc9SDr. David Alan Gilbert 803b3af1bc9SDr. David Alan Gilbert buf[res] = 0; 804b3af1bc9SDr. David Alan Gilbert 805b3af1bc9SDr. David Alan Gilbert return res == len ? res : 0; 806b3af1bc9SDr. David Alan Gilbert } 807a800cd5cSDr. David Alan Gilbert 808a800cd5cSDr. David Alan Gilbert /* 809f0d64cb7SVladimir Sementsov-Ogievskiy * Put a string with one preceding byte containing its length. The length of 810f0d64cb7SVladimir Sementsov-Ogievskiy * the string should be less than 256. 811f0d64cb7SVladimir Sementsov-Ogievskiy */ 812f0d64cb7SVladimir Sementsov-Ogievskiy void qemu_put_counted_string(QEMUFile *f, const char *str) 813f0d64cb7SVladimir Sementsov-Ogievskiy { 814f0d64cb7SVladimir Sementsov-Ogievskiy size_t len = strlen(str); 815f0d64cb7SVladimir Sementsov-Ogievskiy 816f0d64cb7SVladimir Sementsov-Ogievskiy assert(len < 256); 817f0d64cb7SVladimir Sementsov-Ogievskiy qemu_put_byte(f, len); 818f0d64cb7SVladimir Sementsov-Ogievskiy qemu_put_buffer(f, (const uint8_t *)str, len); 819f0d64cb7SVladimir Sementsov-Ogievskiy } 820f0d64cb7SVladimir Sementsov-Ogievskiy 821f0d64cb7SVladimir Sementsov-Ogievskiy /* 822a800cd5cSDr. David Alan Gilbert * Set the blocking state of the QEMUFile. 823a800cd5cSDr. David Alan Gilbert * Note: On some transports the OS only keeps a single blocking state for 824a800cd5cSDr. David Alan Gilbert * both directions, and thus changing the blocking on the main 825a800cd5cSDr. David Alan Gilbert * QEMUFile can also affect the return path. 826a800cd5cSDr. David Alan Gilbert */ 827a800cd5cSDr. David Alan Gilbert void qemu_file_set_blocking(QEMUFile *f, bool block) 828a800cd5cSDr. David Alan Gilbert { 82906ad5135SDaniel P. Berrange if (f->ops->set_blocking) { 8303d661c8aSYury Kotov f->ops->set_blocking(f->opaque, block, NULL); 831a800cd5cSDr. David Alan Gilbert } 83206ad5135SDaniel P. Berrange } 833