block: drop BlockDriverState::read_only
cover block: drop BlockDriverState::read_only
Hi all!
The field duplicates information in .open_flags. We have to carefully sync these two fields everywhere. It's simple to introduce a bug by forgetting it.
Let's drop the field, and fix users to call bdrv_is_read_only() and bdrv_is_writable() instead.
Vladimir Sementsov-Ogievskiy (3):
block: consistently use bdrv_is_read_only()
block: drop BlockDriverState::read_only
block: drop BlockBackendRootState::read_only
include/block/block_int.h | 2 -- block.c | 16 +++++++--------- block/block-backend.c | 10 ++-------- block/commit.c | 2 +- block/io.c | 4 ++-- block/qapi.c | 2 +- block/qcow2-snapshot.c | 2 +- block/qcow2.c | 4 ++-- block/snapshot.c | 2 +- block/vhdx-log.c | 2 +- blockdev.c | 4 +++- tests/unit/test-block-iothread.c | 6 ------ 12 files changed, 21 insertions(+), 35 deletions(-)
Signature · 1 line
2.29.2
PATCH 1/3 block: consistently use bdrv_is_read_only()
It's better to use accessor function instead of bs->read_only directly. In some places use bdrv_is_writable() instead of
checking both BDRV_O_RDWR set and BDRV_O_INACTIVE not set.
In bdrv_open_common() it's a bit strange to add one more variable, but we are going to drop bs->read_only in the next patch, so new ro local variable substitutes it here.
--- block.c | 11 +++++++---- block/block-backend.c | 2 +- block/commit.c | 2 +- block/io.c | 4 ++-- block/qapi.c | 2 +- block/qcow2-snapshot.c | 2 +- block/qcow2.c | 4 ++-- block/snapshot.c | 2 +- block/vhdx-log.c | 2 +- 9 files changed, 17 insertions(+), 14 deletions(-)
block.c +7 -4
@@ -1720,6 +1720,7 @@ static int bdrv_open_common(BlockDriverState *bs, BlockBackend *file,QemuOpts *opts;BlockDriver *drv;Error *local_err =NULL ; +bool ro;assert (bs->file ==NULL );assert (options !=NULL && bs->options != options);
@@ -1772,15 +1773,17 @@ static int bdrv_open_common(BlockDriverState *bs, BlockBackend *file, bs->read_only = !(bs->open_flags &BDRV_O_RDWR ); -if (use_bdrv_whitelist && !bdrv_is_whitelisted (drv, bs->read_only)) { -if (!bs->read_only &&bdrv_is_whitelisted (drv, true)) { + ro =bdrv_is_read_only (bs); + +if (use_bdrv_whitelist && !bdrv_is_whitelisted (drv, ro)) { +if (!ro &&bdrv_is_whitelisted (drv, true)) { ret =bdrv_apply_auto_read_only (bs,NULL ,NULL ); }else { ret = -ENOTSUP ; }if (ret <0 ) {error_setg (errp, - !bs->read_only &&bdrv_is_whitelisted (drv, true) + !ro &&bdrv_is_whitelisted (drv, true) ?"Driver '%s' can only be used for read-only devices" :"Driver '%s' is not whitelisted" , drv->format_name);
@@ -1792,7 +1795,7 @@ static int bdrv_open_common(BlockDriverState *bs, BlockBackend *file,assert (qatomic_read (&bs->copy_on_read) ==0 );if (bs->open_flags &BDRV_O_COPY_ON_READ ) { -if (!bs->read_only) { +if (!ro) {bdrv_enable_copy_on_read (bs); }else {error_setg (errp,"Can't use copy-on-read on read-only device" );
block/block-backend.c +1 -1
@@ -2269,7 +2269,7 @@ void blk_update_root_state(BlockBackend *blk)assert (blk->root); blk->root_state.open_flags = blk->root->bs->open_flags; - blk->root_state.read_only = blk->root->bs->read_only; + blk->root_state.read_only =bdrv_is_read_only (blk->root->bs); blk->root_state.detect_zeroes = blk->root->bs->detect_zeroes; }
block/commit.c +1 -1
@@ -453,7 +453,7 @@ int bdrv_commit(BlockDriverState *bs)return -EBUSY ; } - ro = backing_file_bs->read_only; + ro =bdrv_is_read_only (backing_file_bs);if (ro) {if (bdrv_reopen_set_read_only (backing_file_bs, false,NULL )) {
block/io.c +2 -2
@@ -1973,7 +1973,7 @@ bdrv_co_write_req_prepare(BdrvChild *child, int64_t offset, int64_t bytes,bdrv_check_request (offset, bytes, &error_abort); -if (bs->read_only) { +if (bdrv_is_read_only (bs)) {return -EPERM ; }
@@ -3406,7 +3406,7 @@ int coroutine_fn bdrv_co_truncate(BdrvChild *child, int64_t offset, bool exact,if (new_bytes) {bdrv_make_request_serialising (&req,1 ); } -if (bs->read_only) { +if (bdrv_is_read_only (bs)) {error_setg (errp,"Image is read-only" ); ret = -EACCES ; goto out;
block/qapi.c +1 -1
@@ -59,7 +59,7 @@ BlockDeviceInfo *bdrv_block_device_info(BlockBackend *blk, info =g_malloc0 (sizeof (*info)); info->file =g_strdup (bs->filename); - info->ro = bs->read_only; + info->ro =bdrv_is_read_only (bs); info->drv =g_strdup (bs->drv->format_name); info->encrypted = bs->encrypted;
block/qcow2-snapshot.c +1 -1
@@ -1026,7 +1026,7 @@ int qcow2_snapshot_load_tmp(BlockDriverState *bs,int new_l1_bytes;int ret; -assert (bs->read_only); +assert (bdrv_is_read_only (bs));/* Search the snapshot */ snapshot_index =find_snapshot_by_id_and_name (bs, snapshot_id, name);
block/qcow2.c +2 -2
@@ -1724,7 +1724,7 @@ static int coroutine_fn qcow2_do_open(BlockDriverState *bs, QDict *options,/* Clear unknown autoclear feature bits */ update_header |= s->autoclear_features & ~QCOW2_AUTOCLEAR_MASK ; update_header = - update_header && !bs->read_only && !(flags &BDRV_O_INACTIVE ); + update_header &&bdrv_is_writable (bs);if (update_header) { s->autoclear_features &=QCOW2_AUTOCLEAR_MASK ; }
@@ -1811,7 +1811,7 @@ static int coroutine_fn qcow2_do_open(BlockDriverState *bs, QDict *options, bs->supported_truncate_flags =BDRV_REQ_ZERO_WRITE ;/* Repair image if dirty */ -if (!(flags & (BDRV_O_CHECK |BDRV_O_INACTIVE )) && !bs->read_only && +if (!(flags &BDRV_O_CHECK ) &&bdrv_is_writable (bs) && (s->incompatible_features &QCOW2_INCOMPAT_DIRTY )) {BdrvCheckResult result = {0 };
block/snapshot.c +1 -1
@@ -415,7 +415,7 @@ int bdrv_snapshot_load_tmp(BlockDriverState *bs,error_setg (errp,"snapshot_id and name are both NULL" );return -EINVAL ; } -if (!bs->read_only) { +if (!bdrv_is_read_only (bs)) {error_setg (errp,"Device is not readonly" );return -EINVAL ; }
block/vhdx-log.c +1 -1
@@ -801,7 +801,7 @@ int vhdx_parse_log(BlockDriverState *bs, BDRVVHDXState *s, bool *flushed, }if (logs.valid) { -if (bs->read_only) { +if (bdrv_is_read_only (bs)) {bdrv_refresh_filename (bs); ret = -EPERM ; error_setg(errp,
Signature · 1 line
2.29.2
Re: [PATCH 1/3] block: consistently use bdrv_is_read_only()
Am 26.05.2021 um 23:15 hat Vladimir Sementsov-Ogievskiy geschrieben:
Quoted 15 lines
It's better to use accessor function instead of
bs->read_onlydirectly. In some places usebdrv_is_writable()instead of
checking bothBDRV_O_RDWRset andBDRV_O_INACTIVEnot set.In
bdrv_open_common()it's a bit strange to add one more variable, but we are going to dropbs->read_onlyin the next patch, so new ro local variable substitutes it here.Signed-off-by Vladimir Sementsov-Ogievskiy <[email protected]>block/qcow2.c +0
-0
block/qcow2.c static int coroutine_fn qcow2_do_open(BlockDriverState *bs, QDict *options, · line 1727
Quoted 5 lines
block/qcow2.c +1
-1@@ -1724,7 +1724,7 @@ static int coroutine_fn qcow2_do_open(BlockDriverState *bs, QDict *options,/* Clear unknown autoclear feature bits */ update_header |= s->autoclear_features & ~QCOW2_AUTOCLEAR_MASK ; update_header = - update_header && !bs->read_only && !(flags &BDRV_O_INACTIVE ); + update_header &&bdrv_is_writable (bs);
This statement fits on a single line now.
Kevin
PATCH 2/3 block: drop BlockDriverState::read_only
In reply to Vladimir Sementsov-OgievskiyThis variable is just a cache for !(bs->open_flags & BDRV_O_RDWR), which we have to synchronize everywhere. Let's just drop it and consistently use bdrv_is_read_only().
--- include/block/block_int.h | 1 - block.c | 7 +------ tests/unit/test-block-iothread.c | 6 ------ 3 files changed, 1 insertion(+), 13 deletions(-)
include/block/block_int.h +0 -1
@@ -843,7 +843,6 @@ struct BlockDriverState {* locking needed during I/O... */ int open_flags;/* flags used to open the file, re-used for re-open */ -bool read_only;/* if true, the media is read only */ bool encrypted;/* if true, the media is encrypted */ bool sg;/* if true, the device is a /dev/sg* */ bool probed;/* if true, format was probed rather than specified */
block.c +1 -6
@@ -265,7 +265,7 @@ void bdrv_parse_filename_strip_prefix(const char *filename, const char *prefix,* image is inactivated. */ bool bdrv_is_read_only (BlockDriverState *bs) { -return bs->read_only; +return !(bs->open_flags &BDRV_O_RDWR ); } int bdrv_can_set_read_only(BlockDriverState *bs, bool read_only,
@@ -317,7 +317,6 @@ int bdrv_apply_auto_read_only(BlockDriverState *bs, const char *errmsg, goto fail; } - bs->read_only = true; bs->open_flags &= ~BDRV_O_RDWR ;return 0 ;
@@ -1549,7 +1548,6 @@ static int bdrv_open_driver(BlockDriverState *bs, BlockDriver *drv, } bs->drv = drv; - bs->read_only = !(bs->open_flags &BDRV_O_RDWR ); bs->opaque =g_malloc0 (drv->instance_size);if (drv->bdrv_file_open) {
@@ -1771,8 +1769,6 @@ static int bdrv_open_common(BlockDriverState *bs, BlockBackend *file,trace_bdrv_open_common (bs, filename ?:"" , bs->open_flags, drv->format_name); - bs->read_only = !(bs->open_flags &BDRV_O_RDWR ); - ro =bdrv_is_read_only (bs);if (use_bdrv_whitelist && !bdrv_is_whitelisted (drv, ro)) {
@@ -4548,7 +4544,6 @@ static void bdrv_reopen_commit(BDRVReopenState *reopen_state) bs->explicit_options = reopen_state->explicit_options; bs->options = reopen_state->options; bs->open_flags = reopen_state->flags; - bs->read_only = !(reopen_state->flags &BDRV_O_RDWR ); bs->detect_zeroes = reopen_state->detect_zeroes;if (reopen_state->replace_backing_bs) {
tests/unit/test-block-iothread.c +0 -6
@@ -194,13 +194,11 @@ static void test_sync_op_truncate(BdrvChild *c) g_assert_cmpint(ret, ==, -EINVAL);/* Error: Read-only image */ - c->bs->read_only = true; c->bs->open_flags &= ~BDRV_O_RDWR ; ret =bdrv_truncate (c,65536 , false,PREALLOC_MODE_OFF ,0 ,NULL ); g_assert_cmpint(ret, ==, -EACCES); - c->bs->read_only = false; c->bs->open_flags |=BDRV_O_RDWR ; }
@@ -236,13 +234,11 @@ static void test_sync_op_flush(BdrvChild *c) g_assert_cmpint(ret, ==, 0);/* Early success: Read-only image */ - c->bs->read_only = true; c->bs->open_flags &= ~BDRV_O_RDWR ; ret =bdrv_flush (c->bs); g_assert_cmpint(ret, ==, 0); - c->bs->read_only = false; c->bs->open_flags |=BDRV_O_RDWR ; }
@@ -256,13 +252,11 @@ static void test_sync_op_blk_flush(BlockBackend *blk) g_assert_cmpint(ret, ==, 0);/* Early success: Read-only image */ - bs->read_only = true; bs->open_flags &= ~BDRV_O_RDWR ; ret =blk_flush (blk); g_assert_cmpint(ret, ==, 0); - bs->read_only = false; bs->open_flags |=BDRV_O_RDWR ; }
Signature · 1 line
2.29.2
PATCH 3/3 block: drop BlockBackendRootState::read_only
In reply to Vladimir Sementsov-OgievskiyInstead of keeping additional boolean field, let's store the information in BDRV_O_RDWR bit of BlockBackendRootState::open_flags.
--- include/block/block_int.h | 1 - block/block-backend.c | 10 ++-------- blockdev.c | 4 +++- 3 files changed, 5 insertions(+), 10 deletions(-)
include/block/block_int.h +0 -1
@@ -1007,7 +1007,6 @@ struct BlockDriverState {struct BlockBackendRootState {int open_flags; -bool read_only;BlockdevDetectZeroesOptions detect_zeroes; };
block/block-backend.c +2 -8
@@ -1852,7 +1852,7 @@ bool blk_supports_write_perm(BlockBackend *blk)if (bs) {return !bdrv_is_read_only (bs); }else { -return !blk->root_state.read_only; +return blk->root_state.open_flags &BDRV_O_RDWR ; } }
@@ -2269,7 +2269,6 @@ void blk_update_root_state(BlockBackend *blk)assert (blk->root); blk->root_state.open_flags = blk->root->bs->open_flags; - blk->root_state.read_only =bdrv_is_read_only (blk->root->bs); blk->root_state.detect_zeroes = blk->root->bs->detect_zeroes; }
@@ -2288,12 +2287,7 @@ bool blk_get_detect_zeroes_from_root_state(BlockBackend *blk)*/ int blk_get_open_flags_from_root_state (BlockBackend *blk) { -int bs_flags; - - bs_flags = blk->root_state.read_only ?0 :BDRV_O_RDWR ; - bs_flags |= blk->root_state.open_flags & ~BDRV_O_RDWR ; - -return bs_flags; +return blk->root_state.open_flags; }BlockBackendRootState *blk_get_root_state (BlockBackend *blk)
blockdev.c +3 -1
@@ -576,6 +576,9 @@ static BlockBackend *blockdev_init(const char *file, QDict *bs_opts, } read_only =qemu_opt_get_bool (opts,BDRV_OPT_READ_ONLY , false); +if (!read_only) { + bdrv_flags |=BDRV_O_RDWR ; + }/* init */ if ((!file || !*file) && !qdict_size (bs_opts)) {
@@ -584,7 +587,6 @@ static BlockBackend *blockdev_init(const char *file, QDict *bs_opts, blk =blk_new (qemu_get_aio_context (),0 ,BLK_PERM_ALL ); blk_rs =blk_get_root_state (blk); blk_rs->open_flags = bdrv_flags; - blk_rs->read_only = read_only; blk_rs->detect_zeroes = detect_zeroes;qobject_unref (bs_opts);
Signature · 1 line
2.29.2
Re: [PATCH 3/3] block: drop BlockBackendRootState::read_only
Am 26.05.2021 um 23:15 hat Vladimir Sementsov-Ogievskiy geschrieben:
Quoted 20 lines
Instead of keeping additional boolean field, let's store the information in
BDRV_O_RDWRbit ofBlockBackendRootState::open_flags.Signed-off-by Vladimir Sementsov-Ogievskiy <[email protected]>blockdev.c +3
-0@@ -576,6 +576,9 @@ static BlockBackend *blockdev_init(const char *file, QDict *bs_opts, } read_only =qemu_opt_get_bool (opts,BDRV_OPT_READ_ONLY , false); +if (!read_only) { + bdrv_flags |=BDRV_O_RDWR ; + }/* init */ if ((!file || !*file) && !qdict_size (bs_opts)) {
blockdev.c static BlockBackend *blockdev_init(const char *file, QDict *bs_opts, · line 592
Quoted 7 lines
blockdev.c +0
-1@@ -584,7 +587,6 @@ static BlockBackend *blockdev_init(const char *file, QDict *bs_opts, blk =blk_new (qemu_get_aio_context (),0 ,BLK_PERM_ALL ); blk_rs =blk_get_root_state (blk); blk_rs->open_flags = bdrv_flags; - blk_rs->read_only = read_only; blk_rs->detect_zeroes = detect_zeroes;qobject_unref (bs_opts);
That we're now using different flags for blk_new_open() looked a bit suspicious, though the new state intuitively makes more sense than the old one.
Anyway, this patch breaks ahci-test:
# starting QEMU: exec build/x86_64-softmmu/qemu-system-x86_64 -qtest unix:/tmp/qtest-54104.sock -qtest-log /dev/null -chardev socket,path=/tmp/qtest-54104.qmp,id=char0 -mon chardev=char0,mode=control -display none -drive file=/tmp/qtest.mGtxeg,if=none,id=drive0,cache=writeback,rerror=stop,werror=stop,format=raw -M q35 -device ide-hd,drive=drive0 -incoming unix:/tmp/qtest-migration.XVRZZi -accel qtest qemu-system-x86_64: -drive file=/tmp/qtest.mGtxeg,if=none,id=drive0,cache=writeback,rerror=stop,werror=stop,format=raw: Block node is read-only
Kevin
Re: [PATCH 3/3] block: drop BlockBackendRootState::read_only
Quoted 33 lines · Kevin Wolf
Am 26.05.2021 um 23:15 hat Vladimir Sementsov-Ogievskiy geschrieben:
Quoted 20 lines
Instead of keeping additional boolean field, let's store the information in
BDRV_O_RDWRbit ofBlockBackendRootState::open_flags.Signed-off-by Vladimir Sementsov-Ogievskiy <[email protected]>blockdev.c +3
-0@@ -576,6 +576,9 @@ static BlockBackend *blockdev_init(const char *file, QDict *bs_opts, } read_only =qemu_opt_get_bool (opts,BDRV_OPT_READ_ONLY , false); +if (!read_only) { + bdrv_flags |=BDRV_O_RDWR ; + }/* init */ if ((!file || !*file) && !qdict_size (bs_opts)) {blockdev.c static BlockBackend *blockdev_init(const char *file, QDict *bs_opts, · line 592
Quoted 7 lines
blockdev.c +0
-1@@ -584,7 +587,6 @@ static BlockBackend *blockdev_init(const char *file, QDict *bs_opts, blk =blk_new (qemu_get_aio_context (),0 ,BLK_PERM_ALL ); blk_rs =blk_get_root_state (blk); blk_rs->open_flags = bdrv_flags; - blk_rs->read_only = read_only; blk_rs->detect_zeroes = detect_zeroes;qobject_unref (bs_opts);That we're now using different flags for
blk_new_open()looked a bit suspicious, though the new state intuitively makes more sense than the old one.
Quoted 5 lines
Anyway, this patch breaks ahci-test:
# starting QEMU: exec build/x86_64-softmmu/qemu-system-x86_64 -qtest unix:/tmp/qtest-54104.sock -qtest-log /dev/null -chardev socket,path=/tmp/qtest-54104.qmp,id=char0 -mon chardev=char0,mode=control -display none -drive file=/tmp/qtest.mGtxeg,if=none,id=drive0,cache=writeback,rerror=stop,werror=stop,format=raw -M q35 -device ide-hd,drive=drive0 -incoming unix:/tmp/qtest-migration.XVRZZi -accel qtest qemu-system-x86_64: -drive file=/tmp/qtest.mGtxeg,if=none,id=drive0,cache=writeback,rerror=stop,werror=stop,format=raw: Block node is read-only
Interesting (me again not running make check, sorry for this :\. And that's fixed by
blockdev.c +1 -1
@@ -609,7 +609,7 @@ static BlockBackend *blockdev_init(const char *file, QDict *bs_opts, bdrv_flags |=BDRV_O_INACTIVE ; } - blk =blk_new_open (file,NULL , bs_opts, bdrv_flags, errp); + blk =blk_new_open (file,NULL , bs_opts, bdrv_flags & ~BDRV_O_RDWR , errp);if (!blk) { goto err_no_bs_opts; }
so to make a RW node we should not pass both option and flag ??
Most probably that's because in blk_new_open we do
if (flags & BDRV_O_RDWR) {
perm |= BLK_PERM_WRITE;
}Probably this should be refactored somehow, as it's strange that we tread read-only option and O_RDWR flags in a different ways.. Still not here. I'll resend so that this patch modifies only BlockBackendRootState, not the logic of blk_new_open() call.
Signature · 2 lines
Best regards, Vladimir