]> git.proxmox.com Git - mirror_qemu.git/blob - tests/test-blockjob-txn.c
job: Create Job, JobDriver and job_create()
[mirror_qemu.git] / tests / test-blockjob-txn.c
1 /*
2 * Blockjob transactions tests
3 *
4 * Copyright Red Hat, Inc. 2015
5 *
6 * Authors:
7 * Stefan Hajnoczi <stefanha@redhat.com>
8 *
9 * This work is licensed under the terms of the GNU LGPL, version 2 or later.
10 * See the COPYING.LIB file in the top-level directory.
11 */
12
13 #include "qemu/osdep.h"
14 #include "qapi/error.h"
15 #include "qemu/main-loop.h"
16 #include "block/blockjob_int.h"
17 #include "sysemu/block-backend.h"
18
19 typedef struct {
20 BlockJob common;
21 unsigned int iterations;
22 bool use_timer;
23 int rc;
24 int *result;
25 } TestBlockJob;
26
27 static void test_block_job_complete(BlockJob *job, void *opaque)
28 {
29 BlockDriverState *bs = blk_bs(job->blk);
30 int rc = (intptr_t)opaque;
31
32 if (block_job_is_cancelled(job)) {
33 rc = -ECANCELED;
34 }
35
36 block_job_completed(job, rc);
37 bdrv_unref(bs);
38 }
39
40 static void coroutine_fn test_block_job_run(void *opaque)
41 {
42 TestBlockJob *s = opaque;
43 BlockJob *job = &s->common;
44
45 while (s->iterations--) {
46 if (s->use_timer) {
47 block_job_sleep_ns(job, 0);
48 } else {
49 block_job_yield(job);
50 }
51
52 if (block_job_is_cancelled(job)) {
53 break;
54 }
55 }
56
57 block_job_defer_to_main_loop(job, test_block_job_complete,
58 (void *)(intptr_t)s->rc);
59 }
60
61 typedef struct {
62 TestBlockJob *job;
63 int *result;
64 } TestBlockJobCBData;
65
66 static void test_block_job_cb(void *opaque, int ret)
67 {
68 TestBlockJobCBData *data = opaque;
69 if (!ret && block_job_is_cancelled(&data->job->common)) {
70 ret = -ECANCELED;
71 }
72 *data->result = ret;
73 g_free(data);
74 }
75
76 static const BlockJobDriver test_block_job_driver = {
77 .job_driver = {
78 .instance_size = sizeof(TestBlockJob),
79 },
80 .start = test_block_job_run,
81 };
82
83 /* Create a block job that completes with a given return code after a given
84 * number of event loop iterations. The return code is stored in the given
85 * result pointer.
86 *
87 * The event loop iterations can either be handled automatically with a 0 delay
88 * timer, or they can be stepped manually by entering the coroutine.
89 */
90 static BlockJob *test_block_job_start(unsigned int iterations,
91 bool use_timer,
92 int rc, int *result, BlockJobTxn *txn)
93 {
94 BlockDriverState *bs;
95 TestBlockJob *s;
96 TestBlockJobCBData *data;
97 static unsigned counter;
98 char job_id[24];
99
100 data = g_new0(TestBlockJobCBData, 1);
101
102 bs = bdrv_open("null-co://", NULL, NULL, 0, &error_abort);
103 g_assert_nonnull(bs);
104
105 snprintf(job_id, sizeof(job_id), "job%u", counter++);
106 s = block_job_create(job_id, &test_block_job_driver, txn, bs,
107 0, BLK_PERM_ALL, 0, BLOCK_JOB_DEFAULT,
108 test_block_job_cb, data, &error_abort);
109 s->iterations = iterations;
110 s->use_timer = use_timer;
111 s->rc = rc;
112 s->result = result;
113 data->job = s;
114 data->result = result;
115 return &s->common;
116 }
117
118 static void test_single_job(int expected)
119 {
120 BlockJob *job;
121 BlockJobTxn *txn;
122 int result = -EINPROGRESS;
123
124 txn = block_job_txn_new();
125 job = test_block_job_start(1, true, expected, &result, txn);
126 block_job_start(job);
127
128 if (expected == -ECANCELED) {
129 block_job_cancel(job, false);
130 }
131
132 while (result == -EINPROGRESS) {
133 aio_poll(qemu_get_aio_context(), true);
134 }
135 g_assert_cmpint(result, ==, expected);
136
137 block_job_txn_unref(txn);
138 }
139
140 static void test_single_job_success(void)
141 {
142 test_single_job(0);
143 }
144
145 static void test_single_job_failure(void)
146 {
147 test_single_job(-EIO);
148 }
149
150 static void test_single_job_cancel(void)
151 {
152 test_single_job(-ECANCELED);
153 }
154
155 static void test_pair_jobs(int expected1, int expected2)
156 {
157 BlockJob *job1;
158 BlockJob *job2;
159 BlockJobTxn *txn;
160 int result1 = -EINPROGRESS;
161 int result2 = -EINPROGRESS;
162
163 txn = block_job_txn_new();
164 job1 = test_block_job_start(1, true, expected1, &result1, txn);
165 job2 = test_block_job_start(2, true, expected2, &result2, txn);
166 block_job_start(job1);
167 block_job_start(job2);
168
169 /* Release our reference now to trigger as many nice
170 * use-after-free bugs as possible.
171 */
172 block_job_txn_unref(txn);
173
174 if (expected1 == -ECANCELED) {
175 block_job_cancel(job1, false);
176 }
177 if (expected2 == -ECANCELED) {
178 block_job_cancel(job2, false);
179 }
180
181 while (result1 == -EINPROGRESS || result2 == -EINPROGRESS) {
182 aio_poll(qemu_get_aio_context(), true);
183 }
184
185 /* Failure or cancellation of one job cancels the other job */
186 if (expected1 != 0) {
187 expected2 = -ECANCELED;
188 } else if (expected2 != 0) {
189 expected1 = -ECANCELED;
190 }
191
192 g_assert_cmpint(result1, ==, expected1);
193 g_assert_cmpint(result2, ==, expected2);
194 }
195
196 static void test_pair_jobs_success(void)
197 {
198 test_pair_jobs(0, 0);
199 }
200
201 static void test_pair_jobs_failure(void)
202 {
203 /* Test both orderings. The two jobs run for a different number of
204 * iterations so the code path is different depending on which job fails
205 * first.
206 */
207 test_pair_jobs(-EIO, 0);
208 test_pair_jobs(0, -EIO);
209 }
210
211 static void test_pair_jobs_cancel(void)
212 {
213 test_pair_jobs(-ECANCELED, 0);
214 test_pair_jobs(0, -ECANCELED);
215 }
216
217 static void test_pair_jobs_fail_cancel_race(void)
218 {
219 BlockJob *job1;
220 BlockJob *job2;
221 BlockJobTxn *txn;
222 int result1 = -EINPROGRESS;
223 int result2 = -EINPROGRESS;
224
225 txn = block_job_txn_new();
226 job1 = test_block_job_start(1, true, -ECANCELED, &result1, txn);
227 job2 = test_block_job_start(2, false, 0, &result2, txn);
228 block_job_start(job1);
229 block_job_start(job2);
230
231 block_job_cancel(job1, false);
232
233 /* Now make job2 finish before the main loop kicks jobs. This simulates
234 * the race between a pending kick and another job completing.
235 */
236 block_job_enter(job2);
237 block_job_enter(job2);
238
239 while (result1 == -EINPROGRESS || result2 == -EINPROGRESS) {
240 aio_poll(qemu_get_aio_context(), true);
241 }
242
243 g_assert_cmpint(result1, ==, -ECANCELED);
244 g_assert_cmpint(result2, ==, -ECANCELED);
245
246 block_job_txn_unref(txn);
247 }
248
249 int main(int argc, char **argv)
250 {
251 qemu_init_main_loop(&error_abort);
252 bdrv_init();
253
254 g_test_init(&argc, &argv, NULL);
255 g_test_add_func("/single/success", test_single_job_success);
256 g_test_add_func("/single/failure", test_single_job_failure);
257 g_test_add_func("/single/cancel", test_single_job_cancel);
258 g_test_add_func("/pair/success", test_pair_jobs_success);
259 g_test_add_func("/pair/failure", test_pair_jobs_failure);
260 g_test_add_func("/pair/cancel", test_pair_jobs_cancel);
261 g_test_add_func("/pair/fail-cancel-race", test_pair_jobs_fail_cancel_race);
262 return g_test_run();
263 }