1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
34static 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
56static 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
76static void test_image_locking_basic(void)
77{
78 BlockBackend *blk1, *blk2, *blk3;
79 char img_path[] = "/tmp/qtest.XXXXXX";
80 uint64_t perm, shared_perm;
81
82 int fd = mkstemp(img_path);
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
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
98 blk3 = open_image(img_path, perm, BLK_PERM_WRITE_UNCHANGED, NULL);
99 g_assert_null(blk3);
100
101 blk_unref(blk2);
102
103
104 check_locked_bytes(fd, perm, ~shared_perm);
105
106 blk_unref(blk1);
107
108
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
117static void test_set_perm_abort(void)
118{
119 BlockBackend *blk1, *blk2;
120 char img_path[] = "/tmp/qtest.XXXXXX";
121 uint64_t perm, shared_perm;
122 int r;
123 int fd = mkstemp(img_path);
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
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}
144
145int main(int argc, char **argv)
146{
147 bdrv_init();
148 qemu_init_main_loop(&error_abort);
149
150 g_test_init(&argc, &argv, NULL);
151
152 if (qemu_has_ofd_lock()) {
153 g_test_add_func("/image-locking/basic", test_image_locking_basic);
154 g_test_add_func("/image-locking/set-perm-abort", test_set_perm_abort);
155 }
156
157 return g_test_run();
158}
159