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