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