xref: /qemu/migration/qemu-file.c (revision 43bfb507)
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-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"
30 #include "migration/qemu-file-internal.h"
31 #include "trace.h"
32 
33 /*
34  * Stop a file from being read/written - not all backing files can do this
35  * typically only sockets can.
36  */
37 int 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 
45 bool 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 
57 QEMUFile *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  */
75 int qemu_file_get_error(QEMUFile *f)
76 {
77     return f->last_error;
78 }
79 
80 void qemu_file_set_error(QEMUFile *f, int ret)
81 {
82     if (f->last_error == 0) {
83         f->last_error = ret;
84     }
85 }
86 
87 bool qemu_file_is_writable(QEMUFile *f)
88 {
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  */
98 void 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 
125 void 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 
137 void 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 
149 void 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 
163 size_t ram_control_save_page(QEMUFile *f, ram_addr_t block_offset,
164                          ram_addr_t offset, size_t size, int *bytes_sent)
165 {
166     if (f->ops->save_page) {
167         int ret = f->ops->save_page(f, f->opaque, block_offset,
168                                     offset, size, bytes_sent);
169 
170         if (ret != RAM_SAVE_CONTROL_DELAYED) {
171             if (bytes_sent && *bytes_sent > 0) {
172                 qemu_update_position(f, *bytes_sent);
173             } else if (ret < 0) {
174                 qemu_file_set_error(f, ret);
175             }
176         }
177 
178         return ret;
179     }
180 
181     return RAM_SAVE_CONTROL_NOT_SUPP;
182 }
183 
184 /*
185  * Attempt to fill the buffer from the underlying file
186  * Returns the number of bytes read, or negative value for an error.
187  *
188  * Note that it can return a partially full buffer even in a not error/not EOF
189  * case if the underlying file descriptor gives a short read, and that can
190  * happen even on a blocking fd.
191  */
192 static ssize_t qemu_fill_buffer(QEMUFile *f)
193 {
194     int len;
195     int pending;
196 
197     assert(!qemu_file_is_writable(f));
198 
199     pending = f->buf_size - f->buf_index;
200     if (pending > 0) {
201         memmove(f->buf, f->buf + f->buf_index, pending);
202     }
203     f->buf_index = 0;
204     f->buf_size = pending;
205 
206     len = f->ops->get_buffer(f->opaque, f->buf + pending, f->pos,
207                         IO_BUF_SIZE - pending);
208     if (len > 0) {
209         f->buf_size += len;
210         f->pos += len;
211     } else if (len == 0) {
212         qemu_file_set_error(f, -EIO);
213     } else if (len != -EAGAIN) {
214         qemu_file_set_error(f, len);
215     }
216 
217     return len;
218 }
219 
220 int qemu_get_fd(QEMUFile *f)
221 {
222     if (f->ops->get_fd) {
223         return f->ops->get_fd(f->opaque);
224     }
225     return -1;
226 }
227 
228 void qemu_update_position(QEMUFile *f, size_t size)
229 {
230     f->pos += size;
231 }
232 
233 /** Closes the file
234  *
235  * Returns negative error value if any error happened on previous operations or
236  * while closing the file. Returns 0 or positive number on success.
237  *
238  * The meaning of return value on success depends on the specific backend
239  * being used.
240  */
241 int qemu_fclose(QEMUFile *f)
242 {
243     int ret;
244     qemu_fflush(f);
245     ret = qemu_file_get_error(f);
246 
247     if (f->ops->close) {
248         int ret2 = f->ops->close(f->opaque);
249         if (ret >= 0) {
250             ret = ret2;
251         }
252     }
253     /* If any error was spotted before closing, we should report it
254      * instead of the close() return value.
255      */
256     if (f->last_error) {
257         ret = f->last_error;
258     }
259     g_free(f);
260     trace_qemu_file_fclose();
261     return ret;
262 }
263 
264 static void add_to_iovec(QEMUFile *f, const uint8_t *buf, int size)
265 {
266     /* check for adjacent buffer and coalesce them */
267     if (f->iovcnt > 0 && buf == f->iov[f->iovcnt - 1].iov_base +
268         f->iov[f->iovcnt - 1].iov_len) {
269         f->iov[f->iovcnt - 1].iov_len += size;
270     } else {
271         f->iov[f->iovcnt].iov_base = (uint8_t *)buf;
272         f->iov[f->iovcnt++].iov_len = size;
273     }
274 
275     if (f->iovcnt >= MAX_IOV_SIZE) {
276         qemu_fflush(f);
277     }
278 }
279 
280 void qemu_put_buffer_async(QEMUFile *f, const uint8_t *buf, int size)
281 {
282     if (!f->ops->writev_buffer) {
283         qemu_put_buffer(f, buf, size);
284         return;
285     }
286 
287     if (f->last_error) {
288         return;
289     }
290 
291     f->bytes_xfer += size;
292     add_to_iovec(f, buf, size);
293 }
294 
295 void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size)
296 {
297     int l;
298 
299     if (f->last_error) {
300         return;
301     }
302 
303     while (size > 0) {
304         l = IO_BUF_SIZE - f->buf_index;
305         if (l > size) {
306             l = size;
307         }
308         memcpy(f->buf + f->buf_index, buf, l);
309         f->bytes_xfer += l;
310         if (f->ops->writev_buffer) {
311             add_to_iovec(f, f->buf + f->buf_index, l);
312         }
313         f->buf_index += l;
314         if (f->buf_index == IO_BUF_SIZE) {
315             qemu_fflush(f);
316         }
317         if (qemu_file_get_error(f)) {
318             break;
319         }
320         buf += l;
321         size -= l;
322     }
323 }
324 
325 void qemu_put_byte(QEMUFile *f, int v)
326 {
327     if (f->last_error) {
328         return;
329     }
330 
331     f->buf[f->buf_index] = v;
332     f->bytes_xfer++;
333     if (f->ops->writev_buffer) {
334         add_to_iovec(f, f->buf + f->buf_index, 1);
335     }
336     f->buf_index++;
337     if (f->buf_index == IO_BUF_SIZE) {
338         qemu_fflush(f);
339     }
340 }
341 
342 void qemu_file_skip(QEMUFile *f, int size)
343 {
344     if (f->buf_index + size <= f->buf_size) {
345         f->buf_index += size;
346     }
347 }
348 
349 /*
350  * Read 'size' bytes from file (at 'offset') into buf without moving the
351  * pointer.
352  *
353  * It will return size bytes unless there was an error, in which case it will
354  * return as many as it managed to read (assuming blocking fd's which
355  * all current QEMUFile are)
356  */
357 int qemu_peek_buffer(QEMUFile *f, uint8_t *buf, int size, size_t offset)
358 {
359     int pending;
360     int index;
361 
362     assert(!qemu_file_is_writable(f));
363     assert(offset < IO_BUF_SIZE);
364     assert(size <= IO_BUF_SIZE - offset);
365 
366     /* The 1st byte to read from */
367     index = f->buf_index + offset;
368     /* The number of available bytes starting at index */
369     pending = f->buf_size - index;
370 
371     /*
372      * qemu_fill_buffer might return just a few bytes, even when there isn't
373      * an error, so loop collecting them until we get enough.
374      */
375     while (pending < size) {
376         int received = qemu_fill_buffer(f);
377 
378         if (received <= 0) {
379             break;
380         }
381 
382         index = f->buf_index + offset;
383         pending = f->buf_size - index;
384     }
385 
386     if (pending <= 0) {
387         return 0;
388     }
389     if (size > pending) {
390         size = pending;
391     }
392 
393     memcpy(buf, f->buf + index, size);
394     return size;
395 }
396 
397 /*
398  * Read 'size' bytes of data from the file into buf.
399  * 'size' can be larger than the internal buffer.
400  *
401  * It will return size bytes unless there was an error, in which case it will
402  * return as many as it managed to read (assuming blocking fd's which
403  * all current QEMUFile are)
404  */
405 int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size)
406 {
407     int pending = size;
408     int done = 0;
409 
410     while (pending > 0) {
411         int res;
412 
413         res = qemu_peek_buffer(f, buf, MIN(pending, IO_BUF_SIZE), 0);
414         if (res == 0) {
415             return done;
416         }
417         qemu_file_skip(f, res);
418         buf += res;
419         pending -= res;
420         done += res;
421     }
422     return done;
423 }
424 
425 /*
426  * Peeks a single byte from the buffer; this isn't guaranteed to work if
427  * offset leaves a gap after the previous read/peeked data.
428  */
429 int qemu_peek_byte(QEMUFile *f, int offset)
430 {
431     int index = f->buf_index + offset;
432 
433     assert(!qemu_file_is_writable(f));
434     assert(offset < IO_BUF_SIZE);
435 
436     if (index >= f->buf_size) {
437         qemu_fill_buffer(f);
438         index = f->buf_index + offset;
439         if (index >= f->buf_size) {
440             return 0;
441         }
442     }
443     return f->buf[index];
444 }
445 
446 int qemu_get_byte(QEMUFile *f)
447 {
448     int result;
449 
450     result = qemu_peek_byte(f, 0);
451     qemu_file_skip(f, 1);
452     return result;
453 }
454 
455 int64_t qemu_ftell(QEMUFile *f)
456 {
457     qemu_fflush(f);
458     return f->pos;
459 }
460 
461 int qemu_file_rate_limit(QEMUFile *f)
462 {
463     if (qemu_file_get_error(f)) {
464         return 1;
465     }
466     if (f->xfer_limit > 0 && f->bytes_xfer > f->xfer_limit) {
467         return 1;
468     }
469     return 0;
470 }
471 
472 int64_t qemu_file_get_rate_limit(QEMUFile *f)
473 {
474     return f->xfer_limit;
475 }
476 
477 void qemu_file_set_rate_limit(QEMUFile *f, int64_t limit)
478 {
479     f->xfer_limit = limit;
480 }
481 
482 void qemu_file_reset_rate_limit(QEMUFile *f)
483 {
484     f->bytes_xfer = 0;
485 }
486 
487 void qemu_put_be16(QEMUFile *f, unsigned int v)
488 {
489     qemu_put_byte(f, v >> 8);
490     qemu_put_byte(f, v);
491 }
492 
493 void qemu_put_be32(QEMUFile *f, unsigned int v)
494 {
495     qemu_put_byte(f, v >> 24);
496     qemu_put_byte(f, v >> 16);
497     qemu_put_byte(f, v >> 8);
498     qemu_put_byte(f, v);
499 }
500 
501 void qemu_put_be64(QEMUFile *f, uint64_t v)
502 {
503     qemu_put_be32(f, v >> 32);
504     qemu_put_be32(f, v);
505 }
506 
507 unsigned int qemu_get_be16(QEMUFile *f)
508 {
509     unsigned int v;
510     v = qemu_get_byte(f) << 8;
511     v |= qemu_get_byte(f);
512     return v;
513 }
514 
515 unsigned int qemu_get_be32(QEMUFile *f)
516 {
517     unsigned int v;
518     v = (unsigned int)qemu_get_byte(f) << 24;
519     v |= qemu_get_byte(f) << 16;
520     v |= qemu_get_byte(f) << 8;
521     v |= qemu_get_byte(f);
522     return v;
523 }
524 
525 uint64_t qemu_get_be64(QEMUFile *f)
526 {
527     uint64_t v;
528     v = (uint64_t)qemu_get_be32(f) << 32;
529     v |= qemu_get_be32(f);
530     return v;
531 }
532