From a547697ef31e2879f38f5ac6f71f67751560283e Mon Sep 17 00:00:00 2001
From: Xiao Wang <jasowang@redhat.com>
Date: Wed, 6 Apr 2011 11:07:21 -0300
Subject: [RHEL6 qemu-kvm PATCH 2/5] floppy: save and restore DIR register

RH-Author: Xiao Wang <jasowang@redhat.com>
Message-id: <20110406110721.11772.11724.stgit@dhcp-91-7.nay.redhat.com.englab.nay.redhat.com>
Patchwork-id: 21520
O-Subject: [PATCH] floppy: save and restore DIR register
Bugzilla: 681777
RH-Acked-by: Kevin Wolf <kwolf@redhat.com>
RH-Acked-by: Juan Quintela <quintela@redhat.com>
RH-Acked-by: Paolo Bonzini <pbonzini@redhat.com>

Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=681777
Upstream: POST V4 to the list
Brew Build: https://brewweb.devel.redhat.com/taskinfo?taskID=3235081
Test status: Tested in my local desktop
Notes:
Since drives_reopen() in RHEL6 also call bdrv_close()/bdrv_open() which may also
touch media_changed flag after migration. I simply cache it there to make sure
its value was not changed after drives_reopen().

We need to keep DIR register unchanged across migration, but currently it
depends on the media_changed flags from block layer. Since we do not
save/restore it and the bdrv_open() called in dest node may set the
media_changed flag when trying to open floppy image, guest driver may think the
floppy have changed after migration. To fix this, a new filed media_changed in
FDrive strcutre was introduced in order to save and restore the it from block
layer through pre_save/post_load callbacks.

Signed-off-by: Jason Wang <jasowang@redhat.com>
---
 hw/fdc.c |   49 +++++++++++++++++++++++++++++++++++++++++++++++++
 vl.c     |    2 ++
 2 files changed, 51 insertions(+), 0 deletions(-)

Signed-off-by: Eduardo Habkost <ehabkost@redhat.com>
---
 hw/fdc.c |   49 +++++++++++++++++++++++++++++++++++++++++++++++++
 vl.c     |    2 ++
 2 files changed, 51 insertions(+), 0 deletions(-)

diff --git a/hw/fdc.c b/hw/fdc.c
index 35c1a9e..e78d76b 100644
--- a/hw/fdc.c
+++ b/hw/fdc.c
@@ -35,6 +35,7 @@
 #include "sysbus.h"
 #include "qdev-addr.h"
 #include "sysemu.h"
+#include "block_int.h"
 
 /********************************************************/
 /* debug Floppy devices */
@@ -96,6 +97,7 @@ typedef struct fdrive_t {
     uint8_t max_track;        /* Nb of tracks           */
     uint16_t bps;             /* Bytes per sector       */
     uint8_t ro;               /* Is read-only           */
+    uint8_t media_changed;    /* Is media changed       */
 } fdrive_t;
 
 static void fd_init (fdrive_t *drv)
@@ -632,6 +634,45 @@ static CPUWriteMemoryFunc * const fdctrl_mem_write_strict[3] = {
     NULL,
 };
 
+static void fdrive_media_changed_pre_save(void *opaque)
+{
+    fdrive_t *drive = opaque;
+
+    drive->media_changed = drive->bs->media_changed;
+}
+
+static int fdrive_media_changed_post_load(void *opaque, int version_id)
+{
+    fdrive_t *drive = opaque;
+
+    if (drive->bs != NULL) {
+        drive->bs->media_changed = drive->media_changed;
+    }
+
+    /* User ejected the floppy when drive->bs == NULL */
+    return 0;
+}
+
+static bool fdrive_media_changed_needed(void *opaque)
+{
+    fdrive_t *drive = opaque;
+
+    return (drive->bs != NULL && drive->bs->media_changed != 1);
+}
+
+static const VMStateDescription vmstate_fdrive_media_changed = {
+    .name = "fdrive/media_changed",
+    .version_id = 1,
+    .minimum_version_id = 1,
+    .minimum_version_id_old = 1,
+    .pre_save = fdrive_media_changed_pre_save,
+    .post_load = fdrive_media_changed_post_load,
+    .fields      = (VMStateField[]) {
+        VMSTATE_UINT8(media_changed, fdrive_t),
+        VMSTATE_END_OF_LIST()
+    }
+};
+
 static const VMStateDescription vmstate_fdrive = {
     .name = "fdrive",
     .version_id = 1,
@@ -642,6 +683,14 @@ static const VMStateDescription vmstate_fdrive = {
         VMSTATE_UINT8(track, fdrive_t),
         VMSTATE_UINT8(sect, fdrive_t),
         VMSTATE_END_OF_LIST()
+    },
+    .subsections = (VMStateSubsection[]) {
+        {
+            .vmsd = &vmstate_fdrive_media_changed,
+            .needed = &fdrive_media_changed_needed,
+        } , {
+            /* empty */
+        }
     }
 };
 
diff --git a/vl.c b/vl.c
index 9c2ec66..4e800b2 100644
--- a/vl.c
+++ b/vl.c
@@ -2257,8 +2257,10 @@ int drives_reopen(void)
     QTAILQ_FOREACH(dinfo, &drives, next) {
         if (dinfo->opened && !bdrv_is_read_only(dinfo->bdrv)) {
             int res;
+            int media_changed = dinfo->bdrv->media_changed;
             bdrv_close(dinfo->bdrv);
             res = drive_open(dinfo);
+            dinfo->bdrv->media_changed = media_changed;
             if (res) {
 		    fprintf(stderr, "qemu: re-open of %s failed wth error %d\n",
 			    dinfo->file, res);
-- 
1.7.3.2

