1 /* 2 * QEMU System Emulator 3 * 4 * Copyright (c) 2003-2008 Fabrice Bellard 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining a copy 7 * of this software and associated documentation files (the "Software"), to deal 8 * in the Software without restriction, including without limitation the rights 9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 * copies of the Software, and to permit persons to whom the Software is 11 * furnished to do so, subject to the following conditions: 12 * 13 * The above copyright notice and this permission notice shall be included in 14 * all copies or substantial portions of the Software. 15 * 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 * THE SOFTWARE. 23 */ 24 #include "qemu/osdep.h" 25 #include <zlib.h> 26 #include "qemu/madvise.h" 27 #include "qemu/error-report.h" 28 #include "qemu/iov.h" 29 #include "migration.h" 30 #include "qemu-file.h" 31 #include "trace.h" 32 #include "qapi/error.h" 33 34 #define IO_BUF_SIZE 32768 35 #define MAX_IOV_SIZE MIN_CONST(IOV_MAX, 64) 36 37 struct QEMUFile { 38 const QEMUFileHooks *hooks; 39 QIOChannel *ioc; 40 bool is_writable; 41 42 /* 43 * Maximum amount of data in bytes to transfer during one 44 * rate limiting time window 45 */ 46 int64_t rate_limit_max; 47 /* 48 * Total amount of data in bytes queued for transfer 49 * during this rate limiting time window 50 */ 51 int64_t rate_limit_used; 52 53 /* The sum of bytes transferred on the wire */ 54 int64_t total_transferred; 55 56 int buf_index; 57 int buf_size; /* 0 when writing */ 58 uint8_t buf[IO_BUF_SIZE]; 59 60 DECLARE_BITMAP(may_free, MAX_IOV_SIZE); 61 struct iovec iov[MAX_IOV_SIZE]; 62 unsigned int iovcnt; 63 64 int last_error; 65 Error *last_error_obj; 66 /* has the file has been shutdown */ 67 bool shutdown; 68 }; 69 70 /* 71 * Stop a file from being read/written - not all backing files can do this 72 * typically only sockets can. 73 * 74 * TODO: convert to propagate Error objects instead of squashing 75 * to a fixed errno value 76 */ 77 int qemu_file_shutdown(QEMUFile *f) 78 { 79 int ret = 0; 80 81 f->shutdown = true; 82 if (!qio_channel_has_feature(f->ioc, 83 QIO_CHANNEL_FEATURE_SHUTDOWN)) { 84 return -ENOSYS; 85 } 86 87 if (qio_channel_shutdown(f->ioc, QIO_CHANNEL_SHUTDOWN_BOTH, NULL) < 0) { 88 ret = -EIO; 89 } 90 91 if (!f->last_error) { 92 qemu_file_set_error(f, -EIO); 93 } 94 return ret; 95 } 96 97 bool qemu_file_mode_is_not_valid(const char *mode) 98 { 99 if (mode == NULL || 100 (mode[0] != 'r' && mode[0] != 'w') || 101 mode[1] != 'b' || mode[2] != 0) { 102 fprintf(stderr, "qemu_fopen: Argument validity check failed\n"); 103 return true; 104 } 105 106 return false; 107 } 108 109 static QEMUFile *qemu_file_new_impl(QIOChannel *ioc, bool is_writable) 110 { 111 QEMUFile *f; 112 113 f = g_new0(QEMUFile, 1); 114 115 object_ref(ioc); 116 f->ioc = ioc; 117 f->is_writable = is_writable; 118 119 return f; 120 } 121 122 /* 123 * Result: QEMUFile* for a 'return path' for comms in the opposite direction 124 * NULL if not available 125 */ 126 QEMUFile *qemu_file_get_return_path(QEMUFile *f) 127 { 128 return qemu_file_new_impl(f->ioc, !f->is_writable); 129 } 130 131 QEMUFile *qemu_file_new_output(QIOChannel *ioc) 132 { 133 return qemu_file_new_impl(ioc, true); 134 } 135 136 QEMUFile *qemu_file_new_input(QIOChannel *ioc) 137 { 138 return qemu_file_new_impl(ioc, false); 139 } 140 141 void qemu_file_set_hooks(QEMUFile *f, const QEMUFileHooks *hooks) 142 { 143 f->hooks = hooks; 144 } 145 146 /* 147 * Get last error for stream f with optional Error* 148 * 149 * Return negative error value if there has been an error on previous 150 * operations, return 0 if no error happened. 151 * Optional, it returns Error* in errp, but it may be NULL even if return value 152 * is not 0. 153 * 154 */ 155 int qemu_file_get_error_obj(QEMUFile *f, Error **errp) 156 { 157 if (errp) { 158 *errp = f->last_error_obj ? error_copy(f->last_error_obj) : NULL; 159 } 160 return f->last_error; 161 } 162 163 /* 164 * Get last error for either stream f1 or f2 with optional Error*. 165 * The error returned (non-zero) can be either from f1 or f2. 166 * 167 * If any of the qemufile* is NULL, then skip the check on that file. 168 * 169 * When there is no error on both qemufile, zero is returned. 170 */ 171 int qemu_file_get_error_obj_any(QEMUFile *f1, QEMUFile *f2, Error **errp) 172 { 173 int ret = 0; 174 175 if (f1) { 176 ret = qemu_file_get_error_obj(f1, errp); 177 /* If there's already error detected, return */ 178 if (ret) { 179 return ret; 180 } 181 } 182 183 if (f2) { 184 ret = qemu_file_get_error_obj(f2, errp); 185 } 186 187 return ret; 188 } 189 190 /* 191 * Set the last error for stream f with optional Error* 192 */ 193 void qemu_file_set_error_obj(QEMUFile *f, int ret, Error *err) 194 { 195 if (f->last_error == 0 && ret) { 196 f->last_error = ret; 197 error_propagate(&f->last_error_obj, err); 198 } else if (err) { 199 error_report_err(err); 200 } 201 } 202 203 /* 204 * Get last error for stream f 205 * 206 * Return negative error value if there has been an error on previous 207 * operations, return 0 if no error happened. 208 * 209 */ 210 int qemu_file_get_error(QEMUFile *f) 211 { 212 return qemu_file_get_error_obj(f, NULL); 213 } 214 215 /* 216 * Set the last error for stream f 217 */ 218 void qemu_file_set_error(QEMUFile *f, int ret) 219 { 220 qemu_file_set_error_obj(f, ret, NULL); 221 } 222 223 bool qemu_file_is_writable(QEMUFile *f) 224 { 225 return f->is_writable; 226 } 227 228 static void qemu_iovec_release_ram(QEMUFile *f) 229 { 230 struct iovec iov; 231 unsigned long idx; 232 233 /* Find and release all the contiguous memory ranges marked as may_free. */ 234 idx = find_next_bit(f->may_free, f->iovcnt, 0); 235 if (idx >= f->iovcnt) { 236 return; 237 } 238 iov = f->iov[idx]; 239 240 /* The madvise() in the loop is called for iov within a continuous range and 241 * then reinitialize the iov. And in the end, madvise() is called for the 242 * last iov. 243 */ 244 while ((idx = find_next_bit(f->may_free, f->iovcnt, idx + 1)) < f->iovcnt) { 245 /* check for adjacent buffer and coalesce them */ 246 if (iov.iov_base + iov.iov_len == f->iov[idx].iov_base) { 247 iov.iov_len += f->iov[idx].iov_len; 248 continue; 249 } 250 if (qemu_madvise(iov.iov_base, iov.iov_len, QEMU_MADV_DONTNEED) < 0) { 251 error_report("migrate: madvise DONTNEED failed %p %zd: %s", 252 iov.iov_base, iov.iov_len, strerror(errno)); 253 } 254 iov = f->iov[idx]; 255 } 256 if (qemu_madvise(iov.iov_base, iov.iov_len, QEMU_MADV_DONTNEED) < 0) { 257 error_report("migrate: madvise DONTNEED failed %p %zd: %s", 258 iov.iov_base, iov.iov_len, strerror(errno)); 259 } 260 memset(f->may_free, 0, sizeof(f->may_free)); 261 } 262 263 264 /** 265 * Flushes QEMUFile buffer 266 * 267 * This will flush all pending data. If data was only partially flushed, it 268 * will set an error state. 269 */ 270 void qemu_fflush(QEMUFile *f) 271 { 272 if (!qemu_file_is_writable(f)) { 273 return; 274 } 275 276 if (f->shutdown) { 277 return; 278 } 279 if (f->iovcnt > 0) { 280 Error *local_error = NULL; 281 if (qio_channel_writev_all(f->ioc, 282 f->iov, f->iovcnt, 283 &local_error) < 0) { 284 qemu_file_set_error_obj(f, -EIO, local_error); 285 } else { 286 f->total_transferred += iov_size(f->iov, f->iovcnt); 287 } 288 289 qemu_iovec_release_ram(f); 290 } 291 292 f->buf_index = 0; 293 f->iovcnt = 0; 294 } 295 296 void ram_control_before_iterate(QEMUFile *f, uint64_t flags) 297 { 298 int ret = 0; 299 300 if (f->hooks && f->hooks->before_ram_iterate) { 301 ret = f->hooks->before_ram_iterate(f, flags, NULL); 302 if (ret < 0) { 303 qemu_file_set_error(f, ret); 304 } 305 } 306 } 307 308 void ram_control_after_iterate(QEMUFile *f, uint64_t flags) 309 { 310 int ret = 0; 311 312 if (f->hooks && f->hooks->after_ram_iterate) { 313 ret = f->hooks->after_ram_iterate(f, flags, NULL); 314 if (ret < 0) { 315 qemu_file_set_error(f, ret); 316 } 317 } 318 } 319 320 void ram_control_load_hook(QEMUFile *f, uint64_t flags, void *data) 321 { 322 int ret = -EINVAL; 323 324 if (f->hooks && f->hooks->hook_ram_load) { 325 ret = f->hooks->hook_ram_load(f, flags, data); 326 if (ret < 0) { 327 qemu_file_set_error(f, ret); 328 } 329 } else { 330 /* 331 * Hook is a hook specifically requested by the source sending a flag 332 * that expects there to be a hook on the destination. 333 */ 334 if (flags == RAM_CONTROL_HOOK) { 335 qemu_file_set_error(f, ret); 336 } 337 } 338 } 339 340 size_t ram_control_save_page(QEMUFile *f, ram_addr_t block_offset, 341 ram_addr_t offset, size_t size, 342 uint64_t *bytes_sent) 343 { 344 if (f->hooks && f->hooks->save_page) { 345 int ret = f->hooks->save_page(f, block_offset, 346 offset, size, bytes_sent); 347 if (ret != RAM_SAVE_CONTROL_NOT_SUPP) { 348 f->rate_limit_used += size; 349 } 350 351 if (ret != RAM_SAVE_CONTROL_DELAYED && 352 ret != RAM_SAVE_CONTROL_NOT_SUPP) { 353 if (bytes_sent && *bytes_sent > 0) { 354 qemu_file_credit_transfer(f, *bytes_sent); 355 } else if (ret < 0) { 356 qemu_file_set_error(f, ret); 357 } 358 } 359 360 return ret; 361 } 362 363 return RAM_SAVE_CONTROL_NOT_SUPP; 364 } 365 366 /* 367 * Attempt to fill the buffer from the underlying file 368 * Returns the number of bytes read, or negative value for an error. 369 * 370 * Note that it can return a partially full buffer even in a not error/not EOF 371 * case if the underlying file descriptor gives a short read, and that can 372 * happen even on a blocking fd. 373 */ 374 static ssize_t qemu_fill_buffer(QEMUFile *f) 375 { 376 int len; 377 int pending; 378 Error *local_error = NULL; 379 380 assert(!qemu_file_is_writable(f)); 381 382 pending = f->buf_size - f->buf_index; 383 if (pending > 0) { 384 memmove(f->buf, f->buf + f->buf_index, pending); 385 } 386 f->buf_index = 0; 387 f->buf_size = pending; 388 389 if (f->shutdown) { 390 return 0; 391 } 392 393 do { 394 len = qio_channel_read(f->ioc, 395 (char *)f->buf + pending, 396 IO_BUF_SIZE - pending, 397 &local_error); 398 if (len == QIO_CHANNEL_ERR_BLOCK) { 399 if (qemu_in_coroutine()) { 400 qio_channel_yield(f->ioc, G_IO_IN); 401 } else { 402 qio_channel_wait(f->ioc, G_IO_IN); 403 } 404 } else if (len < 0) { 405 len = -EIO; 406 } 407 } while (len == QIO_CHANNEL_ERR_BLOCK); 408 409 if (len > 0) { 410 f->buf_size += len; 411 f->total_transferred += len; 412 } else if (len == 0) { 413 qemu_file_set_error_obj(f, -EIO, local_error); 414 } else { 415 qemu_file_set_error_obj(f, len, local_error); 416 } 417 418 return len; 419 } 420 421 void qemu_file_credit_transfer(QEMUFile *f, size_t size) 422 { 423 f->total_transferred += size; 424 } 425 426 /** Closes the file 427 * 428 * Returns negative error value if any error happened on previous operations or 429 * while closing the file. Returns 0 or positive number on success. 430 * 431 * The meaning of return value on success depends on the specific backend 432 * being used. 433 */ 434 int qemu_fclose(QEMUFile *f) 435 { 436 int ret, ret2; 437 qemu_fflush(f); 438 ret = qemu_file_get_error(f); 439 440 ret2 = qio_channel_close(f->ioc, NULL); 441 if (ret >= 0) { 442 ret = ret2; 443 } 444 g_clear_pointer(&f->ioc, object_unref); 445 446 /* If any error was spotted before closing, we should report it 447 * instead of the close() return value. 448 */ 449 if (f->last_error) { 450 ret = f->last_error; 451 } 452 error_free(f->last_error_obj); 453 g_free(f); 454 trace_qemu_file_fclose(); 455 return ret; 456 } 457 458 /* 459 * Add buf to iovec. Do flush if iovec is full. 460 * 461 * Return values: 462 * 1 iovec is full and flushed 463 * 0 iovec is not flushed 464 * 465 */ 466 static int add_to_iovec(QEMUFile *f, const uint8_t *buf, size_t size, 467 bool may_free) 468 { 469 /* check for adjacent buffer and coalesce them */ 470 if (f->iovcnt > 0 && buf == f->iov[f->iovcnt - 1].iov_base + 471 f->iov[f->iovcnt - 1].iov_len && 472 may_free == test_bit(f->iovcnt - 1, f->may_free)) 473 { 474 f->iov[f->iovcnt - 1].iov_len += size; 475 } else { 476 if (f->iovcnt >= MAX_IOV_SIZE) { 477 /* Should only happen if a previous fflush failed */ 478 assert(f->shutdown || !qemu_file_is_writable(f)); 479 return 1; 480 } 481 if (may_free) { 482 set_bit(f->iovcnt, f->may_free); 483 } 484 f->iov[f->iovcnt].iov_base = (uint8_t *)buf; 485 f->iov[f->iovcnt++].iov_len = size; 486 } 487 488 if (f->iovcnt >= MAX_IOV_SIZE) { 489 qemu_fflush(f); 490 return 1; 491 } 492 493 return 0; 494 } 495 496 static void add_buf_to_iovec(QEMUFile *f, size_t len) 497 { 498 if (!add_to_iovec(f, f->buf + f->buf_index, len, false)) { 499 f->buf_index += len; 500 if (f->buf_index == IO_BUF_SIZE) { 501 qemu_fflush(f); 502 } 503 } 504 } 505 506 void qemu_put_buffer_async(QEMUFile *f, const uint8_t *buf, size_t size, 507 bool may_free) 508 { 509 if (f->last_error) { 510 return; 511 } 512 513 f->rate_limit_used += size; 514 add_to_iovec(f, buf, size, may_free); 515 } 516 517 void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, size_t size) 518 { 519 size_t l; 520 521 if (f->last_error) { 522 return; 523 } 524 525 while (size > 0) { 526 l = IO_BUF_SIZE - f->buf_index; 527 if (l > size) { 528 l = size; 529 } 530 memcpy(f->buf + f->buf_index, buf, l); 531 f->rate_limit_used += l; 532 add_buf_to_iovec(f, l); 533 if (qemu_file_get_error(f)) { 534 break; 535 } 536 buf += l; 537 size -= l; 538 } 539 } 540 541 void qemu_put_byte(QEMUFile *f, int v) 542 { 543 if (f->last_error) { 544 return; 545 } 546 547 f->buf[f->buf_index] = v; 548 f->rate_limit_used++; 549 add_buf_to_iovec(f, 1); 550 } 551 552 void qemu_file_skip(QEMUFile *f, int size) 553 { 554 if (f->buf_index + size <= f->buf_size) { 555 f->buf_index += size; 556 } 557 } 558 559 /* 560 * Read 'size' bytes from file (at 'offset') without moving the 561 * pointer and set 'buf' to point to that data. 562 * 563 * It will return size bytes unless there was an error, in which case it will 564 * return as many as it managed to read (assuming blocking fd's which 565 * all current QEMUFile are) 566 */ 567 size_t qemu_peek_buffer(QEMUFile *f, uint8_t **buf, size_t size, size_t offset) 568 { 569 ssize_t pending; 570 size_t index; 571 572 assert(!qemu_file_is_writable(f)); 573 assert(offset < IO_BUF_SIZE); 574 assert(size <= IO_BUF_SIZE - offset); 575 576 /* The 1st byte to read from */ 577 index = f->buf_index + offset; 578 /* The number of available bytes starting at index */ 579 pending = f->buf_size - index; 580 581 /* 582 * qemu_fill_buffer might return just a few bytes, even when there isn't 583 * an error, so loop collecting them until we get enough. 584 */ 585 while (pending < size) { 586 int received = qemu_fill_buffer(f); 587 588 if (received <= 0) { 589 break; 590 } 591 592 index = f->buf_index + offset; 593 pending = f->buf_size - index; 594 } 595 596 if (pending <= 0) { 597 return 0; 598 } 599 if (size > pending) { 600 size = pending; 601 } 602 603 *buf = f->buf + index; 604 return size; 605 } 606 607 /* 608 * Read 'size' bytes of data from the file into buf. 609 * 'size' can be larger than the internal buffer. 610 * 611 * It will return size bytes unless there was an error, in which case it will 612 * return as many as it managed to read (assuming blocking fd's which 613 * all current QEMUFile are) 614 */ 615 size_t qemu_get_buffer(QEMUFile *f, uint8_t *buf, size_t size) 616 { 617 size_t pending = size; 618 size_t done = 0; 619 620 while (pending > 0) { 621 size_t res; 622 uint8_t *src; 623 624 res = qemu_peek_buffer(f, &src, MIN(pending, IO_BUF_SIZE), 0); 625 if (res == 0) { 626 return done; 627 } 628 memcpy(buf, src, res); 629 qemu_file_skip(f, res); 630 buf += res; 631 pending -= res; 632 done += res; 633 } 634 return done; 635 } 636 637 /* 638 * Read 'size' bytes of data from the file. 639 * 'size' can be larger than the internal buffer. 640 * 641 * The data: 642 * may be held on an internal buffer (in which case *buf is updated 643 * to point to it) that is valid until the next qemu_file operation. 644 * OR 645 * will be copied to the *buf that was passed in. 646 * 647 * The code tries to avoid the copy if possible. 648 * 649 * It will return size bytes unless there was an error, in which case it will 650 * return as many as it managed to read (assuming blocking fd's which 651 * all current QEMUFile are) 652 * 653 * Note: Since **buf may get changed, the caller should take care to 654 * keep a pointer to the original buffer if it needs to deallocate it. 655 */ 656 size_t qemu_get_buffer_in_place(QEMUFile *f, uint8_t **buf, size_t size) 657 { 658 if (size < IO_BUF_SIZE) { 659 size_t res; 660 uint8_t *src = NULL; 661 662 res = qemu_peek_buffer(f, &src, size, 0); 663 664 if (res == size) { 665 qemu_file_skip(f, res); 666 *buf = src; 667 return res; 668 } 669 } 670 671 return qemu_get_buffer(f, *buf, size); 672 } 673 674 /* 675 * Peeks a single byte from the buffer; this isn't guaranteed to work if 676 * offset leaves a gap after the previous read/peeked data. 677 */ 678 int qemu_peek_byte(QEMUFile *f, int offset) 679 { 680 int index = f->buf_index + offset; 681 682 assert(!qemu_file_is_writable(f)); 683 assert(offset < IO_BUF_SIZE); 684 685 if (index >= f->buf_size) { 686 qemu_fill_buffer(f); 687 index = f->buf_index + offset; 688 if (index >= f->buf_size) { 689 return 0; 690 } 691 } 692 return f->buf[index]; 693 } 694 695 int qemu_get_byte(QEMUFile *f) 696 { 697 int result; 698 699 result = qemu_peek_byte(f, 0); 700 qemu_file_skip(f, 1); 701 return result; 702 } 703 704 int64_t qemu_file_total_transferred_fast(QEMUFile *f) 705 { 706 int64_t ret = f->total_transferred; 707 int i; 708 709 for (i = 0; i < f->iovcnt; i++) { 710 ret += f->iov[i].iov_len; 711 } 712 713 return ret; 714 } 715 716 int64_t qemu_file_total_transferred(QEMUFile *f) 717 { 718 qemu_fflush(f); 719 return f->total_transferred; 720 } 721 722 int qemu_file_rate_limit(QEMUFile *f) 723 { 724 if (f->shutdown) { 725 return 1; 726 } 727 if (qemu_file_get_error(f)) { 728 return 1; 729 } 730 if (f->rate_limit_max > 0 && f->rate_limit_used > f->rate_limit_max) { 731 return 1; 732 } 733 return 0; 734 } 735 736 int64_t qemu_file_get_rate_limit(QEMUFile *f) 737 { 738 return f->rate_limit_max; 739 } 740 741 void qemu_file_set_rate_limit(QEMUFile *f, int64_t limit) 742 { 743 f->rate_limit_max = limit; 744 } 745 746 void qemu_file_reset_rate_limit(QEMUFile *f) 747 { 748 f->rate_limit_used = 0; 749 } 750 751 void qemu_file_acct_rate_limit(QEMUFile *f, int64_t len) 752 { 753 f->rate_limit_used += len; 754 } 755 756 void qemu_put_be16(QEMUFile *f, unsigned int v) 757 { 758 qemu_put_byte(f, v >> 8); 759 qemu_put_byte(f, v); 760 } 761 762 void qemu_put_be32(QEMUFile *f, unsigned int v) 763 { 764 qemu_put_byte(f, v >> 24); 765 qemu_put_byte(f, v >> 16); 766 qemu_put_byte(f, v >> 8); 767 qemu_put_byte(f, v); 768 } 769 770 void qemu_put_be64(QEMUFile *f, uint64_t v) 771 { 772 qemu_put_be32(f, v >> 32); 773 qemu_put_be32(f, v); 774 } 775 776 unsigned int qemu_get_be16(QEMUFile *f) 777 { 778 unsigned int v; 779 v = qemu_get_byte(f) << 8; 780 v |= qemu_get_byte(f); 781 return v; 782 } 783 784 unsigned int qemu_get_be32(QEMUFile *f) 785 { 786 unsigned int v; 787 v = (unsigned int)qemu_get_byte(f) << 24; 788 v |= qemu_get_byte(f) << 16; 789 v |= qemu_get_byte(f) << 8; 790 v |= qemu_get_byte(f); 791 return v; 792 } 793 794 uint64_t qemu_get_be64(QEMUFile *f) 795 { 796 uint64_t v; 797 v = (uint64_t)qemu_get_be32(f) << 32; 798 v |= qemu_get_be32(f); 799 return v; 800 } 801 802 /* return the size after compression, or negative value on error */ 803 static int qemu_compress_data(z_stream *stream, uint8_t *dest, size_t dest_len, 804 const uint8_t *source, size_t source_len) 805 { 806 int err; 807 808 err = deflateReset(stream); 809 if (err != Z_OK) { 810 return -1; 811 } 812 813 stream->avail_in = source_len; 814 stream->next_in = (uint8_t *)source; 815 stream->avail_out = dest_len; 816 stream->next_out = dest; 817 818 err = deflate(stream, Z_FINISH); 819 if (err != Z_STREAM_END) { 820 return -1; 821 } 822 823 return stream->next_out - dest; 824 } 825 826 /* Compress size bytes of data start at p and store the compressed 827 * data to the buffer of f. 828 * 829 * Since the file is dummy file with empty_ops, return -1 if f has no space to 830 * save the compressed data. 831 */ 832 ssize_t qemu_put_compression_data(QEMUFile *f, z_stream *stream, 833 const uint8_t *p, size_t size) 834 { 835 ssize_t blen = IO_BUF_SIZE - f->buf_index - sizeof(int32_t); 836 837 if (blen < compressBound(size)) { 838 return -1; 839 } 840 841 blen = qemu_compress_data(stream, f->buf + f->buf_index + sizeof(int32_t), 842 blen, p, size); 843 if (blen < 0) { 844 return -1; 845 } 846 847 qemu_put_be32(f, blen); 848 add_buf_to_iovec(f, blen); 849 return blen + sizeof(int32_t); 850 } 851 852 /* Put the data in the buffer of f_src to the buffer of f_des, and 853 * then reset the buf_index of f_src to 0. 854 */ 855 856 int qemu_put_qemu_file(QEMUFile *f_des, QEMUFile *f_src) 857 { 858 int len = 0; 859 860 if (f_src->buf_index > 0) { 861 len = f_src->buf_index; 862 qemu_put_buffer(f_des, f_src->buf, f_src->buf_index); 863 f_src->buf_index = 0; 864 f_src->iovcnt = 0; 865 } 866 return len; 867 } 868 869 /* 870 * Get a string whose length is determined by a single preceding byte 871 * A preallocated 256 byte buffer must be passed in. 872 * Returns: len on success and a 0 terminated string in the buffer 873 * else 0 874 * (Note a 0 length string will return 0 either way) 875 */ 876 size_t qemu_get_counted_string(QEMUFile *f, char buf[256]) 877 { 878 size_t len = qemu_get_byte(f); 879 size_t res = qemu_get_buffer(f, (uint8_t *)buf, len); 880 881 buf[res] = 0; 882 883 return res == len ? res : 0; 884 } 885 886 /* 887 * Put a string with one preceding byte containing its length. The length of 888 * the string should be less than 256. 889 */ 890 void qemu_put_counted_string(QEMUFile *f, const char *str) 891 { 892 size_t len = strlen(str); 893 894 assert(len < 256); 895 qemu_put_byte(f, len); 896 qemu_put_buffer(f, (const uint8_t *)str, len); 897 } 898 899 /* 900 * Set the blocking state of the QEMUFile. 901 * Note: On some transports the OS only keeps a single blocking state for 902 * both directions, and thus changing the blocking on the main 903 * QEMUFile can also affect the return path. 904 */ 905 void qemu_file_set_blocking(QEMUFile *f, bool block) 906 { 907 qio_channel_set_blocking(f->ioc, block, NULL); 908 } 909 910 /* 911 * qemu_file_get_ioc: 912 * 913 * Get the ioc object for the file, without incrementing 914 * the reference count. 915 * 916 * Returns: the ioc object 917 */ 918 QIOChannel *qemu_file_get_ioc(QEMUFile *file) 919 { 920 return file->ioc; 921 } 922