diff mbox series

[for-4.1,1/2] fdc: Fix inserting read-only media in empty drive

Message ID 20190730145727.28965-2-kwolf@redhat.com (mailing list archive)
State New, archived
Headers show
Series fdc: Fix inserting read-only media in empty drive | expand

Commit Message

Kevin Wolf July 30, 2019, 2:57 p.m. UTC
In order to insert a read-only medium (i.e. a read-only block node) to
the BlockBackend of a floppy drive, we must not have taken write
permissions on that BlockBackend, or the operation will fail with the
error message "Block node is read-only".

The device already takes care to remove all permissions when the medium
is ejected, but the state isn't correct if the drive is initially empty:
It uses blk_is_read_only() to check whether write permissions should be
taken, but this function returns false for empty BlockBackends in the
common case.

Fix floppy_drive_realize() to avoid taking write permissions if the
drive is empty.

Signed-off-by: Kevin Wolf <kwolf@redhat.com>
---
 hw/block/fdc.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)

Comments

Max Reitz July 30, 2019, 3:17 p.m. UTC | #1
On 30.07.19 16:57, Kevin Wolf wrote:
> In order to insert a read-only medium (i.e. a read-only block node) to
> the BlockBackend of a floppy drive, we must not have taken write
> permissions on that BlockBackend, or the operation will fail with the
> error message "Block node is read-only".
> 
> The device already takes care to remove all permissions when the medium
> is ejected, but the state isn't correct if the drive is initially empty:
> It uses blk_is_read_only() to check whether write permissions should be
> taken, but this function returns false for empty BlockBackends in the
> common case.
> 
> Fix floppy_drive_realize() to avoid taking write permissions if the
> drive is empty.

...and once the drive is loaded with anything, fd_change_cb() calls
blkconf_apply_backend_option() again with the updated RO state.  Looks
correct.

> Signed-off-by: Kevin Wolf <kwolf@redhat.com>
> ---
>  hw/block/fdc.c | 11 ++++++++---
>  1 file changed, 8 insertions(+), 3 deletions(-)

Reviewed-by: Max Reitz <mreitz@redhat.com>
John Snow July 30, 2019, 3:24 p.m. UTC | #2
On 7/30/19 10:57 AM, Kevin Wolf wrote:
> In order to insert a read-only medium (i.e. a read-only block node) to
> the BlockBackend of a floppy drive, we must not have taken write
> permissions on that BlockBackend, or the operation will fail with the
> error message "Block node is read-only".
> 
> The device already takes care to remove all permissions when the medium
> is ejected, but the state isn't correct if the drive is initially empty:
> It uses blk_is_read_only() to check whether write permissions should be
> taken, but this function returns false for empty BlockBackends in the
> common case.
> 
> Fix floppy_drive_realize() to avoid taking write permissions if the
> drive is empty.
> 
> Signed-off-by: Kevin Wolf <kwolf@redhat.com>
> ---
>  hw/block/fdc.c | 11 ++++++++---
>  1 file changed, 8 insertions(+), 3 deletions(-)
> 
> diff --git a/hw/block/fdc.c b/hw/block/fdc.c
> index 77af9979de..9b24cb9b85 100644
> --- a/hw/block/fdc.c
> +++ b/hw/block/fdc.c
> @@ -514,6 +514,7 @@ static void floppy_drive_realize(DeviceState *qdev, Error **errp)
>      FloppyDrive *dev = FLOPPY_DRIVE(qdev);
>      FloppyBus *bus = FLOPPY_BUS(qdev->parent_bus);
>      FDrive *drive;
> +    bool read_only;
>      int ret;
>  
>      if (dev->unit == -1) {
> @@ -542,6 +543,12 @@ static void floppy_drive_realize(DeviceState *qdev, Error **errp)
>          dev->conf.blk = blk_new(qemu_get_aio_context(), 0, BLK_PERM_ALL);
>          ret = blk_attach_dev(dev->conf.blk, qdev);
>          assert(ret == 0);
> +
> +        /* Don't take write permissions on an empty drive to allow attaching a
> +         * read-only node later */
> +        read_only = true;
> +    } else {
> +        read_only = !blk_bs(dev->conf.blk) || blk_is_read_only(dev->conf.blk);
>      }
>  
>      blkconf_blocksizes(&dev->conf);
> @@ -559,9 +566,7 @@ static void floppy_drive_realize(DeviceState *qdev, Error **errp)
>      dev->conf.rerror = BLOCKDEV_ON_ERROR_AUTO;
>      dev->conf.werror = BLOCKDEV_ON_ERROR_AUTO;
>  
> -    if (!blkconf_apply_backend_options(&dev->conf,
> -                                       blk_is_read_only(dev->conf.blk),
> -                                       false, errp)) {
> +    if (!blkconf_apply_backend_options(&dev->conf, read_only, false, errp)) {
>          return;
>      }
>  
> 

Smells correct.

Reviewed-by: John Snow <jsnow@redhat.com>
diff mbox series

Patch

diff --git a/hw/block/fdc.c b/hw/block/fdc.c
index 77af9979de..9b24cb9b85 100644
--- a/hw/block/fdc.c
+++ b/hw/block/fdc.c
@@ -514,6 +514,7 @@  static void floppy_drive_realize(DeviceState *qdev, Error **errp)
     FloppyDrive *dev = FLOPPY_DRIVE(qdev);
     FloppyBus *bus = FLOPPY_BUS(qdev->parent_bus);
     FDrive *drive;
+    bool read_only;
     int ret;
 
     if (dev->unit == -1) {
@@ -542,6 +543,12 @@  static void floppy_drive_realize(DeviceState *qdev, Error **errp)
         dev->conf.blk = blk_new(qemu_get_aio_context(), 0, BLK_PERM_ALL);
         ret = blk_attach_dev(dev->conf.blk, qdev);
         assert(ret == 0);
+
+        /* Don't take write permissions on an empty drive to allow attaching a
+         * read-only node later */
+        read_only = true;
+    } else {
+        read_only = !blk_bs(dev->conf.blk) || blk_is_read_only(dev->conf.blk);
     }
 
     blkconf_blocksizes(&dev->conf);
@@ -559,9 +566,7 @@  static void floppy_drive_realize(DeviceState *qdev, Error **errp)
     dev->conf.rerror = BLOCKDEV_ON_ERROR_AUTO;
     dev->conf.werror = BLOCKDEV_ON_ERROR_AUTO;
 
-    if (!blkconf_apply_backend_options(&dev->conf,
-                                       blk_is_read_only(dev->conf.blk),
-                                       false, errp)) {
+    if (!blkconf_apply_backend_options(&dev->conf, read_only, false, errp)) {
         return;
     }