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