]> rtime.felk.cvut.cz Git - lisovros/qemu_apohw.git/blob - qemu-file.c
Merge remote-tracking branch 'remotes/mcayland/qemu-openbios' into staging
[lisovros/qemu_apohw.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     return fwrite(buf, 1, size, s->stdio_file);
104 }
105
106 static int stdio_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
107 {
108     QEMUFileStdio *s = opaque;
109     FILE *fp = s->stdio_file;
110     int bytes;
111
112     for (;;) {
113         clearerr(fp);
114         bytes = fread(buf, 1, size, fp);
115         if (bytes != 0 || !ferror(fp)) {
116             break;
117         }
118         if (errno == EAGAIN) {
119             yield_until_fd_readable(fileno(fp));
120         } else if (errno != EINTR) {
121             break;
122         }
123     }
124     return bytes;
125 }
126
127 static int stdio_pclose(void *opaque)
128 {
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 */
137     }
138     g_free(s);
139     return ret;
140 }
141
142 static int stdio_fclose(void *opaque)
143 {
144     QEMUFileStdio *s = opaque;
145     int ret = 0;
146
147     if (s->file->ops->put_buffer || s->file->ops->writev_buffer) {
148         int fd = fileno(s->stdio_file);
149         struct stat st;
150
151         ret = fstat(fd, &st);
152         if (ret == 0 && S_ISREG(st.st_mode)) {
153             /*
154              * If the file handle is a regular file make sure the
155              * data is flushed to disk before signaling success.
156              */
157             ret = fsync(fd);
158             if (ret != 0) {
159                 ret = -errno;
160                 return ret;
161             }
162         }
163     }
164     if (fclose(s->stdio_file) == EOF) {
165         ret = -errno;
166     }
167     g_free(s);
168     return ret;
169 }
170
171 static const QEMUFileOps stdio_pipe_read_ops = {
172     .get_fd =     stdio_get_fd,
173     .get_buffer = stdio_get_buffer,
174     .close =      stdio_pclose
175 };
176
177 static const QEMUFileOps stdio_pipe_write_ops = {
178     .get_fd =     stdio_get_fd,
179     .put_buffer = stdio_put_buffer,
180     .close =      stdio_pclose
181 };
182
183 QEMUFile *qemu_popen_cmd(const char *command, const char *mode)
184 {
185     FILE *stdio_file;
186     QEMUFileStdio *s;
187
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;
191     }
192
193     stdio_file = popen(command, mode);
194     if (stdio_file == NULL) {
195         return NULL;
196     }
197
198     s = g_malloc0(sizeof(QEMUFileStdio));
199
200     s->stdio_file = stdio_file;
201
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);
206     }
207     return s->file;
208 }
209
210 static const QEMUFileOps stdio_file_read_ops = {
211     .get_fd =     stdio_get_fd,
212     .get_buffer = stdio_get_buffer,
213     .close =      stdio_fclose
214 };
215
216 static const QEMUFileOps stdio_file_write_ops = {
217     .get_fd =     stdio_get_fd,
218     .put_buffer = stdio_put_buffer,
219     .close =      stdio_fclose
220 };
221
222 static ssize_t unix_writev_buffer(void *opaque, struct iovec *iov, int iovcnt,
223                                   int64_t pos)
224 {
225     QEMUFileSocket *s = opaque;
226     ssize_t len, offset;
227     ssize_t size = iov_size(iov, iovcnt);
228     ssize_t total = 0;
229
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--;
237         }
238
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;
243
244         do {
245             len = writev(s->fd, iov, iovcnt);
246         } while (len == -1 && errno == EINTR);
247         if (len == -1) {
248             return -errno;
249         }
250
251         /* Undo the changes above */
252         iov[0].iov_base -= offset;
253         iov[0].iov_len += offset;
254
255         /* Prepare for the next iteration */
256         offset += len;
257         total += len;
258         size -= len;
259     }
260
261     return total;
262 }
263
264 static int unix_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
265 {
266     QEMUFileSocket *s = opaque;
267     ssize_t len;
268
269     for (;;) {
270         len = read(s->fd, buf, size);
271         if (len != -1) {
272             break;
273         }
274         if (errno == EAGAIN) {
275             yield_until_fd_readable(s->fd);
276         } else if (errno != EINTR) {
277             break;
278         }
279     }
280
281     if (len == -1) {
282         len = -errno;
283     }
284     return len;
285 }
286
287 static int unix_close(void *opaque)
288 {
289     QEMUFileSocket *s = opaque;
290     close(s->fd);
291     g_free(s);
292     return 0;
293 }
294
295 static const QEMUFileOps unix_read_ops = {
296     .get_fd =     socket_get_fd,
297     .get_buffer = unix_get_buffer,
298     .close =      unix_close
299 };
300
301 static const QEMUFileOps unix_write_ops = {
302     .get_fd =     socket_get_fd,
303     .writev_buffer = unix_writev_buffer,
304     .close =      unix_close
305 };
306
307 QEMUFile *qemu_fdopen(int fd, const char *mode)
308 {
309     QEMUFileSocket *s;
310
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;
316     }
317
318     s = g_malloc0(sizeof(QEMUFileSocket));
319     s->fd = fd;
320
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);
325     }
326     return s->file;
327 }
328
329 static const QEMUFileOps socket_read_ops = {
330     .get_fd =     socket_get_fd,
331     .get_buffer = socket_get_buffer,
332     .close =      socket_close
333 };
334
335 static const QEMUFileOps socket_write_ops = {
336     .get_fd =     socket_get_fd,
337     .writev_buffer = socket_writev_buffer,
338     .close =      socket_close
339 };
340
341 bool qemu_file_mode_is_not_valid(const char *mode)
342 {
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;
348     }
349
350     return false;
351 }
352
353 QEMUFile *qemu_fopen_socket(int fd, const char *mode)
354 {
355     QEMUFileSocket *s;
356
357     if (qemu_file_mode_is_not_valid(mode)) {
358         return NULL;
359     }
360
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);
368     }
369     return s->file;
370 }
371
372 QEMUFile *qemu_fopen(const char *filename, const char *mode)
373 {
374     QEMUFileStdio *s;
375
376     if (qemu_file_mode_is_not_valid(mode)) {
377         return NULL;
378     }
379
380     s = g_malloc0(sizeof(QEMUFileStdio));
381
382     s->stdio_file = fopen(filename, mode);
383     if (!s->stdio_file) {
384         goto fail;
385     }
386
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);
391     }
392     return s->file;
393 fail:
394     g_free(s);
395     return NULL;
396 }
397
398 QEMUFile *qemu_fopen_ops(void *opaque, const QEMUFileOps *ops)
399 {
400     QEMUFile *f;
401
402     f = g_malloc0(sizeof(QEMUFile));
403
404     f->opaque = opaque;
405     f->ops = ops;
406     return f;
407 }
408
409 /*
410  * Get last error for stream f
411  *
412  * Return negative error value if there has been an error on previous
413  * operations, return 0 if no error happened.
414  *
415  */
416 int qemu_file_get_error(QEMUFile *f)
417 {
418     return f->last_error;
419 }
420
421 void qemu_file_set_error(QEMUFile *f, int ret)
422 {
423     if (f->last_error == 0) {
424         f->last_error = ret;
425     }
426 }
427
428 static inline bool qemu_file_is_writable(QEMUFile *f)
429 {
430     return f->ops->writev_buffer || f->ops->put_buffer;
431 }
432
433 /**
434  * Flushes QEMUFile buffer
435  *
436  * If there is writev_buffer QEMUFileOps it uses it otherwise uses
437  * put_buffer ops.
438  */
439 void qemu_fflush(QEMUFile *f)
440 {
441     ssize_t ret = 0;
442
443     if (!qemu_file_is_writable(f)) {
444         return;
445     }
446
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);
450         }
451     } else {
452         if (f->buf_index > 0) {
453             ret = f->ops->put_buffer(f->opaque, f->buf, f->pos, f->buf_index);
454         }
455     }
456     if (ret >= 0) {
457         f->pos += ret;
458     }
459     f->buf_index = 0;
460     f->iovcnt = 0;
461     if (ret < 0) {
462         qemu_file_set_error(f, ret);
463     }
464 }
465
466 void ram_control_before_iterate(QEMUFile *f, uint64_t flags)
467 {
468     int ret = 0;
469
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);
474         }
475     }
476 }
477
478 void ram_control_after_iterate(QEMUFile *f, uint64_t flags)
479 {
480     int ret = 0;
481
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);
486         }
487     }
488 }
489
490 void ram_control_load_hook(QEMUFile *f, uint64_t flags)
491 {
492     int ret = -EINVAL;
493
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);
498         }
499     } else {
500         qemu_file_set_error(f, ret);
501     }
502 }
503
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)
506 {
507     if (f->ops->save_page) {
508         int ret = f->ops->save_page(f, f->opaque, block_offset,
509                                     offset, size, bytes_sent);
510
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);
516             }
517         }
518
519         return ret;
520     }
521
522     return RAM_SAVE_CONTROL_NOT_SUPP;
523 }
524
525 static void qemu_fill_buffer(QEMUFile *f)
526 {
527     int len;
528     int pending;
529
530     assert(!qemu_file_is_writable(f));
531
532     pending = f->buf_size - f->buf_index;
533     if (pending > 0) {
534         memmove(f->buf, f->buf + f->buf_index, pending);
535     }
536     f->buf_index = 0;
537     f->buf_size = pending;
538
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);
548     }
549 }
550
551 int qemu_get_fd(QEMUFile *f)
552 {
553     if (f->ops->get_fd) {
554         return f->ops->get_fd(f->opaque);
555     }
556     return -1;
557 }
558
559 void qemu_update_position(QEMUFile *f, size_t size)
560 {
561     f->pos += size;
562 }
563
564 /** Closes the file
565  *
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.
568  *
569  * The meaning of return value on success depends on the specific backend
570  * being used.
571  */
572 int qemu_fclose(QEMUFile *f)
573 {
574     int ret;
575     qemu_fflush(f);
576     ret = qemu_file_get_error(f);
577
578     if (f->ops->close) {
579         int ret2 = f->ops->close(f->opaque);
580         if (ret >= 0) {
581             ret = ret2;
582         }
583     }
584     /* If any error was spotted before closing, we should report it
585      * instead of the close() return value.
586      */
587     if (f->last_error) {
588         ret = f->last_error;
589     }
590     g_free(f);
591     return ret;
592 }
593
594 static void add_to_iovec(QEMUFile *f, const uint8_t *buf, int size)
595 {
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;
603     }
604
605     if (f->iovcnt >= MAX_IOV_SIZE) {
606         qemu_fflush(f);
607     }
608 }
609
610 void qemu_put_buffer_async(QEMUFile *f, const uint8_t *buf, int size)
611 {
612     if (!f->ops->writev_buffer) {
613         qemu_put_buffer(f, buf, size);
614         return;
615     }
616
617     if (f->last_error) {
618         return;
619     }
620
621     f->bytes_xfer += size;
622     add_to_iovec(f, buf, size);
623 }
624
625 void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size)
626 {
627     int l;
628
629     if (f->last_error) {
630         return;
631     }
632
633     while (size > 0) {
634         l = IO_BUF_SIZE - f->buf_index;
635         if (l > size) {
636             l = size;
637         }
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);
642         }
643         f->buf_index += l;
644         if (f->buf_index == IO_BUF_SIZE) {
645             qemu_fflush(f);
646         }
647         if (qemu_file_get_error(f)) {
648             break;
649         }
650         buf += l;
651         size -= l;
652     }
653 }
654
655 void qemu_put_byte(QEMUFile *f, int v)
656 {
657     if (f->last_error) {
658         return;
659     }
660
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);
665     }
666     f->buf_index++;
667     if (f->buf_index == IO_BUF_SIZE) {
668         qemu_fflush(f);
669     }
670 }
671
672 void qemu_file_skip(QEMUFile *f, int size)
673 {
674     if (f->buf_index + size <= f->buf_size) {
675         f->buf_index += size;
676     }
677 }
678
679 int qemu_peek_buffer(QEMUFile *f, uint8_t *buf, int size, size_t offset)
680 {
681     int pending;
682     int index;
683
684     assert(!qemu_file_is_writable(f));
685
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;
692     }
693
694     if (pending <= 0) {
695         return 0;
696     }
697     if (size > pending) {
698         size = pending;
699     }
700
701     memcpy(buf, f->buf + index, size);
702     return size;
703 }
704
705 int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size)
706 {
707     int pending = size;
708     int done = 0;
709
710     while (pending > 0) {
711         int res;
712
713         res = qemu_peek_buffer(f, buf, pending, 0);
714         if (res == 0) {
715             return done;
716         }
717         qemu_file_skip(f, res);
718         buf += res;
719         pending -= res;
720         done += res;
721     }
722     return done;
723 }
724
725 int qemu_peek_byte(QEMUFile *f, int offset)
726 {
727     int index = f->buf_index + offset;
728
729     assert(!qemu_file_is_writable(f));
730
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;
736         }
737     }
738     return f->buf[index];
739 }
740
741 int qemu_get_byte(QEMUFile *f)
742 {
743     int result;
744
745     result = qemu_peek_byte(f, 0);
746     qemu_file_skip(f, 1);
747     return result;
748 }
749
750 int64_t qemu_ftell(QEMUFile *f)
751 {
752     qemu_fflush(f);
753     return f->pos;
754 }
755
756 int qemu_file_rate_limit(QEMUFile *f)
757 {
758     if (qemu_file_get_error(f)) {
759         return 1;
760     }
761     if (f->xfer_limit > 0 && f->bytes_xfer > f->xfer_limit) {
762         return 1;
763     }
764     return 0;
765 }
766
767 int64_t qemu_file_get_rate_limit(QEMUFile *f)
768 {
769     return f->xfer_limit;
770 }
771
772 void qemu_file_set_rate_limit(QEMUFile *f, int64_t limit)
773 {
774     f->xfer_limit = limit;
775 }
776
777 void qemu_file_reset_rate_limit(QEMUFile *f)
778 {
779     f->bytes_xfer = 0;
780 }
781
782 void qemu_put_be16(QEMUFile *f, unsigned int v)
783 {
784     qemu_put_byte(f, v >> 8);
785     qemu_put_byte(f, v);
786 }
787
788 void qemu_put_be32(QEMUFile *f, unsigned int v)
789 {
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);
794 }
795
796 void qemu_put_be64(QEMUFile *f, uint64_t v)
797 {
798     qemu_put_be32(f, v >> 32);
799     qemu_put_be32(f, v);
800 }
801
802 unsigned int qemu_get_be16(QEMUFile *f)
803 {
804     unsigned int v;
805     v = qemu_get_byte(f) << 8;
806     v |= qemu_get_byte(f);
807     return v;
808 }
809
810 unsigned int qemu_get_be32(QEMUFile *f)
811 {
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;
818 }
819
820 uint64_t qemu_get_be64(QEMUFile *f)
821 {
822     uint64_t v;
823     v = (uint64_t)qemu_get_be32(f) << 32;
824     v |= qemu_get_be32(f);
825     return v;
826 }