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