Re: [PATCH 1/2] block: Separate blk_is_writable() and blk_supports_write_perm()

2021-01-19 Thread Max Reitz

On 18.01.21 13:34, Kevin Wolf wrote:

Currently, blk_is_read_only() tells whether a given BlockBackend can
only be used in read-only mode because its root node is read-only. Some
callers actually try to answer a slightly different question: Is the
BlockBackend configured to be writable, by taking write permissions on
the root node?

This can differ, for example, for CD-ROM devices which don't take write
permissions, but may be backed by a writable image file. scsi-cd allows
write requests to the drive if blk_is_read_only() returns false.
However, the write request will immediately run into an assertion
failure because the write permission is missing.

This patch introduces separate functions for both questions.
blk_supports_write_perm() answers the question whether the block
node/image file can support writable devices, whereas blk_is_writable()
tells whether the BlockBackend is currently configured to be writable.

All calls of blk_is_read_only() are converted to one of the two new
functions.

Fixes: https://bugs.launchpad.net/bugs/1906693
Cc: qemu-sta...@nongnu.org
Signed-off-by: Kevin Wolf 
---
  include/sysemu/block-backend.h |  3 ++-
  block/block-backend.c  | 19 ---
  hw/block/dataplane/xen-block.c |  2 +-
  hw/block/fdc.c |  9 +
  hw/block/m25p80.c  |  6 +++---
  hw/block/nand.c|  2 +-
  hw/block/nvme-ns.c |  7 ---
  hw/block/onenand.c |  2 +-
  hw/block/pflash_cfi01.c|  2 +-
  hw/block/pflash_cfi02.c|  2 +-
  hw/block/swim.c|  6 +++---
  hw/block/virtio-blk.c  |  6 +++---
  hw/block/xen-block.c   |  2 +-
  hw/ide/core.c  |  2 +-
  hw/misc/sifive_u_otp.c |  2 +-
  hw/ppc/pnv_pnor.c  |  2 +-
  hw/scsi/scsi-disk.c| 10 +-
  hw/scsi/scsi-generic.c |  4 ++--
  hw/sd/sd.c |  6 +++---
  hw/usb/dev-storage.c   |  4 ++--
  20 files changed, 57 insertions(+), 41 deletions(-)


Reviewed-by: Max Reitz 




Re: [PATCH 1/2] block: Separate blk_is_writable() and blk_supports_write_perm()

2021-01-18 Thread Philippe Mathieu-Daudé
On 1/18/21 1:34 PM, Kevin Wolf wrote:
> Currently, blk_is_read_only() tells whether a given BlockBackend can
> only be used in read-only mode because its root node is read-only. Some
> callers actually try to answer a slightly different question: Is the
> BlockBackend configured to be writable, by taking write permissions on
> the root node?
> 
> This can differ, for example, for CD-ROM devices which don't take write
> permissions, but may be backed by a writable image file. scsi-cd allows
> write requests to the drive if blk_is_read_only() returns false.
> However, the write request will immediately run into an assertion
> failure because the write permission is missing.
> 
> This patch introduces separate functions for both questions.
> blk_supports_write_perm() answers the question whether the block
> node/image file can support writable devices, whereas blk_is_writable()
> tells whether the BlockBackend is currently configured to be writable.
> 
> All calls of blk_is_read_only() are converted to one of the two new
> functions.
> 
> Fixes: https://bugs.launchpad.net/bugs/1906693
> Cc: qemu-sta...@nongnu.org
> Signed-off-by: Kevin Wolf 
> ---
>  include/sysemu/block-backend.h |  3 ++-
>  block/block-backend.c  | 19 ---
>  hw/block/dataplane/xen-block.c |  2 +-
>  hw/block/fdc.c |  9 +
>  hw/block/m25p80.c  |  6 +++---
>  hw/block/nand.c|  2 +-
>  hw/block/nvme-ns.c |  7 ---
>  hw/block/onenand.c |  2 +-
>  hw/block/pflash_cfi01.c|  2 +-
>  hw/block/pflash_cfi02.c|  2 +-
>  hw/block/swim.c|  6 +++---
>  hw/block/virtio-blk.c  |  6 +++---
>  hw/block/xen-block.c   |  2 +-
>  hw/ide/core.c  |  2 +-
>  hw/misc/sifive_u_otp.c |  2 +-
>  hw/ppc/pnv_pnor.c  |  2 +-
>  hw/scsi/scsi-disk.c| 10 +-
>  hw/scsi/scsi-generic.c |  4 ++--
>  hw/sd/sd.c |  6 +++---
>  hw/usb/dev-storage.c   |  4 ++--
>  20 files changed, 57 insertions(+), 41 deletions(-)

Reviewed-by: Philippe Mathieu-Daudé 




[PATCH 1/2] block: Separate blk_is_writable() and blk_supports_write_perm()

2021-01-18 Thread Kevin Wolf
Currently, blk_is_read_only() tells whether a given BlockBackend can
only be used in read-only mode because its root node is read-only. Some
callers actually try to answer a slightly different question: Is the
BlockBackend configured to be writable, by taking write permissions on
the root node?

This can differ, for example, for CD-ROM devices which don't take write
permissions, but may be backed by a writable image file. scsi-cd allows
write requests to the drive if blk_is_read_only() returns false.
However, the write request will immediately run into an assertion
failure because the write permission is missing.

