]>
Commit | Line | Data |
---|---|---|
2dbfadf6 VSO |
1 | /* |
2 | * Block node graph modifications tests | |
3 | * | |
d71cc67d | 4 | * Copyright (c) 2019-2021 Virtuozzo International GmbH. All rights reserved. |
2dbfadf6 VSO |
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" | |
db725815 | 23 | #include "qemu/main-loop.h" |
2dbfadf6 VSO |
24 | #include "block/block_int.h" |
25 | #include "sysemu/block-backend.h" | |
26 | ||
27 | static BlockDriver bdrv_pass_through = { | |
28 | .format_name = "pass-through", | |
1921b4f7 VSO |
29 | .is_filter = true, |
30 | .filtered_child_is_backing = true, | |
69dca43d | 31 | .bdrv_child_perm = bdrv_default_perms, |
2dbfadf6 VSO |
32 | }; |
33 | ||
34 | static void no_perm_default_perms(BlockDriverState *bs, BdrvChild *c, | |
bf8e925e | 35 | BdrvChildRole role, |
2dbfadf6 VSO |
36 | BlockReopenQueue *reopen_queue, |
37 | uint64_t perm, uint64_t shared, | |
38 | uint64_t *nperm, uint64_t *nshared) | |
39 | { | |
40 | *nperm = 0; | |
41 | *nshared = BLK_PERM_ALL; | |
42 | } | |
43 | ||
44 | static BlockDriver bdrv_no_perm = { | |
45 | .format_name = "no-perm", | |
25f78d9e | 46 | .supports_backing = true, |
2dbfadf6 VSO |
47 | .bdrv_child_perm = no_perm_default_perms, |
48 | }; | |
49 | ||
d71cc67d VSO |
50 | static void exclusive_write_perms(BlockDriverState *bs, BdrvChild *c, |
51 | BdrvChildRole role, | |
52 | BlockReopenQueue *reopen_queue, | |
53 | uint64_t perm, uint64_t shared, | |
54 | uint64_t *nperm, uint64_t *nshared) | |
55 | { | |
56 | *nperm = BLK_PERM_WRITE; | |
57 | *nshared = BLK_PERM_ALL & ~BLK_PERM_WRITE; | |
58 | } | |
59 | ||
60 | static BlockDriver bdrv_exclusive_writer = { | |
61 | .format_name = "exclusive-writer", | |
1921b4f7 VSO |
62 | .is_filter = true, |
63 | .filtered_child_is_backing = true, | |
d71cc67d VSO |
64 | .bdrv_child_perm = exclusive_write_perms, |
65 | }; | |
66 | ||
2dbfadf6 VSO |
67 | static BlockDriverState *no_perm_node(const char *name) |
68 | { | |
69 | return bdrv_new_open_driver(&bdrv_no_perm, name, BDRV_O_RDWR, &error_abort); | |
70 | } | |
71 | ||
72 | static BlockDriverState *pass_through_node(const char *name) | |
73 | { | |
74 | return bdrv_new_open_driver(&bdrv_pass_through, name, | |
75 | BDRV_O_RDWR, &error_abort); | |
76 | } | |
77 | ||
d71cc67d VSO |
78 | static BlockDriverState *exclusive_writer_node(const char *name) |
79 | { | |
80 | return bdrv_new_open_driver(&bdrv_exclusive_writer, name, | |
81 | BDRV_O_RDWR, &error_abort); | |
82 | } | |
83 | ||
2dbfadf6 VSO |
84 | /* |
85 | * test_update_perm_tree | |
86 | * | |
87 | * When checking node for a possibility to update permissions, it's subtree | |
88 | * should be correctly checked too. New permissions for each node should be | |
89 | * calculated and checked in context of permissions of other nodes. If we | |
90 | * check new permissions of the node only in context of old permissions of | |
91 | * its neighbors, we can finish up with wrong permission graph. | |
92 | * | |
93 | * This test firstly create the following graph: | |
94 | * +--------+ | |
95 | * | root | | |
96 | * +--------+ | |
97 | * | | |
98 | * | perm: write, read | |
99 | * | shared: except write | |
100 | * v | |
96420a30 MT |
101 | * +--------------------+ +----------------+ |
102 | * | passthrough filter |--------->| null-co node | | |
103 | * +--------------------+ +----------------+ | |
2dbfadf6 VSO |
104 | * |
105 | * | |
106 | * and then, tries to append filter under node. Expected behavior: fail. | |
107 | * Otherwise we'll get the following picture, with two BdrvChild'ren, having | |
108 | * write permission to one node, without actually sharing it. | |
109 | * | |
110 | * +--------+ | |
111 | * | root | | |
112 | * +--------+ | |
113 | * | | |
114 | * | perm: write, read | |
115 | * | shared: except write | |
116 | * v | |
96420a30 MT |
117 | * +--------------------+ |
118 | * | passthrough filter | | |
119 | * +--------------------+ | |
2dbfadf6 VSO |
120 | * | | |
121 | * perm: write, read | | perm: write, read | |
122 | * shared: except write | | shared: except write | |
123 | * v v | |
124 | * +----------------+ | |
125 | * | null co node | | |
126 | * +----------------+ | |
127 | */ | |
128 | static void test_update_perm_tree(void) | |
129 | { | |
934aee14 | 130 | int ret; |
2dbfadf6 | 131 | |
d861ab3a KW |
132 | BlockBackend *root = blk_new(qemu_get_aio_context(), |
133 | BLK_PERM_WRITE | BLK_PERM_CONSISTENT_READ, | |
2dbfadf6 VSO |
134 | BLK_PERM_ALL & ~BLK_PERM_WRITE); |
135 | BlockDriverState *bs = no_perm_node("node"); | |
136 | BlockDriverState *filter = pass_through_node("filter"); | |
137 | ||
138 | blk_insert_bs(root, bs, &error_abort); | |
139 | ||
afdaeb9e | 140 | bdrv_graph_wrlock(NULL); |
a16be3cd | 141 | bdrv_attach_child(filter, bs, "child", &child_of_bds, |
1921b4f7 | 142 | BDRV_CHILD_DATA, &error_abort); |
afdaeb9e | 143 | bdrv_graph_wrunlock(); |
2dbfadf6 | 144 | |
487b9187 | 145 | aio_context_acquire(qemu_get_aio_context()); |
934aee14 VSO |
146 | ret = bdrv_append(filter, bs, NULL); |
147 | g_assert_cmpint(ret, <, 0); | |
487b9187 | 148 | aio_context_release(qemu_get_aio_context()); |
2dbfadf6 | 149 | |
ae9d4417 | 150 | bdrv_unref(filter); |
2dbfadf6 VSO |
151 | blk_unref(root); |
152 | } | |
153 | ||
154 | /* | |
155 | * test_should_update_child | |
156 | * | |
157 | * Test that bdrv_replace_node, and concretely should_update_child | |
158 | * do the right thing, i.e. not creating loops on the graph. | |
159 | * | |
160 | * The test does the following: | |
161 | * 1. initial graph: | |
162 | * | |
163 | * +------+ +--------+ | |
164 | * | root | | filter | | |
165 | * +------+ +--------+ | |
166 | * | | | |
167 | * root| target| | |
168 | * v v | |
169 | * +------+ +--------+ | |
170 | * | node |<---------| target | | |
171 | * +------+ backing +--------+ | |
172 | * | |
173 | * 2. Append @filter above @node. If should_update_child works correctly, | |
174 | * it understands, that backing child of @target should not be updated, | |
175 | * as it will create a loop on node graph. Resulting picture should | |
176 | * be the left one, not the right: | |
177 | * | |
178 | * +------+ +------+ | |
179 | * | root | | root | | |
180 | * +------+ +------+ | |
181 | * | | | |
182 | * root| root| | |
183 | * v v | |
184 | * +--------+ target +--------+ target | |
185 | * | filter |--------------+ | filter |--------------+ | |
186 | * +--------+ | +--------+ | | |
187 | * | | | ^ v | |
188 | * backing| | backing| | +--------+ | |
189 | * v v | +-----------| target | | |
190 | * +------+ +--------+ v backing +--------+ | |
191 | * | node |<---------| target | +------+ | |
192 | * +------+ backing +--------+ | node | | |
193 | * +------+ | |
194 | * | |
195 | * (good picture) (bad picture) | |
196 | * | |
197 | */ | |
198 | static void test_should_update_child(void) | |
199 | { | |
d861ab3a | 200 | BlockBackend *root = blk_new(qemu_get_aio_context(), 0, BLK_PERM_ALL); |
2dbfadf6 VSO |
201 | BlockDriverState *bs = no_perm_node("node"); |
202 | BlockDriverState *filter = no_perm_node("filter"); | |
203 | BlockDriverState *target = no_perm_node("target"); | |
204 | ||
205 | blk_insert_bs(root, bs, &error_abort); | |
206 | ||
207 | bdrv_set_backing_hd(target, bs, &error_abort); | |
208 | ||
209 | g_assert(target->backing->bs == bs); | |
afdaeb9e | 210 | bdrv_graph_wrlock(NULL); |
a16be3cd HR |
211 | bdrv_attach_child(filter, target, "target", &child_of_bds, |
212 | BDRV_CHILD_DATA, &error_abort); | |
afdaeb9e | 213 | bdrv_graph_wrunlock(); |
487b9187 | 214 | aio_context_acquire(qemu_get_aio_context()); |
2dbfadf6 | 215 | bdrv_append(filter, bs, &error_abort); |
487b9187 | 216 | aio_context_release(qemu_get_aio_context()); |
2dbfadf6 VSO |
217 | g_assert(target->backing->bs == bs); |
218 | ||
ae9d4417 | 219 | bdrv_unref(filter); |
2dbfadf6 VSO |
220 | bdrv_unref(bs); |
221 | blk_unref(root); | |
222 | } | |
223 | ||
d71cc67d VSO |
224 | /* |
225 | * test_parallel_exclusive_write | |
226 | * | |
227 | * Check that when we replace node, old permissions of the node being removed | |
228 | * doesn't break the replacement. | |
229 | */ | |
230 | static void test_parallel_exclusive_write(void) | |
231 | { | |
232 | BlockDriverState *top = exclusive_writer_node("top"); | |
233 | BlockDriverState *base = no_perm_node("base"); | |
234 | BlockDriverState *fl1 = pass_through_node("fl1"); | |
235 | BlockDriverState *fl2 = pass_through_node("fl2"); | |
236 | ||
ccd6a379 KW |
237 | bdrv_drained_begin(fl1); |
238 | bdrv_drained_begin(fl2); | |
239 | ||
d71cc67d VSO |
240 | /* |
241 | * bdrv_attach_child() eats child bs reference, so we need two @base | |
ccd6a379 KW |
242 | * references for two filters. We also need an additional @fl1 reference so |
243 | * that it still exists when we want to undrain it. | |
d71cc67d VSO |
244 | */ |
245 | bdrv_ref(base); | |
ccd6a379 | 246 | bdrv_ref(fl1); |
d71cc67d | 247 | |
afdaeb9e | 248 | bdrv_graph_wrlock(NULL); |
1921b4f7 VSO |
249 | bdrv_attach_child(top, fl1, "backing", &child_of_bds, |
250 | BDRV_CHILD_FILTERED | BDRV_CHILD_PRIMARY, | |
d71cc67d | 251 | &error_abort); |
1921b4f7 VSO |
252 | bdrv_attach_child(fl1, base, "backing", &child_of_bds, |
253 | BDRV_CHILD_FILTERED | BDRV_CHILD_PRIMARY, | |
d71cc67d | 254 | &error_abort); |
1921b4f7 VSO |
255 | bdrv_attach_child(fl2, base, "backing", &child_of_bds, |
256 | BDRV_CHILD_FILTERED | BDRV_CHILD_PRIMARY, | |
d71cc67d VSO |
257 | &error_abort); |
258 | ||
259 | bdrv_replace_node(fl1, fl2, &error_abort); | |
ccd6a379 KW |
260 | bdrv_graph_wrunlock(); |
261 | ||
262 | bdrv_drained_end(fl2); | |
263 | bdrv_drained_end(fl1); | |
d71cc67d | 264 | |
ccd6a379 | 265 | bdrv_unref(fl1); |
d71cc67d VSO |
266 | bdrv_unref(fl2); |
267 | bdrv_unref(top); | |
268 | } | |
269 | ||
1dcea719 VSO |
270 | /* |
271 | * write-to-selected node may have several DATA children, one of them may be | |
272 | * "selected". Exclusive write permission is taken on selected child. | |
273 | * | |
274 | * We don't realize write handler itself, as we need only to test how permission | |
275 | * update works. | |
276 | */ | |
277 | typedef struct BDRVWriteToSelectedState { | |
278 | BdrvChild *selected; | |
279 | } BDRVWriteToSelectedState; | |
280 | ||
281 | static void write_to_selected_perms(BlockDriverState *bs, BdrvChild *c, | |
282 | BdrvChildRole role, | |
283 | BlockReopenQueue *reopen_queue, | |
284 | uint64_t perm, uint64_t shared, | |
285 | uint64_t *nperm, uint64_t *nshared) | |
e6af4f0e | 286 | { |
1dcea719 VSO |
287 | BDRVWriteToSelectedState *s = bs->opaque; |
288 | ||
289 | if (s->selected && c == s->selected) { | |
e6af4f0e VSO |
290 | *nperm = BLK_PERM_WRITE; |
291 | *nshared = BLK_PERM_ALL & ~BLK_PERM_WRITE; | |
292 | } else { | |
293 | *nperm = 0; | |
294 | *nshared = BLK_PERM_ALL; | |
295 | } | |
296 | } | |
297 | ||
1dcea719 VSO |
298 | static BlockDriver bdrv_write_to_selected = { |
299 | .format_name = "write-to-selected", | |
300 | .instance_size = sizeof(BDRVWriteToSelectedState), | |
301 | .bdrv_child_perm = write_to_selected_perms, | |
e6af4f0e VSO |
302 | }; |
303 | ||
304 | ||
305 | /* | |
306 | * The following test shows that topological-sort order is required for | |
307 | * permission update, simple DFS is not enough. | |
308 | * | |
1dcea719 VSO |
309 | * Consider the block driver (write-to-selected) which has two children: one is |
310 | * selected so we have exclusive write access to it and for the other one we | |
311 | * don't need any specific permissions. | |
e6af4f0e VSO |
312 | * |
313 | * And, these two children has a common base child, like this: | |
1dcea719 VSO |
314 | * (additional "top" on top is used in test just because the only public |
315 | * function to update permission should get a specific child to update. | |
316 | * Making bdrv_refresh_perms() public just for this test isn't worth it) | |
e6af4f0e | 317 | * |
1dcea719 VSO |
318 | * ┌─────┐ ┌───────────────────┐ ┌─────┐ |
319 | * │ fl2 │ ◀── │ write-to-selected │ ◀── │ top │ | |
320 | * └─────┘ └───────────────────┘ └─────┘ | |
e6af4f0e VSO |
321 | * │ │ |
322 | * │ │ w | |
323 | * │ ▼ | |
324 | * │ ┌──────┐ | |
325 | * │ │ fl1 │ | |
326 | * │ └──────┘ | |
327 | * │ │ | |
328 | * │ │ w | |
329 | * │ ▼ | |
330 | * │ ┌──────┐ | |
331 | * └───────▶ │ base │ | |
332 | * └──────┘ | |
333 | * | |
334 | * So, exclusive write is propagated. | |
335 | * | |
1dcea719 VSO |
336 | * Assume, we want to select fl2 instead of fl1. |
337 | * So, we set some option for write-to-selected driver and do permission update. | |
e6af4f0e VSO |
338 | * |
339 | * With simple DFS, if permission update goes first through | |
1dcea719 VSO |
340 | * write-to-selected -> fl1 -> base branch it will succeed: it firstly drop |
341 | * exclusive write permissions and than apply them for another BdrvChildren. | |
342 | * But if permission update goes first through write-to-selected -> fl2 -> base | |
343 | * branch it will fail, as when we try to update fl2->base child, old not yet | |
e6af4f0e VSO |
344 | * updated fl1->base child will be in conflict. |
345 | * | |
346 | * With topological-sort order we always update parents before children, so fl1 | |
347 | * and fl2 are both updated when we update base and there is no conflict. | |
348 | */ | |
349 | static void test_parallel_perm_update(void) | |
350 | { | |
351 | BlockDriverState *top = no_perm_node("top"); | |
1dcea719 VSO |
352 | BlockDriverState *ws = |
353 | bdrv_new_open_driver(&bdrv_write_to_selected, "ws", BDRV_O_RDWR, | |
e6af4f0e | 354 | &error_abort); |
1dcea719 | 355 | BDRVWriteToSelectedState *s = ws->opaque; |
e6af4f0e VSO |
356 | BlockDriverState *base = no_perm_node("base"); |
357 | BlockDriverState *fl1 = pass_through_node("fl1"); | |
358 | BlockDriverState *fl2 = pass_through_node("fl2"); | |
359 | BdrvChild *c_fl1, *c_fl2; | |
360 | ||
361 | /* | |
362 | * bdrv_attach_child() eats child bs reference, so we need two @base | |
363 | * references for two filters: | |
364 | */ | |
365 | bdrv_ref(base); | |
366 | ||
afdaeb9e | 367 | bdrv_graph_wrlock(NULL); |
1dcea719 | 368 | bdrv_attach_child(top, ws, "file", &child_of_bds, BDRV_CHILD_DATA, |
e6af4f0e | 369 | &error_abort); |
1dcea719 VSO |
370 | c_fl1 = bdrv_attach_child(ws, fl1, "first", &child_of_bds, |
371 | BDRV_CHILD_DATA, &error_abort); | |
372 | c_fl2 = bdrv_attach_child(ws, fl2, "second", &child_of_bds, | |
373 | BDRV_CHILD_DATA, &error_abort); | |
1921b4f7 VSO |
374 | bdrv_attach_child(fl1, base, "backing", &child_of_bds, |
375 | BDRV_CHILD_FILTERED | BDRV_CHILD_PRIMARY, | |
e6af4f0e | 376 | &error_abort); |
1921b4f7 VSO |
377 | bdrv_attach_child(fl2, base, "backing", &child_of_bds, |
378 | BDRV_CHILD_FILTERED | BDRV_CHILD_PRIMARY, | |
e6af4f0e | 379 | &error_abort); |
afdaeb9e | 380 | bdrv_graph_wrunlock(); |
e6af4f0e VSO |
381 | |
382 | /* Select fl1 as first child to be active */ | |
1dcea719 | 383 | s->selected = c_fl1; |
3804e3cf KW |
384 | |
385 | bdrv_graph_rdlock_main_loop(); | |
386 | ||
e6af4f0e VSO |
387 | bdrv_child_refresh_perms(top, top->children.lh_first, &error_abort); |
388 | ||
389 | assert(c_fl1->perm & BLK_PERM_WRITE); | |
390 | assert(!(c_fl2->perm & BLK_PERM_WRITE)); | |
391 | ||
392 | /* Now, try to switch active child and update permissions */ | |
1dcea719 | 393 | s->selected = c_fl2; |
e6af4f0e VSO |
394 | bdrv_child_refresh_perms(top, top->children.lh_first, &error_abort); |
395 | ||
396 | assert(c_fl2->perm & BLK_PERM_WRITE); | |
397 | assert(!(c_fl1->perm & BLK_PERM_WRITE)); | |
398 | ||
399 | /* Switch once more, to not care about real child order in the list */ | |
1dcea719 | 400 | s->selected = c_fl1; |
e6af4f0e VSO |
401 | bdrv_child_refresh_perms(top, top->children.lh_first, &error_abort); |
402 | ||
403 | assert(c_fl1->perm & BLK_PERM_WRITE); | |
404 | assert(!(c_fl2->perm & BLK_PERM_WRITE)); | |
405 | ||
3804e3cf | 406 | bdrv_graph_rdunlock_main_loop(); |
e6af4f0e VSO |
407 | bdrv_unref(top); |
408 | } | |
409 | ||
397f7cc0 VSO |
410 | /* |
411 | * It's possible that filter required permissions allows to insert it to backing | |
412 | * chain, like: | |
413 | * | |
414 | * 1. [top] -> [filter] -> [base] | |
415 | * | |
416 | * but doesn't allow to add it as a branch: | |
417 | * | |
418 | * 2. [filter] --\ | |
419 | * v | |
420 | * [top] -> [base] | |
421 | * | |
422 | * So, inserting such filter should do all graph modifications and only then | |
423 | * update permissions. If we try to go through intermediate state [2] and update | |
424 | * permissions on it we'll fail. | |
425 | * | |
426 | * Let's check that bdrv_append() can append such a filter. | |
427 | */ | |
428 | static void test_append_greedy_filter(void) | |
429 | { | |
430 | BlockDriverState *top = exclusive_writer_node("top"); | |
431 | BlockDriverState *base = no_perm_node("base"); | |
432 | BlockDriverState *fl = exclusive_writer_node("fl1"); | |
433 | ||
afdaeb9e | 434 | bdrv_graph_wrlock(NULL); |
1921b4f7 VSO |
435 | bdrv_attach_child(top, base, "backing", &child_of_bds, |
436 | BDRV_CHILD_FILTERED | BDRV_CHILD_PRIMARY, | |
397f7cc0 | 437 | &error_abort); |
afdaeb9e | 438 | bdrv_graph_wrunlock(); |
397f7cc0 | 439 | |
487b9187 | 440 | aio_context_acquire(qemu_get_aio_context()); |
397f7cc0 | 441 | bdrv_append(fl, base, &error_abort); |
487b9187 | 442 | aio_context_release(qemu_get_aio_context()); |
ae9d4417 | 443 | bdrv_unref(fl); |
397f7cc0 VSO |
444 | bdrv_unref(top); |
445 | } | |
446 | ||
2dbfadf6 VSO |
447 | int main(int argc, char *argv[]) |
448 | { | |
449 | bdrv_init(); | |
450 | qemu_init_main_loop(&error_abort); | |
451 | ||
452 | g_test_init(&argc, &argv, NULL); | |
453 | ||
454 | g_test_add_func("/bdrv-graph-mod/update-perm-tree", test_update_perm_tree); | |
455 | g_test_add_func("/bdrv-graph-mod/should-update-child", | |
456 | test_should_update_child); | |
bd57f8f7 VSO |
457 | g_test_add_func("/bdrv-graph-mod/parallel-perm-update", |
458 | test_parallel_perm_update); | |
3bb0e298 VSO |
459 | g_test_add_func("/bdrv-graph-mod/parallel-exclusive-write", |
460 | test_parallel_exclusive_write); | |
2272edcf VSO |
461 | g_test_add_func("/bdrv-graph-mod/append-greedy-filter", |
462 | test_append_greedy_filter); | |
d71cc67d | 463 | |
2dbfadf6 VSO |
464 | return g_test_run(); |
465 | } |