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