From c5d19034d9b29ff6329e15e42b65c5051d7d0f93 Mon Sep 17 00:00:00 2001
From: Markus Armbruster <armbru@redhat.com>
Date: Tue, 4 Oct 2011 16:24:25 +0200
Subject: [PATCH 50/76] block: Leave tracking media change to device models

RH-Author: Markus Armbruster <armbru@redhat.com>
Message-id: <1317745491-18401-43-git-send-email-armbru@redhat.com>
Patchwork-id: 33634
O-Subject: [PATCH RHEL-6.2 qemu-kvm 42/68] block: Leave tracking media change to device models
Bugzilla: 742458
RH-Acked-by: Paolo Bonzini <pbonzini@redhat.com>
RH-Acked-by: Amit Shah <amit.shah@redhat.com>
RH-Acked-by: Juan Quintela <quintela@redhat.com>

hw/fdc.c is the only one that cares.

Signed-off-by: Markus Armbruster <armbru@redhat.com>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
(cherry picked from commit 8e49ca46249481588c881918d5bd023e16209095)

Conflicts:

	hw/fdc.c

Conflicts due to our commit 9bb1ec33 revert "floppy: save and restore
DIR register", and because we don't have commit 5c02c033 fdc: don't
use reserved _t suffix.
---
 block.c     |   20 ++++++--------------
 block_int.h |    1 -
 hw/fdc.c    |   18 ++++++++++++++++++
 3 files changed, 24 insertions(+), 15 deletions(-)

Signed-off-by: Michal Novotny <minovotn@redhat.com>
---
 block.c     |   20 ++++++--------------
 block_int.h |    1 -
 hw/fdc.c    |   18 ++++++++++++++++++
 3 files changed, 24 insertions(+), 15 deletions(-)

diff --git a/block.c b/block.c
index ec799d5..99d5e9f 100644
--- a/block.c
+++ b/block.c
@@ -626,7 +626,6 @@ int bdrv_open(BlockDriverState *bs, const char *filename, int flags,
     }
 
     if (!bdrv_key_required(bs)) {
-        bs->media_changed = 1;
         bdrv_dev_change_media_cb(bs);
     }
 
@@ -662,7 +661,6 @@ void bdrv_close(BlockDriverState *bs)
             bdrv_close(bs->file);
         }
 
-        bs->media_changed = 1;
         bdrv_dev_change_media_cb(bs);
     }
 }
@@ -1404,7 +1402,6 @@ int bdrv_set_key(BlockDriverState *bs, const char *key)
     } else if (!bs->valid_key) {
         bs->valid_key = 1;
         /* call the change callback now, we skipped it on open */
-        bs->media_changed = 1;
         bdrv_dev_change_media_cb(bs);
     }
     return ret;
@@ -2621,22 +2618,17 @@ int bdrv_is_inserted(BlockDriverState *bs)
 }
 
 /**
- * Return TRUE if the media changed since the last call to this
- * function. It is currently only used for floppy disks
+ * Return whether the media changed since the last call to this
+ * function, or -ENOTSUP if we don't know.  Most drivers don't know.
  */
 int bdrv_media_changed(BlockDriverState *bs)
 {
     BlockDriver *drv = bs->drv;
-    int ret;
 
-    if (!drv || !drv->bdrv_media_changed)
-        ret = -ENOTSUP;
-    else
-        ret = drv->bdrv_media_changed(bs);
-    if (ret == -ENOTSUP)
-        ret = bs->media_changed;
-    bs->media_changed = 0;
-    return ret;
+    if (drv && drv->bdrv_media_changed) {
+        return drv->bdrv_media_changed(bs);
+    }
+    return -ENOTSUP;
 }
 
 /**
diff --git a/block_int.h b/block_int.h
index f44baa4..f2f22d8 100644
--- a/block_int.h
+++ b/block_int.h
@@ -168,7 +168,6 @@ struct BlockDriverState {
                                 this file image */
     char backing_format[16]; /* if non-zero and backing_file exists */
     int is_temporary;
-    int media_changed;
 
     BlockDriverState *backing_hd;
     BlockDriverState *file;
diff --git a/hw/fdc.c b/hw/fdc.c
index b5a9511..2378e3f 100644
--- a/hw/fdc.c
+++ b/hw/fdc.c
@@ -95,6 +95,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)
@@ -1000,6 +1001,10 @@ static int fdctrl_media_changed(fdrive_t *drv)
     if (!drv->bs)
         return 0;
     ret = bdrv_media_changed(drv->bs);
+    if (ret < 0) {
+        ret = drv->media_changed;
+    }
+    drv->media_changed = 0;
     if (ret) {
         fd_revalidate(drv);
     }
@@ -1870,6 +1875,17 @@ static void fdctrl_result_timer(void *opaque)
     fdctrl_stop_transfer(fdctrl, 0x00, 0x00, 0x00);
 }
 
+static void fdctrl_change_cb(void *opaque)
+{
+    fdrive_t *drive = opaque;
+
+    drive->media_changed = 1;
+}
+
+static const BlockDevOps fdctrl_block_ops = {
+    .change_media_cb = fdctrl_change_cb,
+};
+
 /* Init functions */
 static void fdctrl_connect_drives(fdctrl_t *fdctrl)
 {
@@ -1882,7 +1898,9 @@ static void fdctrl_connect_drives(fdctrl_t *fdctrl)
         fd_init(drive);
         fd_revalidate(drive);
         if (drive->bs) {
+            drive->media_changed = 1;
             bdrv_set_removable(drive->bs, 1);
+            bdrv_set_dev_ops(drive->bs, &fdctrl_block_ops, drive);
         }
     }
 }
-- 
1.7.4.4

