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