From 86b1cf322789b79c8ace977430ac6a443d491cc0 Mon Sep 17 00:00:00 2001 From: Kevin Wolf Date: Mon, 18 Jan 2021 13:34:47 +0100 Subject: block: Separate blk_is_writable() and blk_supports_write_perm() 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-stable@nongnu.org Signed-off-by: Kevin Wolf Message-Id: <20210118123448.307825-2-kwolf@redhat.com> Reviewed-by: Philippe Mathieu-Daudé Reviewed-by: Max Reitz Signed-off-by: Kevin Wolf --- block/block-backend.c | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) (limited to 'block') 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); -- cgit v1.2.3-55-g7522