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