blob: 1a4f9868ed57dafb449b2d526191e547e4e9c37f [file] [log] [blame]
Eduardo Habkost9a4ac512014-10-01 14:47:33 -03001/*
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 */
Eduardo Habkost093c4552013-11-28 12:01:16 -020024#include "qemu-common.h"
25#include "qemu/iov.h"
26#include "qemu/sockets.h"
27#include "block/coroutine.h"
28#include "migration/migration.h"
29#include "migration/qemu-file.h"
Dr. David Alan Gilbert4f9d0902014-12-12 11:13:40 +000030#include "migration/qemu-file-internal.h"
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +110031#include "trace.h"
Eduardo Habkost093c4552013-11-28 12:01:16 -020032
Dr. David Alan Gilberte1a8c9b2015-01-08 11:11:30 +000033/*
34 * Stop a file from being read/written - not all backing files can do this
35 * typically only sockets can.
36 */
37int qemu_file_shutdown(QEMUFile *f)
38{
39 if (!f->ops->shut_down) {
40 return -ENOSYS;
41 }
42 return f->ops->shut_down(f->opaque, true, true);
43}
44
Eduardo Habkost093c4552013-11-28 12:01:16 -020045bool qemu_file_mode_is_not_valid(const char *mode)
46{
47 if (mode == NULL ||
48 (mode[0] != 'r' && mode[0] != 'w') ||
49 mode[1] != 'b' || mode[2] != 0) {
50 fprintf(stderr, "qemu_fopen: Argument validity check failed\n");
51 return true;
52 }
53
54 return false;
55}
56
Eduardo Habkost093c4552013-11-28 12:01:16 -020057QEMUFile *qemu_fopen_ops(void *opaque, const QEMUFileOps *ops)
58{
59 QEMUFile *f;
60
61 f = g_malloc0(sizeof(QEMUFile));
62
63 f->opaque = opaque;
64 f->ops = ops;
65 return f;
66}
67
68/*
69 * Get last error for stream f
70 *
71 * Return negative error value if there has been an error on previous
72 * operations, return 0 if no error happened.
73 *
74 */
75int qemu_file_get_error(QEMUFile *f)
76{
77 return f->last_error;
78}
79
80void qemu_file_set_error(QEMUFile *f, int ret)
81{
82 if (f->last_error == 0) {
83 f->last_error = ret;
84 }
85}
86
Eduardo Habkoste68dd362014-10-01 17:34:34 -030087bool qemu_file_is_writable(QEMUFile *f)
Eduardo Habkost093c4552013-11-28 12:01:16 -020088{
89 return f->ops->writev_buffer || f->ops->put_buffer;
90}
91
92/**
93 * Flushes QEMUFile buffer
94 *
95 * If there is writev_buffer QEMUFileOps it uses it otherwise uses
96 * put_buffer ops.
97 */
98void qemu_fflush(QEMUFile *f)
99{
100 ssize_t ret = 0;
101
102 if (!qemu_file_is_writable(f)) {
103 return;
104 }
105
106 if (f->ops->writev_buffer) {
107 if (f->iovcnt > 0) {
108 ret = f->ops->writev_buffer(f->opaque, f->iov, f->iovcnt, f->pos);
109 }
110 } else {
111 if (f->buf_index > 0) {
112 ret = f->ops->put_buffer(f->opaque, f->buf, f->pos, f->buf_index);
113 }
114 }
115 if (ret >= 0) {
116 f->pos += ret;
117 }
118 f->buf_index = 0;
119 f->iovcnt = 0;
120 if (ret < 0) {
121 qemu_file_set_error(f, ret);
122 }
123}
124
125void ram_control_before_iterate(QEMUFile *f, uint64_t flags)
126{
127 int ret = 0;
128
129 if (f->ops->before_ram_iterate) {
130 ret = f->ops->before_ram_iterate(f, f->opaque, flags);
131 if (ret < 0) {
132 qemu_file_set_error(f, ret);
133 }
134 }
135}
136
137void ram_control_after_iterate(QEMUFile *f, uint64_t flags)
138{
139 int ret = 0;
140
141 if (f->ops->after_ram_iterate) {
142 ret = f->ops->after_ram_iterate(f, f->opaque, flags);
143 if (ret < 0) {
144 qemu_file_set_error(f, ret);
145 }
146 }
147}
148
149void ram_control_load_hook(QEMUFile *f, uint64_t flags)
150{
151 int ret = -EINVAL;
152
153 if (f->ops->hook_ram_load) {
154 ret = f->ops->hook_ram_load(f, f->opaque, flags);
155 if (ret < 0) {
156 qemu_file_set_error(f, ret);
157 }
158 } else {
159 qemu_file_set_error(f, ret);
160 }
161}
162
163size_t ram_control_save_page(QEMUFile *f, ram_addr_t block_offset,
Juan Quintela6e1dea42015-02-12 19:02:42 +0100164 ram_addr_t offset, size_t size,
165 uint64_t *bytes_sent)
Eduardo Habkost093c4552013-11-28 12:01:16 -0200166{
167 if (f->ops->save_page) {
168 int ret = f->ops->save_page(f, f->opaque, block_offset,
169 offset, size, bytes_sent);
170
171 if (ret != RAM_SAVE_CONTROL_DELAYED) {
172 if (bytes_sent && *bytes_sent > 0) {
173 qemu_update_position(f, *bytes_sent);
174 } else if (ret < 0) {
175 qemu_file_set_error(f, ret);
176 }
177 }
178
179 return ret;
180 }
181
182 return RAM_SAVE_CONTROL_NOT_SUPP;
183}
184
Dr. David Alan Gilbert548f52e2014-04-08 15:29:37 +0100185/*
186 * Attempt to fill the buffer from the underlying file
187 * Returns the number of bytes read, or negative value for an error.
188 *
189 * Note that it can return a partially full buffer even in a not error/not EOF
190 * case if the underlying file descriptor gives a short read, and that can
191 * happen even on a blocking fd.
192 */
193static ssize_t qemu_fill_buffer(QEMUFile *f)
Eduardo Habkost093c4552013-11-28 12:01:16 -0200194{
195 int len;
196 int pending;
197
198 assert(!qemu_file_is_writable(f));
199
200 pending = f->buf_size - f->buf_index;
201 if (pending > 0) {
202 memmove(f->buf, f->buf + f->buf_index, pending);
203 }
204 f->buf_index = 0;
205 f->buf_size = pending;
206
207 len = f->ops->get_buffer(f->opaque, f->buf + pending, f->pos,
208 IO_BUF_SIZE - pending);
209 if (len > 0) {
210 f->buf_size += len;
211 f->pos += len;
212 } else if (len == 0) {
213 qemu_file_set_error(f, -EIO);
214 } else if (len != -EAGAIN) {
215 qemu_file_set_error(f, len);
216 }
Dr. David Alan Gilbert548f52e2014-04-08 15:29:37 +0100217
218 return len;
Eduardo Habkost093c4552013-11-28 12:01:16 -0200219}
220
221int qemu_get_fd(QEMUFile *f)
222{
223 if (f->ops->get_fd) {
224 return f->ops->get_fd(f->opaque);
225 }
226 return -1;
227}
228
229void qemu_update_position(QEMUFile *f, size_t size)
230{
231 f->pos += size;
232}
233
234/** Closes the file
235 *
236 * Returns negative error value if any error happened on previous operations or
237 * while closing the file. Returns 0 or positive number on success.
238 *
239 * The meaning of return value on success depends on the specific backend
240 * being used.
241 */
242int qemu_fclose(QEMUFile *f)
243{
244 int ret;
245 qemu_fflush(f);
246 ret = qemu_file_get_error(f);
247
248 if (f->ops->close) {
249 int ret2 = f->ops->close(f->opaque);
250 if (ret >= 0) {
251 ret = ret2;
252 }
253 }
254 /* If any error was spotted before closing, we should report it
255 * instead of the close() return value.
256 */
257 if (f->last_error) {
258 ret = f->last_error;
259 }
260 g_free(f);
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +1100261 trace_qemu_file_fclose();
Eduardo Habkost093c4552013-11-28 12:01:16 -0200262 return ret;
263}
264
265static void add_to_iovec(QEMUFile *f, const uint8_t *buf, int size)
266{
267 /* check for adjacent buffer and coalesce them */
268 if (f->iovcnt > 0 && buf == f->iov[f->iovcnt - 1].iov_base +
269 f->iov[f->iovcnt - 1].iov_len) {
270 f->iov[f->iovcnt - 1].iov_len += size;
271 } else {
272 f->iov[f->iovcnt].iov_base = (uint8_t *)buf;
273 f->iov[f->iovcnt++].iov_len = size;
274 }
275
276 if (f->iovcnt >= MAX_IOV_SIZE) {
277 qemu_fflush(f);
278 }
279}
280
281void qemu_put_buffer_async(QEMUFile *f, const uint8_t *buf, int size)
282{
283 if (!f->ops->writev_buffer) {
284 qemu_put_buffer(f, buf, size);
285 return;
286 }
287
288 if (f->last_error) {
289 return;
290 }
291
292 f->bytes_xfer += size;
293 add_to_iovec(f, buf, size);
294}
295
296void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size)
297{
298 int l;
299
300 if (f->last_error) {
301 return;
302 }
303
304 while (size > 0) {
305 l = IO_BUF_SIZE - f->buf_index;
306 if (l > size) {
307 l = size;
308 }
309 memcpy(f->buf + f->buf_index, buf, l);
310 f->bytes_xfer += l;
311 if (f->ops->writev_buffer) {
312 add_to_iovec(f, f->buf + f->buf_index, l);
313 }
314 f->buf_index += l;
315 if (f->buf_index == IO_BUF_SIZE) {
316 qemu_fflush(f);
317 }
318 if (qemu_file_get_error(f)) {
319 break;
320 }
321 buf += l;
322 size -= l;
323 }
324}
325
326void qemu_put_byte(QEMUFile *f, int v)
327{
328 if (f->last_error) {
329 return;
330 }
331
332 f->buf[f->buf_index] = v;
333 f->bytes_xfer++;
334 if (f->ops->writev_buffer) {
335 add_to_iovec(f, f->buf + f->buf_index, 1);
336 }
337 f->buf_index++;
338 if (f->buf_index == IO_BUF_SIZE) {
339 qemu_fflush(f);
340 }
341}
342
343void qemu_file_skip(QEMUFile *f, int size)
344{
345 if (f->buf_index + size <= f->buf_size) {
346 f->buf_index += size;
347 }
348}
349
Dr. David Alan Gilbert548f52e2014-04-08 15:29:37 +0100350/*
351 * Read 'size' bytes from file (at 'offset') into buf without moving the
352 * pointer.
353 *
354 * It will return size bytes unless there was an error, in which case it will
355 * return as many as it managed to read (assuming blocking fd's which
356 * all current QEMUFile are)
357 */
Eduardo Habkost093c4552013-11-28 12:01:16 -0200358int qemu_peek_buffer(QEMUFile *f, uint8_t *buf, int size, size_t offset)
359{
360 int pending;
361 int index;
362
363 assert(!qemu_file_is_writable(f));
Dr. David Alan Gilbert548f52e2014-04-08 15:29:37 +0100364 assert(offset < IO_BUF_SIZE);
365 assert(size <= IO_BUF_SIZE - offset);
Eduardo Habkost093c4552013-11-28 12:01:16 -0200366
Dr. David Alan Gilbert548f52e2014-04-08 15:29:37 +0100367 /* The 1st byte to read from */
Eduardo Habkost093c4552013-11-28 12:01:16 -0200368 index = f->buf_index + offset;
Dr. David Alan Gilbert548f52e2014-04-08 15:29:37 +0100369 /* The number of available bytes starting at index */
Eduardo Habkost093c4552013-11-28 12:01:16 -0200370 pending = f->buf_size - index;
Dr. David Alan Gilbert548f52e2014-04-08 15:29:37 +0100371
372 /*
373 * qemu_fill_buffer might return just a few bytes, even when there isn't
374 * an error, so loop collecting them until we get enough.
375 */
376 while (pending < size) {
377 int received = qemu_fill_buffer(f);
378
379 if (received <= 0) {
380 break;
381 }
382
Eduardo Habkost093c4552013-11-28 12:01:16 -0200383 index = f->buf_index + offset;
384 pending = f->buf_size - index;
385 }
386
387 if (pending <= 0) {
388 return 0;
389 }
390 if (size > pending) {
391 size = pending;
392 }
393
394 memcpy(buf, f->buf + index, size);
395 return size;
396}
397
Dr. David Alan Gilbert548f52e2014-04-08 15:29:37 +0100398/*
399 * Read 'size' bytes of data from the file into buf.
400 * 'size' can be larger than the internal buffer.
401 *
402 * It will return size bytes unless there was an error, in which case it will
403 * return as many as it managed to read (assuming blocking fd's which
404 * all current QEMUFile are)
405 */
Eduardo Habkost093c4552013-11-28 12:01:16 -0200406int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size)
407{
408 int pending = size;
409 int done = 0;
410
411 while (pending > 0) {
412 int res;
413
Dr. David Alan Gilbert548f52e2014-04-08 15:29:37 +0100414 res = qemu_peek_buffer(f, buf, MIN(pending, IO_BUF_SIZE), 0);
Eduardo Habkost093c4552013-11-28 12:01:16 -0200415 if (res == 0) {
416 return done;
417 }
418 qemu_file_skip(f, res);
419 buf += res;
420 pending -= res;
421 done += res;
422 }
423 return done;
424}
425
Dr. David Alan Gilbert548f52e2014-04-08 15:29:37 +0100426/*
427 * Peeks a single byte from the buffer; this isn't guaranteed to work if
428 * offset leaves a gap after the previous read/peeked data.
429 */
Eduardo Habkost093c4552013-11-28 12:01:16 -0200430int qemu_peek_byte(QEMUFile *f, int offset)
431{
432 int index = f->buf_index + offset;
433
434 assert(!qemu_file_is_writable(f));
Dr. David Alan Gilbert548f52e2014-04-08 15:29:37 +0100435 assert(offset < IO_BUF_SIZE);
Eduardo Habkost093c4552013-11-28 12:01:16 -0200436
437 if (index >= f->buf_size) {
438 qemu_fill_buffer(f);
439 index = f->buf_index + offset;
440 if (index >= f->buf_size) {
441 return 0;
442 }
443 }
444 return f->buf[index];
445}
446
447int qemu_get_byte(QEMUFile *f)
448{
449 int result;
450
451 result = qemu_peek_byte(f, 0);
452 qemu_file_skip(f, 1);
453 return result;
454}
455
Alexander Graf97221402015-01-22 15:01:38 +0100456int64_t qemu_ftell_fast(QEMUFile *f)
457{
458 int64_t ret = f->pos;
459 int i;
460
461 if (f->ops->writev_buffer) {
462 for (i = 0; i < f->iovcnt; i++) {
463 ret += f->iov[i].iov_len;
464 }
465 } else {
466 ret += f->buf_index;
467 }
468
469 return ret;
470}
471
Eduardo Habkost093c4552013-11-28 12:01:16 -0200472int64_t qemu_ftell(QEMUFile *f)
473{
474 qemu_fflush(f);
475 return f->pos;
476}
477
478int qemu_file_rate_limit(QEMUFile *f)
479{
480 if (qemu_file_get_error(f)) {
481 return 1;
482 }
483 if (f->xfer_limit > 0 && f->bytes_xfer > f->xfer_limit) {
484 return 1;
485 }
486 return 0;
487}
488
489int64_t qemu_file_get_rate_limit(QEMUFile *f)
490{
491 return f->xfer_limit;
492}
493
494void qemu_file_set_rate_limit(QEMUFile *f, int64_t limit)
495{
496 f->xfer_limit = limit;
497}
498
499void qemu_file_reset_rate_limit(QEMUFile *f)
500{
501 f->bytes_xfer = 0;
502}
503
504void qemu_put_be16(QEMUFile *f, unsigned int v)
505{
506 qemu_put_byte(f, v >> 8);
507 qemu_put_byte(f, v);
508}
509
510void qemu_put_be32(QEMUFile *f, unsigned int v)
511{
512 qemu_put_byte(f, v >> 24);
513 qemu_put_byte(f, v >> 16);
514 qemu_put_byte(f, v >> 8);
515 qemu_put_byte(f, v);
516}
517
518void qemu_put_be64(QEMUFile *f, uint64_t v)
519{
520 qemu_put_be32(f, v >> 32);
521 qemu_put_be32(f, v);
522}
523
524unsigned int qemu_get_be16(QEMUFile *f)
525{
526 unsigned int v;
527 v = qemu_get_byte(f) << 8;
528 v |= qemu_get_byte(f);
529 return v;
530}
531
532unsigned int qemu_get_be32(QEMUFile *f)
533{
534 unsigned int v;
Peter Maydell90d6a672014-12-23 22:26:55 +0000535 v = (unsigned int)qemu_get_byte(f) << 24;
Eduardo Habkost093c4552013-11-28 12:01:16 -0200536 v |= qemu_get_byte(f) << 16;
537 v |= qemu_get_byte(f) << 8;
538 v |= qemu_get_byte(f);
539 return v;
540}
541
542uint64_t qemu_get_be64(QEMUFile *f)
543{
544 uint64_t v;
545 v = (uint64_t)qemu_get_be32(f) << 32;
546 v |= qemu_get_be32(f);
547 return v;
548}