block: cleanup backing and file handling
cover block: cleanup backing and file handling
Hi all!
I started this as a follow-up to
"block: Attempt on fixing 030-reported errors" by Hanna.
In Hanna's series I really like how bs->children handling moved to .attach/.detach handlers.
.file and .backing are kind of "shortcuts" or "links" to some elementes of this list, they duplicate the information. So they should be updated in the same place to be in sync.
On the way to this target, I do also the following cleanups:
- establish, which restrictions we have on how much children of different roles should node has, and which of the should be linked in .file / .backing. Add documentation and assertions.
- drop all the complicated logic around passing pointer to bs->backing / bs->file (BdrvChild **c), so that the field be automatically updated. Now they are natively automatically updated in .attach/.detach, so the rest of the code becomes simpler.
- now .file / .backing are updated ONLY in .attach / .detach, no other code modify these fields
Vladimir Sementsov-Ogievskiy (14):
block: BlockDriver: add .filtered_child_is_backing field
block: introduce bdrv_open_file_child() helper block/blklogwrites: don't care to remove bs->file child on failure
test-bdrv-graph-mod: update test_parallel_perm_update test case
tests-bdrv-drain: bdrv_replace_test driver: declare supports_backing
test-bdrv-graph-mod: fix filters to be filters
block: document connection between child roles and
bs->backing/bs->file
block/snapshot: stress that we fallback to primary child Revert "block: Let replace_child_noperm free children" Revert "block: Let replace_child_tran keep indirect pointer" Revert "block: Restructure remove_file_or_backing_child()" Revert "block: Pass BdrvChild ** to replace_child_noperm"
block: Manipulate bs->file / bs->backing pointers in .attach/.detach block/snapshot: drop indirection around bdrv_snapshot_fallback_ptr
include/block/block.h | 45 +++++ include/block/block_int.h | 30 ++- block.c | 335 ++++++++++--------------------- block/blkdebug.c | 9 +- block/blklogwrites.c | 11 +- block/blkreplay.c | 7 +- block/blkverify.c | 9 +- block/bochs.c | 7 +- block/cloop.c | 7 +- block/commit.c | 1 + block/copy-before-write.c | 9 +- block/copy-on-read.c | 9 +- block/crypto.c | 11 +- block/dmg.c | 7 +- block/filter-compress.c | 6 +- block/mirror.c | 1 + block/parallels.c | 7 +- block/preallocate.c | 9 +- block/qcow.c | 6 +- block/qcow2.c | 8 +- block/qed.c | 8 +- block/raw-format.c | 4 +- block/replication.c | 8 +- block/snapshot.c | 60 ++---- block/throttle.c | 8 +- block/vdi.c | 7 +- block/vhdx.c | 7 +- block/vmdk.c | 7 +- block/vpc.c | 7 +- tests/unit/test-bdrv-drain.c | 11 +- tests/unit/test-bdrv-graph-mod.c | 94 ++++++--- 31 files changed, 343 insertions(+), 412 deletions(-)
Signature · 1 line
2.31.1
PATCH 1/14 block: BlockDriver: add .filtered_child_is_backing field
Unfortunately not all filters use .file child as filtered child. Two exclusions are mirror_top and commit_top. Happily they both are private filters. Bad thing is that this inconsistency is observable through qmp commands query-block / query-named-block-nodes. So, could we just change mirror_top and commit_top to use file child as all other filter driver is an open question. Probably, we could do that with some kind of deprecation period, but how to warn users during it?
For now, let's just add a field so we can distinguish them in generic code, it will be used in further commits.
--- include/block/block_int.h | 14 ++++++++++++++ block/commit.c | 1 + block/mirror.c | 1 + 3 files changed, 16 insertions(+)
include/block/block_int.h +14 -0
@@ -117,6 +117,20 @@ struct BlockDriver {* (And this filtered child must then be bs->file or bs->backing.) */ bool is_filter; + +/* +* Only make sense for filter drivers, for others must be false. +* If true, filtered child is bs->backing. Otherwise it's bs->file. +* Only two internal filters use bs->backing as filtered child and has this +* field set to true: mirror_top and commit_top. +* +* Never create any more such filters! +* +* TODO: imagine how to deprecate this behavior and make all filters work +* similarly using bs->file as filtered child. +*/ +bool filtered_child_is_backing; +/* * Set to true if the BlockDriver is a format driver. Format nodes * generally do not expect their children to be other format nodes
block/commit.c +1 -0
@@ -237,6 +237,7 @@ static BlockDriver bdrv_commit_top = { .bdrv_child_perm = bdrv_commit_top_child_perm, .is_filter = true, + .filtered_child_is_backing = true, }; void commit_start(const char *job_id, BlockDriverState *bs,
block/mirror.c +1 -0
@@ -1587,6 +1587,7 @@ static BlockDriver bdrv_mirror_top = { .bdrv_child_perm = bdrv_mirror_top_child_perm, .is_filter = true, + .filtered_child_is_backing = true, }; static BlockJob *mirror_start_job(
Signature · 1 line
2.31.1
PATCH 2/14 block: introduce bdrv_open_file_child() helper
In reply to Vladimir Sementsov-OgievskiyAlmost all drivers call bdrv_open_child() similarly. Let's create a helper for this.
The only not updated driver that call bdrv_open_child() to set bs->file is raw-format, as it sometimes want to have filtered child but don't set drv->is_filter to true.
Possibly we should implement drv->is_filter_func() handler, to consider raw-format as filter when it works as filter.. But it's another story.
Note also, that we decrease assignments to bs->file in code: it helps us restrict modifying this field in further commit.
--- include/block/block.h | 3 +++ block.c | 21 +++++++++++++++++++++ block/blkdebug.c | 9 +++------ block/blklogwrites.c | 7 ++----- block/blkreplay.c | 7 ++----- block/blkverify.c | 9 +++------ block/bochs.c | 7 +++---- block/cloop.c | 7 +++---- block/copy-before-write.c | 9 ++++----- block/copy-on-read.c | 9 ++++----- block/crypto.c | 11 ++++++----- block/dmg.c | 7 +++---- block/filter-compress.c | 6 ++---- block/parallels.c | 7 +++---- block/preallocate.c | 9 ++++----- block/qcow.c | 6 ++---- block/qcow2.c | 8 ++++---- block/qed.c | 8 ++++---- block/replication.c | 8 +++----- block/throttle.c | 8 +++----- block/vdi.c | 7 +++---- block/vhdx.c | 7 +++---- block/vmdk.c | 7 +++---- block/vpc.c | 7 +++---- 24 files changed, 94 insertions(+), 100 deletions(-)
include/block/block.h +3 -0
@@ -376,6 +376,9 @@ BdrvChild *bdrv_open_child(const char *filename,const BdrvChildClass *child_class, BdrvChildRole child_role, bool allow_none, Error **errp); +int bdrv_open_file_child (const char *filename, +QDict *options,const char *bdref_key, +BlockDriverState *parent,Error **errp);BlockDriverState *bdrv_open_blockdev_ref (BlockdevRef *ref,Error **errp);int bdrv_set_backing_hd (BlockDriverState *bs,BlockDriverState *backing_hd,Error **errp);
block.c +21 -0
@@ -3546,6 +3546,27 @@ BdrvChild *bdrv_open_child(const char *filename, errp); } +/* +* Wrapper on bdrv_open_child() for most popular case: open primary child of bs. +*/ +int bdrv_open_file_child (const char *filename, +QDict *options,const char *bdref_key, +BlockDriverState *parent,Error **errp) +{ +BdrvChildRole role; + +/* commit_top and mirror_top don't use this function */ +assert (!parent->drv->filtered_child_is_backing); + + role = parent->drv->is_filter ? + (BDRV_CHILD_FILTERED |BDRV_CHILD_PRIMARY ) :BDRV_CHILD_IMAGE ; + + parent->file =bdrv_open_child (filename, options, bdref_key, parent, + &child_of_bds, role, false, errp); + +return parent->file ?0 : -EINVAL ; +} +/* * TODO Future callers may need to specify parent/child_class in order for * option inheritance to work. Existing callers use it for the root node.
block/blkdebug.c +3 -6
@@ -503,12 +503,9 @@ static int blkdebug_open(BlockDriverState *bs, QDict *options, int flags, }/* Open the image file */ - bs->file =bdrv_open_child (qemu_opt_get (opts,"x-image" ), options,"image" , - bs, &child_of_bds, -BDRV_CHILD_FILTERED |BDRV_CHILD_PRIMARY , - false, errp); -if (!bs->file) { - ret = -EINVAL ; + ret =bdrv_open_file_child (qemu_opt_get (opts,"x-image" ), options,"image" , + bs, errp); +if (ret <0 ) { goto out; }
block/blklogwrites.c +2 -5
@@ -155,11 +155,8 @@ static int blk_log_writes_open(BlockDriverState *bs, QDict *options, int flags, }/* Open the file */ - bs->file =bdrv_open_child (NULL , options,"file" , bs, &child_of_bds, -BDRV_CHILD_FILTERED |BDRV_CHILD_PRIMARY , false, - errp); -if (!bs->file) { - ret = -EINVAL ; + ret =bdrv_open_file_child (NULL , options,"file" , bs, errp); +if (ret <0 ) { goto fail; }
block/blkreplay.c +2 -5
@@ -26,11 +26,8 @@ static int blkreplay_open(BlockDriverState *bs, QDict *options, int flags,int ret;/* Open the image file */ - bs->file =bdrv_open_child (NULL , options,"image" , bs, &child_of_bds, -BDRV_CHILD_FILTERED |BDRV_CHILD_PRIMARY , - false, errp); -if (!bs->file) { - ret = -EINVAL ; + ret =bdrv_open_file_child (NULL , options,"image" , bs, errp); +if (ret <0 ) { goto fail; }
block/blkverify.c +3 -6
@@ -121,12 +121,9 @@ static int blkverify_open(BlockDriverState *bs, QDict *options, int flags, }/* Open the raw file */ - bs->file =bdrv_open_child (qemu_opt_get (opts,"x-raw" ), options,"raw" , - bs, &child_of_bds, -BDRV_CHILD_FILTERED |BDRV_CHILD_PRIMARY , - false, errp); -if (!bs->file) { - ret = -EINVAL ; + ret =bdrv_open_file_child (qemu_opt_get (opts,"x-raw" ), options,"raw" , + bs, errp); +if (ret <0 ) { goto fail; }
block/bochs.c +3 -4
@@ -110,10 +110,9 @@ static int bochs_open(BlockDriverState *bs, QDict *options, int flags,return ret; } - bs->file =bdrv_open_child (NULL , options,"file" , bs, &child_of_bds, -BDRV_CHILD_IMAGE , false, errp); -if (!bs->file) { -return -EINVAL ; + ret =bdrv_open_file_child (NULL , options,"file" , bs, errp); +if (ret <0 ) { +return ret; } ret =bdrv_pread (bs->file,0 , &bochs,sizeof (bochs));
block/cloop.c +3 -4
@@ -71,10 +71,9 @@ static int cloop_open(BlockDriverState *bs, QDict *options, int flags,return ret; } - bs->file =bdrv_open_child (NULL , options,"file" , bs, &child_of_bds, -BDRV_CHILD_IMAGE , false, errp); -if (!bs->file) { -return -EINVAL ; + ret =bdrv_open_file_child (NULL , options,"file" , bs, errp); +if (ret <0 ) { +return ret; }/* read header */
block/copy-before-write.c +4 -5
@@ -150,12 +150,11 @@ static int cbw_open(BlockDriverState *bs, QDict *options, int flags, {BDRVCopyBeforeWriteState *s = bs->opaque;BdrvDirtyBitmap *copy_bitmap; +int ret; - bs->file =bdrv_open_child (NULL , options,"file" , bs, &child_of_bds, -BDRV_CHILD_FILTERED |BDRV_CHILD_PRIMARY , - false, errp); -if (!bs->file) { -return -EINVAL ; + ret =bdrv_open_file_child (NULL , options,"file" , bs, errp); +if (ret <0 ) { +return ret; } s->target = bdrv_open_child(NULL, options, "target", bs, &child_of_bds,
block/copy-on-read.c +4 -5
@@ -41,12 +41,11 @@ static int cor_open(BlockDriverState *bs, QDict *options, int flags,BDRVStateCOR *state = bs->opaque;/* Find a bottom node name, if any */ const char *bottom_node =qdict_get_try_str (options,"bottom" ); +int ret; - bs->file =bdrv_open_child (NULL , options,"file" , bs, &child_of_bds, -BDRV_CHILD_FILTERED |BDRV_CHILD_PRIMARY , - false, errp); -if (!bs->file) { -return -EINVAL ; + ret =bdrv_open_file_child (NULL , options,"file" , bs, errp); +if (ret <0 ) { +return ret; } bs->supported_read_flags =BDRV_REQ_PREFETCH ;
block/crypto.c +6 -5
@@ -260,15 +260,14 @@ static int block_crypto_open_generic(QCryptoBlockFormat format, {BlockCrypto *crypto = bs->opaque;QemuOpts *opts =NULL ; -int ret = -EINVAL ; +int ret;QCryptoBlockOpenOptions *open_opts =NULL ;unsigned int cflags =0 ;QDict *cryptoopts =NULL ; - bs->file =bdrv_open_child (NULL , options,"file" , bs, &child_of_bds, -BDRV_CHILD_IMAGE , false, errp); -if (!bs->file) { -return -EINVAL ; + ret =bdrv_open_file_child (NULL , options,"file" , bs, errp); +if (ret <0 ) { +return ret; } bs->supported_write_flags = BDRV_REQ_FUA &
@@ -276,6 +275,7 @@ static int block_crypto_open_generic(QCryptoBlockFormat format, opts =qemu_opts_create (opts_spec,NULL ,0 , &error_abort);if (!qemu_opts_absorb_qdict (opts, options, errp)) { + ret = -EINVAL ; goto cleanup; }
@@ -284,6 +284,7 @@ static int block_crypto_open_generic(QCryptoBlockFormat format, open_opts =block_crypto_open_opts_init (cryptoopts, errp);if (!open_opts) { + ret = -EINVAL ; goto cleanup; }
block/dmg.c +3 -4
@@ -439,10 +439,9 @@ static int dmg_open(BlockDriverState *bs, QDict *options, int flags,return ret; } - bs->file =bdrv_open_child (NULL , options,"file" , bs, &child_of_bds, -BDRV_CHILD_IMAGE , false, errp); -if (!bs->file) { -return -EINVAL ; + ret =bdrv_open_file_child (NULL , options,"file" , bs, errp); +if (ret <0 ) { +return ret; }block_module_load_one ("dmg-bz2" );
block/filter-compress.c +2 -4
@@ -30,10 +30,8 @@static int compress_open (BlockDriverState *bs,QDict *options,int flags,Error **errp) { - bs->file =bdrv_open_child (NULL , options,"file" , bs, &child_of_bds, -BDRV_CHILD_FILTERED |BDRV_CHILD_PRIMARY , - false, errp); -if (!bs->file) { +int ret =bdrv_open_file_child (NULL , options,"file" , bs, errp); +if (ret <0 ) {return -EINVAL ; }
block/parallels.c +3 -4
@@ -735,10 +735,9 @@ static int parallels_open(BlockDriverState *bs, QDict *options, int flags,Error *local_err =NULL ;char *buf; - bs->file =bdrv_open_child (NULL , options,"file" , bs, &child_of_bds, -BDRV_CHILD_IMAGE , false, errp); -if (!bs->file) { -return -EINVAL ; + ret =bdrv_open_file_child (NULL , options,"file" , bs, errp); +if (ret <0 ) { +return ret; } ret =bdrv_pread (bs->file,0 , &ph,sizeof (ph));
block/preallocate.c +4 -5
@@ -134,6 +134,7 @@ static int preallocate_open(BlockDriverState *bs, QDict *options, int flags, Error **errp) {BDRVPreallocateState *s = bs->opaque; +int ret;/* * s->data_end and friends should be initialized on permission update.
@@ -141,11 +142,9 @@ static int preallocate_open(BlockDriverState *bs, QDict *options, int flags,*/ s->file_end = s->zero_start = s->data_end = -EINVAL ; - bs->file =bdrv_open_child (NULL , options,"file" , bs, &child_of_bds, -BDRV_CHILD_FILTERED |BDRV_CHILD_PRIMARY , - false, errp); -if (!bs->file) { -return -EINVAL ; + ret =bdrv_open_file_child (NULL , options,"file" , bs, errp); +if (ret <0 ) { +return ret; }if (!preallocate_absorb_opts (&s->opts, options, bs->file->bs, errp)) {
block/qcow.c +2 -4
@@ -120,10 +120,8 @@ static int qcow_open(BlockDriverState *bs, QDict *options, int flags,qdict_extract_subqdict (options, &encryptopts,"encrypt." ); encryptfmt =qdict_get_try_str (encryptopts,"format" ); - bs->file =bdrv_open_child (NULL , options,"file" , bs, &child_of_bds, -BDRV_CHILD_IMAGE , false, errp); -if (!bs->file) { - ret = -EINVAL ; + ret =bdrv_open_file_child (NULL , options,"file" , bs, errp); +if (ret <0 ) { goto fail; }
block/qcow2.c +4 -4
@@ -1890,11 +1890,11 @@ static int qcow2_open(BlockDriverState *bs, QDict *options, int flags, .errp = errp, .ret = -EINPROGRESS }; +int ret; - bs->file =bdrv_open_child (NULL , options,"file" , bs, &child_of_bds, -BDRV_CHILD_IMAGE , false, errp); -if (!bs->file) { -return -EINVAL ; + ret =bdrv_open_file_child (NULL , options,"file" , bs, errp); +if (ret <0 ) { +return ret; }/* Initialise locks */
block/qed.c +4 -4
@@ -558,11 +558,11 @@ static int bdrv_qed_open(BlockDriverState *bs, QDict *options, int flags, .errp = errp, .ret = -EINPROGRESS }; +int ret; - bs->file =bdrv_open_child (NULL , options,"file" , bs, &child_of_bds, -BDRV_CHILD_IMAGE , false, errp); -if (!bs->file) { -return -EINVAL ; + ret =bdrv_open_file_child (NULL , options,"file" , bs, errp); +if (ret <0 ) { +return ret; }bdrv_qed_init_state (bs);
block/replication.c +3 -5
@@ -88,11 +88,9 @@ static int replication_open(BlockDriverState *bs, QDict *options,const char *mode;const char *top_id; - bs->file =bdrv_open_child (NULL , options,"file" , bs, &child_of_bds, -BDRV_CHILD_FILTERED |BDRV_CHILD_PRIMARY , - false, errp); -if (!bs->file) { -return -EINVAL ; + ret =bdrv_open_file_child (NULL , options,"file" , bs, errp); +if (ret <0 ) { +return ret; } ret = -EINVAL ;
block/throttle.c +3 -5
@@ -78,11 +78,9 @@ static int throttle_open(BlockDriverState *bs, QDict *options,char *group;int ret; - bs->file =bdrv_open_child (NULL , options,"file" , bs, &child_of_bds, -BDRV_CHILD_FILTERED |BDRV_CHILD_PRIMARY , - false, errp); -if (!bs->file) { -return -EINVAL ; + ret =bdrv_open_file_child (NULL , options,"file" , bs, errp); +if (ret <0 ) { +return ret; } bs->supported_write_flags = bs->file->bs->supported_write_flags |BDRV_REQ_WRITE_UNCHANGED ;
block/vdi.c +3 -4
@@ -376,10 +376,9 @@ static int vdi_open(BlockDriverState *bs, QDict *options, int flags,int ret;QemuUUID uuid_link, uuid_parent; - bs->file =bdrv_open_child (NULL , options,"file" , bs, &child_of_bds, -BDRV_CHILD_IMAGE , false, errp); -if (!bs->file) { -return -EINVAL ; + ret =bdrv_open_file_child (NULL , options,"file" , bs, errp); +if (ret <0 ) { +return ret; }logout ("\n" );
block/vhdx.c +3 -4
@@ -996,10 +996,9 @@ static int vhdx_open(BlockDriverState *bs, QDict *options, int flags,uint64_t signature;Error *local_err =NULL ; - bs->file =bdrv_open_child (NULL , options,"file" , bs, &child_of_bds, -BDRV_CHILD_IMAGE , false, errp); -if (!bs->file) { -return -EINVAL ; + ret =bdrv_open_file_child (NULL , options,"file" , bs, errp); +if (ret <0 ) { +return ret; } s->bat =NULL ;
block/vmdk.c +3 -4
@@ -1262,10 +1262,9 @@ static int vmdk_open(BlockDriverState *bs, QDict *options, int flags,BDRVVmdkState *s = bs->opaque;uint32_t magic; - bs->file =bdrv_open_child (NULL , options,"file" , bs, &child_of_bds, -BDRV_CHILD_IMAGE , false, errp); -if (!bs->file) { -return -EINVAL ; + ret =bdrv_open_file_child (NULL , options,"file" , bs, errp); +if (ret <0 ) { +return ret; } buf =vmdk_read_desc (bs->file,0 , errp);
block/vpc.c +3 -4
@@ -232,10 +232,9 @@ static int vpc_open(BlockDriverState *bs, QDict *options, int flags,int ret;int64_t bs_size; - bs->file =bdrv_open_child (NULL , options,"file" , bs, &child_of_bds, -BDRV_CHILD_IMAGE , false, errp); -if (!bs->file) { -return -EINVAL ; + ret =bdrv_open_file_child (NULL , options,"file" , bs, errp); +if (ret <0 ) { +return ret; } opts =qemu_opts_create (&vpc_runtime_opts,NULL ,0 , &error_abort);
Signature · 1 line
2.31.1
PATCH 3/14 block/blklogwrites: don't care to remove bs->file child on failure
In reply to Vladimir Sementsov-OgievskiyWe don't need to remove bs->file, generic layer takes care of it. No other driver cares to remove bs->file on failure by hand.
--- block/blklogwrites.c | 4 ---- 1 file changed, 4 deletions(-)
block/blklogwrites.c +0 -4
@@ -254,10 +254,6 @@ fail_log: s->log_file =NULL ; } fail: -if (ret <0 ) { -bdrv_unref_child (bs, bs->file); - bs->file =NULL ; - }qemu_opts_del (opts);return ret; }
Signature · 1 line
2.31.1
PATCH 4/14 test-bdrv-graph-mod: update test_parallel_perm_update test case
In reply to Vladimir Sementsov-Ogievskiytest_parallel_perm_update() does two things that we are going to restrict in the near future:
1. It updates bs->file field by hand. bs->file will be managed automatically by generic code (together with bs->children list).
Let's better refactor our "tricky" bds to have own state where one of children is linked as "selected".
This also looks less "tricky", so avoid using this word.
2. It create FILTERED children that are not PRIMARY. Except for tests all FILTERED children in the Qemu block layer are always PRIMARY as well. We are going to formalize this rule, so let's better use DATA children here.
While being here, update the picture to better correspond to the test code.
--- tests/unit/test-bdrv-graph-mod.c | 70 ++++++++++++++++++++------------ 1 file changed, 44 insertions(+), 26 deletions(-)
tests/unit/test-bdrv-graph-mod.c +44 -26
@@ -241,13 +241,26 @@ static void test_parallel_exclusive_write(void)bdrv_unref (top); } -static void write_to_file_perms (BlockDriverState *bs,BdrvChild *c, -BdrvChildRole role, -BlockReopenQueue *reopen_queue, -uint64_t perm,uint64_t shared, -uint64_t *nperm,uint64_t *nshared) +/* +* write-to-selected node may have several DATA children, one of them may be +* "selected". Exclusive write permission is taken on selected child. +* +* We don't realize write handler itself, as we need only to test how permission +* update works. +*/ +typedef struct BDRVWriteToSelectedState { +BdrvChild *selected; +}BDRVWriteToSelectedState ; + +static void write_to_selected_perms (BlockDriverState *bs,BdrvChild *c, +BdrvChildRole role, +BlockReopenQueue *reopen_queue, +uint64_t perm,uint64_t shared, +uint64_t *nperm,uint64_t *nshared) { -if (bs->file && c == bs->file) { +BDRVWriteToSelectedState *s = bs->opaque; + +if (s->selected && c == s->selected) { *nperm =BLK_PERM_WRITE ; *nshared =BLK_PERM_ALL & ~BLK_PERM_WRITE ; }else {
@@ -256,9 +269,10 @@ static void write_to_file_perms(BlockDriverState *bs, BdrvChild *c, } } -static BlockDriver bdrv_write_to_file = { - .format_name ="tricky-perm" , - .bdrv_child_perm = write_to_file_perms, +static BlockDriver bdrv_write_to_selected = { + .format_name ="write-to-selected" , + .instance_size =sizeof (BDRVWriteToSelectedState), + .bdrv_child_perm = write_to_selected_perms, };
@@ -266,15 +280,18 @@ static BlockDriver bdrv_write_to_file = {* The following test shows that topological-sort order is required for * permission update, simple DFS is not enough. * -* Consider the block driver which has two filter children: one active -* with exclusive write access and one inactive with no specific -* permissions. +* Consider the block driver (write-to-selected) which has two children: one is +* selected so we have exclusive write access to it and for the other one we +* don't need any specific permissions. * * And, these two children has a common base child, like this: +* (additional "top" on top is used in test just because the only public +* function to update permission should get a specific child to update. +* Making bdrv_refresh_perms() public just for this test doesn't worth it) * -* ┌─────┐ ┌──────┐ -* │ fl2 │ ◀── │ top │ -* └─────┘ └──────┘ +* ┌─────┐ ┌───────────────────┐ ┌─────┐ +* │ fl2 │ ◀── │ write-to-selected │ ◀── │ top │ +* └─────┘ └───────────────────┘ └─────┘ * │ │ * │ │ w * │ ▼
@@ -290,7 +307,7 @@ static BlockDriver bdrv_write_to_file = {* * So, exclusive write is propagated. * -* Assume, we want to make fl2 active instead of fl1. +* Assume, we want to select fl2 instead of fl1. * So, we set some option for top driver and do permission update. * * With simple DFS, if permission update goes first through
@@ -306,9 +323,10 @@ static BlockDriver bdrv_write_to_file = {static void test_parallel_perm_update (void ) {BlockDriverState *top =no_perm_node ("top" ); -BlockDriverState *tricky = -bdrv_new_open_driver (&bdrv_write_to_file,"tricky" ,BDRV_O_RDWR , +BlockDriverState *ws = +bdrv_new_open_driver (&bdrv_write_to_selected,"ws" ,BDRV_O_RDWR , &error_abort); +BDRVWriteToSelectedState *s = ws->opaque;BlockDriverState *base =no_perm_node ("base" );BlockDriverState *fl1 =pass_through_node ("fl1" );BlockDriverState *fl2 =pass_through_node ("fl2" );
@@ -320,33 +338,33 @@ static void test_parallel_perm_update(void)*/ bdrv_ref (base); -bdrv_attach_child (top, tricky,"file" , &child_of_bds,BDRV_CHILD_DATA , +bdrv_attach_child (top, ws,"file" , &child_of_bds,BDRV_CHILD_DATA , &error_abort); - c_fl1 =bdrv_attach_child (tricky, fl1,"first" , &child_of_bds, -BDRV_CHILD_FILTERED , &error_abort); - c_fl2 =bdrv_attach_child (tricky, fl2,"second" , &child_of_bds, -BDRV_CHILD_FILTERED , &error_abort); + c_fl1 =bdrv_attach_child (ws, fl1,"first" , &child_of_bds, +BDRV_CHILD_DATA , &error_abort); + c_fl2 =bdrv_attach_child (ws, fl2,"second" , &child_of_bds, +BDRV_CHILD_DATA , &error_abort);bdrv_attach_child (fl1, base,"backing" , &child_of_bds,BDRV_CHILD_FILTERED , &error_abort);bdrv_attach_child (fl2, base,"backing" , &child_of_bds,BDRV_CHILD_FILTERED , &error_abort);/* Select fl1 as first child to be active */ - tricky->file = c_fl1; + s->selected = c_fl1;bdrv_child_refresh_perms (top, top->children.lh_first, &error_abort);assert (c_fl1->perm &BLK_PERM_WRITE );assert (!(c_fl2->perm &BLK_PERM_WRITE ));/* Now, try to switch active child and update permissions */ - tricky->file = c_fl2; + s->selected = c_fl2;bdrv_child_refresh_perms (top, top->children.lh_first, &error_abort);assert (c_fl2->perm &BLK_PERM_WRITE );assert (!(c_fl1->perm &BLK_PERM_WRITE ));/* Switch once more, to not care about real child order in the list */ - tricky->file = c_fl1; + s->selected = c_fl1;bdrv_child_refresh_perms (top, top->children.lh_first, &error_abort);assert (c_fl1->perm &BLK_PERM_WRITE );
Signature · 1 line
2.31.1
PATCH 5/14 tests-bdrv-drain: bdrv_replace_test driver: declare supports_backing
In reply to Vladimir Sementsov-OgievskiyWe do add COW child to the node. In future we are going to forbid adding COW child to the node that doesn't support backing. So, fix it here now.
Don't worry about setting bs->backing itself: it further commit we'll update the block-layer to automatically set/unset this field in generic code.
--- tests/unit/test-bdrv-drain.c | 1 + 1 file changed, 1 insertion(+)
tests/unit/test-bdrv-drain.c +1 -0
@@ -1944,6 +1944,7 @@ static void coroutine_fn bdrv_replace_test_co_drain_end(BlockDriverState *bs)static BlockDriver bdrv_replace_test = { .format_name ="replace_test" , .instance_size =sizeof (BDRVReplaceTestState), + .supports_backing = true, .bdrv_close = bdrv_replace_test_close, .bdrv_co_preadv = bdrv_replace_test_co_preadv,
Signature · 1 line
2.31.1
PATCH 6/14 test-bdrv-graph-mod: fix filters to be filters
In reply to Vladimir Sementsov-Ogievskiybdrv_pass_through is used as filter, even all node variables has corresponding names. We want to append it, so it should be backing-child-based filter like mirror_top.
So, in test_update_perm_tree, first child should be DATA, as we don't want filters with two filtered children.
bdrv_exclusive_writer is used as a filter once. So it should be filter anyway. We want to append it, so it should be backing-child-based fitler too.
Make all FILTERED children to be PRIMARY as well. We are going to force this rule by assertion soon.
--- include/block/block_int.h | 5 +++-- tests/unit/test-bdrv-graph-mod.c | 24 +++++++++++++++++------- 2 files changed, 20 insertions(+), 9 deletions(-)
include/block/block_int.h +3 -2
@@ -121,8 +121,9 @@ struct BlockDriver {/* * Only make sense for filter drivers, for others must be false. * If true, filtered child is bs->backing. Otherwise it's bs->file. -* Only two internal filters use bs->backing as filtered child and has this -* field set to true: mirror_top and commit_top. +* Two internal filters use bs->backing as filtered child and has this +* field set to true: mirror_top and commit_top. There also two such test +* filters in tests/unit/test-bdrv-graph-mod.c. * * Never create any more such filters! *
tests/unit/test-bdrv-graph-mod.c +17 -7
@@ -26,6 +26,8 @@static BlockDriver bdrv_pass_through = { .format_name ="pass-through" , + .is_filter = true, + .filtered_child_is_backing = true, .bdrv_child_perm = bdrv_default_perms, };
@@ -57,6 +59,8 @@ static void exclusive_write_perms(BlockDriverState *bs, BdrvChild *c,static BlockDriver bdrv_exclusive_writer = { .format_name ="exclusive-writer" , + .is_filter = true, + .filtered_child_is_backing = true, .bdrv_child_perm = exclusive_write_perms, };
@@ -134,7 +138,7 @@ static void test_update_perm_tree(void)blk_insert_bs (root, bs, &error_abort);bdrv_attach_child (filter, bs,"child" , &child_of_bds, -BDRV_CHILD_FILTERED |BDRV_CHILD_PRIMARY , &error_abort); +BDRV_CHILD_DATA , &error_abort); ret =bdrv_append (filter, bs,NULL ); g_assert_cmpint(ret, <, 0);
@@ -228,11 +232,14 @@ static void test_parallel_exclusive_write(void)*/ bdrv_ref (base); -bdrv_attach_child (top, fl1,"backing" , &child_of_bds,BDRV_CHILD_DATA , +bdrv_attach_child (top, fl1,"backing" , &child_of_bds, +BDRV_CHILD_FILTERED |BDRV_CHILD_PRIMARY , &error_abort); -bdrv_attach_child (fl1, base,"backing" , &child_of_bds,BDRV_CHILD_FILTERED , +bdrv_attach_child (fl1, base,"backing" , &child_of_bds, +BDRV_CHILD_FILTERED |BDRV_CHILD_PRIMARY , &error_abort); -bdrv_attach_child (fl2, base,"backing" , &child_of_bds,BDRV_CHILD_FILTERED , +bdrv_attach_child (fl2, base,"backing" , &child_of_bds, +BDRV_CHILD_FILTERED |BDRV_CHILD_PRIMARY , &error_abort);bdrv_replace_node (fl1, fl2, &error_abort);
@@ -344,9 +351,11 @@ static void test_parallel_perm_update(void) BDRV_CHILD_DATA, &error_abort); c_fl2 =bdrv_attach_child (ws, fl2,"second" , &child_of_bds,BDRV_CHILD_DATA , &error_abort); -bdrv_attach_child (fl1, base,"backing" , &child_of_bds,BDRV_CHILD_FILTERED , +bdrv_attach_child (fl1, base,"backing" , &child_of_bds, +BDRV_CHILD_FILTERED |BDRV_CHILD_PRIMARY , &error_abort); -bdrv_attach_child (fl2, base,"backing" , &child_of_bds,BDRV_CHILD_FILTERED , +bdrv_attach_child (fl2, base,"backing" , &child_of_bds, +BDRV_CHILD_FILTERED |BDRV_CHILD_PRIMARY , &error_abort);/* Select fl1 as first child to be active */
@@ -397,7 +406,8 @@ static void test_append_greedy_filter(void)BlockDriverState *base =no_perm_node ("base" );BlockDriverState *fl =exclusive_writer_node ("fl1" ); -bdrv_attach_child (top, base,"backing" , &child_of_bds,BDRV_CHILD_COW , +bdrv_attach_child (top, base,"backing" , &child_of_bds, +BDRV_CHILD_FILTERED |BDRV_CHILD_PRIMARY , &error_abort);bdrv_append (fl, base, &error_abort);
Signature · 1 line
2.31.1
PATCH 7/14 block: document connection between child roles and bs->backing/bs->file
In reply to Vladimir Sementsov-OgievskiyMake the informal rules formal. In further commit we'll add corresponding assertions.
--- include/block/block.h | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+)
include/block/block.h +42 -0
@@ -290,6 +290,48 @@ enum {* * At least one of DATA, METADATA, FILTERED, or COW must be set for * every child. +* +* +* = Connection with bs->children, bs->file and bs->backing fields = +* +* 1. Filters +* +* Filter drivers has drv->is_filter = true. +* +* Filter driver has exactly one FILTERED|PRIMARY child, any may have other +* children which must not have these bits (the example is copy-before-write +* filter that also has target DATA child). +* +* Filter driver never has COW children. +* +* For all filters except for mirror_top and commit_top, the filtered child is +* linked in bs->file, bs->backing is NULL. +* +* For mirror_top and commit_top filtered child is linked in bs->backing and +* their bs->file is NULL. These two filters has drv->filtered_child_is_backing +* = true. +* +* 2. "raw" driver (block/raw-format.c) +* +* Formally it's not a filter (drv->is_filter = false) +* +* bs->backing is always NULL +* +* Only has one child, linked in bs->file. It's role is either FILTERED|PRIMARY +* (like filter) either DATA|PRIMARY depending on options. +* +* 3. Other drivers +* +* Doesn't have any FILTERED children. +* +* May have at most one COW child. In this case it's linked in bs->backing. +* Otherwise bs->backing is NULL. COW child is never PRIMARY. +* +* May have at most one PRIMARY child. In this case it's linked in bs->file. +* Otherwise bs->file is NULL. +* +* May also have some other children that don't have neither PRIMARY nor COW +* bits set. */ enum BdrvChildRoleBits {/*
Signature · 1 line
2.31.1
PATCH 8/14 block/snapshot: stress that we fallback to primary child
In reply to Vladimir Sementsov-OgievskiyActually what we chose is a primary child. Let's stress it in the code.
We are going to drop indirect pointer logic here in future. Actually this commit simplifies the future work: we drop use of indirection in the assertion now.
--- block/snapshot.c | 30 ++++++++++-------------------- 1 file changed, 10 insertions(+), 20 deletions(-)
block/snapshot.c +10 -20
@@ -158,21 +158,14 @@ bool bdrv_snapshot_find_by_id_and_name(BlockDriverState *bs,static BdrvChild **bdrv_snapshot_fallback_ptr (BlockDriverState *bs) {BdrvChild **fallback; -BdrvChild *child; +BdrvChild *child =bdrv_primary_child (bs); -/* -* The only BdrvChild pointers that are safe to modify (and which -* we can thus return a reference to) are bs->file and -* bs->backing. -*/ - fallback = &bs->file; -if (!*fallback && bs->drv && bs->drv->is_filter) { - fallback = &bs->backing; - } - -if (!*fallback) { +/* We allow fallback only to primary child */ +if (!child) {return NULL ; } + fallback = (child == bs->file ? &bs->file : &bs->backing); +assert (*fallback == child);/* * Check that there are no other children that would need to be
@@ -300,15 +293,12 @@ int bdrv_snapshot_goto(BlockDriverState *bs, }/* -* fallback_ptr is &bs->file or &bs->backing. *fallback_ptr -* was closed above and set to NULL, but the .bdrv_open() call -* has opened it again, because we set the respective option -* (with the qdict_put_str() call above). -* Assert that .bdrv_open() has attached some child on -* *fallback_ptr, and that it has attached the one we wanted -* it to (i.e., fallback_bs). +* fallback was a primary child. It was closed above and set to NULL, +* but the .bdrv_open() call has opened it again, because we set the +* respective option (with the qdict_put_str() call above). +* Assert that .bdrv_open() has attached some BDS as primary child. */ -assert (*fallback_ptr && fallback_bs == (*fallback_ptr)->bs); +assert (bdrv_primary_bs (bs) == fallback_bs);bdrv_unref (fallback_bs);return ret; }
Signature · 1 line
2.31.1
PATCH 9/14 Revert "block: Let replace_child_noperm free children"
In reply to Vladimir Sementsov-OgievskiyWe are going to reimplement this behavior (clear bs->file / bs->backing pointers automatically when child->bs is cleared) in a nicer way.
This reverts commit b0a9f6fed3d80de610dcd04a7e66f9f30a04174f.
--- block.c | 102 +++++++++++++------------------------------------------- 1 file changed, 23 insertions(+), 79 deletions(-)
block.c +23 -79
@@ -87,10 +87,8 @@ static BlockDriverState *bdrv_open_inherit(const char *filename,static bool bdrv_recurse_has_child (BlockDriverState *bs,BlockDriverState *child); -static void bdrv_child_free (BdrvChild *child);static void bdrv_replace_child_noperm (BdrvChild **child, -BlockDriverState *new_bs, -bool free_empty_child); +BlockDriverState *new_bs);static void bdrv_remove_file_or_backing_child (BlockDriverState *bs,BdrvChild *child,Transaction *tran);
@@ -2258,16 +2256,12 @@ typedef struct BdrvReplaceChildState {BdrvChild *child;BdrvChild **childp;BlockDriverState *old_bs; -bool free_empty_child; } BdrvReplaceChildState;static void bdrv_replace_child_commit (void *opaque) {BdrvReplaceChildState *s = opaque; -if (s->free_empty_child && !s->child->bs) { -bdrv_child_free (s->child); - }bdrv_unref (s->old_bs); }
@@ -2284,26 +2278,22 @@ static void bdrv_replace_child_abort(void *opaque)* modify the BdrvChild * pointer we indirectly pass to it, i.e. it * will not modify s->child. From that perspective, it does not matter * whether we pass s->childp or &s->child. +* (TODO: Right now, bdrv_replace_child_noperm() never modifies that +* pointer anyway (though it will in the future), so at this point it +* absolutely does not matter whether we pass s->childp or &s->child.) * (2) If new_bs is not NULL, s->childp will be NULL. We then cannot use * it here. * (3) If new_bs is NULL, *s->childp will have been NULLed by * bdrv_replace_child_tran()'s bdrv_replace_child_noperm() call, and we * must not pass a NULL *s->childp here. +* (TODO: In its current state, bdrv_replace_child_noperm() will not +* have NULLed *s->childp, so this does not apply yet. It will in the +* future.) * * So whether new_bs was NULL or not, we cannot pass s->childp here; and in * any case, there is no reason to pass it anyway. */ -bdrv_replace_child_noperm (&s->child, s->old_bs, true); -/* -* The child was pre-existing, so s->old_bs must be non-NULL, and -* s->child thus must not have been freed -*/ -assert (s->child !=NULL ); -if (!new_bs) { -/* As described above, *s->childp was cleared, so restore it */ -assert (s->childp !=NULL ); - *s->childp = s->child; - } +bdrv_replace_child_noperm (&s->child, s->old_bs);bdrv_unref (new_bs); }
@@ -2320,44 +2310,30 @@ static TransactionActionDrv bdrv_replace_child_drv = {* * The function doesn't update permissions, caller is responsible for this. * -* (*childp)->bs must not be NULL. -* * Note that if new_bs == NULL, @childp is stored in a state object attached * to @tran, so that the old child can be reinstated in the abort handler. * Therefore, if @new_bs can be NULL, @childp must stay valid until the * transaction is committed or aborted. * -* If @free_empty_child is true and @new_bs is NULL, the BdrvChild is -* freed (on commit). @free_empty_child should only be false if the -* caller will free the BDrvChild themselves (which may be important -* if this is in turn called in another transactional context). +* (TODO: The reinstating does not happen yet, but it will once +* bdrv_replace_child_noperm() NULLs *childp when new_bs is NULL.) */ static void bdrv_replace_child_tran (BdrvChild **childp,BlockDriverState *new_bs, -Transaction *tran, -bool free_empty_child) +Transaction *tran) {BdrvReplaceChildState *s =g_new (BdrvReplaceChildState,1 ); *s = (BdrvReplaceChildState ) { .child = *childp, .childp = new_bs ==NULL ? childp :NULL , .old_bs = (*childp)->bs, - .free_empty_child = free_empty_child, };tran_add (tran, &bdrv_replace_child_drv, s); -/* The abort handler relies on this */ -assert (s->old_bs !=NULL ); -if (new_bs) {bdrv_ref (new_bs); } -/* -* Pass free_empty_child=false, we will free the child (if -* necessary) in bdrv_replace_child_commit() (if our -* @free_empty_child parameter was true). -*/ -bdrv_replace_child_noperm (childp, new_bs, false); +bdrv_replace_child_noperm (childp, new_bs);/* old_bs reference is transparently moved from *childp to @s */ }
@@ -2729,22 +2705,8 @@ uint64_t bdrv_qapi_perm_to_blk_perm(BlockPermission qapi_perm)return permissions[qapi_perm]; } -/** -* Replace (*childp)->bs by @new_bs. -* -* If @new_bs is NULL, *childp will be set to NULL, too: BDS parents -* generally cannot handle a BdrvChild with .bs == NULL, so clearing -* BdrvChild.bs should generally immediately be followed by the -* BdrvChild pointer being cleared as well. -* -* If @free_empty_child is true and @new_bs is NULL, the BdrvChild is -* freed. @free_empty_child should only be false if the caller will -* free the BdrvChild themselves (this may be important in a -* transactional context, where it may only be freed on commit). -*/ static void bdrv_replace_child_noperm (BdrvChild **childp, -BlockDriverState *new_bs, -bool free_empty_child) +BlockDriverState *new_bs) {BdrvChild *child = *childp;BlockDriverState *old_bs = child->bs;
@@ -2781,9 +2743,6 @@ static void bdrv_replace_child_noperm(BdrvChild **childp, } child->bs = new_bs; -if (!new_bs) { - *childp =NULL ; - }if (new_bs) {QLIST_INSERT_HEAD (&new_bs->parents, child, next_parent);
@@ -2813,10 +2772,6 @@ static void bdrv_replace_child_noperm(BdrvChild **childp,bdrv_parent_drained_end_single (child); drain_saldo++; } - -if (free_empty_child && !child->bs) { -bdrv_child_free (child); - } }/**
@@ -2846,14 +2801,7 @@ static void bdrv_attach_child_common_abort(void *opaque)BdrvChild *child = *s->child;BlockDriverState *bs = child->bs; -/* -* Pass free_empty_child=false, because we still need the child -* for the AioContext operations on the parent below; those -* BdrvChildClass methods all work on a BdrvChild object, so we -* need to keep it as an empty shell (after this function, it will -* not be attached to any parent, and it will not have a .bs). -*/ -bdrv_replace_child_noperm (s->child,NULL , false); +bdrv_replace_child_noperm (s->child,NULL );if (bdrv_get_aio_context (bs) != s->old_child_ctx) {bdrv_try_set_aio_context (bs, s->old_child_ctx, &error_abort);
@@ -2875,6 +2823,7 @@ static void bdrv_attach_child_common_abort(void *opaque)bdrv_unref (bs);bdrv_child_free (child); + *s->child =NULL ; }static TransactionActionDrv bdrv_attach_child_common_drv = {
@@ -2952,9 +2901,7 @@ static int bdrv_attach_child_common(BlockDriverState *child_bs, }bdrv_ref (child_bs); -bdrv_replace_child_noperm (&new_child, child_bs, true); -/* child_bs was non-NULL, so new_child must not have been freed */ -assert (new_child !=NULL ); +bdrv_replace_child_noperm (&new_child, child_bs); *child = new_child;
@@ -3013,7 +2960,8 @@ static void bdrv_detach_child(BdrvChild **childp) {BlockDriverState *old_bs = (*childp)->bs; -bdrv_replace_child_noperm (childp,NULL , true); +bdrv_replace_child_noperm (childp,NULL ); +bdrv_child_free (*childp);if (old_bs) {/*
@@ -5024,11 +4972,7 @@ static void bdrv_remove_file_or_backing_child(BlockDriverState *bs, }if (child->bs) { -/* -* Pass free_empty_child=false, we will free the child in -* bdrv_remove_filter_or_cow_child_commit() -*/ -bdrv_replace_child_tran (childp,NULL , tran, false); +bdrv_replace_child_tran (childp,NULL , tran); } s =g_new (BdrvRemoveFilterOrCowChild,1 );
@@ -5038,6 +4982,8 @@ static void bdrv_remove_file_or_backing_child(BlockDriverState *bs, .is_backing = (childp == &bs->backing), };tran_add (tran, &bdrv_remove_filter_or_cow_child_drv, s); + + *childp =NULL ; }/*
@@ -5080,7 +5026,7 @@ static int bdrv_replace_node_noperm(BlockDriverState *from,* Passing a pointer to the local variable @c is fine here, because * @to is not NULL, and so &c will not be attached to the transaction. */ -bdrv_replace_child_tran (&c, to, tran, true); +bdrv_replace_child_tran (&c, to, tran); }return 0 ;
@@ -5236,9 +5182,7 @@ int bdrv_replace_child_bs(BdrvChild *child, BlockDriverState *new_bs,bdrv_drained_begin (old_bs);bdrv_drained_begin (new_bs); -bdrv_replace_child_tran (&child, new_bs, tran, true); -/* @new_bs must have been non-NULL, so @child must not have been freed */ -assert (child !=NULL ); +bdrv_replace_child_tran (&child, new_bs, tran); found =g_hash_table_new (NULL ,NULL ); refresh_list =bdrv_topological_dfs (refresh_list, found, old_bs);
Signature · 1 line
2.31.1
PATCH 10/14 Revert "block: Let replace_child_tran keep indirect pointer"
In reply to Vladimir Sementsov-OgievskiyThat's a preparation to previously reverted
"block: Let replace_child_noperm free children". Drop it too, we don't need it for a new approach.
This reverts commit 82b54cf51656bf3cd5ed1ac549e8a1085a0e3290.
--- block.c | 83 +++++++-------------------------------------------------- 1 file changed, 10 insertions(+), 73 deletions(-)
block.c +10 -73
@@ -2254,7 +2254,6 @@ static int bdrv_drv_set_perm(BlockDriverState *bs, uint64_t perm,typedef struct BdrvReplaceChildState {BdrvChild *child; -BdrvChild **childp;BlockDriverState *old_bs; }BdrvReplaceChildState ;
@@ -2270,29 +2269,7 @@ static void bdrv_replace_child_abort(void *opaque)BdrvReplaceChildState *s = opaque;BlockDriverState *new_bs = s->child->bs; -/* -* old_bs reference is transparently moved from @s to s->child. -* -* Pass &s->child here instead of s->childp, because: -* (1) s->old_bs must be non-NULL, so bdrv_replace_child_noperm() will not -* modify the BdrvChild * pointer we indirectly pass to it, i.e. it -* will not modify s->child. From that perspective, it does not matter -* whether we pass s->childp or &s->child. -* (TODO: Right now, bdrv_replace_child_noperm() never modifies that -* pointer anyway (though it will in the future), so at this point it -* absolutely does not matter whether we pass s->childp or &s->child.) -* (2) If new_bs is not NULL, s->childp will be NULL. We then cannot use -* it here. -* (3) If new_bs is NULL, *s->childp will have been NULLed by -* bdrv_replace_child_tran()'s bdrv_replace_child_noperm() call, and we -* must not pass a NULL *s->childp here. -* (TODO: In its current state, bdrv_replace_child_noperm() will not -* have NULLed *s->childp, so this does not apply yet. It will in the -* future.) -* -* So whether new_bs was NULL or not, we cannot pass s->childp here; and in -* any case, there is no reason to pass it anyway. -*/ +/* old_bs reference is transparently moved from @s to @s->child */ bdrv_replace_child_noperm (&s->child, s->old_bs);bdrv_unref (new_bs); }
@@ -2309,32 +2286,22 @@ static TransactionActionDrv bdrv_replace_child_drv = {* Note: real unref of old_bs is done only on commit. * * The function doesn't update permissions, caller is responsible for this. -* -* Note that if new_bs == NULL, @childp is stored in a state object attached -* to @tran, so that the old child can be reinstated in the abort handler. -* Therefore, if @new_bs can be NULL, @childp must stay valid until the -* transaction is committed or aborted. -* -* (TODO: The reinstating does not happen yet, but it will once -* bdrv_replace_child_noperm() NULLs *childp when new_bs is NULL.) */ -static void bdrv_replace_child_tran (BdrvChild **childp, -BlockDriverState *new_bs, +static void bdrv_replace_child_tran (BdrvChild *child,BlockDriverState *new_bs,Transaction *tran) {BdrvReplaceChildState *s =g_new (BdrvReplaceChildState,1 ); *s = (BdrvReplaceChildState ) { - .child = *childp, - .childp = new_bs ==NULL ? childp :NULL , - .old_bs = (*childp)->bs, + .child = child, + .old_bs = child->bs, };tran_add (tran, &bdrv_replace_child_drv, s);if (new_bs) {bdrv_ref (new_bs); } -bdrv_replace_child_noperm (childp, new_bs); -/* old_bs reference is transparently moved from *childp to @s */ +bdrv_replace_child_noperm (&child, new_bs); +/* old_bs reference is transparently moved from @child to @s */ }/*
@@ -4898,7 +4865,6 @@ static bool should_update_child(BdrvChild *c, BlockDriverState *to)typedef struct BdrvRemoveFilterOrCowChild {BdrvChild *child; -BlockDriverState *bs;bool is_backing; }BdrvRemoveFilterOrCowChild ;
@@ -4928,19 +4894,10 @@ static void bdrv_remove_filter_or_cow_child_commit(void *opaque)bdrv_child_free (s->child); } -static void bdrv_remove_filter_or_cow_child_clean (void *opaque) -{ -BdrvRemoveFilterOrCowChild *s = opaque; - -/* Drop the bs reference after the transaction is done */ -bdrv_unref (s->bs); -g_free (s); -} -static TransactionActionDrv bdrv_remove_filter_or_cow_child_drv = { .abort = bdrv_remove_filter_or_cow_child_abort, .commit = bdrv_remove_filter_or_cow_child_commit, - .clean = bdrv_remove_filter_or_cow_child_clean, + .clean = g_free, };/*
@@ -4958,11 +4915,6 @@ static void bdrv_remove_file_or_backing_child(BlockDriverState *bs,return ; } -/* -* Keep a reference to @bs so @childp will stay valid throughout the -* transaction (required by bdrv_replace_child_tran()) -*/ -bdrv_ref (bs);if (child == bs->backing) { childp = &bs->backing; }else if (child == bs->file) {
@@ -4972,13 +4924,12 @@ static void bdrv_remove_file_or_backing_child(BlockDriverState *bs, }if (child->bs) { -bdrv_replace_child_tran (childp,NULL , tran); +bdrv_replace_child_tran (*childp,NULL , tran); } s =g_new (BdrvRemoveFilterOrCowChild,1 ); *s = (BdrvRemoveFilterOrCowChild ) { .child = child, - .bs = bs, .is_backing = (childp == &bs->backing), };tran_add (tran, &bdrv_remove_filter_or_cow_child_drv, s);
@@ -5004,8 +4955,6 @@ static int bdrv_replace_node_noperm(BlockDriverState *from, {BdrvChild *c, *next; -assert (to !=NULL ); - QLIST_FOREACH_SAFE(c, &from->parents, next_parent, next) {assert (c->bs == from);if (!should_update_child (c, to)) {
@@ -5021,12 +4970,7 @@ static int bdrv_replace_node_noperm(BlockDriverState *from, c->name, from->node_name);return -EPERM ; } - -/* -* Passing a pointer to the local variable @c is fine here, because -* @to is not NULL, and so &c will not be attached to the transaction. -*/ -bdrv_replace_child_tran (&c, to, tran); +bdrv_replace_child_tran (c, to, tran); }return 0 ;
@@ -5041,8 +4985,6 @@ static int bdrv_replace_node_noperm(BlockDriverState *from,* * With @detach_subchain=true @to must be in a backing chain of @from. In this * case backing link of the cow-parent of @to is removed. -* -* @to must not be NULL. */ static int bdrv_replace_node_common(BlockDriverState *from, BlockDriverState *to,
@@ -5055,8 +4997,6 @@ static int bdrv_replace_node_common(BlockDriverState *from,BlockDriverState *to_cow_parent =NULL ;int ret; -assert (to !=NULL ); -if (detach_subchain) {assert (bdrv_chain_contains (from, to));assert (from != to);
@@ -5112,9 +5052,6 @@ out:return ret; } -/** -* Replace node @from by @to (where neither may be NULL). -*/ int bdrv_replace_node (BlockDriverState *from,BlockDriverState *to,Error **errp) {
@@ -5182,7 +5119,7 @@ int bdrv_replace_child_bs(BdrvChild *child, BlockDriverState *new_bs,bdrv_drained_begin (old_bs);bdrv_drained_begin (new_bs); -bdrv_replace_child_tran (&child, new_bs, tran); +bdrv_replace_child_tran (child, new_bs, tran); found =g_hash_table_new (NULL ,NULL ); refresh_list =bdrv_topological_dfs (refresh_list, found, old_bs);
Signature · 1 line
2.31.1
PATCH 11/14 Revert "block: Restructure remove_file_or_backing_child()"
In reply to Vladimir Sementsov-OgievskiyThat's a preparation to previously reverted
"block: Let replace_child_noperm free children". Drop it too, we don't need it for a new approach.
This reverts commit 562bda8bb41879eeda0bd484dd3d55134579b28e.
--- block.c | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-)
block.c +9 -12
@@ -4908,33 +4908,30 @@ static void bdrv_remove_file_or_backing_child(BlockDriverState *bs, BdrvChild *child, Transaction *tran) { -BdrvChild **childp;BdrvRemoveFilterOrCowChild *s; +assert (child == bs->backing || child == bs->file); +if (!child) {return ; } -if (child == bs->backing) { - childp = &bs->backing; - }else if (child == bs->file) { - childp = &bs->file; - }else { -g_assert_not_reached (); - } -if (child->bs) { -bdrv_replace_child_tran (*childp,NULL , tran); +bdrv_replace_child_tran (child,NULL , tran); } s =g_new (BdrvRemoveFilterOrCowChild,1 ); *s = (BdrvRemoveFilterOrCowChild ) { .child = child, - .is_backing = (childp == &bs->backing), + .is_backing = (child == bs->backing), };tran_add (tran, &bdrv_remove_filter_or_cow_child_drv, s); - *childp =NULL ; +if (s->is_backing) { + bs->backing =NULL ; + }else { + bs->file =NULL ; + } }/*
Signature · 1 line
2.31.1
PATCH 12/14 Revert "block: Pass BdrvChild ** to replace_child_noperm"
In reply to Vladimir Sementsov-OgievskiyThat's a preparation to previously reverted
"block: Let replace_child_noperm free children". Drop it too, we don't need it for a new approach.
This reverts commit be64bbb0149748f3999c49b13976aafb8330ea86.
--- block.c | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-)
block.c +11 -12
@@ -87,7 +87,7 @@ static BlockDriverState *bdrv_open_inherit(const char *filename,static bool bdrv_recurse_has_child (BlockDriverState *bs,BlockDriverState *child); -static void bdrv_replace_child_noperm (BdrvChild **child, +static void bdrv_replace_child_noperm (BdrvChild *child,BlockDriverState *new_bs); static void bdrv_remove_file_or_backing_child(BlockDriverState *bs, BdrvChild *child,
@@ -2270,7 +2270,7 @@ static void bdrv_replace_child_abort(void *opaque)BlockDriverState *new_bs = s->child->bs;/* old_bs reference is transparently moved from @s to @s->child */ -bdrv_replace_child_noperm (&s->child, s->old_bs); +bdrv_replace_child_noperm (s->child, s->old_bs);bdrv_unref (new_bs); }
@@ -2300,7 +2300,7 @@ static void bdrv_replace_child_tran(BdrvChild *child, BlockDriverState *new_bs,if (new_bs) {bdrv_ref (new_bs); } -bdrv_replace_child_noperm (&child, new_bs); +bdrv_replace_child_noperm (child, new_bs);/* old_bs reference is transparently moved from @child to @s */ }
@@ -2672,10 +2672,9 @@ uint64_t bdrv_qapi_perm_to_blk_perm(BlockPermission qapi_perm)return permissions[qapi_perm]; } -static void bdrv_replace_child_noperm (BdrvChild **childp, +static void bdrv_replace_child_noperm (BdrvChild *child,BlockDriverState *new_bs) { -BdrvChild *child = *childp;BlockDriverState *old_bs = child->bs;int new_bs_quiesce_counter;int drain_saldo;
@@ -2768,7 +2767,7 @@ static void bdrv_attach_child_common_abort(void *opaque)BdrvChild *child = *s->child;BlockDriverState *bs = child->bs; -bdrv_replace_child_noperm (s->child,NULL ); +bdrv_replace_child_noperm (child,NULL );if (bdrv_get_aio_context (bs) != s->old_child_ctx) {bdrv_try_set_aio_context (bs, s->old_child_ctx, &error_abort);
@@ -2868,7 +2867,7 @@ static int bdrv_attach_child_common(BlockDriverState *child_bs, }bdrv_ref (child_bs); -bdrv_replace_child_noperm (&new_child, child_bs); +bdrv_replace_child_noperm (new_child, child_bs); *child = new_child;
@@ -2923,12 +2922,12 @@ static int bdrv_attach_child_noperm(BlockDriverState *parent_bs,return 0 ; } -static void bdrv_detach_child (BdrvChild **childp) +static void bdrv_detach_child (BdrvChild *child) { -BlockDriverState *old_bs = (*childp)->bs; +BlockDriverState *old_bs = child->bs; -bdrv_replace_child_noperm (childp,NULL ); -bdrv_child_free (*childp); +bdrv_replace_child_noperm (child,NULL ); +bdrv_child_free (child);if (old_bs) {/*
@@ -3034,7 +3033,7 @@ void bdrv_root_unref_child(BdrvChild *child)BlockDriverState *child_bs; child_bs = child->bs; -bdrv_detach_child (&child); +bdrv_detach_child (child);bdrv_unref (child_bs); }
Signature · 1 line
2.31.1
PATCH 13/14 block: Manipulate bs->file / bs->backing pointers in .attach/.detach
In reply to Vladimir Sementsov-Ogievskiybs->file and bs->backing are a kind of duplication of part of bs->children. But very useful diplication, so let's not drop them at all:)
We should manage bs->file and bs->backing in same place, where we manage bs->children, to keep them in sync.
Moreover, generic io paths are unprepared to BdrvChild without a bs, so it's double good to clear bs->file / bs->backing when we detach the child.
Detach is simple: if we detach bs->file or bs->backing child, just set corresponding field to NULL.
Attach is a bit more complicated. But we still can precisely detect should we set one of bs->file / bs->backing or not:
- if role is BDRV_CHILD_COW, we definitely deal with bs->backing
- else, if role is BDRV_CHILD_FILTERED (it must be also BDRV_CHILD_PRIMARY), it's a filtered child. Use bs->drv->filtered_child_is_backing to chose the pointer field to modify.
- else, if role is BDRV_CHILD_PRIMARY, we deal with bs->file
- in all other cases, it's neither bs->backing nor bs->file. It's some other child and we shouldn't care
OK. This change brings one more good thing: we can (and should) get rid of all indirect pointers in the block-graph-change transactions:
bdrv_attach_child_common() stores BdrvChild** into transaction to clear it on abort.
bdrv_attach_child_common() has two callers: bdrv_attach_child_noperm() just pass-through this feature, bdrv_root_attach_child() doesn't need the feature.
Look at bdrv_attach_child_noperm() callers:
- bdrv_attach_child() doesn't need the feature
- bdrv_set_file_or_backing_noperm() uses the feature to manage
bs->file and bs->backing, we don't want it anymore
- bdrv_append() uses the feature to manage bs->backing, again we don't want it anymore
So, we should drop this stuff! Great!
We still keep BdrvChild** argument to return the child and int return value, and not move to simply returning BdrvChild*, as we don't want to lose int return values.
However we don't require *@child to be NULL anymore, and even allow @child to be NULL, if caller don't need the new child pointer.
Finally, we now set .file / .backing automatically in generic code and want to restring setting them by hand outside of .attach/.detach. So, this patch cleanups all remaining places where they were set. To find such places I use:
git grep '\->file ='
git grep '\->backing ='
git grep '&.*\<backing\>'
git grep '&.*\<file\>'
--- include/block/block_int.h | 15 +++- block.c | 155 ++++++++++++++++------------------- block/raw-format.c | 4 +- block/snapshot.c | 1 - tests/unit/test-bdrv-drain.c | 10 +-- 5 files changed, 88 insertions(+), 97 deletions(-)
include/block/block_int.h +12 -3
@@ -937,9 +937,6 @@ struct BlockDriverState {QDict *full_open_options;char exact_filename[PATH_MAX ]; -BdrvChild *backing; -BdrvChild *file; -/* I/O Limits */ BlockLimits bl;
@@ -992,7 +989,19 @@ struct BlockDriverState {* which can affect this node by changing these defaults). This is always a * parent node of this node. */ BlockDriverState *inherits_from; + +/* +* @backing and @file are some of @children or NULL. All these three fields +* (@file, @backing and @children) are modified only in +* bdrv_child_cb_attach() and bdrv_child_cb_detach(). +* +* See also comment in include/block/block.h, to learn how backing and file +* are connected with BdrvChildRole. +*/ QLIST_HEAD(, BdrvChild) children; +BdrvChild *backing; +BdrvChild *file; + QLIST_HEAD(, BdrvChild) parents;QDict *options;
block.c +70 -85
314 lines not on this page · raw message
block/raw-format.c +2 -2
@@ -456,8 +456,8 @@ static int raw_open(BlockDriverState *bs, QDict *options, int flags, file_role =BDRV_CHILD_FILTERED |BDRV_CHILD_PRIMARY ; } - bs->file =bdrv_open_child (NULL , options,"file" , bs, &child_of_bds, - file_role, false, errp); +bdrv_open_child (NULL , options,"file" , bs, &child_of_bds, + file_role, false, errp);if (!bs->file) {return -EINVAL ; }
block/snapshot.c +0 -1
@@ -279,7 +279,6 @@ int bdrv_snapshot_goto(BlockDriverState *bs,/* .bdrv_open() will re-attach it */ bdrv_unref_child (bs, *fallback_ptr); - *fallback_ptr =NULL ; ret =bdrv_snapshot_goto (fallback_bs, snapshot_id, errp); open_ret = drv->bdrv_open (bs, options, bs->open_flags, &local_err);
tests/unit/test-bdrv-drain.c +4 -6
@@ -1804,9 +1804,8 @@ static void test_drop_intermediate_poll(void)for (i =0 ; i <3 ; i++) {if (i) {/* Takes the reference to chain[i - 1] */ - chain[i]->backing =bdrv_attach_child (chain[i], chain[i -1 ], -"chain" , &chain_child_class, -BDRV_CHILD_COW , &error_abort); +bdrv_attach_child (chain[i], chain[i -1 ],"chain" , + &chain_child_class,BDRV_CHILD_COW , &error_abort); } }
@@ -2024,9 +2023,8 @@ static void do_test_replace_child_mid_drain(int old_drain_count, new_child_bs->total_sectors =1 ;bdrv_ref (old_child_bs); - parent_bs->backing =bdrv_attach_child (parent_bs, old_child_bs,"child" , - &child_of_bds,BDRV_CHILD_COW , - &error_abort); +bdrv_attach_child (parent_bs, old_child_bs,"child" , &child_of_bds, +BDRV_CHILD_COW , &error_abort);for (i =0 ; i < old_drain_count; i++) {bdrv_drained_begin (old_child_bs);
Signature · 1 line
2.31.1
PATCH 14/14 block/snapshot: drop indirection around bdrv_snapshot_fallback_ptr
In reply to Vladimir Sementsov-OgievskiyNow the indirection is not actually used, we can safely reduce it to simple pointer.
--- block/snapshot.c | 39 +++++++++++++++++---------------------- 1 file changed, 17 insertions(+), 22 deletions(-)
block/snapshot.c +17 -22
@@ -148,34 +148,29 @@ bool bdrv_snapshot_find_by_id_and_name(BlockDriverState *bs, }/** -* Return a pointer to the child BDS pointer to which we can fall +* Return a pointer to child of given BDS to which we can fall * back if the given BDS does not support snapshots. * Return NULL if there is no BDS to (safely) fall back to. -* -* We need to return an indirect pointer because bdrv_snapshot_goto() -* has to modify the BdrvChild pointer. */ -static BdrvChild **bdrv_snapshot_fallback_ptr (BlockDriverState *bs) +static BdrvChild *bdrv_snapshot_fallback_ptr (BlockDriverState *bs) { -BdrvChild **fallback; -BdrvChild *child =bdrv_primary_child (bs); +BdrvChild *fallback =bdrv_primary_child (bs); +BdrvChild *child;/* We allow fallback only to primary child */ -if (!child) { +if (!fallback) {return NULL ; } - fallback = (child == bs->file ? &bs->file : &bs->backing); -assert (*fallback == child);/* * Check that there are no other children that would need to be * snapshotted. If there are, it is not safe to fall back to -* *fallback. +* fallback. */ QLIST_FOREACH(child, &bs->children, next) {if (child->role & (BDRV_CHILD_DATA |BDRV_CHILD_METADATA |BDRV_CHILD_FILTERED ) && - child != *fallback) + child != fallback) {return NULL ; }
@@ -186,8 +181,8 @@ static BdrvChild **bdrv_snapshot_fallback_ptr(BlockDriverState *bs)static BlockDriverState *bdrv_snapshot_fallback (BlockDriverState *bs) { -BdrvChild **child_ptr =bdrv_snapshot_fallback_ptr (bs); -return child_ptr ? (*child_ptr)->bs :NULL ; +BdrvChild *child_ptr =bdrv_snapshot_fallback_ptr (bs); +return child_ptr ? child_ptr->bs :NULL ; }int bdrv_can_snapshot (BlockDriverState *bs)
@@ -230,7 +225,7 @@ int bdrv_snapshot_goto(BlockDriverState *bs, Error **errp) {BlockDriver *drv = bs->drv; -BdrvChild **fallback_ptr; +BdrvChild *fallback;int ret, open_ret;if (!drv) {
@@ -251,13 +246,13 @@ int bdrv_snapshot_goto(BlockDriverState *bs,return ret; } - fallback_ptr =bdrv_snapshot_fallback_ptr (bs); -if (fallback_ptr) { + fallback =bdrv_snapshot_fallback_ptr (bs); +if (fallback) {QDict *options;QDict *file_options;Error *local_err =NULL ; -BlockDriverState *fallback_bs = (*fallback_ptr)->bs; -char *subqdict_prefix =g_strdup_printf ("%s." , (*fallback_ptr)->name); +BlockDriverState *fallback_bs = fallback->bs; +char *subqdict_prefix =g_strdup_printf ("%s." , fallback->name); options =qdict_clone_shallow (bs->options);
@@ -268,8 +263,8 @@ int bdrv_snapshot_goto(BlockDriverState *bs,qobject_unref (file_options);g_free (subqdict_prefix); -/* Force .bdrv_open() below to re-attach fallback_bs on *fallback_ptr */ -qdict_put_str (options, (*fallback_ptr)->name, +/* Force .bdrv_open() below to re-attach fallback_bs on fallback */ +qdict_put_str (options, fallback->name,bdrv_get_node_name (fallback_bs));/* Now close bs, apply the snapshot on fallback_bs, and re-open bs */
@@ -278,7 +273,7 @@ int bdrv_snapshot_goto(BlockDriverState *bs, }/* .bdrv_open() will re-attach it */ -bdrv_unref_child (bs, *fallback_ptr); +bdrv_unref_child (bs, fallback); ret =bdrv_snapshot_goto (fallback_bs, snapshot_id, errp); open_ret = drv->bdrv_open (bs, options, bs->open_flags, &local_err);
Signature · 1 line
2.31.1
Ping
Quoted 76 lines · Vladimir Sementsov-Ogievskiy
Hi all!
I started this as a follow-up to
"block: Attempt on fixing 030-reported errors" by Hanna.In Hanna's series I really like how
bs->childrenhandling moved to .attach/.detach handlers..file and .backing are kind of "shortcuts" or "links" to some elementes of this list, they duplicate the information. So they should be updated in the same place to be in sync.
On the way to this target, I do also the following cleanups:
- establish, which restrictions we have on how much children of different roles should node has, and which of the should be linked in .file / .backing. Add documentation and assertions.
- drop all the complicated logic around passing pointer to
bs->backing/bs->file(BdrvChild **c), so that the field be automatically updated. Now they are natively automatically updated in .attach/.detach, so the rest of the code becomes simpler.- now .file / .backing are updated ONLY in .attach / .detach, no other code modify these fields
Vladimir Sementsov-Ogievskiy (14):
block: BlockDriver: add .filtered_child_is_backing field
block: introducebdrv_open_file_child()helper block/blklogwrites: don't care to removebs->filechild on failure
test-bdrv-graph-mod: updatetest_parallel_perm_updatetest case
tests-bdrv-drain:bdrv_replace_testdriver: declaresupports_backing
test-bdrv-graph-mod: fix filters to be filters
block: document connection between child roles andbs->backing/bs->fileblock/snapshot: stress that we fallback to primary child Revert "block: Let
replace_child_nopermfree children" Revert "block: Letreplace_child_trankeep indirect pointer" Revert "block: Restructureremove_file_or_backing_child()" Revert "block: Pass BdrvChild ** toreplace_child_noperm"
block: Manipulatebs->file/bs->backingpointers in .attach/.detach block/snapshot: drop indirection aroundbdrv_snapshot_fallback_ptrinclude/block/block.h | 45 +++++ include/block/block_int.h | 30 ++- block.c | 335 ++++++++++--------------------- block/blkdebug.c | 9 +- block/blklogwrites.c | 11 +- block/blkreplay.c | 7 +- block/blkverify.c | 9 +- block/bochs.c | 7 +- block/cloop.c | 7 +- block/commit.c | 1 + block/copy-before-write.c | 9 +- block/copy-on-read.c | 9 +- block/crypto.c | 11 +- block/dmg.c | 7 +- block/filter-compress.c | 6 +- block/mirror.c | 1 + block/parallels.c | 7 +- block/preallocate.c | 9 +- block/qcow.c | 6 +- block/qcow2.c | 8 +- block/qed.c | 8 +- block/raw-format.c | 4 +- block/replication.c | 8 +- block/snapshot.c | 60 ++---- block/throttle.c | 8 +- block/vdi.c | 7 +- block/vhdx.c | 7 +- block/vmdk.c | 7 +- block/vpc.c | 7 +- tests/unit/test-bdrv-drain.c | 11 +- tests/unit/test-bdrv-graph-mod.c | 94 ++++++--- 31 files changed, 343 insertions(+), 412 deletions(-)
Signature · 2 lines
Best regards, Vladimir