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