From 47ac2265d2040b168555d1a7fa89d21bc3d8ed82 Mon Sep 17 00:00:00 2001
From: Juan Quintela <quintela@redhat.com>
Date: Sun, 9 Oct 2011 19:32:06 +0200
Subject: [PATCH 2/8] savevm: teach qemu_fill_buffer to do partial refills

RH-Author: Juan Quintela <quintela@redhat.com>
Message-id: <b82d8174a185473f6a07ab8ed8800793d36ede0c.1318188414.git.quintela@redhat.com>
Patchwork-id: 33949
O-Subject: [PATCH 1/5] savevm: teach qemu_fill_buffer to do partial refills
Bugzilla: 725565
RH-Acked-by: Orit Wasserman <owasserm@redhat.com>
RH-Acked-by: Paolo Bonzini <pbonzini@redhat.com>
RH-Acked-by: Alex Williamson <alex.williamson@redhat.com>

We will need on next patch to be able to lookahead on next patch

v2: rename "used" to "pending" (Alex Williams)

Signed-off-by: Juan Quintela<quintela@redhat.com>
Reviewed-by: Anthony Liguori <aliguori@us.ibm.com>
Signed-off-by: Juan Quintela <quintela@redhat.com>
---
 savevm.c |   14 +++++++++++---
 1 files changed, 11 insertions(+), 3 deletions(-)

Signed-off-by: Michal Novotny <minovotn@redhat.com>
---
 savevm.c |   14 +++++++++++---
 1 files changed, 11 insertions(+), 3 deletions(-)

diff --git a/savevm.c b/savevm.c
index a532abd..629bcad 100644
--- a/savevm.c
+++ b/savevm.c
@@ -458,6 +458,7 @@ void qemu_fflush(QEMUFile *f)
 static void qemu_fill_buffer(QEMUFile *f)
 {
     int len;
+    int pending;
 
     if (!f->get_buffer)
         return;
@@ -465,10 +466,17 @@ static void qemu_fill_buffer(QEMUFile *f)
     if (f->is_write)
         abort();
 
-    len = f->get_buffer(f->opaque, f->buf, f->buf_offset, IO_BUF_SIZE);
+    pending = f->buf_size - f->buf_index;
+    if (pending > 0) {
+        memmove(f->buf, f->buf + f->buf_index, pending);
+    }
+    f->buf_index = 0;
+    f->buf_size = pending;
+
+    len = f->get_buffer(f->opaque, f->buf + pending, f->buf_offset,
+                        IO_BUF_SIZE - pending);
     if (len > 0) {
-        f->buf_index = 0;
-        f->buf_size = len;
+        f->buf_size += len;
         f->buf_offset += len;
     } else if (len != -EAGAIN)
         f->has_error = 1;
-- 
1.7.4.4

