block: update graph permissions update
Subsystems (2)
v2 cover block: update graph permissions update
Hi all!
Here is a proposal of updating graph changing procedures.
The thing brought me here is a question about "activating" filters after insertion, which is done in mirror_top and backup_top. The problem is that we can't simply avoid permission conflict when inserting the
filter: during insertion old permissions of relations to be removed conflicting with new permissions of new created relations. And current solution is supporting additional "inactive" mode for the filter when it doesn't require any permissions.
I suggest to change the order of operations: let's first do all graph relations modifications and then refresh permissions. Of course we'll need a way to restore old graph if refresh fails.
Another problem with permission update is that we update permissions in order of DFS which is not always correct. Better is update node when all its parents already updated and require correct permissions. This needs a topological sort of nodes prior to permission update, see more in patches later.
Patches plan:
01,02 - add failing tests to illustrate conceptual problems of current permission update system.
[Here is side suggestion: we usually add tests after fix, so careful
reviewer has to change order of patches to check that test fails before fix. I add tests in the way the may be simply executed but not yet take part in make check. It seems more native: first show the problem, then fix it. And when fixed, make tests available for make check]
03-09 - some perparations, refactorings which may go in separate
10 - new transaction API
15 - toplogical sort implemented for permission update, one of new tests now pass
19 - improve bdrv_replace_node. second new test now pass
26 - drop .active field and activation procedure for backup-top!
30 - update bdrv_reopen_multiple. At this point everything is using new paradigm of permission update
31-36 - post refactoring, drop old interfaces, we are done.
Note, that this series does nothing with another graph-update problem discussed under "[PATCH RFC 0/5] Fix accidental crash in iotest 30".
The series based on block-next Max's branch and can be found here:
git: https://src.openvz.org/scm/~vsementsov/qemu.git
tag: up-block-topologic-perm-v2
Vladimir Sementsov-Ogievskiy (36):
tests/test-bdrv-graph-mod: add test_parallel_exclusive_write tests/test-bdrv-graph-mod: add test_parallel_perm_update
block: bdrv_append(): don't consume reference
block: bdrv_append(): return status
block: add bdrv_parent_try_set_aio_context
block: BdrvChildClass: add .get_parent_aio_context handler
block: drop ctx argument from bdrv_root_attach_child
block: make bdrv_reopen_{prepare,commit,abort} private
block: return value from bdrv_replace_node()
util: add transactions.c
block: bdrv_refresh_perms: check parents compliance
block: refactor bdrv_child* permission functions
block: rewrite bdrv_child_try_set_perm() using bdrv_refresh_perms()
block: inline bdrv_child_*() permission functions calls
block: use topological sort for permission update
block: add bdrv_drv_set_perm transaction action
block: add bdrv_list_* permission update functions
block: add bdrv_replace_child_safe() transaction action
block: fix bdrv_replace_node_common
block: add bdrv_attach_child_common() transaction action
block: add bdrv_attach_child_noperm() transaction action
block: split out bdrv_replace_node_noperm()
block: adapt bdrv_append() for inserting filters
block: add bdrv_remove_backing transaction action
block: introduce bdrv_drop_filter()
block/backup-top: drop .active
block: drop ignore_children for permission update functions
block: add bdrv_set_backing_noperm() transaction action
blockdev: qmp_x_blockdev_reopen: acquire all contexts
block: bdrv_reopen_multiple: refresh permissions on updated graph
block: drop unused permission update functions
block: inline bdrv_check_perm_common()
block: inline bdrv_replace_child()
block: refactor bdrv_child_set_perm_safe() transaction action
block: rename bdrv_replace_child_safe() to bdrv_replace_child()
block: refactor bdrv_node_check_perm()
include/block/block.h | 20 +- include/block/block_int.h | 8 +- include/qemu/transactions.h | 46 ++ block.c | 1319 ++++++++++++++++++++--------------- block/backup-top.c | 39 +- block/block-backend.c | 13 +- block/commit.c | 7 +- block/file-posix.c | 84 +-- block/mirror.c | 9 +- blockdev.c | 33 +- blockjob.c | 11 +- tests/test-bdrv-drain.c | 2 +- tests/test-bdrv-graph-mod.c | 122 +++- util/transactions.c | 81 +++ tests/qemu-iotests/283.out | 2 +- util/meson.build | 1 + 16 files changed, 1100 insertions(+), 697 deletions(-) create mode 100644 include/qemu/transactions.h create mode 100644 util/transactions.c
Signature · 1 line
2.21.3
PATCH v2 1/36 tests/test-bdrv-graph-mod: add test_parallel_exclusive_write
Add the test that shows that concept of ignore_children is incomplete. Actually, when we want to update something, ignoring permission of some existing BdrvChild, we should ignore also the propagated effect of this child to the other children. But that's not done. Better approach (update permissions on already updated graph) will be implemented later.
Now the test fails, so it's added with -d argument to not break make check.
Test fails with
"Conflicts with use by fl1 as 'backing', which does not allow 'write' on base"
because when updating permissions we can ignore original top->fl1 BdrvChild. But we don't ignore exclusive write permission in fl1->base BdrvChild, which is propagated. Correct thing to do is make graph change first and then do permission update from the top node.
To run test do
./test-bdrv-graph-mod -d -p /bdrv-graph-mod/parallel-exclusive-write
from <build-directory>/tests.
--- tests/test-bdrv-graph-mod.c | 62 +++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+)
tests/test-bdrv-graph-mod.c +62 -0
@@ -44,6 +44,21 @@ static BlockDriver bdrv_no_perm = { .bdrv_child_perm = no_perm_default_perms, }; +static void exclusive_write_perms (BlockDriverState *bs,BdrvChild *c, +BdrvChildRole role, +BlockReopenQueue *reopen_queue, +uint64_t perm,uint64_t shared, +uint64_t *nperm,uint64_t *nshared) +{ + *nperm =BLK_PERM_WRITE ; + *nshared =BLK_PERM_ALL & ~BLK_PERM_WRITE ; +} + +static BlockDriver bdrv_exclusive_writer = { + .format_name ="exclusive-writer" , + .bdrv_child_perm = exclusive_write_perms, +}; +static BlockDriverState *no_perm_node (const char *name) {return bdrv_new_open_driver (&bdrv_no_perm, name,BDRV_O_RDWR , &error_abort);
@@ -55,6 +70,12 @@ static BlockDriverState *pass_through_node(const char *name) BDRV_O_RDWR, &error_abort); } +static BlockDriverState *exclusive_writer_node (const char *name) +{ +return bdrv_new_open_driver (&bdrv_exclusive_writer, name, +BDRV_O_RDWR , &error_abort); +} +/* * test_update_perm_tree *
@@ -185,8 +206,44 @@ static void test_should_update_child(void)blk_unref (root); } +/* +* test_parallel_exclusive_write +* +* Check that when we replace node, old permissions of the node being removed +* doesn't break the replacement. +*/ +static void test_parallel_exclusive_write (void ) +{ +BlockDriverState *top =exclusive_writer_node ("top" ); +BlockDriverState *base =no_perm_node ("base" ); +BlockDriverState *fl1 =pass_through_node ("fl1" ); +BlockDriverState *fl2 =pass_through_node ("fl2" ); + +bdrv_attach_child (top, fl1,"backing" , &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); +bdrv_ref (base); + +bdrv_replace_node (fl1, fl2, &error_abort); + +bdrv_unref (top); +} +int main (int argc,char *argv[]) { +int i; +bool debug = false; + +for (i =1 ; i < argc; i++) { +if (!strcmp (argv[i],"-d" )) { + debug = true; +break ; + } + } +bdrv_init ();qemu_init_main_loop (&error_abort);
@@ -196,5 +253,10 @@ int main(int argc, char *argv[])g_test_add_func ("/bdrv-graph-mod/should-update-child" , test_should_update_child); +if (debug) { +g_test_add_func ("/bdrv-graph-mod/parallel-exclusive-write" , + test_parallel_exclusive_write); + } +return g_test_run (); }
Signature · 1 line
2.21.3
PATCH v2 2/36 tests/test-bdrv-graph-mod: add test_parallel_perm_update
In reply to Vladimir Sementsov-OgievskiyAdd test to show that simple DFS recursion order is not correct for permission update. Correct order is topological-sort order, which will be introduced later.
Consider the block driver which has two filter children: one active with exclusive write access and one inactive with no specific permissions.
And, these two children has a common base child, like this:
┌─────┐ ┌──────┐
│ fl2 │ ◀── │ top │
└─────┘ └──────┘
│ │
│ │ w
│ ▼
│ ┌──────┐
│ │ fl1 │
│ └──────┘
│ │
│ │ w
│ ▼
│ ┌──────┐
└───────▶ │ base │
└──────┘So, exclusive write is propagated.
Assume, we want to make fl2 active instead of fl1. So, we set some option for top driver and do permission update.
If permission update (remember, it's DFS) goes first through top->fl1->base branch it will succeed: it firstly drop exclusive write permissions and than apply them for another BdrvChildren. But if permission update goes first through top->fl2->base branch it will fail, as when we try to update fl2->base child, old not yet updated fl1->base child will be in conflict.
Now test fails, so it runs only with -d flag. To run do
./test-bdrv-graph-mod -d -p /bdrv-graph-mod/parallel-perm-update
from <build-directory>/tests.
--- tests/test-bdrv-graph-mod.c | 64 +++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+)
tests/test-bdrv-graph-mod.c +64 -0
@@ -232,6 +232,68 @@ 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) +{ +if (bs->file && c == bs->file) { + *nperm =BLK_PERM_WRITE ; + *nshared =BLK_PERM_ALL & ~BLK_PERM_WRITE ; + }else { + *nperm =0 ; + *nshared =BLK_PERM_ALL ; + } +} + +static BlockDriver bdrv_write_to_file = { + .format_name ="tricky-perm" , + .bdrv_child_perm = write_to_file_perms, +}; + +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 , + &error_abort); +BlockDriverState *base =no_perm_node ("base" ); +BlockDriverState *fl1 =pass_through_node ("fl1" ); +BlockDriverState *fl2 =pass_through_node ("fl2" ); +BdrvChild *c_fl1, *c_fl2; + +bdrv_attach_child (top, tricky,"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); +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); +bdrv_ref (base); + +/* Select fl1 as first child to be active */ + tricky->file = c_fl1; +bdrv_child_refresh_perms (top, top->children.lh_first, &error_abort); + +assert (c_fl1->perm &BLK_PERM_WRITE ); + +/* Now, try to switch active child and update permissions */ + tricky->file = c_fl2; +bdrv_child_refresh_perms (top, top->children.lh_first, &error_abort); + +assert (c_fl2->perm &BLK_PERM_WRITE ); + +/* Switch once more, to not care about real child order in the list */ + tricky->file = c_fl1; +bdrv_child_refresh_perms (top, top->children.lh_first, &error_abort); + +assert (c_fl1->perm &BLK_PERM_WRITE ); +} +int main (int argc,char *argv[]) {int i;
@@ -256,6 +318,8 @@ int main(int argc, char *argv[])if (debug) {g_test_add_func ("/bdrv-graph-mod/parallel-exclusive-write" , test_parallel_exclusive_write); +g_test_add_func ("/bdrv-graph-mod/parallel-perm-update" , + test_parallel_perm_update); }return g_test_run ();
Signature · 1 line
2.21.3
Re: [PATCH v2 02/36] tests/test-bdrv-graph-mod: add test_parallel_perm_update
Am 27.11.2020 um 15:44 hat Vladimir Sementsov-Ogievskiy geschrieben:
Quoted 107 lines
Add test to show that simple DFS recursion order is not correct for permission update. Correct order is topological-sort order, which will be introduced later.
Consider the block driver which has two filter children: one active with exclusive write access and one inactive with no specific permissions.
And, these two children has a common base child, like this:
┌─────┐ ┌──────┐ │ fl2 │ ◀── │ top │ └─────┘ └──────┘ │ │ │ │ w │ ▼ │ ┌──────┐ │ │ fl1 │ │ └──────┘ │ │ │ │ w │ ▼ │ ┌──────┐ └───────▶ │ base │ └──────┘So, exclusive write is propagated.
Assume, we want to make fl2 active instead of fl1. So, we set some option for top driver and do permission update.
If permission update (remember, it's DFS) goes first through
top->fl1->basebranch it will succeed: it firstly drop exclusive write permissions and than apply them for another BdrvChildren. But if permission update goes first throughtop->fl2->basebranch it will fail, as when we try to updatefl2->basechild, old not yet updatedfl1->basechild will be in conflict.Now test fails, so it runs only with -d flag. To run do
./test-bdrv-graph-mod -d -p /bdrv-graph-mod/parallel-perm-update
from <build-directory>/tests.
Signed-off-by Vladimir Sementsov-Ogievskiy <[email protected]>--- tests/test-bdrv-graph-mod.c | 64 +++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+)tests/test-bdrv-graph-mod.c +50
-0@@ -232,6 +232,68 @@ 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) +{ +if (bs->file && c == bs->file) { + *nperm =BLK_PERM_WRITE ; + *nshared =BLK_PERM_ALL & ~BLK_PERM_WRITE ; + }else { + *nperm =0 ; + *nshared =BLK_PERM_ALL ; + } +} + +static BlockDriver bdrv_write_to_file = { + .format_name ="tricky-perm" , + .bdrv_child_perm = write_to_file_perms, +}; + +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 , + &error_abort); +BlockDriverState *base =no_perm_node ("base" ); +BlockDriverState *fl1 =pass_through_node ("fl1" ); +BlockDriverState *fl2 =pass_through_node ("fl2" ); +BdrvChild *c_fl1, *c_fl2; + +bdrv_attach_child (top, tricky,"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); +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); +bdrv_ref (base); + +/* Select fl1 as first child to be active */ + tricky->file = c_fl1; +bdrv_child_refresh_perms (top, top->children.lh_first, &error_abort); + +assert (c_fl1->perm &BLK_PERM_WRITE ); + +/* Now, try to switch active child and update permissions */
tests/test-bdrv-graph-mod.c static void test_parallel_exclusive_write(void) · line 294
Quoted 10 lines
tests/test-bdrv-graph-mod.c +10
-0+ tricky->file = c_fl2; +bdrv_child_refresh_perms (top, top->children.lh_first, &error_abort); + +assert (c_fl2->perm &BLK_PERM_WRITE ); + +/* Switch once more, to not care about real child order in the list */ + tricky->file = c_fl1; +bdrv_child_refresh_perms (top, top->children.lh_first, &error_abort); + +assert (c_fl1->perm &BLK_PERM_WRITE );
Should we also assert in each case that the we don't hole the write permission for the inactive child?
Kevin
Re: [PATCH v2 02/36] tests/test-bdrv-graph-mod: add test_parallel_perm_update
Quoted 113 lines · Kevin Wolf
Am 27.11.2020 um 15:44 hat Vladimir Sementsov-Ogievskiy geschrieben:
Quoted 112 lines
Add test to show that simple DFS recursion order is not correct for permission update. Correct order is topological-sort order, which will be introduced later.
Consider the block driver which has two filter children: one active with exclusive write access and one inactive with no specific permissions.
And, these two children has a common base child, like this:
┌─────┐ ┌──────┐ │ fl2 │ ◀── │ top │ └─────┘ └──────┘ │ │ │ │ w │ ▼ │ ┌──────┐ │ │ fl1 │ │ └──────┘ │ │ │ │ w │ ▼ │ ┌──────┐ └───────▶ │ base │ └──────┘So, exclusive write is propagated.
Assume, we want to make fl2 active instead of fl1. So, we set some option for top driver and do permission update.
If permission update (remember, it's DFS) goes first through
top->fl1->basebranch it will succeed: it firstly drop exclusive write permissions and than apply them for another BdrvChildren. But if permission update goes first throughtop->fl2->basebranch it will fail, as when we try to updatefl2->basechild, old not yet updatedfl1->basechild will be in conflict.Now test fails, so it runs only with -d flag. To run do
./test-bdrv-graph-mod -d -p /bdrv-graph-mod/parallel-perm-update
from <build-directory>/tests.
Signed-off-by Vladimir Sementsov-Ogievskiy <[email protected]>--- tests/test-bdrv-graph-mod.c | 64 +++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+)tests/test-bdrv-graph-mod.c +55
-0@@ -232,6 +232,68 @@ 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) +{ +if (bs->file && c == bs->file) { + *nperm =BLK_PERM_WRITE ; + *nshared =BLK_PERM_ALL & ~BLK_PERM_WRITE ; + }else { + *nperm =0 ; + *nshared =BLK_PERM_ALL ; + } +} + +static BlockDriver bdrv_write_to_file = { + .format_name ="tricky-perm" , + .bdrv_child_perm = write_to_file_perms, +}; + +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 , + &error_abort); +BlockDriverState *base =no_perm_node ("base" ); +BlockDriverState *fl1 =pass_through_node ("fl1" ); +BlockDriverState *fl2 =pass_through_node ("fl2" ); +BdrvChild *c_fl1, *c_fl2; + +bdrv_attach_child (top, tricky,"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); +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); +bdrv_ref (base); + +/* Select fl1 as first child to be active */ + tricky->file = c_fl1; +bdrv_child_refresh_perms (top, top->children.lh_first, &error_abort); + +assert (c_fl1->perm &BLK_PERM_WRITE ); + +/* Now, try to switch active child and update permissions */ + tricky->file = c_fl2; +bdrv_child_refresh_perms (top, top->children.lh_first, &error_abort); + +assert (c_fl2->perm &BLK_PERM_WRITE ); +
Quoted 9 lines
tests/test-bdrv-graph-mod.c static void test_parallel_exclusive_write(void) · line 294
Quoted 5 lines
tests/test-bdrv-graph-mod.c +5
-0+/* Switch once more, to not care about real child order in the list */ + tricky->file = c_fl1; +bdrv_child_refresh_perms (top, top->children.lh_first, &error_abort); + +assert (c_fl1->perm &BLK_PERM_WRITE );Should we also assert in each case that the we don't hole the write permission for the inactive child?
Won't hurt, will add
Signature · 2 lines
Best regards, Vladimir
PATCH v2 3/36 block: bdrv_append(): don't consume reference
In reply to Vladimir Sementsov-OgievskiyWe have too much comments for this feature. It seems better just don't do it. Most of real users (tests don't count) have to create additional reference.
Drop also comment in external_snapshot_prepare:
- bdrv_append doesn't "remove" old bs in common sense, it sounds strange
- the fact that bdrv_append can fail is obvious from the context
- the fact that we must rollback all changes in transaction abort is known (it's the direct role of abort)
--- block.c | 19 +++---------------- block/backup-top.c | 1 - block/commit.c | 1 + block/mirror.c | 3 --- blockdev.c | 4 ---- tests/test-bdrv-drain.c | 2 +- tests/test-bdrv-graph-mod.c | 2 ++ 7 files changed, 7 insertions(+), 25 deletions(-)
block.c +3 -16
@@ -3145,11 +3145,6 @@ static BlockDriverState *bdrv_append_temp_snapshot(BlockDriverState *bs, goto out; } -/* bdrv_append() consumes a strong reference to bs_snapshot -* (i.e. it will call bdrv_unref() on it) even on error, so in -* order to be able to return one, we have to increase -* bs_snapshot's refcount here */ -bdrv_ref (bs_snapshot);bdrv_append (bs_snapshot, bs, &local_err);if (local_err) {error_propagate (errp, local_err);
@@ -4608,10 +4603,8 @@ void bdrv_replace_node(BlockDriverState *from, BlockDriverState *to,* * This function does not create any image files. * -* bdrv_append() takes ownership of a bs_new reference and unrefs it because -* that's what the callers commonly need. bs_new will be referenced by the old -* parents of bs_top after bdrv_append() returns. If the caller needs to keep a -* reference of its own, it must call bdrv_ref(). +* Recent update: bdrv_append does NOT eat bs_new reference for now. Drop this +* comment several moths later. */ void bdrv_append (BlockDriverState *bs_new,BlockDriverState *bs_top,Error **errp)
@@ -4621,20 +4614,14 @@ void bdrv_append(BlockDriverState *bs_new, BlockDriverState *bs_top,bdrv_set_backing_hd (bs_new, bs_top, &local_err);if (local_err) {error_propagate (errp, local_err); - goto out; +return ; }bdrv_replace_node (bs_top, bs_new, &local_err);if (local_err) {error_propagate (errp, local_err);bdrv_set_backing_hd (bs_new,NULL , &error_abort); - goto out; } - -/* bs_new is now referenced by its new parents, we don't need the -* additional reference any more. */ -out: -bdrv_unref (bs_new); }static void bdrv_delete (BlockDriverState *bs)
block/backup-top.c +0 -1
@@ -222,7 +222,6 @@ BlockDriverState *bdrv_backup_top_append(BlockDriverState *source,bdrv_drained_begin (source); -bdrv_ref (top);bdrv_append (top, source, &local_err);if (local_err) {error_prepend (&local_err,"Cannot append backup-top filter: " );
block/commit.c +1 -0
@@ -313,6 +313,7 @@ void commit_start(const char *job_id, BlockDriverState *bs, commit_top_bs->total_sectors = top->total_sectors;bdrv_append (commit_top_bs, top, &local_err); +bdrv_unref (commit_top_bs);/* referenced by new parents or failed */ if (local_err) { commit_top_bs =NULL ;error_propagate (errp, local_err);
block/mirror.c +0 -3
@@ -1605,9 +1605,6 @@ static BlockJob *mirror_start_job( bs_opaque =g_new0 (MirrorBDSOpaque,1 ); mirror_top_bs->opaque = bs_opaque; -/* bdrv_append takes ownership of the mirror_top_bs reference, need to keep -* it alive until block_job_create() succeeds even if bs has no parent. */ -bdrv_ref (mirror_top_bs);bdrv_drained_begin (bs);bdrv_append (mirror_top_bs, bs, &local_err);bdrv_drained_end (bs);
blockdev.c +0 -4
@@ -1587,10 +1587,6 @@ static void external_snapshot_prepare(BlkActionState *common, goto out; } -/* This removes our old bs and adds the new bs. This is an operation that -* can fail, so we need to do it in .prepare; undoing it for abort is -* always possible. */ -bdrv_ref (state->new_bs);bdrv_append (state->new_bs, state->old_bs, &local_err);if (local_err) {error_propagate (errp, local_err);
tests/test-bdrv-drain.c +1 -1
@@ -1478,7 +1478,6 @@ static void test_append_to_drained(void) g_assert_cmpint(base_s->drain_count, ==, 1); g_assert_cmpint(base->in_flight, ==, 0); -/* Takes ownership of overlay, so we don't have to unref it later */ bdrv_append (overlay, base, &error_abort); g_assert_cmpint(base->in_flight, ==, 0); g_assert_cmpint(overlay->in_flight, ==, 0);
@@ -1495,6 +1494,7 @@ static void test_append_to_drained(void) g_assert_cmpint(overlay->quiesce_counter, ==, 0); g_assert_cmpint(overlay_s->drain_count, ==, 0); +bdrv_unref (overlay);bdrv_unref (base);blk_unref (blk); }
tests/test-bdrv-graph-mod.c +2 -0
@@ -138,6 +138,7 @@ static void test_update_perm_tree(void)bdrv_append (filter, bs, &local_err);error_free_or_abort (&local_err); +bdrv_unref (filter);blk_unref (root); }
@@ -202,6 +203,7 @@ static void test_should_update_child(void)bdrv_append (filter, bs, &error_abort);g_assert (target->backing->bs == bs); +bdrv_unref (filter);bdrv_unref (bs);blk_unref (root); }
Signature · 1 line
2.21.3
Re: [PATCH v2 03/36] block: bdrv_append(): don't consume reference
Am 27.11.2020 um 15:44 hat Vladimir Sementsov-Ogievskiy geschrieben:
Quoted 39 lines
We have too much comments for this feature. It seems better just don't do it. Most of real users (tests don't count) have to create additional reference.
Drop also comment in
external_snapshot_prepare:
-bdrv_appenddoesn't "remove" old bs in common sense, it sounds strange
- the fact thatbdrv_appendcan fail is obvious from the context
- the fact that we must rollback all changes in transaction abort is known (it's the direct role of abort)Signed-off-by Vladimir Sementsov-Ogievskiy <[email protected]>--- block.c | 19 +++---------------- block/backup-top.c | 1 - block/commit.c | 1 + block/mirror.c | 3 --- blockdev.c | 4 ---- tests/test-bdrv-drain.c | 2 +- tests/test-bdrv-graph-mod.c | 2 ++ 7 files changed, 7 insertions(+), 25 deletions(-)block.c +0
-5@@ -3145,11 +3145,6 @@ static BlockDriverState *bdrv_append_temp_snapshot(BlockDriverState *bs, goto out; } -/* bdrv_append() consumes a strong reference to bs_snapshot -* (i.e. it will call bdrv_unref() on it) even on error, so in -* order to be able to return one, we have to increase -* bs_snapshot's refcount here */ -bdrv_ref (bs_snapshot);bdrv_append (bs_snapshot, bs, &local_err);if (local_err) {error_propagate (errp, local_err);
block.c void bdrv_replace_node(BlockDriverState *from, BlockDriverState *to, · line 4607
Quoted 9 lines
block.c +2
-4@@ -4608,10 +4603,8 @@ void bdrv_replace_node(BlockDriverState *from, BlockDriverState *to,* * This function does not create any image files. * -* bdrv_append() takes ownership of a bs_new reference and unrefs it because -* that's what the callers commonly need. bs_new will be referenced by the old -* parents of bs_top after bdrv_append() returns. If the caller needs to keep a -* reference of its own, it must call bdrv_ref(). +* Recent update: bdrv_append does NOT eat bs_new reference for now. Drop this +* comment several moths later.
A comment like this is unusual. Do you think there is a high risk of somebody introducing a new bdrv_append() in parallel and that they would read this comment when rebasing their existing patches?
If we do keep the comment: s/for now/now/ (it has recently changed, we're not intending to change it later) and s/moths/months/.
Quoted 6 lines
block.c +0
-0*/ void bdrv_append (BlockDriverState *bs_new,BlockDriverState *bs_top,Error **errp)@@ -4621,20 +4614,14 @@ void bdrv_append(BlockDriverState *bs_new, BlockDriverState *bs_top,bdrv_set_backing_hd (bs_new, bs_top, &local_err);if (local_err) {
block.c void bdrv_append(BlockDriverState *bs_new, BlockDriverState *bs_top, · line 4631
Quoted 10 lines
block.c +1
-2error_propagate (errp, local_err); - goto out; +return ; }bdrv_replace_node (bs_top, bs_new, &local_err);if (local_err) {error_propagate (errp, local_err);bdrv_set_backing_hd (bs_new,NULL , &error_abort); - goto out;
Can we leave a return here just in case that new code will be added at the end of the function?
Quoted 52 lines
block.c +0
-5} - -/* bs_new is now referenced by its new parents, we don't need the -* additional reference any more. */ -out: -bdrv_unref (bs_new); }static void bdrv_delete (BlockDriverState *bs)block/backup-top.c +0
-1@@ -222,7 +222,6 @@ BlockDriverState *bdrv_backup_top_append(BlockDriverState *source,bdrv_drained_begin (source); -bdrv_ref (top);bdrv_append (top, source, &local_err);if (local_err) {error_prepend (&local_err,"Cannot append backup-top filter: " );block/commit.c +1
-0@@ -313,6 +313,7 @@ void commit_start(const char *job_id, BlockDriverState *bs, commit_top_bs->total_sectors = top->total_sectors;bdrv_append (commit_top_bs, top, &local_err); +bdrv_unref (commit_top_bs);/* referenced by new parents or failed */ if (local_err) { commit_top_bs =NULL ;error_propagate (errp, local_err);block/mirror.c +0
-3@@ -1605,9 +1605,6 @@ static BlockJob *mirror_start_job( bs_opaque =g_new0 (MirrorBDSOpaque,1 ); mirror_top_bs->opaque = bs_opaque; -/* bdrv_append takes ownership of the mirror_top_bs reference, need to keep -* it alive until block_job_create() succeeds even if bs has no parent. */ -bdrv_ref (mirror_top_bs);bdrv_drained_begin (bs);bdrv_append (mirror_top_bs, bs, &local_err);bdrv_drained_end (bs);blockdev.c +0
-0
blockdev.c static void external_snapshot_prepare(BlkActionState *common, · line 1592
Quoted 6 lines
blockdev.c +0
-3@@ -1587,10 +1587,6 @@ static void external_snapshot_prepare(BlkActionState *common, goto out; } -/* This removes our old bs and adds the new bs. This is an operation that -* can fail, so we need to do it in .prepare; undoing it for abort is -* always possible. */
This comment is still relevant, it's unrelated to the bdrv_ref().
blockdev.c static void external_snapshot_prepare(BlkActionState *common, · line 1592
Quoted 4 lines
blockdev.c +0
-1-bdrv_ref (state->new_bs);bdrv_append (state->new_bs, state->old_bs, &local_err);if (local_err) {error_propagate (errp, local_err);
Kevin
Re: [PATCH v2 03/36] block: bdrv_append(): don't consume reference
Quoted 45 lines · Kevin Wolf
Am 27.11.2020 um 15:44 hat Vladimir Sementsov-Ogievskiy geschrieben:
Quoted 44 lines
We have too much comments for this feature. It seems better just don't do it. Most of real users (tests don't count) have to create additional reference.
Drop also comment in
external_snapshot_prepare:
-bdrv_appenddoesn't "remove" old bs in common sense, it sounds strange
- the fact thatbdrv_appendcan fail is obvious from the context
- the fact that we must rollback all changes in transaction abort is known (it's the direct role of abort)Signed-off-by Vladimir Sementsov-Ogievskiy <[email protected]>--- block.c | 19 +++---------------- block/backup-top.c | 1 - block/commit.c | 1 + block/mirror.c | 3 --- blockdev.c | 4 ---- tests/test-bdrv-drain.c | 2 +- tests/test-bdrv-graph-mod.c | 2 ++ 7 files changed, 7 insertions(+), 25 deletions(-)block.c +0
-7@@ -3145,11 +3145,6 @@ static BlockDriverState *bdrv_append_temp_snapshot(BlockDriverState *bs, goto out; } -/* bdrv_append() consumes a strong reference to bs_snapshot -* (i.e. it will call bdrv_unref() on it) even on error, so in -* order to be able to return one, we have to increase -* bs_snapshot's refcount here */ -bdrv_ref (bs_snapshot);bdrv_append (bs_snapshot, bs, &local_err);if (local_err) {error_propagate (errp, local_err);@@ -4608,10 +4603,8 @@ void bdrv_replace_node(BlockDriverState *from, BlockDriverState *to,* * This function does not create any image files. * -* bdrv_append() takes ownership of a bs_new reference and unrefs it because -* that's what the callers commonly need. bs_new will be referenced by the old
Quoted 8 lines
block.c void bdrv_replace_node(BlockDriverState *from, BlockDriverState *to, · line 4607
Quoted 4 lines
block.c +2
-2-* parents of bs_top after bdrv_append() returns. If the caller needs to keep a -* reference of its own, it must call bdrv_ref(). +* Recent update: bdrv_append does NOT eat bs_new reference for now. Drop this +* comment several moths later. A comment like this is unusual. Do you think there is a high risk of somebody introducing a new
bdrv_append()in parallel and that they would read this comment when rebasing their existing patches?
Or even later, someone may remember that bdrv_append() eat the reference, and then face some strange behavior with a new call of bdrv_append(), finally go to check the function code and see the new comment.. I don't insist, we can drop the comment
Quoted 15 lines
If we do keep the comment: s/for now/now/ (it has recently changed, we're not intending to change it later) and s/moths/months/.
Quoted 11 lines
block.c +1
-1*/ void bdrv_append (BlockDriverState *bs_new,BlockDriverState *bs_top,Error **errp)@@ -4621,20 +4614,14 @@ void bdrv_append(BlockDriverState *bs_new, BlockDriverState *bs_top,bdrv_set_backing_hd (bs_new, bs_top, &local_err);if (local_err) {error_propagate (errp, local_err); - goto out; +return ; }
Quoted 8 lines
block.c void bdrv_append(BlockDriverState *bs_new, BlockDriverState *bs_top, · line 4631
Quoted 5 lines
block.c +0
-1bdrv_replace_node (bs_top, bs_new, &local_err);if (local_err) {error_propagate (errp, local_err);bdrv_set_backing_hd (bs_new,NULL , &error_abort); - goto out;Can we leave a return here just in case that new code will be added at the end of the function?
sure
Quoted 56 lines
Quoted 56 lines
block.c +0
-5} - -/* bs_new is now referenced by its new parents, we don't need the -* additional reference any more. */ -out: -bdrv_unref (bs_new); }static void bdrv_delete (BlockDriverState *bs)block/backup-top.c +0
-1@@ -222,7 +222,6 @@ BlockDriverState *bdrv_backup_top_append(BlockDriverState *source,bdrv_drained_begin (source); -bdrv_ref (top);bdrv_append (top, source, &local_err);if (local_err) {error_prepend (&local_err,"Cannot append backup-top filter: " );block/commit.c +1
-0@@ -313,6 +313,7 @@ void commit_start(const char *job_id, BlockDriverState *bs, commit_top_bs->total_sectors = top->total_sectors;bdrv_append (commit_top_bs, top, &local_err); +bdrv_unref (commit_top_bs);/* referenced by new parents or failed */ if (local_err) { commit_top_bs =NULL ;error_propagate (errp, local_err);block/mirror.c +0
-3@@ -1605,9 +1605,6 @@ static BlockJob *mirror_start_job( bs_opaque =g_new0 (MirrorBDSOpaque,1 ); mirror_top_bs->opaque = bs_opaque; -/* bdrv_append takes ownership of the mirror_top_bs reference, need to keep -* it alive until block_job_create() succeeds even if bs has no parent. */ -bdrv_ref (mirror_top_bs);bdrv_drained_begin (bs);bdrv_append (mirror_top_bs, bs, &local_err);bdrv_drained_end (bs);blockdev.c +0
-0@@ -1587,10 +1587,6 @@ static void external_snapshot_prepare(BlkActionState *common, goto out; }
Quoted 5 lines
blockdev.c static void external_snapshot_prepare(BlkActionState *common, · line 1592
Quoted 3 lines
blockdev.c +0
-3-/* This removes our old bs and adds the new bs. This is an operation that -* can fail, so we need to do it in .prepare; undoing it for abort is -* always possible. */ This comment is still relevant, it's unrelated to the
bdrv_ref().
I described in commit message, why I dislike this comment :) I can keep it of course, it's not critical
Quoted 8 lines
blockdev.c static void external_snapshot_prepare(BlkActionState *common, · line 1592
Quoted 5 lines
blockdev.c +0
-1-bdrv_ref (state->new_bs);bdrv_append (state->new_bs, state->old_bs, &local_err);if (local_err) {error_propagate (errp, local_err);Kevin
Signature · 2 lines
Best regards, Vladimir
Re: [PATCH v2 03/36] block: bdrv_append(): don't consume reference
Am 18.01.2021 um 18:21 hat Vladimir Sementsov-Ogievskiy geschrieben:
Quoted 28 lines
Quoted 14 lines · Kevin Wolf
Am 27.11.2020 um 15:44 hat Vladimir Sementsov-Ogievskiy geschrieben:
Quoted 12 lines
We have too much comments for this feature. It seems better just don't do it. Most of real users (tests don't count) have to create additional reference.
Drop also comment in
external_snapshot_prepare:
-bdrv_appenddoesn't "remove" old bs in common sense, it sounds strange
- the fact thatbdrv_appendcan fail is obvious from the context
- the fact that we must rollback all changes in transaction abort is known (it's the direct role of abort)Signed-off-by Vladimir Sementsov-Ogievskiy <[email protected]>Quoted 12 lines
blockdev.c static void external_snapshot_prepare(BlkActionState *common, · line 1591
Quoted 10 lines
blockdev.c +0
-3@@ -1587,10 +1587,6 @@ static void external_snapshot_prepare(BlkActionState *common, goto out; } -/* This removes our old bs and adds the new bs. This is an operation that -* can fail, so we need to do it in .prepare; undoing it for abort is -* always possible. */ This comment is still relevant, it's unrelated to the
bdrv_ref().
Quoted 2 lines
I described in commit message, why I dislike this comment :) I can keep it of course, it's not critical
Ah, right, I missed this bit in the commit message (or forgot it until I reached this hunk) and thought it was an accidental removal.
If it's intentional, no reason to change the patch.
Kevin
PATCH v2 4/36 block: bdrv_append(): return status
In reply to Vladimir Sementsov-OgievskiyReturn int status to avoid extra error propagation schemes.
--- include/block/block.h | 4 ++-- block.c | 15 ++++++++------- block/commit.c | 6 ++---- block/mirror.c | 6 ++---- blockdev.c | 6 +++--- tests/test-bdrv-graph-mod.c | 6 +++--- 6 files changed, 20 insertions(+), 23 deletions(-)
include/block/block.h +2 -2
@@ -344,8 +344,8 @@ int bdrv_create(BlockDriver *drv, const char* filename,int bdrv_create_file (const char *filename,QemuOpts *opts,Error **errp);BlockDriverState *bdrv_new (void ); -void bdrv_append (BlockDriverState *bs_new,BlockDriverState *bs_top, -Error **errp); +int bdrv_append (BlockDriverState *bs_new,BlockDriverState *bs_top, +Error **errp);void bdrv_replace_node (BlockDriverState *from,BlockDriverState *to,Error **errp);
block.c +8 -7
@@ -3103,7 +3103,6 @@ static BlockDriverState *bdrv_append_temp_snapshot(BlockDriverState *bs,int64_t total_size;QemuOpts *opts =NULL ;BlockDriverState *bs_snapshot =NULL ; -Error *local_err =NULL ;int ret;/* if snapshot, we create a temporary backing file and open it
@@ -3145,9 +3144,8 @@ static BlockDriverState *bdrv_append_temp_snapshot(BlockDriverState *bs, goto out; } -bdrv_append (bs_snapshot, bs, &local_err); -if (local_err) { -error_propagate (errp, local_err); + ret =bdrv_append (bs_snapshot, bs, errp); +if (ret <0 ) { bs_snapshot =NULL ; goto out; }
@@ -4606,22 +4604,25 @@ void bdrv_replace_node(BlockDriverState *from, BlockDriverState *to,* Recent update: bdrv_append does NOT eat bs_new reference for now. Drop this * comment several moths later. */ -void bdrv_append (BlockDriverState *bs_new,BlockDriverState *bs_top, -Error **errp) +int bdrv_append (BlockDriverState *bs_new,BlockDriverState *bs_top, +Error **errp) {Error *local_err =NULL ;bdrv_set_backing_hd (bs_new, bs_top, &local_err);if (local_err) {error_propagate (errp, local_err); -return ; +return -EPERM ; }bdrv_replace_node (bs_top, bs_new, &local_err);if (local_err) {error_propagate (errp, local_err);bdrv_set_backing_hd (bs_new,NULL , &error_abort); +return -EPERM ; } + +return 0 ; }static void bdrv_delete (BlockDriverState *bs)
block/commit.c +2 -4
@@ -254,7 +254,6 @@ void commit_start(const char *job_id, BlockDriverState *bs,BlockDriverState *iter;BlockDriverState *commit_top_bs =NULL ;BlockDriverState *filtered_base; -Error *local_err =NULL ;int64_t base_size, top_size;uint64_t base_perms, iter_shared_perms;int ret;
@@ -312,11 +311,10 @@ void commit_start(const char *job_id, BlockDriverState *bs, commit_top_bs->total_sectors = top->total_sectors; -bdrv_append (commit_top_bs, top, &local_err); + ret =bdrv_append (commit_top_bs, top, errp);bdrv_unref (commit_top_bs);/* referenced by new parents or failed */ -if (local_err) { +if (ret <0 ) { commit_top_bs =NULL ; -error_propagate (errp, local_err); goto fail; }
block/mirror.c +2 -4
@@ -1560,7 +1560,6 @@ static BlockJob *mirror_start_job(BlockDriverState *mirror_top_bs;bool target_is_backing;uint64_t target_perms, target_shared_perms; -Error *local_err =NULL ;int ret;if (granularity ==0 ) {
@@ -1606,12 +1605,11 @@ static BlockJob *mirror_start_job( mirror_top_bs->opaque = bs_opaque;bdrv_drained_begin (bs); -bdrv_append (mirror_top_bs, bs, &local_err); + ret =bdrv_append (mirror_top_bs, bs, errp);bdrv_drained_end (bs); -if (local_err) { +if (ret <0 ) {bdrv_unref (mirror_top_bs); -error_propagate (errp, local_err);return NULL ; }
blockdev.c +3 -3
@@ -1432,6 +1432,7 @@ typedef struct ExternalSnapshotState {static void external_snapshot_prepare (BlkActionState *common,Error **errp) { +int ret;int flags =0 ;QDict *options =NULL ;Error *local_err =NULL ;
@@ -1587,9 +1588,8 @@ static void external_snapshot_prepare(BlkActionState *common, goto out; } -bdrv_append (state->new_bs, state->old_bs, &local_err); -if (local_err) { -error_propagate (errp, local_err); + ret =bdrv_append (state->new_bs, state->old_bs, errp); +if (ret <0 ) { goto out; } state->overlay_appended = true;
tests/test-bdrv-graph-mod.c +3 -3
@@ -122,7 +122,7 @@ static BlockDriverState *exclusive_writer_node(const char *name)*/ static void test_update_perm_tree (void ) { -Error *local_err =NULL ; +int ret; BlockBackend *root = blk_new(qemu_get_aio_context(), BLK_PERM_WRITE | BLK_PERM_CONSISTENT_READ,
@@ -135,8 +135,8 @@ static void test_update_perm_tree(void)bdrv_attach_child (filter, bs,"child" , &child_of_bds,BDRV_CHILD_FILTERED |BDRV_CHILD_PRIMARY , &error_abort); -bdrv_append (filter, bs, &local_err); -error_free_or_abort (&local_err); + ret =bdrv_append (filter, bs,NULL ); + g_assert_cmpint(ret, <, 0);bdrv_unref (filter);blk_unref (root);
Signature · 1 line
2.21.3
Re: [PATCH v2 04/36] block: bdrv_append(): return status
Quoted 3 lines · Vladimir Sementsov-Ogievskiy
Return int status to avoid extra error propagation schemes.
Signed-off-by Vladimir Sementsov-Ogievskiy <[email protected]>
Berto
Re: [PATCH v2 04/36] block: bdrv_append(): return status
In reply to Vladimir Sementsov-OgievskiyAm 27.11.2020 um 15:44 hat Vladimir Sementsov-Ogievskiy geschrieben:
Quoted 3 lines
Return int status to avoid extra error propagation schemes.
Signed-off-by Vladimir Sementsov-Ogievskiy <[email protected]>
PATCH v2 5/36 block: add bdrv_parent_try_set_aio_context
In reply to Vladimir Sementsov-OgievskiyWe already have bdrv_parent_can_set_aio_context(). Add corresponding bdrv_parent_set_aio_context_ignore() and bdrv_parent_try_set_aio_context() and use them instead of open-coding.
Make bdrv_parent_try_set_aio_context() public, as it will be used in further commit.
--- include/block/block.h | 2 ++ block.c | 51 +++++++++++++++++++++++++++++++++---------- 2 files changed, 41 insertions(+), 12 deletions(-)
include/block/block.h +2 -0
@@ -686,6 +686,8 @@ bool bdrv_child_can_set_aio_context(BdrvChild *c, AioContext *ctx, GSList **ignore, Error **errp);bool bdrv_can_set_aio_context (BlockDriverState *bs,AioContext *ctx,GSList **ignore,Error **errp); +int bdrv_parent_try_set_aio_context (BdrvChild *c,AioContext *ctx, +Error **errp);int bdrv_probe_blocksizes (BlockDriverState *bs,BlockSizes *bsz);int bdrv_probe_geometry (BlockDriverState *bs,HDGeometry *geo);
block.c +39 -12
77 lines not on this page · raw message
Signature · 1 line
2.21.3
Re: [PATCH v2 05/36] block: add bdrv_parent_try_set_aio_context
Am 27.11.2020 um 15:44 hat Vladimir Sementsov-Ogievskiy geschrieben:
Quoted 41 lines
We already have
bdrv_parent_can_set_aio_context(). Add correspondingbdrv_parent_set_aio_context_ignore()andbdrv_parent_try_set_aio_context()and use them instead of open-coding.Make
bdrv_parent_try_set_aio_context()public, as it will be used in further commit.Signed-off-by Vladimir Sementsov-Ogievskiy <[email protected]>--- include/block/block.h | 2 ++ block.c | 51 +++++++++++++++++++++++++++++++++---------- 2 files changed, 41 insertions(+), 12 deletions(-)include/block/block.h +2
-0@@ -686,6 +686,8 @@ bool bdrv_child_can_set_aio_context(BdrvChild *c, AioContext *ctx, GSList **ignore, Error **errp);bool bdrv_can_set_aio_context (BlockDriverState *bs,AioContext *ctx,GSList **ignore,Error **errp); +int bdrv_parent_try_set_aio_context (BdrvChild *c,AioContext *ctx, +Error **errp);int bdrv_probe_blocksizes (BlockDriverState *bs,BlockSizes *bsz);int bdrv_probe_geometry (BlockDriverState *bs,HDGeometry *geo);block.c +3
-0@@ -81,6 +81,9 @@ static BlockDriverState *bdrv_open_inherit(const char *filename,BdrvChildRole child_role, Error **errp); +static void bdrv_parent_set_aio_context_ignore (BdrvChild *c,AioContext *ctx, +GSList **ignore); +/* If non-zero, use only whitelisted block drivers */ static int use_bdrv_whitelist;
block.c BdrvChild *bdrv_root_attach_child(BlockDriverState *child_bs, · line 2660
Quoted 6 lines
block.c +0
-3@@ -2655,17 +2658,12 @@ BdrvChild *bdrv_root_attach_child(BlockDriverState *child_bs,* try moving the parent into the AioContext of child_bs instead. */ if (bdrv_get_aio_context (child_bs) != ctx) { ret =bdrv_try_set_aio_context (child_bs, ctx, &local_err); -if (ret <0 && child_class->can_set_aio_ctx) { -GSList *ignore =g_slist_prepend (NULL , child); - ctx =bdrv_get_aio_context (child_bs);
You are losing this line...
block.c BdrvChild *bdrv_root_attach_child(BlockDriverState *child_bs, · line 2662
Quoted 4 lines
block.c +2
-2-if (child_class->can_set_aio_ctx (child, ctx, &ignore,NULL )) { -error_free (local_err); +if (ret <0 ) { +if (bdrv_parent_try_set_aio_context (child, ctx,NULL ) ==0 ) {
...before this one, so I think the wrong context is passed now. Instead of trying to move the parent to the AioContext of the child, we'll try to move it to the AioContext in which it already is (and which doesn't match the AioContext of the child).
Kevin
Quoted 63 lines
block.c +34
-7ret =0 ; -g_slist_free (ignore); - ignore =g_slist_prepend (NULL , child); - child_class->set_aio_ctx (child, ctx, &ignore); +error_free (local_err); + local_err =NULL ; } -g_slist_free (ignore); }if (ret <0 ) {error_propagate (errp, local_err);@@ -6452,9 +6450,7 @@ void bdrv_set_aio_context_ignore(BlockDriverState *bs,if (g_slist_find (*ignore, child)) {continue ; } -assert (child->klass->set_aio_ctx); - *ignore =g_slist_prepend (*ignore, child); - child->klass->set_aio_ctx (child, new_context, ignore); +bdrv_parent_set_aio_context_ignore (child, new_context, ignore); }bdrv_detach_aio_context (bs);@@ -6511,6 +6507,37 @@ static bool bdrv_parent_can_set_aio_context(BdrvChild *c, AioContext *ctx,return true; } +static void bdrv_parent_set_aio_context_ignore (BdrvChild *c,AioContext *ctx, +GSList **ignore) +{ +if (g_slist_find (*ignore, c)) { +return ; + } + *ignore =g_slist_prepend (*ignore, c); + +assert (c->klass->set_aio_ctx); + c->klass->set_aio_ctx (c, ctx, ignore); +} + +int bdrv_parent_try_set_aio_context (BdrvChild *c,AioContext *ctx, +Error **errp) +{ +GSList *ignore =NULL ; + +if (!bdrv_parent_can_set_aio_context (c, ctx, &ignore, errp)) { +g_slist_free (ignore); +return -EPERM ; + } + +g_slist_free (ignore); + ignore =NULL ; + +bdrv_parent_set_aio_context_ignore (c, ctx, &ignore); +g_slist_free (ignore); + +return 0 ; +} +bool bdrv_child_can_set_aio_context (BdrvChild *c,AioContext *ctx,GSList **ignore,Error **errp) {Signature · 1 line
2.21.3
Re: [PATCH v2 05/36] block: add bdrv_parent_try_set_aio_context
Quoted 56 lines · Kevin Wolf
Am 27.11.2020 um 15:44 hat Vladimir Sementsov-Ogievskiy geschrieben:
Quoted 41 lines
We already have
bdrv_parent_can_set_aio_context(). Add correspondingbdrv_parent_set_aio_context_ignore()andbdrv_parent_try_set_aio_context()and use them instead of open-coding.Make
bdrv_parent_try_set_aio_context()public, as it will be used in further commit.Signed-off-by Vladimir Sementsov-Ogievskiy <[email protected]>--- include/block/block.h | 2 ++ block.c | 51 +++++++++++++++++++++++++++++++++---------- 2 files changed, 41 insertions(+), 12 deletions(-)include/block/block.h +2
-0@@ -686,6 +686,8 @@ bool bdrv_child_can_set_aio_context(BdrvChild *c, AioContext *ctx, GSList **ignore, Error **errp);bool bdrv_can_set_aio_context (BlockDriverState *bs,AioContext *ctx,GSList **ignore,Error **errp); +int bdrv_parent_try_set_aio_context (BdrvChild *c,AioContext *ctx, +Error **errp);int bdrv_probe_blocksizes (BlockDriverState *bs,BlockSizes *bsz);int bdrv_probe_geometry (BlockDriverState *bs,HDGeometry *geo);block.c +3
-0@@ -81,6 +81,9 @@ static BlockDriverState *bdrv_open_inherit(const char *filename,BdrvChildRole child_role, Error **errp); +static void bdrv_parent_set_aio_context_ignore (BdrvChild *c,AioContext *ctx, +GSList **ignore); +/* If non-zero, use only whitelisted block drivers */ static int use_bdrv_whitelist;block.c BdrvChild *bdrv_root_attach_child(BlockDriverState *child_bs, · line 2660
Quoted 6 lines
block.c +0
-3@@ -2655,17 +2658,12 @@ BdrvChild *bdrv_root_attach_child(BlockDriverState *child_bs,* try moving the parent into the AioContext of child_bs instead. */ if (bdrv_get_aio_context (child_bs) != ctx) { ret =bdrv_try_set_aio_context (child_bs, ctx, &local_err); -if (ret <0 && child_class->can_set_aio_ctx) { -GSList *ignore =g_slist_prepend (NULL , child); - ctx =bdrv_get_aio_context (child_bs);You are losing this line...
block.c BdrvChild *bdrv_root_attach_child(BlockDriverState *child_bs, · line 2662
Quoted 4 lines
block.c +2
-2-if (child_class->can_set_aio_ctx (child, ctx, &ignore,NULL )) { -error_free (local_err); +if (ret <0 ) { +if (bdrv_parent_try_set_aio_context (child, ctx,NULL ) ==0 ) {
Quoted 5 lines
...before this one, so I think the wrong context is passed now. Instead of trying to move the parent to the AioContext of the child, we'll try to move it to the AioContext in which it already is (and which doesn't match the AioContext of the child).
Oops, right, will fix
Quoted 65 lines
Quoted 64 lines
block.c +34
-7ret =0 ; -g_slist_free (ignore); - ignore =g_slist_prepend (NULL , child); - child_class->set_aio_ctx (child, ctx, &ignore); +error_free (local_err); + local_err =NULL ; } -g_slist_free (ignore); }if (ret <0 ) {error_propagate (errp, local_err);@@ -6452,9 +6450,7 @@ void bdrv_set_aio_context_ignore(BlockDriverState *bs,if (g_slist_find (*ignore, child)) {continue ; } -assert (child->klass->set_aio_ctx); - *ignore =g_slist_prepend (*ignore, child); - child->klass->set_aio_ctx (child, new_context, ignore); +bdrv_parent_set_aio_context_ignore (child, new_context, ignore); }bdrv_detach_aio_context (bs);@@ -6511,6 +6507,37 @@ static bool bdrv_parent_can_set_aio_context(BdrvChild *c, AioContext *ctx,return true; } +static void bdrv_parent_set_aio_context_ignore (BdrvChild *c,AioContext *ctx, +GSList **ignore) +{ +if (g_slist_find (*ignore, c)) { +return ; + } + *ignore =g_slist_prepend (*ignore, c); + +assert (c->klass->set_aio_ctx); + c->klass->set_aio_ctx (c, ctx, ignore); +} + +int bdrv_parent_try_set_aio_context (BdrvChild *c,AioContext *ctx, +Error **errp) +{ +GSList *ignore =NULL ; + +if (!bdrv_parent_can_set_aio_context (c, ctx, &ignore, errp)) { +g_slist_free (ignore); +return -EPERM ; + } + +g_slist_free (ignore); + ignore =NULL ; + +bdrv_parent_set_aio_context_ignore (c, ctx, &ignore); +g_slist_free (ignore); + +return 0 ; +} +bool bdrv_child_can_set_aio_context (BdrvChild *c,AioContext *ctx,GSList **ignore,Error **errp) {Signature · 1 line
2.21.3
Signature · 2 lines
Best regards, Vladimir
PATCH v2 6/36 block: BdrvChildClass: add .get_parent_aio_context handler
In reply to Vladimir Sementsov-OgievskiyAdd new handler to get aio context and implement it in all child classes. Add corresponding public interface to be used soon.
--- include/block/block.h | 3 +++ include/block/block_int.h | 2 ++ block.c | 13 +++++++++++++ block/block-backend.c | 9 +++++++++ blockjob.c | 8 ++++++++ 5 files changed, 35 insertions(+)
include/block/block.h +3 -0
@@ -688,6 +688,9 @@ bool bdrv_can_set_aio_context(BlockDriverState *bs, AioContext *ctx, GSList **ignore, Error **errp);int bdrv_parent_try_set_aio_context (BdrvChild *c,AioContext *ctx,Error **errp); + +AioContext *bdrv_child_get_parent_aio_context (BdrvChild *c); +int bdrv_probe_blocksizes (BlockDriverState *bs,BlockSizes *bsz);int bdrv_probe_geometry (BlockDriverState *bs,HDGeometry *geo);
include/block/block_int.h +2 -0
@@ -772,6 +772,8 @@ struct BdrvChildClass {bool (*can_set_aio_ctx)(BdrvChild *child,AioContext *ctx,GSList **ignore,Error **errp);void (*set_aio_ctx)(BdrvChild *child,AioContext *ctx,GSList **ignore); + + AioContext *(*get_parent_aio_context)(BdrvChild *child); };extern const BdrvChildClass child_of_bds;
block.c +13 -0
@@ -1334,6 +1334,13 @@ static int bdrv_child_cb_update_filename(BdrvChild *c, BlockDriverState *base,return 0 ; } +static AioContext *bdrv_child_cb_get_parent_aio_context (BdrvChild *c) +{ +BlockDriverState *bs = c->opaque; + +return bdrv_get_aio_context (bs); +} +const BdrvChildClass child_of_bds = { .parent_is_bds = true, .get_parent_desc = bdrv_child_get_parent_desc,
@@ -1347,8 +1354,14 @@ const BdrvChildClass child_of_bds = { .can_set_aio_ctx = bdrv_child_cb_can_set_aio_ctx, .set_aio_ctx = bdrv_child_cb_set_aio_ctx, .update_filename = bdrv_child_cb_update_filename, + .get_parent_aio_context = bdrv_child_cb_get_parent_aio_context, }; +AioContext *bdrv_child_get_parent_aio_context (BdrvChild *c) +{ +return c->klass->get_parent_aio_context (c); +} +static int bdrv_open_flags (BlockDriverState *bs,int flags) {int open_flags = flags;
block/block-backend.c +9 -0
@@ -298,6 +298,13 @@ static void blk_root_detach(BdrvChild *child) } } +static AioContext *blk_root_get_parent_aio_context (BdrvChild *c) +{ +BlockBackend *blk = c->opaque; + +return blk_get_aio_context (blk); +} +static const BdrvChildClass child_root = { .inherit_options = blk_root_inherit_options,
@@ -318,6 +325,8 @@ static const BdrvChildClass child_root = { .can_set_aio_ctx = blk_root_can_set_aio_ctx, .set_aio_ctx = blk_root_set_aio_ctx, + + .get_parent_aio_context = blk_root_get_parent_aio_context, };/*
blockjob.c +8 -0
@@ -163,6 +163,13 @@ static void child_job_set_aio_ctx(BdrvChild *c, AioContext *ctx, job->job.aio_context = ctx; } +static AioContext *child_job_get_parent_aio_context (BdrvChild *c) +{ +BlockJob *job = c->opaque; + +return job->job.aio_context; +} +static const BdrvChildClass child_job = { .get_parent_desc = child_job_get_parent_desc, .drained_begin = child_job_drained_begin,
@@ -171,6 +178,7 @@ static const BdrvChildClass child_job = { .can_set_aio_ctx = child_job_can_set_aio_ctx, .set_aio_ctx = child_job_set_aio_ctx, .stay_at_node = true, + .get_parent_aio_context = child_job_get_parent_aio_context, };void block_job_remove_all_bdrv (BlockJob *job)
Signature · 1 line
2.21.3
Re: [PATCH v2 06/36] block: BdrvChildClass: add .get_parent_aio_context handler
Am 27.11.2020 um 15:44 hat Vladimir Sementsov-Ogievskiy geschrieben:
Quoted 4 lines
Add new handler to get aio context and implement it in all child classes. Add corresponding public interface to be used soon.
Signed-off-by Vladimir Sementsov-Ogievskiy <[email protected]>
Hm, are you going to introduce cases where parent and child context don't match, or why is this a useful function?
Even if so, I feel it shouldn't be any of the child's business what AioContext the parent uses.
Well, maybe the rest of the series will answer this.
Kevin
Re: [PATCH v2 06/36] block: BdrvChildClass: add .get_parent_aio_context handler
Quoted 6 lines · Kevin Wolf
Am 27.11.2020 um 15:44 hat Vladimir Sementsov-Ogievskiy geschrieben:
Quoted 4 lines
Add new handler to get aio context and implement it in all child classes. Add corresponding public interface to be used soon.
Signed-off-by Vladimir Sementsov-Ogievskiy <[email protected]>
Quoted 8 lines
Hm, are you going to introduce cases where parent and child context don't match, or why is this a useful function?
Even if so, I feel it shouldn't be any of the child's business what AioContext the parent uses.
Well, maybe the rest of the series will answer this.
It's for the following patch, to not pass parent (as opaque) with it's class, and with its ctx in separate. Just to simplify the interface of the function, we are going to work with a lot.
Hm. I'll take this opportunity to say, that the terminology that calls graph edge "BdrvChild" always leads to the mess between parents and children.. "child_class" is a class of parent.. list of parents is list of children... It all would be a lot simpler to understand if BdrvChild was BdrvEdge or something like this.
Signature · 2 lines
Best regards, Vladimir
Re: [PATCH v2 06/36] block: BdrvChildClass: add .get_parent_aio_context handler
Am 18.01.2021 um 18:36 hat Vladimir Sementsov-Ogievskiy geschrieben:
Quoted 10 lines
Quoted 9 lines · Kevin Wolf
Am 27.11.2020 um 15:44 hat Vladimir Sementsov-Ogievskiy geschrieben:
Quoted 4 lines
Add new handler to get aio context and implement it in all child classes. Add corresponding public interface to be used soon.
Signed-off-by Vladimir Sementsov-Ogievskiy <[email protected]>Hm, are you going to introduce cases where parent and child context don't match, or why is this a useful function?
Quoted 8 lines
Quoted 4 lines
Even if so, I feel it shouldn't be any of the child's business what AioContext the parent uses.
Well, maybe the rest of the series will answer this.
It's for the following patch, to not pass parent (as opaque) with it's class, and with its ctx in separate. Just to simplify the interface of the function, we are going to work with a lot.
In a way, the result is nicer because we already assume that ctx is the parent context when we move things to different AioContexts. So it's more consistent to just directly take it from the parent.
At the same time, it also complicates things a bit because now we need a defined state in the middle of an operation that adds, removes or replaces a child.
I guess I still to make more progress in the review of this series until I see how you're using the interface.
Quoted 5 lines
Hm. I'll take this opportunity to say, that the terminology that calls graph edge "BdrvChild" always leads to the mess between parents and children.. "
child_class" is a class of parent.. list of parents is list of children... It all would be a lot simpler to understand if BdrvChild was BdrvEdge or something like this.
Yeah, that would probably be clearer now that we actually use it to work with both ends of the edge. And BdrvNode instead of BlockDriverState, I guess.
Kevin
Re: [PATCH v2 06/36] block: BdrvChildClass: add .get_parent_aio_context handler
Quoted 37 lines · Kevin Wolf
Am 18.01.2021 um 18:36 hat Vladimir Sementsov-Ogievskiy geschrieben:
Quoted 10 lines
Quoted 9 lines · Kevin Wolf
Am 27.11.2020 um 15:44 hat Vladimir Sementsov-Ogievskiy geschrieben:
Quoted 4 lines
Add new handler to get aio context and implement it in all child classes. Add corresponding public interface to be used soon.
Signed-off-by Vladimir Sementsov-Ogievskiy <[email protected]>Hm, are you going to introduce cases where parent and child context don't match, or why is this a useful function?
Quoted 8 lines
Quoted 4 lines
Even if so, I feel it shouldn't be any of the child's business what AioContext the parent uses.
Well, maybe the rest of the series will answer this.
It's for the following patch, to not pass parent (as opaque) with it's class, and with its ctx in separate. Just to simplify the interface of the function, we are going to work with a lot.
In a way, the result is nicer because we already assume that ctx is the parent context when we move things to different AioContexts. So it's more consistent to just directly take it from the parent.
At the same time, it also complicates things a bit because now we need a defined state in the middle of an operation that adds, removes or replaces a child.
I guess I still to make more progress in the review of this series until I see how you're using the interface.
Quoted 5 lines
Hm. I'll take this opportunity to say, that the terminology that calls graph edge "BdrvChild" always leads to the mess between parents and children.. "
child_class" is a class of parent.. list of parents is list of children... It all would be a lot simpler to understand if BdrvChild was BdrvEdge or something like this.
Quoted 3 lines
Yeah, that would probably be clearer now that we actually use it to work with both ends of the edge. And BdrvNode instead of BlockDriverState, I guess.
Do you think, we can just rename them? Or it would be too painful for developers, who get used to current names? I can make patches
Signature · 2 lines
Best regards, Vladimir
Re: [PATCH v2 06/36] block: BdrvChildClass: add .get_parent_aio_context handler
Am 22.01.2021 um 12:04 hat Vladimir Sementsov-Ogievskiy geschrieben:
Quoted 38 lines
Quoted 37 lines · Kevin Wolf
Am 18.01.2021 um 18:36 hat Vladimir Sementsov-Ogievskiy geschrieben:
Quoted 10 lines
Quoted 9 lines · Kevin Wolf
Am 27.11.2020 um 15:44 hat Vladimir Sementsov-Ogievskiy geschrieben:
Quoted 4 lines
Add new handler to get aio context and implement it in all child classes. Add corresponding public interface to be used soon.
Signed-off-by Vladimir Sementsov-Ogievskiy <[email protected]>Hm, are you going to introduce cases where parent and child context don't match, or why is this a useful function?
Quoted 8 lines
Quoted 4 lines
Even if so, I feel it shouldn't be any of the child's business what AioContext the parent uses.
Well, maybe the rest of the series will answer this.
It's for the following patch, to not pass parent (as opaque) with it's class, and with its ctx in separate. Just to simplify the interface of the function, we are going to work with a lot.
In a way, the result is nicer because we already assume that ctx is the parent context when we move things to different AioContexts. So it's more consistent to just directly take it from the parent.
At the same time, it also complicates things a bit because now we need a defined state in the middle of an operation that adds, removes or replaces a child.
I guess I still to make more progress in the review of this series until I see how you're using the interface.
Quoted 5 lines
Hm. I'll take this opportunity to say, that the terminology that calls graph edge "BdrvChild" always leads to the mess between parents and children.. "
child_class" is a class of parent.. list of parents is list of children... It all would be a lot simpler to understand if BdrvChild was BdrvEdge or something like this.
Quoted 6 lines
Quoted 3 lines
Yeah, that would probably be clearer now that we actually use it to work with both ends of the edge. And BdrvNode instead of BlockDriverState, I guess.
Do you think, we can just rename them? Or it would be too painful for developers, who get used to current names? I can make patches
I think getting used to new names wouldn't be too bad. I would be more afraid of the merge conflicts.
Not sure, but it might in the category that we would do it differently if we were starting over, but maybe not worth changing now.
Kevin
Re: [PATCH v2 06/36] block: BdrvChildClass: add .get_parent_aio_context handler
Quoted 43 lines · Kevin Wolf
Am 22.01.2021 um 12:04 hat Vladimir Sementsov-Ogievskiy geschrieben:
Quoted 42 lines
Quoted 40 lines · Kevin Wolf
Am 18.01.2021 um 18:36 hat Vladimir Sementsov-Ogievskiy geschrieben:
Quoted 10 lines
Quoted 9 lines · Kevin Wolf
Am 27.11.2020 um 15:44 hat Vladimir Sementsov-Ogievskiy geschrieben:
Quoted 4 lines
Add new handler to get aio context and implement it in all child classes. Add corresponding public interface to be used soon.
Signed-off-by Vladimir Sementsov-Ogievskiy <[email protected]>Hm, are you going to introduce cases where parent and child context don't match, or why is this a useful function?
Quoted 8 lines
Quoted 4 lines
Even if so, I feel it shouldn't be any of the child's business what AioContext the parent uses.
Well, maybe the rest of the series will answer this.
It's for the following patch, to not pass parent (as opaque) with it's class, and with its ctx in separate. Just to simplify the interface of the function, we are going to work with a lot.
In a way, the result is nicer because we already assume that ctx is the parent context when we move things to different AioContexts. So it's more consistent to just directly take it from the parent.
At the same time, it also complicates things a bit because now we need a defined state in the middle of an operation that adds, removes or replaces a child.
I guess I still to make more progress in the review of this series until I see how you're using the interface.
Quoted 5 lines
Hm. I'll take this opportunity to say, that the terminology that calls graph edge "BdrvChild" always leads to the mess between parents and children.. "
child_class" is a class of parent.. list of parents is list of children... It all would be a lot simpler to understand if BdrvChild was BdrvEdge or something like this.Yeah, that would probably be clearer now that we actually use it to work with both ends of the edge. And BdrvNode instead of BlockDriverState, I guess.
Quoted 9 lines
Quoted 2 lines
Do you think, we can just rename them? Or it would be too painful for developers, who get used to current names? I can make patches
I think getting used to new names wouldn't be too bad. I would be more afraid of the merge conflicts.
Not sure, but it might in the category that we would do it differently if we were starting over, but maybe not worth changing now.
Hmm yes, such rename will add a year of uncomfortable patch backporting..
Signature · 2 lines
Best regards, Vladimir
PATCH v2 7/36 block: drop ctx argument from bdrv_root_attach_child
In reply to Vladimir Sementsov-OgievskiyPassing parent aio context is redundant, as child_class and parent opaque pointer are enough to retrieve it. Drop the argument and use new bdrv_child_get_parent_aio_context() interface.
--- include/block/block_int.h | 1 - block.c | 8 +++++--- block/block-backend.c | 4 ++-- blockjob.c | 3 +-- 4 files changed, 8 insertions(+), 8 deletions(-)
include/block/block_int.h +0 -1
@@ -1278,7 +1278,6 @@ BdrvChild *bdrv_root_attach_child(BlockDriverState *child_bs,const char *child_name, const BdrvChildClass *child_class, BdrvChildRole child_role, - AioContext *ctx, uint64_t perm, uint64_t shared_perm, void *opaque, Error **errp);void bdrv_root_unref_child (BdrvChild *child);
block.c +5 -3
@@ -2640,13 +2640,13 @@ BdrvChild *bdrv_root_attach_child(BlockDriverState *child_bs,const char *child_name, const BdrvChildClass *child_class, BdrvChildRole child_role, - AioContext *ctx, uint64_t perm, uint64_t shared_perm, void *opaque, Error **errp) { BdrvChild *child;Error *local_err =NULL ;int ret; +AioContext *ctx; ret =bdrv_check_update_perm (child_bs,NULL , perm, shared_perm,NULL , errp);if (ret <0 ) {
@@ -2666,6 +2666,8 @@ BdrvChild *bdrv_root_attach_child(BlockDriverState *child_bs, .opaque = opaque, }; + ctx =bdrv_child_get_parent_aio_context (child); +/* If the AioContexts don't match, first try to move the subtree of * child_bs into the AioContext of the new parent. If this doesn't work, * try moving the parent into the AioContext of child_bs instead. */
@@ -2721,8 +2723,8 @@ BdrvChild *bdrv_attach_child(BlockDriverState *parent_bs, perm, shared_perm, &perm, &shared_perm); child =bdrv_root_attach_child (child_bs, child_name, child_class, - child_role,bdrv_get_aio_context (parent_bs), - perm, shared_perm, parent_bs, errp); + child_role, perm, shared_perm, parent_bs, + errp);if (child ==NULL ) {return NULL ; }
block/block-backend.c +2 -2
@@ -435,7 +435,7 @@ BlockBackend *blk_new_open(const char *filename, const char *reference, blk->root =bdrv_root_attach_child (bs,"root" , &child_root,BDRV_CHILD_FILTERED |BDRV_CHILD_PRIMARY , - blk->ctx, perm,BLK_PERM_ALL , blk, errp); + perm,BLK_PERM_ALL , blk, errp);if (!blk->root) {blk_unref (blk);return NULL ;
@@ -849,7 +849,7 @@ int blk_insert_bs(BlockBackend *blk, BlockDriverState *bs, Error **errp)bdrv_ref (bs); blk->root =bdrv_root_attach_child (bs,"root" , &child_root,BDRV_CHILD_FILTERED |BDRV_CHILD_PRIMARY , - blk->ctx, blk->perm, blk->shared_perm, + blk->perm, blk->shared_perm, blk, errp);if (blk->root ==NULL ) {return -EPERM ;
blockjob.c +1 -2
@@ -225,8 +225,7 @@ int block_job_add_bdrv(BlockJob *job, const char *name, BlockDriverState *bs,if (job->job.aio_context !=qemu_get_aio_context ()) {aio_context_release (job->job.aio_context); } - c =bdrv_root_attach_child (bs, name, &child_job,0 , - job->job.aio_context, perm, shared_perm, job, + c =bdrv_root_attach_child (bs, name, &child_job,0 , perm, shared_perm, job, errp);if (job->job.aio_context !=qemu_get_aio_context ()) {aio_context_acquire (job->job.aio_context);
Signature · 1 line
2.21.3
PATCH v2 9/36 block: return value from bdrv_replace_node()
In reply to Vladimir Sementsov-OgievskiyFunctions with errp argument are not recommened to be void-functions. Improve bdrv_replace_node().
--- include/block/block.h | 4 ++-- block.c | 14 ++++++++------ 2 files changed, 10 insertions(+), 8 deletions(-)
include/block/block.h +2 -2
@@ -346,8 +346,8 @@ int bdrv_create_file(const char *filename, QemuOpts *opts, Error **errp);BlockDriverState *bdrv_new (void );int bdrv_append (BlockDriverState *bs_new,BlockDriverState *bs_top,Error **errp); -void bdrv_replace_node (BlockDriverState *from,BlockDriverState *to, -Error **errp); +int bdrv_replace_node (BlockDriverState *from,BlockDriverState *to, +Error **errp);int bdrv_parse_aio (const char *mode,int *flags);int bdrv_parse_cache_mode (const char *mode,int *flags,bool *writethrough);
block.c +8 -6
@@ -4537,14 +4537,14 @@ static bool should_update_child(BdrvChild *c, BlockDriverState *to)* With auto_skip=false the error is returned if from has a parent which should * not be updated. */ -static void bdrv_replace_node_common (BlockDriverState *from, -BlockDriverState *to, -bool auto_skip,Error **errp) +static int bdrv_replace_node_common (BlockDriverState *from, +BlockDriverState *to, +bool auto_skip,Error **errp) { +int ret = -EPERM ;BdrvChild *c, *next;GSList *list =NULL , *p;uint64_t perm =0 , shared =BLK_PERM_ALL ; -int ret;/* Make sure that @from doesn't go away until we have successfully attached * all of its parents to @to. */
@@ -4600,10 +4600,12 @@ out:g_slist_free (list);bdrv_drained_end (from);bdrv_unref (from); + +return ret; } -void bdrv_replace_node (BlockDriverState *from,BlockDriverState *to, -Error **errp) +int bdrv_replace_node (BlockDriverState *from,BlockDriverState *to, +Error **errp) {return bdrv_replace_node_common (from, to, true, errp); }
Signature · 1 line
2.21.3
Re: [PATCH v2 09/36] block: return value from bdrv_replace_node()
Quoted 4 lines · Vladimir Sementsov-Ogievskiy
Functions with errp argument are not recommened to be void-functions. Improve
bdrv_replace_node().Signed-off-by Vladimir Sementsov-Ogievskiy <[email protected]>
Berto
Re: [PATCH v2 09/36] block: return value from bdrv_replace_node()
In reply to Vladimir Sementsov-OgievskiyAm 27.11.2020 um 15:44 hat Vladimir Sementsov-Ogievskiy geschrieben:
Quoted 34 lines
Functions with errp argument are not recommened to be void-functions. Improve
bdrv_replace_node().Signed-off-by Vladimir Sementsov-Ogievskiy <[email protected]>--- include/block/block.h | 4 ++-- block.c | 14 ++++++++------ 2 files changed, 10 insertions(+), 8 deletions(-)include/block/block.h +2
-2@@ -346,8 +346,8 @@ int bdrv_create_file(const char *filename, QemuOpts *opts, Error **errp);BlockDriverState *bdrv_new (void );int bdrv_append (BlockDriverState *bs_new,BlockDriverState *bs_top,Error **errp); -void bdrv_replace_node (BlockDriverState *from,BlockDriverState *to, -Error **errp); +int bdrv_replace_node (BlockDriverState *from,BlockDriverState *to, +Error **errp);int bdrv_parse_aio (const char *mode,int *flags);int bdrv_parse_cache_mode (const char *mode,int *flags,bool *writethrough);block.c +0
-2@@ -4537,14 +4537,14 @@ static bool should_update_child(BdrvChild *c, BlockDriverState *to)* With auto_skip=false the error is returned if from has a parent which should * not be updated. */ -static void bdrv_replace_node_common(BlockDriverState *from, - BlockDriverState *to,
block.c static bool should_update_child(BdrvChild *c, BlockDriverState *to) · line 4547
Quoted 10 lines
block.c +4
-2- bool auto_skip, Error **errp) +static int bdrv_replace_node_common (BlockDriverState *from, +BlockDriverState *to, +bool auto_skip,Error **errp) { +int ret = -EPERM ;BdrvChild *c, *next;GSList *list =NULL , *p;uint64_t perm =0 , shared =BLK_PERM_ALL ; -int ret;
I think I'd prefer setting ret in each error path. This makes it more obvious that ret has the right value and hasn't been modified between the initialisation and the error.
block.c out:
Quoted 4 lines
block.c +0
-0/* Make sure that @from doesn't go away until we have successfully attached * all of its parents to @to. */ @@ -4600,10 +4600,12 @@ out:
Let's add an explicit ret = 0 right before the out: label.
block.c out: · line 4605
Quoted 6 lines
block.c +2
-0g_slist_free (list);bdrv_drained_end (from);bdrv_unref (from); + +return ret; }
With these changes:
PATCH v2 10/36 util: add transactions.c
In reply to Vladimir Sementsov-OgievskiyAdd simple transaction API to use in further update of block graph operations.
Supposed usage is:
- "prepare" is main function of the action and it should make the main effect of the action to be visible for the following actions, keeping possibility of roll-back, saving necessary things in action state, which is prepended to the list. So, driver struct doesn't include "prepare" field, as it is supposed to be called directly.
- commit/rollback is supposed to be called for the list of action states, to commit/rollback all the actions in reverse order
- When possible "commit" should not make visible effect for other actions, which make possible transparent logical interaction between actions.
--- include/qemu/transactions.h | 46 +++++++++++++++++++++ util/transactions.c | 81 +++++++++++++++++++++++++++++++++++++ util/meson.build | 1 + 3 files changed, 128 insertions(+) create mode 100644 include/qemu/transactions.h create mode 100644 util/transactions.c
include/qemu/transactions.h new file +46 -0
@@ -0,0 +1,46 @@ +/* +* Simple transactions API +* +* Copyright (c) 2020 Virtuozzo International GmbH. +* +* Author: +* Vladimir Sementsov-Ogievskiy <[email protected]> +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation; either version 2 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program. If not, see <http://www.gnu.org/licenses/>. +*/ + +#ifndef QEMU_TRANSACTIONS_H +#define QEMU_TRANSACTIONS_H + +#include <gmodule.h> + +typedef struct TransactionActionDrv { +void (*abort)(void *opeque); +void (*commit)(void *opeque); +void (*clean)(void *opeque); +}TransactionActionDrv ; + +void tran_prepend (GSList **list,TransactionActionDrv *drv,void *opaque); +void tran_abort (GSList *backup); +void tran_commit (GSList *backup); +static inline void tran_finalize (GSList *backup,int ret) +{ +if (ret <0 ) { +tran_abort (backup); + }else { +tran_commit (backup); + } +} + +#endif /* QEMU_TRANSACTIONS_H */
util/transactions.c new file +81 -0
81 lines not on this page · raw message
util/meson.build +1 -0
@@ -41,6 +41,7 @@ util_ss.add(files('qsp.c')) util_ss.add(files('range.c')) util_ss.add(files('stats64.c')) util_ss.add(files('systemd.c')) +util_ss.add(files('transactions.c')) util_ss.add(when: 'CONFIG_POSIX', if_true: files('drm.c')) util_ss.add(files('guest-random.c'))
Signature · 1 line
2.21.3
Re: [PATCH v2 10/36] util: add transactions.c
Am 27.11.2020 um 15:44 hat Vladimir Sementsov-Ogievskiy geschrieben:
Quoted 10 lines
Add simple transaction API to use in further update of block graph operations.
Supposed usage is:
- "prepare" is main function of the action and it should make the main effect of the action to be visible for the following actions, keeping possibility of roll-back, saving necessary things in action state, which is prepended to the list. So, driver struct doesn't include "prepare" field, as it is supposed to be called directly.
So the convention is that tran_prepend() should be called by the function that does the preparation? Or would we call tran_prepend() and do the actual action in different places?
Quoted 44 lines
- commit/rollback is supposed to be called for the list of action states, to commit/rollback all the actions in reverse order
- When possible "commit" should not make visible effect for other actions, which make possible transparent logical interaction between actions.
Signed-off-by Vladimir Sementsov-Ogievskiy <[email protected]>--- include/qemu/transactions.h | 46 +++++++++++++++++++++ util/transactions.c | 81 +++++++++++++++++++++++++++++++++++++ util/meson.build | 1 + 3 files changed, 128 insertions(+) create mode 100644 include/qemu/transactions.h create mode 100644 util/transactions.cinclude/qemu/transactions.h new file +22
-0@@ -0,0 +1,46 @@ +/* +* Simple transactions API +* +* Copyright (c) 2020 Virtuozzo International GmbH. +* +* Author: +* Vladimir Sementsov-Ogievskiy <[email protected]> +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation; either version 2 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program. If not, see <http://www.gnu.org/licenses/>. +*/ +
include/qemu/transactions.h line 32
Quoted 10 lines
include/qemu/transactions.h new file +10
-0+#ifndef QEMU_TRANSACTIONS_H +#define QEMU_TRANSACTIONS_H + +#include <gmodule.h> + +typedef struct TransactionActionDrv { +void (*abort)(void *opeque); +void (*commit)(void *opeque); +void (*clean)(void *opeque); +}TransactionActionDrv ;
s/opeque/opaque/
include/qemu/transactions.h line 36
Quoted 3 lines
include/qemu/transactions.h new file +3
-0+void tran_prepend (GSList **list,TransactionActionDrv *drv,void *opaque); +void tran_abort (GSList *backup); +void tran_commit (GSList *backup);
I'd add an empty line before a full function definition.
include/qemu/transactions.h line 44
Quoted 8 lines
include/qemu/transactions.h new file +8
-0+static inline void tran_finalize (GSList *backup,int ret) +{ +if (ret <0 ) { +tran_abort (backup); + }else { +tran_commit (backup); + } +}
Let's use an opaque struct instead of GSList here and...
Quoted 27 lines
include/qemu/transactions.h new file +1
-0+#endif /* QEMU_TRANSACTIONS_H */ util/transactions.c new file +20
-0@@ -0,0 +1,81 @@ +/* +* Simple transactions API +* +* Copyright (c) 2020 Virtuozzo International GmbH. +* +* Author: +* Sementsov-Ogievskiy Vladimir <[email protected]> +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation; either version 2 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program. If not, see <http://www.gnu.org/licenses/>.
util/transactions.c line 30
Quoted 10 lines
util/transactions.c new file +10
-0+*/ + +#include "qemu/osdep.h" + +#include "qemu/transactions.h" + +typedef struct BdrvAction { +TransactionActionDrv *drv; +void *opaque; +}BdrvAction ;
...add a QSLIST_ENTRY (or similar) here to make it a type-safe list.
The missing type safety of GSList means that we should avoid it whenever it's easily possible (i.e. we know the number of lists in which an element will be). Here, each BdrvAction will only be in a single list, so typed lists should be simple enough.
Kevin
Re: [PATCH v2 10/36] util: add transactions.c
Quoted 6 lines · Kevin Wolf
Am 27.11.2020 um 15:44 hat Vladimir Sementsov-Ogievskiy geschrieben:
Quoted 5 lines
Add simple transaction API to use in further update of block graph operations.
Supposed usage is:
Quoted 8 lines
Quoted 5 lines
- "prepare" is main function of the action and it should make the main effect of the action to be visible for the following actions, keeping possibility of roll-back, saving necessary things in action state, which is prepended to the list. So, driver struct doesn't include "prepare" field, as it is supposed to be called directly.
So the convention is that
tran_prepend()should be called by the function that does the preparation?
yes.
Quoted 117 lines
Or would we call
tran_prepend()and
do the actual action in different places?Quoted 44 lines
- commit/rollback is supposed to be called for the list of action states, to commit/rollback all the actions in reverse order
- When possible "commit" should not make visible effect for other actions, which make possible transparent logical interaction between actions.
Signed-off-by Vladimir Sementsov-Ogievskiy <[email protected]>--- include/qemu/transactions.h | 46 +++++++++++++++++++++ util/transactions.c | 81 +++++++++++++++++++++++++++++++++++++ util/meson.build | 1 + 3 files changed, 128 insertions(+)create mode 100644
include/qemu/transactions.h
create mode 100644util/transactions.cinclude/qemu/transactions.h new file +22
-0@@ -0,0 +1,46 @@ +/* +* Simple transactions API +* +* Copyright (c) 2020 Virtuozzo International GmbH. +* +* Author: +* Vladimir Sementsov-Ogievskiy <[email protected]> +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation; either version 2 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program. If not, see <http://www.gnu.org/licenses/>. +*/ +include/qemu/transactions.h line 32
Quoted 10 lines
include/qemu/transactions.h new file +10
-0+#ifndef QEMU_TRANSACTIONS_H +#define QEMU_TRANSACTIONS_H + +#include <gmodule.h> + +typedef struct TransactionActionDrv { +void (*abort)(void *opeque); +void (*commit)(void *opeque); +void (*clean)(void *opeque); +}TransactionActionDrv ;s/opeque/opaque/
include/qemu/transactions.h line 36
Quoted 3 lines
include/qemu/transactions.h new file +3
-0+void tran_prepend (GSList **list,TransactionActionDrv *drv,void *opaque); +void tran_abort (GSList *backup); +void tran_commit (GSList *backup);I'd add an empty line before a full function definition.
include/qemu/transactions.h line 44
Quoted 8 lines
include/qemu/transactions.h new file +8
-0+static inline void tran_finalize (GSList *backup,int ret) +{ +if (ret <0 ) { +tran_abort (backup); + }else { +tran_commit (backup); + } +}Let's use an opaque struct instead of GSList here and...
Quoted 27 lines
include/qemu/transactions.h new file +1
-0+#endif /* QEMU_TRANSACTIONS_H */ util/transactions.c new file +20
-0@@ -0,0 +1,81 @@ +/* +* Simple transactions API +* +* Copyright (c) 2020 Virtuozzo International GmbH. +* +* Author: +* Sementsov-Ogievskiy Vladimir <[email protected]> +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation; either version 2 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program. If not, see <http://www.gnu.org/licenses/>. util/transactions.c line 30
Quoted 10 lines
util/transactions.c new file +10
-0+*/ + +#include "qemu/osdep.h" + +#include "qemu/transactions.h" + +typedef struct BdrvAction { +TransactionActionDrv *drv; +void *opaque; +}BdrvAction ;...add a
QSLIST_ENTRY(or similar) here to make it a type-safe list.
Quoted 5 lines
The missing type safety of GSList means that we should avoid it whenever it's easily possible (i.e. we know the number of lists in which an element will be). Here, each BdrvAction will only be in a single list, so typed lists should be simple enough.
OK
Signature · 2 lines
Best regards, Vladimir
PATCH v2 11/36 block: bdrv_refresh_perms: check parents compliance
In reply to Vladimir Sementsov-OgievskiyAdd additional check that node parents do not interfere with each other. This should not hurt existing callers and allows in further patch use bdrv_refresh_perms() to update a subtree of changed BdrvChild (check that change is correct).
New check will substitute bdrv_check_update_perm() in following permissions refactoring, so keep error messages the same to avoid unit test result changes.
--- block.c | 63 ++++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 54 insertions(+), 9 deletions(-)
block.c +54 -9
@@ -1966,6 +1966,57 @@ bool bdrv_is_writable(BlockDriverState *bs)return bdrv_is_writable_after_reopen (bs,NULL ); } +static char *bdrv_child_user_desc (BdrvChild *c) +{ +if (c->klass->get_parent_desc) { +return c->klass->get_parent_desc (c); + } + +return g_strdup ("another user" ); +} + +static bool bdrv_a_allow_b (BdrvChild *a,BdrvChild *b,Error **errp) +{ + g_autofree char *user = NULL; + g_autofree char *perm_names = NULL; + +if ((b->perm & a->shared_perm) == b->perm) { +return true; + } + + perm_names =bdrv_perm_names (b->perm & ~a->shared_perm); + user =bdrv_child_user_desc (a); +error_setg (errp,"Conflicts with use by %s as '%s', which does not " +"allow '%s' on %s" , + user, a->name, perm_names,bdrv_get_node_name (b->bs)); + +return false; +} + +static bool bdrv_check_parents_compliance (BlockDriverState *bs,Error **errp) +{ +BdrvChild *a, *b; + +/* +* During the loop we'll look at each pair twice. That's correct is +* bdrv_a_allow_b() is asymmetric and we should check each pair in both +* directions. +*/ + QLIST_FOREACH(a, &bs->parents, next_parent) { + QLIST_FOREACH(b, &bs->parents, next_parent) { +if (a == b) { +continue ; + } + +if (!bdrv_a_allow_b (a, b, errp)) { +return false; + } + } + } + +return true; +} + static void bdrv_child_perm(BlockDriverState *bs, BlockDriverState *child_bs, BdrvChild *c, BdrvChildRole role, BlockReopenQueue *reopen_queue,
@@ -2143,15 +2194,6 @@ void bdrv_get_cumulative_perm(BlockDriverState *bs, uint64_t *perm, *shared_perm = cumulative_shared_perms; } -static char *bdrv_child_user_desc (BdrvChild *c) -{ -if (c->klass->get_parent_desc) { -return c->klass->get_parent_desc (c); - } - -return g_strdup ("another user" ); -} -char *bdrv_perm_names (uint64_t perm) { struct perm_name {
@@ -2295,6 +2337,9 @@ static int bdrv_refresh_perms(BlockDriverState *bs, Error **errp)int ret;uint64_t perm, shared_perm; +if (!bdrv_check_parents_compliance (bs, errp)) { +return -EPERM ; + }bdrv_get_cumulative_perm (bs, &perm, &shared_perm); ret =bdrv_check_perm (bs,NULL , perm, shared_perm,NULL , errp);if (ret <0 ) {
Signature · 1 line
2.21.3
Re: [PATCH v2 11/36] block: bdrv_refresh_perms: check parents compliance
Am 27.11.2020 um 15:44 hat Vladimir Sementsov-Ogievskiy geschrieben:
Quoted 10 lines
Add additional check that node parents do not interfere with each other. This should not hurt existing callers and allows in further patch use
bdrv_refresh_perms()to update a subtree of changed BdrvChild (check that change is correct).New check will substitute
bdrv_check_update_perm()in following permissions refactoring, so keep error messages the same to avoid unit test result changes.Signed-off-by Vladimir Sementsov-Ogievskiy <[email protected]>
The change itself looks ok, but I'm not happy with the naming. It feels a bit unspecific. How about inverting the result and calling it bdrv_parent_perms_conflict() and bdrv_child_perms_conflict()?
At least, I'd call it "permission consistency" rather then "compliance".
Quoted 31 lines
block.c +23
-0@@ -1966,6 +1966,57 @@ bool bdrv_is_writable(BlockDriverState *bs)return bdrv_is_writable_after_reopen (bs,NULL ); } +static char *bdrv_child_user_desc (BdrvChild *c) +{ +if (c->klass->get_parent_desc) { +return c->klass->get_parent_desc (c); + } + +return g_strdup ("another user" ); +} + +static bool bdrv_a_allow_b (BdrvChild *a,BdrvChild *b,Error **errp) +{ + g_autofree char *user = NULL; + g_autofree char *perm_names = NULL; + +if ((b->perm & a->shared_perm) == b->perm) { +return true; + } + + perm_names =bdrv_perm_names (b->perm & ~a->shared_perm); + user =bdrv_child_user_desc (a); +error_setg (errp,"Conflicts with use by %s as '%s', which does not " +"allow '%s' on %s" , + user, a->name, perm_names,bdrv_get_node_name (b->bs));
block.c bool bdrv_is_writable(BlockDriverState *bs) · line 2001
Quoted 10 lines
block.c +10
-0+ +return false; +} + +static bool bdrv_check_parents_compliance (BlockDriverState *bs,Error **errp) +{ +BdrvChild *a, *b; + +/* +* During the loop we'll look at each pair twice. That's correct is
s/is/because/ or what did you mean here?
Quoted 7 lines
block.c +7
-0+* bdrv_a_allow_b() is asymmetric and we should check each pair in both +* directions. +*/ + QLIST_FOREACH(a, &bs->parents, next_parent) { + QLIST_FOREACH(b, &bs->parents, next_parent) { +if (a == b) { +continue ;
block.c bool bdrv_is_writable(BlockDriverState *bs) · line 2018
Quoted 10 lines
block.c +10
-0+ } + +if (!bdrv_a_allow_b (a, b, errp)) { +return false; + } + } + } + +return true; +}
Kevin
Re: [PATCH v2 11/36] block: bdrv_refresh_perms: check parents compliance
Quoted 10 lines · Kevin Wolf
Am 27.11.2020 um 15:44 hat Vladimir Sementsov-Ogievskiy geschrieben:
Quoted 9 lines
Add additional check that node parents do not interfere with each other. This should not hurt existing callers and allows in further patch use
bdrv_refresh_perms()to update a subtree of changed BdrvChild (check that change is correct).New check will substitute
bdrv_check_update_perm()in following permissions refactoring, so keep error messages the same to avoid unit test result changes.
Quoted 7 lines
Quoted 1 line
Signed-off-by Vladimir Sementsov-Ogievskiy <[email protected]>The change itself looks ok, but I'm not happy with the naming. It feels a bit unspecific. How about inverting the result and calling it
bdrv_parent_perms_conflict()andbdrv_child_perms_conflict()?At least, I'd call it "permission consistency" rather then "compliance".
bdrv_parent_perms_conflict() sound good for me, OK
Quoted 36 lines
Quoted 36 lines
block.c +27
-0@@ -1966,6 +1966,57 @@ bool bdrv_is_writable(BlockDriverState *bs)return bdrv_is_writable_after_reopen (bs,NULL ); } +static char *bdrv_child_user_desc (BdrvChild *c) +{ +if (c->klass->get_parent_desc) { +return c->klass->get_parent_desc (c); + } + +return g_strdup ("another user" ); +} + +static bool bdrv_a_allow_b (BdrvChild *a,BdrvChild *b,Error **errp) +{ + g_autofree char *user = NULL; + g_autofree char *perm_names = NULL; + +if ((b->perm & a->shared_perm) == b->perm) { +return true; + } + + perm_names =bdrv_perm_names (b->perm & ~a->shared_perm); + user =bdrv_child_user_desc (a); +error_setg (errp,"Conflicts with use by %s as '%s', which does not " +"allow '%s' on %s" , + user, a->name, perm_names,bdrv_get_node_name (b->bs)); + +return false; +} +
Quoted 8 lines
block.c bool bdrv_is_writable(BlockDriverState *bs) · line 2001
Quoted 6 lines
block.c +6
-0+static bool bdrv_check_parents_compliance (BlockDriverState *bs,Error **errp) +{ +BdrvChild *a, *b; + +/* +* During the loop we'll look at each pair twice. That's correct is s/is/because/ or what did you mean here?
yes, s/is/because/
Quoted 21 lines
Quoted 8 lines
block.c +7
-0+* bdrv_a_allow_b() is asymmetric and we should check each pair in both +* directions. +*/ + QLIST_FOREACH(a, &bs->parents, next_parent) { + QLIST_FOREACH(b, &bs->parents, next_parent) { +if (a == b) { +continue ;block.c bool bdrv_is_writable(BlockDriverState *bs) · line 2018
Quoted 10 lines
block.c +10
-0+ } + +if (!bdrv_a_allow_b (a, b, errp)) { +return false; + } + } + } + +return true; +}Kevin
Signature · 2 lines
Best regards, Vladimir
PATCH v2 8/36 block: make bdrv_reopen_{prepare, commit, abort} private
In reply to Vladimir Sementsov-OgievskiyThese functions are called only from bdrv_reopen_multiple() in block.c. No reason to publish them.
--- include/block/block.h | 4 ---- block.c | 13 +++++++++---- 2 files changed, 9 insertions(+), 8 deletions(-)
include/block/block.h +0 -4
@@ -373,10 +373,6 @@ BlockReopenQueue *bdrv_reopen_queue(BlockReopenQueue *bs_queue,int bdrv_reopen_multiple (BlockReopenQueue *bs_queue,Error **errp);int bdrv_reopen_set_read_only (BlockDriverState *bs,bool read_only,Error **errp); -int bdrv_reopen_prepare (BDRVReopenState *reopen_state, -BlockReopenQueue *queue,Error **errp); -void bdrv_reopen_commit (BDRVReopenState *reopen_state); -void bdrv_reopen_abort (BDRVReopenState *reopen_state);int bdrv_pwrite_zeroes (BdrvChild *child,int64_t offset,int bytes,BdrvRequestFlags flags);int bdrv_make_zero (BdrvChild *child,BdrvRequestFlags flags);
block.c +9 -4
@@ -84,6 +84,11 @@ static BlockDriverState *bdrv_open_inherit(const char *filename,static void bdrv_parent_set_aio_context_ignore (BdrvChild *c,AioContext *ctx,GSList **ignore); +static int bdrv_reopen_prepare (BDRVReopenState *reopen_state,BlockReopenQueue + *queue,Error **errp); +static void bdrv_reopen_commit (BDRVReopenState *reopen_state); +static void bdrv_reopen_abort (BDRVReopenState *reopen_state); +/* If non-zero, use only whitelisted block drivers */ static int use_bdrv_whitelist;
@@ -4082,8 +4087,8 @@ static int bdrv_reopen_parse_backing(BDRVReopenState *reopen_state,* commit() for any other BDS that have been left in a prepare() state * */ -int bdrv_reopen_prepare (BDRVReopenState *reopen_state,BlockReopenQueue *queue, -Error **errp) +static int bdrv_reopen_prepare (BDRVReopenState *reopen_state, +BlockReopenQueue *queue,Error **errp) {int ret =-1 ;int old_flags;
@@ -4298,7 +4303,7 @@ error:* makes them final by swapping the staging BlockDriverState contents into * the active BlockDriverState contents. */ -void bdrv_reopen_commit (BDRVReopenState *reopen_state) +static void bdrv_reopen_commit (BDRVReopenState *reopen_state) {BlockDriver *drv;BlockDriverState *bs;
@@ -4358,7 +4363,7 @@ void bdrv_reopen_commit(BDRVReopenState *reopen_state)* Abort the reopen, and delete and free the staged changes in * reopen_state */ -void bdrv_reopen_abort (BDRVReopenState *reopen_state) +static void bdrv_reopen_abort (BDRVReopenState *reopen_state) {BlockDriver *drv;
Signature · 1 line
2.21.3
Re: [PATCH v2 08/36] block: make bdrv_reopen_{prepare, commit, abort} private
Quoted 4 lines · Vladimir Sementsov-Ogievskiy via
These functions are called only from
bdrv_reopen_multiple()inblock.c. No reason to publish them.Signed-off-by Vladimir Sementsov-Ogievskiy <[email protected]>
Berto
Re: [PATCH v2 08/36] block: make bdrv_reopen_{prepare,commit,abort} private
In reply to Vladimir Sementsov-Ogievskiy viaAm 27.11.2020 um 15:44 hat Vladimir Sementsov-Ogievskiy geschrieben:
Quoted 4 lines
These functions are called only from
bdrv_reopen_multiple()inblock.c. No reason to publish them.Signed-off-by Vladimir Sementsov-Ogievskiy <[email protected]>
PATCH v2 12/36 block: refactor bdrv_child* permission functions
In reply to Vladimir Sementsov-OgievskiySplit out non-recursive parts, and refactor as block graph transaction action.
--- block.c | 79 ++++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 59 insertions(+), 20 deletions(-)
block.c +59 -20
@@ -48,6 +48,7 @@#include "qemu/timer.h" #include "qemu/cutils.h" #include "qemu/id.h" +#include "qemu/transactions.h" #include "block/coroutines.h" #ifdef CONFIG_BSD
@@ -2033,6 +2034,61 @@ static void bdrv_child_perm(BlockDriverState *bs, BlockDriverState *child_bs, } } +static void bdrv_child_set_perm_commit (void *opaque) +{ +BdrvChild *c = opaque; + + c->has_backup_perm = false; +} + +static void bdrv_child_set_perm_abort (void *opaque) +{ +BdrvChild *c = opaque; +/* +* We may have child->has_backup_perm unset at this point, as in case of +* _check_ stage of permission update failure we may _check_ not the whole +* subtree. Still, _abort_ is called on the whole subtree anyway. +*/ +if (c->has_backup_perm) { + c->perm = c->backup_perm; + c->shared_perm = c->backup_shared_perm; + c->has_backup_perm = false; + } +} + +static TransactionActionDrv bdrv_child_set_pem_drv = { + .abort = bdrv_child_set_perm_abort, + .commit = bdrv_child_set_perm_commit, +}; + +/* +* With tran=NULL needs to be followed by direct call to either +* bdrv_child_set_perm_commit() or bdrv_child_set_perm_abort(). +* +* With non-NUll tran needs to be followed by tran_abort() or tran_commit() +* instead. +*/ +static void bdrv_child_set_perm_safe (BdrvChild *c,uint64_t perm, +uint64_t shared,GSList **tran) +{ +if (!c->has_backup_perm) { + c->has_backup_perm = true; + c->backup_perm = c->perm; + c->backup_shared_perm = c->shared_perm; + } +/* +* Note: it's OK if c->has_backup_perm was already set, as we can find the +* same c twice during check_perm procedure +*/ + + c->perm = perm; + c->shared_perm = shared; + +if (tran) { +tran_prepend (tran, &bdrv_child_set_pem_drv, c); + } +} +/* * Check whether permissions on this node can be changed in a way that * @cumulative_perms and @cumulative_shared_perms are the new cumulative
@@ -2298,37 +2354,20 @@ static int bdrv_child_check_perm(BdrvChild *c, BlockReopenQueue *q,return ret; } -if (!c->has_backup_perm) { - c->has_backup_perm = true; - c->backup_perm = c->perm; - c->backup_shared_perm = c->shared_perm; - } -/* -* Note: it's OK if c->has_backup_perm was already set, as we can find the -* same child twice during check_perm procedure -*/ - - c->perm = perm; - c->shared_perm = shared; +bdrv_child_set_perm_safe (c, perm, shared,NULL );return 0 ; }static void bdrv_child_set_perm (BdrvChild *c) { - c->has_backup_perm = false; - +bdrv_child_set_perm_commit (c);bdrv_set_perm (c->bs); }static void bdrv_child_abort_perm_update (BdrvChild *c) { -if (c->has_backup_perm) { - c->perm = c->backup_perm; - c->shared_perm = c->backup_shared_perm; - c->has_backup_perm = false; - } - +bdrv_child_set_perm_abort (c);bdrv_abort_perm_update (c->bs); }
Signature · 1 line
2.21.3
Re: [PATCH v2 12/36] block: refactor bdrv_child* permission functions
Am 27.11.2020 um 15:44 hat Vladimir Sementsov-Ogievskiy geschrieben:
Quoted 46 lines
Split out non-recursive parts, and refactor as block graph transaction action.
Signed-off-by Vladimir Sementsov-Ogievskiy <[email protected]>--- block.c | 79 ++++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 59 insertions(+), 20 deletions(-)block.c +23
-0@@ -48,6 +48,7 @@#include "qemu/timer.h" #include "qemu/cutils.h" #include "qemu/id.h" +#include "qemu/transactions.h" #include "block/coroutines.h" #ifdef CONFIG_BSD @@ -2033,6 +2034,61 @@ static void bdrv_child_perm(BlockDriverState *bs, BlockDriverState *child_bs, } } +static void bdrv_child_set_perm_commit (void *opaque) +{ +BdrvChild *c = opaque; + + c->has_backup_perm = false; +} + +static void bdrv_child_set_perm_abort (void *opaque) +{ +BdrvChild *c = opaque; +/* +* We may have child->has_backup_perm unset at this point, as in case of +* _check_ stage of permission update failure we may _check_ not the whole +* subtree. Still, _abort_ is called on the whole subtree anyway. +*/ +if (c->has_backup_perm) { + c->perm = c->backup_perm; + c->shared_perm = c->backup_shared_perm; + c->has_backup_perm = false; + } +} +
block.c static void bdrv_child_perm(BlockDriverState *bs, BlockDriverState *child_bs, · line 2068
Quoted 10 lines
block.c +10
-0+static TransactionActionDrv bdrv_child_set_pem_drv = { + .abort = bdrv_child_set_perm_abort, + .commit = bdrv_child_set_perm_commit, +}; + +/* +* With tran=NULL needs to be followed by direct call to either +* bdrv_child_set_perm_commit() or bdrv_child_set_perm_abort(). +* +* With non-NUll tran needs to be followed by tran_abort() or tran_commit()
s/NUll/NULL/
block.c static void bdrv_child_perm(BlockDriverState *bs, BlockDriverState *child_bs, · line 2078
Quoted 10 lines
block.c +10
-0+* instead. +*/ +static void bdrv_child_set_perm_safe (BdrvChild *c,uint64_t perm, +uint64_t shared,GSList **tran) +{ +if (!c->has_backup_perm) { + c->has_backup_perm = true; + c->backup_perm = c->perm; + c->backup_shared_perm = c->shared_perm; + }
This is the obvious refactoring at this point, and it's fine as such.
However, when you start to actually use tran (and in new places), it means that I have to check that we can never end up here recursively with a different tran.
It would probably be much cleaner if the next patch moved the backup state into the opaque struct for BdrvAction.
Kevin
Re: [PATCH v2 12/36] block: refactor bdrv_child* permission functions
Quoted 73 lines · Kevin Wolf
Am 27.11.2020 um 15:44 hat Vladimir Sementsov-Ogievskiy geschrieben:
Quoted 46 lines
Split out non-recursive parts, and refactor as block graph transaction action.
Signed-off-by Vladimir Sementsov-Ogievskiy <[email protected]>--- block.c | 79 ++++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 59 insertions(+), 20 deletions(-)block.c +23
-0@@ -48,6 +48,7 @@#include "qemu/timer.h" #include "qemu/cutils.h" #include "qemu/id.h" +#include "qemu/transactions.h" #include "block/coroutines.h" #ifdef CONFIG_BSD @@ -2033,6 +2034,61 @@ static void bdrv_child_perm(BlockDriverState *bs, BlockDriverState *child_bs, } } +static void bdrv_child_set_perm_commit (void *opaque) +{ +BdrvChild *c = opaque; + + c->has_backup_perm = false; +} + +static void bdrv_child_set_perm_abort (void *opaque) +{ +BdrvChild *c = opaque; +/* +* We may have child->has_backup_perm unset at this point, as in case of +* _check_ stage of permission update failure we may _check_ not the whole +* subtree. Still, _abort_ is called on the whole subtree anyway. +*/ +if (c->has_backup_perm) { + c->perm = c->backup_perm; + c->shared_perm = c->backup_shared_perm; + c->has_backup_perm = false; + } +} +block.c static void bdrv_child_perm(BlockDriverState *bs, BlockDriverState *child_bs, · line 2068
Quoted 10 lines
block.c +10
-0+static TransactionActionDrv bdrv_child_set_pem_drv = { + .abort = bdrv_child_set_perm_abort, + .commit = bdrv_child_set_perm_commit, +}; + +/* +* With tran=NULL needs to be followed by direct call to either +* bdrv_child_set_perm_commit() or bdrv_child_set_perm_abort(). +* +* With non-NUll tran needs to be followed by tran_abort() or tran_commit() s/NUll/NULL/
block.c static void bdrv_child_perm(BlockDriverState *bs, BlockDriverState *child_bs, · line 2078
Quoted 10 lines
block.c +10
-0+* instead. +*/ +static void bdrv_child_set_perm_safe (BdrvChild *c,uint64_t perm, +uint64_t shared,GSList **tran) +{ +if (!c->has_backup_perm) { + c->has_backup_perm = true; + c->backup_perm = c->perm; + c->backup_shared_perm = c->shared_perm; + }This is the obvious refactoring at this point, and it's fine as such.
Quoted 3 lines
However, when you start to actually use tran (and in new places), it means that I have to check that we can never end up here recursively with a different tran.
I don't follow.. Which different tran do you mean?
Quoted 3 lines
It would probably be much cleaner if the next patch moved the backup state into the opaque struct for BdrvAction.
But old code which calls the function with tran=NULL can't use it.. Hmm, we can probably support both ways: c->backup_perm for callers with tran=NULL and opaque struct for new style callers.
Signature · 2 lines
Best regards, Vladimir
Re: [PATCH v2 12/36] block: refactor bdrv_child* permission functions
Am 19.01.2021 um 19:30 hat Vladimir Sementsov-Ogievskiy geschrieben:
Quoted 72 lines
Quoted 71 lines · Kevin Wolf
Am 27.11.2020 um 15:44 hat Vladimir Sementsov-Ogievskiy geschrieben:
Quoted 44 lines
Split out non-recursive parts, and refactor as block graph transaction action.
Signed-off-by Vladimir Sementsov-Ogievskiy <[email protected]>--- block.c | 79 ++++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 59 insertions(+), 20 deletions(-)block.c +23
-0@@ -48,6 +48,7 @@#include "qemu/timer.h" #include "qemu/cutils.h" #include "qemu/id.h" +#include "qemu/transactions.h" #include "block/coroutines.h" #ifdef CONFIG_BSD @@ -2033,6 +2034,61 @@ static void bdrv_child_perm(BlockDriverState *bs, BlockDriverState *child_bs, } } +static void bdrv_child_set_perm_commit (void *opaque) +{ +BdrvChild *c = opaque; + + c->has_backup_perm = false; +} + +static void bdrv_child_set_perm_abort (void *opaque) +{ +BdrvChild *c = opaque; +/* +* We may have child->has_backup_perm unset at this point, as in case of +* _check_ stage of permission update failure we may _check_ not the whole +* subtree. Still, _abort_ is called on the whole subtree anyway. +*/ +if (c->has_backup_perm) { + c->perm = c->backup_perm; + c->shared_perm = c->backup_shared_perm; + c->has_backup_perm = false; + } +} +block.c static void bdrv_child_perm(BlockDriverState *bs, BlockDriverState *child_bs, · line 2067
Quoted 10 lines
block.c +10
-0+static TransactionActionDrv bdrv_child_set_pem_drv = { + .abort = bdrv_child_set_perm_abort, + .commit = bdrv_child_set_perm_commit, +}; + +/* +* With tran=NULL needs to be followed by direct call to either +* bdrv_child_set_perm_commit() or bdrv_child_set_perm_abort(). +* +* With non-NUll tran needs to be followed by tran_abort() or tran_commit() s/NUll/NULL/
block.c static void bdrv_child_perm(BlockDriverState *bs, BlockDriverState *child_bs, · line 2078
Quoted 10 lines
block.c +10
-0+* instead. +*/ +static void bdrv_child_set_perm_safe (BdrvChild *c,uint64_t perm, +uint64_t shared,GSList **tran) +{ +if (!c->has_backup_perm) { + c->has_backup_perm = true; + c->backup_perm = c->perm; + c->backup_shared_perm = c->shared_perm; + }This is the obvious refactoring at this point, and it's fine as such.
Quoted 5 lines
Quoted 3 lines
However, when you start to actually use tran (and in new places), it means that I have to check that we can never end up here recursively with a different tran.
I don't follow.. Which different tran do you mean?
Any really. At this point in the series, nothing passes a non-NULL tran yet. When you add the first user, it is only a local transaction for a single node. If something else called bdrv_child_set_perm_safe() on the same node before the transaction has completed, the result would be broken.
So reviewing/understanding this change (and actually developing it in the first place) means going through all the code that ends up inside the transaction and making sure that we never try to change permissions for the same node a second time in any context.
Quoted 7 lines
Quoted 3 lines
It would probably be much cleaner if the next patch moved the backup state into the opaque struct for BdrvAction.
But old code which calls the function with tran=NULL can't use it.. Hmm, we can probably support both ways:
c->backup_permfor callers with tran=NULL and opaque struct for new style callers.
Hm, you're right about that... Maybe that's too much complication then.
What happens in the next patch is still understandable enough with the way you currently have it. Let's see what it looks like with the rest.
Kevin
Re: [PATCH v2 12/36] block: refactor bdrv_child* permission functions
Quoted 77 lines · Kevin Wolf
Am 19.01.2021 um 19:30 hat Vladimir Sementsov-Ogievskiy geschrieben:
Quoted 76 lines
Quoted 74 lines · Kevin Wolf
Am 27.11.2020 um 15:44 hat Vladimir Sementsov-Ogievskiy geschrieben:
Quoted 44 lines
Split out non-recursive parts, and refactor as block graph transaction action.
Signed-off-by Vladimir Sementsov-Ogievskiy <[email protected]>--- block.c | 79 ++++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 59 insertions(+), 20 deletions(-)block.c +23
-0@@ -48,6 +48,7 @@#include "qemu/timer.h" #include "qemu/cutils.h" #include "qemu/id.h" +#include "qemu/transactions.h" #include "block/coroutines.h" #ifdef CONFIG_BSD @@ -2033,6 +2034,61 @@ static void bdrv_child_perm(BlockDriverState *bs, BlockDriverState *child_bs, } } +static void bdrv_child_set_perm_commit (void *opaque) +{ +BdrvChild *c = opaque; + + c->has_backup_perm = false; +} + +static void bdrv_child_set_perm_abort (void *opaque) +{ +BdrvChild *c = opaque; +/* +* We may have child->has_backup_perm unset at this point, as in case of +* _check_ stage of permission update failure we may _check_ not the whole +* subtree. Still, _abort_ is called on the whole subtree anyway. +*/ +if (c->has_backup_perm) { + c->perm = c->backup_perm; + c->shared_perm = c->backup_shared_perm; + c->has_backup_perm = false; + } +} +block.c static void bdrv_child_perm(BlockDriverState *bs, BlockDriverState *child_bs, · line 2067
Quoted 10 lines
block.c +10
-0+static TransactionActionDrv bdrv_child_set_pem_drv = { + .abort = bdrv_child_set_perm_abort, + .commit = bdrv_child_set_perm_commit, +}; + +/* +* With tran=NULL needs to be followed by direct call to either +* bdrv_child_set_perm_commit() or bdrv_child_set_perm_abort(). +* +* With non-NUll tran needs to be followed by tran_abort() or tran_commit() s/NUll/NULL/
block.c static void bdrv_child_perm(BlockDriverState *bs, BlockDriverState *child_bs, · line 2078
Quoted 10 lines
block.c +10
-0+* instead. +*/ +static void bdrv_child_set_perm_safe (BdrvChild *c,uint64_t perm, +uint64_t shared,GSList **tran) +{ +if (!c->has_backup_perm) { + c->has_backup_perm = true; + c->backup_perm = c->perm; + c->backup_shared_perm = c->shared_perm; + }This is the obvious refactoring at this point, and it's fine as such.
However, when you start to actually use tran (and in new places), it means that I have to check that we can never end up here recursively with a different tran.
Quoted 7 lines
Quoted 1 line
I don't follow.. Which different tran do you mean?
Any really. At this point in the series, nothing passes a non-NULL tran yet. When you add the first user, it is only a local transaction for a single node. If something else called
bdrv_child_set_perm_safe()on the same node before the transaction has completed, the result would be broken.
But this problem is preexisting: if someone call bdrv_child_set_perm twice on the same node during one update operation, c->backup* will be rewritten.
Quoted 5 lines
So reviewing/understanding this change (and actually developing it in the first place) means going through all the code that ends up inside the transaction and making sure that we never try to change permissions for the same node a second time in any context.
I think we do it, when find same node several times during update. And that is fixed in "[PATCH v2 15/36] block: use topological sort for permission update", when we move to topological sorted list.
Quoted 14 lines
Quoted 8 lines
Quoted 4 lines
It would probably be much cleaner if the next patch moved the backup state into the opaque struct for BdrvAction.
But old code which calls the function with tran=NULL can't use it.. Hmm, we can probably support both ways:
c->backup_permfor callers with tran=NULL and opaque struct for new style callers.Hm, you're right about that... Maybe that's too much complication then.
What happens in the next patch is still understandable enough with the way you currently have it. Let's see what it looks like with the rest.
Signature · 2 lines
Best regards, Vladimir
Re: [PATCH v2 12/36] block: refactor bdrv_child* permission functions
Am 20.01.2021 um 10:56 hat Vladimir Sementsov-Ogievskiy geschrieben:
Quoted 91 lines
Quoted 77 lines · Kevin Wolf
Am 19.01.2021 um 19:30 hat Vladimir Sementsov-Ogievskiy geschrieben:
Quoted 76 lines
Quoted 74 lines · Kevin Wolf
Am 27.11.2020 um 15:44 hat Vladimir Sementsov-Ogievskiy geschrieben:
Quoted 44 lines
Split out non-recursive parts, and refactor as block graph transaction action.
Signed-off-by Vladimir Sementsov-Ogievskiy <[email protected]>--- block.c | 79 ++++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 59 insertions(+), 20 deletions(-)block.c +23
-0@@ -48,6 +48,7 @@#include "qemu/timer.h" #include "qemu/cutils.h" #include "qemu/id.h" +#include "qemu/transactions.h" #include "block/coroutines.h" #ifdef CONFIG_BSD @@ -2033,6 +2034,61 @@ static void bdrv_child_perm(BlockDriverState *bs, BlockDriverState *child_bs, } } +static void bdrv_child_set_perm_commit (void *opaque) +{ +BdrvChild *c = opaque; + + c->has_backup_perm = false; +} + +static void bdrv_child_set_perm_abort (void *opaque) +{ +BdrvChild *c = opaque; +/* +* We may have child->has_backup_perm unset at this point, as in case of +* _check_ stage of permission update failure we may _check_ not the whole +* subtree. Still, _abort_ is called on the whole subtree anyway. +*/ +if (c->has_backup_perm) { + c->perm = c->backup_perm; + c->shared_perm = c->backup_shared_perm; + c->has_backup_perm = false; + } +} +block.c static void bdrv_child_perm(BlockDriverState *bs, BlockDriverState *child_bs, · line 2067
Quoted 10 lines
block.c +10
-0+static TransactionActionDrv bdrv_child_set_pem_drv = { + .abort = bdrv_child_set_perm_abort, + .commit = bdrv_child_set_perm_commit, +}; + +/* +* With tran=NULL needs to be followed by direct call to either +* bdrv_child_set_perm_commit() or bdrv_child_set_perm_abort(). +* +* With non-NUll tran needs to be followed by tran_abort() or tran_commit() s/NUll/NULL/
block.c static void bdrv_child_perm(BlockDriverState *bs, BlockDriverState *child_bs, · line 2078
Quoted 10 lines
block.c +10
-0+* instead. +*/ +static void bdrv_child_set_perm_safe (BdrvChild *c,uint64_t perm, +uint64_t shared,GSList **tran) +{ +if (!c->has_backup_perm) { + c->has_backup_perm = true; + c->backup_perm = c->perm; + c->backup_shared_perm = c->shared_perm; + }This is the obvious refactoring at this point, and it's fine as such.
However, when you start to actually use tran (and in new places), it means that I have to check that we can never end up here recursively with a different tran.
Quoted 7 lines
Quoted 1 line
I don't follow.. Which different tran do you mean?
Any really. At this point in the series, nothing passes a non-NULL tran yet. When you add the first user, it is only a local transaction for a single node. If something else called
bdrv_child_set_perm_safe()on the same node before the transaction has completed, the result would be broken.But this problem is preexisting: if someone call
bdrv_child_set_permtwice on the same node during one update operation, c->backup* will be rewritten.Quoted 1 line
Quoted 8 lines
Quoted 4 lines
So reviewing/understanding this change (and actually developing it in the first place) means going through all the code that ends up inside the transaction and making sure that we never try to change permissions for the same node a second time in any context.
I think we do it, when find same node several times during update. And that is fixed in "[PATCH v2 15/36] block: use topological sort for permission update", when we move to topological sorted list.
Ah, fair. Knowing that the state is broken before this patch makes things easier in a way...
Kevin
PATCH v2 13/36 block: rewrite bdrv_child_try_set_perm() using bdrv_refresh_perms()
In reply to Vladimir Sementsov-OgievskiyWe are going to drop recursive bdrv_child_* functions, so stop use them in bdrv_child_try_set_perm() as a first step.
--- block.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-)
block.c +8 -6
@@ -2394,11 +2394,16 @@ int bdrv_child_try_set_perm(BdrvChild *c, uint64_t perm, uint64_t shared, Error **errp) {Error *local_err =NULL ; +GSList *tran =NULL ;int ret; - ret =bdrv_child_check_perm (c,NULL , perm, shared,NULL , &local_err); +bdrv_child_set_perm_safe (c, perm, shared, &tran); + + ret =bdrv_refresh_perms (c->bs, &local_err); + +tran_finalize (tran, ret); +if (ret <0 ) { -bdrv_child_abort_perm_update (c);if ((perm & ~c->perm) || (c->shared_perm & ~shared)) {/* tighten permissions */ error_propagate (errp, local_err);
@@ -2412,12 +2417,9 @@ int bdrv_child_try_set_perm(BdrvChild *c, uint64_t perm, uint64_t shared,error_free (local_err); ret =0 ; } -return ret; } -bdrv_child_set_perm (c); - -return 0 ; +return ret; }int bdrv_child_refresh_perms (BlockDriverState *bs,BdrvChild *c,Error **errp)
Signature · 1 line
2.21.3
PATCH v2 14/36 block: inline bdrv_child_*() permission functions calls
In reply to Vladimir Sementsov-OgievskiyEach of them has only one caller. Open-coding simplifies further pemission-update system changes.
--- block.c | 59 +++++++++++++++++---------------------------------------- 1 file changed, 17 insertions(+), 42 deletions(-)
block.c +17 -42
@@ -1914,11 +1914,11 @@ static int bdrv_fill_options(QDict **options, const char *filename,return 0 ; } -static int bdrv_child_check_perm (BdrvChild *c,BlockReopenQueue *q, -uint64_t perm,uint64_t shared, -GSList *ignore_children,Error **errp); -static void bdrv_child_abort_perm_update (BdrvChild *c); -static void bdrv_child_set_perm (BdrvChild *c); +static int bdrv_check_update_perm (BlockDriverState *bs,BlockReopenQueue *q, +uint64_t new_used_perm, +uint64_t new_shared_perm, +GSList *ignore_children, +Error **errp); typedef struct BlockReopenQueueEntry { bool prepared;
@@ -2166,15 +2166,21 @@ static int bdrv_check_perm(BlockDriverState *bs, BlockReopenQueue *q,/* Check all children */ QLIST_FOREACH(c, &bs->children, next) {uint64_t cur_perm, cur_shared; +GSList *cur_ignore_children;bdrv_child_perm (bs, c->bs, c, c->role, q, cumulative_perms, cumulative_shared_perms, &cur_perm, &cur_shared); - ret =bdrv_child_check_perm (c, q, cur_perm, cur_shared, ignore_children, - errp); + + cur_ignore_children =g_slist_prepend (g_slist_copy (ignore_children), c); + ret =bdrv_check_update_perm (c->bs, q, cur_perm, cur_shared, + cur_ignore_children, errp); +g_slist_free (cur_ignore_children);if (ret <0 ) {return ret; } + +bdrv_child_set_perm_safe (c, cur_perm, cur_shared,NULL ); }return 0 ;
@@ -2201,7 +2207,8 @@ static void bdrv_abort_perm_update(BlockDriverState *bs) } QLIST_FOREACH(c, &bs->children, next) { -bdrv_child_abort_perm_update (c); +bdrv_child_set_perm_abort (c); +bdrv_abort_perm_update (c->bs); } }
@@ -2230,7 +2237,8 @@ static void bdrv_set_perm(BlockDriverState *bs)/* Update all children */ QLIST_FOREACH(c, &bs->children, next) { -bdrv_child_set_perm (c); +bdrv_child_set_perm_commit (c); +bdrv_set_perm (c->bs); } }
@@ -2338,39 +2346,6 @@ static int bdrv_check_update_perm(BlockDriverState *bs, BlockReopenQueue *q, ignore_children, errp); } -/* Needs to be followed by a call to either bdrv_child_set_perm() or -* bdrv_child_abort_perm_update(). */ -static int bdrv_child_check_perm (BdrvChild *c,BlockReopenQueue *q, -uint64_t perm,uint64_t shared, -GSList *ignore_children,Error **errp) -{ -int ret; - - ignore_children =g_slist_prepend (g_slist_copy (ignore_children), c); - ret =bdrv_check_update_perm (c->bs, q, perm, shared, ignore_children, errp); -g_slist_free (ignore_children); - -if (ret <0 ) { -return ret; - } - -bdrv_child_set_perm_safe (c, perm, shared,NULL ); - -return 0 ; -} - -static void bdrv_child_set_perm (BdrvChild *c) -{ -bdrv_child_set_perm_commit (c); -bdrv_set_perm (c->bs); -} - -static void bdrv_child_abort_perm_update (BdrvChild *c) -{ -bdrv_child_set_perm_abort (c); -bdrv_abort_perm_update (c->bs); -} -static int bdrv_refresh_perms (BlockDriverState *bs,Error **errp) {int ret;
Signature · 1 line
2.21.3
Re: [PATCH v2 14/36] block: inline bdrv_child_*() permission functions calls
Quoted 4 lines · Vladimir Sementsov-Ogievskiy
Each of them has only one caller. Open-coding simplifies further pemission-update system changes.
Signed-off-by Vladimir Sementsov-Ogievskiy <[email protected]>
Berto
PATCH v2 15/36 block: use topological sort for permission update
In reply to Vladimir Sementsov-OgievskiyRewrite bdrv_check_perm(), bdrv_abort_perm_update() and bdrv_set_perm() to update nodes in topological sort order instead of simple DFS. With topologically sorted nodes, we update a node only when all its parents already updated. With DFS it's not so.
Consider the following example:
A -+
| |
| v
| B
| |
v |
C<-+A is parent for B and C, B is parent for C.
Obviously, to update permissions, we should go in order A B C, so, when we update C, all parent permissions already updated. But with current approach (simple recursion) we can update in sequence A C B C (C is updated twice). On first update of C, we consider old B permissions, so doing wrong thing. If it succeed, all is OK, on second C update we will finish with correct graph. But if the wrong thing failed, we break the whole process for no reason (it's possible that updated B permission will be less strict, but we will never check it).
Also new approach gives a way to simultaneously and correctly update several nodes, we just need to run bdrv_topological_dfs() several times to add all nodes and their subtrees into one topologically sorted list (next patch will update bdrv_replace_node() in this manner).
Test test_parallel_perm_update() is now passing, so move it out of debugging "if".
We also need to support ignore_children inbdrv_check_parents_compliance().
For test 283 order of parents compliance check is changed.
--- block.c | 103 +++++++++++++++++++++++++++++------- tests/test-bdrv-graph-mod.c | 4 +- tests/qemu-iotests/283.out | 2 +- 3 files changed, 86 insertions(+), 23 deletions(-)
block.c +83 -20
@@ -1994,7 +1994,9 @@ static bool bdrv_a_allow_b(BdrvChild *a, BdrvChild *b, Error **errp)return false; } -static bool bdrv_check_parents_compliance (BlockDriverState *bs,Error **errp) +static bool bdrv_check_parents_compliance (BlockDriverState *bs, +GSList *ignore_children, +Error **errp) {BdrvChild *a, *b;
@@ -2005,7 +2007,9 @@ static bool bdrv_check_parents_compliance(BlockDriverState *bs, Error **errp)*/ QLIST_FOREACH(a, &bs->parents, next_parent) { QLIST_FOREACH(b, &bs->parents, next_parent) { -if (a == b) { +if (a == b ||g_slist_find (ignore_children, a) || +g_slist_find (ignore_children, b)) + {continue ; }
@@ -2034,6 +2038,29 @@ static void bdrv_child_perm(BlockDriverState *bs, BlockDriverState *child_bs, } } +static GSList *bdrv_topological_dfs (GSList *list,GHashTable *found, +BlockDriverState *bs) +{ +BdrvChild *child; +g_autoptr (GHashTable) local_found =NULL ; + +if (!found) { +assert (!list); + found = local_found =g_hash_table_new (NULL ,NULL ); + } + +if (g_hash_table_contains (found, bs)) { +return list; + } +g_hash_table_add (found, bs); + + QLIST_FOREACH(child, &bs->children, next) { + list =bdrv_topological_dfs (list, found, child->bs); + } + +return g_slist_prepend (list, bs); +} +static void bdrv_child_set_perm_commit (void *opaque) {BdrvChild *c = opaque;
@@ -2098,10 +2125,10 @@ static void bdrv_child_set_perm_safe(BdrvChild *c, uint64_t perm,* A call to this function must always be followed by a call to bdrv_set_perm() * or bdrv_abort_perm_update(). */ -static int bdrv_check_perm (BlockDriverState *bs,BlockReopenQueue *q, -uint64_t cumulative_perms, -uint64_t cumulative_shared_perms, -GSList *ignore_children,Error **errp) +static int bdrv_node_check_perm (BlockDriverState *bs,BlockReopenQueue *q, +uint64_t cumulative_perms, +uint64_t cumulative_shared_perms, +GSList *ignore_children,Error **errp) {BlockDriver *drv = bs->drv;BdrvChild *c;
@@ -2166,21 +2193,43 @@ static int bdrv_check_perm(BlockDriverState *bs, BlockReopenQueue *q,/* Check all children */ QLIST_FOREACH(c, &bs->children, next) {uint64_t cur_perm, cur_shared; -GSList *cur_ignore_children;bdrv_child_perm (bs, c->bs, c, c->role, q, cumulative_perms, cumulative_shared_perms, &cur_perm, &cur_shared); +bdrv_child_set_perm_safe (c, cur_perm, cur_shared,NULL ); + } + +return 0 ; +} + +static int bdrv_check_perm (BlockDriverState *bs,BlockReopenQueue *q, +uint64_t cumulative_perms, +uint64_t cumulative_shared_perms, +GSList *ignore_children,Error **errp) +{ +int ret; +BlockDriverState *root = bs; +g_autoptr (GSList) list =bdrv_topological_dfs (NULL ,NULL , root); + +for ( ; list; list = list->next) { + bs = list->data; + +if (bs != root) { +if (!bdrv_check_parents_compliance (bs, ignore_children, errp)) { +return -EINVAL ; + } + +bdrv_get_cumulative_perm (bs, &cumulative_perms, + &cumulative_shared_perms); + } - cur_ignore_children =g_slist_prepend (g_slist_copy (ignore_children), c); - ret =bdrv_check_update_perm (c->bs, q, cur_perm, cur_shared, - cur_ignore_children, errp); -g_slist_free (cur_ignore_children); + ret =bdrv_node_check_perm (bs, q, cumulative_perms, + cumulative_shared_perms, + ignore_children, errp);if (ret <0 ) {return ret; } - -bdrv_child_set_perm_safe (c, cur_perm, cur_shared,NULL ); }return 0 ;
@@ -2190,10 +2239,8 @@ static int bdrv_check_perm(BlockDriverState *bs, BlockReopenQueue *q,* Notifies drivers that after a previous bdrv_check_perm() call, the * permission update is not performed and any preparations made for it (e.g. * taken file locks) need to be undone. -* -* This function recursively notifies all child nodes. */ -static void bdrv_abort_perm_update (BlockDriverState *bs) +static void bdrv_node_abort_perm_update (BlockDriverState *bs) {BlockDriver *drv = bs->drv;BdrvChild *c;
@@ -2208,11 +2255,19 @@ static void bdrv_abort_perm_update(BlockDriverState *bs) QLIST_FOREACH(c, &bs->children, next) {bdrv_child_set_perm_abort (c); -bdrv_abort_perm_update (c->bs); } } -static void bdrv_set_perm (BlockDriverState *bs) +static void bdrv_abort_perm_update (BlockDriverState *bs) +{ +g_autoptr (GSList) list =bdrv_topological_dfs (NULL ,NULL , bs); + +for ( ; list; list = list->next) { +bdrv_node_abort_perm_update ((BlockDriverState *)list->data); + } +} + +static void bdrv_node_set_perm (BlockDriverState *bs) {uint64_t cumulative_perms, cumulative_shared_perms;BlockDriver *drv = bs->drv;
@@ -2238,7 +2293,15 @@ static void bdrv_set_perm(BlockDriverState *bs)/* Update all children */ QLIST_FOREACH(c, &bs->children, next) {bdrv_child_set_perm_commit (c); -bdrv_set_perm (c->bs); + } +} + +static void bdrv_set_perm (BlockDriverState *bs) +{ +g_autoptr (GSList) list =bdrv_topological_dfs (NULL ,NULL , bs); + +for ( ; list; list = list->next) { +bdrv_node_set_perm ((BlockDriverState *)list->data); } }
@@ -2351,7 +2414,7 @@ static int bdrv_refresh_perms(BlockDriverState *bs, Error **errp)int ret;uint64_t perm, shared_perm; -if (!bdrv_check_parents_compliance (bs, errp)) { +if (!bdrv_check_parents_compliance (bs,NULL , errp)) {return -EPERM ; }bdrv_get_cumulative_perm (bs, &perm, &shared_perm);
tests/test-bdrv-graph-mod.c +2 -2
@@ -316,12 +316,12 @@ int main(int argc, char *argv[])g_test_add_func ("/bdrv-graph-mod/update-perm-tree" , test_update_perm_tree);g_test_add_func ("/bdrv-graph-mod/should-update-child" , test_should_update_child); +g_test_add_func ("/bdrv-graph-mod/parallel-perm-update" , + test_parallel_perm_update);if (debug) {g_test_add_func ("/bdrv-graph-mod/parallel-exclusive-write" , test_parallel_exclusive_write); -g_test_add_func ("/bdrv-graph-mod/parallel-perm-update" , - test_parallel_perm_update); }return g_test_run ();
tests/qemu-iotests/283.out +1 -1
@@ -5,4 +5,4 @@ {"execute": "blockdev-add", "arguments": {"driver": "blkdebug", "image": "base", "node-name": "other", "take-child-perms": ["write"]}} {"return": {}} {"execute": "blockdev-backup", "arguments": {"device": "source", "sync": "full", "target": "target"}} -{"error": {"class": "GenericError", "desc": "Cannot set permissions for backup-top filter: Conflicts with use by other as 'image', which uses 'write' on base"}} +{"error": {"class": "GenericError", "desc": "Cannot set permissions for backup-top filter: Conflicts with use by source as 'image', which does not allow 'write' on base"}}
Signature · 1 line
2.21.3
Re: [PATCH v2 15/36] block: use topological sort for permission update
Am 27.11.2020 um 15:45 hat Vladimir Sementsov-Ogievskiy geschrieben:
Quoted 15 lines
Rewrite
bdrv_check_perm(),bdrv_abort_perm_update()andbdrv_set_perm()to update nodes in topological sort order instead of simple DFS. With topologically sorted nodes, we update a node only when all its parents already updated. With DFS it's not so.Consider the following example:
A -+ | | | v | B | | v | C<-+
Quoted 4 lines
A is parent for B and C, B is parent for C.
Obviously, to update permissions, we should go in order A B C, so, when we update C, all parent permissions already updated.
I wondered for a moment why this order is obvious. Taking a permission on A may mean that we need to take the permisson on C, too.
The answer is (or so I think) that the whole operation is atomic so the half-updated state will never be visible to a caller, but this is about calculating the right permissions. Permissions a node needs on its children may depend on what its parents requested, but parent permissions never depend on what children request.
Ok, makes sense.
Quoted 44 lines
But with current
approach (simple recursion) we can update in sequence A C B C (C is updated twice). On first update of C, we consider old B permissions, so doing wrong thing. If it succeed, all is OK, on second C update we will finish with correct graph. But if the wrong thing failed, we break the whole process for no reason (it's possible that updated B permission will be less strict, but we will never check it).Also new approach gives a way to simultaneously and correctly update several nodes, we just need to run
bdrv_topological_dfs()several times to add all nodes and their subtrees into one topologically sorted list (next patch will updatebdrv_replace_node()in this manner).Test
test_parallel_perm_update()is now passing, so move it out of debugging "if".We also need to support
ignore_childreninbdrv_check_parents_compliance().For test 283 order of parents compliance check is changed.
Signed-off-by Vladimir Sementsov-Ogievskiy <[email protected]>--- block.c | 103 +++++++++++++++++++++++++++++------- tests/test-bdrv-graph-mod.c | 4 +- tests/qemu-iotests/283.out | 2 +- 3 files changed, 86 insertions(+), 23 deletions(-)block.c +3
-1@@ -1994,7 +1994,9 @@ static bool bdrv_a_allow_b(BdrvChild *a, BdrvChild *b, Error **errp)return false; } -static bool bdrv_check_parents_compliance (BlockDriverState *bs,Error **errp) +static bool bdrv_check_parents_compliance (BlockDriverState *bs, +GSList *ignore_children, +Error **errp) {BdrvChild *a, *b;
block.c static bool bdrv_check_parents_compliance(BlockDriverState *bs, Error **errp) · line 2011
Quoted 6 lines
block.c +2
-1@@ -2005,7 +2007,9 @@ static bool bdrv_check_parents_compliance(BlockDriverState *bs, Error **errp)*/ QLIST_FOREACH(a, &bs->parents, next_parent) { QLIST_FOREACH(b, &bs->parents, next_parent) { -if (a == b) { +if (a == b ||g_slist_find (ignore_children, a) || +g_slist_find (ignore_children, b))
'a' should be checked in the outer loop, no reason to repeat the same check all the time in the inner loop.
block.c static void bdrv_child_perm(BlockDriverState *bs, BlockDriverState *child_bs, · line 2042
Quoted 10 lines
block.c +3
-0+ {continue ; }@@ -2034,6 +2038,29 @@ static void bdrv_child_perm(BlockDriverState *bs, BlockDriverState *child_bs, } } +static GSList *bdrv_topological_dfs (GSList *list,GHashTable *found, +BlockDriverState *bs)
It would be good to have a comment that explains the details of the contract.
In particular, this seems to require that @list is already topologically sorted, and it's complete in the sense that if a node is in the list, all of its children are in the list, too.
Quoted 25 lines
block.c +21
-0+{ +BdrvChild *child; +g_autoptr (GHashTable) local_found =NULL ; + +if (!found) { +assert (!list); + found = local_found =g_hash_table_new (NULL ,NULL ); + } + +if (g_hash_table_contains (found, bs)) { +return list; + } +g_hash_table_add (found, bs); + + QLIST_FOREACH(child, &bs->children, next) { + list =bdrv_topological_dfs (list, found, child->bs); + } + +return g_slist_prepend (list, bs); +} +static void bdrv_child_set_perm_commit (void *opaque) {BdrvChild *c = opaque;
block.c static void bdrv_child_set_perm_safe(BdrvChild *c, uint64_t perm, · line 2127
Quoted 3 lines
block.c +0
-0@@ -2098,10 +2125,10 @@ static void bdrv_child_set_perm_safe(BdrvChild *c, uint64_t perm,* A call to this function must always be followed by a call to bdrv_set_perm() * or bdrv_abort_perm_update(). */
One big source of confusion for me when trying to understand this was that bdrv_check_perm() is a misnomer since commit f962e96150e and the above comment isn't really accurate any more.
The function doesn't only check the validity of the new permissions in advance to actually making the change, but it already updates the permissions of all child nodes (however not of its root node).
So we have gone from the original check/set/abort model (which the function names still suggest) to a prepare/commit/rollback model.
I think some comment updates are in order, and possibly we should rename some functions, too.
Quoted 12 lines
block.c +4
-4-static int bdrv_check_perm (BlockDriverState *bs,BlockReopenQueue *q, -uint64_t cumulative_perms, -uint64_t cumulative_shared_perms, -GSList *ignore_children,Error **errp) +static int bdrv_node_check_perm (BlockDriverState *bs,BlockReopenQueue *q, +uint64_t cumulative_perms, +uint64_t cumulative_shared_perms, +GSList *ignore_children,Error **errp) {BlockDriver *drv = bs->drv;BdrvChild *c;
block.c static int bdrv_check_perm(BlockDriverState *bs, BlockReopenQueue *q, · line 2200
Quoted 9 lines
block.c +1
-1@@ -2166,21 +2193,43 @@ static int bdrv_check_perm(BlockDriverState *bs, BlockReopenQueue *q,/* Check all children */ QLIST_FOREACH(c, &bs->children, next) {uint64_t cur_perm, cur_shared; -GSList *cur_ignore_children;bdrv_child_perm (bs, c->bs, c, c->role, q, cumulative_perms, cumulative_shared_perms, &cur_perm, &cur_shared); +bdrv_child_set_perm_safe (c, cur_perm, cur_shared,NULL );
This "added" line is actually old code. What is removed here is the recursive call of bdrv_check_update_perm(). This is what the code below will have to replace.
Quoted 11 lines
block.c +11
-0+ } + +return 0 ; +} + +static int bdrv_check_perm (BlockDriverState *bs,BlockReopenQueue *q, +uint64_t cumulative_perms, +uint64_t cumulative_shared_perms, +GSList *ignore_children,Error **errp) +{ +int ret;
block.c static int bdrv_check_perm(BlockDriverState *bs, BlockReopenQueue *q, · line 2221
Quoted 10 lines
block.c +10
-0+BlockDriverState *root = bs; +g_autoptr (GSList) list =bdrv_topological_dfs (NULL ,NULL , root); + +for ( ; list; list = list->next) { + bs = list->data; + +if (bs != root) { +if (!bdrv_check_parents_compliance (bs, ignore_children, errp)) { +return -EINVAL ; + }
At this point bs still had the old permissions, but we don't access them. As we're going in topological order, the parents have already been updated if they were a child covered in bdrv_node_check_perm(), so we're checking the relevant values. Good.
What about the root node? If I understand correctly, the parents of the root nodes wouldn't have been checked in the old code. In the new state, the parent BdrvChild already has to contain the new permission.
In bdrv_refresh_perms(), we already check parent conflicts, so no change for all callers going through it. Good.
bdrv_reopen_multiple() is less obvious. It passes permissions from the BDRVReopenState, without applying the permissions first. Do we check the old parent permissions instead of the new state here?
block.c static int bdrv_check_perm(BlockDriverState *bs, BlockReopenQueue *q, · line 2229
Quoted 11 lines
block.c +6
-4+bdrv_get_cumulative_perm (bs, &cumulative_perms, + &cumulative_shared_perms); + } - cur_ignore_children =g_slist_prepend (g_slist_copy (ignore_children), c); - ret =bdrv_check_update_perm (c->bs, q, cur_perm, cur_shared, - cur_ignore_children, errp); -g_slist_free (cur_ignore_children); + ret =bdrv_node_check_perm (bs, q, cumulative_perms, + cumulative_shared_perms, + ignore_children, errp);
We use the original ignore_children for every node in the sorted list. The old code extends it with all nodes in the path to each node.
For the bdrv_check_update_perm() call that is now replaced with bdrv_check_parents_compliance(), I think this was necessary because bdrv_check_update_perm() always assumes adding a new edge, so if you update one instead of adding it, you have to ignore it so that it can't conflict with itself. This isn't necessary any more now because we just update and then check for consistency.
For passing to bdrv_node_check_perm() it doesn't make a difference anyway because the parameter is now unused (and should probably be removed).
block.c static int bdrv_check_perm(BlockDriverState *bs, BlockReopenQueue *q, · line 2235
Quoted 8 lines
block.c +0
-2if (ret <0 ) {return ret; } - -bdrv_child_set_perm_safe (c, cur_perm, cur_shared,NULL ); }return 0 ;
A tricky patch to understand, but I think it's right for the most part.
Kevin
Re: [PATCH v2 15/36] block: use topological sort for permission update
Quoted 21 lines · Kevin Wolf
Am 27.11.2020 um 15:45 hat Vladimir Sementsov-Ogievskiy geschrieben:
Quoted 15 lines
Rewrite
bdrv_check_perm(),bdrv_abort_perm_update()andbdrv_set_perm()to update nodes in topological sort order instead of simple DFS. With topologically sorted nodes, we update a node only when all its parents already updated. With DFS it's not so.Consider the following example:
A -+ | | | v | B | | v | C<-+Quoted 4 lines
A is parent for B and C, B is parent for C.
Obviously, to update permissions, we should go in order A B C, so, when we update C, all parent permissions already updated.
Quoted 9 lines
I wondered for a moment why this order is obvious. Taking a permission on A may mean that we need to take the permisson on C, too.
The answer is (or so I think) that the whole operation is atomic so the half-updated state will never be visible to a caller, but this is about calculating the right permissions. Permissions a node needs on its children may depend on what its parents requested, but parent permissions never depend on what children request.
yes, that's about these relations
Quoted 69 lines
Quoted 45 lines
But with current
approach (simple recursion) we can update in sequence A C B C (C is updated twice). On first update of C, we consider old B permissions, so doing wrong thing. If it succeed, all is OK, on second C update we will finish with correct graph. But if the wrong thing failed, we break the whole process for no reason (it's possible that updated B permission will be less strict, but we will never check it).Also new approach gives a way to simultaneously and correctly update several nodes, we just need to run
bdrv_topological_dfs()several times to add all nodes and their subtrees into one topologically sorted list (next patch will updatebdrv_replace_node()in this manner).Test
test_parallel_perm_update()is now passing, so move it out of debugging "if".We also need to support
ignore_childreninbdrv_check_parents_compliance().For test 283 order of parents compliance check is changed.
Signed-off-by Vladimir Sementsov-Ogievskiy <[email protected]>--- block.c | 103 +++++++++++++++++++++++++++++------- tests/test-bdrv-graph-mod.c | 4 +- tests/qemu-iotests/283.out | 2 +- 3 files changed, 86 insertions(+), 23 deletions(-)block.c +3
-1@@ -1994,7 +1994,9 @@ static bool bdrv_a_allow_b(BdrvChild *a, BdrvChild *b, Error **errp)return false; } -static bool bdrv_check_parents_compliance (BlockDriverState *bs,Error **errp) +static bool bdrv_check_parents_compliance (BlockDriverState *bs, +GSList *ignore_children, +Error **errp) {BdrvChild *a, *b;block.c static bool bdrv_check_parents_compliance(BlockDriverState *bs, Error **errp) · line 2011
Quoted 6 lines
block.c +2
-1@@ -2005,7 +2007,9 @@ static bool bdrv_check_parents_compliance(BlockDriverState *bs, Error **errp)*/ QLIST_FOREACH(a, &bs->parents, next_parent) { QLIST_FOREACH(b, &bs->parents, next_parent) { -if (a == b) { +if (a == b ||g_slist_find (ignore_children, a) || +g_slist_find (ignore_children, b))'a' should be checked in the outer loop, no reason to repeat the same check all the time in the inner loop.
block.c static void bdrv_child_perm(BlockDriverState *bs, BlockDriverState *child_bs, · line 2042
Quoted 10 lines
block.c +3
-0+ {continue ; }@@ -2034,6 +2038,29 @@ static void bdrv_child_perm(BlockDriverState *bs, BlockDriverState *child_bs, } } +static GSList *bdrv_topological_dfs (GSList *list,GHashTable *found, +BlockDriverState *bs)It would be good to have a comment that explains the details of the contract.
Quoted 3 lines
In particular, this seems to require that @list is already topologically sorted, and it's complete in the sense that if a node is in the list, all of its children are in the list, too.
Right, will add
Quoted 38 lines
Quoted 26 lines
block.c +21
-0+{ +BdrvChild *child; +g_autoptr (GHashTable) local_found =NULL ; + +if (!found) { +assert (!list); + found = local_found =g_hash_table_new (NULL ,NULL ); + } + +if (g_hash_table_contains (found, bs)) { +return list; + } +g_hash_table_add (found, bs); + + QLIST_FOREACH(child, &bs->children, next) { + list =bdrv_topological_dfs (list, found, child->bs); + } + +return g_slist_prepend (list, bs); +} +static void bdrv_child_set_perm_commit (void *opaque) {BdrvChild *c = opaque;block.c static void bdrv_child_set_perm_safe(BdrvChild *c, uint64_t perm, · line 2127
Quoted 3 lines
block.c +0
-0@@ -2098,10 +2125,10 @@ static void bdrv_child_set_perm_safe(BdrvChild *c, uint64_t perm,* A call to this function must always be followed by a call to bdrv_set_perm() * or bdrv_abort_perm_update(). */ One big source of confusion for me when trying to understand this was that
bdrv_check_perm()is a misnomer since commitf962e96150eand the above comment isn't really accurate any more.The function doesn't only check the validity of the new permissions in advance to actually making the change, but it already updates the permissions of all child nodes (however not of its root node).
Quoted 6 lines
So we have gone from the original check/set/abort model (which the function names still suggest) to a prepare/commit/rollback model.
I think some comment updates are in order, and possibly we should rename some functions, too.
In the end of the series they are refactored and renamed to be native part of new transaction system (introduced in [10])
Quoted 17 lines
Quoted 17 lines
block.c +4
-5-static int bdrv_check_perm (BlockDriverState *bs,BlockReopenQueue *q, -uint64_t cumulative_perms, -uint64_t cumulative_shared_perms, -GSList *ignore_children,Error **errp) +static int bdrv_node_check_perm (BlockDriverState *bs,BlockReopenQueue *q, +uint64_t cumulative_perms, +uint64_t cumulative_shared_perms, +GSList *ignore_children,Error **errp) {BlockDriver *drv = bs->drv;BdrvChild *c;@@ -2166,21 +2193,43 @@ static int bdrv_check_perm(BlockDriverState *bs, BlockReopenQueue *q,/* Check all children */ QLIST_FOREACH(c, &bs->children, next) {uint64_t cur_perm, cur_shared; -GSList *cur_ignore_children;
Quoted 8 lines
block.c static int bdrv_check_perm(BlockDriverState *bs, BlockReopenQueue *q, · line 2200
Quoted 4 lines
block.c +1
-0bdrv_child_perm (bs, c->bs, c, c->role, q, cumulative_perms, cumulative_shared_perms, &cur_perm, &cur_shared); +bdrv_child_set_perm_safe (c, cur_perm, cur_shared,NULL );This "added" line is actually old code. What is removed here is the recursive call of
bdrv_check_update_perm(). This is what the code below will have to replace.
yes, we'll use explicit loop instead of recursion
Quoted 32 lines
Quoted 12 lines
block.c +11
-0+ } + +return 0 ; +} + +static int bdrv_check_perm (BlockDriverState *bs,BlockReopenQueue *q, +uint64_t cumulative_perms, +uint64_t cumulative_shared_perms, +GSList *ignore_children,Error **errp) +{ +int ret;block.c static int bdrv_check_perm(BlockDriverState *bs, BlockReopenQueue *q, · line 2221
Quoted 10 lines
block.c +10
-0+BlockDriverState *root = bs; +g_autoptr (GSList) list =bdrv_topological_dfs (NULL ,NULL , root); + +for ( ; list; list = list->next) { + bs = list->data; + +if (bs != root) { +if (!bdrv_check_parents_compliance (bs, ignore_children, errp)) { +return -EINVAL ; + }At this point bs still had the old permissions, but we don't access them. As we're going in topological order, the parents have already been updated if they were a child covered in
bdrv_node_check_perm(), so we're checking the relevant values. Good.What about the root node? If I understand correctly, the parents of the root nodes wouldn't have been checked in the old code. In the new state, the parent BdrvChild already has to contain the new permission.
Quoted 5 lines
In
bdrv_refresh_perms(), we already check parent conflicts, so no change for all callers going through it. Good.
bdrv_reopen_multiple()is less obvious. It passes permissions from the BDRVReopenState, without applying the permissions first.
It will be changed in the series
Quoted 2 lines
Do we check the
old parent permissions instead of the new state here?
We use given (new) cumulative permissions for bs, and recalculate permissions for bs subtree.
It follows old behavior. The only thing is changed that pre-patch we do DFS recursion starting from bs (and probably visit some nodes several times), after-patch we first do topological sort of bs subtree and go through the list. The order of nodes is better and we visit each node once.
Quoted 23 lines
block.c static int bdrv_check_perm(BlockDriverState *bs, BlockReopenQueue *q, · line 2229
Quoted 12 lines
block.c +6
-4+bdrv_get_cumulative_perm (bs, &cumulative_perms, + &cumulative_shared_perms); + } - cur_ignore_children =g_slist_prepend (g_slist_copy (ignore_children), c); - ret =bdrv_check_update_perm (c->bs, q, cur_perm, cur_shared, - cur_ignore_children, errp); -g_slist_free (cur_ignore_children); + ret =bdrv_node_check_perm (bs, q, cumulative_perms, + cumulative_shared_perms, + ignore_children, errp);We use the original
ignore_childrenfor every node in the sorted list. The old code extends it with all nodes in the path to each node.For the
bdrv_check_update_perm()call that is now replaced withbdrv_check_parents_compliance(), I think this was necessary becausebdrv_check_update_perm()always assumes adding a new edge, so if you update one instead of adding it, you have to ignore it so that it can't conflict with itself. This isn't necessary any more now because we just update and then check for consistency.
Quoted 3 lines
For passing to
bdrv_node_check_perm()it doesn't make a difference anyway because the parameter is now unused (and should probably be removed).
ignore_children will be dropped in [27]. For now it is still needed for bdrv_replace_node_common
Quoted 14 lines
block.c static int bdrv_check_perm(BlockDriverState *bs, BlockReopenQueue *q, · line 2235
Quoted 9 lines
block.c +0
-2if (ret <0 ) {return ret; } - -bdrv_child_set_perm_safe (c, cur_perm, cur_shared,NULL ); }return 0 ;A tricky patch to understand, but I think it's right for the most part.
Kevin
Signature · 2 lines
Best regards, Vladimir
Re: [PATCH v2 15/36] block: use topological sort for permission update
Am 28.01.2021 um 10:34 hat Vladimir Sementsov-Ogievskiy geschrieben:
Quoted 67 lines
Quoted 17 lines · Kevin Wolf
Am 27.11.2020 um 15:45 hat Vladimir Sementsov-Ogievskiy geschrieben:
Quoted 16 lines
block.c +4
-5-static int bdrv_check_perm (BlockDriverState *bs,BlockReopenQueue *q, -uint64_t cumulative_perms, -uint64_t cumulative_shared_perms, -GSList *ignore_children,Error **errp) +static int bdrv_node_check_perm (BlockDriverState *bs,BlockReopenQueue *q, +uint64_t cumulative_perms, +uint64_t cumulative_shared_perms, +GSList *ignore_children,Error **errp) {BlockDriver *drv = bs->drv;BdrvChild *c;@@ -2166,21 +2193,43 @@ static int bdrv_check_perm(BlockDriverState *bs, BlockReopenQueue *q,/* Check all children */ QLIST_FOREACH(c, &bs->children, next) {uint64_t cur_perm, cur_shared; -GSList *cur_ignore_children;Quoted 8 lines
block.c static int bdrv_check_perm(BlockDriverState *bs, BlockReopenQueue *q, · line 2200
Quoted 4 lines
block.c +1
-0bdrv_child_perm (bs, c->bs, c, c->role, q, cumulative_perms, cumulative_shared_perms, &cur_perm, &cur_shared); +bdrv_child_set_perm_safe (c, cur_perm, cur_shared,NULL );This "added" line is actually old code. What is removed here is the recursive call of
bdrv_check_update_perm(). This is what the code below will have to replace.yes, we'll use explicit loop instead of recursion
Quoted 32 lines
Quoted 12 lines
block.c +11
-0+ } + +return 0 ; +} + +static int bdrv_check_perm (BlockDriverState *bs,BlockReopenQueue *q, +uint64_t cumulative_perms, +uint64_t cumulative_shared_perms, +GSList *ignore_children,Error **errp) +{ +int ret;block.c static int bdrv_check_perm(BlockDriverState *bs, BlockReopenQueue *q, · line 2221
Quoted 10 lines
block.c +10
-0+BlockDriverState *root = bs; +g_autoptr (GSList) list =bdrv_topological_dfs (NULL ,NULL , root); + +for ( ; list; list = list->next) { + bs = list->data; + +if (bs != root) { +if (!bdrv_check_parents_compliance (bs, ignore_children, errp)) { +return -EINVAL ; + }At this point bs still had the old permissions, but we don't access them. As we're going in topological order, the parents have already been updated if they were a child covered in
bdrv_node_check_perm(), so we're checking the relevant values. Good.What about the root node? If I understand correctly, the parents of the root nodes wouldn't have been checked in the old code. In the new state, the parent BdrvChild already has to contain the new permission.
Quoted 5 lines
In
bdrv_refresh_perms(), we already check parent conflicts, so no change for all callers going through it. Good.
bdrv_reopen_multiple()is less obvious. It passes permissions from the BDRVReopenState, without applying the permissions first.
Quoted 7 lines
It will be changed in the series
Quoted 2 lines
Do we check the
old parent permissions instead of the new state here?We use given (new) cumulative permissions for bs, and recalculate permissions for bs subtree.
Where do we actually set them? I would expect a bdrv_child_set_perm_safe() call somewhere, but I can't see it in the call path from bdrv_reopen_multiple().
Quoted 5 lines
It follows old behavior. The only thing is changed that pre-patch we do DFS recursion starting from bs (and probably visit some nodes several times), after-patch we first do topological sort of bs subtree and go through the list. The order of nodes is better and we visit each node once.
It's not the only thing that changes. Maybe this is what makes the patch hard to understand, because it seems to do two steps at once:
1. Change the order in which nodes are processed
2. Replace bdrv_check_update_perm() with bdrv_check_parents_compliance()
In step 2, the point I mentioned above is important (new permissions must already be set in the BdrvChild objects).
The switch to bdrv_check_parents_compliance() also means that error messages become a bit worse because we don't know any more which of the conflicting nodes is the new one, so we can't provide two different messages any more. This is probably unavoidable, though.
Quoted 22 lines
Quoted 22 lines
block.c static int bdrv_check_perm(BlockDriverState *bs, BlockReopenQueue *q, · line 2229
Quoted 11 lines
block.c +6
-4+bdrv_get_cumulative_perm (bs, &cumulative_perms, + &cumulative_shared_perms); + } - cur_ignore_children =g_slist_prepend (g_slist_copy (ignore_children), c); - ret =bdrv_check_update_perm (c->bs, q, cur_perm, cur_shared, - cur_ignore_children, errp); -g_slist_free (cur_ignore_children); + ret =bdrv_node_check_perm (bs, q, cumulative_perms, + cumulative_shared_perms, + ignore_children, errp);We use the original
ignore_childrenfor every node in the sorted list. The old code extends it with all nodes in the path to each node.For the
bdrv_check_update_perm()call that is now replaced withbdrv_check_parents_compliance(), I think this was necessary becausebdrv_check_update_perm()always assumes adding a new edge, so if you update one instead of adding it, you have to ignore it so that it can't conflict with itself. This isn't necessary any more now because we just update and then check for consistency.
Quoted 6 lines
Quoted 3 lines
For passing to
bdrv_node_check_perm()it doesn't make a difference anyway because the parameter is now unused (and should probably be removed).
ignore_childrenwill be dropped in [27]. For now it is still needed forbdrv_replace_node_common
In bdrv_node_check_perm(), it's already unused after this patch. But fair enough.
Kevin
Re: [PATCH v2 15/36] block: use topological sort for permission update
Quoted 73 lines · Kevin Wolf
Am 28.01.2021 um 10:34 hat Vladimir Sementsov-Ogievskiy geschrieben:
Quoted 72 lines
Quoted 17 lines · Kevin Wolf
Am 27.11.2020 um 15:45 hat Vladimir Sementsov-Ogievskiy geschrieben:
Quoted 16 lines
block.c +4
-5-static int bdrv_check_perm (BlockDriverState *bs,BlockReopenQueue *q, -uint64_t cumulative_perms, -uint64_t cumulative_shared_perms, -GSList *ignore_children,Error **errp) +static int bdrv_node_check_perm (BlockDriverState *bs,BlockReopenQueue *q, +uint64_t cumulative_perms, +uint64_t cumulative_shared_perms, +GSList *ignore_children,Error **errp) {BlockDriver *drv = bs->drv;BdrvChild *c;@@ -2166,21 +2193,43 @@ static int bdrv_check_perm(BlockDriverState *bs, BlockReopenQueue *q,/* Check all children */ QLIST_FOREACH(c, &bs->children, next) {uint64_t cur_perm, cur_shared; -GSList *cur_ignore_children;Quoted 8 lines
block.c static int bdrv_check_perm(BlockDriverState *bs, BlockReopenQueue *q, · line 2200
Quoted 4 lines
block.c +1
-0bdrv_child_perm (bs, c->bs, c, c->role, q, cumulative_perms, cumulative_shared_perms, &cur_perm, &cur_shared); +bdrv_child_set_perm_safe (c, cur_perm, cur_shared,NULL );This "added" line is actually old code. What is removed here is the recursive call of
bdrv_check_update_perm(). This is what the code below will have to replace.yes, we'll use explicit loop instead of recursion
Quoted 32 lines
Quoted 12 lines
block.c +11
-0+ } + +return 0 ; +} + +static int bdrv_check_perm (BlockDriverState *bs,BlockReopenQueue *q, +uint64_t cumulative_perms, +uint64_t cumulative_shared_perms, +GSList *ignore_children,Error **errp) +{ +int ret;block.c static int bdrv_check_perm(BlockDriverState *bs, BlockReopenQueue *q, · line 2221
Quoted 10 lines
block.c +10
-0+BlockDriverState *root = bs; +g_autoptr (GSList) list =bdrv_topological_dfs (NULL ,NULL , root); + +for ( ; list; list = list->next) { + bs = list->data; + +if (bs != root) { +if (!bdrv_check_parents_compliance (bs, ignore_children, errp)) { +return -EINVAL ; + }At this point bs still had the old permissions, but we don't access them. As we're going in topological order, the parents have already been updated if they were a child covered in
bdrv_node_check_perm(), so we're checking the relevant values. Good.What about the root node? If I understand correctly, the parents of the root nodes wouldn't have been checked in the old code. In the new state, the parent BdrvChild already has to contain the new permission.
Quoted 5 lines
In
bdrv_refresh_perms(), we already check parent conflicts, so no change for all callers going through it. Good.
bdrv_reopen_multiple()is less obvious. It passes permissions from the BDRVReopenState, without applying the permissions first.It will be changed in the series
Quoted 2 lines
Do we check the
old parent permissions instead of the new state here?
Quoted 6 lines
Quoted 2 lines
We use given (new) cumulative permissions for bs, and recalculate permissions for bs subtree.
Where do we actually set them? I would expect a
bdrv_child_set_perm_safe()call somewhere, but I can't see it in the call path frombdrv_reopen_multiple().
You mean parent BdrvChild objects? Then this question applies as well to pre-patch code.
So, we just call bdrv_check_perm() for bs in bdrv_reopen_multiple.. I think the answer is like this:
if state->perm and state->shared_perm are different from actual cumulative permissions (before reopne), then we must have the parent(s) of the node in same bs_queue. Then, corresponding children are updated as part of another bdrv_check_perm call from same loop in bdrv_reopen_multiple().
Let's check how state->perm and state->shared_perm are set:
bdrv_reopen_queue_child()
/* This needs to be overwritten in bdrv_reopen_prepare() */
bs_entry->state.perm = UINT64_MAX;
bs_entry->state.shared_perm = 0;...
bdrv_reopen_prepare()
bdrv_reopen_perm(queue, reopen_state->bs,
&reopen_state->perm, &reopen_state->shared_perm);and bdrv_reopen_perm() calculate cumulative permissions, taking permissions from the queue, for parents which exists in queue.
Not sure how much it correct, keeping in mind that we may look at a node in queue, for which bdrv_reopen_perm was not yet called, but the idea is clean.
Quoted 7 lines
Quoted 6 lines
It follows old behavior. The only thing is changed that pre-patch we do DFS recursion starting from bs (and probably visit some nodes several times), after-patch we first do topological sort of bs subtree and go through the list. The order of nodes is better and we visit each node once.
Quoted 6 lines
It's not the only thing that changes. Maybe this is what makes the patch hard to understand, because it seems to do two steps at once:
1. Change the order in which nodes are processed
2. Replace
bdrv_check_update_perm()withbdrv_check_parents_compliance()
hmm, yes. But we do bdrv_check_parents_compliance() only for nodes inside subtree, for all except root. So, for them we have updated permissions.
Quoted 43 lines
In step 2, the point I mentioned above is important (new permissions must already be set in the BdrvChild objects).
The switch to
bdrv_check_parents_compliance()also means that error messages become a bit worse because we don't know any more which of the conflicting nodes is the new one, so we can't provide two different messages any more. This is probably unavoidable, though.Quoted 22 lines
Quoted 22 lines
block.c static int bdrv_check_perm(BlockDriverState *bs, BlockReopenQueue *q, · line 2229
Quoted 11 lines
block.c +6
-4+bdrv_get_cumulative_perm (bs, &cumulative_perms, + &cumulative_shared_perms); + } - cur_ignore_children =g_slist_prepend (g_slist_copy (ignore_children), c); - ret =bdrv_check_update_perm (c->bs, q, cur_perm, cur_shared, - cur_ignore_children, errp); -g_slist_free (cur_ignore_children); + ret =bdrv_node_check_perm (bs, q, cumulative_perms, + cumulative_shared_perms, + ignore_children, errp);We use the original
ignore_childrenfor every node in the sorted list. The old code extends it with all nodes in the path to each node.For the
bdrv_check_update_perm()call that is now replaced withbdrv_check_parents_compliance(), I think this was necessary becausebdrv_check_update_perm()always assumes adding a new edge, so if you update one instead of adding it, you have to ignore it so that it can't conflict with itself. This isn't necessary any more now because we just update and then check for consistency.Quoted 6 lines
Quoted 3 lines
For passing to
bdrv_node_check_perm()it doesn't make a difference anyway because the parameter is now unused (and should probably be removed).
ignore_childrenwill be dropped in [27]. For now it is still needed forbdrv_replace_node_commonIn
bdrv_node_check_perm(), it's already unused after this patch. But fair enough.Kevin
Signature · 2 lines
Best regards, Vladimir
Re: [PATCH v2 15/36] block: use topological sort for permission update
Am 28.01.2021 um 19:04 hat Vladimir Sementsov-Ogievskiy geschrieben:
Quoted 77 lines
Quoted 76 lines · Kevin Wolf
Am 28.01.2021 um 10:34 hat Vladimir Sementsov-Ogievskiy geschrieben:
Quoted 74 lines
Quoted 17 lines · Kevin Wolf
Am 27.11.2020 um 15:45 hat Vladimir Sementsov-Ogievskiy geschrieben:
Quoted 16 lines
block.c +4
-5-static int bdrv_check_perm (BlockDriverState *bs,BlockReopenQueue *q, -uint64_t cumulative_perms, -uint64_t cumulative_shared_perms, -GSList *ignore_children,Error **errp) +static int bdrv_node_check_perm (BlockDriverState *bs,BlockReopenQueue *q, +uint64_t cumulative_perms, +uint64_t cumulative_shared_perms, +GSList *ignore_children,Error **errp) {BlockDriver *drv = bs->drv;BdrvChild *c;@@ -2166,21 +2193,43 @@ static int bdrv_check_perm(BlockDriverState *bs, BlockReopenQueue *q,/* Check all children */ QLIST_FOREACH(c, &bs->children, next) {uint64_t cur_perm, cur_shared; -GSList *cur_ignore_children;Quoted 8 lines
block.c static int bdrv_check_perm(BlockDriverState *bs, BlockReopenQueue *q, · line 2200
Quoted 4 lines
block.c +1
-0bdrv_child_perm (bs, c->bs, c, c->role, q, cumulative_perms, cumulative_shared_perms, &cur_perm, &cur_shared); +bdrv_child_set_perm_safe (c, cur_perm, cur_shared,NULL );This "added" line is actually old code. What is removed here is the recursive call of
bdrv_check_update_perm(). This is what the code below will have to replace.yes, we'll use explicit loop instead of recursion
Quoted 32 lines
Quoted 12 lines
block.c +11
-0+ } + +return 0 ; +} + +static int bdrv_check_perm (BlockDriverState *bs,BlockReopenQueue *q, +uint64_t cumulative_perms, +uint64_t cumulative_shared_perms, +GSList *ignore_children,Error **errp) +{ +int ret;block.c static int bdrv_check_perm(BlockDriverState *bs, BlockReopenQueue *q, · line 2221
Quoted 10 lines
block.c +10
-0+BlockDriverState *root = bs; +g_autoptr (GSList) list =bdrv_topological_dfs (NULL ,NULL , root); + +for ( ; list; list = list->next) { + bs = list->data; + +if (bs != root) { +if (!bdrv_check_parents_compliance (bs, ignore_children, errp)) { +return -EINVAL ; + }At this point bs still had the old permissions, but we don't access them. As we're going in topological order, the parents have already been updated if they were a child covered in
bdrv_node_check_perm(), so we're checking the relevant values. Good.What about the root node? If I understand correctly, the parents of the root nodes wouldn't have been checked in the old code. In the new state, the parent BdrvChild already has to contain the new permission.
Quoted 5 lines
In
bdrv_refresh_perms(), we already check parent conflicts, so no change for all callers going through it. Good.
bdrv_reopen_multiple()is less obvious. It passes permissions from the BDRVReopenState, without applying the permissions first.It will be changed in the series
Quoted 2 lines
Do we check the
old parent permissions instead of the new state here?We use given (new) cumulative permissions for bs, and recalculate permissions for bs subtree.
Quoted 6 lines
Quoted 3 lines
Where do we actually set them? I would expect a
bdrv_child_set_perm_safe()call somewhere, but I can't see it in the call path frombdrv_reopen_multiple().You mean parent BdrvChild objects? Then this question applies as well to pre-patch code.
I don't think so. The pre-patch code doesn't rely on the permissions already being set in the BdrvChild object, but it gets them passed in parameters. Changing the graph first and relying on the information in BdrvChild is the new approach that you're introducing.
Quoted 18 lines
So, we just call
bdrv_check_perm()for bs inbdrv_reopen_multiple.. I think the answer is like this:if
state->permandstate->shared_permare different from actual cumulative permissions (before reopne), then we must have the parent(s) of the node in samebs_queue. Then, corresponding children are updated as part of anotherbdrv_check_permcall from same loop inbdrv_reopen_multiple().Let's check how
state->permandstate->shared_permare set:
bdrv_reopen_queue_child()/* This needs to be overwritten in bdrv_reopen_prepare() */ bs_entry->state.perm = UINT64_MAX; bs_entry->state.shared_perm = 0;
Quoted 8 lines
...
bdrv_reopen_prepare()bdrv_reopen_perm(queue, reopen_state->bs, &reopen_state->perm, &reopen_state->shared_perm);and
bdrv_reopen_perm()calculate cumulative permissions, taking permissions from the queue, for parents which exists in queue.
Right, but it stores the new permissions in reopen_state, not in the BdrvChild objects that this patch is looking it. Or am I missing something?
Quoted 3 lines
Not sure how much it correct, keeping in mind that we may look at a node in queue, for which
bdrv_reopen_permwas not yet called, but the idea is clean.
I don't think the above code can work correctly without something actually updating the BdrvChild first.
Quoted 9 lines
Quoted 9 lines
Quoted 5 lines
It follows old behavior. The only thing is changed that pre-patch we do DFS recursion starting from bs (and probably visit some nodes several times), after-patch we first do topological sort of bs subtree and go through the list. The order of nodes is better and we visit each node once.
It's not the only thing that changes. Maybe this is what makes the patch hard to understand, because it seems to do two steps at once:
Quoted 7 lines
Quoted 3 lines
1. Change the order in which nodes are processed
2. Replace
bdrv_check_update_perm()withbdrv_check_parents_compliance()hmm, yes. But we do
bdrv_check_parents_compliance()only for nodes inside subtree, for all except root. So, for them we have updated permissions.
Ah! This might be the missing piece that makes it safe.
Maybe worth a comment?
Kevin
Re: [PATCH v2 15/36] block: use topological sort for permission update
Quoted 141 lines · Kevin Wolf
Am 28.01.2021 um 19:04 hat Vladimir Sementsov-Ogievskiy geschrieben:
Quoted 77 lines
Quoted 76 lines · Kevin Wolf
Am 28.01.2021 um 10:34 hat Vladimir Sementsov-Ogievskiy geschrieben:
Quoted 74 lines
Quoted 17 lines · Kevin Wolf
Am 27.11.2020 um 15:45 hat Vladimir Sementsov-Ogievskiy geschrieben:
Quoted 16 lines
block.c +4
-5-static int bdrv_check_perm (BlockDriverState *bs,BlockReopenQueue *q, -uint64_t cumulative_perms, -uint64_t cumulative_shared_perms, -GSList *ignore_children,Error **errp) +static int bdrv_node_check_perm (BlockDriverState *bs,BlockReopenQueue *q, +uint64_t cumulative_perms, +uint64_t cumulative_shared_perms, +GSList *ignore_children,Error **errp) {BlockDriver *drv = bs->drv;BdrvChild *c;@@ -2166,21 +2193,43 @@ static int bdrv_check_perm(BlockDriverState *bs, BlockReopenQueue *q,/* Check all children */ QLIST_FOREACH(c, &bs->children, next) {uint64_t cur_perm, cur_shared; -GSList *cur_ignore_children;Quoted 8 lines
block.c static int bdrv_check_perm(BlockDriverState *bs, BlockReopenQueue *q, · line 2200
Quoted 4 lines
block.c +1
-0bdrv_child_perm (bs, c->bs, c, c->role, q, cumulative_perms, cumulative_shared_perms, &cur_perm, &cur_shared); +bdrv_child_set_perm_safe (c, cur_perm, cur_shared,NULL );This "added" line is actually old code. What is removed here is the recursive call of
bdrv_check_update_perm(). This is what the code below will have to replace.yes, we'll use explicit loop instead of recursion
Quoted 32 lines
Quoted 12 lines
block.c +11
-0+ } + +return 0 ; +} + +static int bdrv_check_perm (BlockDriverState *bs,BlockReopenQueue *q, +uint64_t cumulative_perms, +uint64_t cumulative_shared_perms, +GSList *ignore_children,Error **errp) +{ +int ret;block.c static int bdrv_check_perm(BlockDriverState *bs, BlockReopenQueue *q, · line 2221
Quoted 10 lines
block.c +10
-0+BlockDriverState *root = bs; +g_autoptr (GSList) list =bdrv_topological_dfs (NULL ,NULL , root); + +for ( ; list; list = list->next) { + bs = list->data; + +if (bs != root) { +if (!bdrv_check_parents_compliance (bs, ignore_children, errp)) { +return -EINVAL ; + }At this point bs still had the old permissions, but we don't access them. As we're going in topological order, the parents have already been updated if they were a child covered in
bdrv_node_check_perm(), so we're checking the relevant values. Good.What about the root node? If I understand correctly, the parents of the root nodes wouldn't have been checked in the old code. In the new state, the parent BdrvChild already has to contain the new permission.
Quoted 5 lines
In
bdrv_refresh_perms(), we already check parent conflicts, so no change for all callers going through it. Good.
bdrv_reopen_multiple()is less obvious. It passes permissions from the BDRVReopenState, without applying the permissions first.It will be changed in the series
Quoted 2 lines
Do we check the
old parent permissions instead of the new state here?We use given (new) cumulative permissions for bs, and recalculate permissions for bs subtree.
Quoted 6 lines
Quoted 3 lines
Where do we actually set them? I would expect a
bdrv_child_set_perm_safe()call somewhere, but I can't see it in the call path frombdrv_reopen_multiple().You mean parent BdrvChild objects? Then this question applies as well to pre-patch code.
I don't think so. The pre-patch code doesn't rely on the permissions already being set in the BdrvChild object, but it gets them passed in parameters. Changing the graph first and relying on the information in BdrvChild is the new approach that you're introducing.
Quoted 18 lines
So, we just call
bdrv_check_perm()for bs inbdrv_reopen_multiple.. I think the answer is like this:if
state->permandstate->shared_permare different from actual cumulative permissions (before reopne), then we must have the parent(s) of the node in samebs_queue. Then, corresponding children are updated as part of anotherbdrv_check_permcall from same loop inbdrv_reopen_multiple().Let's check how
state->permandstate->shared_permare set:
bdrv_reopen_queue_child()/* This needs to be overwritten in bdrv_reopen_prepare() */ bs_entry->state.perm = UINT64_MAX; bs_entry->state.shared_perm = 0;Quoted 8 lines
...
bdrv_reopen_prepare()bdrv_reopen_perm(queue, reopen_state->bs, &reopen_state->perm, &reopen_state->shared_perm);and
bdrv_reopen_perm()calculate cumulative permissions, taking permissions from the queue, for parents which exists in queue.Right, but it stores the new permissions in
reopen_state, not in the BdrvChild objects that this patch is looking it. Or am I missing something?Quoted 3 lines
Not sure how much it correct, keeping in mind that we may look at a node in queue, for which
bdrv_reopen_permwas not yet called, but the idea is clean.I don't think the above code can work correctly without something actually updating the BdrvChild first.
Quoted 13 lines
Quoted 12 lines
Quoted 5 lines
It follows old behavior. The only thing is changed that pre-patch we do DFS recursion starting from bs (and probably visit some nodes several times), after-patch we first do topological sort of bs subtree and go through the list. The order of nodes is better and we visit each node once.
It's not the only thing that changes. Maybe this is what makes the patch hard to understand, because it seems to do two steps at once:
1. Change the order in which nodes are processed
2. Replace
bdrv_check_update_perm()withbdrv_check_parents_compliance()
Quoted 8 lines
Quoted 3 lines
hmm, yes. But we do
bdrv_check_parents_compliance()only for nodes inside subtree, for all except root. So, for them we have updated permissions.Ah! This might be the missing piece that makes it safe.
Maybe worth a comment?
Will add
Signature · 2 lines
Best regards, Vladimir
Re: [PATCH v2 15/36] block: use topological sort for permission update
In reply to Kevin WolfQuoted 82 lines · Kevin Wolf
Am 28.01.2021 um 19:04 hat Vladimir Sementsov-Ogievskiy geschrieben:
Quoted 81 lines
Quoted 79 lines · Kevin Wolf
Am 28.01.2021 um 10:34 hat Vladimir Sementsov-Ogievskiy geschrieben:
Quoted 67 lines
Quoted 17 lines · Kevin Wolf
Am 27.11.2020 um 15:45 hat Vladimir Sementsov-Ogievskiy geschrieben:
Quoted 16 lines
block.c +4
-5-static int bdrv_check_perm (BlockDriverState *bs,BlockReopenQueue *q, -uint64_t cumulative_perms, -uint64_t cumulative_shared_perms, -GSList *ignore_children,Error **errp) +static int bdrv_node_check_perm (BlockDriverState *bs,BlockReopenQueue *q, +uint64_t cumulative_perms, +uint64_t cumulative_shared_perms, +GSList *ignore_children,Error **errp) {BlockDriver *drv = bs->drv;BdrvChild *c;@@ -2166,21 +2193,43 @@ static int bdrv_check_perm(BlockDriverState *bs, BlockReopenQueue *q,/* Check all children */ QLIST_FOREACH(c, &bs->children, next) {uint64_t cur_perm, cur_shared; -GSList *cur_ignore_children;Quoted 8 lines
block.c static int bdrv_check_perm(BlockDriverState *bs, BlockReopenQueue *q, · line 2200
Quoted 4 lines
block.c +1
-0bdrv_child_perm (bs, c->bs, c, c->role, q, cumulative_perms, cumulative_shared_perms, &cur_perm, &cur_shared); +bdrv_child_set_perm_safe (c, cur_perm, cur_shared,NULL );This "added" line is actually old code. What is removed here is the recursive call of
bdrv_check_update_perm(). This is what the code below will have to replace.yes, we'll use explicit loop instead of recursion
Quoted 32 lines
Quoted 12 lines
block.c +11
-0+ } + +return 0 ; +} + +static int bdrv_check_perm (BlockDriverState *bs,BlockReopenQueue *q, +uint64_t cumulative_perms, +uint64_t cumulative_shared_perms, +GSList *ignore_children,Error **errp) +{ +int ret;block.c static int bdrv_check_perm(BlockDriverState *bs, BlockReopenQueue *q, · line 2221
Quoted 10 lines
block.c +10
-0+BlockDriverState *root = bs; +g_autoptr (GSList) list =bdrv_topological_dfs (NULL ,NULL , root); + +for ( ; list; list = list->next) { + bs = list->data; + +if (bs != root) { +if (!bdrv_check_parents_compliance (bs, ignore_children, errp)) { +return -EINVAL ; + }At this point bs still had the old permissions, but we don't access them. As we're going in topological order, the parents have already been updated if they were a child covered in
bdrv_node_check_perm(), so we're checking the relevant values. Good.What about the root node? If I understand correctly, the parents of the root nodes wouldn't have been checked in the old code. In the new state, the parent BdrvChild already has to contain the new permission.
Quoted 5 lines
In
bdrv_refresh_perms(), we already check parent conflicts, so no change for all callers going through it. Good.
bdrv_reopen_multiple()is less obvious. It passes permissions from the BDRVReopenState, without applying the permissions first.Quoted 7 lines
It will be changed in the series
Quoted 2 lines
Do we check the
old parent permissions instead of the new state here?We use given (new) cumulative permissions for bs, and recalculate permissions for bs subtree.
Where do we actually set them? I would expect a
bdrv_child_set_perm_safe()call somewhere, but I can't see it in the call path frombdrv_reopen_multiple().
Quoted 7 lines
Quoted 2 lines
You mean parent BdrvChild objects? Then this question applies as well to pre-patch code.
I don't think so. The pre-patch code doesn't rely on the permissions already being set in the BdrvChild object, but it gets them passed in parameters. Changing the graph first and relying on the information in BdrvChild is the new approach that you're introducing.
New code still pass permissions as parameters for root node. And only inside subtree we rely on updated parents. But that's correct due to topological order of updating.
OK, that's incorrect for the following case: when one of the parents (X) of inner node in bs subtree IS NOT in the bs subtree but IS in reopen queue. And we'll use wrong permission of X. Still:
1. It's preexisting. bdrv_check_update_perm() doesn't take reopen queue in mind when calculate cumulative permissions (and ignore_children doesn't help for the described case
2. We can hope that on next permission update, started from node X, permissions will become more correct
3. At the end of series permission calculation in bdrv_reopen_multiple is rewritten anyway.
Quoted 62 lines
Quoted 19 lines
So, we just call
bdrv_check_perm()for bs inbdrv_reopen_multiple.. I think the answer is like this:if
state->permandstate->shared_permare different from actual cumulative permissions (before reopne), then we must have the parent(s) of the node in samebs_queue. Then, corresponding children are updated as part of anotherbdrv_check_permcall from same loop inbdrv_reopen_multiple().Let's check how
state->permandstate->shared_permare set:
bdrv_reopen_queue_child()/* This needs to be overwritten in bdrv_reopen_prepare() */ bs_entry->state.perm = UINT64_MAX; bs_entry->state.shared_perm = 0;Quoted 8 lines
...
bdrv_reopen_prepare()bdrv_reopen_perm(queue, reopen_state->bs, &reopen_state->perm, &reopen_state->shared_perm);and
bdrv_reopen_perm()calculate cumulative permissions, taking permissions from the queue, for parents which exists in queue.Right, but it stores the new permissions in
reopen_state, not in the BdrvChild objects that this patch is looking it. Or am I missing something?Quoted 3 lines
Not sure how much it correct, keeping in mind that we may look at a node in queue, for which
bdrv_reopen_permwas not yet called, but the idea is clean.I don't think the above code can work correctly without something actually updating the BdrvChild first.
Quoted 9 lines
Quoted 9 lines
Quoted 5 lines
It follows old behavior. The only thing is changed that pre-patch we do DFS recursion starting from bs (and probably visit some nodes several times), after-patch we first do topological sort of bs subtree and go through the list. The order of nodes is better and we visit each node once.
It's not the only thing that changes. Maybe this is what makes the patch hard to understand, because it seems to do two steps at once:
Quoted 7 lines
Quoted 3 lines
1. Change the order in which nodes are processed
2. Replace
bdrv_check_update_perm()withbdrv_check_parents_compliance()hmm, yes. But we do
bdrv_check_parents_compliance()only for nodes inside subtree, for all except root. So, for them we have updated permissions.Ah! This might be the missing piece that makes it safe.
Maybe worth a comment?
Kevin
Signature · 2 lines
Best regards, Vladimir
Re: [PATCH v2 15/36] block: use topological sort for permission update
Am 10.03.2021 um 12:08 hat Vladimir Sementsov-Ogievskiy geschrieben:
Quoted 104 lines
Quoted 82 lines · Kevin Wolf
Am 28.01.2021 um 19:04 hat Vladimir Sementsov-Ogievskiy geschrieben:
Quoted 81 lines
Quoted 79 lines · Kevin Wolf
Am 28.01.2021 um 10:34 hat Vladimir Sementsov-Ogievskiy geschrieben:
Quoted 67 lines
Quoted 17 lines · Kevin Wolf
Am 27.11.2020 um 15:45 hat Vladimir Sementsov-Ogievskiy geschrieben:
Quoted 16 lines
(quoted diff) +4
-5-static int bdrv_check_perm(BlockDriverState *bs, BlockReopenQueue *q, - uint64_t cumulative_perms, - uint64_t cumulative_shared_perms, - GSList *ignore_children, Error **errp) +static int bdrv_node_check_perm(BlockDriverState *bs, BlockReopenQueue *q, + uint64_t cumulative_perms, + uint64_t cumulative_shared_perms, + GSList *ignore_children, Error **errp) { BlockDriver *drv = bs->drv; BdrvChild *c;@@ -2166,21 +2193,43 @@ static int bdrv_check_perm(BlockDriverState *bs, BlockReopenQueue *q, /* Check all children */ QLIST_FOREACH(c, &bs->children, next) { uint64_t cur_perm, cur_shared; - GSList *cur_ignore_children;Quoted 8 lines
Review static int bdrv_check_perm(BlockDriverState *bs, BlockReopenQueue *q, · line 2199
Quoted 4 lines
(quoted diff) +1
-0bdrv_child_perm(bs, c->bs, c, c->role, q, cumulative_perms, cumulative_shared_perms, &cur_perm, &cur_shared); + bdrv_child_set_perm_safe(c, cur_perm, cur_shared, NULL);This "added" line is actually old code. What is removed here is the recursive call of
bdrv_check_update_perm(). This is what the code below will have to replace.yes, we'll use explicit loop instead of recursion
Quoted 32 lines
Quoted 12 lines
(quoted diff) +11
-0+ } + + return 0; +} + +static int bdrv_check_perm(BlockDriverState *bs, BlockReopenQueue *q, + uint64_t cumulative_perms, + uint64_t cumulative_shared_perms, + GSList *ignore_children, Error **errp) +{ + int ret;Review static int bdrv_check_perm(BlockDriverState *bs, BlockReopenQueue *q, · line 2220
Quoted 10 lines
(quoted diff) +10
-0+ BlockDriverState *root = bs; + g_autoptr(GSList) list = bdrv_topological_dfs(NULL, NULL, root); + + for ( ; list; list = list->next) { + bs = list->data; + + if (bs != root) { + if (!bdrv_check_parents_compliance(bs, ignore_children, errp)) { + return -EINVAL; + }At this point bs still had the old permissions, but we don't access them. As we're going in topological order, the parents have already been updated if they were a child covered in
bdrv_node_check_perm(), so we're checking the relevant values. Good.What about the root node? If I understand correctly, the parents of the root nodes wouldn't have been checked in the old code. In the new state, the parent BdrvChild already has to contain the new permission.
Quoted 5 lines
In
bdrv_refresh_perms(), we already check parent conflicts, so no change for all callers going through it. Good.
bdrv_reopen_multiple()is less obvious. It passes permissions from the BDRVReopenState, without applying the permissions first.Quoted 7 lines
It will be changed in the series
Quoted 2 lines
Do we check the
old parent permissions instead of the new state here?We use given (new) cumulative permissions for bs, and recalculate permissions for bs subtree.
Where do we actually set them? I would expect a
bdrv_child_set_perm_safe()call somewhere, but I can't see it in the call path frombdrv_reopen_multiple().Quoted 7 lines
Quoted 2 lines
You mean parent BdrvChild objects? Then this question applies as well to pre-patch code.
I don't think so. The pre-patch code doesn't rely on the permissions already being set in the BdrvChild object, but it gets them passed in parameters. Changing the graph first and relying on the information in BdrvChild is the new approach that you're introducing.
New code still pass permissions as parameters for root node. And only inside subtree we rely on updated parents. But that's correct due to topological order of updating.
OK, that's incorrect for the following case: when one of the parents (X) of inner node in bs subtree IS NOT in the bs subtree but IS in reopen queue. And we'll use wrong permission of X. Still:
1. It's preexisting.
bdrv_check_update_perm()doesn't take reopen queue in mind when calculate cumulative permissions (andignore_childrendoesn't help for the described case
Quoted 5 lines
2. We can hope that on next permission update, started from node X, permissions will become more correct
3. At the end of series permission calculation in
bdrv_reopen_multipleis rewritten anyway.
Yes, I think 3. is the strongest argument in favour of the patch.
Kevin
PATCH v2 16/36 block: add bdrv_drv_set_perm transaction action
In reply to Vladimir Sementsov-OgievskiyRefactor calling driver callbacks to a separate transaction action to be used later.
--- block.c | 70 ++++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 54 insertions(+), 16 deletions(-)
block.c +54 -16
@@ -2116,6 +2116,54 @@ static void bdrv_child_set_perm_safe(BdrvChild *c, uint64_t perm, } } +static void bdrv_drv_set_perm_commit (void *opaque) +{ +BlockDriverState *bs = opaque; +uint64_t cumulative_perms, cumulative_shared_perms; + +if (bs->drv->bdrv_set_perm) { +bdrv_get_cumulative_perm (bs, &cumulative_perms, + &cumulative_shared_perms); + bs->drv->bdrv_set_perm (bs, cumulative_perms, cumulative_shared_perms); + } +} + +static void bdrv_drv_set_perm_abort (void *opaque) +{ +BlockDriverState *bs = opaque; + +if (bs->drv->bdrv_abort_perm_update) { + bs->drv->bdrv_abort_perm_update (bs); + } +} + +TransactionActionDrv bdrv_drv_set_perm_drv = { + .abort = bdrv_drv_set_perm_abort, + .commit = bdrv_drv_set_perm_commit, +}; + +static int bdrv_drv_set_perm (BlockDriverState *bs,uint64_t perm, +uint64_t shared_perm,GSList **tran, +Error **errp) +{ +if (!bs->drv) { +return 0 ; + } + +if (bs->drv->bdrv_check_perm) { +int ret = bs->drv->bdrv_check_perm (bs, perm, shared_perm, errp); +if (ret <0 ) { +return ret; + } + } + +if (tran) { +tran_prepend (tran, &bdrv_drv_set_perm_drv, bs); + } + +return 0 ; +} +/* * Check whether permissions on this node can be changed in a way that * @cumulative_perms and @cumulative_shared_perms are the new cumulative
@@ -2176,12 +2224,10 @@ static int bdrv_node_check_perm(BlockDriverState *bs, BlockReopenQueue *q,return 0 ; } -if (drv->bdrv_check_perm) { - ret = drv->bdrv_check_perm (bs, cumulative_perms, - cumulative_shared_perms, errp); -if (ret <0 ) { -return ret; - } + ret =bdrv_drv_set_perm (bs, cumulative_perms, cumulative_shared_perms,NULL , + errp); +if (ret <0 ) { +return ret; }/* Drivers that never have children can omit .bdrv_child_perm() */
@@ -2249,9 +2295,7 @@ static void bdrv_node_abort_perm_update(BlockDriverState *bs)return ; } -if (drv->bdrv_abort_perm_update) { - drv->bdrv_abort_perm_update (bs); - } +bdrv_drv_set_perm_abort (bs); QLIST_FOREACH(c, &bs->children, next) {bdrv_child_set_perm_abort (c);
@@ -2269,7 +2313,6 @@ static void bdrv_abort_perm_update(BlockDriverState *bs)static void bdrv_node_set_perm (BlockDriverState *bs) { -uint64_t cumulative_perms, cumulative_shared_perms;BlockDriver *drv = bs->drv;BdrvChild *c;
@@ -2277,12 +2320,7 @@ static void bdrv_node_set_perm(BlockDriverState *bs)return ; } -bdrv_get_cumulative_perm (bs, &cumulative_perms, &cumulative_shared_perms); - -/* Update this node */ -if (drv->bdrv_set_perm) { - drv->bdrv_set_perm (bs, cumulative_perms, cumulative_shared_perms); - } +bdrv_drv_set_perm_commit (bs);/* Drivers that never have children can omit .bdrv_child_perm() */ if (!drv->bdrv_child_perm) {
Signature · 1 line
2.21.3
PATCH v2 17/36 block: add bdrv_list_* permission update functions
In reply to Vladimir Sementsov-OgievskiyAdd new interface, allowing use of existing node list. It will be used to fix bdrv_replace_node() in the further commit.
--- block.c | 106 +++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 71 insertions(+), 35 deletions(-)
block.c +71 -35
@@ -2176,7 +2176,8 @@ static int bdrv_drv_set_perm(BlockDriverState *bs, uint64_t perm,static int bdrv_node_check_perm (BlockDriverState *bs,BlockReopenQueue *q,uint64_t cumulative_perms,uint64_t cumulative_shared_perms, -GSList *ignore_children,Error **errp) +GSList *ignore_children, +GSList **tran,Error **errp) {BlockDriver *drv = bs->drv;BdrvChild *c;
@@ -2224,7 +2225,7 @@ static int bdrv_node_check_perm(BlockDriverState *bs, BlockReopenQueue *q,return 0 ; } - ret =bdrv_drv_set_perm (bs, cumulative_perms, cumulative_shared_perms,NULL , + ret =bdrv_drv_set_perm (bs, cumulative_perms, cumulative_shared_perms, tran, errp);if (ret <0 ) {return ret;
@@ -2243,36 +2244,53 @@ static int bdrv_node_check_perm(BlockDriverState *bs, BlockReopenQueue *q,bdrv_child_perm (bs, c->bs, c, c->role, q, cumulative_perms, cumulative_shared_perms, &cur_perm, &cur_shared); -bdrv_child_set_perm_safe (c, cur_perm, cur_shared,NULL ); +bdrv_child_set_perm_safe (c, cur_perm, cur_shared, tran); }return 0 ; } -static int bdrv_check_perm (BlockDriverState *bs,BlockReopenQueue *q, -uint64_t cumulative_perms, -uint64_t cumulative_shared_perms, -GSList *ignore_children,Error **errp) +/* +* If use_cumulative_perms is true, use cumulative_perms and +* cumulative_shared_perms for first element of the list. Otherwise just refresh +* all permissions. +*/ +static int bdrv_check_perm_common (GSList *list,BlockReopenQueue *q, +bool use_cumulative_perms, +uint64_t cumulative_perms, +uint64_t cumulative_shared_perms, +GSList *ignore_children, +GSList **tran,Error **errp) {int ret; -BlockDriverState *root = bs; -g_autoptr (GSList) list =bdrv_topological_dfs (NULL ,NULL , root); +BlockDriverState *bs; -for ( ; list; list = list->next) { +if (use_cumulative_perms) { bs = list->data; -if (bs != root) { -if (!bdrv_check_parents_compliance (bs, ignore_children, errp)) { -return -EINVAL ; - } + ret =bdrv_node_check_perm (bs, q, cumulative_perms, + cumulative_shared_perms, + ignore_children, tran, errp); +if (ret <0 ) { +return ret; + } -bdrv_get_cumulative_perm (bs, &cumulative_perms, - &cumulative_shared_perms); + list = list->next; + } + +for ( ; list; list = list->next) { + bs = list->data; + +if (!bdrv_check_parents_compliance (bs, ignore_children, errp)) { +return -EINVAL ; } +bdrv_get_cumulative_perm (bs, &cumulative_perms, + &cumulative_shared_perms); + ret =bdrv_node_check_perm (bs, q, cumulative_perms, cumulative_shared_perms, - ignore_children, errp); + ignore_children, tran, errp);if (ret <0 ) {return ret; }
@@ -2281,6 +2299,23 @@ static int bdrv_check_perm(BlockDriverState *bs, BlockReopenQueue *q,return 0 ; } +static int bdrv_check_perm (BlockDriverState *bs,BlockReopenQueue *q, +uint64_t cumulative_perms, +uint64_t cumulative_shared_perms, +GSList *ignore_children,Error **errp) +{ +g_autoptr (GSList) list =bdrv_topological_dfs (NULL ,NULL , bs); +return bdrv_check_perm_common (list, q, true, cumulative_perms, + cumulative_shared_perms, ignore_children, +NULL , errp); +} + +static int bdrv_list_refresh_perms (GSList *list,BlockReopenQueue *q, +GSList **tran,Error **errp) +{ +return bdrv_check_perm_common (list, q, false,0 ,0 ,NULL , tran, errp); +} +/* * Notifies drivers that after a previous bdrv_check_perm() call, the * permission update is not performed and any preparations made for it (e.g.
@@ -2302,15 +2337,19 @@ static void bdrv_node_abort_perm_update(BlockDriverState *bs) } } -static void bdrv_abort_perm_update (BlockDriverState *bs) +static void bdrv_list_abort_perm_update (GSList *list) { -g_autoptr (GSList) list =bdrv_topological_dfs (NULL ,NULL , bs); -for ( ; list; list = list->next) {bdrv_node_abort_perm_update ((BlockDriverState *)list->data); } } +static void bdrv_abort_perm_update (BlockDriverState *bs) +{ +g_autoptr (GSList) list =bdrv_topological_dfs (NULL ,NULL , bs); +return bdrv_list_abort_perm_update (list); +} +static void bdrv_node_set_perm (BlockDriverState *bs) {BlockDriver *drv = bs->drv;
@@ -2334,15 +2373,19 @@ static void bdrv_node_set_perm(BlockDriverState *bs) } } -static void bdrv_set_perm (BlockDriverState *bs) +static void bdrv_list_set_perm (GSList *list) { -g_autoptr (GSList) list =bdrv_topological_dfs (NULL ,NULL , bs); -for ( ; list; list = list->next) {bdrv_node_set_perm ((BlockDriverState *)list->data); } } +static void bdrv_set_perm (BlockDriverState *bs) +{ +g_autoptr (GSList) list =bdrv_topological_dfs (NULL ,NULL , bs); +return bdrv_list_set_perm (list); +} +void bdrv_get_cumulative_perm (BlockDriverState *bs,uint64_t *perm,uint64_t *shared_perm) {
@@ -2450,20 +2493,13 @@ static int bdrv_check_update_perm(BlockDriverState *bs, BlockReopenQueue *q,static int bdrv_refresh_perms (BlockDriverState *bs,Error **errp) {int ret; -uint64_t perm, shared_perm; +GSList *tran =NULL ; +g_autoptr (GSList) list =bdrv_topological_dfs (NULL ,NULL , bs); -if (!bdrv_check_parents_compliance (bs,NULL , errp)) { -return -EPERM ; - } -bdrv_get_cumulative_perm (bs, &perm, &shared_perm); - ret =bdrv_check_perm (bs,NULL , perm, shared_perm,NULL , errp); -if (ret <0 ) { -bdrv_abort_perm_update (bs); -return ret; - } -bdrv_set_perm (bs); + ret =bdrv_list_refresh_perms (list,NULL , &tran, errp); +tran_finalize (tran, ret); -return 0 ; +return ret; } int bdrv_child_try_set_perm(BdrvChild *c, uint64_t perm, uint64_t shared,
Signature · 1 line
2.21.3
PATCH v2 18/36 block: add bdrv_replace_child_safe() transaction action
In reply to Vladimir Sementsov-OgievskiyTo be used in the following commit.
--- block.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+)
block.c +53 -0
@@ -84,6 +84,8 @@ static BlockDriverState *bdrv_open_inherit(const char *filename,static void bdrv_parent_set_aio_context_ignore (BdrvChild *c,AioContext *ctx,GSList **ignore); +static void bdrv_replace_child_noperm (BdrvChild *child, +BlockDriverState *new_bs);static int bdrv_reopen_prepare (BDRVReopenState *reopen_state,BlockReopenQueue *queue,Error **errp);
@@ -2164,6 +2166,57 @@ static int bdrv_drv_set_perm(BlockDriverState *bs, uint64_t perm,return 0 ; } +typedef struct BdrvReplaceChildState { +BdrvChild *child; +BlockDriverState *old_bs; +}BdrvReplaceChildState ; + +static void bdrv_replace_child_commit (void *opaque) +{ +BdrvReplaceChildState *s = opaque; + +bdrv_unref (s->old_bs); +} + +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 */ +bdrv_replace_child_noperm (s->child, s->old_bs); +bdrv_unref (new_bs); +} + +static TransactionActionDrv bdrv_replace_child_drv = { + .commit = bdrv_replace_child_commit, + .abort = bdrv_replace_child_abort, + .clean = g_free, +}; + +/* +* bdrv_replace_child_safe +* +* Note: real unref of old_bs is done only on commit. +*/ +__attribute__((unused)) +static void bdrv_replace_child_safe (BdrvChild *child,BlockDriverState *new_bs, +GSList **tran) +{ +BdrvReplaceChildState *s =g_new (BdrvReplaceChildState,1 ); + *s = (BdrvReplaceChildState ) { + .child = child, + .old_bs = child->bs, + }; +tran_prepend (tran, &bdrv_replace_child_drv, s); + +if (new_bs) { +bdrv_ref (new_bs); + } +bdrv_replace_child_noperm (child, new_bs); +/* old_bs reference is transparently moved from @child to @s */ +} +/* * Check whether permissions on this node can be changed in a way that * @cumulative_perms and @cumulative_shared_perms are the new cumulative
Signature · 1 line
2.21.3
PATCH v2 19/36 block: fix bdrv_replace_node_common
In reply to Vladimir Sementsov-Ogievskiyinore_children thing doesn't help to track all propagated permissions of children we want to ignore. The simplest way to correctly update permissions is update graph first and then do permission update. In this case we just referesh permissions for the whole subgraph (in topological-sort defined order) and everything is correctly calculated automatically without any ignore_children.
So, refactor bdrv_replace_node_common to first do graph update and then refresh the permissions.
Test test_parallel_exclusive_write() now pass, so move it out of debugging "if".
--- block.c | 42 ++++++++++++++----------------------- tests/test-bdrv-graph-mod.c | 18 +++------------- 2 files changed, 19 insertions(+), 41 deletions(-)
block.c +16 -26
@@ -2199,7 +2199,6 @@ static TransactionActionDrv bdrv_replace_child_drv = {* * Note: real unref of old_bs is done only on commit. */ -__attribute__((unused))static void bdrv_replace_child_safe (BdrvChild *child,BlockDriverState *new_bs,GSList **tran) {
@@ -4794,8 +4793,9 @@ static int bdrv_replace_node_common(BlockDriverState *from, {int ret = -EPERM ;BdrvChild *c, *next; -GSList *list =NULL , *p; -uint64_t perm =0 , shared =BLK_PERM_ALL ; +GSList *tran =NULL ; +g_autoptr (GHashTable) found =NULL ; +g_autoptr (GSList) refresh_list =NULL ;/* Make sure that @from doesn't go away until we have successfully attached * all of its parents to @to. */
@@ -4805,7 +4805,12 @@ static int bdrv_replace_node_common(BlockDriverState *from,assert (bdrv_get_aio_context (from) ==bdrv_get_aio_context (to));bdrv_drained_begin (from); -/* Put all parents into @list and calculate their cumulative permissions */ +/* +* Do the replacement without permission update. +* Replacement may influence the permissions, we should calculate new +* permissions based on new graph. If we fail, we'll roll-back the +* replacement. +*/ QLIST_FOREACH_SAFE(c, &from->parents, next_parent, next) {assert (c->bs == from);if (!should_update_child (c, to)) {
@@ -4821,34 +4826,19 @@ static int bdrv_replace_node_common(BlockDriverState *from, c->name, from->node_name); goto out; } - list =g_slist_prepend (list, c); - perm |= c->perm; - shared &= c->shared_perm; - } - -/* Check whether the required permissions can be granted on @to, ignoring -* all BdrvChild in @list so that they can't block themselves. */ - ret =bdrv_check_update_perm (to,NULL , perm, shared, list, errp); -if (ret <0 ) { -bdrv_abort_perm_update (to); - goto out; +bdrv_replace_child_safe (c, to, &tran); } -/* Now actually perform the change. We performed the permission check for -* all elements of @list at once, so set the permissions all at once at the -* very end. */ -for (p = list; p !=NULL ; p = p->next) { - c = p->data; + found =g_hash_table_new (NULL ,NULL ); -bdrv_ref (to); -bdrv_replace_child_noperm (c, to); -bdrv_unref (from); - } + refresh_list =bdrv_topological_dfs (refresh_list, found, to); + refresh_list =bdrv_topological_dfs (refresh_list, found, from); -bdrv_set_perm (to); + ret =bdrv_list_refresh_perms (refresh_list,NULL , &tran, errp); out: -g_slist_free (list); +tran_finalize (tran, ret); +bdrv_drained_end (from);bdrv_unref (from);
tests/test-bdrv-graph-mod.c +3 -15
@@ -294,20 +294,11 @@ static void test_parallel_perm_update(void)bdrv_child_refresh_perms (top, top->children.lh_first, &error_abort);assert (c_fl1->perm &BLK_PERM_WRITE ); +bdrv_unref (top); }int main (int argc,char *argv[]) { -int i; -bool debug = false; - -for (i =1 ; i < argc; i++) { -if (!strcmp (argv[i],"-d" )) { - debug = true; -break ; - } - } -bdrv_init ();qemu_init_main_loop (&error_abort);
@@ -318,11 +309,8 @@ int main(int argc, char *argv[]) test_should_update_child);g_test_add_func ("/bdrv-graph-mod/parallel-perm-update" , test_parallel_perm_update); - -if (debug) { -g_test_add_func ("/bdrv-graph-mod/parallel-exclusive-write" , - test_parallel_exclusive_write); - } +g_test_add_func ("/bdrv-graph-mod/parallel-exclusive-write" , + test_parallel_exclusive_write);return g_test_run (); }
Signature · 1 line
2.21.3
Re: [PATCH v2 19/36] block: fix bdrv_replace_node_common
Am 27.11.2020 um 15:45 hat Vladimir Sementsov-Ogievskiy geschrieben:
Quoted 20 lines
inore_childrenthing doesn't help to track all propagated permissions of children we want to ignore. The simplest way to correctly update permissions is update graph first and then do permission update. In this case we just referesh permissions for the whole subgraph (in topological-sort defined order) and everything is correctly calculated automatically without anyignore_children.So, refactor
bdrv_replace_node_commonto first do graph update and then refresh the permissions.Test
test_parallel_exclusive_write()now pass, so move it out of debugging "if".Signed-off-by Vladimir Sementsov-Ogievskiy <[email protected]>tests/test-bdrv-graph-mod.c +0
-0
tests/test-bdrv-graph-mod.c static void test_parallel_perm_update(void) · line 298
Quoted 5 lines
tests/test-bdrv-graph-mod.c +1
-0@@ -294,20 +294,11 @@ static void test_parallel_perm_update(void)bdrv_child_refresh_perms (top, top->children.lh_first, &error_abort);assert (c_fl1->perm &BLK_PERM_WRITE ); +bdrv_unref (top); }
Why do have this addition in this patch? Shouldn't the changed function behave the same as before with respect to referenced nodes?
Kevin
Re: [PATCH v2 19/36] block: fix bdrv_replace_node_common
Quoted 23 lines · Kevin Wolf
Am 27.11.2020 um 15:45 hat Vladimir Sementsov-Ogievskiy geschrieben:
Quoted 22 lines
inore_childrenthing doesn't help to track all propagated permissions of children we want to ignore. The simplest way to correctly update permissions is update graph first and then do permission update. In this case we just referesh permissions for the whole subgraph (in topological-sort defined order) and everything is correctly calculated automatically without anyignore_children.So, refactor
bdrv_replace_node_commonto first do graph update and then refresh the permissions.Test
test_parallel_exclusive_write()now pass, so move it out of debugging "if".Signed-off-by Vladimir Sementsov-Ogievskiy <[email protected]>tests/test-bdrv-graph-mod.c +0
-0@@ -294,20 +294,11 @@ static void test_parallel_perm_update(void)bdrv_child_refresh_perms (top, top->children.lh_first, &error_abort);
Quoted 7 lines
tests/test-bdrv-graph-mod.c static void test_parallel_perm_update(void) · line 298
Quoted 3 lines
tests/test-bdrv-graph-mod.c +1
-0assert (c_fl1->perm &BLK_PERM_WRITE ); +bdrv_unref (top); }Why do have this addition in this patch? Shouldn't the changed function behave the same as before with respect to referenced nodes?
Hmm, looks like accidental fixup that should be squashed to original commit.. Or just a mistake. Will check when prepare next version
Signature · 2 lines
Best regards, Vladimir
PATCH v2 20/36 block: add bdrv_attach_child_common() transaction action
In reply to Vladimir Sementsov-OgievskiySplit out no-perm part of bdrv_root_attach_child() into separate transaction action. bdrv_root_attach_child() now moves to new permission update paradigm: first update graph relations then update permissions.
--- block.c | 162 ++++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 117 insertions(+), 45 deletions(-)
block.c +117 -45
@@ -86,6 +86,13 @@ static void bdrv_parent_set_aio_context_ignore(BdrvChild *c, AioContext *ctx, GSList **ignore);static void bdrv_replace_child_noperm (BdrvChild *child,BlockDriverState *new_bs); +static int bdrv_attach_child_common (BlockDriverState *child_bs, +const char *child_name, +const BdrvChildClass *child_class, +BdrvChildRole child_role, +uint64_t perm,uint64_t shared_perm, +void *opaque,BdrvChild **child, +GSList **tran,Error **errp);static int bdrv_reopen_prepare (BDRVReopenState *reopen_state,BlockReopenQueue *queue,Error **errp);
@@ -2898,55 +2905,22 @@ BdrvChild *bdrv_root_attach_child(BlockDriverState *child_bs, uint64_t perm, uint64_t shared_perm, void *opaque, Error **errp) { - BdrvChild *child; -Error *local_err =NULL ; int ret; -AioContext *ctx; +BdrvChild *child =NULL ; +GSList *tran =NULL ; - ret =bdrv_check_update_perm (child_bs,NULL , perm, shared_perm,NULL , errp); + ret =bdrv_attach_child_common (child_bs, child_name, child_class, + child_role, perm, shared_perm, opaque, + &child, &tran, errp);if (ret <0 ) { -bdrv_abort_perm_update (child_bs);bdrv_unref (child_bs);return NULL ; } - child =g_new (BdrvChild,1 ); - *child = (BdrvChild ) { - .bs =NULL , - .name =g_strdup (child_name), - .klass = child_class, - .role = child_role, - .perm = perm, - .shared_perm = shared_perm, - .opaque = opaque, - }; - - ctx =bdrv_child_get_parent_aio_context (child); - -/* If the AioContexts don't match, first try to move the subtree of -* child_bs into the AioContext of the new parent. If this doesn't work, -* try moving the parent into the AioContext of child_bs instead. */ -if (bdrv_get_aio_context (child_bs) != ctx) { - ret =bdrv_try_set_aio_context (child_bs, ctx, &local_err); -if (ret <0 ) { -if (bdrv_parent_try_set_aio_context (child, ctx,NULL ) ==0 ) { - ret =0 ; -error_free (local_err); - local_err =NULL ; - } - } -if (ret <0 ) { -error_propagate (errp, local_err); -g_free (child); -bdrv_abort_perm_update (child_bs); -bdrv_unref (child_bs); -return NULL ; - } - } - -/* This performs the matching bdrv_set_perm() for the above check. */ -bdrv_replace_child (child, child_bs); + ret =bdrv_refresh_perms (child_bs, errp); +tran_finalize (tran, ret); +bdrv_unref (child_bs);return child; }
@@ -2988,16 +2962,114 @@ BdrvChild *bdrv_attach_child(BlockDriverState *parent_bs,return child; } -static void bdrv_detach_child (BdrvChild *child) +static void bdrv_remove_empty_child (BdrvChild *child) { +assert (!child->bs);QLIST_SAFE_REMOVE (child, next); - -bdrv_replace_child (child,NULL ); -g_free (child->name);g_free (child); } +typedef struct BdrvAttachChildCommonState { +BdrvChild **child; +AioContext *old_parent_ctx; +AioContext *old_child_ctx; +}BdrvAttachChildCommonState ; + +static void bdrv_attach_child_common_abort (void *opaque) +{ +BdrvAttachChildCommonState *s = opaque; +BdrvChild *child = *s->child; +BlockDriverState *bs = child->bs; + +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); + } + +if (bdrv_child_get_parent_aio_context (child) != s->old_parent_ctx) { +bdrv_parent_try_set_aio_context (child, s->old_parent_ctx, + &error_abort); + } + +bdrv_unref (bs); +bdrv_remove_empty_child (child); + *s->child =NULL ; +} + +static TransactionActionDrv bdrv_attach_child_common_drv = { + .abort = bdrv_attach_child_common_abort, +}; + +/* +* Common part of attoching bdrv child to bs or to blk or to job +*/ +static int bdrv_attach_child_common (BlockDriverState *child_bs, +const char *child_name, +const BdrvChildClass *child_class, +BdrvChildRole child_role, +uint64_t perm,uint64_t shared_perm, +void *opaque,BdrvChild **child, +GSList **tran,Error **errp) +{ +int ret; +BdrvChild *new_child; +AioContext *parent_ctx; +AioContext *child_ctx =bdrv_get_aio_context (child_bs); + +assert (child); +assert (*child ==NULL ); + + new_child =g_new (BdrvChild,1 ); + *new_child = (BdrvChild ) { + .bs =NULL , + .name =g_strdup (child_name), + .klass = child_class, + .role = child_role, + .perm = perm, + .shared_perm = shared_perm, + .opaque = opaque, + }; + + parent_ctx =bdrv_child_get_parent_aio_context (new_child); +if (child_ctx != parent_ctx) { + ret =bdrv_try_set_aio_context (child_bs, parent_ctx,NULL ); +if (ret <0 ) { +/* +* bdrv_try_set_aio_context_tran don't need rollback after failure, +* so we don't care. +*/ + ret =bdrv_parent_try_set_aio_context (new_child, child_ctx, errp); + } +if (ret <0 ) { +bdrv_remove_empty_child (new_child); +return ret; + } + } + +bdrv_ref (child_bs); +bdrv_replace_child_noperm (new_child, child_bs); + + *child = new_child; + +BdrvAttachChildCommonState *s =g_new (BdrvAttachChildCommonState,1 ); + *s = (BdrvAttachChildCommonState ) { + .child = child, + .old_parent_ctx = parent_ctx, + .old_child_ctx = child_ctx, + }; +tran_prepend (tran, &bdrv_attach_child_common_drv, s); + +return 0 ; +} + +static void bdrv_detach_child (BdrvChild *child) +{ +bdrv_replace_child (child,NULL ); +bdrv_remove_empty_child (child); +} +/* Callers must ensure that child->frozen is false. */ void bdrv_root_unref_child (BdrvChild *child) {
Signature · 1 line
2.21.3
Re: [PATCH v2 20/36] block: add bdrv_attach_child_common() transaction action
Am 27.11.2020 um 15:45 hat Vladimir Sementsov-Ogievskiy geschrieben:
Quoted 15 lines
Split out no-perm part of
bdrv_root_attach_child()into separate transaction action.bdrv_root_attach_child()now moves to new permission update paradigm: first update graph relations then update permissions.Signed-off-by Vladimir Sementsov-Ogievskiy <[email protected]>--- block.c | 162 ++++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 117 insertions(+), 45 deletions(-)block.c +0
-0
block.c static void bdrv_parent_set_aio_context_ignore(BdrvChild *c, AioContext *ctx, · line 95
Quoted 10 lines
block.c +7
-0@@ -86,6 +86,13 @@ static void bdrv_parent_set_aio_context_ignore(BdrvChild *c, AioContext *ctx, GSList **ignore);static void bdrv_replace_child_noperm (BdrvChild *child,BlockDriverState *new_bs); +static int bdrv_attach_child_common (BlockDriverState *child_bs, +const char *child_name, +const BdrvChildClass *child_class, +BdrvChildRole child_role, +uint64_t perm,uint64_t shared_perm, +void *opaque,BdrvChild **child, +GSList **tran,Error **errp);
If you added the new code above bdrv_root_attach_child(), we wouldn't need the forward declaration and the patch would probably be simpler to read (because it's the first part of bdrv_root_attach_child() that is factored out).
Quoted 88 lines
block.c +16
-45static int bdrv_reopen_prepare (BDRVReopenState *reopen_state,BlockReopenQueue *queue,Error **errp);@@ -2898,55 +2905,22 @@ BdrvChild *bdrv_root_attach_child(BlockDriverState *child_bs, uint64_t perm, uint64_t shared_perm, void *opaque, Error **errp) { - BdrvChild *child; -Error *local_err =NULL ; int ret; -AioContext *ctx; +BdrvChild *child =NULL ; +GSList *tran =NULL ; - ret =bdrv_check_update_perm (child_bs,NULL , perm, shared_perm,NULL , errp); + ret =bdrv_attach_child_common (child_bs, child_name, child_class, + child_role, perm, shared_perm, opaque, + &child, &tran, errp);if (ret <0 ) { -bdrv_abort_perm_update (child_bs);bdrv_unref (child_bs);return NULL ; } - child =g_new (BdrvChild,1 ); - *child = (BdrvChild ) { - .bs =NULL , - .name =g_strdup (child_name), - .klass = child_class, - .role = child_role, - .perm = perm, - .shared_perm = shared_perm, - .opaque = opaque, - }; - - ctx =bdrv_child_get_parent_aio_context (child); - -/* If the AioContexts don't match, first try to move the subtree of -* child_bs into the AioContext of the new parent. If this doesn't work, -* try moving the parent into the AioContext of child_bs instead. */ -if (bdrv_get_aio_context (child_bs) != ctx) { - ret =bdrv_try_set_aio_context (child_bs, ctx, &local_err); -if (ret <0 ) { -if (bdrv_parent_try_set_aio_context (child, ctx,NULL ) ==0 ) { - ret =0 ; -error_free (local_err); - local_err =NULL ; - } - } -if (ret <0 ) { -error_propagate (errp, local_err); -g_free (child); -bdrv_abort_perm_update (child_bs); -bdrv_unref (child_bs); -return NULL ; - } - } - -/* This performs the matching bdrv_set_perm() for the above check. */ -bdrv_replace_child (child, child_bs); + ret =bdrv_refresh_perms (child_bs, errp); +tran_finalize (tran, ret); +bdrv_unref (child_bs);return child; }@@ -2988,16 +2962,114 @@ BdrvChild *bdrv_attach_child(BlockDriverState *parent_bs,return child; } -static void bdrv_detach_child (BdrvChild *child) +static void bdrv_remove_empty_child (BdrvChild *child) { +assert (!child->bs);QLIST_SAFE_REMOVE (child, next); - -bdrv_replace_child (child,NULL ); -g_free (child->name);g_free (child); } +typedef struct BdrvAttachChildCommonState { +BdrvChild **child; +AioContext *old_parent_ctx; +AioContext *old_child_ctx; +}BdrvAttachChildCommonState ; +
block.c BdrvChild *bdrv_attach_child(BlockDriverState *parent_bs, · line 2988
Quoted 10 lines
block.c +10
-0+static void bdrv_attach_child_common_abort (void *opaque) +{ +BdrvAttachChildCommonState *s = opaque; +BdrvChild *child = *s->child; +BlockDriverState *bs = child->bs; + +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);
Would failure actually be fatal? I think we can ignore it, the node is in an AioContext that works for it.
block.c BdrvChild *bdrv_attach_child(BlockDriverState *parent_bs, · line 2993
Quoted 5 lines
block.c +5
-0+ } + +if (bdrv_child_get_parent_aio_context (child) != s->old_parent_ctx) { +bdrv_parent_try_set_aio_context (child, s->old_parent_ctx, + &error_abort);
And the same here.
Quoted 46 lines
block.c +46
-0+ } + +bdrv_unref (bs); +bdrv_remove_empty_child (child); + *s->child =NULL ; +} + +static TransactionActionDrv bdrv_attach_child_common_drv = { + .abort = bdrv_attach_child_common_abort, +}; + +/* +* Common part of attoching bdrv child to bs or to blk or to job +*/ +static int bdrv_attach_child_common (BlockDriverState *child_bs, +const char *child_name, +const BdrvChildClass *child_class, +BdrvChildRole child_role, +uint64_t perm,uint64_t shared_perm, +void *opaque,BdrvChild **child, +GSList **tran,Error **errp) +{ +int ret; +BdrvChild *new_child; +AioContext *parent_ctx; +AioContext *child_ctx =bdrv_get_aio_context (child_bs); + +assert (child); +assert (*child ==NULL ); + + new_child =g_new (BdrvChild,1 ); + *new_child = (BdrvChild ) { + .bs =NULL , + .name =g_strdup (child_name), + .klass = child_class, + .role = child_role, + .perm = perm, + .shared_perm = shared_perm, + .opaque = opaque, + }; + + parent_ctx =bdrv_child_get_parent_aio_context (new_child); +if (child_ctx != parent_ctx) { + ret =bdrv_try_set_aio_context (child_bs, parent_ctx,NULL ); +if (ret <0 ) { +/*
block.c BdrvChild *bdrv_attach_child(BlockDriverState *parent_bs, · line 3049
Quoted 10 lines
block.c +10
-0+* bdrv_try_set_aio_context_tran don't need rollback after failure, +* so we don't care. +*/ + ret =bdrv_parent_try_set_aio_context (new_child, child_ctx, errp); + } +if (ret <0 ) { +bdrv_remove_empty_child (new_child); +return ret; + } + }
Not sure why you decided to rewrite this block while moving it from bdrv_root_attach_child().
We're losing the comment above it, and a possible error message is now related to changing the context of the parent node instead of the newly added node, which I imagine is less obvious in the general case.
Quoted 5 lines
block.c +5
-0+bdrv_ref (child_bs); +bdrv_replace_child_noperm (new_child, child_bs); + + *child = new_child; +
block.c BdrvChild *bdrv_attach_child(BlockDriverState *parent_bs, · line 3065
Quoted 10 lines
block.c +10
-0+BdrvAttachChildCommonState *s =g_new (BdrvAttachChildCommonState,1 ); + *s = (BdrvAttachChildCommonState ) { + .child = child, + .old_parent_ctx = parent_ctx, + .old_child_ctx = child_ctx, + }; +tran_prepend (tran, &bdrv_attach_child_common_drv, s); + +return 0 ; +}
Kevin
Re: [PATCH v2 20/36] block: add bdrv_attach_child_common() transaction action
Quoted 125 lines · Kevin Wolf
Am 27.11.2020 um 15:45 hat Vladimir Sementsov-Ogievskiy geschrieben:
Quoted 15 lines
Split out no-perm part of
bdrv_root_attach_child()into separate transaction action.bdrv_root_attach_child()now moves to new permission update paradigm: first update graph relations then update permissions.Signed-off-by Vladimir Sementsov-Ogievskiy <[email protected]>--- block.c | 162 ++++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 117 insertions(+), 45 deletions(-)block.c +0
-0block.c static void bdrv_parent_set_aio_context_ignore(BdrvChild *c, AioContext *ctx, · line 95
Quoted 10 lines
block.c +7
-0@@ -86,6 +86,13 @@ static void bdrv_parent_set_aio_context_ignore(BdrvChild *c, AioContext *ctx, GSList **ignore);static void bdrv_replace_child_noperm (BdrvChild *child,BlockDriverState *new_bs); +static int bdrv_attach_child_common (BlockDriverState *child_bs, +const char *child_name, +const BdrvChildClass *child_class, +BdrvChildRole child_role, +uint64_t perm,uint64_t shared_perm, +void *opaque,BdrvChild **child, +GSList **tran,Error **errp);If you added the new code above
bdrv_root_attach_child(), we wouldn't need the forward declaration and the patch would probably be simpler to read (because it's the first part ofbdrv_root_attach_child()that is factored out).Quoted 93 lines
block.c +21
-45static int bdrv_reopen_prepare (BDRVReopenState *reopen_state,BlockReopenQueue *queue,Error **errp);@@ -2898,55 +2905,22 @@ BdrvChild *bdrv_root_attach_child(BlockDriverState *child_bs, uint64_t perm, uint64_t shared_perm, void *opaque, Error **errp) { - BdrvChild *child; -Error *local_err =NULL ; int ret; -AioContext *ctx; +BdrvChild *child =NULL ; +GSList *tran =NULL ; - ret =bdrv_check_update_perm (child_bs,NULL , perm, shared_perm,NULL , errp); + ret =bdrv_attach_child_common (child_bs, child_name, child_class, + child_role, perm, shared_perm, opaque, + &child, &tran, errp);if (ret <0 ) { -bdrv_abort_perm_update (child_bs);bdrv_unref (child_bs);return NULL ; } - child =g_new (BdrvChild,1 ); - *child = (BdrvChild ) { - .bs =NULL , - .name =g_strdup (child_name), - .klass = child_class, - .role = child_role, - .perm = perm, - .shared_perm = shared_perm, - .opaque = opaque, - }; - - ctx =bdrv_child_get_parent_aio_context (child); - -/* If the AioContexts don't match, first try to move the subtree of -* child_bs into the AioContext of the new parent. If this doesn't work, -* try moving the parent into the AioContext of child_bs instead. */ -if (bdrv_get_aio_context (child_bs) != ctx) { - ret =bdrv_try_set_aio_context (child_bs, ctx, &local_err); -if (ret <0 ) { -if (bdrv_parent_try_set_aio_context (child, ctx,NULL ) ==0 ) { - ret =0 ; -error_free (local_err); - local_err =NULL ; - } - } -if (ret <0 ) { -error_propagate (errp, local_err); -g_free (child); -bdrv_abort_perm_update (child_bs); -bdrv_unref (child_bs); -return NULL ; - } - } - -/* This performs the matching bdrv_set_perm() for the above check. */ -bdrv_replace_child (child, child_bs); + ret =bdrv_refresh_perms (child_bs, errp); +tran_finalize (tran, ret); +bdrv_unref (child_bs);return child; }@@ -2988,16 +2962,114 @@ BdrvChild *bdrv_attach_child(BlockDriverState *parent_bs,return child; } -static void bdrv_detach_child (BdrvChild *child) +static void bdrv_remove_empty_child (BdrvChild *child) { +assert (!child->bs);QLIST_SAFE_REMOVE (child, next); - -bdrv_replace_child (child,NULL ); -g_free (child->name);g_free (child); } +typedef struct BdrvAttachChildCommonState { +BdrvChild **child; +AioContext *old_parent_ctx; +AioContext *old_child_ctx; +}BdrvAttachChildCommonState ; + +static void bdrv_attach_child_common_abort (void *opaque) +{ +BdrvAttachChildCommonState *s = opaque; +BdrvChild *child = *s->child; +BlockDriverState *bs = child->bs;
Quoted 8 lines
block.c BdrvChild *bdrv_attach_child(BlockDriverState *parent_bs, · line 2988
Quoted 5 lines
block.c +5
-0+ +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);Would failure actually be fatal? I think we can ignore it, the node is in an AioContext that works for it.
As far as I explored the code, check-aio-context is transparent enough, nothing rely on IO, etc, and if we succeeded to change it we must success in revert.
And as I understand it is critical: if we failed to rollback aio-context change somewhere (but succeeded in reverting graph relation change), it means that we end up with different aio contexts inside one block subtree..
Quoted 69 lines
block.c BdrvChild *bdrv_attach_child(BlockDriverState *parent_bs, · line 2993
Quoted 6 lines
block.c +5
-0+ } + +if (bdrv_child_get_parent_aio_context (child) != s->old_parent_ctx) { +bdrv_parent_try_set_aio_context (child, s->old_parent_ctx, + &error_abort);And the same here.
Quoted 46 lines
block.c +46
-0+ } + +bdrv_unref (bs); +bdrv_remove_empty_child (child); + *s->child =NULL ; +} + +static TransactionActionDrv bdrv_attach_child_common_drv = { + .abort = bdrv_attach_child_common_abort, +}; + +/* +* Common part of attoching bdrv child to bs or to blk or to job +*/ +static int bdrv_attach_child_common (BlockDriverState *child_bs, +const char *child_name, +const BdrvChildClass *child_class, +BdrvChildRole child_role, +uint64_t perm,uint64_t shared_perm, +void *opaque,BdrvChild **child, +GSList **tran,Error **errp) +{ +int ret; +BdrvChild *new_child; +AioContext *parent_ctx; +AioContext *child_ctx =bdrv_get_aio_context (child_bs); + +assert (child); +assert (*child ==NULL ); + + new_child =g_new (BdrvChild,1 ); + *new_child = (BdrvChild ) { + .bs =NULL , + .name =g_strdup (child_name), + .klass = child_class, + .role = child_role, + .perm = perm, + .shared_perm = shared_perm, + .opaque = opaque, + }; + + parent_ctx =bdrv_child_get_parent_aio_context (new_child); +if (child_ctx != parent_ctx) { + ret =bdrv_try_set_aio_context (child_bs, parent_ctx,NULL ); +if (ret <0 ) { +/* block.c BdrvChild *bdrv_attach_child(BlockDriverState *parent_bs, · line 3049
Quoted 10 lines
block.c +10
-0+* bdrv_try_set_aio_context_tran don't need rollback after failure, +* so we don't care. +*/ + ret =bdrv_parent_try_set_aio_context (new_child, child_ctx, errp); + } +if (ret <0 ) { +bdrv_remove_empty_child (new_child); +return ret; + } + }Not sure why you decided to rewrite this block while moving it from
bdrv_root_attach_child().
Quoted 3 lines
We're losing the comment above it, and a possible error message is now related to changing the context of the parent node instead of the newly added node, which I imagine is less obvious in the general case.
Don't remember:( Will try to revert, and if find that it's really needed, will leave some good comment on it.
Quoted 19 lines
Quoted 6 lines
block.c +5
-0+bdrv_ref (child_bs); +bdrv_replace_child_noperm (new_child, child_bs); + + *child = new_child; +block.c BdrvChild *bdrv_attach_child(BlockDriverState *parent_bs, · line 3065
Quoted 10 lines
block.c +10
-0+BdrvAttachChildCommonState *s =g_new (BdrvAttachChildCommonState,1 ); + *s = (BdrvAttachChildCommonState ) { + .child = child, + .old_parent_ctx = parent_ctx, + .old_child_ctx = child_ctx, + }; +tran_prepend (tran, &bdrv_attach_child_common_drv, s); + +return 0 ; +}Kevin
Signature · 2 lines
Best regards, Vladimir
Re: [PATCH v2 20/36] block: add bdrv_attach_child_common() transaction action
Am 04.02.2021 um 08:34 hat Vladimir Sementsov-Ogievskiy geschrieben:
Quoted 23 lines
Quoted 8 lines · Kevin Wolf
Am 27.11.2020 um 15:45 hat Vladimir Sementsov-Ogievskiy geschrieben:
Quoted 6 lines
Split out no-perm part of
bdrv_root_attach_child()into separate transaction action.bdrv_root_attach_child()now moves to new permission update paradigm: first update graph relations then update permissions.Signed-off-by Vladimir Sementsov-Ogievskiy <[email protected]>Quoted 13 lines
block.c BdrvChild *bdrv_attach_child(BlockDriverState *parent_bs, · line 2988
Quoted 10 lines
block.c +10
-0+static void bdrv_attach_child_common_abort (void *opaque) +{ +BdrvAttachChildCommonState *s = opaque; +BdrvChild *child = *s->child; +BlockDriverState *bs = child->bs; + +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);Would failure actually be fatal? I think we can ignore it, the node is in an AioContext that works for it.
Quoted 8 lines
As far as I explored the code, check-aio-context is transparent enough, nothing rely on IO, etc, and if we succeeded to change it we must success in revert.
And as I understand it is critical: if we failed to rollback aio-context change somewhere (but succeeded in reverting graph relation change), it means that we end up with different aio contexts inside one block subtree..
Ah, right, we're going to change the graph once again, so what is working now doesn't have to be working for the changed graph.
Ok, let's leave this as &error_abort.
Kevin
PATCH v2 21/36 block: add bdrv_attach_child_noperm() transaction action
In reply to Vladimir Sementsov-OgievskiySplit no-perm part of bdrv_attach_child as separate transaction action. It will be used in later commits.
--- block.c | 71 ++++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 58 insertions(+), 13 deletions(-)
block.c +58 -13
@@ -86,6 +86,14 @@ static void bdrv_parent_set_aio_context_ignore(BdrvChild *c, AioContext *ctx, GSList **ignore);static void bdrv_replace_child_noperm (BdrvChild *child,BlockDriverState *new_bs); +static int bdrv_attach_child_noperm (BlockDriverState *parent_bs, +BlockDriverState *child_bs, +const char *child_name, +const BdrvChildClass *child_class, +BdrvChildRole child_role, +BdrvChild **child, +GSList **tran, +Error **errp); static int bdrv_attach_child_common(BlockDriverState *child_bs, const char *child_name, const BdrvChildClass *child_class,
@@ -2942,23 +2950,26 @@ BdrvChild *bdrv_attach_child(BlockDriverState *parent_bs,BdrvChildRole child_role, Error **errp) { - BdrvChild *child; -uint64_t perm, shared_perm; - -bdrv_get_cumulative_perm (parent_bs, &perm, &shared_perm); + int ret; +BdrvChild *child =NULL ; +GSList *tran =NULL ; -assert (parent_bs->drv); -bdrv_child_perm (parent_bs, child_bs,NULL , child_role,NULL , - perm, shared_perm, &perm, &shared_perm); + ret =bdrv_attach_child_noperm (parent_bs, child_bs, child_name, child_class, + child_role, &child, &tran, errp); +if (ret <0 ) { + goto out; + } - child =bdrv_root_attach_child (child_bs, child_name, child_class, - child_role, perm, shared_perm, parent_bs, - errp); -if (child ==NULL ) { -return NULL ; + ret =bdrv_refresh_perms (parent_bs, errp); +if (ret <0 ) { + goto out; } -QLIST_INSERT_HEAD (&parent_bs->children, child, next); +out: +tran_finalize (tran, ret); + +bdrv_unref (child_bs); +return child; }
@@ -3064,6 +3075,40 @@ static int bdrv_attach_child_common(BlockDriverState *child_bs,return 0 ; } +static int bdrv_attach_child_noperm (BlockDriverState *parent_bs, +BlockDriverState *child_bs, +const char *child_name, +const BdrvChildClass *child_class, +BdrvChildRole child_role, +BdrvChild **child, +GSList **tran, +Error **errp) +{ +int ret; +uint64_t perm, shared_perm; + +assert (parent_bs->drv); + +bdrv_get_cumulative_perm (parent_bs, &perm, &shared_perm); +bdrv_child_perm (parent_bs, child_bs,NULL , child_role,NULL , + perm, shared_perm, &perm, &shared_perm); + + ret =bdrv_attach_child_common (child_bs, child_name, child_class, + child_role, perm, shared_perm, parent_bs, + child, tran, errp); +if (ret <0 ) { +return ret; + } + +QLIST_INSERT_HEAD (&parent_bs->children, *child, next); +/* +* child is removed in bdrv_attach_child_common_abort(), so don't care to +* abort this change separately. +*/ + +return 0 ; +} +static void bdrv_detach_child (BdrvChild *child) {bdrv_replace_child (child,NULL );
Signature · 1 line
2.21.3
PATCH v2 22/36 block: split out bdrv_replace_node_noperm()
In reply to Vladimir Sementsov-OgievskiySplit part of bdrv_replace_node_common() to be used separately.
--- block.c | 47 ++++++++++++++++++++++++++++++----------------- 1 file changed, 30 insertions(+), 17 deletions(-)
block.c +30 -17
@@ -4897,6 +4897,33 @@ static bool should_update_child(BdrvChild *c, BlockDriverState *to)return ret; } +static int bdrv_replace_node_noperm (BlockDriverState *from, +BlockDriverState *to, +bool auto_skip,GSList **tran,Error **errp) +{ +BdrvChild *c, *next; + + QLIST_FOREACH_SAFE(c, &from->parents, next_parent, next) { +assert (c->bs == from); +if (!should_update_child (c, to)) { +if (auto_skip) { +continue ; + } +error_setg (errp,"Should not change '%s' link to '%s'" , + c->name, from->node_name); +return -EPERM ; + } +if (c->frozen) { +error_setg (errp,"Cannot change '%s' link to '%s'" , + c->name, from->node_name); +return -EPERM ; + } +bdrv_replace_child_safe (c, to, tran); + } + +return 0 ; +} +/* * With auto_skip=true bdrv_replace_node_common skips updating from parents * if it creates a parent-child relation loop or if parent is block-job.
@@ -4909,7 +4936,6 @@ static int bdrv_replace_node_common(BlockDriverState *from, bool auto_skip, Error **errp) { int ret = -EPERM; -BdrvChild *c, *next;GSList *tran =NULL ;g_autoptr (GHashTable) found =NULL ;g_autoptr (GSList) refresh_list =NULL ;
@@ -4928,22 +4954,9 @@ static int bdrv_replace_node_common(BlockDriverState *from,* permissions based on new graph. If we fail, we'll roll-back the * replacement. */ - QLIST_FOREACH_SAFE(c, &from->parents, next_parent, next) { -assert (c->bs == from); -if (!should_update_child (c, to)) { -if (auto_skip) { -continue ; - } -error_setg (errp,"Should not change '%s' link to '%s'" , - c->name, from->node_name); - goto out; - } -if (c->frozen) { -error_setg (errp,"Cannot change '%s' link to '%s'" , - c->name, from->node_name); - goto out; - } -bdrv_replace_child_safe (c, to, &tran); + ret =bdrv_replace_node_noperm (from, to, auto_skip, &tran, errp); +if (ret <0 ) { + goto out; } found =g_hash_table_new (NULL ,NULL );
Signature · 1 line
2.21.3
Re: [PATCH v2 22/36] block: split out bdrv_replace_node_noperm()
Am 27.11.2020 um 15:45 hat Vladimir Sementsov-Ogievskiy geschrieben:
block.c static int bdrv_replace_node_common(BlockDriverState *from, · line 4941
Quoted 12 lines
Split part of
bdrv_replace_node_common()to be used separately.Signed-off-by Vladimir Sementsov-Ogievskiy <[email protected]>block.c +0
-1@@ -4909,7 +4936,6 @@ static int bdrv_replace_node_common(BlockDriverState *from, bool auto_skip, Error **errp) { int ret = -EPERM; -BdrvChild *c, *next;GSList *tran =NULL ;g_autoptr (GHashTable) found =NULL ;g_autoptr (GSList) refresh_list =NULL ;
ret doesn't need to be initialised any more now.
Kevin
PATCH v2 23/36 block: adapt bdrv_append() for inserting filters
In reply to Vladimir Sementsov-Ogievskiybdrv_append is not very good for inserting filters: it does extra permission update as part of bdrv_set_backing_hd(). During this update filter may conflict with other parents of top_bs.
Instead, let's first do all graph modifications and after it update permissions.
Note: bdrv_append() is still only works for backing-child based filters. It's something to improve later.
It simplifies the fact that bdrv_append() used to append new nodes, without backing child. Let's add an assertion.
--- block.c | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-)
block.c +17 -11
@@ -4998,22 +4998,28 @@ int bdrv_replace_node(BlockDriverState *from, BlockDriverState *to,int bdrv_append (BlockDriverState *bs_new,BlockDriverState *bs_top,Error **errp) { -Error *local_err =NULL ; +int ret; +GSList *tran =NULL ; -bdrv_set_backing_hd (bs_new, bs_top, &local_err); -if (local_err) { -error_propagate (errp, local_err); -return -EPERM ; +assert (!bs_new->backing); + + ret =bdrv_attach_child_noperm (bs_new, bs_top,"backing" , + &child_of_bds,bdrv_backing_role (bs_new), + &bs_new->backing, &tran, errp); +if (ret <0 ) { + goto out; } -bdrv_replace_node (bs_top, bs_new, &local_err); -if (local_err) { -error_propagate (errp, local_err); -bdrv_set_backing_hd (bs_new,NULL , &error_abort); -return -EPERM ; + ret =bdrv_replace_node_noperm (bs_top, bs_new, true, &tran, errp); +if (ret <0 ) { + goto out; } -return 0 ; + ret =bdrv_refresh_perms (bs_new, errp); +out: +tran_finalize (tran, ret); + +return ret; }static void bdrv_delete (BlockDriverState *bs)
Signature · 1 line
2.21.3
Re: [PATCH v2 23/36] block: adapt bdrv_append() for inserting filters
Am 27.11.2020 um 15:45 hat Vladimir Sementsov-Ogievskiy geschrieben:
Quoted 6 lines
bdrv_appendis not very good for inserting filters: it does extra permission update as part ofbdrv_set_backing_hd(). During this update filter may conflict with other parents oftop_bs.Instead, let's first do all graph modifications and after it update permissions.
This sounds like it fixes a bug. If so, should we have a test like for the other cases fixed by this series?
Quoted 25 lines
Note:
bdrv_append()is still only works for backing-child based filters. It's something to improve later.It simplifies the fact that
bdrv_append()used to append new nodes, without backing child. Let's add an assertion.Signed-off-by Vladimir Sementsov-Ogievskiy <[email protected]>--- block.c | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-)block.c +2
-3@@ -4998,22 +4998,28 @@ int bdrv_replace_node(BlockDriverState *from, BlockDriverState *to,int bdrv_append (BlockDriverState *bs_new,BlockDriverState *bs_top,Error **errp) { -Error *local_err =NULL ; +int ret; +GSList *tran =NULL ; -bdrv_set_backing_hd (bs_new, bs_top, &local_err); -if (local_err) {
block.c int bdrv_replace_node(BlockDriverState *from, BlockDriverState *to, · line 5011
Quoted 10 lines
block.c +7
-2-error_propagate (errp, local_err); -return -EPERM ; +assert (!bs_new->backing); + + ret =bdrv_attach_child_noperm (bs_new, bs_top,"backing" , + &child_of_bds,bdrv_backing_role (bs_new), + &bs_new->backing, &tran, errp); +if (ret <0 ) { + goto out; }
I don't think changing bs->backing without bdrv_set_backing_hd() is correct at the moment. We lose a few things:
1. The bdrv_is_backing_chain_frozen() check
2. Updating backing_hd->inherits_from if necessary
3. bdrv_refresh_limits()
If I'm not missing anything, all of these are needed in the context of bdrv_append().
Quoted 7 lines
block.c +2
-5-bdrv_replace_node (bs_top, bs_new, &local_err); -if (local_err) { -error_propagate (errp, local_err); -bdrv_set_backing_hd (bs_new,NULL , &error_abort); -return -EPERM ; + ret =bdrv_replace_node_noperm (bs_top, bs_new, true, &tran, errp); +if (ret <0 ) {
block.c int bdrv_replace_node(BlockDriverState *from, BlockDriverState *to, · line 5023
Quoted 10 lines
block.c +6
-1+ goto out; } -return 0 ; + ret =bdrv_refresh_perms (bs_new, errp); +out: +tran_finalize (tran, ret); + +return ret; }
Kevin
Re: [PATCH v2 23/36] block: adapt bdrv_append() for inserting filters
Quoted 10 lines · Kevin Wolf
Am 27.11.2020 um 15:45 hat Vladimir Sementsov-Ogievskiy geschrieben:
Quoted 6 lines
bdrv_appendis not very good for inserting filters: it does extra permission update as part ofbdrv_set_backing_hd(). During this update filter may conflict with other parents oftop_bs.Instead, let's first do all graph modifications and after it update permissions.
This sounds like it fixes a bug. If so, should we have a test like for the other cases fixed by this series?
Hm. I considered it mostly like a lack not a bug. We just have to workaround this lack by "inactive" mode of filters. But adding a test is good idea anyway. Will do.
Quoted 40 lines
Quoted 26 lines
Note:
bdrv_append()is still only works for backing-child based filters. It's something to improve later.It simplifies the fact that
bdrv_append()used to append new nodes, without backing child. Let's add an assertion.Signed-off-by Vladimir Sementsov-Ogievskiy <[email protected]>--- block.c | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-)block.c +2
-3@@ -4998,22 +4998,28 @@ int bdrv_replace_node(BlockDriverState *from, BlockDriverState *to,int bdrv_append (BlockDriverState *bs_new,BlockDriverState *bs_top,Error **errp) { -Error *local_err =NULL ; +int ret; +GSList *tran =NULL ; -bdrv_set_backing_hd (bs_new, bs_top, &local_err); -if (local_err) {block.c int bdrv_replace_node(BlockDriverState *from, BlockDriverState *to, · line 5011
Quoted 10 lines
block.c +7
-2-error_propagate (errp, local_err); -return -EPERM ; +assert (!bs_new->backing); + + ret =bdrv_attach_child_noperm (bs_new, bs_top,"backing" , + &child_of_bds,bdrv_backing_role (bs_new), + &bs_new->backing, &tran, errp); +if (ret <0 ) { + goto out; }I don't think changing
bs->backingwithoutbdrv_set_backing_hd()is correct at the moment. We lose a few things:
Quoted 6 lines
1. The
bdrv_is_backing_chain_frozen()check
2. Updatingbacking_hd->inherits_fromif necessary
3.bdrv_refresh_limits()If I'm not missing anything, all of these are needed in the context of
bdrv_append().
I decided that bdrv_append() is only for appending new nodes, so frozen and inherts_from checks are not needed. And I've added assert(!bs_new->backing)...
Checking this now:
- appending filters is obvious
- bdrv_append_temp_snapshot() creates new qcow2 node based on tmp file, don't see any backing initialization (and it would be rather strange)
- external_snapshot_prepare() do check if (bdrv_cow_child(state->new_bs)) { error-out }
So everything is OK. I should describe it in commit message and add a comment to bdrv_append.
Quoted 21 lines
Quoted 8 lines
block.c +2
-5-bdrv_replace_node (bs_top, bs_new, &local_err); -if (local_err) { -error_propagate (errp, local_err); -bdrv_set_backing_hd (bs_new,NULL , &error_abort); -return -EPERM ; + ret =bdrv_replace_node_noperm (bs_top, bs_new, true, &tran, errp); +if (ret <0 ) {block.c int bdrv_replace_node(BlockDriverState *from, BlockDriverState *to, · line 5023
Quoted 10 lines
block.c +6
-1+ goto out; } -return 0 ; + ret =bdrv_refresh_perms (bs_new, errp); +out: +tran_finalize (tran, ret); + +return ret; }Kevin
Signature · 2 lines
Best regards, Vladimir
Re: [PATCH v2 23/36] block: adapt bdrv_append() for inserting filters
Am 04.02.2021 um 09:30 hat Vladimir Sementsov-Ogievskiy geschrieben:
Quoted 35 lines
Quoted 23 lines · Kevin Wolf
Am 27.11.2020 um 15:45 hat Vladimir Sementsov-Ogievskiy geschrieben:
Quoted 8 lines
int bdrv_append(BlockDriverState
*bs_new, BlockDriverState*bs_top,
Error **errp)
{block.c +2
-3-Error *local_err =NULL ; +int ret; +GSList *tran =NULL ; -bdrv_set_backing_hd (bs_new, bs_top, &local_err); -if (local_err) {block.c int bdrv_replace_node(BlockDriverState *from, BlockDriverState *to, · line 5011
Quoted 10 lines
block.c +7
-2-error_propagate (errp, local_err); -return -EPERM ; +assert (!bs_new->backing); + + ret =bdrv_attach_child_noperm (bs_new, bs_top,"backing" , + &child_of_bds,bdrv_backing_role (bs_new), + &bs_new->backing, &tran, errp); +if (ret <0 ) { + goto out; }I don't think changing
bs->backingwithoutbdrv_set_backing_hd()is correct at the moment. We lose a few things:Quoted 6 lines
1. The
bdrv_is_backing_chain_frozen()check
2. Updatingbacking_hd->inherits_fromif necessary
3.bdrv_refresh_limits()If I'm not missing anything, all of these are needed in the context of
bdrv_append().I decided that
bdrv_append()is only for appending new nodes, so frozen andinherts_fromchecks are not needed. And I've addedassert(!bs_new->backing)...
Quoted 6 lines
Checking this now:
- appending filters is obvious
-bdrv_append_temp_snapshot()creates new qcow2 node based on tmp file, don't see any backing initialization (and it would be rather strange)
Yes, the internal uses are obviously unproblematic for the frozen check.
Quoted 2 lines
-
external_snapshot_prepare()do check if
(bdrv_cow_child(state->new_bs)) { error-out }
Ok, the only thing bdrv_set_backing_hd() can and must check is whether the link to the old backing file was frozen, and we know that we don't have an old backing file. Makes sense.
Same thing for inherits_from, we only do this if the the new backing file (i.e. the old active layer for bdrv_append) was already in the backing chain of the new node.
Quoted 2 lines
So everything is OK. I should describe it in commit message and add a comment to
bdrv_append.
What about bdrv_refresh_limits()? The node gains a new backing file, so I think the limits could change.
Ideally, bdrv_child_cb_attach/detach() would take care of this, but at the moment they don't.
Kevin
Re: [PATCH v2 23/36] block: adapt bdrv_append() for inserting filters
Quoted 56 lines · Kevin Wolf
Am 04.02.2021 um 09:30 hat Vladimir Sementsov-Ogievskiy geschrieben:
Quoted 35 lines
Quoted 23 lines · Kevin Wolf
Am 27.11.2020 um 15:45 hat Vladimir Sementsov-Ogievskiy geschrieben:
Quoted 8 lines
int bdrv_append(BlockDriverState
*bs_new, BlockDriverState*bs_top,
Error **errp)
{block.c +2
-3-Error *local_err =NULL ; +int ret; +GSList *tran =NULL ; -bdrv_set_backing_hd (bs_new, bs_top, &local_err); -if (local_err) {block.c int bdrv_replace_node(BlockDriverState *from, BlockDriverState *to, · line 5011
Quoted 10 lines
block.c +7
-2-error_propagate (errp, local_err); -return -EPERM ; +assert (!bs_new->backing); + + ret =bdrv_attach_child_noperm (bs_new, bs_top,"backing" , + &child_of_bds,bdrv_backing_role (bs_new), + &bs_new->backing, &tran, errp); +if (ret <0 ) { + goto out; }I don't think changing
bs->backingwithoutbdrv_set_backing_hd()is correct at the moment. We lose a few things:Quoted 6 lines
1. The
bdrv_is_backing_chain_frozen()check
2. Updatingbacking_hd->inherits_fromif necessary
3.bdrv_refresh_limits()If I'm not missing anything, all of these are needed in the context of
bdrv_append().I decided that
bdrv_append()is only for appending new nodes, so frozen andinherts_fromchecks are not needed. And I've addedassert(!bs_new->backing)...Quoted 6 lines
Checking this now:
- appending filters is obvious
-bdrv_append_temp_snapshot()creates new qcow2 node based on tmp file, don't see any backing initialization (and it would be rather strange)Yes, the internal uses are obviously unproblematic for the frozen check.
Quoted 2 lines
-
external_snapshot_prepare()do check if
(bdrv_cow_child(state->new_bs)) { error-out }Ok, the only thing
bdrv_set_backing_hd()can and must check is whether the link to the old backing file was frozen, and we know that we don't have an old backing file. Makes sense.Same thing for
inherits_from, we only do this if the the new backing file (i.e. the old active layer forbdrv_append) was already in the backing chain of the new node.
Quoted 9 lines
Quoted 2 lines
So everything is OK. I should describe it in commit message and add a comment to
bdrv_append.What about
bdrv_refresh_limits()? The node gains a new backing file, so I think the limits could change.Ideally, bdrv_child_cb_attach/detach() would take care of this, but at the moment they don't.
when answering I thought that it is called at the end of a function. But I both forget to write it in the answer and was wrong :) As it's actually bdrv_refresh_perms(). I'll add call of bdrv_refresh_limits()
Signature · 2 lines
Best regards, Vladimir
PATCH v2 24/36 block: add bdrv_remove_backing transaction action
In reply to Vladimir Sementsov-Ogievskiy--- block.c | 42 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 40 insertions(+), 2 deletions(-)
block.c +40 -2
@@ -2973,12 +2973,19 @@ out:return child; } +static void bdrv_child_free (void *opaque) +{ +BdrvChild *c = opaque; + +g_free (c->name); +g_free (c); +} +static void bdrv_remove_empty_child (BdrvChild *child) {assert (!child->bs);QLIST_SAFE_REMOVE (child, next); -g_free (child->name); -g_free (child); +bdrv_child_free (child); } typedef struct BdrvAttachChildCommonState {
@@ -4897,6 +4904,37 @@ static bool should_update_child(BdrvChild *c, BlockDriverState *to)return ret; } +/* this doesn't restore original child bs, only the child itself */ +static void bdrv_remove_backing_abort (void *opaque) +{ +BdrvChild *c = opaque; +BlockDriverState *parent_bs = c->opaque; + +QLIST_INSERT_HEAD (&parent_bs->children, c, next); + parent_bs->backing = c; +} + +static TransactionActionDrv bdrv_remove_backing_drv = { + .abort = bdrv_remove_backing_abort, + .commit = bdrv_child_free, +}; + +__attribute__((unused)) +static void bdrv_remove_backing (BlockDriverState *bs,GSList **tran) +{ +if (!bs->backing) { +return ; + } + +if (bs->backing->bs) { +bdrv_replace_child_safe (bs->backing,NULL , tran); + } + +tran_prepend (tran, &bdrv_remove_backing_drv, bs->backing); +QLIST_SAFE_REMOVE (bs->backing, next); + bs->backing =NULL ; +} +static int bdrv_replace_node_noperm (BlockDriverState *from,BlockDriverState *to,bool auto_skip,GSList **tran,Error **errp)
Signature · 1 line
2.21.3
PATCH v2 25/36 block: introduce bdrv_drop_filter()
In reply to Vladimir Sementsov-OgievskiyUsing bdrv_replace_node() for removing filter is not good enough: it keeps child reference of the filter, which may conflict with original top node during permission update.
Instead let's create new interface, which will do all graph modifications first and then update permissions.
Let's modify bdrv_replace_node_common(), allowing it additionally drop backing chain child link pointing to new node. This is quite appropriate for bdrv_drop_intermediate() and makes possible to add new bdrv_drop_filter() as a simple wrapper.
--- include/block/block.h | 1 + block.c | 42 ++++++++++++++++++++++++++++++++++++++---- 2 files changed, 39 insertions(+), 4 deletions(-)
include/block/block.h +1 -0
@@ -348,6 +348,7 @@ int bdrv_append(BlockDriverState *bs_new, BlockDriverState *bs_top, Error **errp);int bdrv_replace_node (BlockDriverState *from,BlockDriverState *to,Error **errp); +int bdrv_drop_filter (BlockDriverState *bs,Error **errp);int bdrv_parse_aio (const char *mode,int *flags);int bdrv_parse_cache_mode (const char *mode,int *flags,bool *writethrough);
block.c +38 -4
80 lines not on this page · raw message
Signature · 1 line
2.21.3
Re: [PATCH v2 25/36] block: introduce bdrv_drop_filter()
Am 27.11.2020 um 15:45 hat Vladimir Sementsov-Ogievskiy geschrieben:
Quoted 43 lines
Using
bdrv_replace_node()for removing filter is not good enough: it keeps child reference of the filter, which may conflict with original top node during permission update.Instead let's create new interface, which will do all graph modifications first and then update permissions.
Let's modify
bdrv_replace_node_common(), allowing it additionally drop backing chain child link pointing to new node. This is quite appropriate forbdrv_drop_intermediate()and makes possible to add newbdrv_drop_filter()as a simple wrapper.Signed-off-by Vladimir Sementsov-Ogievskiy <[email protected]>--- include/block/block.h | 1 + block.c | 42 ++++++++++++++++++++++++++++++++++++++---- 2 files changed, 39 insertions(+), 4 deletions(-)include/block/block.h +1
-0@@ -348,6 +348,7 @@ int bdrv_append(BlockDriverState *bs_new, BlockDriverState *bs_top, Error **errp);int bdrv_replace_node (BlockDriverState *from,BlockDriverState *to,Error **errp); +int bdrv_drop_filter (BlockDriverState *bs,Error **errp);int bdrv_parse_aio (const char *mode,int *flags);int bdrv_parse_cache_mode (const char *mode,int *flags,bool *writethrough);block.c +0
-1@@ -4919,7 +4919,6 @@ static TransactionActionDrv bdrv_remove_backing_drv = { .commit = bdrv_child_free, }; -__attribute__((unused))static void bdrv_remove_backing (BlockDriverState *bs,GSList **tran) {if (!bs->backing) {
block.c static int bdrv_replace_node_noperm(BlockDriverState *from, · line 4971
Quoted 5 lines
block.c +2
-0@@ -4968,15 +4967,30 @@ static int bdrv_replace_node_noperm(BlockDriverState *from,* * With auto_skip=false the error is returned if from has a parent which should * not be updated. +* +* With detach_subchain to must be in a backing chain of from. In this case
@to and @from make it easier to read.
Quoted 6 lines
block.c +2
-1+* backing link of the cow-parent of @to is removed. */ static int bdrv_replace_node_common(BlockDriverState *from, BlockDriverState *to, -bool auto_skip,Error **errp) + bool auto_skip, bool detach_subchain,
block.c static int bdrv_replace_node_noperm(BlockDriverState *from, · line 4986
Quoted 10 lines
block.c +5
-0+ Error **errp) {int ret = -EPERM ;GSList *tran =NULL ;g_autoptr (GHashTable) found =NULL ;g_autoptr (GSList) refresh_list =NULL ; +BlockDriverState *to_cow_parent; + +if (detach_subchain) { +assert (bdrv_chain_contains (from, to));
The loop below also relies on from != to, so maybe assert that, too.
Quoted 11 lines
block.c +7
-0+for (to_cow_parent = from; +bdrv_filter_or_cow_bs (to_cow_parent) != to; + to_cow_parent =bdrv_filter_or_cow_bs (to_cow_parent)) + { + ; + } + }/* Make sure that @from doesn't go away until we have successfully attached * all of its parents to @to. */
block.c static int bdrv_replace_node_common(BlockDriverState *from, · line 5016
Quoted 6 lines
block.c +3
-0@@ -4997,6 +5011,10 @@ static int bdrv_replace_node_common(BlockDriverState *from, goto out; } +if (detach_subchain) { +bdrv_remove_backing (to_cow_parent, &tran); + }
So bdrv_drop_filter() only works for filters that go through bs->backing?
Wouldn't it have been more useful to make it bdrv_remove_filter_or_cow() like you use already use in other places in this patch?
If not, the limitation needs to be documented for bdrv_drop_filter().
Kevin
Re: [PATCH v2 25/36] block: introduce bdrv_drop_filter()
Quoted 92 lines · Kevin Wolf
Am 27.11.2020 um 15:45 hat Vladimir Sementsov-Ogievskiy geschrieben:
Quoted 43 lines
Using
bdrv_replace_node()for removing filter is not good enough: it keeps child reference of the filter, which may conflict with original top node during permission update.Instead let's create new interface, which will do all graph modifications first and then update permissions.
Let's modify
bdrv_replace_node_common(), allowing it additionally drop backing chain child link pointing to new node. This is quite appropriate forbdrv_drop_intermediate()and makes possible to add newbdrv_drop_filter()as a simple wrapper.Signed-off-by Vladimir Sementsov-Ogievskiy <[email protected]>--- include/block/block.h | 1 + block.c | 42 ++++++++++++++++++++++++++++++++++++++---- 2 files changed, 39 insertions(+), 4 deletions(-)include/block/block.h +1
-0@@ -348,6 +348,7 @@ int bdrv_append(BlockDriverState *bs_new, BlockDriverState *bs_top, Error **errp);int bdrv_replace_node (BlockDriverState *from,BlockDriverState *to,Error **errp); +int bdrv_drop_filter (BlockDriverState *bs,Error **errp);int bdrv_parse_aio (const char *mode,int *flags);int bdrv_parse_cache_mode (const char *mode,int *flags,bool *writethrough);block.c +0
-1@@ -4919,7 +4919,6 @@ static TransactionActionDrv bdrv_remove_backing_drv = { .commit = bdrv_child_free, }; -__attribute__((unused))static void bdrv_remove_backing (BlockDriverState *bs,GSList **tran) {if (!bs->backing) {block.c static int bdrv_replace_node_noperm(BlockDriverState *from, · line 4971
Quoted 5 lines
block.c +2
-0@@ -4968,15 +4967,30 @@ static int bdrv_replace_node_noperm(BlockDriverState *from,* * With auto_skip=false the error is returned if from has a parent which should * not be updated. +* +* With detach_subchain to must be in a backing chain of from. In this case @to and @from make it easier to read.
Quoted 6 lines
block.c +2
-1+* backing link of the cow-parent of @to is removed. */ static int bdrv_replace_node_common(BlockDriverState *from, BlockDriverState *to, -bool auto_skip,Error **errp) + bool auto_skip, bool detach_subchain,block.c static int bdrv_replace_node_noperm(BlockDriverState *from, · line 4986
Quoted 10 lines
block.c +5
-0+ Error **errp) {int ret = -EPERM ;GSList *tran =NULL ;g_autoptr (GHashTable) found =NULL ;g_autoptr (GSList) refresh_list =NULL ; +BlockDriverState *to_cow_parent; + +if (detach_subchain) { +assert (bdrv_chain_contains (from, to));The loop below also relies on from != to, so maybe assert that, too.
Quoted 11 lines
block.c +7
-0+for (to_cow_parent = from; +bdrv_filter_or_cow_bs (to_cow_parent) != to; + to_cow_parent =bdrv_filter_or_cow_bs (to_cow_parent)) + { + ; + } + }/* Make sure that @from doesn't go away until we have successfully attached * all of its parents to @to. */ block.c static int bdrv_replace_node_common(BlockDriverState *from, · line 5016
Quoted 6 lines
block.c +3
-0@@ -4997,6 +5011,10 @@ static int bdrv_replace_node_common(BlockDriverState *from, goto out; } +if (detach_subchain) { +bdrv_remove_backing (to_cow_parent, &tran); + }So
bdrv_drop_filter()only works for filters that go throughbs->backing?
Quoted 5 lines
Wouldn't it have been more useful to make it
bdrv_remove_filter_or_cow()like you use already use in other places in this patch?If not, the limitation needs to be documented for
bdrv_drop_filter().
bdrv_append supports only bs->backing based filters too.. So for now it's enough. And probably in future we'll refactor it all again when implement multi-reopen qmp command. Will look at it when prepare new version and either improve or document limitation.
Signature · 2 lines
Best regards, Vladimir
PATCH v2 26/36 block/backup-top: drop .active
In reply to Vladimir Sementsov-OgievskiyWe don't need this workaround anymore: bdrv_append is already smart enough and we can use new bdrv_drop_filter().
--- block/backup-top.c | 38 +------------------------------------- tests/qemu-iotests/283.out | 2 +- 2 files changed, 2 insertions(+), 38 deletions(-)
block/backup-top.c +1 -37
@@ -37,7 +37,6 @@typedef struct BDRVBackupTopState {BlockCopyState *bcs;BdrvChild *target; -bool active;int64_t cluster_size; }BDRVBackupTopState ;
@@ -127,21 +126,6 @@ static void backup_top_child_perm(BlockDriverState *bs, BdrvChild *c, uint64_t perm, uint64_t shared, uint64_t *nperm, uint64_t *nshared) { - BDRVBackupTopState *s = bs->opaque; - -if (!s->active) { -/* -* The filter node may be in process of bdrv_append(), which firstly do -* bdrv_set_backing_hd() and then bdrv_replace_node(). This means that -* we can't unshare BLK_PERM_WRITE during bdrv_append() operation. So, -* let's require nothing during bdrv_append() and refresh permissions -* after it (see bdrv_backup_top_append()). -*/ - *nperm =0 ; - *nshared =BLK_PERM_ALL ; -return ; - } - if (!(role & BDRV_CHILD_FILTERED)) {/* * Target child
@@ -229,18 +213,6 @@ BlockDriverState *bdrv_backup_top_append(BlockDriverState *source, } appended = true; -/* -* bdrv_append() finished successfully, now we can require permissions -* we want. -*/ - state->active = true; -bdrv_child_refresh_perms (top, top->backing, &local_err); -if (local_err) { -error_prepend (&local_err, -"Cannot set permissions for backup-top filter: " ); - goto fail; - } - state->cluster_size = cluster_size; state->bcs =block_copy_state_new (top->backing, state->target, cluster_size, write_flags, &local_err);
@@ -256,7 +228,6 @@ BlockDriverState *bdrv_backup_top_append(BlockDriverState *source, fail:if (appended) { - state->active = false;bdrv_backup_top_drop (top); }else {bdrv_unref (top);
@@ -272,16 +243,9 @@ void bdrv_backup_top_drop(BlockDriverState *bs) {BDRVBackupTopState *s = bs->opaque; -bdrv_drained_begin (bs); +bdrv_drop_filter (bs, &error_abort);block_copy_state_free (s->bcs); - s->active = false; -bdrv_child_refresh_perms (bs, bs->backing, &error_abort); -bdrv_replace_node (bs, bs->backing->bs, &error_abort); -bdrv_set_backing_hd (bs,NULL , &error_abort); - -bdrv_drained_end (bs); -bdrv_unref (bs); }
tests/qemu-iotests/283.out +1 -1
@@ -5,4 +5,4 @@ {"execute": "blockdev-add", "arguments": {"driver": "blkdebug", "image": "base", "node-name": "other", "take-child-perms": ["write"]}} {"return": {}} {"execute": "blockdev-backup", "arguments": {"device": "source", "sync": "full", "target": "target"}} -{"error": {"class": "GenericError", "desc": "Cannot set permissions for backup-top filter: Conflicts with use by source as 'image', which does not allow 'write' on base"}} +{"error": {"class": "GenericError", "desc": "Cannot append backup-top filter: Conflicts with use by source as 'image', which does not allow 'write' on base"}}
Signature · 1 line
2.21.3
Re: [PATCH v2 26/36] block/backup-top: drop .active
Am 27.11.2020 um 15:45 hat Vladimir Sementsov-Ogievskiy geschrieben:
Quoted 44 lines
We don't need this workaround anymore:
bdrv_appendis already smart enough and we can use newbdrv_drop_filter().Signed-off-by Vladimir Sementsov-Ogievskiy <[email protected]>--- block/backup-top.c | 38 +------------------------------------- tests/qemu-iotests/283.out | 2 +- 2 files changed, 2 insertions(+), 38 deletions(-)block/backup-top.c +0
-16@@ -37,7 +37,6 @@typedef struct BDRVBackupTopState {BlockCopyState *bcs;BdrvChild *target; -bool active;int64_t cluster_size; }BDRVBackupTopState ;@@ -127,21 +126,6 @@ static void backup_top_child_perm(BlockDriverState *bs, BdrvChild *c, uint64_t perm, uint64_t shared, uint64_t *nperm, uint64_t *nshared) { - BDRVBackupTopState *s = bs->opaque; - -if (!s->active) { -/* -* The filter node may be in process of bdrv_append(), which firstly do -* bdrv_set_backing_hd() and then bdrv_replace_node(). This means that -* we can't unshare BLK_PERM_WRITE during bdrv_append() operation. So, -* let's require nothing during bdrv_append() and refresh permissions -* after it (see bdrv_backup_top_append()). -*/ - *nperm =0 ; - *nshared =BLK_PERM_ALL ; -return ; - } - if (!(role & BDRV_CHILD_FILTERED)) {/* * Target child
block/backup-top.c BlockDriverState *bdrv_backup_top_append(BlockDriverState *source, · line 237
Quoted 9 lines
block/backup-top.c +0
-6@@ -229,18 +213,6 @@ BlockDriverState *bdrv_backup_top_append(BlockDriverState *source, } appended = true; -/* -* bdrv_append() finished successfully, now we can require permissions -* we want. -*/ - state->active = true; -bdrv_child_refresh_perms (top, top->backing, &local_err);
bdrv_append() uses bdrv_refresh_perms() for the whole node. Is it doing unnecessary extra work there and should really do the same as backup-top did here, i.e. bdrv_child_refresh_perms(bs_new->backing)?
(Really a comment for an earlier patch. This patch itself looks fine.)
Kevin
Re: [PATCH v2 26/36] block/backup-top: drop .active
Quoted 59 lines · Kevin Wolf
Am 27.11.2020 um 15:45 hat Vladimir Sementsov-Ogievskiy geschrieben:
Quoted 44 lines
We don't need this workaround anymore:
bdrv_appendis already smart enough and we can use newbdrv_drop_filter().Signed-off-by Vladimir Sementsov-Ogievskiy <[email protected]>--- block/backup-top.c | 38 +------------------------------------- tests/qemu-iotests/283.out | 2 +- 2 files changed, 2 insertions(+), 38 deletions(-)block/backup-top.c +0
-16@@ -37,7 +37,6 @@typedef struct BDRVBackupTopState {BlockCopyState *bcs;BdrvChild *target; -bool active;int64_t cluster_size; }BDRVBackupTopState ;@@ -127,21 +126,6 @@ static void backup_top_child_perm(BlockDriverState *bs, BdrvChild *c, uint64_t perm, uint64_t shared, uint64_t *nperm, uint64_t *nshared) { - BDRVBackupTopState *s = bs->opaque; - -if (!s->active) { -/* -* The filter node may be in process of bdrv_append(), which firstly do -* bdrv_set_backing_hd() and then bdrv_replace_node(). This means that -* we can't unshare BLK_PERM_WRITE during bdrv_append() operation. So, -* let's require nothing during bdrv_append() and refresh permissions -* after it (see bdrv_backup_top_append()). -*/ - *nperm =0 ; - *nshared =BLK_PERM_ALL ; -return ; - } - if (!(role & BDRV_CHILD_FILTERED)) {/* * Target child block/backup-top.c BlockDriverState *bdrv_backup_top_append(BlockDriverState *source, · line 237
Quoted 9 lines
block/backup-top.c +0
-6@@ -229,18 +213,6 @@ BlockDriverState *bdrv_backup_top_append(BlockDriverState *source, } appended = true; -/* -* bdrv_append() finished successfully, now we can require permissions -* we want. -*/ - state->active = true; -bdrv_child_refresh_perms (top, top->backing, &local_err);
bdrv_append()usesbdrv_refresh_perms()for the whole node. Is it doing unnecessary extra work there and should really do the same as backup-top did here, i.e.bdrv_child_refresh_perms(bs_new->backing)?
Quoted 2 lines
(Really a comment for an earlier patch. This patch itself looks fine.)
You mean how backup-top code works at the point when we modified bdrv_append()? Actually all works, as we use state->active. We set it to true and should call refresh_perms. Now we drop _refresh_perms _together_ with state->active variable, so filter is always "active", but new bdrv_append can handle it now. I.e., before this patch backup-top.c code is correct but over-complicated with logic which is not necessary after bdrv_append() improvement (and of-course we need also bdrv_drop_filter() to drop the whole state->active related logic).
Signature · 2 lines
Best regards, Vladimir
Re: [PATCH v2 26/36] block/backup-top: drop .active
Am 04.02.2021 um 13:33 hat Vladimir Sementsov-Ogievskiy geschrieben:
Quoted 61 lines
Quoted 57 lines · Kevin Wolf
Am 27.11.2020 um 15:45 hat Vladimir Sementsov-Ogievskiy geschrieben:
Quoted 43 lines
We don't need this workaround anymore:
bdrv_appendis already smart enough and we can use newbdrv_drop_filter().Signed-off-by Vladimir Sementsov-Ogievskiy <[email protected]>--- block/backup-top.c | 38 +------------------------------------- tests/qemu-iotests/283.out | 2 +- 2 files changed, 2 insertions(+), 38 deletions(-)block/backup-top.c +0
-16@@ -37,7 +37,6 @@typedef struct BDRVBackupTopState {BlockCopyState *bcs;BdrvChild *target; -bool active;int64_t cluster_size; }BDRVBackupTopState ;@@ -127,21 +126,6 @@ static void backup_top_child_perm(BlockDriverState *bs, BdrvChild *c, uint64_t perm, uint64_t shared, uint64_t *nperm, uint64_t *nshared) { - BDRVBackupTopState *s = bs->opaque; - -if (!s->active) { -/* -* The filter node may be in process of bdrv_append(), which firstly do -* bdrv_set_backing_hd() and then bdrv_replace_node(). This means that -* we can't unshare BLK_PERM_WRITE during bdrv_append() operation. So, -* let's require nothing during bdrv_append() and refresh permissions -* after it (see bdrv_backup_top_append()). -*/ - *nperm =0 ; - *nshared =BLK_PERM_ALL ; -return ; - } - if (!(role & BDRV_CHILD_FILTERED)) {/* * Target child block/backup-top.c BlockDriverState *bdrv_backup_top_append(BlockDriverState *source, · line 236
Quoted 8 lines
block/backup-top.c +0
-6@@ -229,18 +213,6 @@ BlockDriverState *bdrv_backup_top_append(BlockDriverState *source, } appended = true; -/* -* bdrv_append() finished successfully, now we can require permissions -* we want. -*/ - state->active = true; -bdrv_child_refresh_perms (top, top->backing, &local_err);
bdrv_append()usesbdrv_refresh_perms()for the whole node. Is it doing unnecessary extra work there and should really do the same as backup-top did here, i.e.bdrv_child_refresh_perms(bs_new->backing)?Quoted 2 lines
(Really a comment for an earlier patch. This patch itself looks fine.)
Quoted 9 lines
You mean how backup-top code works at the point when we modified
bdrv_append()? Actually all works, as we usestate->active. We set it to true and should callrefresh_perms. Now we drop_refresh_perms_together_ withstate->activevariable, so filter is always "active", but newbdrv_appendcan handle it now. I.e., before this patchbackup-top.ccode is correct but over-complicated with logic which is not necessary afterbdrv_append()improvement (and of-course we need alsobdrv_drop_filter()to drop the wholestate->activerelated logic).
No, I just mean that bdrv_child_refresh_perms(bs, bs->backing) is enough when adding a new image to the chain. A full bdrv_child_refresh_perms() like we now have in bdrv_append() is doing more work than is necessary.
It doesn't make a difference for backup-top (because the filter has only a single child), but if you append a new qcow2 snapshot, you would also recalculate permissions for the bs->file subtree even though nothing has changed there.
It's only a small detail anyway, not very important in a slow path.
Kevin
Re: [PATCH v2 26/36] block/backup-top: drop .active
Quoted 76 lines · Kevin Wolf
Am 04.02.2021 um 13:33 hat Vladimir Sementsov-Ogievskiy geschrieben:
Quoted 61 lines
Quoted 57 lines · Kevin Wolf
Am 27.11.2020 um 15:45 hat Vladimir Sementsov-Ogievskiy geschrieben:
Quoted 43 lines
We don't need this workaround anymore:
bdrv_appendis already smart enough and we can use newbdrv_drop_filter().Signed-off-by Vladimir Sementsov-Ogievskiy <[email protected]>--- block/backup-top.c | 38 +------------------------------------- tests/qemu-iotests/283.out | 2 +- 2 files changed, 2 insertions(+), 38 deletions(-)block/backup-top.c +0
-16@@ -37,7 +37,6 @@typedef struct BDRVBackupTopState {BlockCopyState *bcs;BdrvChild *target; -bool active;int64_t cluster_size; }BDRVBackupTopState ;@@ -127,21 +126,6 @@ static void backup_top_child_perm(BlockDriverState *bs, BdrvChild *c, uint64_t perm, uint64_t shared, uint64_t *nperm, uint64_t *nshared) { - BDRVBackupTopState *s = bs->opaque; - -if (!s->active) { -/* -* The filter node may be in process of bdrv_append(), which firstly do -* bdrv_set_backing_hd() and then bdrv_replace_node(). This means that -* we can't unshare BLK_PERM_WRITE during bdrv_append() operation. So, -* let's require nothing during bdrv_append() and refresh permissions -* after it (see bdrv_backup_top_append()). -*/ - *nperm =0 ; - *nshared =BLK_PERM_ALL ; -return ; - } - if (!(role & BDRV_CHILD_FILTERED)) {/* * Target child block/backup-top.c BlockDriverState *bdrv_backup_top_append(BlockDriverState *source, · line 236
Quoted 8 lines
block/backup-top.c +0
-6@@ -229,18 +213,6 @@ BlockDriverState *bdrv_backup_top_append(BlockDriverState *source, } appended = true; -/* -* bdrv_append() finished successfully, now we can require permissions -* we want. -*/ - state->active = true; -bdrv_child_refresh_perms (top, top->backing, &local_err);
bdrv_append()usesbdrv_refresh_perms()for the whole node. Is it doing unnecessary extra work there and should really do the same as backup-top did here, i.e.bdrv_child_refresh_perms(bs_new->backing)?Quoted 2 lines
(Really a comment for an earlier patch. This patch itself looks fine.)
Quoted 9 lines
You mean how backup-top code works at the point when we modified
bdrv_append()? Actually all works, as we usestate->active. We set it to true and should callrefresh_perms. Now we drop_refresh_perms_together_ withstate->activevariable, so filter is always "active", but newbdrv_appendcan handle it now. I.e., before this patchbackup-top.ccode is correct but over-complicated with logic which is not necessary afterbdrv_append()improvement (and of-course we need alsobdrv_drop_filter()to drop the wholestate->activerelated logic).No, I just mean that
bdrv_child_refresh_perms(bs, bs->backing)is enough when adding a new image to the chain. A fullbdrv_child_refresh_perms()like we now have inbdrv_append()is doing more work than is necessary.
Quoted 7 lines
It doesn't make a difference for backup-top (because the filter has only a single child), but if you append a new qcow2 snapshot, you would also recalculate permissions for the
bs->filesubtree even though nothing has changed there.It's only a small detail anyway, not very important in a slow path.
Understand now. I think bdrv_append() do correct things: bs_new gets new parents, so we refresh the whole subtree.. So for appending qcow2 we should refresh its file child as well. Probably new permissions of new bs_new parents will influence what qcow2 wants to do with it file node..
Signature · 2 lines
Best regards, Vladimir
Re: [PATCH v2 26/36] block/backup-top: drop .active
Am 04.02.2021 um 14:46 hat Vladimir Sementsov-Ogievskiy geschrieben:
Quoted 82 lines
Quoted 81 lines · Kevin Wolf
Am 04.02.2021 um 13:33 hat Vladimir Sementsov-Ogievskiy geschrieben:
Quoted 61 lines
Quoted 57 lines · Kevin Wolf
Am 27.11.2020 um 15:45 hat Vladimir Sementsov-Ogievskiy geschrieben:
Quoted 43 lines
We don't need this workaround anymore:
bdrv_appendis already smart enough and we can use newbdrv_drop_filter().Signed-off-by Vladimir Sementsov-Ogievskiy <[email protected]>--- block/backup-top.c | 38 +------------------------------------- tests/qemu-iotests/283.out | 2 +- 2 files changed, 2 insertions(+), 38 deletions(-)block/backup-top.c +0
-16@@ -37,7 +37,6 @@typedef struct BDRVBackupTopState {BlockCopyState *bcs;BdrvChild *target; -bool active;int64_t cluster_size; }BDRVBackupTopState ;@@ -127,21 +126,6 @@ static void backup_top_child_perm(BlockDriverState *bs, BdrvChild *c, uint64_t perm, uint64_t shared, uint64_t *nperm, uint64_t *nshared) { - BDRVBackupTopState *s = bs->opaque; - -if (!s->active) { -/* -* The filter node may be in process of bdrv_append(), which firstly do -* bdrv_set_backing_hd() and then bdrv_replace_node(). This means that -* we can't unshare BLK_PERM_WRITE during bdrv_append() operation. So, -* let's require nothing during bdrv_append() and refresh permissions -* after it (see bdrv_backup_top_append()). -*/ - *nperm =0 ; - *nshared =BLK_PERM_ALL ; -return ; - } - if (!(role & BDRV_CHILD_FILTERED)) {/* * Target child block/backup-top.c BlockDriverState *bdrv_backup_top_append(BlockDriverState *source, · line 236
Quoted 8 lines
block/backup-top.c +0
-6@@ -229,18 +213,6 @@ BlockDriverState *bdrv_backup_top_append(BlockDriverState *source, } appended = true; -/* -* bdrv_append() finished successfully, now we can require permissions -* we want. -*/ - state->active = true; -bdrv_child_refresh_perms (top, top->backing, &local_err);
bdrv_append()usesbdrv_refresh_perms()for the whole node. Is it doing unnecessary extra work there and should really do the same as backup-top did here, i.e.bdrv_child_refresh_perms(bs_new->backing)?Quoted 2 lines
(Really a comment for an earlier patch. This patch itself looks fine.)
Quoted 9 lines
You mean how backup-top code works at the point when we modified
bdrv_append()? Actually all works, as we usestate->active. We set it to true and should callrefresh_perms. Now we drop_refresh_perms_together_ withstate->activevariable, so filter is always "active", but newbdrv_appendcan handle it now. I.e., before this patchbackup-top.ccode is correct but over-complicated with logic which is not necessary afterbdrv_append()improvement (and of-course we need alsobdrv_drop_filter()to drop the wholestate->activerelated logic).No, I just mean that
bdrv_child_refresh_perms(bs, bs->backing)is enough when adding a new image to the chain. A fullbdrv_child_refresh_perms()like we now have inbdrv_append()is doing more work than is necessary.It doesn't make a difference for backup-top (because the filter has only a single child), but if you append a new qcow2 snapshot, you would also recalculate permissions for the
bs->filesubtree even though nothing has changed there.
Quoted 7 lines
Quoted 1 line
It's only a small detail anyway, not very important in a slow path.
Understand now. I think
bdrv_append()do correct things:bs_newgets new parents, so we refresh the whole subtree.. So for appending qcow2 we should refresh its file child as well. Probably new permissions of newbs_newparents will influence what qcow2 wants to do with it file node..
You mean the parents that move from bs_top to bs_new and that they could change the permissions that bs_new needs?
Good point, yes.
Kevin
PATCH v2 27/36 block: drop ignore_children for permission update functions
In reply to Vladimir Sementsov-OgievskiyThis argument is always NULL. Drop it.
--- block.c | 36 +++++++++++------------------------- 1 file changed, 11 insertions(+), 25 deletions(-)
block.c +11 -25
@@ -1934,7 +1934,6 @@ static int bdrv_fill_options(QDict **options, const char *filename,static int bdrv_check_update_perm (BlockDriverState *bs,BlockReopenQueue *q,uint64_t new_used_perm,uint64_t new_shared_perm, -GSList *ignore_children,Error **errp); typedef struct BlockReopenQueueEntry {
@@ -2011,9 +2010,7 @@ static bool bdrv_a_allow_b(BdrvChild *a, BdrvChild *b, Error **errp)return false; } -static bool bdrv_check_parents_compliance (BlockDriverState *bs, -GSList *ignore_children, -Error **errp) +static bool bdrv_check_parents_compliance (BlockDriverState *bs,Error **errp) {BdrvChild *a, *b;
@@ -2024,9 +2021,7 @@ static bool bdrv_check_parents_compliance(BlockDriverState *bs,*/ QLIST_FOREACH(a, &bs->parents, next_parent) { QLIST_FOREACH(b, &bs->parents, next_parent) { -if (a == b ||g_slist_find (ignore_children, a) || -g_slist_find (ignore_children, b)) - { +if (a == b) {continue ; }
@@ -2243,7 +2238,6 @@ static void bdrv_replace_child_safe(BdrvChild *child, BlockDriverState *new_bs,static int bdrv_node_check_perm (BlockDriverState *bs,BlockReopenQueue *q,uint64_t cumulative_perms,uint64_t cumulative_shared_perms, -GSList *ignore_children,GSList **tran,Error **errp) {BlockDriver *drv = bs->drv;
@@ -2326,7 +2320,6 @@ static int bdrv_check_perm_common(GSList *list, BlockReopenQueue *q,bool use_cumulative_perms, uint64_t cumulative_perms, uint64_t cumulative_shared_perms, - GSList *ignore_children, GSList **tran, Error **errp) { int ret;
@@ -2337,7 +2330,7 @@ static int bdrv_check_perm_common(GSList *list, BlockReopenQueue *q, ret =bdrv_node_check_perm (bs, q, cumulative_perms, cumulative_shared_perms, - ignore_children, tran, errp); + tran, errp);if (ret <0 ) {return ret; }
@@ -2348,7 +2341,7 @@ static int bdrv_check_perm_common(GSList *list, BlockReopenQueue *q,for ( ; list; list = list->next) { bs = list->data; -if (!bdrv_check_parents_compliance (bs, ignore_children, errp)) { +if (!bdrv_check_parents_compliance (bs, errp)) {return -EINVAL ; }
@@ -2357,7 +2350,7 @@ static int bdrv_check_perm_common(GSList *list, BlockReopenQueue *q, ret =bdrv_node_check_perm (bs, q, cumulative_perms, cumulative_shared_perms, - ignore_children, tran, errp); + tran, errp);if (ret <0 ) {return ret; }
@@ -2368,19 +2361,17 @@ static int bdrv_check_perm_common(GSList *list, BlockReopenQueue *q,static int bdrv_check_perm (BlockDriverState *bs,BlockReopenQueue *q,uint64_t cumulative_perms, -uint64_t cumulative_shared_perms, -GSList *ignore_children,Error **errp) +uint64_t cumulative_shared_perms,Error **errp) {g_autoptr (GSList) list =bdrv_topological_dfs (NULL ,NULL , bs);return bdrv_check_perm_common (list, q, true, cumulative_perms, - cumulative_shared_perms, ignore_children, -NULL , errp); + cumulative_shared_perms,NULL , errp); }static int bdrv_list_refresh_perms (GSList *list,BlockReopenQueue *q,GSList **tran,Error **errp) { -return bdrv_check_perm_common (list, q, false,0 ,0 ,NULL , tran, errp); +return bdrv_check_perm_common (list, q, false,0 ,0 , tran, errp); }/*
@@ -2509,7 +2500,6 @@ char *bdrv_perm_names(uint64_t perm)static int bdrv_check_update_perm (BlockDriverState *bs,BlockReopenQueue *q,uint64_t new_used_perm,uint64_t new_shared_perm, -GSList *ignore_children,Error **errp) {BdrvChild *c;
@@ -2521,10 +2511,6 @@ static int bdrv_check_update_perm(BlockDriverState *bs, BlockReopenQueue *q,assert (new_shared_perm &BLK_PERM_WRITE_UNCHANGED ); QLIST_FOREACH(c, &bs->parents, next_parent) { -if (g_slist_find (ignore_children, c)) { -continue ; - } -if ((new_used_perm & c->shared_perm) != new_used_perm) {char *user =bdrv_child_user_desc (c);char *perm_names =bdrv_perm_names (new_used_perm & ~c->shared_perm);
@@ -2554,7 +2540,7 @@ static int bdrv_check_update_perm(BlockDriverState *bs, BlockReopenQueue *q, }return bdrv_check_perm (bs, q, cumulative_perms, cumulative_shared_perms, - ignore_children, errp); + errp); }static int bdrv_refresh_perms (BlockDriverState *bs,Error **errp)
@@ -4149,7 +4135,7 @@ int bdrv_reopen_multiple(BlockReopenQueue *bs_queue, Error **errp) QTAILQ_FOREACH(bs_entry, bs_queue, entry) {BDRVReopenState *state = &bs_entry->state; ret =bdrv_check_perm (state->bs, bs_queue, state->perm, - state->shared_perm,NULL , errp); + state->shared_perm, errp);if (ret <0 ) { goto cleanup_perm; }
@@ -4161,7 +4147,7 @@ int bdrv_reopen_multiple(BlockReopenQueue *bs_queue, Error **errp) bs_queue, state->perm, state->shared_perm, &nperm, &nshared); ret =bdrv_check_update_perm (state->new_backing_bs,NULL , - nperm, nshared,NULL , errp); + nperm, nshared, errp);if (ret <0 ) { goto cleanup_perm; }
Signature · 1 line
2.21.3
PATCH v2 28/36 block: add bdrv_set_backing_noperm() transaction action
In reply to Vladimir Sementsov-OgievskiySplit out no-perm part of bdrv_set_backing_hd() as a separate transaction action. Note the in case of existing BdrvChild we reuse it, not recreate, just to do less actions.
--- block.c | 111 +++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 89 insertions(+), 22 deletions(-)
block.c +89 -22
@@ -101,6 +101,7 @@ static int bdrv_attach_child_common(BlockDriverState *child_bs, uint64_t perm, uint64_t shared_perm, void *opaque, BdrvChild **child, GSList **tran, Error **errp); +static void bdrv_remove_backing (BlockDriverState *bs,GSList **tran);static int bdrv_reopen_prepare (BDRVReopenState *reopen_state,BlockReopenQueue *queue,Error **errp);
@@ -3194,45 +3195,111 @@ static BdrvChildRole bdrv_backing_role(BlockDriverState *bs) } } +typedef struct BdrvSetBackingNoPermState { +BlockDriverState *bs; +BlockDriverState *backing_bs; +BlockDriverState *old_inherits_from; +GSList *attach_tran; +}BdrvSetBackingNoPermState ; + +static void bdrv_set_backing_noperm_abort (void *opaque) +{ +BdrvSetBackingNoPermState *s = opaque; + +if (s->backing_bs) { + s->backing_bs->inherits_from = s->old_inherits_from; + } + +tran_abort (s->attach_tran); + +bdrv_refresh_limits (s->bs,NULL ); +if (s->old_inherits_from) { +bdrv_refresh_limits (s->old_inherits_from,NULL ); + } +} + +static void bdrv_set_backing_noperm_commit (void *opaque) +{ +BdrvSetBackingNoPermState *s = opaque; + +tran_commit (s->attach_tran); +} + +static TransactionActionDrv bdrv_set_backing_noperm_drv = { + .abort = bdrv_set_backing_noperm_abort, + .commit = bdrv_set_backing_noperm_commit, + .clean = g_free, +}; +/* * Sets the bs->backing link of a BDS. A new reference is created; callers * which don't need their own reference any more must call bdrv_unref(). */ -void bdrv_set_backing_hd (BlockDriverState *bs,BlockDriverState *backing_hd, -Error **errp) +static int bdrv_set_backing_noperm (BlockDriverState *bs, +BlockDriverState *backing_bs, +GSList **tran,Error **errp) { -bool update_inherits_from =bdrv_chain_contains (bs, backing_hd) && -bdrv_inherits_from_recursive (backing_hd, bs); +int ret =0 ; +bool update_inherits_from =bdrv_chain_contains (bs, backing_bs) && +bdrv_inherits_from_recursive (backing_bs, bs); +GSList *attach_tran =NULL ; +BdrvSetBackingNoPermState *s;if (bdrv_is_backing_chain_frozen (bs,child_bs (bs->backing), errp)) { -return ; +return -EPERM ; } -if (backing_hd) { -bdrv_ref (backing_hd); +if (bs->backing && backing_bs) { +bdrv_replace_child_safe (bs->backing, backing_bs, tran); + }else if (bs->backing && !backing_bs) { +bdrv_remove_backing (bs, tran); + }else if (backing_bs) { +assert (!bs->backing); + ret =bdrv_attach_child_noperm (bs, backing_bs,"backing" , + &child_of_bds,bdrv_backing_role (bs), + &bs->backing, &attach_tran, errp); +if (ret <0 ) { +tran_abort (attach_tran); +return ret; + } } -if (bs->backing) { -/* Cannot be frozen, we checked that above */ -bdrv_unref_child (bs, bs->backing); - bs->backing =NULL ; - } + s =g_new (BdrvSetBackingNoPermState,1 ); + *s = (BdrvSetBackingNoPermState ) { + .bs = bs, + .backing_bs = backing_bs, + .old_inherits_from = backing_bs ? backing_bs->inherits_from :NULL , + }; +tran_prepend (tran, &bdrv_set_backing_noperm_drv, s); -if (!backing_hd) { - goto out; +/* +* If backing_bs was already part of bs's backing chain, and +* inherits_from pointed recursively to bs then let's update it to +* point directly to bs (else it will become NULL). +*/ +if (backing_bs && update_inherits_from) { + backing_bs->inherits_from = bs; } - bs->backing =bdrv_attach_child (bs, backing_hd,"backing" , &child_of_bds, -bdrv_backing_role (bs), errp); -/* If backing_hd was already part of bs's backing chain, and -* inherits_from pointed recursively to bs then let's update it to -* point directly to bs (else it will become NULL). */ -if (bs->backing && update_inherits_from) { - backing_hd->inherits_from = bs; +bdrv_refresh_limits (bs,NULL ); + +return 0 ; +} + +void bdrv_set_backing_hd (BlockDriverState *bs,BlockDriverState *backing_hd, +Error **errp) +{ +int ret; +GSList *tran =NULL ; + + ret =bdrv_set_backing_noperm (bs, backing_hd, &tran, errp); +if (ret <0 ) { + goto out; } + ret =bdrv_refresh_perms (bs, errp); out: -bdrv_refresh_limits (bs,NULL ); +tran_finalize (tran, ret); }/*
Signature · 1 line
2.21.3
Re: [PATCH v2 28/36] block: add bdrv_set_backing_noperm() transaction action
Am 27.11.2020 um 15:45 hat Vladimir Sementsov-Ogievskiy geschrieben:
Quoted 22 lines
Split out no-perm part of
bdrv_set_backing_hd()as a separate transaction action. Note the in case of existing BdrvChild we reuse it, not recreate, just to do less actions.Signed-off-by Vladimir Sementsov-Ogievskiy <[email protected]>--- block.c | 111 +++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 89 insertions(+), 22 deletions(-)block.c +1
-0@@ -101,6 +101,7 @@ static int bdrv_attach_child_common(BlockDriverState *child_bs, uint64_t perm, uint64_t shared_perm, void *opaque, BdrvChild **child, GSList **tran, Error **errp); +static void bdrv_remove_backing (BlockDriverState *bs,GSList **tran);static int bdrv_reopen_prepare (BDRVReopenState *reopen_state,BlockReopenQueue *queue,Error **errp);
block.c static BdrvChildRole bdrv_backing_role(BlockDriverState *bs) · line 3203
Quoted 9 lines
block.c +6
-0@@ -3194,45 +3195,111 @@ static BdrvChildRole bdrv_backing_role(BlockDriverState *bs) } } +typedef struct BdrvSetBackingNoPermState { +BlockDriverState *bs; +BlockDriverState *backing_bs; +BlockDriverState *old_inherits_from; +GSList *attach_tran; +}BdrvSetBackingNoPermState ;
Why do we need the nested attach_tran instead of just including these actions in the outer transaction?
Quoted 4 lines
block.c +4
-0+static void bdrv_set_backing_noperm_abort (void *opaque) +{ +BdrvSetBackingNoPermState *s = opaque; +
block.c static BdrvChildRole bdrv_backing_role(BlockDriverState *bs) · line 3218
Quoted 10 lines
block.c +10
-0+if (s->backing_bs) { + s->backing_bs->inherits_from = s->old_inherits_from; + } + +tran_abort (s->attach_tran); + +bdrv_refresh_limits (s->bs,NULL ); +if (s->old_inherits_from) { +bdrv_refresh_limits (s->old_inherits_from,NULL ); + }
How is bs->inherits_from related to limits? I don't see a bdrv_refresh_limits() call in bdrv_set_backing_noperm() that this would undo.
Quoted 41 lines
block.c +25
-7+} + +static void bdrv_set_backing_noperm_commit (void *opaque) +{ +BdrvSetBackingNoPermState *s = opaque; + +tran_commit (s->attach_tran); +} + +static TransactionActionDrv bdrv_set_backing_noperm_drv = { + .abort = bdrv_set_backing_noperm_abort, + .commit = bdrv_set_backing_noperm_commit, + .clean = g_free, +}; +/* * Sets the bs->backing link of a BDS. A new reference is created; callers * which don't need their own reference any more must call bdrv_unref(). */ -void bdrv_set_backing_hd (BlockDriverState *bs,BlockDriverState *backing_hd, -Error **errp) +static int bdrv_set_backing_noperm (BlockDriverState *bs, +BlockDriverState *backing_bs, +GSList **tran,Error **errp) { -bool update_inherits_from =bdrv_chain_contains (bs, backing_hd) && -bdrv_inherits_from_recursive (backing_hd, bs); +int ret =0 ; +bool update_inherits_from =bdrv_chain_contains (bs, backing_bs) && +bdrv_inherits_from_recursive (backing_bs, bs); +GSList *attach_tran =NULL ; +BdrvSetBackingNoPermState *s;if (bdrv_is_backing_chain_frozen (bs,child_bs (bs->backing), errp)) { -return ; +return -EPERM ; } -if (backing_hd) { -bdrv_ref (backing_hd); +if (bs->backing && backing_bs) {
block.c static BdrvChildRole bdrv_backing_role(BlockDriverState *bs) · line 3262
Quoted 10 lines
block.c +10
-0+bdrv_replace_child_safe (bs->backing, backing_bs, tran); + }else if (bs->backing && !backing_bs) { +bdrv_remove_backing (bs, tran); + }else if (backing_bs) { +assert (!bs->backing); + ret =bdrv_attach_child_noperm (bs, backing_bs,"backing" , + &child_of_bds,bdrv_backing_role (bs), + &bs->backing, &attach_tran, errp); +if (ret <0 ) { +tran_abort (attach_tran);
This looks wrong to me, we'll call tran_abort() a second time through bdrv_set_backing_noperm_abort() when the outer transaction aborts.
I also notice that the other two if branches do just add things to the outer 'tran', it's just this branch that gets a nested one.
Quoted 13 lines
block.c +6
-5+return ret; + } } -if (bs->backing) { -/* Cannot be frozen, we checked that above */ -bdrv_unref_child (bs, bs->backing); - bs->backing =NULL ; - } + s =g_new (BdrvSetBackingNoPermState,1 ); + *s = (BdrvSetBackingNoPermState ) { + .bs = bs, + .backing_bs = backing_bs,
block.c static BdrvChildRole bdrv_backing_role(BlockDriverState *bs) · line 3278
Quoted 10 lines
block.c +7
-2+ .old_inherits_from = backing_bs ? backing_bs->inherits_from : NULL, + }; +tran_prepend (tran, &bdrv_set_backing_noperm_drv, s); -if (!backing_hd) { - goto out; +/* +* If backing_bs was already part of bs's backing chain, and +* inherits_from pointed recursively to bs then let's update it to +* point directly to bs (else it will become NULL).
Setting it to NULL was previously done by bdrv_unref_child().
bdrv_replace_child_safe() and bdrv_remove_backing() don't seem to do this any more.
Quoted 6 lines
block.c +3
-1+*/ +if (backing_bs && update_inherits_from) { + backing_bs->inherits_from = bs; } - bs->backing = bdrv_attach_child(bs, backing_hd, "backing", &child_of_bds,
block.c static BdrvChildRole bdrv_backing_role(BlockDriverState *bs) · line 3287
Quoted 10 lines
block.c +4
-6- bdrv_backing_role(bs), errp); -/* If backing_hd was already part of bs's backing chain, and -* inherits_from pointed recursively to bs then let's update it to -* point directly to bs (else it will become NULL). */ -if (bs->backing && update_inherits_from) { - backing_hd->inherits_from = bs; +bdrv_refresh_limits (bs,NULL ); + +return 0 ; +}
Kevin
Re: [PATCH v2 28/36] block: add bdrv_set_backing_noperm() transaction action
Quoted 137 lines · Kevin Wolf
Am 27.11.2020 um 15:45 hat Vladimir Sementsov-Ogievskiy geschrieben:
Quoted 22 lines
Split out no-perm part of
bdrv_set_backing_hd()as a separate transaction action. Note the in case of existing BdrvChild we reuse it, not recreate, just to do less actions.Signed-off-by Vladimir Sementsov-Ogievskiy <[email protected]>--- block.c | 111 +++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 89 insertions(+), 22 deletions(-)block.c +1
-0@@ -101,6 +101,7 @@ static int bdrv_attach_child_common(BlockDriverState *child_bs, uint64_t perm, uint64_t shared_perm, void *opaque, BdrvChild **child, GSList **tran, Error **errp); +static void bdrv_remove_backing (BlockDriverState *bs,GSList **tran);static int bdrv_reopen_prepare (BDRVReopenState *reopen_state,BlockReopenQueue *queue,Error **errp);block.c static BdrvChildRole bdrv_backing_role(BlockDriverState *bs) · line 3203
Quoted 9 lines
block.c +6
-0@@ -3194,45 +3195,111 @@ static BdrvChildRole bdrv_backing_role(BlockDriverState *bs) } } +typedef struct BdrvSetBackingNoPermState { +BlockDriverState *bs; +BlockDriverState *backing_bs; +BlockDriverState *old_inherits_from; +GSList *attach_tran; +}BdrvSetBackingNoPermState ;Why do we need the nested
attach_traninstead of just including these actions in the outer transaction?Quoted 4 lines
block.c +4
-0+static void bdrv_set_backing_noperm_abort (void *opaque) +{ +BdrvSetBackingNoPermState *s = opaque; +block.c static BdrvChildRole bdrv_backing_role(BlockDriverState *bs) · line 3218
Quoted 10 lines
block.c +10
-0+if (s->backing_bs) { + s->backing_bs->inherits_from = s->old_inherits_from; + } + +tran_abort (s->attach_tran); + +bdrv_refresh_limits (s->bs,NULL ); +if (s->old_inherits_from) { +bdrv_refresh_limits (s->old_inherits_from,NULL ); + }How is
bs->inherits_fromrelated to limits? I don't see abdrv_refresh_limits()call inbdrv_set_backing_noperm()that this would undo.Quoted 41 lines
block.c +25
-7+} + +static void bdrv_set_backing_noperm_commit (void *opaque) +{ +BdrvSetBackingNoPermState *s = opaque; + +tran_commit (s->attach_tran); +} + +static TransactionActionDrv bdrv_set_backing_noperm_drv = { + .abort = bdrv_set_backing_noperm_abort, + .commit = bdrv_set_backing_noperm_commit, + .clean = g_free, +}; +/* * Sets the bs->backing link of a BDS. A new reference is created; callers * which don't need their own reference any more must call bdrv_unref(). */ -void bdrv_set_backing_hd (BlockDriverState *bs,BlockDriverState *backing_hd, -Error **errp) +static int bdrv_set_backing_noperm (BlockDriverState *bs, +BlockDriverState *backing_bs, +GSList **tran,Error **errp) { -bool update_inherits_from =bdrv_chain_contains (bs, backing_hd) && -bdrv_inherits_from_recursive (backing_hd, bs); +int ret =0 ; +bool update_inherits_from =bdrv_chain_contains (bs, backing_bs) && +bdrv_inherits_from_recursive (backing_bs, bs); +GSList *attach_tran =NULL ; +BdrvSetBackingNoPermState *s;if (bdrv_is_backing_chain_frozen (bs,child_bs (bs->backing), errp)) { -return ; +return -EPERM ; } -if (backing_hd) { -bdrv_ref (backing_hd); +if (bs->backing && backing_bs) {block.c static BdrvChildRole bdrv_backing_role(BlockDriverState *bs) · line 3262
Quoted 10 lines
block.c +10
-0+bdrv_replace_child_safe (bs->backing, backing_bs, tran); + }else if (bs->backing && !backing_bs) { +bdrv_remove_backing (bs, tran); + }else if (backing_bs) { +assert (!bs->backing); + ret =bdrv_attach_child_noperm (bs, backing_bs,"backing" , + &child_of_bds,bdrv_backing_role (bs), + &bs->backing, &attach_tran, errp); +if (ret <0 ) { +tran_abort (attach_tran);This looks wrong to me, we'll call
tran_abort()a second time throughbdrv_set_backing_noperm_abort()when the outer transaction aborts.I also notice that the other two if branches do just add things to the outer 'tran', it's just this branch that gets a nested one.
Quoted 13 lines
block.c +6
-5+return ret; + } } -if (bs->backing) { -/* Cannot be frozen, we checked that above */ -bdrv_unref_child (bs, bs->backing); - bs->backing =NULL ; - } + s =g_new (BdrvSetBackingNoPermState,1 ); + *s = (BdrvSetBackingNoPermState ) { + .bs = bs, + .backing_bs = backing_bs,block.c static BdrvChildRole bdrv_backing_role(BlockDriverState *bs) · line 3278
Quoted 10 lines
block.c +7
-2+ .old_inherits_from = backing_bs ? backing_bs->inherits_from : NULL, + }; +tran_prepend (tran, &bdrv_set_backing_noperm_drv, s); -if (!backing_hd) { - goto out; +/* +* If backing_bs was already part of bs's backing chain, and +* inherits_from pointed recursively to bs then let's update it to +* point directly to bs (else it will become NULL).
Quoted 4 lines
Setting it to NULL was previously done by
bdrv_unref_child().
bdrv_replace_child_safe()andbdrv_remove_backing()don't seem to do this any more.
Hmm, yes.. May be we should move bdrv_unset_inherts_from() from bdrv_unref_child() to bdrv_replace_child_noperm() ?
Quoted 20 lines
Quoted 7 lines
block.c +3
-1+*/ +if (backing_bs && update_inherits_from) { + backing_bs->inherits_from = bs; } - bs->backing = bdrv_attach_child(bs, backing_hd, "backing", &child_of_bds,block.c static BdrvChildRole bdrv_backing_role(BlockDriverState *bs) · line 3287
Quoted 10 lines
block.c +4
-6- bdrv_backing_role(bs), errp); -/* If backing_hd was already part of bs's backing chain, and -* inherits_from pointed recursively to bs then let's update it to -* point directly to bs (else it will become NULL). */ -if (bs->backing && update_inherits_from) { - backing_hd->inherits_from = bs; +bdrv_refresh_limits (bs,NULL ); + +return 0 ; +}Kevin
Signature · 2 lines
Best regards, Vladimir
Re: [PATCH v2 28/36] block: add bdrv_set_backing_noperm() transaction action
Am 05.02.2021 um 17:06 hat Vladimir Sementsov-Ogievskiy geschrieben:
Quoted 132 lines
Quoted 131 lines · Kevin Wolf
Am 27.11.2020 um 15:45 hat Vladimir Sementsov-Ogievskiy geschrieben:
Quoted 21 lines
Split out no-perm part of
bdrv_set_backing_hd()as a separate transaction action. Note the in case of existing BdrvChild we reuse it, not recreate, just to do less actions.Signed-off-by Vladimir Sementsov-Ogievskiy <[email protected]>--- block.c | 111 +++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 89 insertions(+), 22 deletions(-)block.c +1
-0@@ -101,6 +101,7 @@ static int bdrv_attach_child_common(BlockDriverState *child_bs, uint64_t perm, uint64_t shared_perm, void *opaque, BdrvChild **child, GSList **tran, Error **errp); +static void bdrv_remove_backing (BlockDriverState *bs,GSList **tran);static int bdrv_reopen_prepare (BDRVReopenState *reopen_state,BlockReopenQueue *queue,Error **errp);block.c static BdrvChildRole bdrv_backing_role(BlockDriverState *bs) · line 3202
Quoted 8 lines
block.c +6
-0@@ -3194,45 +3195,111 @@ static BdrvChildRole bdrv_backing_role(BlockDriverState *bs) } } +typedef struct BdrvSetBackingNoPermState { +BlockDriverState *bs; +BlockDriverState *backing_bs; +BlockDriverState *old_inherits_from; +GSList *attach_tran; +}BdrvSetBackingNoPermState ;Why do we need the nested
attach_traninstead of just including these actions in the outer transaction?Quoted 4 lines
block.c +4
-0+static void bdrv_set_backing_noperm_abort (void *opaque) +{ +BdrvSetBackingNoPermState *s = opaque; +block.c static BdrvChildRole bdrv_backing_role(BlockDriverState *bs) · line 3218
Quoted 10 lines
block.c +10
-0+if (s->backing_bs) { + s->backing_bs->inherits_from = s->old_inherits_from; + } + +tran_abort (s->attach_tran); + +bdrv_refresh_limits (s->bs,NULL ); +if (s->old_inherits_from) { +bdrv_refresh_limits (s->old_inherits_from,NULL ); + }How is
bs->inherits_fromrelated to limits? I don't see abdrv_refresh_limits()call inbdrv_set_backing_noperm()that this would undo.Quoted 39 lines
block.c +25
-7+} + +static void bdrv_set_backing_noperm_commit (void *opaque) +{ +BdrvSetBackingNoPermState *s = opaque; + +tran_commit (s->attach_tran); +} + +static TransactionActionDrv bdrv_set_backing_noperm_drv = { + .abort = bdrv_set_backing_noperm_abort, + .commit = bdrv_set_backing_noperm_commit, + .clean = g_free, +}; +/* * Sets the bs->backing link of a BDS. A new reference is created; callers * which don't need their own reference any more must call bdrv_unref(). */ -void bdrv_set_backing_hd (BlockDriverState *bs,BlockDriverState *backing_hd, -Error **errp) +static int bdrv_set_backing_noperm (BlockDriverState *bs, +BlockDriverState *backing_bs, +GSList **tran,Error **errp) { -bool update_inherits_from =bdrv_chain_contains (bs, backing_hd) && -bdrv_inherits_from_recursive (backing_hd, bs); +int ret =0 ; +bool update_inherits_from =bdrv_chain_contains (bs, backing_bs) && +bdrv_inherits_from_recursive (backing_bs, bs); +GSList *attach_tran =NULL ; +BdrvSetBackingNoPermState *s;if (bdrv_is_backing_chain_frozen (bs,child_bs (bs->backing), errp)) { -return ; +return -EPERM ; } -if (backing_hd) { -bdrv_ref (backing_hd); +if (bs->backing && backing_bs) {block.c static BdrvChildRole bdrv_backing_role(BlockDriverState *bs) · line 3262
Quoted 10 lines
block.c +10
-0+bdrv_replace_child_safe (bs->backing, backing_bs, tran); + }else if (bs->backing && !backing_bs) { +bdrv_remove_backing (bs, tran); + }else if (backing_bs) { +assert (!bs->backing); + ret =bdrv_attach_child_noperm (bs, backing_bs,"backing" , + &child_of_bds,bdrv_backing_role (bs), + &bs->backing, &attach_tran, errp); +if (ret <0 ) { +tran_abort (attach_tran);This looks wrong to me, we'll call
tran_abort()a second time throughbdrv_set_backing_noperm_abort()when the outer transaction aborts.I also notice that the other two if branches do just add things to the outer 'tran', it's just this branch that gets a nested one.
Quoted 21 lines
block.c +13
-7+return ret; + } } -if (bs->backing) { -/* Cannot be frozen, we checked that above */ -bdrv_unref_child (bs, bs->backing); - bs->backing =NULL ; - } + s =g_new (BdrvSetBackingNoPermState,1 ); + *s = (BdrvSetBackingNoPermState ) { + .bs = bs, + .backing_bs = backing_bs, + .old_inherits_from = backing_bs ? backing_bs->inherits_from :NULL , + }; +tran_prepend (tran, &bdrv_set_backing_noperm_drv, s); -if (!backing_hd) { - goto out; +/* +* If backing_bs was already part of bs's backing chain, and +* inherits_from pointed recursively to bs then let's update it to +* point directly to bs (else it will become NULL).
Quoted 7 lines
Quoted 4 lines
Setting it to NULL was previously done by
bdrv_unref_child().
bdrv_replace_child_safe()andbdrv_remove_backing()don't seem to do this any more.Hmm, yes.. May be we should move
bdrv_unset_inherts_from()frombdrv_unref_child()tobdrv_replace_child_noperm()?
Sounds good to me. This should hopefully be called for all graph changes that could possibly happen.
Kevin
Quoted 25 lines
Quoted 19 lines
Quoted 6 lines
block.c +3
-1+*/ +if (backing_bs && update_inherits_from) { + backing_bs->inherits_from = bs; } - bs->backing = bdrv_attach_child(bs, backing_hd, "backing", &child_of_bds,block.c static BdrvChildRole bdrv_backing_role(BlockDriverState *bs) · line 3287
Quoted 10 lines
block.c +4
-6- bdrv_backing_role(bs), errp); -/* If backing_hd was already part of bs's backing chain, and -* inherits_from pointed recursively to bs then let's update it to -* point directly to bs (else it will become NULL). */ -if (bs->backing && update_inherits_from) { - backing_hd->inherits_from = bs; +bdrv_refresh_limits (bs,NULL ); + +return 0 ; +}Kevin
Signature · 2 lines
Best regards, Vladimir
Re: [PATCH v2 28/36] block: add bdrv_set_backing_noperm() transaction action
Quoted 135 lines · Kevin Wolf
Am 05.02.2021 um 17:06 hat Vladimir Sementsov-Ogievskiy geschrieben:
Quoted 134 lines
Quoted 133 lines · Kevin Wolf
Am 27.11.2020 um 15:45 hat Vladimir Sementsov-Ogievskiy geschrieben:
Quoted 21 lines
Split out no-perm part of
bdrv_set_backing_hd()as a separate transaction action. Note the in case of existing BdrvChild we reuse it, not recreate, just to do less actions.Signed-off-by Vladimir Sementsov-Ogievskiy <[email protected]>--- block.c | 111 +++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 89 insertions(+), 22 deletions(-)block.c +1
-0@@ -101,6 +101,7 @@ static int bdrv_attach_child_common(BlockDriverState *child_bs, uint64_t perm, uint64_t shared_perm, void *opaque, BdrvChild **child, GSList **tran, Error **errp); +static void bdrv_remove_backing (BlockDriverState *bs,GSList **tran);static int bdrv_reopen_prepare (BDRVReopenState *reopen_state,BlockReopenQueue *queue,Error **errp);block.c static BdrvChildRole bdrv_backing_role(BlockDriverState *bs) · line 3202
Quoted 8 lines
block.c +6
-0@@ -3194,45 +3195,111 @@ static BdrvChildRole bdrv_backing_role(BlockDriverState *bs) } } +typedef struct BdrvSetBackingNoPermState { +BlockDriverState *bs; +BlockDriverState *backing_bs; +BlockDriverState *old_inherits_from; +GSList *attach_tran; +}BdrvSetBackingNoPermState ;Why do we need the nested
attach_traninstead of just including these actions in the outer transaction?Quoted 4 lines
block.c +4
-0+static void bdrv_set_backing_noperm_abort (void *opaque) +{ +BdrvSetBackingNoPermState *s = opaque; +block.c static BdrvChildRole bdrv_backing_role(BlockDriverState *bs) · line 3218
Quoted 10 lines
block.c +10
-0+if (s->backing_bs) { + s->backing_bs->inherits_from = s->old_inherits_from; + } + +tran_abort (s->attach_tran); + +bdrv_refresh_limits (s->bs,NULL ); +if (s->old_inherits_from) { +bdrv_refresh_limits (s->old_inherits_from,NULL ); + }How is
bs->inherits_fromrelated to limits? I don't see abdrv_refresh_limits()call inbdrv_set_backing_noperm()that this would undo.Quoted 39 lines
block.c +25
-7+} + +static void bdrv_set_backing_noperm_commit (void *opaque) +{ +BdrvSetBackingNoPermState *s = opaque; + +tran_commit (s->attach_tran); +} + +static TransactionActionDrv bdrv_set_backing_noperm_drv = { + .abort = bdrv_set_backing_noperm_abort, + .commit = bdrv_set_backing_noperm_commit, + .clean = g_free, +}; +/* * Sets the bs->backing link of a BDS. A new reference is created; callers * which don't need their own reference any more must call bdrv_unref(). */ -void bdrv_set_backing_hd (BlockDriverState *bs,BlockDriverState *backing_hd, -Error **errp) +static int bdrv_set_backing_noperm (BlockDriverState *bs, +BlockDriverState *backing_bs, +GSList **tran,Error **errp) { -bool update_inherits_from =bdrv_chain_contains (bs, backing_hd) && -bdrv_inherits_from_recursive (backing_hd, bs); +int ret =0 ; +bool update_inherits_from =bdrv_chain_contains (bs, backing_bs) && +bdrv_inherits_from_recursive (backing_bs, bs); +GSList *attach_tran =NULL ; +BdrvSetBackingNoPermState *s;if (bdrv_is_backing_chain_frozen (bs,child_bs (bs->backing), errp)) { -return ; +return -EPERM ; } -if (backing_hd) { -bdrv_ref (backing_hd); +if (bs->backing && backing_bs) {block.c static BdrvChildRole bdrv_backing_role(BlockDriverState *bs) · line 3262
Quoted 10 lines
block.c +10
-0+bdrv_replace_child_safe (bs->backing, backing_bs, tran); + }else if (bs->backing && !backing_bs) { +bdrv_remove_backing (bs, tran); + }else if (backing_bs) { +assert (!bs->backing); + ret =bdrv_attach_child_noperm (bs, backing_bs,"backing" , + &child_of_bds,bdrv_backing_role (bs), + &bs->backing, &attach_tran, errp); +if (ret <0 ) { +tran_abort (attach_tran);This looks wrong to me, we'll call
tran_abort()a second time throughbdrv_set_backing_noperm_abort()when the outer transaction aborts.I also notice that the other two if branches do just add things to the outer 'tran', it's just this branch that gets a nested one.
Quoted 11 lines
block.c +5
-5+return ret; + } } -if (bs->backing) { -/* Cannot be frozen, we checked that above */ -bdrv_unref_child (bs, bs->backing); - bs->backing =NULL ; - } + s =g_new (BdrvSetBackingNoPermState,1 ); + *s = (BdrvSetBackingNoPermState ) { + .bs = bs,block.c static BdrvChildRole bdrv_backing_role(BlockDriverState *bs) · line 3278
Quoted 10 lines
block.c +8
-2+ .backing_bs = backing_bs, + .old_inherits_from = backing_bs ? backing_bs->inherits_from : NULL, + }; +tran_prepend (tran, &bdrv_set_backing_noperm_drv, s); -if (!backing_hd) { - goto out; +/* +* If backing_bs was already part of bs's backing chain, and +* inherits_from pointed recursively to bs then let's update it to +* point directly to bs (else it will become NULL). Setting it to NULL was previously done by
bdrv_unref_child().
Quoted 9 lines
Quoted 5 lines
Quoted 2 lines
bdrv_replace_child_safe()andbdrv_remove_backing()don't seem to do this any more.Hmm, yes.. May be we should move
bdrv_unset_inherts_from()frombdrv_unref_child()tobdrv_replace_child_noperm()?Sounds good to me. This should hopefully be called for all graph changes that could possibly happen.
This will break current "feature":
block jobs don't break inherits_from at all: when filter inserted inherits_from is broken. But when filter removed, it works again, as .inherits_from is not changed by bdrv_append().. So, I'll just try to keep current behavior around inherits_from as is.
Signature · 2 lines
Best regards, Vladimir
Re: [PATCH v2 28/36] block: add bdrv_set_backing_noperm() transaction action
In reply to Vladimir Sementsov-OgievskiyAm 27.11.2020 um 15:45 hat Vladimir Sementsov-Ogievskiy geschrieben:
Quoted 23 lines
Split out no-perm part of
bdrv_set_backing_hd()as a separate transaction action. Note the in case of existing BdrvChild we reuse it, not recreate, just to do less actions.Signed-off-by Vladimir Sementsov-Ogievskiy <[email protected]>/*
* Sets thebs->backinglink of a BDS. A new reference is created; callers
* which don't need their own reference any more must callbdrv_unref(). */block.c +8
-4-void bdrv_set_backing_hd (BlockDriverState *bs,BlockDriverState *backing_hd, -Error **errp) +static int bdrv_set_backing_noperm (BlockDriverState *bs, +BlockDriverState *backing_bs, +GSList **tran,Error **errp) { -bool update_inherits_from =bdrv_chain_contains (bs, backing_hd) && -bdrv_inherits_from_recursive (backing_hd, bs); +int ret =0 ; +bool update_inherits_from =bdrv_chain_contains (bs, backing_bs) && +bdrv_inherits_from_recursive (backing_bs, bs); +GSList *attach_tran =NULL ; +BdrvSetBackingNoPermState *s;
block.c static BdrvChildRole bdrv_backing_role(BlockDriverState *bs) · line 3253
Quoted 10 lines
block.c +3
-3if (bdrv_is_backing_chain_frozen (bs,child_bs (bs->backing), errp)) { -return ; +return -EPERM ; } -if (backing_hd) { -bdrv_ref (backing_hd); +if (bs->backing && backing_bs) { +bdrv_replace_child_safe (bs->backing, backing_bs, tran);
The old code with separate bdrv_unref_child() and then bdrv_attach_child() tried to make the AioContests of bs and backing_bs compatible by moving one of the nodes if necessary.
bdrv_replace_child_safe() doesn't seem to do that, but it only asserts that both nodes are already in the same context.
I see that iotest 245 doesn't crash, which I think it should if this were broken, but where does the switch happen now?
Kevin
Re: [PATCH v2 28/36] block: add bdrv_set_backing_noperm() transaction action
Quoted 39 lines · Kevin Wolf
Am 27.11.2020 um 15:45 hat Vladimir Sementsov-Ogievskiy geschrieben:
Quoted 23 lines
Split out no-perm part of
bdrv_set_backing_hd()as a separate transaction action. Note the in case of existing BdrvChild we reuse it, not recreate, just to do less actions.Signed-off-by Vladimir Sementsov-Ogievskiy <[email protected]>/*
* Sets thebs->backinglink of a BDS. A new reference is created; callers
* which don't need their own reference any more must callbdrv_unref(). */block.c +8
-4-void bdrv_set_backing_hd (BlockDriverState *bs,BlockDriverState *backing_hd, -Error **errp) +static int bdrv_set_backing_noperm (BlockDriverState *bs, +BlockDriverState *backing_bs, +GSList **tran,Error **errp) { -bool update_inherits_from =bdrv_chain_contains (bs, backing_hd) && -bdrv_inherits_from_recursive (backing_hd, bs); +int ret =0 ; +bool update_inherits_from =bdrv_chain_contains (bs, backing_bs) && +bdrv_inherits_from_recursive (backing_bs, bs); +GSList *attach_tran =NULL ; +BdrvSetBackingNoPermState *s;block.c static BdrvChildRole bdrv_backing_role(BlockDriverState *bs) · line 3253
Quoted 10 lines
block.c +3
-3if (bdrv_is_backing_chain_frozen (bs,child_bs (bs->backing), errp)) { -return ; +return -EPERM ; } -if (backing_hd) { -bdrv_ref (backing_hd); +if (bs->backing && backing_bs) { +bdrv_replace_child_safe (bs->backing, backing_bs, tran);The old code with separate
bdrv_unref_child()and thenbdrv_attach_child()tried to make the AioContests of bs andbacking_bscompatible by moving one of the nodes if necessary.
Quoted 5 lines
bdrv_replace_child_safe()doesn't seem to do that, but it only asserts that both nodes are already in the same context.I see that iotest 245 doesn't crash, which I think it should if this were broken, but where does the switch happen now?
Hmm. Seems on path "if (bs->backing && backing_bs) {" we really miss aio context handling. Probably 245 doesn't check this branch? Or if leaves different aio contexts in one subtree..
Signature · 2 lines
Best regards, Vladimir
PATCH v2 29/36 blockdev: qmp_x_blockdev_reopen: acquire all contexts
In reply to Vladimir Sementsov-OgievskiyDuring reopen we may add backing bs from other aio context, which may lead to changing original context of top bs.
We are going to move graph modification to prepare stage. So, it will be possible that bdrv_flush() in bdrv_reopen_prepare called on bs in non-original aio context, which we didn't aquire which leads to crash.
More correct would be to acquire all aio context we are going to work with. And the simplest ways is to just acquire all of them. It may be optimized later if needed.
--- blockdev.c | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-)
blockdev.c +19 -4
@@ -3531,7 +3531,6 @@ fail:void qmp_x_blockdev_reopen (BlockdevOptions *options,Error **errp) {BlockDriverState *bs; -AioContext *ctx;QObject *obj;Visitor *v =qobject_output_visitor_new (&obj);BlockReopenQueue *queue;
@@ -3557,13 +3556,29 @@ void qmp_x_blockdev_reopen(BlockdevOptions *options, Error **errp)qdict_flatten (qdict);/* Perform the reopen operation */ - ctx =bdrv_get_aio_context (bs); -aio_context_acquire (ctx); +BdrvNextIterator it; +GSList *aio_ctxs =NULL , *ctx; +BlockDriverState *it_bs; + +for (it_bs =bdrv_first (&it); it_bs; it_bs =bdrv_next (&it)) { +AioContext *aio_context =bdrv_get_aio_context (it_bs); + +if (!g_slist_find (aio_ctxs, aio_context)) { + aio_ctxs =g_slist_prepend (aio_ctxs, aio_context); +aio_context_acquire (aio_context); + } + } +bdrv_subtree_drained_begin (bs); queue =bdrv_reopen_queue (NULL , bs, qdict, false);bdrv_reopen_multiple (queue, errp);bdrv_subtree_drained_end (bs); -aio_context_release (ctx); + +for (ctx = aio_ctxs; ctx !=NULL ; ctx = ctx->next) { +AioContext *aio_context = ctx->data; +aio_context_release (aio_context); + } +g_slist_free (aio_ctxs); fail:visit_free (v);
Signature · 1 line
2.21.3
Re: [PATCH v2 29/36] blockdev: qmp_x_blockdev_reopen: acquire all contexts
Am 27.11.2020 um 15:45 hat Vladimir Sementsov-Ogievskiy geschrieben:
Quoted 7 lines
During reopen we may add backing bs from other aio context, which may lead to changing original context of top bs.
We are going to move graph modification to prepare stage. So, it will be possible that
bdrv_flush()inbdrv_reopen_preparecalled on bs in non-original aio context, which we didn't aquire which leads to crash.
Quoted 5 lines
More correct would be to acquire all aio context we are going to work with. And the simplest ways is to just acquire all of them. It may be optimized later if needed.
Signed-off-by Vladimir Sementsov-Ogievskiy <[email protected]>
I'm afraid it's not as easy. Holding the lock of more than one AioContext is always a bit risky with respect to deadlocks.
For example, changing the AioContext of a node with bdrv_set_aio_context_ignore() has explicit rules that are now violated:
* The caller must own the AioContext lock for the old AioContext of bs, but it
* must not own the AioContext lock for new_context (unless new_context is the
* same as the current context of bs).
Draining while holding all AioContext locks is suspicious, too. I think I have seen deadlocks before, which is why bdrv_drain_all_*() are careful to only ever lock a single AioContext at a time.
Kevin
Re: [PATCH v2 29/36] blockdev: qmp_x_blockdev_reopen: acquire all contexts
Quoted 24 lines · Kevin Wolf
Am 27.11.2020 um 15:45 hat Vladimir Sementsov-Ogievskiy geschrieben:
Quoted 7 lines
During reopen we may add backing bs from other aio context, which may lead to changing original context of top bs.
We are going to move graph modification to prepare stage. So, it will be possible that
bdrv_flush()inbdrv_reopen_preparecalled on bs in non-original aio context, which we didn't aquire which leads to crash.Quoted 5 lines
More correct would be to acquire all aio context we are going to work with. And the simplest ways is to just acquire all of them. It may be optimized later if needed.
Signed-off-by Vladimir Sementsov-Ogievskiy <[email protected]>I'm afraid it's not as easy. Holding the lock of more than one AioContext is always a bit risky with respect to deadlocks.
For example, changing the AioContext of a node with
bdrv_set_aio_context_ignore()has explicit rules that are now violated:* The caller must own the AioContext lock for the old AioContext of bs, but it
* must not own the AioContext lock fornew_context(unlessnew_contextis the
* same as the current context of bs).
Quoted 6 lines
Draining while holding all AioContext locks is suspicious, too. I think I have seen deadlocks before, which is why bdrv_drain_all_*() are careful to only ever lock a single AioContext at a time.
Kevin
That's not good :\ Hmm, probably we just should flush everything before all graph modifications.
Signature · 2 lines
Best regards, Vladimir
Re: [PATCH v2 29/36] blockdev: qmp_x_blockdev_reopen: acquire all contexts
Am 05.02.2021 um 17:16 hat Vladimir Sementsov-Ogievskiy geschrieben:
Quoted 25 lines
Quoted 24 lines · Kevin Wolf
Am 27.11.2020 um 15:45 hat Vladimir Sementsov-Ogievskiy geschrieben:
Quoted 7 lines
During reopen we may add backing bs from other aio context, which may lead to changing original context of top bs.
We are going to move graph modification to prepare stage. So, it will be possible that
bdrv_flush()inbdrv_reopen_preparecalled on bs in non-original aio context, which we didn't aquire which leads to crash.Quoted 5 lines
More correct would be to acquire all aio context we are going to work with. And the simplest ways is to just acquire all of them. It may be optimized later if needed.
Signed-off-by Vladimir Sementsov-Ogievskiy <[email protected]>I'm afraid it's not as easy. Holding the lock of more than one AioContext is always a bit risky with respect to deadlocks.
For example, changing the AioContext of a node with
bdrv_set_aio_context_ignore()has explicit rules that are now violated:* The caller must own the AioContext lock for the old AioContext of bs, but it
* must not own the AioContext lock fornew_context(unlessnew_contextis the
* same as the current context of bs).
Quoted 6 lines
Quoted 3 lines
Draining while holding all AioContext locks is suspicious, too. I think I have seen deadlocks before, which is why bdrv_drain_all_*() are careful to only ever lock a single AioContext at a time.
That's not good :\ Hmm, probably we just should flush everything before all graph modifications.
Would that have to be a separate phase before prepare then?
I suppose the same problem exists with drv->bdrv_reopen_prepare, which might be called in a different state (both graph structure and AioContext) than before. I'll have to see the patch first that reorders things, but this callback has always had the problem that sometimes it wants the old state and sometimes it wants the new state...
Kevin
PATCH v2 30/36 block: bdrv_reopen_multiple: refresh permissions on updated graph
In reply to Vladimir Sementsov-OgievskiyMove bdrv_reopen_multiple to new paradigm of permission update:
first update graph relations, then do refresh the permissions.
We have to modify reopen process in file-posix driver: with new scheme we don't have prepared permissions in raw_reopen_prepare(), so we should reconfigure fd in raw_check_perm(). Still this seems more native and simple anyway.
--- include/block/block.h | 2 +- block.c | 183 +++++++++++------------------------------- block/file-posix.c | 84 +++++-------------- 3 files changed, 70 insertions(+), 199 deletions(-)
include/block/block.h +1 -1
@@ -195,7 +195,7 @@ typedef struct BDRVReopenState {BlockdevDetectZeroesOptions detect_zeroes;bool backing_missing;bool replace_backing_bs;/* new_backing_bs is ignored if this is false */ -BlockDriverState *new_backing_bs;/* If NULL then detach the current bs */ +BlockDriverState *old_backing_bs;/* keep pointer for permissions update */ uint64_t perm, shared_perm;QDict *options;QDict *explicit_options;
block.c +48 -135
289 lines not on this page · raw message
block/file-posix.c +21 -63
140 lines not on this page · raw message
Signature · 1 line
2.21.3
Re: [PATCH v2 30/36] block: bdrv_reopen_multiple: refresh permissions on updated graph
Am 27.11.2020 um 15:45 hat Vladimir Sementsov-Ogievskiy geschrieben:
Quoted 7 lines
Move
bdrv_reopen_multipleto new paradigm of permission update:
first update graph relations, then do refresh the permissions.We have to modify reopen process in file-posix driver: with new scheme we don't have prepared permissions in
raw_reopen_prepare(), so we should reconfigure fd inraw_check_perm(). Still this seems more native and simple anyway.
Hm... The diffstat shows that it is simpler because it needs less code.
But relying on the permission change callbacks for getting a new file descriptor that changes more than just permissions doesn't feel completely right either. Can we even expect the permission callbacks to be called when the permissions aren't changed?
But then, reopen and permission updates were already a bit entangled before. If we can guarantee that the permission functions will always be called, even if the permissions don't change, I guess it's okay.
Quoted 12 lines
Signed-off-by Vladimir Sementsov-Ogievskiy <[email protected]>--- include/block/block.h | 2 +- block.c | 183 +++++++++++------------------------------- block/file-posix.c | 84 +++++-------------- 3 files changed, 70 insertions(+), 199 deletions(-)include/block/block.h +0
-0
include/block/block.h typedef struct BDRVReopenState { · line 199
Quoted 6 lines
include/block/block.h +1
-1@@ -195,7 +195,7 @@ typedef struct BDRVReopenState {BlockdevDetectZeroesOptions detect_zeroes;bool backing_missing;bool replace_backing_bs;/* new_backing_bs is ignored if this is false */ -BlockDriverState *new_backing_bs;/* If NULL then detach the current bs */ +BlockDriverState *old_backing_bs;/* keep pointer for permissions update */ uint64_t perm, shared_perm;
perm and shared_perm are unused now and can be removed.
Quoted 46 lines
include/block/block.h +0
-0QDict *options;QDict *explicit_options;block.c +5
-6@@ -103,8 +103,9 @@ static int bdrv_attach_child_common(BlockDriverState *child_bs, GSList **tran, Error **errp);static void bdrv_remove_backing (BlockDriverState *bs,GSList **tran); -static int bdrv_reopen_prepare (BDRVReopenState *reopen_state,BlockReopenQueue - *queue,Error **errp); +static int bdrv_reopen_prepare (BDRVReopenState *reopen_state, +BlockReopenQueue *queue, +GSList **set_backings_tran,Error **errp);static void bdrv_reopen_commit (BDRVReopenState *reopen_state);static void bdrv_reopen_abort (BDRVReopenState *reopen_state);@@ -2403,6 +2404,7 @@ static void bdrv_list_abort_perm_update(GSList *list) } } +__attribute__((unused))static void bdrv_abort_perm_update (BlockDriverState *bs) {g_autoptr (GSList) list =bdrv_topological_dfs (NULL ,NULL , bs);@@ -2498,6 +2500,7 @@ char *bdrv_perm_names(uint64_t perm)* * Needs to be followed by a call to either bdrv_set_perm() or * bdrv_abort_perm_update(). */ +__attribute__((unused)) static int bdrv_check_update_perm(BlockDriverState *bs, BlockReopenQueue *q, uint64_t new_used_perm, uint64_t new_shared_perm,@@ -4100,10 +4103,6 @@ static BlockReopenQueue *bdrv_reopen_queue_child(BlockReopenQueue *bs_queue, bs_entry->state.explicit_options = explicit_options; bs_entry->state.flags = flags; -/* This needs to be overwritten in bdrv_reopen_prepare() */ - bs_entry->state.perm =UINT64_MAX ; - bs_entry->state.shared_perm =0 ; -/* * If keep_old_opts is false then it means that unspecified * options must be reset to their original value. We don't allow
block.c BlockReopenQueue *bdrv_reopen_queue(BlockReopenQueue *bs_queue, · line 4188
Quoted 5 lines
block.c +1
-1@@ -4186,40 +4185,37 @@ BlockReopenQueue *bdrv_reopen_queue(BlockReopenQueue *bs_queue,*/ int bdrv_reopen_multiple (BlockReopenQueue *bs_queue,Error **errp) { -int ret =-1 ; +int ret =0 ;
I would prefer to leave this right before the 'goto cleanup'.
Not sure if I fully understand all consequences yet, but overall, apart from my concerns about file-posix and the potential AioContext locking problems, this looks like a nice simplification of the process.
Come to think of it, the AioContext handling is probably wrong already before your series. reopen_commit for one node could move the whole tree to a different context and then the later nodes would all be processed while holding the wrong lock.
Kevin
Re: [PATCH v2 30/36] block: bdrv_reopen_multiple: refresh permissions on updated graph
Quoted 9 lines · Kevin Wolf
Am 27.11.2020 um 15:45 hat Vladimir Sementsov-Ogievskiy geschrieben:
Quoted 7 lines
Move
bdrv_reopen_multipleto new paradigm of permission update:
first update graph relations, then do refresh the permissions.We have to modify reopen process in file-posix driver: with new scheme we don't have prepared permissions in
raw_reopen_prepare(), so we should reconfigure fd inraw_check_perm(). Still this seems more native and simple anyway.
Quoted 6 lines
Hm... The diffstat shows that it is simpler because it needs less code.
But relying on the permission change callbacks for getting a new file descriptor that changes more than just permissions doesn't feel completely right either. Can we even expect the permission callbacks to be called when the permissions aren't changed?
With new scheme permission update becomes an obvious step of bdrv_reopen_multiple(): we do call bdrv_list_refresh_perms(), for the list of all touched nodes and all their subtrees. And callbacks are called unconditionally bdrv_node_refresh_perm()->bdrv_drv_set_perm(). So, I think, we can rely on it. Probably worth one-two comments.
Quoted 80 lines
But then, reopen and permission updates were already a bit entangled before. If we can guarantee that the permission functions will always be called, even if the permissions don't change, I guess it's okay.
Quoted 12 lines
Signed-off-by Vladimir Sementsov-Ogievskiy <[email protected]>--- include/block/block.h | 2 +- block.c | 183 +++++++++++------------------------------- block/file-posix.c | 84 +++++-------------- 3 files changed, 70 insertions(+), 199 deletions(-)include/block/block.h +0
-0include/block/block.h typedef struct BDRVReopenState { · line 199
Quoted 6 lines
include/block/block.h +1
-1@@ -195,7 +195,7 @@ typedef struct BDRVReopenState {BlockdevDetectZeroesOptions detect_zeroes;bool backing_missing;bool replace_backing_bs;/* new_backing_bs is ignored if this is false */ -BlockDriverState *new_backing_bs;/* If NULL then detach the current bs */ +BlockDriverState *old_backing_bs;/* keep pointer for permissions update */ uint64_t perm, shared_perm;perm and
shared_permare unused now and can be removed.Quoted 46 lines
include/block/block.h +0
-0QDict *options;QDict *explicit_options;block.c +5
-6@@ -103,8 +103,9 @@ static int bdrv_attach_child_common(BlockDriverState *child_bs, GSList **tran, Error **errp);static void bdrv_remove_backing (BlockDriverState *bs,GSList **tran); -static int bdrv_reopen_prepare (BDRVReopenState *reopen_state,BlockReopenQueue - *queue,Error **errp); +static int bdrv_reopen_prepare (BDRVReopenState *reopen_state, +BlockReopenQueue *queue, +GSList **set_backings_tran,Error **errp);static void bdrv_reopen_commit (BDRVReopenState *reopen_state);static void bdrv_reopen_abort (BDRVReopenState *reopen_state);@@ -2403,6 +2404,7 @@ static void bdrv_list_abort_perm_update(GSList *list) } } +__attribute__((unused))static void bdrv_abort_perm_update (BlockDriverState *bs) {g_autoptr (GSList) list =bdrv_topological_dfs (NULL ,NULL , bs);@@ -2498,6 +2500,7 @@ char *bdrv_perm_names(uint64_t perm)* * Needs to be followed by a call to either bdrv_set_perm() or * bdrv_abort_perm_update(). */ +__attribute__((unused)) static int bdrv_check_update_perm(BlockDriverState *bs, BlockReopenQueue *q, uint64_t new_used_perm, uint64_t new_shared_perm,@@ -4100,10 +4103,6 @@ static BlockReopenQueue *bdrv_reopen_queue_child(BlockReopenQueue *bs_queue, bs_entry->state.explicit_options = explicit_options; bs_entry->state.flags = flags; -/* This needs to be overwritten in bdrv_reopen_prepare() */ - bs_entry->state.perm =UINT64_MAX ; - bs_entry->state.shared_perm =0 ; -/* * If keep_old_opts is false then it means that unspecified * options must be reset to their original value. We don't allow block.c BlockReopenQueue *bdrv_reopen_queue(BlockReopenQueue *bs_queue, · line 4188
Quoted 5 lines
block.c +1
-1@@ -4186,40 +4185,37 @@ BlockReopenQueue *bdrv_reopen_queue(BlockReopenQueue *bs_queue,*/ int bdrv_reopen_multiple (BlockReopenQueue *bs_queue,Error **errp) { -int ret =-1 ; +int ret =0 ;I would prefer to leave this right before the 'goto cleanup'.
Quoted 9 lines
Not sure if I fully understand all consequences yet, but overall, apart from my concerns about file-posix and the potential AioContext locking problems, this looks like a nice simplification of the process.
Come to think of it, the AioContext handling is probably wrong already before your series.
reopen_commitfor one node could move the whole tree to a different context and then the later nodes would all be processed while holding the wrong lock.
Probably proper way is to acquire all involved aio contexts as I do in 29 and update aio-context updating functions to work in such conditions(all aio contexts are already acquired by caller).
Signature · 2 lines
Best regards, Vladimir
Re: [PATCH v2 30/36] block: bdrv_reopen_multiple: refresh permissions on updated graph
Am 08.02.2021 um 12:21 hat Vladimir Sementsov-Ogievskiy geschrieben:
Quoted 17 lines
Quoted 9 lines · Kevin Wolf
Am 27.11.2020 um 15:45 hat Vladimir Sementsov-Ogievskiy geschrieben:
Quoted 7 lines
Move
bdrv_reopen_multipleto new paradigm of permission update:
first update graph relations, then do refresh the permissions.We have to modify reopen process in file-posix driver: with new scheme we don't have prepared permissions in
raw_reopen_prepare(), so we should reconfigure fd inraw_check_perm(). Still this seems more native and simple anyway.Quoted 6 lines
Hm... The diffstat shows that it is simpler because it needs less code.
But relying on the permission change callbacks for getting a new file descriptor that changes more than just permissions doesn't feel completely right either. Can we even expect the permission callbacks to be called when the permissions aren't changed?
Quoted 5 lines
With new scheme permission update becomes an obvious step of
bdrv_reopen_multiple(): we do callbdrv_list_refresh_perms(), for the list of all touched nodes and all their subtrees. And callbacks are called unconditionally bdrv_node_refresh_perm()->bdrv_drv_set_perm(). So, I think, we can rely on it. Probably worth one-two comments.
Yes, some comments in the right places that we must call the driver callbacks even if the permissions are the same as before wouldn't hurt.
Quoted 86 lines
Quoted 76 lines
But then, reopen and permission updates were already a bit entangled before. If we can guarantee that the permission functions will always be called, even if the permissions don't change, I guess it's okay.
Quoted 12 lines
Signed-off-by Vladimir Sementsov-Ogievskiy <[email protected]>--- include/block/block.h | 2 +- block.c | 183 +++++++++++------------------------------- block/file-posix.c | 84 +++++-------------- 3 files changed, 70 insertions(+), 199 deletions(-)include/block/block.h +0
-0include/block/block.h typedef struct BDRVReopenState { · line 199
Quoted 6 lines
include/block/block.h +1
-1@@ -195,7 +195,7 @@ typedef struct BDRVReopenState {BlockdevDetectZeroesOptions detect_zeroes;bool backing_missing;bool replace_backing_bs;/* new_backing_bs is ignored if this is false */ -BlockDriverState *new_backing_bs;/* If NULL then detach the current bs */ +BlockDriverState *old_backing_bs;/* keep pointer for permissions update */ uint64_t perm, shared_perm;perm and
shared_permare unused now and can be removed.Quoted 42 lines
include/block/block.h +0
-0QDict *options;QDict *explicit_options;block.c +5
-6@@ -103,8 +103,9 @@ static int bdrv_attach_child_common(BlockDriverState *child_bs, GSList **tran, Error **errp);static void bdrv_remove_backing (BlockDriverState *bs,GSList **tran); -static int bdrv_reopen_prepare (BDRVReopenState *reopen_state,BlockReopenQueue - *queue,Error **errp); +static int bdrv_reopen_prepare (BDRVReopenState *reopen_state, +BlockReopenQueue *queue, +GSList **set_backings_tran,Error **errp);static void bdrv_reopen_commit (BDRVReopenState *reopen_state);static void bdrv_reopen_abort (BDRVReopenState *reopen_state);@@ -2403,6 +2404,7 @@ static void bdrv_list_abort_perm_update(GSList *list) } } +__attribute__((unused))static void bdrv_abort_perm_update (BlockDriverState *bs) {g_autoptr (GSList) list =bdrv_topological_dfs (NULL ,NULL , bs);@@ -2498,6 +2500,7 @@ char *bdrv_perm_names(uint64_t perm)* * Needs to be followed by a call to either bdrv_set_perm() or * bdrv_abort_perm_update(). */ +__attribute__((unused)) static int bdrv_check_update_perm(BlockDriverState *bs, BlockReopenQueue *q, uint64_t new_used_perm, uint64_t new_shared_perm,@@ -4100,10 +4103,6 @@ static BlockReopenQueue *bdrv_reopen_queue_child(BlockReopenQueue *bs_queue, bs_entry->state.explicit_options = explicit_options; bs_entry->state.flags = flags; -/* This needs to be overwritten in bdrv_reopen_prepare() */ - bs_entry->state.perm =UINT64_MAX ; - bs_entry->state.shared_perm =0 ; -/* * If keep_old_opts is false then it means that unspecified * options must be reset to their original value. We don't allow block.c BlockReopenQueue *bdrv_reopen_queue(BlockReopenQueue *bs_queue, · line 4188
Quoted 5 lines
block.c +1
-1@@ -4186,40 +4185,37 @@ BlockReopenQueue *bdrv_reopen_queue(BlockReopenQueue *bs_queue,*/ int bdrv_reopen_multiple (BlockReopenQueue *bs_queue,Error **errp) { -int ret =-1 ; +int ret =0 ;I would prefer to leave this right before the 'goto cleanup'.
Quoted 9 lines
Not sure if I fully understand all consequences yet, but overall, apart from my concerns about file-posix and the potential AioContext locking problems, this looks like a nice simplification of the process.
Come to think of it, the AioContext handling is probably wrong already before your series.
reopen_commitfor one node could move the whole tree to a different context and then the later nodes would all be processed while holding the wrong lock.
Quoted 3 lines
Probably proper way is to acquire all involved aio contexts as I do in 29 and update aio-context updating functions to work in such conditions(all aio contexts are already acquired by caller).
Well, as we already discussed, patch 29 is probably wrong in its current form. But you seemed to have a solution in mind, which will hopefully work here, too.
Kevin
Re: [PATCH v2 30/36] block: bdrv_reopen_multiple: refresh permissions on updated graph
In reply to Vladimir Sementsov-OgievskiyAm 08.02.2021 um 12:21 hat Vladimir Sementsov-Ogievskiy geschrieben:
Quoted 8 lines
Quoted 4 lines
Come to think of it, the AioContext handling is probably wrong already before your series.
reopen_commitfor one node could move the whole tree to a different context and then the later nodes would all be processed while holding the wrong lock.Probably proper way is to acquire all involved aio contexts as I do in 29 and update aio-context updating functions to work in such conditions(all aio contexts are already acquired by caller).
Whoops, what I gave was kind of a non-answer...
So essentially the reason for the locking rules of changing the AioContext is that they drain the node first and drain imposes the locking rule that the AioContext for the node to be drained must be locked, and all other AioContexts must be unlocked.
The reason why drain imposes the rule is that we run AIO_WAIT_WHILE() in one thread and we may need the event loops in other threads to make progress until the while condition can eventually become false. If other threads can't make progress because their lock is taken, we'll see deadlocks sooner or later.
Kevin
PATCH v2 31/36 block: drop unused permission update functions
In reply to Vladimir Sementsov-Ogievskiy--- block.c | 103 -------------------------------------------------------- 1 file changed, 103 deletions(-)
block.c +0 -103
@@ -1933,11 +1933,6 @@ static int bdrv_fill_options(QDict **options, const char *filename,return 0 ; } -static int bdrv_check_update_perm (BlockDriverState *bs,BlockReopenQueue *q, -uint64_t new_used_perm, -uint64_t new_shared_perm, -Error **errp); - typedef struct BlockReopenQueueEntry { bool prepared; bool perms_checked;
@@ -2361,56 +2356,12 @@ static int bdrv_check_perm_common(GSList *list, BlockReopenQueue *q,return 0 ; } -static int bdrv_check_perm (BlockDriverState *bs,BlockReopenQueue *q, -uint64_t cumulative_perms, -uint64_t cumulative_shared_perms,Error **errp) -{ -g_autoptr (GSList) list =bdrv_topological_dfs (NULL ,NULL , bs); -return bdrv_check_perm_common (list, q, true, cumulative_perms, - cumulative_shared_perms,NULL , errp); -} -static int bdrv_list_refresh_perms (GSList *list,BlockReopenQueue *q,GSList **tran,Error **errp) {return bdrv_check_perm_common (list, q, false,0 ,0 , tran, errp); } -/* -* Notifies drivers that after a previous bdrv_check_perm() call, the -* permission update is not performed and any preparations made for it (e.g. -* taken file locks) need to be undone. -*/ -static void bdrv_node_abort_perm_update (BlockDriverState *bs) -{ -BlockDriver *drv = bs->drv; -BdrvChild *c; - -if (!drv) { -return ; - } - -bdrv_drv_set_perm_abort (bs); - - QLIST_FOREACH(c, &bs->children, next) { -bdrv_child_set_perm_abort (c); - } -} - -static void bdrv_list_abort_perm_update (GSList *list) -{ -for ( ; list; list = list->next) { -bdrv_node_abort_perm_update ((BlockDriverState *)list->data); - } -} - -__attribute__((unused)) -static void bdrv_abort_perm_update (BlockDriverState *bs) -{ -g_autoptr (GSList) list =bdrv_topological_dfs (NULL ,NULL , bs); -return bdrv_list_abort_perm_update (list); -} -static void bdrv_node_set_perm (BlockDriverState *bs) {BlockDriver *drv = bs->drv;
@@ -2492,60 +2443,6 @@ char *bdrv_perm_names(uint64_t perm)return g_string_free (result, FALSE); } -/* -* Checks whether a new reference to @bs can be added if the new user requires -* @new_used_perm/@new_shared_perm as its permissions. If @ignore_children is -* set, the BdrvChild objects in this list are ignored in the calculations; -* this allows checking permission updates for an existing reference. -* -* Needs to be followed by a call to either bdrv_set_perm() or -* bdrv_abort_perm_update(). */ -__attribute__((unused)) -static int bdrv_check_update_perm (BlockDriverState *bs,BlockReopenQueue *q, -uint64_t new_used_perm, -uint64_t new_shared_perm, -Error **errp) -{ -BdrvChild *c; -uint64_t cumulative_perms = new_used_perm; -uint64_t cumulative_shared_perms = new_shared_perm; - - -/* There is no reason why anyone couldn't tolerate write_unchanged */ -assert (new_shared_perm &BLK_PERM_WRITE_UNCHANGED ); - - QLIST_FOREACH(c, &bs->parents, next_parent) { -if ((new_used_perm & c->shared_perm) != new_used_perm) { -char *user =bdrv_child_user_desc (c); -char *perm_names =bdrv_perm_names (new_used_perm & ~c->shared_perm); - -error_setg (errp,"Conflicts with use by %s as '%s', which does not " -"allow '%s' on %s" , - user, c->name, perm_names,bdrv_get_node_name (c->bs)); -g_free (user); -g_free (perm_names); -return -EPERM ; - } - -if ((c->perm & new_shared_perm) != c->perm) { -char *user =bdrv_child_user_desc (c); -char *perm_names =bdrv_perm_names (c->perm & ~new_shared_perm); - -error_setg (errp,"Conflicts with use by %s as '%s', which uses " -"'%s' on %s" , - user, c->name, perm_names,bdrv_get_node_name (c->bs)); -g_free (user); -g_free (perm_names); -return -EPERM ; - } - - cumulative_perms |= c->perm; - cumulative_shared_perms &= c->shared_perm; - } - -return bdrv_check_perm (bs, q, cumulative_perms, cumulative_shared_perms, - errp); -}static int bdrv_refresh_perms (BlockDriverState *bs,Error **errp) {
Signature · 1 line
2.21.3
PATCH v2 32/36 block: inline bdrv_check_perm_common()
In reply to Vladimir Sementsov-Ogievskiybdrv_check_perm_common() has only one caller, so no more sense in "common".
--- block.c | 32 +++----------------------------- 1 file changed, 3 insertions(+), 29 deletions(-)
block.c +3 -29
@@ -2308,33 +2308,13 @@ static int bdrv_node_check_perm(BlockDriverState *bs, BlockReopenQueue *q,return 0 ; } -/* -* If use_cumulative_perms is true, use cumulative_perms and -* cumulative_shared_perms for first element of the list. Otherwise just refresh -* all permissions. -*/ -static int bdrv_check_perm_common (GSList *list,BlockReopenQueue *q, -bool use_cumulative_perms, -uint64_t cumulative_perms, -uint64_t cumulative_shared_perms, -GSList **tran,Error **errp) +static int bdrv_list_refresh_perms (GSList *list,BlockReopenQueue *q, +GSList **tran,Error **errp) {int ret; +uint64_t cumulative_perms, cumulative_shared_perms;BlockDriverState *bs; -if (use_cumulative_perms) { - bs = list->data; - - ret =bdrv_node_check_perm (bs, q, cumulative_perms, - cumulative_shared_perms, - tran, errp); -if (ret <0 ) { -return ret; - } - - list = list->next; - } -for ( ; list; list = list->next) { bs = list->data;
@@ -2356,12 +2336,6 @@ static int bdrv_check_perm_common(GSList *list, BlockReopenQueue *q,return 0 ; } -static int bdrv_list_refresh_perms (GSList *list,BlockReopenQueue *q, -GSList **tran,Error **errp) -{ -return bdrv_check_perm_common (list, q, false,0 ,0 , tran, errp); -} -static void bdrv_node_set_perm (BlockDriverState *bs) {BlockDriver *drv = bs->drv;
Signature · 1 line
2.21.3
PATCH v2 33/36 block: inline bdrv_replace_child()
In reply to Vladimir Sementsov-Ogievskiybdrv_replace_child() has only one caller, the second argument is unused. Inline it now. This triggers deletion of some more unused interfaces.
--- block.c | 101 ++++++++++---------------------------------------------- 1 file changed, 18 insertions(+), 83 deletions(-)
block.c +18 -83
@@ -2336,42 +2336,6 @@ static int bdrv_list_refresh_perms(GSList *list, BlockReopenQueue *q,return 0 ; } -static void bdrv_node_set_perm (BlockDriverState *bs) -{ -BlockDriver *drv = bs->drv; -BdrvChild *c; - -if (!drv) { -return ; - } - -bdrv_drv_set_perm_commit (bs); - -/* Drivers that never have children can omit .bdrv_child_perm() */ -if (!drv->bdrv_child_perm) { -assert (QLIST_EMPTY (&bs->children)); -return ; - } - -/* Update all children */ - QLIST_FOREACH(c, &bs->children, next) { -bdrv_child_set_perm_commit (c); - } -} - -static void bdrv_list_set_perm (GSList *list) -{ -for ( ; list; list = list->next) { -bdrv_node_set_perm ((BlockDriverState *)list->data); - } -} - -static void bdrv_set_perm (BlockDriverState *bs) -{ -g_autoptr (GSList) list =bdrv_topological_dfs (NULL ,NULL , bs); -return bdrv_list_set_perm (list); -} -void bdrv_get_cumulative_perm (BlockDriverState *bs,uint64_t *perm,uint64_t *shared_perm) {
@@ -2711,52 +2675,6 @@ static void bdrv_replace_child_noperm(BdrvChild *child, } } -/* -* Updates @child to change its reference to point to @new_bs, including -* checking and applying the necessary permission updates both to the old node -* and to @new_bs. -* -* NULL is passed as @new_bs for removing the reference before freeing @child. -* -* If @new_bs is not NULL, bdrv_check_perm() must be called beforehand, as this -* function uses bdrv_set_perm() to update the permissions according to the new -* reference that @new_bs gets. -* -* Callers must ensure that child->frozen is false. -*/ -static void bdrv_replace_child (BdrvChild *child,BlockDriverState *new_bs) -{ -BlockDriverState *old_bs = child->bs; - -/* Asserts that child->frozen == false */ -bdrv_replace_child_noperm (child, new_bs); - -/* -* Start with the new node's permissions. If @new_bs is a (direct -* or indirect) child of @old_bs, we must complete the permission -* update on @new_bs before we loosen the restrictions on @old_bs. -* Otherwise, bdrv_check_perm() on @old_bs would re-initiate -* updating the permissions of @new_bs, and thus not purely loosen -* restrictions. -*/ -if (new_bs) { -bdrv_set_perm (new_bs); - } - -if (old_bs) { -/* -* Update permissions for old node. We're just taking a parent away, so -* we're loosening restrictions. Errors of permission update are not -* fatal in this case, ignore them. -*/ -bdrv_refresh_perms (old_bs,NULL ); - -/* When the parent requiring a non-default AioContext is removed, the -* node moves back to the main AioContext */ -bdrv_try_set_aio_context (old_bs,qemu_get_aio_context (),NULL ); - } -} -/* * This function steals the reference to child_bs from the caller. * That reference is later dropped by bdrv_root_unref_child().
@@ -2979,8 +2897,25 @@ static int bdrv_attach_child_noperm(BlockDriverState *parent_bs,static void bdrv_detach_child (BdrvChild *child) { -bdrv_replace_child (child,NULL ); +BlockDriverState *old_bs = child->bs; + +bdrv_replace_child_noperm (child,NULL );bdrv_remove_empty_child (child); + +if (old_bs) { +/* +* Update permissions for old node. We're just taking a parent away, so +* we're loosening restrictions. Errors of permission update are not +* fatal in this case, ignore them. +*/ +bdrv_refresh_perms (old_bs,NULL ); + +/* +* When the parent requiring a non-default AioContext is removed, the +* node moves back to the main AioContext +*/ +bdrv_try_set_aio_context (old_bs,qemu_get_aio_context (),NULL ); + } }/* Callers must ensure that child->frozen is false. */
Signature · 1 line
2.21.3
PATCH v2 34/36 block: refactor bdrv_child_set_perm_safe() transaction action
In reply to Vladimir Sementsov-OgievskiyOld interfaces dropped, nobody directly calls bdrv_child_set_perm_abort() and bdrv_child_set_perm_commit(), so we can use personal state structure for the action and stop exploiting BdrvChild structure. Also, drop "_safe" suffix which is redundant now.
--- include/block/block_int.h | 5 ---- block.c | 63 ++++++++++++++------------------------- 2 files changed, 22 insertions(+), 46 deletions(-)
include/block/block_int.h +0 -5
@@ -796,11 +796,6 @@ struct BdrvChild {*/ uint64_t shared_perm; -/* backup of permissions during permission update procedure */ -bool has_backup_perm; -uint64_t backup_perm; -uint64_t backup_shared_perm; -/* * This link is frozen: the child can neither be replaced nor * detached from the parent.
block.c +22 -41
95 lines not on this page · raw message
Signature · 1 line
2.21.3
Re: [PATCH v2 34/36] block: refactor bdrv_child_set_perm_safe() transaction action
Am 27.11.2020 um 15:45 hat Vladimir Sementsov-Ogievskiy geschrieben:
Quoted 35 lines
Old interfaces dropped, nobody directly calls
bdrv_child_set_perm_abort()andbdrv_child_set_perm_commit(), so we can use personal state structure for the action and stop exploiting BdrvChild structure. Also, drop "_safe" suffix which is redundant now.Signed-off-by Vladimir Sementsov-Ogievskiy <[email protected]>block.c +5
-12@@ -2070,59 +2070,40 @@ static GSList *bdrv_topological_dfs(GSList *list, GHashTable *found,return g_slist_prepend (list, bs); } -static void bdrv_child_set_perm_commit (void *opaque) -{ -BdrvChild *c = opaque; - - c->has_backup_perm = false; -} +typedef struct BdrvChildSetPermState { +BdrvChild *child; +uint64_t old_perm; +uint64_t old_shared_perm; +}BdrvChildSetPermState ;static void bdrv_child_set_perm_abort (void *opaque) { -BdrvChild *c = opaque; -/* -* We may have child->has_backup_perm unset at this point, as in case of -* _check_ stage of permission update failure we may _check_ not the whole -* subtree. Still, _abort_ is called on the whole subtree anyway. -*/
block.c static GSList *bdrv_topological_dfs(GSList *list, GHashTable *found, · line 2085
Quoted 10 lines
block.c +4
-5-if (c->has_backup_perm) { - c->perm = c->backup_perm; - c->shared_perm = c->backup_shared_perm; - c->has_backup_perm = false; - } +BdrvChildSetPermState *s = opaque; + + s->child->perm = s->old_perm; + s->child->shared_perm = s->old_shared_perm; }
Ah, so this patch actually implements what I had asked for somewhere at the start of the series.
Don't bother changing it earlier then. As long as it's in the same series, this is fine.
Kevin
PATCH v2 35/36 block: rename bdrv_replace_child_safe() to bdrv_replace_child()
In reply to Vladimir Sementsov-OgievskiyWe don't have bdrv_replace_child(), so it's time for bdrv_replace_child_safe() to take its place.
--- block.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)
block.c +5 -5
@@ -2183,11 +2183,11 @@ static TransactionActionDrv bdrv_replace_child_drv = { };/* -* bdrv_replace_child_safe +* bdrv_replace_child * * Note: real unref of old_bs is done only on commit. */ -static void bdrv_replace_child_safe (BdrvChild *child,BlockDriverState *new_bs, +static void bdrv_replace_child (BdrvChild *child,BlockDriverState *new_bs,GSList **tran) {BdrvReplaceChildState *s =g_new (BdrvReplaceChildState,1 );
@@ -3040,7 +3040,7 @@ static int bdrv_set_backing_noperm(BlockDriverState *bs, }if (bs->backing && backing_bs) { -bdrv_replace_child_safe (bs->backing, backing_bs, tran); +bdrv_replace_child (bs->backing, backing_bs, tran); }else if (bs->backing && !backing_bs) {bdrv_remove_backing (bs, tran); }else if (backing_bs) {
@@ -4679,7 +4679,7 @@ static void bdrv_remove_backing(BlockDriverState *bs, GSList **tran) }if (bs->backing->bs) { -bdrv_replace_child_safe (bs->backing,NULL , tran); +bdrv_replace_child (bs->backing,NULL , tran); }tran_prepend (tran, &bdrv_remove_backing_drv, bs->backing);
@@ -4708,7 +4708,7 @@ static int bdrv_replace_node_noperm(BlockDriverState *from, c->name, from->node_name);return -EPERM ; } -bdrv_replace_child_safe (c, to, tran); +bdrv_replace_child (c, to, tran); }return 0 ;
Signature · 1 line
2.21.3
PATCH v2 36/36 block: refactor bdrv_node_check_perm()
In reply to Vladimir Sementsov-OgievskiyNow, bdrv_node_check_perm() is called only with fresh cumulative permissions, so its actually "refresh_perm".
Move permission calculation to the function. Also, drop unreachable error message.
Add also Virtuozzo copyright, as big work is done at this point.
--- block.c | 38 +++++++++----------------------------- 1 file changed, 9 insertions(+), 29 deletions(-)
block.c +9 -29
@@ -2,6 +2,7 @@* QEMU System Emulator block driver * * Copyright (c) 2003 Fabrice Bellard +* Copyright (c) 2020 Virtuozzo International GmbH. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal
@@ -2204,23 +2205,15 @@ static void bdrv_replace_child(BdrvChild *child, BlockDriverState *new_bs,/* old_bs reference is transparently moved from @child to @s */ } -/* -* Check whether permissions on this node can be changed in a way that -* @cumulative_perms and @cumulative_shared_perms are the new cumulative -* permissions of all its parents. This involves checking whether all necessary -* permission changes to child nodes can be performed. -* -* A call to this function must always be followed by a call to bdrv_set_perm() -* or bdrv_abort_perm_update(). -*/ -static int bdrv_node_check_perm (BlockDriverState *bs,BlockReopenQueue *q, -uint64_t cumulative_perms, -uint64_t cumulative_shared_perms, -GSList **tran,Error **errp) +static int bdrv_node_refresh_perm (BlockDriverState *bs,BlockReopenQueue *q, +GSList **tran,Error **errp) {BlockDriver *drv = bs->drv;BdrvChild *c;int ret; +uint64_t cumulative_perms, cumulative_shared_perms; + +bdrv_get_cumulative_perm (bs, &cumulative_perms, &cumulative_shared_perms);/* Write permissions never work with read-only images */ if ((cumulative_perms & (BLK_PERM_WRITE | BLK_PERM_WRITE_UNCHANGED)) &&
@@ -2229,15 +2222,8 @@ static int bdrv_node_check_perm(BlockDriverState *bs, BlockReopenQueue *q,if (!bdrv_is_writable_after_reopen (bs,NULL )) {error_setg (errp,"Block node is read-only" ); }else { -uint64_t current_perms, current_shared; -bdrv_get_cumulative_perm (bs, ¤t_perms, ¤t_shared); -if (current_perms & (BLK_PERM_WRITE |BLK_PERM_WRITE_UNCHANGED )) { -error_setg (errp,"Cannot make block node read-only, there is " -"a writer on it" ); - }else { -error_setg (errp,"Cannot make block node read-only and create " -"a writer on it" ); - } +error_setg (errp,"Cannot make block node read-only, there is " +"a writer on it" ); }return -EPERM ;
@@ -2293,7 +2279,6 @@ static int bdrv_list_refresh_perms(GSList *list, BlockReopenQueue *q, GSList **tran, Error **errp) {int ret; -uint64_t cumulative_perms, cumulative_shared_perms;BlockDriverState *bs;for ( ; list; list = list->next) {
@@ -2303,12 +2288,7 @@ static int bdrv_list_refresh_perms(GSList *list, BlockReopenQueue *q,return -EINVAL ; } -bdrv_get_cumulative_perm (bs, &cumulative_perms, - &cumulative_shared_perms); - - ret =bdrv_node_check_perm (bs, q, cumulative_perms, - cumulative_shared_perms, - tran, errp); + ret =bdrv_node_refresh_perm (bs, q, tran, errp);if (ret <0 ) {return ret; }
Signature · 1 line
2.21.3
Re: [PATCH v2 36/36] block: refactor bdrv_node_check_perm()
Am 27.11.2020 um 15:45 hat Vladimir Sementsov-Ogievskiy geschrieben:
Quoted 7 lines
Now,
bdrv_node_check_perm()is called only with fresh cumulative permissions, so its actually "refresh_perm".Move permission calculation to the function. Also, drop unreachable error message.
Add also Virtuozzo copyright, as big work is done at this point.
I guess we could add many copyright lines then... Maybe we should, I don't know.
Quoted 20 lines
Signed-off-by Vladimir Sementsov-Ogievskiy <[email protected]>--- block.c | 38 +++++++++----------------------------- 1 file changed, 9 insertions(+), 29 deletions(-)block.c +1
-0@@ -2,6 +2,7 @@* QEMU System Emulator block driver * * Copyright (c) 2003 Fabrice Bellard +* Copyright (c) 2020 Virtuozzo International GmbH. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -2204,23 +2205,15 @@ static void bdrv_replace_child(BdrvChild *child, BlockDriverState *new_bs,/* old_bs reference is transparently moved from @child to @s */ }
block.c static void bdrv_replace_child(BdrvChild *child, BlockDriverState *new_bs, · line 2215
Quoted 10 lines
block.c +0
-9-/* -* Check whether permissions on this node can be changed in a way that -* @cumulative_perms and @cumulative_shared_perms are the new cumulative -* permissions of all its parents. This involves checking whether all necessary -* permission changes to child nodes can be performed. -* -* A call to this function must always be followed by a call to bdrv_set_perm() -* or bdrv_abort_perm_update(). -*/
Would you mind updating the comment rather than removing it?
Quoted 21 lines
block.c +5
-5-static int bdrv_node_check_perm (BlockDriverState *bs,BlockReopenQueue *q, -uint64_t cumulative_perms, -uint64_t cumulative_shared_perms, -GSList **tran,Error **errp) +static int bdrv_node_refresh_perm (BlockDriverState *bs,BlockReopenQueue *q, +GSList **tran,Error **errp) {BlockDriver *drv = bs->drv;BdrvChild *c;int ret; +uint64_t cumulative_perms, cumulative_shared_perms; + +bdrv_get_cumulative_perm (bs, &cumulative_perms, &cumulative_shared_perms);/* Write permissions never work with read-only images */ if ((cumulative_perms & (BLK_PERM_WRITE | BLK_PERM_WRITE_UNCHANGED)) &&@@ -2229,15 +2222,8 @@ static int bdrv_node_check_perm(BlockDriverState *bs, BlockReopenQueue *q,if (!bdrv_is_writable_after_reopen (bs,NULL )) {error_setg (errp,"Block node is read-only" ); }else { -uint64_t current_perms, current_shared;
block.c static int bdrv_node_check_perm(BlockDriverState *bs, BlockReopenQueue *q, · line 2226
Quoted 10 lines
block.c +2
-8-bdrv_get_cumulative_perm (bs, ¤t_perms, ¤t_shared); -if (current_perms & (BLK_PERM_WRITE |BLK_PERM_WRITE_UNCHANGED )) { -error_setg (errp,"Cannot make block node read-only, there is " -"a writer on it" ); - }else { -error_setg (errp,"Cannot make block node read-only and create " -"a writer on it" ); - } +error_setg (errp,"Cannot make block node read-only, there is " +"a writer on it" );
Hm, so if you want to add a new writer to an existing read-only node, this is the error message that you would get?
Now that we can't distinguish both cases any more, should we try to rephrase it so that it makes sense for both directions? Like "Read-only block node <node-name> cannot support read-write users"?
Sorry for it taking so long, but I've now finally looked at all patches in this series. Please feel free to send v3 when you're ready.
Kevin
Re: [PATCH v2 36/36] block: refactor bdrv_node_check_perm()
Quoted 80 lines · Kevin Wolf
Am 27.11.2020 um 15:45 hat Vladimir Sementsov-Ogievskiy geschrieben:
Quoted 7 lines
Now,
bdrv_node_check_perm()is called only with fresh cumulative permissions, so its actually "refresh_perm".Move permission calculation to the function. Also, drop unreachable error message.
Add also Virtuozzo copyright, as big work is done at this point.
I guess we could add many copyright lines then... Maybe we should, I don't know.
Quoted 20 lines
Signed-off-by Vladimir Sementsov-Ogievskiy <[email protected]>--- block.c | 38 +++++++++----------------------------- 1 file changed, 9 insertions(+), 29 deletions(-)block.c +1
-0@@ -2,6 +2,7 @@* QEMU System Emulator block driver * * Copyright (c) 2003 Fabrice Bellard +* Copyright (c) 2020 Virtuozzo International GmbH. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -2204,23 +2205,15 @@ static void bdrv_replace_child(BdrvChild *child, BlockDriverState *new_bs,/* old_bs reference is transparently moved from @child to @s */ }block.c static void bdrv_replace_child(BdrvChild *child, BlockDriverState *new_bs, · line 2215
Quoted 10 lines
block.c +0
-9-/* -* Check whether permissions on this node can be changed in a way that -* @cumulative_perms and @cumulative_shared_perms are the new cumulative -* permissions of all its parents. This involves checking whether all necessary -* permission changes to child nodes can be performed. -* -* A call to this function must always be followed by a call to bdrv_set_perm() -* or bdrv_abort_perm_update(). -*/ Would you mind updating the comment rather than removing it?
Quoted 21 lines
block.c +5
-5-static int bdrv_node_check_perm (BlockDriverState *bs,BlockReopenQueue *q, -uint64_t cumulative_perms, -uint64_t cumulative_shared_perms, -GSList **tran,Error **errp) +static int bdrv_node_refresh_perm (BlockDriverState *bs,BlockReopenQueue *q, +GSList **tran,Error **errp) {BlockDriver *drv = bs->drv;BdrvChild *c;int ret; +uint64_t cumulative_perms, cumulative_shared_perms; + +bdrv_get_cumulative_perm (bs, &cumulative_perms, &cumulative_shared_perms);/* Write permissions never work with read-only images */ if ((cumulative_perms & (BLK_PERM_WRITE | BLK_PERM_WRITE_UNCHANGED)) &&@@ -2229,15 +2222,8 @@ static int bdrv_node_check_perm(BlockDriverState *bs, BlockReopenQueue *q,if (!bdrv_is_writable_after_reopen (bs,NULL )) {error_setg (errp,"Block node is read-only" ); }else { -uint64_t current_perms, current_shared;block.c static int bdrv_node_check_perm(BlockDriverState *bs, BlockReopenQueue *q, · line 2226
Quoted 10 lines
block.c +2
-8-bdrv_get_cumulative_perm (bs, ¤t_perms, ¤t_shared); -if (current_perms & (BLK_PERM_WRITE |BLK_PERM_WRITE_UNCHANGED )) { -error_setg (errp,"Cannot make block node read-only, there is " -"a writer on it" ); - }else { -error_setg (errp,"Cannot make block node read-only and create " -"a writer on it" ); - } +error_setg (errp,"Cannot make block node read-only, there is " +"a writer on it" );Hm, so if you want to add a new writer to an existing read-only node, this is the error message that you would get?
Quoted 4 lines
Now that we can't distinguish both cases any more, should we try to rephrase it so that it makes sense for both directions? Like "Read-only block node <node-name> cannot support read-write users"?
OK.
Quoted 4 lines
Sorry for it taking so long, but I've now finally looked at all patches in this series. Please feel free to send v3 when you're ready.
Thanks a lot for reviewing!
Signature · 2 lines
Best regards, Vladimir
ping
Quoted 114 lines · Vladimir Sementsov-Ogievskiy
Hi all!
Here is a proposal of updating graph changing procedures.
The thing brought me here is a question about "activating" filters after insertion, which is done in
mirror_topandbackup_top. The problem is that we can't simply avoid permission conflict when inserting the
filter: during insertion old permissions of relations to be removed conflicting with new permissions of new created relations. And current solution is supporting additional "inactive" mode for the filter when it doesn't require any permissions.I suggest to change the order of operations: let's first do all graph relations modifications and then refresh permissions. Of course we'll need a way to restore old graph if refresh fails.
Another problem with permission update is that we update permissions in order of DFS which is not always correct. Better is update node when all its parents already updated and require correct permissions. This needs a topological sort of nodes prior to permission update, see more in patches later.
Patches plan:
01,02 - add failing tests to illustrate conceptual problems of current permission update system.
[Here is side suggestion: we usually add tests after fix, so careful
reviewer has to change order of patches to check that test fails before fix. I add tests in the way the may be simply executed but not yet take part in make check. It seems more native: first show the problem, then fix it. And when fixed, make tests available for make check]03-09 - some perparations, refactorings which may go in separate
10 - new transaction API
15 - toplogical sort implemented for permission update, one of new tests now pass
19 - improve
bdrv_replace_node. second new test now pass26 - drop .active field and activation procedure for backup-top!
30 - update
bdrv_reopen_multiple. At this point everything is using new paradigm of permission update31-36 - post refactoring, drop old interfaces, we are done.
Note, that this series does nothing with another graph-update problem discussed under "[PATCH RFC 0/5] Fix accidental crash in iotest 30".
The series based on block-next Max's branch and can be found here:
git: https://src.openvz.org/scm/~vsementsov/qemu.git
tag: up-block-topologic-perm-v2Vladimir Sementsov-Ogievskiy (36):
tests/test-bdrv-graph-mod: addtest_parallel_exclusive_writetests/test-bdrv-graph-mod: addtest_parallel_perm_update
block:bdrv_append(): don't consume reference
block:bdrv_append(): return status
block: addbdrv_parent_try_set_aio_context
block: BdrvChildClass: add .get_parent_aio_context handler
block: drop ctx argument frombdrv_root_attach_child
block: make bdrv_reopen_{prepare,commit,abort} private
block: return value frombdrv_replace_node()
util: addtransactions.c
block:bdrv_refresh_perms: check parents compliance
block: refactor bdrv_child* permission functions
block: rewritebdrv_child_try_set_perm()usingbdrv_refresh_perms()
block: inline bdrv_child_*() permission functions calls
block: use topological sort for permission update
block: addbdrv_drv_set_permtransaction action
block: add bdrv_list_* permission update functions
block: addbdrv_replace_child_safe()transaction action
block: fixbdrv_replace_node_common
block: addbdrv_attach_child_common()transaction action
block: addbdrv_attach_child_noperm()transaction action
block: split outbdrv_replace_node_noperm()
block: adaptbdrv_append()for inserting filters
block: addbdrv_remove_backingtransaction action
block: introducebdrv_drop_filter()
block/backup-top: drop .active
block: dropignore_childrenfor permission update functions
block: addbdrv_set_backing_noperm()transaction action
blockdev:qmp_x_blockdev_reopen: acquire all contexts
block:bdrv_reopen_multiple: refresh permissions on updated graph
block: drop unused permission update functions
block: inlinebdrv_check_perm_common()
block: inlinebdrv_replace_child()
block: refactorbdrv_child_set_perm_safe()transaction action
block: renamebdrv_replace_child_safe()tobdrv_replace_child()
block: refactorbdrv_node_check_perm()include/block/block.h | 20 +- include/block/block_int.h | 8 +- include/qemu/transactions.h | 46 ++ block.c | 1319 ++++++++++++++++++++--------------- block/backup-top.c | 39 +- block/block-backend.c | 13 +- block/commit.c | 7 +- block/file-posix.c | 84 +-- block/mirror.c | 9 +- blockdev.c | 33 +- blockjob.c | 11 +- tests/test-bdrv-drain.c | 2 +- tests/test-bdrv-graph-mod.c | 122 +++- util/transactions.c | 81 +++ tests/qemu-iotests/283.out | 2 +- util/meson.build | 1 + 16 files changed, 1100 insertions(+), 697 deletions(-)create mode 100644
include/qemu/transactions.h
create mode 100644util/transactions.c
Signature · 2 lines
Best regards, Vladimir