tests/test-bdrv-graph-mod: add test_parallel_perm_update
[qemu.git] / tests / unit / test-bdrv-graph-mod.c
bloba8219b131e13646ab3dc8e3359be80bb39333daa
1 /*
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,
33 BdrvChildRole role,
34 BlockReopenQueue *reopen_queue,
35 uint64_t perm, uint64_t shared,
36 uint64_t *nperm, uint64_t *nshared)
38 *nperm = 0;
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,
48 BdrvChildRole role,
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:
89 * +--------+
90 * | root |
91 * +--------+
92 * |
93 * | perm: write, read
94 * | shared: except write
95 * v
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.
105 * +--------+
106 * | root |
107 * +--------+
109 * | perm: write, read
110 * | shared: except write
112 * +-------------------+
113 * | passtrough filter |
114 * +-------------------+
115 * | |
116 * perm: write, read | | perm: write, read
117 * shared: except write | | shared: except write
118 * v v
119 * +----------------+
120 * | null co node |
121 * +----------------+
123 static void test_update_perm_tree(void)
125 int ret;
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);
141 blk_unref(root);
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:
151 * 1. initial graph:
153 * +------+ +--------+
154 * | root | | filter |
155 * +------+ +--------+
156 * | |
157 * root| target|
158 * v v
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:
168 * +------+ +------+
169 * | root | | root |
170 * +------+ +------+
171 * | |
172 * root| root|
173 * v v
174 * +--------+ target +--------+ target
175 * | filter |--------------+ | filter |--------------+
176 * +--------+ | +--------+ |
177 * | | | ^ v
178 * backing| | backing| | +--------+
179 * v v | +-----------| target |
180 * +------+ +--------+ v backing +--------+
181 * | node |<---------| target | +------+
182 * +------+ backing +--------+ | node |
183 * +------+
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);
205 bdrv_unref(bs);
206 blk_unref(root);
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:
226 bdrv_ref(base);
228 bdrv_attach_child(top, fl1, "backing", &child_of_bds, BDRV_CHILD_DATA,
229 &error_abort);
230 bdrv_attach_child(fl1, base, "backing", &child_of_bds, BDRV_CHILD_FILTERED,
231 &error_abort);
232 bdrv_attach_child(fl2, base, "backing", &child_of_bds, BDRV_CHILD_FILTERED,
233 &error_abort);
235 bdrv_replace_node(fl1, fl2, &error_abort);
237 bdrv_unref(fl2);
238 bdrv_unref(top);
241 static void write_to_file_perms(BlockDriverState *bs, BdrvChild *c,
242 BdrvChildRole role,
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;
250 } else {
251 *nperm = 0;
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
268 * permissions.
270 * And, these two children has a common base child, like this:
272 * ┌─────┐ ┌──────┐
273 * │ fl2 │ ◀── │ top │
274 * └─────┘ └──────┘
275 * │ │
276 * │ │ w
277 * │ ▼
278 * │ ┌──────┐
279 * │ │ fl1 │
280 * │ └──────┘
281 * │ │
282 * │ │ w
283 * │ ▼
284 * │ ┌──────┐
285 * └───────▶ │ base │
286 * └──────┘
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,
308 &error_abort);
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:
318 bdrv_ref(base);
320 bdrv_attach_child(top, tricky, "file", &child_of_bds, BDRV_CHILD_DATA,
321 &error_abort);
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,
327 &error_abort);
328 bdrv_attach_child(fl2, base, "backing", &child_of_bds, BDRV_CHILD_FILTERED,
329 &error_abort);
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));
352 bdrv_unref(top);
355 int main(int argc, char *argv[])
357 int i;
358 bool debug = false;
360 for (i = 1; i < argc; i++) {
361 if (!strcmp(argv[i], "-d")) {
362 debug = true;
363 break;
367 bdrv_init();
368 qemu_init_main_loop(&error_abort);
370 g_test_init(&argc, &argv, NULL);
372 g_test_add_func("/bdrv-graph-mod/update-perm-tree", test_update_perm_tree);
373 g_test_add_func("/bdrv-graph-mod/should-update-child",
374 test_should_update_child);
376 if (debug) {
377 g_test_add_func("/bdrv-graph-mod/parallel-exclusive-write",
378 test_parallel_exclusive_write);
379 g_test_add_func("/bdrv-graph-mod/parallel-perm-update",
380 test_parallel_perm_update);
383 return g_test_run();