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 27 #include "qemu/osdep.h" 28 #include "block/block.h" 29 #include "sysemu/block-backend.h" 30 #include "qapi/error.h" 31 #include "qapi/qmp/qdict.h" 32 33 static BlockBackend *open_image(const char *path, 34 uint64_t perm, uint64_t shared_perm, 35 Error **errp) 36 { 37 Error *local_err = NULL; 38 BlockBackend *blk; 39 QDict *options = qdict_new(); 40 41 qdict_put_str(options, "driver", "raw"); 42 blk = blk_new_open(path, NULL, options, BDRV_O_RDWR, &local_err); 43 if (blk) { 44 g_assert_null(local_err); 45 if (blk_set_perm(blk, perm, shared_perm, errp)) { 46 blk_unref(blk); 47 blk = NULL; 48 } 49 } else { 50 error_propagate(errp, local_err); 51 } 52 return blk; 53 } 54 55 static void check_locked_bytes(int fd, uint64_t perm_locks, 56 uint64_t shared_perm_locks) 57 { 58 int i; 59 60 if (!perm_locks && !shared_perm_locks) { 61 g_assert(!qemu_lock_fd_test(fd, 0, 0, true)); 62 return; 63 } 64 for (i = 0; (1ULL << i) <= BLK_PERM_ALL; i++) { 65 uint64_t bit = (1ULL << i); 66 bool perm_expected = !!(bit & perm_locks); 67 bool shared_perm_expected = !!(bit & shared_perm_locks); 68 g_assert_cmpint(perm_expected, ==, 69 !!qemu_lock_fd_test(fd, 100 + i, 1, true)); 70 g_assert_cmpint(shared_perm_expected, ==, 71 !!qemu_lock_fd_test(fd, 200 + i, 1, true)); 72 } 73 } 74 75 static void test_image_locking_basic(void) 76 { 77 BlockBackend *blk1, *blk2, *blk3; 78 char img_path[] = "/tmp/qtest.XXXXXX"; 79 uint64_t perm, shared_perm; 80 81 int fd = mkstemp(img_path); 82 assert(fd >= 0); 83 84 perm = BLK_PERM_WRITE | BLK_PERM_CONSISTENT_READ; 85 shared_perm = BLK_PERM_ALL; 86 blk1 = open_image(img_path, perm, shared_perm, &error_abort); 87 g_assert(blk1); 88 89 check_locked_bytes(fd, perm, ~shared_perm); 90 91 /* compatible perm between blk1 and blk2 */ 92 blk2 = open_image(img_path, perm | BLK_PERM_RESIZE, shared_perm, NULL); 93 g_assert(blk2); 94 check_locked_bytes(fd, perm | BLK_PERM_RESIZE, ~shared_perm); 95 96 /* incompatible perm with already open blk1 and blk2 */ 97 blk3 = open_image(img_path, perm, BLK_PERM_WRITE_UNCHANGED, NULL); 98 g_assert_null(blk3); 99 100 blk_unref(blk2); 101 102 /* Check that extra bytes in blk2 are correctly unlocked */ 103 check_locked_bytes(fd, perm, ~shared_perm); 104 105 blk_unref(blk1); 106 107 /* Image is unused, no lock there */ 108 check_locked_bytes(fd, 0, 0); 109 blk3 = open_image(img_path, perm, BLK_PERM_WRITE_UNCHANGED, &error_abort); 110 g_assert(blk3); 111 blk_unref(blk3); 112 close(fd); 113 unlink(img_path); 114 } 115 116 static void test_set_perm_abort(void) 117 { 118 BlockBackend *blk1, *blk2; 119 char img_path[] = "/tmp/qtest.XXXXXX"; 120 uint64_t perm, shared_perm; 121 int r; 122 int fd = mkstemp(img_path); 123 assert(fd >= 0); 124 125 perm = BLK_PERM_WRITE | BLK_PERM_CONSISTENT_READ; 126 shared_perm = BLK_PERM_ALL; 127 blk1 = open_image(img_path, perm, shared_perm, &error_abort); 128 g_assert(blk1); 129 130 blk2 = open_image(img_path, perm, shared_perm, &error_abort); 131 g_assert(blk2); 132 133 check_locked_bytes(fd, perm, ~shared_perm); 134 135 /* A failed blk_set_perm mustn't change perm status (locked bytes) */ 136 r = blk_set_perm(blk2, perm | BLK_PERM_RESIZE, BLK_PERM_WRITE_UNCHANGED, 137 NULL); 138 g_assert_cmpint(r, !=, 0); 139 check_locked_bytes(fd, perm, ~shared_perm); 140 blk_unref(blk1); 141 blk_unref(blk2); 142 } 143 144 int main(int argc, char **argv) 145 { 146 bdrv_init(); 147 qemu_init_main_loop(&error_abort); 148 149 g_test_init(&argc, &argv, NULL); 150 151 if (qemu_has_ofd_lock()) { 152 g_test_add_func("/image-locking/basic", test_image_locking_basic); 153 g_test_add_func("/image-locking/set-perm-abort", test_set_perm_abort); 154 } 155 156 return g_test_run(); 157 } 158