vhdx: correctly propagate errors
[qemu/kevin.git] / qemu-file.c
blob9473b674ba517b2b78820ff269a0713809952cca
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"
8 #define IO_BUF_SIZE 32768
9 #define MAX_IOV_SIZE MIN(IOV_MAX, 64)
11 struct QEMUFile {
12 const QEMUFileOps *ops;
13 void *opaque;
15 int64_t bytes_xfer;
16 int64_t xfer_limit;
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];
24 struct iovec iov[MAX_IOV_SIZE];
25 unsigned int iovcnt;
27 int last_error;
30 typedef struct QEMUFileStdio {
31 FILE *stdio_file;
32 QEMUFile *file;
33 } QEMUFileStdio;
35 typedef struct QEMUFileSocket {
36 int fd;
37 QEMUFile *file;
38 } QEMUFileSocket;
40 static ssize_t socket_writev_buffer(void *opaque, struct iovec *iov, int iovcnt,
41 int64_t pos)
43 QEMUFileSocket *s = opaque;
44 ssize_t len;
45 ssize_t size = iov_size(iov, iovcnt);
47 len = iov_send(s->fd, iov, iovcnt, 0, size);
48 if (len < size) {
49 len = -socket_error();
51 return len;
54 static int socket_get_fd(void *opaque)
56 QEMUFileSocket *s = opaque;
58 return s->fd;
61 static int socket_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
63 QEMUFileSocket *s = opaque;
64 ssize_t len;
66 for (;;) {
67 len = qemu_recv(s->fd, buf, size, 0);
68 if (len != -1) {
69 break;
71 if (socket_error() == EAGAIN) {
72 yield_until_fd_readable(s->fd);
73 } else if (socket_error() != EINTR) {
74 break;
78 if (len == -1) {
79 len = -socket_error();
81 return len;
84 static int socket_close(void *opaque)
86 QEMUFileSocket *s = opaque;
87 closesocket(s->fd);
88 g_free(s);
89 return 0;
92 static int stdio_get_fd(void *opaque)
94 QEMUFileStdio *s = opaque;
96 return fileno(s->stdio_file);
99 static int stdio_put_buffer(void *opaque, const uint8_t *buf, int64_t pos,
100 int size)
102 QEMUFileStdio *s = opaque;
103 return fwrite(buf, 1, size, s->stdio_file);
106 static int stdio_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
108 QEMUFileStdio *s = opaque;
109 FILE *fp = s->stdio_file;
110 int bytes;
112 for (;;) {
113 clearerr(fp);
114 bytes = fread(buf, 1, size, fp);
115 if (bytes != 0 || !ferror(fp)) {
116 break;
118 if (errno == EAGAIN) {
119 yield_until_fd_readable(fileno(fp));
120 } else if (errno != EINTR) {
121 break;
124 return bytes;
127 static int stdio_pclose(void *opaque)
129 QEMUFileStdio *s = opaque;
130 int ret;
131 ret = pclose(s->stdio_file);
132 if (ret == -1) {
133 ret = -errno;
134 } else if (!WIFEXITED(ret) || WEXITSTATUS(ret) != 0) {
135 /* close succeeded, but non-zero exit code: */
136 ret = -EIO; /* fake errno value */
138 g_free(s);
139 return ret;
142 static int stdio_fclose(void *opaque)
144 QEMUFileStdio *s = opaque;
145 int ret = 0;
147 if (s->file->ops->put_buffer || s->file->ops->writev_buffer) {
148 int fd = fileno(s->stdio_file);
149 struct stat st;
151 ret = fstat(fd, &st);
152 if (ret == 0 && S_ISREG(st.st_mode)) {
154 * If the file handle is a regular file make sure the
155 * data is flushed to disk before signaling success.
157 ret = fsync(fd);
158 if (ret != 0) {
159 ret = -errno;
160 return ret;
164 if (fclose(s->stdio_file) == EOF) {
165 ret = -errno;
167 g_free(s);
168 return ret;
171 static const QEMUFileOps stdio_pipe_read_ops = {
172 .get_fd = stdio_get_fd,
173 .get_buffer = stdio_get_buffer,
174 .close = stdio_pclose
177 static const QEMUFileOps stdio_pipe_write_ops = {
178 .get_fd = stdio_get_fd,
179 .put_buffer = stdio_put_buffer,
180 .close = stdio_pclose
183 QEMUFile *qemu_popen_cmd(const char *command, const char *mode)
185 FILE *stdio_file;
186 QEMUFileStdio *s;
188 if (mode == NULL || (mode[0] != 'r' && mode[0] != 'w') || mode[1] != 0) {
189 fprintf(stderr, "qemu_popen: Argument validity check failed\n");
190 return NULL;
193 stdio_file = popen(command, mode);
194 if (stdio_file == NULL) {
195 return NULL;
198 s = g_malloc0(sizeof(QEMUFileStdio));
200 s->stdio_file = stdio_file;
202 if (mode[0] == 'r') {
203 s->file = qemu_fopen_ops(s, &stdio_pipe_read_ops);
204 } else {
205 s->file = qemu_fopen_ops(s, &stdio_pipe_write_ops);
207 return s->file;
210 static const QEMUFileOps stdio_file_read_ops = {
211 .get_fd = stdio_get_fd,
212 .get_buffer = stdio_get_buffer,
213 .close = stdio_fclose
216 static const QEMUFileOps stdio_file_write_ops = {
217 .get_fd = stdio_get_fd,
218 .put_buffer = stdio_put_buffer,
219 .close = stdio_fclose
222 static ssize_t unix_writev_buffer(void *opaque, struct iovec *iov, int iovcnt,
223 int64_t pos)
225 QEMUFileSocket *s = opaque;
226 ssize_t len, offset;
227 ssize_t size = iov_size(iov, iovcnt);
228 ssize_t total = 0;
230 assert(iovcnt > 0);
231 offset = 0;
232 while (size > 0) {
233 /* Find the next start position; skip all full-sized vector elements */
234 while (offset >= iov[0].iov_len) {
235 offset -= iov[0].iov_len;
236 iov++, iovcnt--;
239 /* skip `offset' bytes from the (now) first element, undo it on exit */
240 assert(iovcnt > 0);
241 iov[0].iov_base += offset;
242 iov[0].iov_len -= offset;
244 do {
245 len = writev(s->fd, iov, iovcnt);
246 } while (len == -1 && errno == EINTR);
247 if (len == -1) {
248 return -errno;
251 /* Undo the changes above */
252 iov[0].iov_base -= offset;
253 iov[0].iov_len += offset;
255 /* Prepare for the next iteration */
256 offset += len;
257 total += len;
258 size -= len;
261 return total;
264 static int unix_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
266 QEMUFileSocket *s = opaque;
267 ssize_t len;
269 for (;;) {
270 len = read(s->fd, buf, size);
271 if (len != -1) {
272 break;
274 if (errno == EAGAIN) {
275 yield_until_fd_readable(s->fd);
276 } else if (errno != EINTR) {
277 break;
281 if (len == -1) {
282 len = -errno;
284 return len;
287 static int unix_close(void *opaque)
289 QEMUFileSocket *s = opaque;
290 close(s->fd);
291 g_free(s);
292 return 0;
295 static const QEMUFileOps unix_read_ops = {
296 .get_fd = socket_get_fd,
297 .get_buffer = unix_get_buffer,
298 .close = unix_close
301 static const QEMUFileOps unix_write_ops = {
302 .get_fd = socket_get_fd,
303 .writev_buffer = unix_writev_buffer,
304 .close = unix_close
307 QEMUFile *qemu_fdopen(int fd, const char *mode)
309 QEMUFileSocket *s;
311 if (mode == NULL ||
312 (mode[0] != 'r' && mode[0] != 'w') ||
313 mode[1] != 'b' || mode[2] != 0) {
314 fprintf(stderr, "qemu_fdopen: Argument validity check failed\n");
315 return NULL;
318 s = g_malloc0(sizeof(QEMUFileSocket));
319 s->fd = fd;
321 if (mode[0] == 'r') {
322 s->file = qemu_fopen_ops(s, &unix_read_ops);
323 } else {
324 s->file = qemu_fopen_ops(s, &unix_write_ops);
326 return s->file;
329 static const QEMUFileOps socket_read_ops = {
330 .get_fd = socket_get_fd,
331 .get_buffer = socket_get_buffer,
332 .close = socket_close
335 static const QEMUFileOps socket_write_ops = {
336 .get_fd = socket_get_fd,
337 .writev_buffer = socket_writev_buffer,
338 .close = socket_close
341 bool qemu_file_mode_is_not_valid(const char *mode)
343 if (mode == NULL ||
344 (mode[0] != 'r' && mode[0] != 'w') ||
345 mode[1] != 'b' || mode[2] != 0) {
346 fprintf(stderr, "qemu_fopen: Argument validity check failed\n");
347 return true;
350 return false;
353 QEMUFile *qemu_fopen_socket(int fd, const char *mode)
355 QEMUFileSocket *s;
357 if (qemu_file_mode_is_not_valid(mode)) {
358 return NULL;
361 s = g_malloc0(sizeof(QEMUFileSocket));
362 s->fd = fd;
363 if (mode[0] == 'w') {
364 qemu_set_block(s->fd);
365 s->file = qemu_fopen_ops(s, &socket_write_ops);
366 } else {
367 s->file = qemu_fopen_ops(s, &socket_read_ops);
369 return s->file;
372 QEMUFile *qemu_fopen(const char *filename, const char *mode)
374 QEMUFileStdio *s;
376 if (qemu_file_mode_is_not_valid(mode)) {
377 return NULL;
380 s = g_malloc0(sizeof(QEMUFileStdio));
382 s->stdio_file = fopen(filename, mode);
383 if (!s->stdio_file) {
384 goto fail;
387 if (mode[0] == 'w') {
388 s->file = qemu_fopen_ops(s, &stdio_file_write_ops);
389 } else {
390 s->file = qemu_fopen_ops(s, &stdio_file_read_ops);
392 return s->file;
393 fail:
394 g_free(s);
395 return NULL;
398 QEMUFile *qemu_fopen_ops(void *opaque, const QEMUFileOps *ops)
400 QEMUFile *f;
402 f = g_malloc0(sizeof(QEMUFile));
404 f->opaque = opaque;
405 f->ops = ops;
406 return f;
410 * Get last error for stream f
412 * Return negative error value if there has been an error on previous
413 * operations, return 0 if no error happened.
416 int qemu_file_get_error(QEMUFile *f)
418 return f->last_error;
421 void qemu_file_set_error(QEMUFile *f, int ret)
423 if (f->last_error == 0) {
424 f->last_error = ret;
428 static inline bool qemu_file_is_writable(QEMUFile *f)
430 return f->ops->writev_buffer || f->ops->put_buffer;
434 * Flushes QEMUFile buffer
436 * If there is writev_buffer QEMUFileOps it uses it otherwise uses
437 * put_buffer ops.
439 void qemu_fflush(QEMUFile *f)
441 ssize_t ret = 0;
443 if (!qemu_file_is_writable(f)) {
444 return;
447 if (f->ops->writev_buffer) {
448 if (f->iovcnt > 0) {
449 ret = f->ops->writev_buffer(f->opaque, f->iov, f->iovcnt, f->pos);
451 } else {
452 if (f->buf_index > 0) {
453 ret = f->ops->put_buffer(f->opaque, f->buf, f->pos, f->buf_index);
456 if (ret >= 0) {
457 f->pos += ret;
459 f->buf_index = 0;
460 f->iovcnt = 0;
461 if (ret < 0) {
462 qemu_file_set_error(f, ret);
466 void ram_control_before_iterate(QEMUFile *f, uint64_t flags)
468 int ret = 0;
470 if (f->ops->before_ram_iterate) {
471 ret = f->ops->before_ram_iterate(f, f->opaque, flags);
472 if (ret < 0) {
473 qemu_file_set_error(f, ret);
478 void ram_control_after_iterate(QEMUFile *f, uint64_t flags)
480 int ret = 0;
482 if (f->ops->after_ram_iterate) {
483 ret = f->ops->after_ram_iterate(f, f->opaque, flags);
484 if (ret < 0) {
485 qemu_file_set_error(f, ret);
490 void ram_control_load_hook(QEMUFile *f, uint64_t flags)
492 int ret = -EINVAL;
494 if (f->ops->hook_ram_load) {
495 ret = f->ops->hook_ram_load(f, f->opaque, flags);
496 if (ret < 0) {
497 qemu_file_set_error(f, ret);
499 } else {
500 qemu_file_set_error(f, ret);
504 size_t ram_control_save_page(QEMUFile *f, ram_addr_t block_offset,
505 ram_addr_t offset, size_t size, int *bytes_sent)
507 if (f->ops->save_page) {
508 int ret = f->ops->save_page(f, f->opaque, block_offset,
509 offset, size, bytes_sent);
511 if (ret != RAM_SAVE_CONTROL_DELAYED) {
512 if (bytes_sent && *bytes_sent > 0) {
513 qemu_update_position(f, *bytes_sent);
514 } else if (ret < 0) {
515 qemu_file_set_error(f, ret);
519 return ret;
522 return RAM_SAVE_CONTROL_NOT_SUPP;
525 static void qemu_fill_buffer(QEMUFile *f)
527 int len;
528 int pending;
530 assert(!qemu_file_is_writable(f));
532 pending = f->buf_size - f->buf_index;
533 if (pending > 0) {
534 memmove(f->buf, f->buf + f->buf_index, pending);
536 f->buf_index = 0;
537 f->buf_size = pending;
539 len = f->ops->get_buffer(f->opaque, f->buf + pending, f->pos,
540 IO_BUF_SIZE - pending);
541 if (len > 0) {
542 f->buf_size += len;
543 f->pos += len;
544 } else if (len == 0) {
545 qemu_file_set_error(f, -EIO);
546 } else if (len != -EAGAIN) {
547 qemu_file_set_error(f, len);
551 int qemu_get_fd(QEMUFile *f)
553 if (f->ops->get_fd) {
554 return f->ops->get_fd(f->opaque);
556 return -1;
559 void qemu_update_position(QEMUFile *f, size_t size)
561 f->pos += size;
564 /** Closes the file
566 * Returns negative error value if any error happened on previous operations or
567 * while closing the file. Returns 0 or positive number on success.
569 * The meaning of return value on success depends on the specific backend
570 * being used.
572 int qemu_fclose(QEMUFile *f)
574 int ret;
575 qemu_fflush(f);
576 ret = qemu_file_get_error(f);
578 if (f->ops->close) {
579 int ret2 = f->ops->close(f->opaque);
580 if (ret >= 0) {
581 ret = ret2;
584 /* If any error was spotted before closing, we should report it
585 * instead of the close() return value.
587 if (f->last_error) {
588 ret = f->last_error;
590 g_free(f);
591 return ret;
594 static void add_to_iovec(QEMUFile *f, const uint8_t *buf, int size)
596 /* check for adjacent buffer and coalesce them */
597 if (f->iovcnt > 0 && buf == f->iov[f->iovcnt - 1].iov_base +
598 f->iov[f->iovcnt - 1].iov_len) {
599 f->iov[f->iovcnt - 1].iov_len += size;
600 } else {
601 f->iov[f->iovcnt].iov_base = (uint8_t *)buf;
602 f->iov[f->iovcnt++].iov_len = size;
605 if (f->iovcnt >= MAX_IOV_SIZE) {
606 qemu_fflush(f);
610 void qemu_put_buffer_async(QEMUFile *f, const uint8_t *buf, int size)
612 if (!f->ops->writev_buffer) {
613 qemu_put_buffer(f, buf, size);
614 return;
617 if (f->last_error) {
618 return;
621 f->bytes_xfer += size;
622 add_to_iovec(f, buf, size);
625 void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size)
627 int l;
629 if (f->last_error) {
630 return;
633 while (size > 0) {
634 l = IO_BUF_SIZE - f->buf_index;
635 if (l > size) {
636 l = size;
638 memcpy(f->buf + f->buf_index, buf, l);
639 f->bytes_xfer += l;
640 if (f->ops->writev_buffer) {
641 add_to_iovec(f, f->buf + f->buf_index, l);
643 f->buf_index += l;
644 if (f->buf_index == IO_BUF_SIZE) {
645 qemu_fflush(f);
647 if (qemu_file_get_error(f)) {
648 break;
650 buf += l;
651 size -= l;
655 void qemu_put_byte(QEMUFile *f, int v)
657 if (f->last_error) {
658 return;
661 f->buf[f->buf_index] = v;
662 f->bytes_xfer++;
663 if (f->ops->writev_buffer) {
664 add_to_iovec(f, f->buf + f->buf_index, 1);
666 f->buf_index++;
667 if (f->buf_index == IO_BUF_SIZE) {
668 qemu_fflush(f);
672 void qemu_file_skip(QEMUFile *f, int size)
674 if (f->buf_index + size <= f->buf_size) {
675 f->buf_index += size;
679 int qemu_peek_buffer(QEMUFile *f, uint8_t *buf, int size, size_t offset)
681 int pending;
682 int index;
684 assert(!qemu_file_is_writable(f));
686 index = f->buf_index + offset;
687 pending = f->buf_size - index;
688 if (pending < size) {
689 qemu_fill_buffer(f);
690 index = f->buf_index + offset;
691 pending = f->buf_size - index;
694 if (pending <= 0) {
695 return 0;
697 if (size > pending) {
698 size = pending;
701 memcpy(buf, f->buf + index, size);
702 return size;
705 int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size)
707 int pending = size;
708 int done = 0;
710 while (pending > 0) {
711 int res;
713 res = qemu_peek_buffer(f, buf, pending, 0);
714 if (res == 0) {
715 return done;
717 qemu_file_skip(f, res);
718 buf += res;
719 pending -= res;
720 done += res;
722 return done;
725 int qemu_peek_byte(QEMUFile *f, int offset)
727 int index = f->buf_index + offset;
729 assert(!qemu_file_is_writable(f));
731 if (index >= f->buf_size) {
732 qemu_fill_buffer(f);
733 index = f->buf_index + offset;
734 if (index >= f->buf_size) {
735 return 0;
738 return f->buf[index];
741 int qemu_get_byte(QEMUFile *f)
743 int result;
745 result = qemu_peek_byte(f, 0);
746 qemu_file_skip(f, 1);
747 return result;
750 int64_t qemu_ftell(QEMUFile *f)
752 qemu_fflush(f);
753 return f->pos;
756 int qemu_file_rate_limit(QEMUFile *f)
758 if (qemu_file_get_error(f)) {
759 return 1;
761 if (f->xfer_limit > 0 && f->bytes_xfer > f->xfer_limit) {
762 return 1;
764 return 0;
767 int64_t qemu_file_get_rate_limit(QEMUFile *f)
769 return f->xfer_limit;
772 void qemu_file_set_rate_limit(QEMUFile *f, int64_t limit)
774 f->xfer_limit = limit;
777 void qemu_file_reset_rate_limit(QEMUFile *f)
779 f->bytes_xfer = 0;
782 void qemu_put_be16(QEMUFile *f, unsigned int v)
784 qemu_put_byte(f, v >> 8);
785 qemu_put_byte(f, v);
788 void qemu_put_be32(QEMUFile *f, unsigned int v)
790 qemu_put_byte(f, v >> 24);
791 qemu_put_byte(f, v >> 16);
792 qemu_put_byte(f, v >> 8);
793 qemu_put_byte(f, v);
796 void qemu_put_be64(QEMUFile *f, uint64_t v)
798 qemu_put_be32(f, v >> 32);
799 qemu_put_be32(f, v);
802 unsigned int qemu_get_be16(QEMUFile *f)
804 unsigned int v;
805 v = qemu_get_byte(f) << 8;
806 v |= qemu_get_byte(f);
807 return v;
810 unsigned int qemu_get_be32(QEMUFile *f)
812 unsigned int v;
813 v = qemu_get_byte(f) << 24;
814 v |= qemu_get_byte(f) << 16;
815 v |= qemu_get_byte(f) << 8;
816 v |= qemu_get_byte(f);
817 return v;
820 uint64_t qemu_get_be64(QEMUFile *f)
822 uint64_t v;
823 v = (uint64_t)qemu_get_be32(f) << 32;
824 v |= qemu_get_be32(f);
825 return v;