Merge remote-tracking branch 'remotes/kevin/tags/for-upstream' into staging
[qemu.git] / qemu-file.c
1 #include "qemu-common.h"
2 #include "qemu/iov.h"
3 #include "qemu/sockets.h"
4 #include "block/coroutine.h"
5 #include "migration/migration.h"
6 #include "migration/qemu-file.h"
7
8 #define IO_BUF_SIZE 32768
9 #define MAX_IOV_SIZE MIN(IOV_MAX, 64)
10
11 struct QEMUFile {
12 const QEMUFileOps *ops;
13 void *opaque;
14
15 int64_t bytes_xfer;
16 int64_t xfer_limit;
17
18 int64_t pos; /* start of buffer when writing, end of buffer
19 when reading */
20 int buf_index;
21 int buf_size; /* 0 when writing */
22 uint8_t buf[IO_BUF_SIZE];
23
24 struct iovec iov[MAX_IOV_SIZE];
25 unsigned int iovcnt;
26
27 int last_error;
28 };
29
30 typedef struct QEMUFileStdio {
31 FILE *stdio_file;
32 QEMUFile *file;
33 } QEMUFileStdio;
34
35 typedef struct QEMUFileSocket {
36 int fd;
37 QEMUFile *file;
38 } QEMUFileSocket;
39
40 static ssize_t socket_writev_buffer(void *opaque, struct iovec *iov, int iovcnt,
41 int64_t pos)
42 {
43 QEMUFileSocket *s = opaque;
44 ssize_t len;
45 ssize_t size = iov_size(iov, iovcnt);
46
47 len = iov_send(s->fd, iov, iovcnt, 0, size);
48 if (len < size) {
49 len = -socket_error();
50 }
51 return len;
52 }
53
54 static int socket_get_fd(void *opaque)
55 {
56 QEMUFileSocket *s = opaque;
57
58 return s->fd;
59 }
60
61 static int socket_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
62 {
63 QEMUFileSocket *s = opaque;
64 ssize_t len;
65
66 for (;;) {
67 len = qemu_recv(s->fd, buf, size, 0);
68 if (len != -1) {
69 break;
70 }
71 if (socket_error() == EAGAIN) {
72 yield_until_fd_readable(s->fd);
73 } else if (socket_error() != EINTR) {
74 break;
75 }
76 }
77
78 if (len == -1) {
79 len = -socket_error();
80 }
81 return len;
82 }
83
84 static int socket_close(void *opaque)
85 {
86 QEMUFileSocket *s = opaque;
87 closesocket(s->fd);
88 g_free(s);
89 return 0;
90 }
91
92 static int stdio_get_fd(void *opaque)
93 {
94 QEMUFileStdio *s = opaque;
95
96 return fileno(s->stdio_file);
97 }
98
99 static int stdio_put_buffer(void *opaque, const uint8_t *buf, int64_t pos,
100 int size)
101 {
102 QEMUFileStdio *s = opaque;
103 int res;
104
105 res = fwrite(buf, 1, size, s->stdio_file);
106
107 if (res != size) {
108 return -errno;
109 }
110 return res;
111 }
112
113 static int stdio_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
114 {
115 QEMUFileStdio *s = opaque;
116 FILE *fp = s->stdio_file;
117 int bytes;
118
119 for (;;) {
120 clearerr(fp);
121 bytes = fread(buf, 1, size, fp);
122 if (bytes != 0 || !ferror(fp)) {
123 break;
124 }
125 if (errno == EAGAIN) {
126 yield_until_fd_readable(fileno(fp));
127 } else if (errno != EINTR) {
128 break;
129 }
130 }
131 return bytes;
132 }
133
134 static int stdio_pclose(void *opaque)
135 {
136 QEMUFileStdio *s = opaque;
137 int ret;
138 ret = pclose(s->stdio_file);
139 if (ret == -1) {
140 ret = -errno;
141 } else if (!WIFEXITED(ret) || WEXITSTATUS(ret) != 0) {
142 /* close succeeded, but non-zero exit code: */
143 ret = -EIO; /* fake errno value */
144 }
145 g_free(s);
146 return ret;
147 }
148
149 static int stdio_fclose(void *opaque)
150 {
151 QEMUFileStdio *s = opaque;
152 int ret = 0;
153
154 if (s->file->ops->put_buffer || s->file->ops->writev_buffer) {
155 int fd = fileno(s->stdio_file);
156 struct stat st;
157
158 ret = fstat(fd, &st);
159 if (ret == 0 && S_ISREG(st.st_mode)) {
160 /*
161 * If the file handle is a regular file make sure the
162 * data is flushed to disk before signaling success.
163 */
164 ret = fsync(fd);
165 if (ret != 0) {
166 ret = -errno;
167 return ret;
168 }
169 }
170 }
171 if (fclose(s->stdio_file) == EOF) {
172 ret = -errno;
173 }
174 g_free(s);
175 return ret;
176 }
177
178 static const QEMUFileOps stdio_pipe_read_ops = {
179 .get_fd = stdio_get_fd,
180 .get_buffer = stdio_get_buffer,
181 .close = stdio_pclose
182 };
183
184 static const QEMUFileOps stdio_pipe_write_ops = {
185 .get_fd = stdio_get_fd,
186 .put_buffer = stdio_put_buffer,
187 .close = stdio_pclose
188 };
189
190 QEMUFile *qemu_popen_cmd(const char *command, const char *mode)
191 {
192 FILE *stdio_file;
193 QEMUFileStdio *s;
194
195 if (mode == NULL || (mode[0] != 'r' && mode[0] != 'w') || mode[1] != 0) {
196 fprintf(stderr, "qemu_popen: Argument validity check failed\n");
197 return NULL;
198 }
199
200 stdio_file = popen(command, mode);
201 if (stdio_file == NULL) {
202 return NULL;
203 }
204
205 s = g_malloc0(sizeof(QEMUFileStdio));
206
207 s->stdio_file = stdio_file;
208
209 if (mode[0] == 'r') {
210 s->file = qemu_fopen_ops(s, &stdio_pipe_read_ops);
211 } else {
212 s->file = qemu_fopen_ops(s, &stdio_pipe_write_ops);
213 }
214 return s->file;
215 }
216
217 static const QEMUFileOps stdio_file_read_ops = {
218 .get_fd = stdio_get_fd,
219 .get_buffer = stdio_get_buffer,
220 .close = stdio_fclose
221 };
222
223 static const QEMUFileOps stdio_file_write_ops = {
224 .get_fd = stdio_get_fd,
225 .put_buffer = stdio_put_buffer,
226 .close = stdio_fclose
227 };
228
229 static ssize_t unix_writev_buffer(void *opaque, struct iovec *iov, int iovcnt,
230 int64_t pos)
231 {
232 QEMUFileSocket *s = opaque;
233 ssize_t len, offset;
234 ssize_t size = iov_size(iov, iovcnt);
235 ssize_t total = 0;
236
237 assert(iovcnt > 0);
238 offset = 0;
239 while (size > 0) {
240 /* Find the next start position; skip all full-sized vector elements */
241 while (offset >= iov[0].iov_len) {
242 offset -= iov[0].iov_len;
243 iov++, iovcnt--;
244 }
245
246 /* skip `offset' bytes from the (now) first element, undo it on exit */
247 assert(iovcnt > 0);
248 iov[0].iov_base += offset;
249 iov[0].iov_len -= offset;
250
251 do {
252 len = writev(s->fd, iov, iovcnt);
253 } while (len == -1 && errno == EINTR);
254 if (len == -1) {
255 return -errno;
256 }
257
258 /* Undo the changes above */
259 iov[0].iov_base -= offset;
260 iov[0].iov_len += offset;
261
262 /* Prepare for the next iteration */
263 offset += len;
264 total += len;
265 size -= len;
266 }
267
268 return total;
269 }
270
271 static int unix_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
272 {
273 QEMUFileSocket *s = opaque;
274 ssize_t len;
275
276 for (;;) {
277 len = read(s->fd, buf, size);
278 if (len != -1) {
279 break;
280 }
281 if (errno == EAGAIN) {
282 yield_until_fd_readable(s->fd);
283 } else if (errno != EINTR) {
284 break;
285 }
286 }
287
288 if (len == -1) {
289 len = -errno;
290 }
291 return len;
292 }
293
294 static int unix_close(void *opaque)
295 {
296 QEMUFileSocket *s = opaque;
297 close(s->fd);
298 g_free(s);
299 return 0;
300 }
301
302 static const QEMUFileOps unix_read_ops = {
303 .get_fd = socket_get_fd,
304 .get_buffer = unix_get_buffer,
305 .close = unix_close
306 };
307
308 static const QEMUFileOps unix_write_ops = {
309 .get_fd = socket_get_fd,
310 .writev_buffer = unix_writev_buffer,
311 .close = unix_close
312 };
313
314 QEMUFile *qemu_fdopen(int fd, const char *mode)
315 {
316 QEMUFileSocket *s;
317
318 if (mode == NULL ||
319 (mode[0] != 'r' && mode[0] != 'w') ||
320 mode[1] != 'b' || mode[2] != 0) {
321 fprintf(stderr, "qemu_fdopen: Argument validity check failed\n");
322 return NULL;
323 }
324
325 s = g_malloc0(sizeof(QEMUFileSocket));
326 s->fd = fd;
327
328 if (mode[0] == 'r') {
329 s->file = qemu_fopen_ops(s, &unix_read_ops);
330 } else {
331 s->file = qemu_fopen_ops(s, &unix_write_ops);
332 }
333 return s->file;
334 }
335
336 static const QEMUFileOps socket_read_ops = {
337 .get_fd = socket_get_fd,
338 .get_buffer = socket_get_buffer,
339 .close = socket_close
340 };
341
342 static const QEMUFileOps socket_write_ops = {
343 .get_fd = socket_get_fd,
344 .writev_buffer = socket_writev_buffer,
345 .close = socket_close
346 };
347
348 bool qemu_file_mode_is_not_valid(const char *mode)
349 {
350 if (mode == NULL ||
351 (mode[0] != 'r' && mode[0] != 'w') ||
352 mode[1] != 'b' || mode[2] != 0) {
353 fprintf(stderr, "qemu_fopen: Argument validity check failed\n");
354 return true;
355 }
356
357 return false;
358 }
359
360 QEMUFile *qemu_fopen_socket(int fd, const char *mode)
361 {
362 QEMUFileSocket *s;
363
364 if (qemu_file_mode_is_not_valid(mode)) {
365 return NULL;
366 }
367
368 s = g_malloc0(sizeof(QEMUFileSocket));
369 s->fd = fd;
370 if (mode[0] == 'w') {
371 qemu_set_block(s->fd);
372 s->file = qemu_fopen_ops(s, &socket_write_ops);
373 } else {
374 s->file = qemu_fopen_ops(s, &socket_read_ops);
375 }
376 return s->file;
377 }
378
379 QEMUFile *qemu_fopen(const char *filename, const char *mode)
380 {
381 QEMUFileStdio *s;
382
383 if (qemu_file_mode_is_not_valid(mode)) {
384 return NULL;
385 }
386
387 s = g_malloc0(sizeof(QEMUFileStdio));
388
389 s->stdio_file = fopen(filename, mode);
390 if (!s->stdio_file) {
391 goto fail;
392 }
393
394 if (mode[0] == 'w') {
395 s->file = qemu_fopen_ops(s, &stdio_file_write_ops);
396 } else {
397 s->file = qemu_fopen_ops(s, &stdio_file_read_ops);
398 }
399 return s->file;
400 fail:
401 g_free(s);
402 return NULL;
403 }
404
405 QEMUFile *qemu_fopen_ops(void *opaque, const QEMUFileOps *ops)
406 {
407 QEMUFile *f;
408
409 f = g_malloc0(sizeof(QEMUFile));
410
411 f->opaque = opaque;
412 f->ops = ops;
413 return f;
414 }
415
416 /*
417 * Get last error for stream f
418 *
419 * Return negative error value if there has been an error on previous
420 * operations, return 0 if no error happened.
421 *
422 */
423 int qemu_file_get_error(QEMUFile *f)
424 {
425 return f->last_error;
426 }
427
428 void qemu_file_set_error(QEMUFile *f, int ret)
429 {
430 if (f->last_error == 0) {
431 f->last_error = ret;
432 }
433 }
434
435 static inline bool qemu_file_is_writable(QEMUFile *f)
436 {
437 return f->ops->writev_buffer || f->ops->put_buffer;
438 }
439
440 /**
441 * Flushes QEMUFile buffer
442 *
443 * If there is writev_buffer QEMUFileOps it uses it otherwise uses
444 * put_buffer ops.
445 */
446 void qemu_fflush(QEMUFile *f)
447 {
448 ssize_t ret = 0;
449
450 if (!qemu_file_is_writable(f)) {
451 return;
452 }
453
454 if (f->ops->writev_buffer) {
455 if (f->iovcnt > 0) {
456 ret = f->ops->writev_buffer(f->opaque, f->iov, f->iovcnt, f->pos);
457 }
458 } else {
459 if (f->buf_index > 0) {
460 ret = f->ops->put_buffer(f->opaque, f->buf, f->pos, f->buf_index);
461 }
462 }
463 if (ret >= 0) {
464 f->pos += ret;
465 }
466 f->buf_index = 0;
467 f->iovcnt = 0;
468 if (ret < 0) {
469 qemu_file_set_error(f, ret);
470 }
471 }
472
473 void ram_control_before_iterate(QEMUFile *f, uint64_t flags)
474 {
475 int ret = 0;
476
477 if (f->ops->before_ram_iterate) {
478 ret = f->ops->before_ram_iterate(f, f->opaque, flags);
479 if (ret < 0) {
480 qemu_file_set_error(f, ret);
481 }
482 }
483 }
484
485 void ram_control_after_iterate(QEMUFile *f, uint64_t flags)
486 {
487 int ret = 0;
488
489 if (f->ops->after_ram_iterate) {
490 ret = f->ops->after_ram_iterate(f, f->opaque, flags);
491 if (ret < 0) {
492 qemu_file_set_error(f, ret);
493 }
494 }
495 }
496
497 void ram_control_load_hook(QEMUFile *f, uint64_t flags)
498 {
499 int ret = -EINVAL;
500
501 if (f->ops->hook_ram_load) {
502 ret = f->ops->hook_ram_load(f, f->opaque, flags);
503 if (ret < 0) {
504 qemu_file_set_error(f, ret);
505 }
506 } else {
507 qemu_file_set_error(f, ret);
508 }
509 }
510
511 size_t ram_control_save_page(QEMUFile *f, ram_addr_t block_offset,
512 ram_addr_t offset, size_t size, int *bytes_sent)
513 {
514 if (f->ops->save_page) {
515 int ret = f->ops->save_page(f, f->opaque, block_offset,
516 offset, size, bytes_sent);
517
518 if (ret != RAM_SAVE_CONTROL_DELAYED) {
519 if (bytes_sent && *bytes_sent > 0) {
520 qemu_update_position(f, *bytes_sent);
521 } else if (ret < 0) {
522 qemu_file_set_error(f, ret);
523 }
524 }
525
526 return ret;
527 }
528
529 return RAM_SAVE_CONTROL_NOT_SUPP;
530 }
531
532 static void qemu_fill_buffer(QEMUFile *f)
533 {
534 int len;
535 int pending;
536
537 assert(!qemu_file_is_writable(f));
538
539 pending = f->buf_size - f->buf_index;
540 if (pending > 0) {
541 memmove(f->buf, f->buf + f->buf_index, pending);
542 }
543 f->buf_index = 0;
544 f->buf_size = pending;
545
546 len = f->ops->get_buffer(f->opaque, f->buf + pending, f->pos,
547 IO_BUF_SIZE - pending);
548 if (len > 0) {
549 f->buf_size += len;
550 f->pos += len;
551 } else if (len == 0) {
552 qemu_file_set_error(f, -EIO);
553 } else if (len != -EAGAIN) {
554 qemu_file_set_error(f, len);
555 }
556 }
557
558 int qemu_get_fd(QEMUFile *f)
559 {
560 if (f->ops->get_fd) {
561 return f->ops->get_fd(f->opaque);
562 }
563 return -1;
564 }
565
566 void qemu_update_position(QEMUFile *f, size_t size)
567 {
568 f->pos += size;
569 }
570
571 /** Closes the file
572 *
573 * Returns negative error value if any error happened on previous operations or
574 * while closing the file. Returns 0 or positive number on success.
575 *
576 * The meaning of return value on success depends on the specific backend
577 * being used.
578 */
579 int qemu_fclose(QEMUFile *f)
580 {
581 int ret;
582 qemu_fflush(f);
583 ret = qemu_file_get_error(f);
584
585 if (f->ops->close) {
586 int ret2 = f->ops->close(f->opaque);
587 if (ret >= 0) {
588 ret = ret2;
589 }
590 }
591 /* If any error was spotted before closing, we should report it
592 * instead of the close() return value.
593 */
594 if (f->last_error) {
595 ret = f->last_error;
596 }
597 g_free(f);
598 return ret;
599 }
600
601 static void add_to_iovec(QEMUFile *f, const uint8_t *buf, int size)
602 {
603 /* check for adjacent buffer and coalesce them */
604 if (f->iovcnt > 0 && buf == f->iov[f->iovcnt - 1].iov_base +
605 f->iov[f->iovcnt - 1].iov_len) {
606 f->iov[f->iovcnt - 1].iov_len += size;
607 } else {
608 f->iov[f->iovcnt].iov_base = (uint8_t *)buf;
609 f->iov[f->iovcnt++].iov_len = size;
610 }
611
612 if (f->iovcnt >= MAX_IOV_SIZE) {
613 qemu_fflush(f);
614 }
615 }
616
617 void qemu_put_buffer_async(QEMUFile *f, const uint8_t *buf, int size)
618 {
619 if (!f->ops->writev_buffer) {
620 qemu_put_buffer(f, buf, size);
621 return;
622 }
623
624 if (f->last_error) {
625 return;
626 }
627
628 f->bytes_xfer += size;
629 add_to_iovec(f, buf, size);
630 }
631
632 void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size)
633 {
634 int l;
635
636 if (f->last_error) {
637 return;
638 }
639
640 while (size > 0) {
641 l = IO_BUF_SIZE - f->buf_index;
642 if (l > size) {
643 l = size;
644 }
645 memcpy(f->buf + f->buf_index, buf, l);
646 f->bytes_xfer += l;
647 if (f->ops->writev_buffer) {
648 add_to_iovec(f, f->buf + f->buf_index, l);
649 }
650 f->buf_index += l;
651 if (f->buf_index == IO_BUF_SIZE) {
652 qemu_fflush(f);
653 }
654 if (qemu_file_get_error(f)) {
655 break;
656 }
657 buf += l;
658 size -= l;
659 }
660 }
661
662 void qemu_put_byte(QEMUFile *f, int v)
663 {
664 if (f->last_error) {
665 return;
666 }
667
668 f->buf[f->buf_index] = v;
669 f->bytes_xfer++;
670 if (f->ops->writev_buffer) {
671 add_to_iovec(f, f->buf + f->buf_index, 1);
672 }
673 f->buf_index++;
674 if (f->buf_index == IO_BUF_SIZE) {
675 qemu_fflush(f);
676 }
677 }
678
679 void qemu_file_skip(QEMUFile *f, int size)
680 {
681 if (f->buf_index + size <= f->buf_size) {
682 f->buf_index += size;
683 }
684 }
685
686 int qemu_peek_buffer(QEMUFile *f, uint8_t *buf, int size, size_t offset)
687 {
688 int pending;
689 int index;
690
691 assert(!qemu_file_is_writable(f));
692
693 index = f->buf_index + offset;
694 pending = f->buf_size - index;
695 if (pending < size) {
696 qemu_fill_buffer(f);
697 index = f->buf_index + offset;
698 pending = f->buf_size - index;
699 }
700
701 if (pending <= 0) {
702 return 0;
703 }
704 if (size > pending) {
705 size = pending;
706 }
707
708 memcpy(buf, f->buf + index, size);
709 return size;
710 }
711
712 int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size)
713 {
714 int pending = size;
715 int done = 0;
716
717 while (pending > 0) {
718 int res;
719
720 res = qemu_peek_buffer(f, buf, pending, 0);
721 if (res == 0) {
722 return done;
723 }
724 qemu_file_skip(f, res);
725 buf += res;
726 pending -= res;
727 done += res;
728 }
729 return done;
730 }
731
732 int qemu_peek_byte(QEMUFile *f, int offset)
733 {
734 int index = f->buf_index + offset;
735
736 assert(!qemu_file_is_writable(f));
737
738 if (index >= f->buf_size) {
739 qemu_fill_buffer(f);
740 index = f->buf_index + offset;
741 if (index >= f->buf_size) {
742 return 0;
743 }
744 }
745 return f->buf[index];
746 }
747
748 int qemu_get_byte(QEMUFile *f)
749 {
750 int result;
751
752 result = qemu_peek_byte(f, 0);
753 qemu_file_skip(f, 1);
754 return result;
755 }
756
757 int64_t qemu_ftell(QEMUFile *f)
758 {
759 qemu_fflush(f);
760 return f->pos;
761 }
762
763 int qemu_file_rate_limit(QEMUFile *f)
764 {
765 if (qemu_file_get_error(f)) {
766 return 1;
767 }
768 if (f->xfer_limit > 0 && f->bytes_xfer > f->xfer_limit) {
769 return 1;
770 }
771 return 0;
772 }
773
774 int64_t qemu_file_get_rate_limit(QEMUFile *f)
775 {
776 return f->xfer_limit;
777 }
778
779 void qemu_file_set_rate_limit(QEMUFile *f, int64_t limit)
780 {
781 f->xfer_limit = limit;
782 }
783
784 void qemu_file_reset_rate_limit(QEMUFile *f)
785 {
786 f->bytes_xfer = 0;
787 }
788
789 void qemu_put_be16(QEMUFile *f, unsigned int v)
790 {
791 qemu_put_byte(f, v >> 8);
792 qemu_put_byte(f, v);
793 }
794
795 void qemu_put_be32(QEMUFile *f, unsigned int v)
796 {
797 qemu_put_byte(f, v >> 24);
798 qemu_put_byte(f, v >> 16);
799 qemu_put_byte(f, v >> 8);
800 qemu_put_byte(f, v);
801 }
802
803 void qemu_put_be64(QEMUFile *f, uint64_t v)
804 {
805 qemu_put_be32(f, v >> 32);
806 qemu_put_be32(f, v);
807 }
808
809 unsigned int qemu_get_be16(QEMUFile *f)
810 {
811 unsigned int v;
812 v = qemu_get_byte(f) << 8;
813 v |= qemu_get_byte(f);
814 return v;
815 }
816
817 unsigned int qemu_get_be32(QEMUFile *f)
818 {
819 unsigned int v;
820 v = qemu_get_byte(f) << 24;
821 v |= qemu_get_byte(f) << 16;
822 v |= qemu_get_byte(f) << 8;
823 v |= qemu_get_byte(f);
824 return v;
825 }
826
827 uint64_t qemu_get_be64(QEMUFile *f)
828 {
829 uint64_t v;
830 v = (uint64_t)qemu_get_be32(f) << 32;
831 v |= qemu_get_be32(f);
832 return v;
833 }