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