test-image-locking.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * Image locking tests
  3. *
  4. * Copyright (c) 2018 Red Hat Inc.
  5. *
  6. * Author: Fam Zheng <famz@redhat.com>
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  21. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. */
  26. #include "qemu/osdep.h"
  27. #include "block/block.h"
  28. #include "sysemu/block-backend.h"
  29. #include "qapi/error.h"
  30. #include "qapi/qmp/qdict.h"
  31. #include "qemu/main-loop.h"
  32. static BlockBackend *open_image(const char *path,
  33. uint64_t perm, uint64_t shared_perm,
  34. Error **errp)
  35. {
  36. Error *local_err = NULL;
  37. BlockBackend *blk;
  38. QDict *options = qdict_new();
  39. qdict_put_str(options, "driver", "raw");
  40. blk = blk_new_open(path, NULL, options, BDRV_O_RDWR, &local_err);
  41. if (blk) {
  42. g_assert_null(local_err);
  43. if (blk_set_perm(blk, perm, shared_perm, errp)) {
  44. blk_unref(blk);
  45. blk = NULL;
  46. }
  47. } else {
  48. error_propagate(errp, local_err);
  49. }
  50. return blk;
  51. }
  52. static void check_locked_bytes(int fd, uint64_t perm_locks,
  53. uint64_t shared_perm_locks)
  54. {
  55. int i;
  56. if (!perm_locks && !shared_perm_locks) {
  57. g_assert(!qemu_lock_fd_test(fd, 0, 0, true));
  58. return;
  59. }
  60. for (i = 0; (1ULL << i) <= BLK_PERM_ALL; i++) {
  61. uint64_t bit = (1ULL << i);
  62. bool perm_expected = !!(bit & perm_locks);
  63. bool shared_perm_expected = !!(bit & shared_perm_locks);
  64. g_assert_cmpint(perm_expected, ==,
  65. !!qemu_lock_fd_test(fd, 100 + i, 1, true));
  66. g_assert_cmpint(shared_perm_expected, ==,
  67. !!qemu_lock_fd_test(fd, 200 + i, 1, true));
  68. }
  69. }
  70. static void test_image_locking_basic(void)
  71. {
  72. BlockBackend *blk1, *blk2, *blk3;
  73. char img_path[] = "/tmp/qtest.XXXXXX";
  74. uint64_t perm, shared_perm;
  75. int fd = mkstemp(img_path);
  76. assert(fd >= 0);
  77. perm = BLK_PERM_WRITE | BLK_PERM_CONSISTENT_READ;
  78. shared_perm = BLK_PERM_ALL;
  79. blk1 = open_image(img_path, perm, shared_perm, &error_abort);
  80. g_assert(blk1);
  81. check_locked_bytes(fd, perm, ~shared_perm);
  82. /* compatible perm between blk1 and blk2 */
  83. blk2 = open_image(img_path, perm | BLK_PERM_RESIZE, shared_perm, NULL);
  84. g_assert(blk2);
  85. check_locked_bytes(fd, perm | BLK_PERM_RESIZE, ~shared_perm);
  86. /* incompatible perm with already open blk1 and blk2 */
  87. blk3 = open_image(img_path, perm, BLK_PERM_WRITE_UNCHANGED, NULL);
  88. g_assert_null(blk3);
  89. blk_unref(blk2);
  90. /* Check that extra bytes in blk2 are correctly unlocked */
  91. check_locked_bytes(fd, perm, ~shared_perm);
  92. blk_unref(blk1);
  93. /* Image is unused, no lock there */
  94. check_locked_bytes(fd, 0, 0);
  95. blk3 = open_image(img_path, perm, BLK_PERM_WRITE_UNCHANGED, &error_abort);
  96. g_assert(blk3);
  97. blk_unref(blk3);
  98. close(fd);
  99. unlink(img_path);
  100. }
  101. static void test_set_perm_abort(void)
  102. {
  103. BlockBackend *blk1, *blk2;
  104. char img_path[] = "/tmp/qtest.XXXXXX";
  105. uint64_t perm, shared_perm;
  106. int r;
  107. int fd = mkstemp(img_path);
  108. assert(fd >= 0);
  109. perm = BLK_PERM_WRITE | BLK_PERM_CONSISTENT_READ;
  110. shared_perm = BLK_PERM_ALL;
  111. blk1 = open_image(img_path, perm, shared_perm, &error_abort);
  112. g_assert(blk1);
  113. blk2 = open_image(img_path, perm, shared_perm, &error_abort);
  114. g_assert(blk2);
  115. check_locked_bytes(fd, perm, ~shared_perm);
  116. /* A failed blk_set_perm mustn't change perm status (locked bytes) */
  117. r = blk_set_perm(blk2, perm | BLK_PERM_RESIZE, BLK_PERM_WRITE_UNCHANGED,
  118. NULL);
  119. g_assert_cmpint(r, !=, 0);
  120. check_locked_bytes(fd, perm, ~shared_perm);
  121. blk_unref(blk1);
  122. blk_unref(blk2);
  123. }
  124. int main(int argc, char **argv)
  125. {
  126. bdrv_init();
  127. qemu_init_main_loop(&error_abort);
  128. g_test_init(&argc, &argv, NULL);
  129. if (qemu_has_ofd_lock()) {
  130. g_test_add_func("/image-locking/basic", test_image_locking_basic);
  131. g_test_add_func("/image-locking/set-perm-abort", test_set_perm_abort);
  132. }
  133. return g_test_run();
  134. }