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