This patch introduces separate functions for both questions.
blk_supports_write_perm() answers the question whether the block
node/image file can support writable devices, whereas blk_is_writable()
tells whether the BlockBackend is currently configured to be writable.

All calls of blk_is_read_only() are converted to one of the two new
functions.

Fixes: https://bugs.launchpad.net/bugs/1906693
Cc: qemu-sta...@nongnu.org
Signed-off-by: Kevin Wolf 
---
 include/sysemu/block-backend.h |  3 ++-
 block/block-backend.c  | 19 ---
 hw/block/dataplane/xen-block.c |  2 +-
 hw/block/fdc.c |  9 +
 hw/block/m25p80.c  |  6 +++---
 hw/block/nand.c|  2 +-
 hw/block/nvme-ns.c |  7 ---
 hw/block/onenand.c |  2 +-
 hw/block/pflash_cfi01.c|  2 +-
 hw/block/pflash_cfi02.c|  2 +-
 hw/block/swim.c|  6 +++---
 hw/block/virtio-blk.c  |  6 +++---
 hw/block/xen-block.c   |  2 +-
 hw/ide/core.c  |  2 +-
 hw/misc/sifive_u_otp.c |  2 +-
 hw/ppc/pnv_pnor.c  |  2 +-
 hw/scsi/scsi-disk.c| 10 +-
 hw/scsi/scsi-generic.c |  4 ++--
 hw/sd/sd.c |  6 +++---
 hw/usb/dev-storage.c   |  4 ++--
 20 files changed, 57 insertions(+), 41 deletions(-)

diff --git a/include/sysemu/block-backend.h b/include/sysemu/block-backend.h
index 8203d7f6f9..880e903293 100644
--- a/include/sysemu/block-backend.h
+++ b/include/sysemu/block-backend.h
@@ -191,7 +191,8 @@ BlockErrorAction blk_get_error_action(BlockBackend *blk, 
bool is_read,
   int error);
 void blk_error_action(BlockBackend *blk, BlockErrorAction action,
   bool is_read, int error);
-bool blk_is_read_only(BlockBackend *blk);
+bool blk_supports_write_perm(BlockBackend *blk);
+bool blk_is_writable(BlockBackend *blk);
 bool blk_is_sg(BlockBackend *blk);
 bool blk_enable_write_cache(BlockBackend *blk);
 void blk_set_enable_write_cache(BlockBackend *blk, bool wce);
diff --git a/block/block-backend.c b/block/block-backend.c
index ce78d30794..e493f17515 100644
--- a/block/block-backend.c
+++ b/block/block-backend.c
@@ -1826,17 +1826,30 @@ void blk_error_action(BlockBackend *blk, 
BlockErrorAction action,
 }
 }
 
-bool blk_is_read_only(BlockBackend *blk)
+/*
+ * Returns true if the BlockBackend can support taking write permissions
+ * (because its root node is not read-only).
+ */
+bool blk_supports_write_perm(BlockBackend *blk)
 {
 BlockDriverState *bs = blk_bs(blk);
 
 if (bs) {
-return bdrv_is_read_only(bs);
+return !bdrv_is_read_only(bs);
 } else {
-return blk->root_state.read_only;
+return !blk->root_state.read_only;
 }
 }
 
+/*
+ * Returns true if the BlockBackend can be written to in its current
+ * configuration (i.e. if write permission have been requested)
+ */
+bool blk_is_writable(BlockBackend *blk)
+{
+return blk->perm & BLK_PERM_WRITE;
+}
+
 bool blk_is_sg(BlockBackend *blk)
 {
 BlockDriverState *bs = blk_bs(blk);
diff --git a/hw/block/dataplane/xen-block.c b/hw/block/dataplane/xen-block.c
index 71c337c7b7..f5b4f4c079 100644
--- a/hw/block/dataplane/xen-block.c
+++ b/hw/block/dataplane/xen-block.c
@@ -168,7 +168,7 @@ static int xen_block_parse_request(XenBlockRequest *request)
 };
 
 if (request->req.operation != BLKIF_OP_READ &&
-blk_is_read_only(dataplane->blk)) {
+!blk_is_writable(dataplane->blk)) {
 error_report("error: write req for ro device");
 goto err;
 }
diff --git a/hw/block/fdc.c b/hw/block/fdc.c
index 3636874432..292ea87805 100644
--- a/hw/block/fdc.c
+++ b/hw/block/fdc.c
@@ -444,7 +444,7 @@ static void fd_revalidate(FDrive *drv)
 
 FLOPPY_DPRINTF("revalidate\n");
 if (drv->blk != NULL) {
-drv->ro = blk_is_read_only(drv->blk);
+drv->ro = !blk_is_writable(drv->blk);
 if (!blk_is_inserted(drv->blk)) {
 FLOPPY_DPRINTF("No disk in drive\n");
 drv->disk = FLOPPY_DRIVE_TYPE_NONE;
@@ -479,8 +479,8 @@ static void fd_change_cb(void *opaque, bool load, Error 
**errp)
 blk_set_perm(drive->blk, 0, BLK_PERM_ALL, &error_abort);
 } else {
 if (!blkconf_apply_backend_options(drive->conf,
-