target-ppc: Fix htab_mask calculation
[qemu/kevin.git] / qemu-file.c
blobf074af15c31bde289076713de9b84dd5ae202713
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 int res;
105 res = fwrite(buf, 1, size, s->stdio_file);
107 if (res != size) {
108 return -EIO; /* fake errno value */
110 return res;
113 static int stdio_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
115 QEMUFileStdio *s = opaque;
116 FILE *fp = s->stdio_file;
117 int bytes;
119 for (;;) {
120 clearerr(fp);
121 bytes = fread(buf, 1, size, fp);
122 if (bytes != 0 || !ferror(fp)) {
123 break;
125 if (errno == EAGAIN) {
126 yield_until_fd_readable(fileno(fp));
127 } else if (errno != EINTR) {
128 break;
131 return bytes;
134 static int stdio_pclose(void *opaque)
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 */
145 g_free(s);
146 return ret;
149 static int stdio_fclose(void *opaque)
151 QEMUFileStdio *s = opaque;
152 int ret = 0;
154 if (s->file->ops->put_buffer || s->file->ops->writev_buffer) {
155 int fd = fileno(s->stdio_file);
156 struct stat st;
158 ret = fstat(fd, &st);
159 if (ret == 0 && S_ISREG(st.st_mode)) {
161 * If the file handle is a regular file make sure the
162 * data is flushed to disk before signaling success.
164 ret = fsync(fd);
165 if (ret != 0) {
166 ret = -errno;
167 return ret;
171 if (fclose(s->stdio_file) == EOF) {
172 ret = -errno;
174 g_free(s);
175 return ret;
178 static const QEMUFileOps stdio_pipe_read_ops = {
179 .get_fd = stdio_get_fd,
180 .get_buffer = stdio_get_buffer,
181 .close = stdio_pclose
184 static const QEMUFileOps stdio_pipe_write_ops = {
185 .get_fd = stdio_get_fd,
186 .put_buffer = stdio_put_buffer,
187 .close = stdio_pclose
190 QEMUFile *qemu_popen_cmd(const char *command, const char *mode)
192 FILE *stdio_file;
193 QEMUFileStdio *s;
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;
200 stdio_file = popen(command, mode);
201 if (stdio_file == NULL) {
202 return NULL;
205 s = g_malloc0(sizeof(QEMUFileStdio));
207 s->stdio_file = stdio_file;
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);
214 return s->file;
217 static const QEMUFileOps stdio_file_read_ops = {
218 .get_fd = stdio_get_fd,
219 .get_buffer = stdio_get_buffer,
220 .close = stdio_fclose
223 static const QEMUFileOps stdio_file_write_ops = {
224 .get_fd = stdio_get_fd,
225 .put_buffer = stdio_put_buffer,
226 .close = stdio_fclose
229 static ssize_t unix_writev_buffer(void *opaque, struct iovec *iov, int iovcnt,
230 int64_t pos)
232 QEMUFileSocket *s = opaque;
233 ssize_t len, offset;
234 ssize_t size = iov_size(iov, iovcnt);
235 ssize_t total = 0;
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--;
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;
251 do {
252 len = writev(s->fd, iov, iovcnt);
253 } while (len == -1 && errno == EINTR);
254 if (len == -1) {
255 return -errno;
258 /* Undo the changes above */
259 iov[0].iov_base -= offset;
260 iov[0].iov_len += offset;
262 /* Prepare for the next iteration */
263 offset += len;
264 total += len;
265 size -= len;
268 return total;
271 static int unix_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
273 QEMUFileSocket *s = opaque;
274 ssize_t len;
276 for (;;) {
277 len = read(s->fd, buf, size);
278 if (len != -1) {
279 break;
281 if (errno == EAGAIN) {
282 yield_until_fd_readable(s->fd);
283 } else if (errno != EINTR) {
284 break;
288 if (len == -1) {
289 len = -errno;
291 return len;
294 static int unix_close(void *opaque)
296 QEMUFileSocket *s = opaque;
297 close(s->fd);
298 g_free(s);
299 return 0;
302 static const QEMUFileOps unix_read_ops = {
303 .get_fd = socket_get_fd,
304 .get_buffer = unix_get_buffer,
305 .close = unix_close
308 static const QEMUFileOps unix_write_ops = {
309 .get_fd = socket_get_fd,
310 .writev_buffer = unix_writev_buffer,
311 .close = unix_close
314 QEMUFile *qemu_fdopen(int fd, const char *mode)
316 QEMUFileSocket *s;
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;
325 s = g_malloc0(sizeof(QEMUFileSocket));
326 s->fd = fd;
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);
333 return s->file;
336 static const QEMUFileOps socket_read_ops = {
337 .get_fd = socket_get_fd,
338 .get_buffer = socket_get_buffer,
339 .close = socket_close
342 static const QEMUFileOps socket_write_ops = {
343 .get_fd = socket_get_fd,
344 .writev_buffer = socket_writev_buffer,
345 .close = socket_close
348 bool qemu_file_mode_is_not_valid(const char *mode)
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;
357 return false;
360 QEMUFile *qemu_fopen_socket(int fd, const char *mode)
362 QEMUFileSocket *s;
364 if (qemu_file_mode_is_not_valid(mode)) {
365 return NULL;
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);
376 return s->file;
379 QEMUFile *qemu_fopen(const char *filename, const char *mode)
381 QEMUFileStdio *s;
383 if (qemu_file_mode_is_not_valid(mode)) {
384 return NULL;
387 s = g_malloc0(sizeof(QEMUFileStdio));
389 s->stdio_file = fopen(filename, mode);
390 if (!s->stdio_file) {
391 goto fail;
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);
399 return s->file;
400 fail:
401 g_free(s);
402 return NULL;
405 QEMUFile *qemu_fopen_ops(void *opaque, const QEMUFileOps *ops)
407 QEMUFile *f;
409 f = g_malloc0(sizeof(QEMUFile));
411 f->opaque = opaque;
412 f->ops = ops;
413 return f;
417 * Get last error for stream f
419 * Return negative error value if there has been an error on previous
420 * operations, return 0 if no error happened.
423 int qemu_file_get_error(QEMUFile *f)
425 return f->last_error;
428 void qemu_file_set_error(QEMUFile *f, int ret)
430 if (f->last_error == 0) {
431 f->last_error = ret;
435 static inline bool qemu_file_is_writable(QEMUFile *f)
437 return f->ops->writev_buffer || f->ops->put_buffer;
441 * Flushes QEMUFile buffer
443 * If there is writev_buffer QEMUFileOps it uses it otherwise uses
444 * put_buffer ops.
446 void qemu_fflush(QEMUFile *f)
448 ssize_t ret = 0;
450 if (!qemu_file_is_writable(f)) {
451 return;
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);
458 } else {
459 if (f->buf_index > 0) {
460 ret = f->ops->put_buffer(f->opaque, f->buf, f->pos, f->buf_index);
463 if (ret >= 0) {
464 f->pos += ret;
466 f->buf_index = 0;
467 f->iovcnt = 0;
468 if (ret < 0) {
469 qemu_file_set_error(f, ret);
473 void ram_control_before_iterate(QEMUFile *f, uint64_t flags)
475 int ret = 0;
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);
485 void ram_control_after_iterate(QEMUFile *f, uint64_t flags)
487 int ret = 0;
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);
497 void ram_control_load_hook(QEMUFile *f, uint64_t flags)
499 int ret = -EINVAL;
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);
506 } else {
507 qemu_file_set_error(f, ret);
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)
514 if (f->ops->save_page) {
515 int ret = f->ops->save_page(f, f->opaque, block_offset,
516 offset, size, bytes_sent);
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);
526 return ret;
529 return RAM_SAVE_CONTROL_NOT_SUPP;
532 static void qemu_fill_buffer(QEMUFile *f)
534 int len;
535 int pending;
537 assert(!qemu_file_is_writable(f));
539 pending = f->buf_size - f->buf_index;
540 if (pending > 0) {
541 memmove(f->buf, f->buf + f->buf_index, pending);
543 f->buf_index = 0;
544 f->buf_size = pending;
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);
558 int qemu_get_fd(QEMUFile *f)
560 if (f->ops->get_fd) {
561 return f->ops->get_fd(f->opaque);
563 return -1;
566 void qemu_update_position(QEMUFile *f, size_t size)
568 f->pos += size;
571 /** Closes the file
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.
576 * The meaning of return value on success depends on the specific backend
577 * being used.
579 int qemu_fclose(QEMUFile *f)
581 int ret;
582 qemu_fflush(f);
583 ret = qemu_file_get_error(f);
585 if (f->ops->close) {
586 int ret2 = f->ops->close(f->opaque);
587 if (ret >= 0) {
588 ret = ret2;
591 /* If any error was spotted before closing, we should report it
592 * instead of the close() return value.
594 if (f->last_error) {
595 ret = f->last_error;
597 g_free(f);
598 return ret;
601 static void add_to_iovec(QEMUFile *f, const uint8_t *buf, int size)
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;
612 if (f->iovcnt >= MAX_IOV_SIZE) {
613 qemu_fflush(f);
617 void qemu_put_buffer_async(QEMUFile *f, const uint8_t *buf, int size)
619 if (!f->ops->writev_buffer) {
620 qemu_put_buffer(f, buf, size);
621 return;
624 if (f->last_error) {
625 return;
628 f->bytes_xfer += size;
629 add_to_iovec(f, buf, size);
632 void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size)
634 int l;
636 if (f->last_error) {
637 return;
640 while (size > 0) {
641 l = IO_BUF_SIZE - f->buf_index;
642 if (l > size) {
643 l = size;
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);
650 f->buf_index += l;
651 if (f->buf_index == IO_BUF_SIZE) {
652 qemu_fflush(f);
654 if (qemu_file_get_error(f)) {
655 break;
657 buf += l;
658 size -= l;
662 void qemu_put_byte(QEMUFile *f, int v)
664 if (f->last_error) {
665 return;
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);
673 f->buf_index++;
674 if (f->buf_index == IO_BUF_SIZE) {
675 qemu_fflush(f);
679 void qemu_file_skip(QEMUFile *f, int size)
681 if (f->buf_index + size <= f->buf_size) {
682 f->buf_index += size;
686 int qemu_peek_buffer(QEMUFile *f, uint8_t *buf, int size, size_t offset)
688 int pending;
689 int index;
691 assert(!qemu_file_is_writable(f));
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;
701 if (pending <= 0) {
702 return 0;
704 if (size > pending) {
705 size = pending;
708 memcpy(buf, f->buf + index, size);
709 return size;
712 int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size)
714 int pending = size;
715 int done = 0;
717 while (pending > 0) {
718 int res;
720 res = qemu_peek_buffer(f, buf, pending, 0);
721 if (res == 0) {
722 return done;
724 qemu_file_skip(f, res);
725 buf += res;
726 pending -= res;
727 done += res;
729 return done;
732 int qemu_peek_byte(QEMUFile *f, int offset)
734 int index = f->buf_index + offset;
736 assert(!qemu_file_is_writable(f));
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;
745 return f->buf[index];
748 int qemu_get_byte(QEMUFile *f)
750 int result;
752 result = qemu_peek_byte(f, 0);
753 qemu_file_skip(f, 1);
754 return result;
757 int64_t qemu_ftell(QEMUFile *f)
759 qemu_fflush(f);
760 return f->pos;
763 int qemu_file_rate_limit(QEMUFile *f)
765 if (qemu_file_get_error(f)) {
766 return 1;
768 if (f->xfer_limit > 0 && f->bytes_xfer > f->xfer_limit) {
769 return 1;
771 return 0;
774 int64_t qemu_file_get_rate_limit(QEMUFile *f)
776 return f->xfer_limit;
779 void qemu_file_set_rate_limit(QEMUFile *f, int64_t limit)
781 f->xfer_limit = limit;
784 void qemu_file_reset_rate_limit(QEMUFile *f)
786 f->bytes_xfer = 0;
789 void qemu_put_be16(QEMUFile *f, unsigned int v)
791 qemu_put_byte(f, v >> 8);
792 qemu_put_byte(f, v);
795 void qemu_put_be32(QEMUFile *f, unsigned int v)
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);
803 void qemu_put_be64(QEMUFile *f, uint64_t v)
805 qemu_put_be32(f, v >> 32);
806 qemu_put_be32(f, v);
809 unsigned int qemu_get_be16(QEMUFile *f)
811 unsigned int v;
812 v = qemu_get_byte(f) << 8;
813 v |= qemu_get_byte(f);
814 return v;
817 unsigned int qemu_get_be32(QEMUFile *f)
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;
827 uint64_t qemu_get_be64(QEMUFile *f)
829 uint64_t v;
830 v = (uint64_t)qemu_get_be32(f) << 32;
831 v |= qemu_get_be32(f);
832 return v;