]> git.proxmox.com Git - mirror_qemu.git/blob - tests/test-bdrv-graph-mod.c
tests: add test-bdrv-graph-mod
[mirror_qemu.git] / tests / test-bdrv-graph-mod.c
1 /*
2 * Block node graph modifications tests
3 *
4 * Copyright (c) 2019 Virtuozzo International GmbH. All rights reserved.
5 *
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.
10 *
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.
15 *
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/>.
18 *
19 */
20
21 #include "qemu/osdep.h"
22 #include "qapi/error.h"
23 #include "block/block_int.h"
24 #include "sysemu/block-backend.h"
25
26 static BlockDriver bdrv_pass_through = {
27 .format_name = "pass-through",
28 .bdrv_child_perm = bdrv_filter_default_perms,
29 };
30
31 static void no_perm_default_perms(BlockDriverState *bs, BdrvChild *c,
32 const BdrvChildRole *role,
33 BlockReopenQueue *reopen_queue,
34 uint64_t perm, uint64_t shared,
35 uint64_t *nperm, uint64_t *nshared)
36 {
37 *nperm = 0;
38 *nshared = BLK_PERM_ALL;
39 }
40
41 static BlockDriver bdrv_no_perm = {
42 .format_name = "no-perm",
43 .bdrv_child_perm = no_perm_default_perms,
44 };
45
46 static BlockDriverState *no_perm_node(const char *name)
47 {
48 return bdrv_new_open_driver(&bdrv_no_perm, name, BDRV_O_RDWR, &error_abort);
49 }
50
51 static BlockDriverState *pass_through_node(const char *name)
52 {
53 return bdrv_new_open_driver(&bdrv_pass_through, name,
54 BDRV_O_RDWR, &error_abort);
55 }
56
57 /*
58 * test_update_perm_tree
59 *
60 * When checking node for a possibility to update permissions, it's subtree
61 * should be correctly checked too. New permissions for each node should be
62 * calculated and checked in context of permissions of other nodes. If we
63 * check new permissions of the node only in context of old permissions of
64 * its neighbors, we can finish up with wrong permission graph.
65 *
66 * This test firstly create the following graph:
67 * +--------+
68 * | root |
69 * +--------+
70 * |
71 * | perm: write, read
72 * | shared: except write
73 * v
74 * +-------------------+ +----------------+
75 * | passtrough filter |---------->| null-co node |
76 * +-------------------+ +----------------+
77 *
78 *
79 * and then, tries to append filter under node. Expected behavior: fail.
80 * Otherwise we'll get the following picture, with two BdrvChild'ren, having
81 * write permission to one node, without actually sharing it.
82 *
83 * +--------+
84 * | root |
85 * +--------+
86 * |
87 * | perm: write, read
88 * | shared: except write
89 * v
90 * +-------------------+
91 * | passtrough filter |
92 * +-------------------+
93 * | |
94 * perm: write, read | | perm: write, read
95 * shared: except write | | shared: except write
96 * v v
97 * +----------------+
98 * | null co node |
99 * +----------------+
100 */
101 static void test_update_perm_tree(void)
102 {
103 Error *local_err = NULL;
104
105 BlockBackend *root = blk_new(BLK_PERM_WRITE | BLK_PERM_CONSISTENT_READ,
106 BLK_PERM_ALL & ~BLK_PERM_WRITE);
107 BlockDriverState *bs = no_perm_node("node");
108 BlockDriverState *filter = pass_through_node("filter");
109
110 blk_insert_bs(root, bs, &error_abort);
111
112 bdrv_attach_child(filter, bs, "child", &child_file, &error_abort);
113
114 bdrv_append(filter, bs, &local_err);
115
116 g_assert_nonnull(local_err);
117
118 bdrv_unref(bs);
119 blk_unref(root);
120 }
121
122 /*
123 * test_should_update_child
124 *
125 * Test that bdrv_replace_node, and concretely should_update_child
126 * do the right thing, i.e. not creating loops on the graph.
127 *
128 * The test does the following:
129 * 1. initial graph:
130 *
131 * +------+ +--------+
132 * | root | | filter |
133 * +------+ +--------+
134 * | |
135 * root| target|
136 * v v
137 * +------+ +--------+
138 * | node |<---------| target |
139 * +------+ backing +--------+
140 *
141 * 2. Append @filter above @node. If should_update_child works correctly,
142 * it understands, that backing child of @target should not be updated,
143 * as it will create a loop on node graph. Resulting picture should
144 * be the left one, not the right:
145 *
146 * +------+ +------+
147 * | root | | root |
148 * +------+ +------+
149 * | |
150 * root| root|
151 * v v
152 * +--------+ target +--------+ target
153 * | filter |--------------+ | filter |--------------+
154 * +--------+ | +--------+ |
155 * | | | ^ v
156 * backing| | backing| | +--------+
157 * v v | +-----------| target |
158 * +------+ +--------+ v backing +--------+
159 * | node |<---------| target | +------+
160 * +------+ backing +--------+ | node |
161 * +------+
162 *
163 * (good picture) (bad picture)
164 *
165 */
166 static void test_should_update_child(void)
167 {
168 BlockBackend *root = blk_new(0, BLK_PERM_ALL);
169 BlockDriverState *bs = no_perm_node("node");
170 BlockDriverState *filter = no_perm_node("filter");
171 BlockDriverState *target = no_perm_node("target");
172
173 blk_insert_bs(root, bs, &error_abort);
174
175 bdrv_set_backing_hd(target, bs, &error_abort);
176
177 g_assert(target->backing->bs == bs);
178 bdrv_attach_child(filter, target, "target", &child_file, &error_abort);
179 bdrv_append(filter, bs, &error_abort);
180 g_assert(target->backing->bs == bs);
181
182 bdrv_unref(bs);
183 blk_unref(root);
184 }
185
186 int main(int argc, char *argv[])
187 {
188 bdrv_init();
189 qemu_init_main_loop(&error_abort);
190
191 g_test_init(&argc, &argv, NULL);
192
193 g_test_add_func("/bdrv-graph-mod/update-perm-tree", test_update_perm_tree);
194 g_test_add_func("/bdrv-graph-mod/should-update-child",
195 test_should_update_child);
196
197 return g_test_run();
198 }