[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [PATCH 27/36] Make qemu_peek_buffer loop until it gets it's
From: |
Juan Quintela |
Subject: |
[Qemu-devel] [PATCH 27/36] Make qemu_peek_buffer loop until it gets it's data |
Date: |
Mon, 5 May 2014 22:30:25 +0200 |
From: "Dr. David Alan Gilbert" <address@hidden>
Make qemu_peek_buffer repeatedly call fill_buffer until it gets
all the data it requires, or until there is an error.
At the moment, qemu_peek_buffer will try one qemu_fill_buffer if there
isn't enough data waiting, however the kernel is entitled to return
just a few bytes, and still leave qemu_peek_buffer with less bytes
than it needed. I've seen this fail in a dev world, and I think it
could theoretically fail in the peeking of the subsection headers in
the current world.
Comment qemu_peek_byte to point out it's not guaranteed to work for
non-continuous peeks
Signed-off-by: Dr. David Alan Gilbert <address@hidden>
Reviewed-by: ChenLiang <address@hidden>
Signed-off-by: Juan Quintela <address@hidden>
---
include/migration/qemu-file.h | 5 ++++
qemu-file.c | 53 +++++++++++++++++++++++++++++++++++++++----
2 files changed, 54 insertions(+), 4 deletions(-)
diff --git a/include/migration/qemu-file.h b/include/migration/qemu-file.h
index a191fb6..c90f529 100644
--- a/include/migration/qemu-file.h
+++ b/include/migration/qemu-file.h
@@ -123,6 +123,11 @@ void qemu_put_be32(QEMUFile *f, unsigned int v);
void qemu_put_be64(QEMUFile *f, uint64_t v);
int qemu_peek_buffer(QEMUFile *f, uint8_t *buf, int size, size_t offset);
int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size);
+/*
+ * Note that you can only peek continuous bytes from where the current pointer
+ * is; you aren't guaranteed to be able to peak to +n bytes unless you've
+ * previously peeked +n-1.
+ */
int qemu_peek_byte(QEMUFile *f, int offset);
int qemu_get_byte(QEMUFile *f);
void qemu_file_skip(QEMUFile *f, int size);
diff --git a/qemu-file.c b/qemu-file.c
index 8d5f45d..a8e3912 100644
--- a/qemu-file.c
+++ b/qemu-file.c
@@ -530,7 +530,15 @@ size_t ram_control_save_page(QEMUFile *f, ram_addr_t
block_offset,
return RAM_SAVE_CONTROL_NOT_SUPP;
}
-static void qemu_fill_buffer(QEMUFile *f)
+/*
+ * Attempt to fill the buffer from the underlying file
+ * Returns the number of bytes read, or negative value for an error.
+ *
+ * Note that it can return a partially full buffer even in a not error/not EOF
+ * case if the underlying file descriptor gives a short read, and that can
+ * happen even on a blocking fd.
+ */
+static ssize_t qemu_fill_buffer(QEMUFile *f)
{
int len;
int pending;
@@ -554,6 +562,8 @@ static void qemu_fill_buffer(QEMUFile *f)
} else if (len != -EAGAIN) {
qemu_file_set_error(f, len);
}
+
+ return len;
}
int qemu_get_fd(QEMUFile *f)
@@ -685,17 +695,39 @@ void qemu_file_skip(QEMUFile *f, int size)
}
}
+/*
+ * Read 'size' bytes from file (at 'offset') into buf without moving the
+ * pointer.
+ *
+ * It will return size bytes unless there was an error, in which case it will
+ * return as many as it managed to read (assuming blocking fd's which
+ * all current QEMUFile are)
+ */
int qemu_peek_buffer(QEMUFile *f, uint8_t *buf, int size, size_t offset)
{
int pending;
int index;
assert(!qemu_file_is_writable(f));
+ assert(offset < IO_BUF_SIZE);
+ assert(size <= IO_BUF_SIZE - offset);
+ /* The 1st byte to read from */
index = f->buf_index + offset;
+ /* The number of available bytes starting at index */
pending = f->buf_size - index;
- if (pending < size) {
- qemu_fill_buffer(f);
+
+ /*
+ * qemu_fill_buffer might return just a few bytes, even when there isn't
+ * an error, so loop collecting them until we get enough.
+ */
+ while (pending < size) {
+ int received = qemu_fill_buffer(f);
+
+ if (received <= 0) {
+ break;
+ }
+
index = f->buf_index + offset;
pending = f->buf_size - index;
}
@@ -711,6 +743,14 @@ int qemu_peek_buffer(QEMUFile *f, uint8_t *buf, int size,
size_t offset)
return size;
}
+/*
+ * Read 'size' bytes of data from the file into buf.
+ * 'size' can be larger than the internal buffer.
+ *
+ * It will return size bytes unless there was an error, in which case it will
+ * return as many as it managed to read (assuming blocking fd's which
+ * all current QEMUFile are)
+ */
int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size)
{
int pending = size;
@@ -719,7 +759,7 @@ int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size)
while (pending > 0) {
int res;
- res = qemu_peek_buffer(f, buf, pending, 0);
+ res = qemu_peek_buffer(f, buf, MIN(pending, IO_BUF_SIZE), 0);
if (res == 0) {
return done;
}
@@ -731,11 +771,16 @@ int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size)
return done;
}
+/*
+ * Peeks a single byte from the buffer; this isn't guaranteed to work if
+ * offset leaves a gap after the previous read/peeked data.
+ */
int qemu_peek_byte(QEMUFile *f, int offset)
{
int index = f->buf_index + offset;
assert(!qemu_file_is_writable(f));
+ assert(offset < IO_BUF_SIZE);
if (index >= f->buf_size) {
qemu_fill_buffer(f);
--
1.9.0
- [Qemu-devel] [PATCH 16/36] tsc210x: fix buffer overrun on invalid state load, (continued)
- [Qemu-devel] [PATCH 16/36] tsc210x: fix buffer overrun on invalid state load, Juan Quintela, 2014/05/05
- [Qemu-devel] [PATCH 15/36] ssd0323: fix buffer overun on invalid state load, Juan Quintela, 2014/05/05
- [Qemu-devel] [PATCH 18/36] virtio-scsi: fix buffer overrun on invalid state load, Juan Quintela, 2014/05/05
- [Qemu-devel] [PATCH 17/36] zaurus: fix buffer overrun on invalid state load, Juan Quintela, 2014/05/05
- [Qemu-devel] [PATCH 21/36] savevm: Ignore minimum_version_id_old if there is no load_state_old, Juan Quintela, 2014/05/05
- [Qemu-devel] [PATCH 22/36] ssi-sd: fix buffer overrun on invalid state load, Juan Quintela, 2014/05/05
- [Qemu-devel] [PATCH 19/36] vmstate: s/VMSTATE_INT32_LE/VMSTATE_INT32_POSITIVE_LE/, Juan Quintela, 2014/05/05
- [Qemu-devel] [PATCH 23/36] openpic: avoid buffer overrun on incoming migration, Juan Quintela, 2014/05/05
- [Qemu-devel] [PATCH 20/36] usb: sanity check setup_index+setup_len in post_load, Juan Quintela, 2014/05/05
- [Qemu-devel] [PATCH 24/36] virtio-net: out-of-bounds buffer write on load, Juan Quintela, 2014/05/05
- [Qemu-devel] [PATCH 27/36] Make qemu_peek_buffer loop until it gets it's data,
Juan Quintela <=
- [Qemu-devel] [PATCH 26/36] Disallow outward migration while awaiting incoming migration, Juan Quintela, 2014/05/05
- [Qemu-devel] [PATCH 25/36] virtio: validate config_len on load, Juan Quintela, 2014/05/05
- [Qemu-devel] [PATCH 30/36] Init the XBZRLE.lock in ram_mig_init, Juan Quintela, 2014/05/05
- [Qemu-devel] [PATCH 28/36] Count used RAMBlock pages for migration_dirty_pages, Juan Quintela, 2014/05/05
- [Qemu-devel] [PATCH 29/36] Provide init function for ram migration, Juan Quintela, 2014/05/05
- [Qemu-devel] [PATCH 31/36] Coverity: Fix failure path for qemu_accept in migration, Juan Quintela, 2014/05/05
- [Qemu-devel] [PATCH 32/36] migration: remove duplicate code, Juan Quintela, 2014/05/05
- [Qemu-devel] [PATCH 33/36] XBZRLE: Fix one XBZRLE corruption issues, Juan Quintela, 2014/05/05
- [Qemu-devel] [PATCH 34/36] migration: Add counts of updating the dirty bitmap, Juan Quintela, 2014/05/05
- [Qemu-devel] [PATCH 35/36] migration: expose the bitmap_sync_count to the end, Juan Quintela, 2014/05/05