block: Use bdrv_default_perms()
[qemu.git] / tests / test-bdrv-graph-mod.c
bloba2d0318b165544bf53d0a14c77536e13d4e5529b
1 /*
2 * Block node graph modifications tests
4 * Copyright (c) 2019 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 const BdrvChildClass *child_class,
34 BdrvChildRole role,
35 BlockReopenQueue *reopen_queue,
36 uint64_t perm, uint64_t shared,
37 uint64_t *nperm, uint64_t *nshared)
39 *nperm = 0;
40 *nshared = BLK_PERM_ALL;
43 static BlockDriver bdrv_no_perm = {
44 .format_name = "no-perm",
45 .bdrv_child_perm = no_perm_default_perms,
48 static BlockDriverState *no_perm_node(const char *name)
50 return bdrv_new_open_driver(&bdrv_no_perm, name, BDRV_O_RDWR, &error_abort);
53 static BlockDriverState *pass_through_node(const char *name)
55 return bdrv_new_open_driver(&bdrv_pass_through, name,
56 BDRV_O_RDWR, &error_abort);
60 * test_update_perm_tree
62 * When checking node for a possibility to update permissions, it's subtree
63 * should be correctly checked too. New permissions for each node should be
64 * calculated and checked in context of permissions of other nodes. If we
65 * check new permissions of the node only in context of old permissions of
66 * its neighbors, we can finish up with wrong permission graph.
68 * This test firstly create the following graph:
69 * +--------+
70 * | root |
71 * +--------+
72 * |
73 * | perm: write, read
74 * | shared: except write
75 * v
76 * +-------------------+ +----------------+
77 * | passtrough filter |---------->| null-co node |
78 * +-------------------+ +----------------+
81 * and then, tries to append filter under node. Expected behavior: fail.
82 * Otherwise we'll get the following picture, with two BdrvChild'ren, having
83 * write permission to one node, without actually sharing it.
85 * +--------+
86 * | root |
87 * +--------+
88 * |
89 * | perm: write, read
90 * | shared: except write
91 * v
92 * +-------------------+
93 * | passtrough filter |
94 * +-------------------+
95 * | |
96 * perm: write, read | | perm: write, read
97 * shared: except write | | shared: except write
98 * v v
99 * +----------------+
100 * | null co node |
101 * +----------------+
103 static void test_update_perm_tree(void)
105 Error *local_err = NULL;
107 BlockBackend *root = blk_new(qemu_get_aio_context(),
108 BLK_PERM_WRITE | BLK_PERM_CONSISTENT_READ,
109 BLK_PERM_ALL & ~BLK_PERM_WRITE);
110 BlockDriverState *bs = no_perm_node("node");
111 BlockDriverState *filter = pass_through_node("filter");
113 blk_insert_bs(root, bs, &error_abort);
115 bdrv_attach_child(filter, bs, "child", &child_of_bds,
116 BDRV_CHILD_FILTERED | BDRV_CHILD_PRIMARY, &error_abort);
118 bdrv_append(filter, bs, &local_err);
120 g_assert_nonnull(local_err);
121 error_free(local_err);
123 blk_unref(root);
127 * test_should_update_child
129 * Test that bdrv_replace_node, and concretely should_update_child
130 * do the right thing, i.e. not creating loops on the graph.
132 * The test does the following:
133 * 1. initial graph:
135 * +------+ +--------+
136 * | root | | filter |
137 * +------+ +--------+
138 * | |
139 * root| target|
140 * v v
141 * +------+ +--------+
142 * | node |<---------| target |
143 * +------+ backing +--------+
145 * 2. Append @filter above @node. If should_update_child works correctly,
146 * it understands, that backing child of @target should not be updated,
147 * as it will create a loop on node graph. Resulting picture should
148 * be the left one, not the right:
150 * +------+ +------+
151 * | root | | root |
152 * +------+ +------+
153 * | |
154 * root| root|
155 * v v
156 * +--------+ target +--------+ target
157 * | filter |--------------+ | filter |--------------+
158 * +--------+ | +--------+ |
159 * | | | ^ v
160 * backing| | backing| | +--------+
161 * v v | +-----------| target |
162 * +------+ +--------+ v backing +--------+
163 * | node |<---------| target | +------+
164 * +------+ backing +--------+ | node |
165 * +------+
167 * (good picture) (bad picture)
170 static void test_should_update_child(void)
172 BlockBackend *root = blk_new(qemu_get_aio_context(), 0, BLK_PERM_ALL);
173 BlockDriverState *bs = no_perm_node("node");
174 BlockDriverState *filter = no_perm_node("filter");
175 BlockDriverState *target = no_perm_node("target");
177 blk_insert_bs(root, bs, &error_abort);
179 bdrv_set_backing_hd(target, bs, &error_abort);
181 g_assert(target->backing->bs == bs);
182 bdrv_attach_child(filter, target, "target", &child_of_bds,
183 BDRV_CHILD_DATA, &error_abort);
184 bdrv_append(filter, bs, &error_abort);
185 g_assert(target->backing->bs == bs);
187 bdrv_unref(bs);
188 blk_unref(root);
191 int main(int argc, char *argv[])
193 bdrv_init();
194 qemu_init_main_loop(&error_abort);
196 g_test_init(&argc, &argv, NULL);
198 g_test_add_func("/bdrv-graph-mod/update-perm-tree", test_update_perm_tree);
199 g_test_add_func("/bdrv-graph-mod/should-update-child",
200 test_should_update_child);
202 return g_test_run();