Fix some issues with QEMUFile
This patch allows QEMUFile's read and write operations to return
negative error codes. This is necessary to detect things like closed
streams during live migration.
It also removes unused code for QEMUFileFD write path. Finally, it
makes sure to avoid attempting to flush an output buffer if the file
is only being used for input. This was spotted by Uri Lublin.
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
git-svn-id: svn://svn.savannah.nongnu.org/qemu/trunk@5474 c046a42c-6fe2-441c-8c8c-71466251a162
diff --git a/vl.c b/vl.c
index 5659fea..c31f972 100644
--- a/vl.c
+++ b/vl.c
@@ -6197,12 +6197,15 @@
QEMUFileCloseFunc *close;
QEMUFileRateLimit *rate_limit;
void *opaque;
+ int is_write;
int64_t buf_offset; /* start of buffer when writing, end of buffer
when reading */
int buf_index;
int buf_size; /* 0 when writing */
uint8_t buf[IO_BUF_SIZE];
+
+ int has_error;
};
typedef struct QEMUFileFD
@@ -6211,34 +6214,6 @@
QEMUFile *file;
} QEMUFileFD;
-static void fd_put_notify(void *opaque)
-{
- QEMUFileFD *s = opaque;
-
- /* Remove writable callback and do a put notify */
- qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
- qemu_file_put_notify(s->file);
-}
-
-static void fd_put_buffer(void *opaque, const uint8_t *buf,
- int64_t pos, int size)
-{
- QEMUFileFD *s = opaque;
- ssize_t len;
-
- do {
- len = write(s->fd, buf, size);
- } while (len == -1 && errno == EINTR);
-
- if (len == -1)
- len = -errno;
-
- /* When the fd becomes writable again, register a callback to do
- * a put notify */
- if (len == -EAGAIN)
- qemu_set_fd_handler2(s->fd, NULL, NULL, fd_put_notify, s);
-}
-
static int fd_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
{
QEMUFileFD *s = opaque;
@@ -6269,7 +6244,7 @@
return NULL;
s->fd = fd;
- s->file = qemu_fopen_ops(s, fd_put_buffer, fd_get_buffer, fd_close, NULL);
+ s->file = qemu_fopen_ops(s, NULL, fd_get_buffer, fd_close, NULL);
return s->file;
}
@@ -6278,12 +6253,13 @@
FILE *outfile;
} QEMUFileStdio;
-static void file_put_buffer(void *opaque, const uint8_t *buf,
+static int file_put_buffer(void *opaque, const uint8_t *buf,
int64_t pos, int size)
{
QEMUFileStdio *s = opaque;
fseek(s->outfile, pos, SEEK_SET);
fwrite(buf, 1, size, s->outfile);
+ return size;
}
static int file_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
@@ -6331,11 +6307,12 @@
int64_t base_offset;
} QEMUFileBdrv;
-static void bdrv_put_buffer(void *opaque, const uint8_t *buf,
- int64_t pos, int size)
+static int bdrv_put_buffer(void *opaque, const uint8_t *buf,
+ int64_t pos, int size)
{
QEMUFileBdrv *s = opaque;
bdrv_pwrite(s->bs, s->base_offset + pos, buf, size);
+ return size;
}
static int bdrv_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
@@ -6384,18 +6361,29 @@
f->get_buffer = get_buffer;
f->close = close;
f->rate_limit = rate_limit;
+ f->is_write = 0;
return f;
}
+int qemu_file_has_error(QEMUFile *f)
+{
+ return f->has_error;
+}
+
void qemu_fflush(QEMUFile *f)
{
if (!f->put_buffer)
return;
- if (f->buf_index > 0) {
- f->put_buffer(f->opaque, f->buf, f->buf_offset, f->buf_index);
- f->buf_offset += f->buf_index;
+ if (f->is_write && f->buf_index > 0) {
+ int len;
+
+ len = f->put_buffer(f->opaque, f->buf, f->buf_offset, f->buf_index);
+ if (len > 0)
+ f->buf_offset += f->buf_index;
+ else
+ f->has_error = 1;
f->buf_index = 0;
}
}
@@ -6407,13 +6395,16 @@
if (!f->get_buffer)
return;
- len = f->get_buffer(f->opaque, f->buf, f->buf_offset, IO_BUF_SIZE);
- if (len < 0)
- len = 0;
+ if (f->is_write)
+ abort();
- f->buf_index = 0;
- f->buf_size = len;
- f->buf_offset += len;
+ len = f->get_buffer(f->opaque, f->buf, f->buf_offset, IO_BUF_SIZE);
+ if (len > 0) {
+ f->buf_index = 0;
+ f->buf_size = len;
+ f->buf_offset += len;
+ } else if (len != -EAGAIN)
+ f->has_error = 1;
}
int qemu_fclose(QEMUFile *f)
@@ -6434,11 +6425,19 @@
void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size)
{
int l;
- while (size > 0) {
+
+ if (!f->has_error && f->is_write == 0 && f->buf_index > 0) {
+ fprintf(stderr,
+ "Attempted to write to buffer while read buffer is not empty\n");
+ abort();
+ }
+
+ while (!f->has_error && size > 0) {
l = IO_BUF_SIZE - f->buf_index;
if (l > size)
l = size;
memcpy(f->buf + f->buf_index, buf, l);
+ f->is_write = 1;
f->buf_index += l;
buf += l;
size -= l;
@@ -6449,7 +6448,14 @@
void qemu_put_byte(QEMUFile *f, int v)
{
+ if (!f->has_error && f->is_write == 0 && f->buf_index > 0) {
+ fprintf(stderr,
+ "Attempted to write to buffer while read buffer is not empty\n");
+ abort();
+ }
+
f->buf[f->buf_index++] = v;
+ f->is_write = 1;
if (f->buf_index >= IO_BUF_SIZE)
qemu_fflush(f);
}
@@ -6458,6 +6464,9 @@
{
int size, l;
+ if (f->is_write)
+ abort();
+
size = size1;
while (size > 0) {
l = f->buf_size - f->buf_index;
@@ -6479,6 +6488,9 @@
int qemu_get_byte(QEMUFile *f)
{
+ if (f->is_write)
+ abort();
+
if (f->buf_index >= f->buf_size) {
qemu_fill_buffer(f);
if (f->buf_index >= f->buf_size)
@@ -6671,6 +6683,9 @@
se->save_live_state(f, QEMU_VM_SECTION_START, se->opaque);
}
+ if (qemu_file_has_error(f))
+ return -EIO;
+
return 0;
}
@@ -6693,6 +6708,9 @@
if (ret)
return 1;
+ if (qemu_file_has_error(f))
+ return -EIO;
+
return 0;
}
@@ -6734,6 +6752,9 @@
qemu_put_byte(f, QEMU_VM_EOF);
+ if (qemu_file_has_error(f))
+ return -EIO;
+
return 0;
}
@@ -6758,8 +6779,12 @@
ret = qemu_savevm_state_complete(f);
out:
- if (saved_vm_running)
+ if (qemu_file_has_error(f))
+ ret = -EIO;
+
+ if (!ret && saved_vm_running)
vm_start();
+
return ret;
}
@@ -6815,6 +6840,10 @@
/* always seek to exact end of record */
qemu_fseek(f, cur_pos + record_len, SEEK_SET);
}
+
+ if (qemu_file_has_error(f))
+ return -EIO;
+
return 0;
}
@@ -6913,6 +6942,9 @@
qemu_free(le);
}
+ if (qemu_file_has_error(f))
+ ret = -EIO;
+
return ret;
}
@@ -7224,6 +7256,10 @@
default:
return -EINVAL;
}
+
+ if (qemu_file_has_error(f))
+ return -EIO;
+
return 0;
}