2
0

test-bdrv-graph-mod.c 6.9 KB

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