2 * Block node graph modifications tests
4 * Copyright (c) 2019-2021 Virtuozzo International GmbH. All rights reserved.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "qemu/osdep.h"
22 #include "qapi/error.h"
23 #include "qemu/main-loop.h"
24 #include "block/block_int.h"
25 #include "sysemu/block-backend.h"
27 static BlockDriver bdrv_pass_through
= {
28 .format_name
= "pass-through",
29 .bdrv_child_perm
= bdrv_default_perms
,
32 static void no_perm_default_perms(BlockDriverState
*bs
, BdrvChild
*c
,
34 BlockReopenQueue
*reopen_queue
,
35 uint64_t perm
, uint64_t shared
,
36 uint64_t *nperm
, uint64_t *nshared
)
39 *nshared
= BLK_PERM_ALL
;
42 static BlockDriver bdrv_no_perm
= {
43 .format_name
= "no-perm",
44 .bdrv_child_perm
= no_perm_default_perms
,
47 static void exclusive_write_perms(BlockDriverState
*bs
, BdrvChild
*c
,
49 BlockReopenQueue
*reopen_queue
,
50 uint64_t perm
, uint64_t shared
,
51 uint64_t *nperm
, uint64_t *nshared
)
53 *nperm
= BLK_PERM_WRITE
;
54 *nshared
= BLK_PERM_ALL
& ~BLK_PERM_WRITE
;
57 static BlockDriver bdrv_exclusive_writer
= {
58 .format_name
= "exclusive-writer",
59 .bdrv_child_perm
= exclusive_write_perms
,
62 static BlockDriverState
*no_perm_node(const char *name
)
64 return bdrv_new_open_driver(&bdrv_no_perm
, name
, BDRV_O_RDWR
, &error_abort
);
67 static BlockDriverState
*pass_through_node(const char *name
)
69 return bdrv_new_open_driver(&bdrv_pass_through
, name
,
70 BDRV_O_RDWR
, &error_abort
);
73 static BlockDriverState
*exclusive_writer_node(const char *name
)
75 return bdrv_new_open_driver(&bdrv_exclusive_writer
, name
,
76 BDRV_O_RDWR
, &error_abort
);
80 * test_update_perm_tree
82 * When checking node for a possibility to update permissions, it's subtree
83 * should be correctly checked too. New permissions for each node should be
84 * calculated and checked in context of permissions of other nodes. If we
85 * check new permissions of the node only in context of old permissions of
86 * its neighbors, we can finish up with wrong permission graph.
88 * This test firstly create the following graph:
94 * | shared: except write
96 * +-------------------+ +----------------+
97 * | passtrough filter |---------->| null-co node |
98 * +-------------------+ +----------------+
101 * and then, tries to append filter under node. Expected behavior: fail.
102 * Otherwise we'll get the following picture, with two BdrvChild'ren, having
103 * write permission to one node, without actually sharing it.
109 * | perm: write, read
110 * | shared: except write
112 * +-------------------+
113 * | passtrough filter |
114 * +-------------------+
116 * perm: write, read | | perm: write, read
117 * shared: except write | | shared: except write
123 static void test_update_perm_tree(void)
127 BlockBackend
*root
= blk_new(qemu_get_aio_context(),
128 BLK_PERM_WRITE
| BLK_PERM_CONSISTENT_READ
,
129 BLK_PERM_ALL
& ~BLK_PERM_WRITE
);
130 BlockDriverState
*bs
= no_perm_node("node");
131 BlockDriverState
*filter
= pass_through_node("filter");
133 blk_insert_bs(root
, bs
, &error_abort
);
135 bdrv_attach_child(filter
, bs
, "child", &child_of_bds
,
136 BDRV_CHILD_FILTERED
| BDRV_CHILD_PRIMARY
, &error_abort
);
138 ret
= bdrv_append(filter
, bs
, NULL
);
139 g_assert_cmpint(ret
, <, 0);
145 * test_should_update_child
147 * Test that bdrv_replace_node, and concretely should_update_child
148 * do the right thing, i.e. not creating loops on the graph.
150 * The test does the following:
153 * +------+ +--------+
154 * | root | | filter |
155 * +------+ +--------+
159 * +------+ +--------+
160 * | node |<---------| target |
161 * +------+ backing +--------+
163 * 2. Append @filter above @node. If should_update_child works correctly,
164 * it understands, that backing child of @target should not be updated,
165 * as it will create a loop on node graph. Resulting picture should
166 * be the left one, not the right:
174 * +--------+ target +--------+ target
175 * | filter |--------------+ | filter |--------------+
176 * +--------+ | +--------+ |
178 * backing| | backing| | +--------+
179 * v v | +-----------| target |
180 * +------+ +--------+ v backing +--------+
181 * | node |<---------| target | +------+
182 * +------+ backing +--------+ | node |
185 * (good picture) (bad picture)
188 static void test_should_update_child(void)
190 BlockBackend
*root
= blk_new(qemu_get_aio_context(), 0, BLK_PERM_ALL
);
191 BlockDriverState
*bs
= no_perm_node("node");
192 BlockDriverState
*filter
= no_perm_node("filter");
193 BlockDriverState
*target
= no_perm_node("target");
195 blk_insert_bs(root
, bs
, &error_abort
);
197 bdrv_set_backing_hd(target
, bs
, &error_abort
);
199 g_assert(target
->backing
->bs
== bs
);
200 bdrv_attach_child(filter
, target
, "target", &child_of_bds
,
201 BDRV_CHILD_DATA
, &error_abort
);
202 bdrv_append(filter
, bs
, &error_abort
);
203 g_assert(target
->backing
->bs
== bs
);
210 * test_parallel_exclusive_write
212 * Check that when we replace node, old permissions of the node being removed
213 * doesn't break the replacement.
215 static void test_parallel_exclusive_write(void)
217 BlockDriverState
*top
= exclusive_writer_node("top");
218 BlockDriverState
*base
= no_perm_node("base");
219 BlockDriverState
*fl1
= pass_through_node("fl1");
220 BlockDriverState
*fl2
= pass_through_node("fl2");
223 * bdrv_attach_child() eats child bs reference, so we need two @base
224 * references for two filters:
228 bdrv_attach_child(top
, fl1
, "backing", &child_of_bds
, BDRV_CHILD_DATA
,
230 bdrv_attach_child(fl1
, base
, "backing", &child_of_bds
, BDRV_CHILD_FILTERED
,
232 bdrv_attach_child(fl2
, base
, "backing", &child_of_bds
, BDRV_CHILD_FILTERED
,
235 bdrv_replace_node(fl1
, fl2
, &error_abort
);
241 static void write_to_file_perms(BlockDriverState
*bs
, BdrvChild
*c
,
243 BlockReopenQueue
*reopen_queue
,
244 uint64_t perm
, uint64_t shared
,
245 uint64_t *nperm
, uint64_t *nshared
)
247 if (bs
->file
&& c
== bs
->file
) {
248 *nperm
= BLK_PERM_WRITE
;
249 *nshared
= BLK_PERM_ALL
& ~BLK_PERM_WRITE
;
252 *nshared
= BLK_PERM_ALL
;
256 static BlockDriver bdrv_write_to_file
= {
257 .format_name
= "tricky-perm",
258 .bdrv_child_perm
= write_to_file_perms
,
263 * The following test shows that topological-sort order is required for
264 * permission update, simple DFS is not enough.
266 * Consider the block driver which has two filter children: one active
267 * with exclusive write access and one inactive with no specific
270 * And, these two children has a common base child, like this:
273 * │ fl2 │ ◀── │ top │
288 * So, exclusive write is propagated.
290 * Assume, we want to make fl2 active instead of fl1.
291 * So, we set some option for top driver and do permission update.
293 * With simple DFS, if permission update goes first through
294 * top->fl1->base branch it will succeed: it firstly drop exclusive write
295 * permissions and than apply them for another BdrvChildren.
296 * But if permission update goes first through top->fl2->base branch it
297 * will fail, as when we try to update fl2->base child, old not yet
298 * updated fl1->base child will be in conflict.
300 * With topological-sort order we always update parents before children, so fl1
301 * and fl2 are both updated when we update base and there is no conflict.
303 static void test_parallel_perm_update(void)
305 BlockDriverState
*top
= no_perm_node("top");
306 BlockDriverState
*tricky
=
307 bdrv_new_open_driver(&bdrv_write_to_file
, "tricky", BDRV_O_RDWR
,
309 BlockDriverState
*base
= no_perm_node("base");
310 BlockDriverState
*fl1
= pass_through_node("fl1");
311 BlockDriverState
*fl2
= pass_through_node("fl2");
312 BdrvChild
*c_fl1
, *c_fl2
;
315 * bdrv_attach_child() eats child bs reference, so we need two @base
316 * references for two filters:
320 bdrv_attach_child(top
, tricky
, "file", &child_of_bds
, BDRV_CHILD_DATA
,
322 c_fl1
= bdrv_attach_child(tricky
, fl1
, "first", &child_of_bds
,
323 BDRV_CHILD_FILTERED
, &error_abort
);
324 c_fl2
= bdrv_attach_child(tricky
, fl2
, "second", &child_of_bds
,
325 BDRV_CHILD_FILTERED
, &error_abort
);
326 bdrv_attach_child(fl1
, base
, "backing", &child_of_bds
, BDRV_CHILD_FILTERED
,
328 bdrv_attach_child(fl2
, base
, "backing", &child_of_bds
, BDRV_CHILD_FILTERED
,
331 /* Select fl1 as first child to be active */
332 tricky
->file
= c_fl1
;
333 bdrv_child_refresh_perms(top
, top
->children
.lh_first
, &error_abort
);
335 assert(c_fl1
->perm
& BLK_PERM_WRITE
);
336 assert(!(c_fl2
->perm
& BLK_PERM_WRITE
));
338 /* Now, try to switch active child and update permissions */
339 tricky
->file
= c_fl2
;
340 bdrv_child_refresh_perms(top
, top
->children
.lh_first
, &error_abort
);
342 assert(c_fl2
->perm
& BLK_PERM_WRITE
);
343 assert(!(c_fl1
->perm
& BLK_PERM_WRITE
));
345 /* Switch once more, to not care about real child order in the list */
346 tricky
->file
= c_fl1
;
347 bdrv_child_refresh_perms(top
, top
->children
.lh_first
, &error_abort
);
349 assert(c_fl1
->perm
& BLK_PERM_WRITE
);
350 assert(!(c_fl2
->perm
& BLK_PERM_WRITE
));
356 * It's possible that filter required permissions allows to insert it to backing
359 * 1. [top] -> [filter] -> [base]
361 * but doesn't allow to add it as a branch:
367 * So, inserting such filter should do all graph modifications and only then
368 * update permissions. If we try to go through intermediate state [2] and update
369 * permissions on it we'll fail.
371 * Let's check that bdrv_append() can append such a filter.
373 static void test_append_greedy_filter(void)
375 BlockDriverState
*top
= exclusive_writer_node("top");
376 BlockDriverState
*base
= no_perm_node("base");
377 BlockDriverState
*fl
= exclusive_writer_node("fl1");
379 bdrv_attach_child(top
, base
, "backing", &child_of_bds
, BDRV_CHILD_COW
,
382 bdrv_append(fl
, base
, &error_abort
);
386 int main(int argc
, char *argv
[])
391 for (i
= 1; i
< argc
; i
++) {
392 if (!strcmp(argv
[i
], "-d")) {
399 qemu_init_main_loop(&error_abort
);
401 g_test_init(&argc
, &argv
, NULL
);
403 g_test_add_func("/bdrv-graph-mod/update-perm-tree", test_update_perm_tree
);
404 g_test_add_func("/bdrv-graph-mod/should-update-child",
405 test_should_update_child
);
408 g_test_add_func("/bdrv-graph-mod/parallel-exclusive-write",
409 test_parallel_exclusive_write
);
410 g_test_add_func("/bdrv-graph-mod/parallel-perm-update",
411 test_parallel_perm_update
);
412 g_test_add_func("/bdrv-graph-mod/append-greedy-filter",
413 test_append_greedy_filter
);