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') into buf without moving the 353 * pointer. 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 memcpy(buf, f->buf + index, size); 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 415 res = qemu_peek_buffer(f, buf, MIN(pending, IO_BUF_SIZE), 0); 416 if (res == 0) { 417 return done; 418 } 419 qemu_file_skip(f, res); 420 buf += res; 421 pending -= res; 422 done += res; 423 } 424 return done; 425 } 426 427 /* 428 * Peeks a single byte from the buffer; this isn't guaranteed to work if 429 * offset leaves a gap after the previous read/peeked data. 430 */ 431 int qemu_peek_byte(QEMUFile *f, int offset) 432 { 433 int index = f->buf_index + offset; 434 435 assert(!qemu_file_is_writable(f)); 436 assert(offset < IO_BUF_SIZE); 437 438 if (index >= f->buf_size) { 439 qemu_fill_buffer(f); 440 index = f->buf_index + offset; 441 if (index >= f->buf_size) { 442 return 0; 443 } 444 } 445 return f->buf[index]; 446 } 447 448 int qemu_get_byte(QEMUFile *f) 449 { 450 int result; 451 452 result = qemu_peek_byte(f, 0); 453 qemu_file_skip(f, 1); 454 return result; 455 } 456 457 int64_t qemu_ftell_fast(QEMUFile *f) 458 { 459 int64_t ret = f->pos; 460 int i; 461 462 if (f->ops->writev_buffer) { 463 for (i = 0; i < f->iovcnt; i++) { 464 ret += f->iov[i].iov_len; 465 } 466 } else { 467 ret += f->buf_index; 468 } 469 470 return ret; 471 } 472 473 int64_t qemu_ftell(QEMUFile *f) 474 { 475 qemu_fflush(f); 476 return f->pos; 477 } 478 479 int qemu_file_rate_limit(QEMUFile *f) 480 { 481 if (qemu_file_get_error(f)) { 482 return 1; 483 } 484 if (f->xfer_limit > 0 && f->bytes_xfer > f->xfer_limit) { 485 return 1; 486 } 487 return 0; 488 } 489 490 int64_t qemu_file_get_rate_limit(QEMUFile *f) 491 { 492 return f->xfer_limit; 493 } 494 495 void qemu_file_set_rate_limit(QEMUFile *f, int64_t limit) 496 { 497 f->xfer_limit = limit; 498 } 499 500 void qemu_file_reset_rate_limit(QEMUFile *f) 501 { 502 f->bytes_xfer = 0; 503 } 504 505 void qemu_put_be16(QEMUFile *f, unsigned int v) 506 { 507 qemu_put_byte(f, v >> 8); 508 qemu_put_byte(f, v); 509 } 510 511 void qemu_put_be32(QEMUFile *f, unsigned int v) 512 { 513 qemu_put_byte(f, v >> 24); 514 qemu_put_byte(f, v >> 16); 515 qemu_put_byte(f, v >> 8); 516 qemu_put_byte(f, v); 517 } 518 519 void qemu_put_be64(QEMUFile *f, uint64_t v) 520 { 521 qemu_put_be32(f, v >> 32); 522 qemu_put_be32(f, v); 523 } 524 525 unsigned int qemu_get_be16(QEMUFile *f) 526 { 527 unsigned int v; 528 v = qemu_get_byte(f) << 8; 529 v |= qemu_get_byte(f); 530 return v; 531 } 532 533 unsigned int qemu_get_be32(QEMUFile *f) 534 { 535 unsigned int v; 536 v = (unsigned int)qemu_get_byte(f) << 24; 537 v |= qemu_get_byte(f) << 16; 538 v |= qemu_get_byte(f) << 8; 539 v |= qemu_get_byte(f); 540 return v; 541 } 542 543 uint64_t qemu_get_be64(QEMUFile *f) 544 { 545 uint64_t v; 546 v = (uint64_t)qemu_get_be32(f) << 32; 547 v |= qemu_get_be32(f); 548 return v; 549 } 550 551 /* compress size bytes of data start at p with specific compression 552 * level and store the compressed data to the buffer of f. 553 */ 554 555 ssize_t qemu_put_compression_data(QEMUFile *f, const uint8_t *p, size_t size, 556 int level) 557 { 558 ssize_t blen = IO_BUF_SIZE - f->buf_index - sizeof(int32_t); 559 560 if (blen < compressBound(size)) { 561 return 0; 562 } 563 if (compress2(f->buf + f->buf_index + sizeof(int32_t), (uLongf *)&blen, 564 (Bytef *)p, size, level) != Z_OK) { 565 error_report("Compress Failed!"); 566 return 0; 567 } 568 qemu_put_be32(f, blen); 569 f->buf_index += blen; 570 return blen + sizeof(int32_t); 571 } 572 573 /* Put the data in the buffer of f_src to the buffer of f_des, and 574 * then reset the buf_index of f_src to 0. 575 */ 576 577 int qemu_put_qemu_file(QEMUFile *f_des, QEMUFile *f_src) 578 { 579 int len = 0; 580 581 if (f_src->buf_index > 0) { 582 len = f_src->buf_index; 583 qemu_put_buffer(f_des, f_src->buf, f_src->buf_index); 584 f_src->buf_index = 0; 585 } 586 return len; 587 } 588