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 <zlib.h> 25 #include "qemu-common.h" 26 #include "qemu/iov.h" 27 #include "qemu/sockets.h" 28 #include "block/coroutine.h" 29 #include "migration/migration.h" 30 #include "migration/qemu-file.h" 31 #include "migration/qemu-file-internal.h" 32 #include "trace.h" 33 34 /* 35 * Stop a file from being read/written - not all backing files can do this 36 * typically only sockets can. 37 */ 38 int qemu_file_shutdown(QEMUFile *f) 39 { 40 if (!f->ops->shut_down) { 41 return -ENOSYS; 42 } 43 return f->ops->shut_down(f->opaque, true, true); 44 } 45 46 bool qemu_file_mode_is_not_valid(const char *mode) 47 { 48 if (mode == NULL || 49 (mode[0] != 'r' && mode[0] != 'w') || 50 mode[1] != 'b' || mode[2] != 0) { 51 fprintf(stderr, "qemu_fopen: Argument validity check failed\n"); 52 return true; 53 } 54 55 return false; 56 } 57 58 QEMUFile *qemu_fopen_ops(void *opaque, const QEMUFileOps *ops) 59 { 60 QEMUFile *f; 61 62 f = g_malloc0(sizeof(QEMUFile)); 63 64 f->opaque = opaque; 65 f->ops = ops; 66 return f; 67 } 68 69 /* 70 * Get last error for stream f 71 * 72 * Return negative error value if there has been an error on previous 73 * operations, return 0 if no error happened. 74 * 75 */ 76 int qemu_file_get_error(QEMUFile *f) 77 { 78 return f->last_error; 79 } 80 81 void qemu_file_set_error(QEMUFile *f, int ret) 82 { 83 if (f->last_error == 0) { 84 f->last_error = ret; 85 } 86 } 87 88 bool qemu_file_is_writable(QEMUFile *f) 89 { 90 return f->ops->writev_buffer || f->ops->put_buffer; 91 } 92 93 /** 94 * Flushes QEMUFile buffer 95 * 96 * If there is writev_buffer QEMUFileOps it uses it otherwise uses 97 * put_buffer ops. 98 */ 99 void qemu_fflush(QEMUFile *f) 100 { 101 ssize_t ret = 0; 102 103 if (!qemu_file_is_writable(f)) { 104 return; 105 } 106 107 if (f->ops->writev_buffer) { 108 if (f->iovcnt > 0) { 109 ret = f->ops->writev_buffer(f->opaque, f->iov, f->iovcnt, f->pos); 110 } 111 } else { 112 if (f->buf_index > 0) { 113 ret = f->ops->put_buffer(f->opaque, f->buf, f->pos, f->buf_index); 114 } 115 } 116 if (ret >= 0) { 117 f->pos += ret; 118 } 119 f->buf_index = 0; 120 f->iovcnt = 0; 121 if (ret < 0) { 122 qemu_file_set_error(f, ret); 123 } 124 } 125 126 void ram_control_before_iterate(QEMUFile *f, uint64_t flags) 127 { 128 int ret = 0; 129 130 if (f->ops->before_ram_iterate) { 131 ret = f->ops->before_ram_iterate(f, f->opaque, flags); 132 if (ret < 0) { 133 qemu_file_set_error(f, ret); 134 } 135 } 136 } 137 138 void ram_control_after_iterate(QEMUFile *f, uint64_t flags) 139 { 140 int ret = 0; 141 142 if (f->ops->after_ram_iterate) { 143 ret = f->ops->after_ram_iterate(f, f->opaque, flags); 144 if (ret < 0) { 145 qemu_file_set_error(f, ret); 146 } 147 } 148 } 149 150 void ram_control_load_hook(QEMUFile *f, uint64_t flags) 151 { 152 int ret = -EINVAL; 153 154 if (f->ops->hook_ram_load) { 155 ret = f->ops->hook_ram_load(f, f->opaque, flags); 156 if (ret < 0) { 157 qemu_file_set_error(f, ret); 158 } 159 } else { 160 qemu_file_set_error(f, ret); 161 } 162 } 163 164 size_t ram_control_save_page(QEMUFile *f, ram_addr_t block_offset, 165 ram_addr_t offset, size_t size, 166 uint64_t *bytes_sent) 167 { 168 if (f->ops->save_page) { 169 int ret = f->ops->save_page(f, f->opaque, block_offset, 170 offset, size, bytes_sent); 171 172 if (ret != RAM_SAVE_CONTROL_DELAYED) { 173 if (bytes_sent && *bytes_sent > 0) { 174 qemu_update_position(f, *bytes_sent); 175 } else if (ret < 0) { 176 qemu_file_set_error(f, ret); 177 } 178 } 179 180 return ret; 181 } 182 183 return RAM_SAVE_CONTROL_NOT_SUPP; 184 } 185 186 /* 187 * Attempt to fill the buffer from the underlying file 188 * Returns the number of bytes read, or negative value for an error. 189 * 190 * Note that it can return a partially full buffer even in a not error/not EOF 191 * case if the underlying file descriptor gives a short read, and that can 192 * happen even on a blocking fd. 193 */ 194 static ssize_t qemu_fill_buffer(QEMUFile *f) 195 { 196 int len; 197 int pending; 198 199 assert(!qemu_file_is_writable(f)); 200 201 pending = f->buf_size - f->buf_index; 202 if (pending > 0) { 203 memmove(f->buf, f->buf + f->buf_index, pending); 204 } 205 f->buf_index = 0; 206 f->buf_size = pending; 207 208 len = f->ops->get_buffer(f->opaque, f->buf + pending, f->pos, 209 IO_BUF_SIZE - pending); 210 if (len > 0) { 211 f->buf_size += len; 212 f->pos += len; 213 } else if (len == 0) { 214 qemu_file_set_error(f, -EIO); 215 } else if (len != -EAGAIN) { 216 qemu_file_set_error(f, len); 217 } 218 219 return len; 220 } 221 222 int qemu_get_fd(QEMUFile *f) 223 { 224 if (f->ops->get_fd) { 225 return f->ops->get_fd(f->opaque); 226 } 227 return -1; 228 } 229 230 void qemu_update_position(QEMUFile *f, size_t size) 231 { 232 f->pos += size; 233 } 234 235 /** Closes the file 236 * 237 * Returns negative error value if any error happened on previous operations or 238 * while closing the file. Returns 0 or positive number on success. 239 * 240 * The meaning of return value on success depends on the specific backend 241 * being used. 242 */ 243 int qemu_fclose(QEMUFile *f) 244 { 245 int ret; 246 qemu_fflush(f); 247 ret = qemu_file_get_error(f); 248 249 if (f->ops->close) { 250 int ret2 = f->ops->close(f->opaque); 251 if (ret >= 0) { 252 ret = ret2; 253 } 254 } 255 /* If any error was spotted before closing, we should report it 256 * instead of the close() return value. 257 */ 258 if (f->last_error) { 259 ret = f->last_error; 260 } 261 g_free(f); 262 trace_qemu_file_fclose(); 263 return ret; 264 } 265 266 static void add_to_iovec(QEMUFile *f, const uint8_t *buf, int size) 267 { 268 /* check for adjacent buffer and coalesce them */ 269 if (f->iovcnt > 0 && buf == f->iov[f->iovcnt - 1].iov_base + 270 f->iov[f->iovcnt - 1].iov_len) { 271 f->iov[f->iovcnt - 1].iov_len += size; 272 } else { 273 f->iov[f->iovcnt].iov_base = (uint8_t *)buf; 274 f->iov[f->iovcnt++].iov_len = size; 275 } 276 277 if (f->iovcnt >= MAX_IOV_SIZE) { 278 qemu_fflush(f); 279 } 280 } 281 282 void qemu_put_buffer_async(QEMUFile *f, const uint8_t *buf, int size) 283 { 284 if (!f->ops->writev_buffer) { 285 qemu_put_buffer(f, buf, size); 286 return; 287 } 288 289 if (f->last_error) { 290 return; 291 } 292 293 f->bytes_xfer += size; 294 add_to_iovec(f, buf, size); 295 } 296 297 void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size) 298 { 299 int l; 300 301 if (f->last_error) { 302 return; 303 } 304 305 while (size > 0) { 306 l = IO_BUF_SIZE - f->buf_index; 307 if (l > size) { 308 l = size; 309 } 310 memcpy(f->buf + f->buf_index, buf, l); 311 f->bytes_xfer += l; 312 if (f->ops->writev_buffer) { 313 add_to_iovec(f, f->buf + f->buf_index, l); 314 } 315 f->buf_index += l; 316 if (f->buf_index == IO_BUF_SIZE) { 317 qemu_fflush(f); 318 } 319 if (qemu_file_get_error(f)) { 320 break; 321 } 322 buf += l; 323 size -= l; 324 } 325 } 326 327 void qemu_put_byte(QEMUFile *f, int v) 328 { 329 if (f->last_error) { 330 return; 331 } 332 333 f->buf[f->buf_index] = v; 334 f->bytes_xfer++; 335 if (f->ops->writev_buffer) { 336 add_to_iovec(f, f->buf + f->buf_index, 1); 337 } 338 f->buf_index++; 339 if (f->buf_index == IO_BUF_SIZE) { 340 qemu_fflush(f); 341 } 342 } 343 344 void qemu_file_skip(QEMUFile *f, int size) 345 { 346 if (f->buf_index + size <= f->buf_size) { 347 f->buf_index += size; 348 } 349 } 350 351 /* 352 * Read 'size' bytes from file (at 'offset') without moving the 353 * pointer and set 'buf' to point to that data. 354 * 355 * It will return size bytes unless there was an error, in which case it will 356 * return as many as it managed to read (assuming blocking fd's which 357 * all current QEMUFile are) 358 */ 359 int qemu_peek_buffer(QEMUFile *f, uint8_t **buf, int size, size_t offset) 360 { 361 int pending; 362 int index; 363 364 assert(!qemu_file_is_writable(f)); 365 assert(offset < IO_BUF_SIZE); 366 assert(size <= IO_BUF_SIZE - offset); 367 368 /* The 1st byte to read from */ 369 index = f->buf_index + offset; 370 /* The number of available bytes starting at index */ 371 pending = f->buf_size - index; 372 373 /* 374 * qemu_fill_buffer might return just a few bytes, even when there isn't 375 * an error, so loop collecting them until we get enough. 376 */ 377 while (pending < size) { 378 int received = qemu_fill_buffer(f); 379 380 if (received <= 0) { 381 break; 382 } 383 384 index = f->buf_index + offset; 385 pending = f->buf_size - index; 386 } 387 388 if (pending <= 0) { 389 return 0; 390 } 391 if (size > pending) { 392 size = pending; 393 } 394 395 *buf = f->buf + index; 396 return size; 397 } 398 399 /* 400 * Read 'size' bytes of data from the file into buf. 401 * 'size' can be larger than the internal buffer. 402 * 403 * It will return size bytes unless there was an error, in which case it will 404 * return as many as it managed to read (assuming blocking fd's which 405 * all current QEMUFile are) 406 */ 407 int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size) 408 { 409 int pending = size; 410 int done = 0; 411 412 while (pending > 0) { 413 int res; 414 uint8_t *src; 415 416 res = qemu_peek_buffer(f, &src, MIN(pending, IO_BUF_SIZE), 0); 417 if (res == 0) { 418 return done; 419 } 420 memcpy(buf, src, res); 421 qemu_file_skip(f, res); 422 buf += res; 423 pending -= res; 424 done += res; 425 } 426 return done; 427 } 428 429 /* 430 * Peeks a single byte from the buffer; this isn't guaranteed to work if 431 * offset leaves a gap after the previous read/peeked data. 432 */ 433 int qemu_peek_byte(QEMUFile *f, int offset) 434 { 435 int index = f->buf_index + offset; 436 437 assert(!qemu_file_is_writable(f)); 438 assert(offset < IO_BUF_SIZE); 439 440 if (index >= f->buf_size) { 441 qemu_fill_buffer(f); 442 index = f->buf_index + offset; 443 if (index >= f->buf_size) { 444 return 0; 445 } 446 } 447 return f->buf[index]; 448 } 449 450 int qemu_get_byte(QEMUFile *f) 451 { 452 int result; 453 454 result = qemu_peek_byte(f, 0); 455 qemu_file_skip(f, 1); 456 return result; 457 } 458 459 int64_t qemu_ftell_fast(QEMUFile *f) 460 { 461 int64_t ret = f->pos; 462 int i; 463 464 if (f->ops->writev_buffer) { 465 for (i = 0; i < f->iovcnt; i++) { 466 ret += f->iov[i].iov_len; 467 } 468 } else { 469 ret += f->buf_index; 470 } 471 472 return ret; 473 } 474 475 int64_t qemu_ftell(QEMUFile *f) 476 { 477 qemu_fflush(f); 478 return f->pos; 479 } 480 481 int qemu_file_rate_limit(QEMUFile *f) 482 { 483 if (qemu_file_get_error(f)) { 484 return 1; 485 } 486 if (f->xfer_limit > 0 && f->bytes_xfer > f->xfer_limit) { 487 return 1; 488 } 489 return 0; 490 } 491 492 int64_t qemu_file_get_rate_limit(QEMUFile *f) 493 { 494 return f->xfer_limit; 495 } 496 497 void qemu_file_set_rate_limit(QEMUFile *f, int64_t limit) 498 { 499 f->xfer_limit = limit; 500 } 501 502 void qemu_file_reset_rate_limit(QEMUFile *f) 503 { 504 f->bytes_xfer = 0; 505 } 506 507 void qemu_put_be16(QEMUFile *f, unsigned int v) 508 { 509 qemu_put_byte(f, v >> 8); 510 qemu_put_byte(f, v); 511 } 512 513 void qemu_put_be32(QEMUFile *f, unsigned int v) 514 { 515 qemu_put_byte(f, v >> 24); 516 qemu_put_byte(f, v >> 16); 517 qemu_put_byte(f, v >> 8); 518 qemu_put_byte(f, v); 519 } 520 521 void qemu_put_be64(QEMUFile *f, uint64_t v) 522 { 523 qemu_put_be32(f, v >> 32); 524 qemu_put_be32(f, v); 525 } 526 527 unsigned int qemu_get_be16(QEMUFile *f) 528 { 529 unsigned int v; 530 v = qemu_get_byte(f) << 8; 531 v |= qemu_get_byte(f); 532 return v; 533 } 534 535 unsigned int qemu_get_be32(QEMUFile *f) 536 { 537 unsigned int v; 538 v = (unsigned int)qemu_get_byte(f) << 24; 539 v |= qemu_get_byte(f) << 16; 540 v |= qemu_get_byte(f) << 8; 541 v |= qemu_get_byte(f); 542 return v; 543 } 544 545 uint64_t qemu_get_be64(QEMUFile *f) 546 { 547 uint64_t v; 548 v = (uint64_t)qemu_get_be32(f) << 32; 549 v |= qemu_get_be32(f); 550 return v; 551 } 552 553 /* compress size bytes of data start at p with specific compression 554 * level and store the compressed data to the buffer of f. 555 */ 556 557 ssize_t qemu_put_compression_data(QEMUFile *f, const uint8_t *p, size_t size, 558 int level) 559 { 560 ssize_t blen = IO_BUF_SIZE - f->buf_index - sizeof(int32_t); 561 562 if (blen < compressBound(size)) { 563 return 0; 564 } 565 if (compress2(f->buf + f->buf_index + sizeof(int32_t), (uLongf *)&blen, 566 (Bytef *)p, size, level) != Z_OK) { 567 error_report("Compress Failed!"); 568 return 0; 569 } 570 qemu_put_be32(f, blen); 571 f->buf_index += blen; 572 return blen + sizeof(int32_t); 573 } 574 575 /* Put the data in the buffer of f_src to the buffer of f_des, and 576 * then reset the buf_index of f_src to 0. 577 */ 578 579 int qemu_put_qemu_file(QEMUFile *f_des, QEMUFile *f_src) 580 { 581 int len = 0; 582 583 if (f_src->buf_index > 0) { 584 len = f_src->buf_index; 585 qemu_put_buffer(f_des, f_src->buf, f_src->buf_index); 586 f_src->buf_index = 0; 587 } 588 return len; 589 } 590 591 /* 592 * Get a string whose length is determined by a single preceding byte 593 * A preallocated 256 byte buffer must be passed in. 594 * Returns: len on success and a 0 terminated string in the buffer 595 * else 0 596 * (Note a 0 length string will return 0 either way) 597 */ 598 size_t qemu_get_counted_string(QEMUFile *f, char buf[256]) 599 { 600 size_t len = qemu_get_byte(f); 601 size_t res = qemu_get_buffer(f, (uint8_t *)buf, len); 602 603 buf[res] = 0; 604 605 return res == len ? res : 0; 606 } 607