Skip to contentSkip to conversation

fdc: Fix inserting read-only media in empty drive

GIT PULL Applied Kevin Wolf 2 participants 4 messages Jul 30, 2019

lore
Share & tools
lore

Kevin Wolf

author
#0 lore raw

The following changes since commit 8517bf84056282ea3e27772d51f76db3a6fa2d26:

Merge remote-tracking branch 'remotes/maxreitz/tags/pull-block-2019-07-30' into staging (2019-07-30 14:23:07 +0100)

are available in the Git repository at:

git://repo.or.cz/qemu/kevin.git tags/for-upstream

for you to fetch changes up to 68174160144c9263366a4397ef8b417698e2735c:

iotests/118: Test inserting a read-only medium (2019-07-30 17:32:01 +0200)

----------------------------------------------------------------
Block layer patches:

- fdc: Fix inserting read-only media in empty drive

----------------------------------------------------------------
Kevin Wolf (2):
fdc: Fix inserting read-only media in empty drive iotests/118: Test inserting a read-only medium

 hw/block/fdc.c             | 11 ++++++++---
 tests/qemu-iotests/118     |  6 +++++-
 tests/qemu-iotests/118.out |  4 ++--
 3 files changed, 15 insertions(+), 6 deletions(-)
author
#1 lore raw

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.

---
 hw/block/fdc.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
hw/block/fdc.c +8 -3
index 77af9979de..9b24cb9b85 100644
@@ -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;
     }
 
Signature · 1 line
2.20.1
author
#2 lore raw

[Qemu-devel] [PULL 2/2] iotests/118: Test inserting a read-only medium

In reply to Kevin Wolf
---
 tests/qemu-iotests/118     | 6 +++++-
 tests/qemu-iotests/118.out | 4 ++--
 2 files changed, 7 insertions(+), 3 deletions(-)
tests/qemu-iotests/118 +5 -1
index 603e10e8a2..499c5f0901 100755
@@ -207,10 +207,11 @@ class GeneralChangeTestsBaseClass(ChangeBaseClass):
             self.assert_qmp(result, 'return[0]/tray_open', False)
         self.assert_qmp(result, 'return[0]/inserted/image/filename', new_img)
 
-    def test_cycle(self):
+    def test_cycle(self, read_only_node=False):
         result = self.vm.qmp('blockdev-add',
                              node_name='new',
                              driver=iotests.imgfmt,
+                             read_only=read_only_node,
                              file={'filename': new_img,
                                     'driver': 'file'})
         self.assert_qmp(result, 'return', {})
@@ -257,6 +258,9 @@ class GeneralChangeTestsBaseClass(ChangeBaseClass):
             self.assert_qmp(result, 'return[0]/tray_open', False)
         self.assert_qmp(result, 'return[0]/inserted/image/filename', new_img)
 
+    def test_cycle_read_only_media(self):
+        self.test_cycle(True)
+
     def test_close_on_closed(self):
         result = self.vm.qmp('blockdev-close-tray', id=self.device_name)
         # Should be a no-op
tests/qemu-iotests/118.out +2 -2
index 6a917130b6..4823c113d5 100644
@@ -1,5 +1,5 @@
-...........................................................
+...............................................................
 ----------------------------------------------------------------------
-Ran 59 tests
+Ran 63 tests
 
 OK
Signature · 1 line
2.20.1
#3 lore raw
In reply to Kevin Wolf
Quoted 16 lines · Kevin Wolf

The following changes since commit 8517bf84056282ea3e27772d51f76db3a6fa2d26:

Merge remote-tracking branch 'remotes/maxreitz/tags/pull-block-2019-07-30' into staging (2019-07-30 14:23:07 +0100)

are available in the Git repository at:

git://repo.or.cz/qemu/kevin.git tags/for-upstream

for you to fetch changes up to 68174160144c9263366a4397ef8b417698e2735c:

iotests/118: Test inserting a read-only medium (2019-07-30 17:32:01 +0200)

----------------------------------------------------------------
Block layer patches:

Quoted 6 lines

- fdc: Fix inserting read-only media in empty drive

----------------------------------------------------------------
Kevin Wolf (2):
fdc: Fix inserting read-only media in empty drive iotests/118: Test inserting a read-only medium

Applied, thanks.

Please update the changelog at https://wiki.qemu.org/ChangeLog/4.1
for any user-visible changes.

-- PMM