]> git.proxmox.com Git - mirror_qemu.git/blame - qemu-file.c
QEMUSizedBuffer based QEMUFile
[mirror_qemu.git] / qemu-file.c
CommitLineData
093c455a
EH
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"
9013dca5 7#include "trace.h"
093c455a
EH
8
9#define IO_BUF_SIZE 32768
10#define MAX_IOV_SIZE MIN(IOV_MAX, 64)
11
12struct QEMUFile {
13 const QEMUFileOps *ops;
14 void *opaque;
15
16 int64_t bytes_xfer;
17 int64_t xfer_limit;
18
19 int64_t pos; /* start of buffer when writing, end of buffer
20 when reading */
21 int buf_index;
22 int buf_size; /* 0 when writing */
23 uint8_t buf[IO_BUF_SIZE];
24
25 struct iovec iov[MAX_IOV_SIZE];
26 unsigned int iovcnt;
27
28 int last_error;
29};
30
31typedef struct QEMUFileStdio {
32 FILE *stdio_file;
33 QEMUFile *file;
34} QEMUFileStdio;
35
36typedef struct QEMUFileSocket {
37 int fd;
38 QEMUFile *file;
39} QEMUFileSocket;
40
41static ssize_t socket_writev_buffer(void *opaque, struct iovec *iov, int iovcnt,
42 int64_t pos)
43{
44 QEMUFileSocket *s = opaque;
45 ssize_t len;
46 ssize_t size = iov_size(iov, iovcnt);
47
48 len = iov_send(s->fd, iov, iovcnt, 0, size);
49 if (len < size) {
50 len = -socket_error();
51 }
52 return len;
53}
54
55static int socket_get_fd(void *opaque)
56{
57 QEMUFileSocket *s = opaque;
58
59 return s->fd;
60}
61
62static int socket_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
63{
64 QEMUFileSocket *s = opaque;
65 ssize_t len;
66
67 for (;;) {
68 len = qemu_recv(s->fd, buf, size, 0);
69 if (len != -1) {
70 break;
71 }
72 if (socket_error() == EAGAIN) {
73 yield_until_fd_readable(s->fd);
74 } else if (socket_error() != EINTR) {
75 break;
76 }
77 }
78
79 if (len == -1) {
80 len = -socket_error();
81 }
82 return len;
83}
84
85static int socket_close(void *opaque)
86{
87 QEMUFileSocket *s = opaque;
88 closesocket(s->fd);
89 g_free(s);
90 return 0;
91}
92
93static int stdio_get_fd(void *opaque)
94{
95 QEMUFileStdio *s = opaque;
96
97 return fileno(s->stdio_file);
98}
99
100static int stdio_put_buffer(void *opaque, const uint8_t *buf, int64_t pos,
101 int size)
102{
103 QEMUFileStdio *s = opaque;
aded6539
JQ
104 int res;
105
106 res = fwrite(buf, 1, size, s->stdio_file);
107
108 if (res != size) {
ac4df4e6 109 return -errno;
aded6539
JQ
110 }
111 return res;
093c455a
EH
112}
113
114static int stdio_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
115{
116 QEMUFileStdio *s = opaque;
117 FILE *fp = s->stdio_file;
118 int bytes;
119
120 for (;;) {
121 clearerr(fp);
122 bytes = fread(buf, 1, size, fp);
123 if (bytes != 0 || !ferror(fp)) {
124 break;
125 }
126 if (errno == EAGAIN) {
127 yield_until_fd_readable(fileno(fp));
128 } else if (errno != EINTR) {
129 break;
130 }
131 }
132 return bytes;
133}
134
135static int stdio_pclose(void *opaque)
136{
137 QEMUFileStdio *s = opaque;
138 int ret;
139 ret = pclose(s->stdio_file);
140 if (ret == -1) {
141 ret = -errno;
142 } else if (!WIFEXITED(ret) || WEXITSTATUS(ret) != 0) {
143 /* close succeeded, but non-zero exit code: */
144 ret = -EIO; /* fake errno value */
145 }
146 g_free(s);
147 return ret;
148}
149
150static int stdio_fclose(void *opaque)
151{
152 QEMUFileStdio *s = opaque;
153 int ret = 0;
154
155 if (s->file->ops->put_buffer || s->file->ops->writev_buffer) {
156 int fd = fileno(s->stdio_file);
157 struct stat st;
158
159 ret = fstat(fd, &st);
160 if (ret == 0 && S_ISREG(st.st_mode)) {
161 /*
162 * If the file handle is a regular file make sure the
163 * data is flushed to disk before signaling success.
164 */
165 ret = fsync(fd);
166 if (ret != 0) {
167 ret = -errno;
168 return ret;
169 }
170 }
171 }
172 if (fclose(s->stdio_file) == EOF) {
173 ret = -errno;
174 }
175 g_free(s);
176 return ret;
177}
178
179static const QEMUFileOps stdio_pipe_read_ops = {
180 .get_fd = stdio_get_fd,
181 .get_buffer = stdio_get_buffer,
182 .close = stdio_pclose
183};
184
185static const QEMUFileOps stdio_pipe_write_ops = {
186 .get_fd = stdio_get_fd,
187 .put_buffer = stdio_put_buffer,
188 .close = stdio_pclose
189};
190
191QEMUFile *qemu_popen_cmd(const char *command, const char *mode)
192{
193 FILE *stdio_file;
194 QEMUFileStdio *s;
195
196 if (mode == NULL || (mode[0] != 'r' && mode[0] != 'w') || mode[1] != 0) {
197 fprintf(stderr, "qemu_popen: Argument validity check failed\n");
198 return NULL;
199 }
200
201 stdio_file = popen(command, mode);
202 if (stdio_file == NULL) {
203 return NULL;
204 }
205
206 s = g_malloc0(sizeof(QEMUFileStdio));
207
208 s->stdio_file = stdio_file;
209
210 if (mode[0] == 'r') {
211 s->file = qemu_fopen_ops(s, &stdio_pipe_read_ops);
212 } else {
213 s->file = qemu_fopen_ops(s, &stdio_pipe_write_ops);
214 }
215 return s->file;
216}
217
218static const QEMUFileOps stdio_file_read_ops = {
219 .get_fd = stdio_get_fd,
220 .get_buffer = stdio_get_buffer,
221 .close = stdio_fclose
222};
223
224static const QEMUFileOps stdio_file_write_ops = {
225 .get_fd = stdio_get_fd,
226 .put_buffer = stdio_put_buffer,
227 .close = stdio_fclose
228};
229
230static ssize_t unix_writev_buffer(void *opaque, struct iovec *iov, int iovcnt,
231 int64_t pos)
232{
233 QEMUFileSocket *s = opaque;
234 ssize_t len, offset;
235 ssize_t size = iov_size(iov, iovcnt);
236 ssize_t total = 0;
237
238 assert(iovcnt > 0);
239 offset = 0;
240 while (size > 0) {
241 /* Find the next start position; skip all full-sized vector elements */
242 while (offset >= iov[0].iov_len) {
243 offset -= iov[0].iov_len;
244 iov++, iovcnt--;
245 }
246
247 /* skip `offset' bytes from the (now) first element, undo it on exit */
248 assert(iovcnt > 0);
249 iov[0].iov_base += offset;
250 iov[0].iov_len -= offset;
251
252 do {
253 len = writev(s->fd, iov, iovcnt);
254 } while (len == -1 && errno == EINTR);
255 if (len == -1) {
256 return -errno;
257 }
258
259 /* Undo the changes above */
260 iov[0].iov_base -= offset;
261 iov[0].iov_len += offset;
262
263 /* Prepare for the next iteration */
264 offset += len;
265 total += len;
266 size -= len;
267 }
268
269 return total;
270}
271
272static int unix_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
273{
274 QEMUFileSocket *s = opaque;
275 ssize_t len;
276
277 for (;;) {
278 len = read(s->fd, buf, size);
279 if (len != -1) {
280 break;
281 }
282 if (errno == EAGAIN) {
283 yield_until_fd_readable(s->fd);
284 } else if (errno != EINTR) {
285 break;
286 }
287 }
288
289 if (len == -1) {
290 len = -errno;
291 }
292 return len;
293}
294
295static int unix_close(void *opaque)
296{
297 QEMUFileSocket *s = opaque;
298 close(s->fd);
299 g_free(s);
300 return 0;
301}
302
303static const QEMUFileOps unix_read_ops = {
304 .get_fd = socket_get_fd,
305 .get_buffer = unix_get_buffer,
306 .close = unix_close
307};
308
309static const QEMUFileOps unix_write_ops = {
310 .get_fd = socket_get_fd,
311 .writev_buffer = unix_writev_buffer,
312 .close = unix_close
313};
314
315QEMUFile *qemu_fdopen(int fd, const char *mode)
316{
317 QEMUFileSocket *s;
318
319 if (mode == NULL ||
320 (mode[0] != 'r' && mode[0] != 'w') ||
321 mode[1] != 'b' || mode[2] != 0) {
322 fprintf(stderr, "qemu_fdopen: Argument validity check failed\n");
323 return NULL;
324 }
325
326 s = g_malloc0(sizeof(QEMUFileSocket));
327 s->fd = fd;
328
329 if (mode[0] == 'r') {
330 s->file = qemu_fopen_ops(s, &unix_read_ops);
331 } else {
332 s->file = qemu_fopen_ops(s, &unix_write_ops);
333 }
334 return s->file;
335}
336
337static const QEMUFileOps socket_read_ops = {
338 .get_fd = socket_get_fd,
339 .get_buffer = socket_get_buffer,
340 .close = socket_close
341};
342
343static const QEMUFileOps socket_write_ops = {
344 .get_fd = socket_get_fd,
345 .writev_buffer = socket_writev_buffer,
346 .close = socket_close
347};
348
349bool qemu_file_mode_is_not_valid(const char *mode)
350{
351 if (mode == NULL ||
352 (mode[0] != 'r' && mode[0] != 'w') ||
353 mode[1] != 'b' || mode[2] != 0) {
354 fprintf(stderr, "qemu_fopen: Argument validity check failed\n");
355 return true;
356 }
357
358 return false;
359}
360
361QEMUFile *qemu_fopen_socket(int fd, const char *mode)
362{
363 QEMUFileSocket *s;
364
365 if (qemu_file_mode_is_not_valid(mode)) {
366 return NULL;
367 }
368
369 s = g_malloc0(sizeof(QEMUFileSocket));
370 s->fd = fd;
371 if (mode[0] == 'w') {
372 qemu_set_block(s->fd);
373 s->file = qemu_fopen_ops(s, &socket_write_ops);
374 } else {
375 s->file = qemu_fopen_ops(s, &socket_read_ops);
376 }
377 return s->file;
378}
379
380QEMUFile *qemu_fopen(const char *filename, const char *mode)
381{
382 QEMUFileStdio *s;
383
384 if (qemu_file_mode_is_not_valid(mode)) {
385 return NULL;
386 }
387
388 s = g_malloc0(sizeof(QEMUFileStdio));
389
390 s->stdio_file = fopen(filename, mode);
391 if (!s->stdio_file) {
392 goto fail;
393 }
394
395 if (mode[0] == 'w') {
396 s->file = qemu_fopen_ops(s, &stdio_file_write_ops);
397 } else {
398 s->file = qemu_fopen_ops(s, &stdio_file_read_ops);
399 }
400 return s->file;
401fail:
402 g_free(s);
403 return NULL;
404}
405
406QEMUFile *qemu_fopen_ops(void *opaque, const QEMUFileOps *ops)
407{
408 QEMUFile *f;
409
410 f = g_malloc0(sizeof(QEMUFile));
411
412 f->opaque = opaque;
413 f->ops = ops;
414 return f;
415}
416
417/*
418 * Get last error for stream f
419 *
420 * Return negative error value if there has been an error on previous
421 * operations, return 0 if no error happened.
422 *
423 */
424int qemu_file_get_error(QEMUFile *f)
425{
426 return f->last_error;
427}
428
429void qemu_file_set_error(QEMUFile *f, int ret)
430{
431 if (f->last_error == 0) {
432 f->last_error = ret;
433 }
434}
435
436static inline bool qemu_file_is_writable(QEMUFile *f)
437{
438 return f->ops->writev_buffer || f->ops->put_buffer;
439}
440
441/**
442 * Flushes QEMUFile buffer
443 *
444 * If there is writev_buffer QEMUFileOps it uses it otherwise uses
445 * put_buffer ops.
446 */
447void qemu_fflush(QEMUFile *f)
448{
449 ssize_t ret = 0;
450
451 if (!qemu_file_is_writable(f)) {
452 return;
453 }
454
455 if (f->ops->writev_buffer) {
456 if (f->iovcnt > 0) {
457 ret = f->ops->writev_buffer(f->opaque, f->iov, f->iovcnt, f->pos);
458 }
459 } else {
460 if (f->buf_index > 0) {
461 ret = f->ops->put_buffer(f->opaque, f->buf, f->pos, f->buf_index);
462 }
463 }
464 if (ret >= 0) {
465 f->pos += ret;
466 }
467 f->buf_index = 0;
468 f->iovcnt = 0;
469 if (ret < 0) {
470 qemu_file_set_error(f, ret);
471 }
472}
473
474void ram_control_before_iterate(QEMUFile *f, uint64_t flags)
475{
476 int ret = 0;
477
478 if (f->ops->before_ram_iterate) {
479 ret = f->ops->before_ram_iterate(f, f->opaque, flags);
480 if (ret < 0) {
481 qemu_file_set_error(f, ret);
482 }
483 }
484}
485
486void ram_control_after_iterate(QEMUFile *f, uint64_t flags)
487{
488 int ret = 0;
489
490 if (f->ops->after_ram_iterate) {
491 ret = f->ops->after_ram_iterate(f, f->opaque, flags);
492 if (ret < 0) {
493 qemu_file_set_error(f, ret);
494 }
495 }
496}
497
498void ram_control_load_hook(QEMUFile *f, uint64_t flags)
499{
500 int ret = -EINVAL;
501
502 if (f->ops->hook_ram_load) {
503 ret = f->ops->hook_ram_load(f, f->opaque, flags);
504 if (ret < 0) {
505 qemu_file_set_error(f, ret);
506 }
507 } else {
508 qemu_file_set_error(f, ret);
509 }
510}
511
512size_t ram_control_save_page(QEMUFile *f, ram_addr_t block_offset,
513 ram_addr_t offset, size_t size, int *bytes_sent)
514{
515 if (f->ops->save_page) {
516 int ret = f->ops->save_page(f, f->opaque, block_offset,
517 offset, size, bytes_sent);
518
519 if (ret != RAM_SAVE_CONTROL_DELAYED) {
520 if (bytes_sent && *bytes_sent > 0) {
521 qemu_update_position(f, *bytes_sent);
522 } else if (ret < 0) {
523 qemu_file_set_error(f, ret);
524 }
525 }
526
527 return ret;
528 }
529
530 return RAM_SAVE_CONTROL_NOT_SUPP;
531}
532
548f52ea
DDAG
533/*
534 * Attempt to fill the buffer from the underlying file
535 * Returns the number of bytes read, or negative value for an error.
536 *
537 * Note that it can return a partially full buffer even in a not error/not EOF
538 * case if the underlying file descriptor gives a short read, and that can
539 * happen even on a blocking fd.
540 */
541static ssize_t qemu_fill_buffer(QEMUFile *f)
093c455a
EH
542{
543 int len;
544 int pending;
545
546 assert(!qemu_file_is_writable(f));
547
548 pending = f->buf_size - f->buf_index;
549 if (pending > 0) {
550 memmove(f->buf, f->buf + f->buf_index, pending);
551 }
552 f->buf_index = 0;
553 f->buf_size = pending;
554
555 len = f->ops->get_buffer(f->opaque, f->buf + pending, f->pos,
556 IO_BUF_SIZE - pending);
557 if (len > 0) {
558 f->buf_size += len;
559 f->pos += len;
560 } else if (len == 0) {
561 qemu_file_set_error(f, -EIO);
562 } else if (len != -EAGAIN) {
563 qemu_file_set_error(f, len);
564 }
548f52ea
DDAG
565
566 return len;
093c455a
EH
567}
568
569int qemu_get_fd(QEMUFile *f)
570{
571 if (f->ops->get_fd) {
572 return f->ops->get_fd(f->opaque);
573 }
574 return -1;
575}
576
577void qemu_update_position(QEMUFile *f, size_t size)
578{
579 f->pos += size;
580}
581
582/** Closes the file
583 *
584 * Returns negative error value if any error happened on previous operations or
585 * while closing the file. Returns 0 or positive number on success.
586 *
587 * The meaning of return value on success depends on the specific backend
588 * being used.
589 */
590int qemu_fclose(QEMUFile *f)
591{
592 int ret;
593 qemu_fflush(f);
594 ret = qemu_file_get_error(f);
595
596 if (f->ops->close) {
597 int ret2 = f->ops->close(f->opaque);
598 if (ret >= 0) {
599 ret = ret2;
600 }
601 }
602 /* If any error was spotted before closing, we should report it
603 * instead of the close() return value.
604 */
605 if (f->last_error) {
606 ret = f->last_error;
607 }
608 g_free(f);
9013dca5 609 trace_qemu_file_fclose();
093c455a
EH
610 return ret;
611}
612
613static void add_to_iovec(QEMUFile *f, const uint8_t *buf, int size)
614{
615 /* check for adjacent buffer and coalesce them */
616 if (f->iovcnt > 0 && buf == f->iov[f->iovcnt - 1].iov_base +
617 f->iov[f->iovcnt - 1].iov_len) {
618 f->iov[f->iovcnt - 1].iov_len += size;
619 } else {
620 f->iov[f->iovcnt].iov_base = (uint8_t *)buf;
621 f->iov[f->iovcnt++].iov_len = size;
622 }
623
624 if (f->iovcnt >= MAX_IOV_SIZE) {
625 qemu_fflush(f);
626 }
627}
628
629void qemu_put_buffer_async(QEMUFile *f, const uint8_t *buf, int size)
630{
631 if (!f->ops->writev_buffer) {
632 qemu_put_buffer(f, buf, size);
633 return;
634 }
635
636 if (f->last_error) {
637 return;
638 }
639
640 f->bytes_xfer += size;
641 add_to_iovec(f, buf, size);
642}
643
644void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size)
645{
646 int l;
647
648 if (f->last_error) {
649 return;
650 }
651
652 while (size > 0) {
653 l = IO_BUF_SIZE - f->buf_index;
654 if (l > size) {
655 l = size;
656 }
657 memcpy(f->buf + f->buf_index, buf, l);
658 f->bytes_xfer += l;
659 if (f->ops->writev_buffer) {
660 add_to_iovec(f, f->buf + f->buf_index, l);
661 }
662 f->buf_index += l;
663 if (f->buf_index == IO_BUF_SIZE) {
664 qemu_fflush(f);
665 }
666 if (qemu_file_get_error(f)) {
667 break;
668 }
669 buf += l;
670 size -= l;
671 }
672}
673
674void qemu_put_byte(QEMUFile *f, int v)
675{
676 if (f->last_error) {
677 return;
678 }
679
680 f->buf[f->buf_index] = v;
681 f->bytes_xfer++;
682 if (f->ops->writev_buffer) {
683 add_to_iovec(f, f->buf + f->buf_index, 1);
684 }
685 f->buf_index++;
686 if (f->buf_index == IO_BUF_SIZE) {
687 qemu_fflush(f);
688 }
689}
690
691void qemu_file_skip(QEMUFile *f, int size)
692{
693 if (f->buf_index + size <= f->buf_size) {
694 f->buf_index += size;
695 }
696}
697
548f52ea
DDAG
698/*
699 * Read 'size' bytes from file (at 'offset') into buf without moving the
700 * pointer.
701 *
702 * It will return size bytes unless there was an error, in which case it will
703 * return as many as it managed to read (assuming blocking fd's which
704 * all current QEMUFile are)
705 */
093c455a
EH
706int qemu_peek_buffer(QEMUFile *f, uint8_t *buf, int size, size_t offset)
707{
708 int pending;
709 int index;
710
711 assert(!qemu_file_is_writable(f));
548f52ea
DDAG
712 assert(offset < IO_BUF_SIZE);
713 assert(size <= IO_BUF_SIZE - offset);
093c455a 714
548f52ea 715 /* The 1st byte to read from */
093c455a 716 index = f->buf_index + offset;
548f52ea 717 /* The number of available bytes starting at index */
093c455a 718 pending = f->buf_size - index;
548f52ea
DDAG
719
720 /*
721 * qemu_fill_buffer might return just a few bytes, even when there isn't
722 * an error, so loop collecting them until we get enough.
723 */
724 while (pending < size) {
725 int received = qemu_fill_buffer(f);
726
727 if (received <= 0) {
728 break;
729 }
730
093c455a
EH
731 index = f->buf_index + offset;
732 pending = f->buf_size - index;
733 }
734
735 if (pending <= 0) {
736 return 0;
737 }
738 if (size > pending) {
739 size = pending;
740 }
741
742 memcpy(buf, f->buf + index, size);
743 return size;
744}
745
548f52ea
DDAG
746/*
747 * Read 'size' bytes of data from the file into buf.
748 * 'size' can be larger than the internal buffer.
749 *
750 * It will return size bytes unless there was an error, in which case it will
751 * return as many as it managed to read (assuming blocking fd's which
752 * all current QEMUFile are)
753 */
093c455a
EH
754int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size)
755{
756 int pending = size;
757 int done = 0;
758
759 while (pending > 0) {
760 int res;
761
548f52ea 762 res = qemu_peek_buffer(f, buf, MIN(pending, IO_BUF_SIZE), 0);
093c455a
EH
763 if (res == 0) {
764 return done;
765 }
766 qemu_file_skip(f, res);
767 buf += res;
768 pending -= res;
769 done += res;
770 }
771 return done;
772}
773
548f52ea
DDAG
774/*
775 * Peeks a single byte from the buffer; this isn't guaranteed to work if
776 * offset leaves a gap after the previous read/peeked data.
777 */
093c455a
EH
778int qemu_peek_byte(QEMUFile *f, int offset)
779{
780 int index = f->buf_index + offset;
781
782 assert(!qemu_file_is_writable(f));
548f52ea 783 assert(offset < IO_BUF_SIZE);
093c455a
EH
784
785 if (index >= f->buf_size) {
786 qemu_fill_buffer(f);
787 index = f->buf_index + offset;
788 if (index >= f->buf_size) {
789 return 0;
790 }
791 }
792 return f->buf[index];
793}
794
795int qemu_get_byte(QEMUFile *f)
796{
797 int result;
798
799 result = qemu_peek_byte(f, 0);
800 qemu_file_skip(f, 1);
801 return result;
802}
803
804int64_t qemu_ftell(QEMUFile *f)
805{
806 qemu_fflush(f);
807 return f->pos;
808}
809
810int qemu_file_rate_limit(QEMUFile *f)
811{
812 if (qemu_file_get_error(f)) {
813 return 1;
814 }
815 if (f->xfer_limit > 0 && f->bytes_xfer > f->xfer_limit) {
816 return 1;
817 }
818 return 0;
819}
820
821int64_t qemu_file_get_rate_limit(QEMUFile *f)
822{
823 return f->xfer_limit;
824}
825
826void qemu_file_set_rate_limit(QEMUFile *f, int64_t limit)
827{
828 f->xfer_limit = limit;
829}
830
831void qemu_file_reset_rate_limit(QEMUFile *f)
832{
833 f->bytes_xfer = 0;
834}
835
836void qemu_put_be16(QEMUFile *f, unsigned int v)
837{
838 qemu_put_byte(f, v >> 8);
839 qemu_put_byte(f, v);
840}
841
842void qemu_put_be32(QEMUFile *f, unsigned int v)
843{
844 qemu_put_byte(f, v >> 24);
845 qemu_put_byte(f, v >> 16);
846 qemu_put_byte(f, v >> 8);
847 qemu_put_byte(f, v);
848}
849
850void qemu_put_be64(QEMUFile *f, uint64_t v)
851{
852 qemu_put_be32(f, v >> 32);
853 qemu_put_be32(f, v);
854}
855
856unsigned int qemu_get_be16(QEMUFile *f)
857{
858 unsigned int v;
859 v = qemu_get_byte(f) << 8;
860 v |= qemu_get_byte(f);
861 return v;
862}
863
864unsigned int qemu_get_be32(QEMUFile *f)
865{
866 unsigned int v;
867 v = qemu_get_byte(f) << 24;
868 v |= qemu_get_byte(f) << 16;
869 v |= qemu_get_byte(f) << 8;
870 v |= qemu_get_byte(f);
871 return v;
872}
873
874uint64_t qemu_get_be64(QEMUFile *f)
875{
876 uint64_t v;
877 v = (uint64_t)qemu_get_be32(f) << 32;
878 v |= qemu_get_be32(f);
879 return v;
880}
deb22f9a
DDAG
881
882#define QSB_CHUNK_SIZE (1 << 10)
883#define QSB_MAX_CHUNK_SIZE (16 * QSB_CHUNK_SIZE)
884
885/**
886 * Create a QEMUSizedBuffer
887 * This type of buffer uses scatter-gather lists internally and
888 * can grow to any size. Any data array in the scatter-gather list
889 * can hold different amount of bytes.
890 *
891 * @buffer: Optional buffer to copy into the QSB
892 * @len: size of initial buffer; if @buffer is given, buffer must
893 * hold at least len bytes
894 *
895 * Returns a pointer to a QEMUSizedBuffer or NULL on allocation failure
896 */
897QEMUSizedBuffer *qsb_create(const uint8_t *buffer, size_t len)
898{
899 QEMUSizedBuffer *qsb;
900 size_t alloc_len, num_chunks, i, to_copy;
901 size_t chunk_size = (len > QSB_MAX_CHUNK_SIZE)
902 ? QSB_MAX_CHUNK_SIZE
903 : QSB_CHUNK_SIZE;
904
905 num_chunks = DIV_ROUND_UP(len ? len : QSB_CHUNK_SIZE, chunk_size);
906 alloc_len = num_chunks * chunk_size;
907
908 qsb = g_try_new0(QEMUSizedBuffer, 1);
909 if (!qsb) {
910 return NULL;
911 }
912
913 qsb->iov = g_try_new0(struct iovec, num_chunks);
914 if (!qsb->iov) {
915 g_free(qsb);
916 return NULL;
917 }
918
919 qsb->n_iov = num_chunks;
920
921 for (i = 0; i < num_chunks; i++) {
922 qsb->iov[i].iov_base = g_try_malloc0(chunk_size);
923 if (!qsb->iov[i].iov_base) {
924 /* qsb_free is safe since g_free can cope with NULL */
925 qsb_free(qsb);
926 return NULL;
927 }
928
929 qsb->iov[i].iov_len = chunk_size;
930 if (buffer) {
931 to_copy = (len - qsb->used) > chunk_size
932 ? chunk_size : (len - qsb->used);
933 memcpy(qsb->iov[i].iov_base, &buffer[qsb->used], to_copy);
934 qsb->used += to_copy;
935 }
936 }
937
938 qsb->size = alloc_len;
939
940 return qsb;
941}
942
943/**
944 * Free the QEMUSizedBuffer
945 *
946 * @qsb: The QEMUSizedBuffer to free
947 */
948void qsb_free(QEMUSizedBuffer *qsb)
949{
950 size_t i;
951
952 if (!qsb) {
953 return;
954 }
955
956 for (i = 0; i < qsb->n_iov; i++) {
957 g_free(qsb->iov[i].iov_base);
958 }
959 g_free(qsb->iov);
960 g_free(qsb);
961}
962
963/**
964 * Get the number of used bytes in the QEMUSizedBuffer
965 *
966 * @qsb: A QEMUSizedBuffer
967 *
968 * Returns the number of bytes currently used in this buffer
969 */
970size_t qsb_get_length(const QEMUSizedBuffer *qsb)
971{
972 return qsb->used;
973}
974
975/**
976 * Set the length of the buffer; the primary usage of this
977 * function is to truncate the number of used bytes in the buffer.
978 * The size will not be extended beyond the current number of
979 * allocated bytes in the QEMUSizedBuffer.
980 *
981 * @qsb: A QEMUSizedBuffer
982 * @new_len: The new length of bytes in the buffer
983 *
984 * Returns the number of bytes the buffer was truncated or extended
985 * to.
986 */
987size_t qsb_set_length(QEMUSizedBuffer *qsb, size_t new_len)
988{
989 if (new_len <= qsb->size) {
990 qsb->used = new_len;
991 } else {
992 qsb->used = qsb->size;
993 }
994 return qsb->used;
995}
996
997/**
998 * Get the iovec that holds the data for a given position @pos.
999 *
1000 * @qsb: A QEMUSizedBuffer
1001 * @pos: The index of a byte in the buffer
1002 * @d_off: Pointer to an offset that this function will indicate
1003 * at what position within the returned iovec the byte
1004 * is to be found
1005 *
1006 * Returns the index of the iovec that holds the byte at the given
1007 * index @pos in the byte stream; a negative number if the iovec
1008 * for the given position @pos does not exist.
1009 */
1010static ssize_t qsb_get_iovec(const QEMUSizedBuffer *qsb,
1011 off_t pos, off_t *d_off)
1012{
1013 ssize_t i;
1014 off_t curr = 0;
1015
1016 if (pos > qsb->used) {
1017 return -1;
1018 }
1019
1020 for (i = 0; i < qsb->n_iov; i++) {
1021 if (curr + qsb->iov[i].iov_len > pos) {
1022 *d_off = pos - curr;
1023 return i;
1024 }
1025 curr += qsb->iov[i].iov_len;
1026 }
1027 return -1;
1028}
1029
1030/*
1031 * Convert the QEMUSizedBuffer into a flat buffer.
1032 *
1033 * Note: If at all possible, try to avoid this function since it
1034 * may unnecessarily copy memory around.
1035 *
1036 * @qsb: pointer to QEMUSizedBuffer
1037 * @start: offset to start at
1038 * @count: number of bytes to copy
1039 * @buf: a pointer to a buffer to write into (at least @count bytes)
1040 *
1041 * Returns the number of bytes copied into the output buffer
1042 */
1043ssize_t qsb_get_buffer(const QEMUSizedBuffer *qsb, off_t start,
1044 size_t count, uint8_t *buffer)
1045{
1046 const struct iovec *iov;
1047 size_t to_copy, all_copy;
1048 ssize_t index;
1049 off_t s_off;
1050 off_t d_off = 0;
1051 char *s;
1052
1053 if (start > qsb->used) {
1054 return 0;
1055 }
1056
1057 all_copy = qsb->used - start;
1058 if (all_copy > count) {
1059 all_copy = count;
1060 } else {
1061 count = all_copy;
1062 }
1063
1064 index = qsb_get_iovec(qsb, start, &s_off);
1065 if (index < 0) {
1066 return 0;
1067 }
1068
1069 while (all_copy > 0) {
1070 iov = &qsb->iov[index];
1071
1072 s = iov->iov_base;
1073
1074 to_copy = iov->iov_len - s_off;
1075 if (to_copy > all_copy) {
1076 to_copy = all_copy;
1077 }
1078 memcpy(&buffer[d_off], &s[s_off], to_copy);
1079
1080 d_off += to_copy;
1081 all_copy -= to_copy;
1082
1083 s_off = 0;
1084 index++;
1085 }
1086
1087 return count;
1088}
1089
1090/**
1091 * Grow the QEMUSizedBuffer to the given size and allocate
1092 * memory for it.
1093 *
1094 * @qsb: A QEMUSizedBuffer
1095 * @new_size: The new size of the buffer
1096 *
1097 * Return:
1098 * a negative error code in case of memory allocation failure
1099 * or
1100 * the new size of the buffer. The returned size may be greater or equal
1101 * to @new_size.
1102 */
1103static ssize_t qsb_grow(QEMUSizedBuffer *qsb, size_t new_size)
1104{
1105 size_t needed_chunks, i;
1106
1107 if (qsb->size < new_size) {
1108 struct iovec *new_iov;
1109 size_t size_diff = new_size - qsb->size;
1110 size_t chunk_size = (size_diff > QSB_MAX_CHUNK_SIZE)
1111 ? QSB_MAX_CHUNK_SIZE : QSB_CHUNK_SIZE;
1112
1113 needed_chunks = DIV_ROUND_UP(size_diff, chunk_size);
1114
1115 new_iov = g_try_new(struct iovec, qsb->n_iov + needed_chunks);
1116 if (new_iov == NULL) {
1117 return -ENOMEM;
1118 }
1119
1120 /* Allocate new chunks as needed into new_iov */
1121 for (i = qsb->n_iov; i < qsb->n_iov + needed_chunks; i++) {
1122 new_iov[i].iov_base = g_try_malloc0(chunk_size);
1123 new_iov[i].iov_len = chunk_size;
1124 if (!new_iov[i].iov_base) {
1125 size_t j;
1126
1127 /* Free previously allocated new chunks */
1128 for (j = qsb->n_iov; j < i; j++) {
1129 g_free(new_iov[j].iov_base);
1130 }
1131 g_free(new_iov);
1132
1133 return -ENOMEM;
1134 }
1135 }
1136
1137 /*
1138 * Now we can't get any allocation errors, copy over to new iov
1139 * and switch.
1140 */
1141 for (i = 0; i < qsb->n_iov; i++) {
1142 new_iov[i] = qsb->iov[i];
1143 }
1144
1145 qsb->n_iov += needed_chunks;
1146 g_free(qsb->iov);
1147 qsb->iov = new_iov;
1148 qsb->size += (needed_chunks * chunk_size);
1149 }
1150
1151 return qsb->size;
1152}
1153
1154/**
1155 * Write into the QEMUSizedBuffer at a given position and a given
1156 * number of bytes. This function will automatically grow the
1157 * QEMUSizedBuffer.
1158 *
1159 * @qsb: A QEMUSizedBuffer
1160 * @source: A byte array to copy data from
1161 * @pos: The position within the @qsb to write data to
1162 * @size: The number of bytes to copy into the @qsb
1163 *
1164 * Returns @size or a negative error code in case of memory allocation failure,
1165 * or with an invalid 'pos'
1166 */
1167ssize_t qsb_write_at(QEMUSizedBuffer *qsb, const uint8_t *source,
1168 off_t pos, size_t count)
1169{
1170 ssize_t rc = qsb_grow(qsb, pos + count);
1171 size_t to_copy;
1172 size_t all_copy = count;
1173 const struct iovec *iov;
1174 ssize_t index;
1175 char *dest;
1176 off_t d_off, s_off = 0;
1177
1178 if (rc < 0) {
1179 return rc;
1180 }
1181
1182 if (pos + count > qsb->used) {
1183 qsb->used = pos + count;
1184 }
1185
1186 index = qsb_get_iovec(qsb, pos, &d_off);
1187 if (index < 0) {
1188 return -EINVAL;
1189 }
1190
1191 while (all_copy > 0) {
1192 iov = &qsb->iov[index];
1193
1194 dest = iov->iov_base;
1195
1196 to_copy = iov->iov_len - d_off;
1197 if (to_copy > all_copy) {
1198 to_copy = all_copy;
1199 }
1200
1201 memcpy(&dest[d_off], &source[s_off], to_copy);
1202
1203 s_off += to_copy;
1204 all_copy -= to_copy;
1205
1206 d_off = 0;
1207 index++;
1208 }
1209
1210 return count;
1211}
1212
1213/**
1214 * Create a deep copy of the given QEMUSizedBuffer.
1215 *
1216 * @qsb: A QEMUSizedBuffer
1217 *
1218 * Returns a clone of @qsb or NULL on allocation failure
1219 */
1220QEMUSizedBuffer *qsb_clone(const QEMUSizedBuffer *qsb)
1221{
1222 QEMUSizedBuffer *out = qsb_create(NULL, qsb_get_length(qsb));
1223 size_t i;
1224 ssize_t res;
1225 off_t pos = 0;
1226
1227 if (!out) {
1228 return NULL;
1229 }
1230
1231 for (i = 0; i < qsb->n_iov; i++) {
1232 res = qsb_write_at(out, qsb->iov[i].iov_base,
1233 pos, qsb->iov[i].iov_len);
1234 if (res < 0) {
1235 qsb_free(out);
1236 return NULL;
1237 }
1238 pos += res;
1239 }
1240
1241 return out;
1242}
1243
1244typedef struct QEMUBuffer {
1245 QEMUSizedBuffer *qsb;
1246 QEMUFile *file;
1247} QEMUBuffer;
1248
1249static int buf_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
1250{
1251 QEMUBuffer *s = opaque;
1252 ssize_t len = qsb_get_length(s->qsb) - pos;
1253
1254 if (len <= 0) {
1255 return 0;
1256 }
1257
1258 if (len > size) {
1259 len = size;
1260 }
1261 return qsb_get_buffer(s->qsb, pos, len, buf);
1262}
1263
1264static int buf_put_buffer(void *opaque, const uint8_t *buf,
1265 int64_t pos, int size)
1266{
1267 QEMUBuffer *s = opaque;
1268
1269 return qsb_write_at(s->qsb, buf, pos, size);
1270}
1271
1272static int buf_close(void *opaque)
1273{
1274 QEMUBuffer *s = opaque;
1275
1276 qsb_free(s->qsb);
1277
1278 g_free(s);
1279
1280 return 0;
1281}
1282
1283const QEMUSizedBuffer *qemu_buf_get(QEMUFile *f)
1284{
1285 QEMUBuffer *p;
1286
1287 qemu_fflush(f);
1288
1289 p = f->opaque;
1290
1291 return p->qsb;
1292}
1293
1294static const QEMUFileOps buf_read_ops = {
1295 .get_buffer = buf_get_buffer,
1296 .close = buf_close,
1297};
1298
1299static const QEMUFileOps buf_write_ops = {
1300 .put_buffer = buf_put_buffer,
1301 .close = buf_close,
1302};
1303
1304QEMUFile *qemu_bufopen(const char *mode, QEMUSizedBuffer *input)
1305{
1306 QEMUBuffer *s;
1307
1308 if (mode == NULL || (mode[0] != 'r' && mode[0] != 'w') ||
1309 mode[1] != '\0') {
1310 error_report("qemu_bufopen: Argument validity check failed");
1311 return NULL;
1312 }
1313
1314 s = g_malloc0(sizeof(QEMUBuffer));
1315 if (mode[0] == 'r') {
1316 s->qsb = input;
1317 }
1318
1319 if (s->qsb == NULL) {
1320 s->qsb = qsb_create(NULL, 0);
1321 }
1322 if (!s->qsb) {
1323 g_free(s);
1324 error_report("qemu_bufopen: qsb_create failed");
1325 return NULL;
1326 }
1327
1328
1329 if (mode[0] == 'r') {
1330 s->file = qemu_fopen_ops(s, &buf_read_ops);
1331 } else {
1332 s->file = qemu_fopen_ops(s, &buf_write_ops);
1333 }
1334 return s->file;
1335}