4 * Copyright (c) 2018 Red Hat Inc.
6 * Author: Fam Zheng <famz@redhat.com>
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:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
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
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"
34 static BlockBackend
*open_image(const char *path
,
35 uint64_t perm
, uint64_t shared_perm
,
38 Error
*local_err
= NULL
;
40 QDict
*options
= qdict_new();
42 qdict_put_str(options
, "driver", "raw");
43 blk
= blk_new_open(path
, NULL
, options
, BDRV_O_RDWR
, &local_err
);
45 g_assert_null(local_err
);
46 if (blk_set_perm(blk
, perm
, shared_perm
, errp
)) {
51 error_propagate(errp
, local_err
);
56 static void check_locked_bytes(int fd
, uint64_t perm_locks
,
57 uint64_t shared_perm_locks
)
61 if (!perm_locks
&& !shared_perm_locks
) {
62 g_assert(!qemu_lock_fd_test(fd
, 0, 0, true));
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));
76 static void test_image_locking_basic(void)
78 BlockBackend
*blk1
, *blk2
, *blk3
;
79 char img_path
[] = "/tmp/qtest.XXXXXX";
80 uint64_t perm
, shared_perm
;
82 int fd
= mkstemp(img_path
);
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
);
90 check_locked_bytes(fd
, perm
, ~shared_perm
);
92 /* compatible perm between blk1 and blk2 */
93 blk2
= open_image(img_path
, perm
| BLK_PERM_RESIZE
, shared_perm
, NULL
);
95 check_locked_bytes(fd
, perm
| BLK_PERM_RESIZE
, ~shared_perm
);
97 /* incompatible perm with already open blk1 and blk2 */
98 blk3
= open_image(img_path
, perm
, BLK_PERM_WRITE_UNCHANGED
, NULL
);
103 /* Check that extra bytes in blk2 are correctly unlocked */
104 check_locked_bytes(fd
, perm
, ~shared_perm
);
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
);
117 static void test_set_perm_abort(void)
119 BlockBackend
*blk1
, *blk2
;
120 char img_path
[] = "/tmp/qtest.XXXXXX";
121 uint64_t perm
, shared_perm
;
123 int fd
= mkstemp(img_path
);
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
);
131 blk2
= open_image(img_path
, perm
, shared_perm
, &error_abort
);
134 check_locked_bytes(fd
, perm
, ~shared_perm
);
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
,
139 g_assert_cmpint(r
, !=, 0);
140 check_locked_bytes(fd
, perm
, ~shared_perm
);
145 int main(int argc
, char **argv
)
148 qemu_init_main_loop(&error_abort
);
150 g_test_init(&argc
, &argv
, NULL
);
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
);