]> git.proxmox.com Git - mirror_qemu.git/blame - tests/test-blockjob-txn.c
tests/tcg: fix typo when calling clean-tcg
[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"
ca1ef1e6 18#include "qapi/qmp/qdict.h"
6c6f312d
SH
19
20typedef struct {
21 BlockJob common;
22 unsigned int iterations;
23 bool use_timer;
24 int rc;
25 int *result;
26} TestBlockJob;
27
e4dad427 28static void test_block_job_clean(Job *job)
6c6f312d 29{
1908a559
KW
30 BlockJob *bjob = container_of(job, BlockJob, job);
31 BlockDriverState *bs = blk_bs(bjob->blk);
6c6f312d 32
6c6f312d
SH
33 bdrv_unref(bs);
34}
35
f67432a2 36static int coroutine_fn test_block_job_run(Job *job, Error **errp)
6c6f312d 37{
f67432a2 38 TestBlockJob *s = container_of(job, TestBlockJob, common.job);
6c6f312d
SH
39
40 while (s->iterations--) {
41 if (s->use_timer) {
f67432a2 42 job_sleep_ns(job, 0);
6c6f312d 43 } else {
f67432a2 44 job_yield(job);
6c6f312d
SH
45 }
46
f67432a2 47 if (job_is_cancelled(job)) {
6c6f312d
SH
48 break;
49 }
50 }
51
f67432a2 52 return s->rc;
6c6f312d
SH
53}
54
55typedef struct {
56 TestBlockJob *job;
57 int *result;
58} TestBlockJobCBData;
59
60static void test_block_job_cb(void *opaque, int ret)
61{
62 TestBlockJobCBData *data = opaque;
daa7f2f9 63 if (!ret && job_is_cancelled(&data->job->common.job)) {
6c6f312d
SH
64 ret = -ECANCELED;
65 }
66 *data->result = ret;
67 g_free(data);
68}
69
5ccac6f1 70static const BlockJobDriver test_block_job_driver = {
33e9e9bd
KW
71 .job_driver = {
72 .instance_size = sizeof(TestBlockJob),
80fa2c75 73 .free = block_job_free,
b15de828 74 .user_resume = block_job_user_resume,
b69f777d 75 .drain = block_job_drain,
f67432a2 76 .run = test_block_job_run,
e4dad427 77 .clean = test_block_job_clean,
33e9e9bd 78 },
5ccac6f1
JS
79};
80
6c6f312d
SH
81/* Create a block job that completes with a given return code after a given
82 * number of event loop iterations. The return code is stored in the given
83 * result pointer.
84 *
85 * The event loop iterations can either be handled automatically with a 0 delay
86 * timer, or they can be stepped manually by entering the coroutine.
87 */
88static BlockJob *test_block_job_start(unsigned int iterations,
89 bool use_timer,
62c9e416 90 int rc, int *result, JobTxn *txn)
6c6f312d
SH
91{
92 BlockDriverState *bs;
93 TestBlockJob *s;
94 TestBlockJobCBData *data;
7f0317cf
AG
95 static unsigned counter;
96 char job_id[24];
6c6f312d
SH
97
98 data = g_new0(TestBlockJobCBData, 1);
d185cf0e 99
ca1ef1e6
AS
100 QDict *opt = qdict_new();
101 qdict_put_str(opt, "file.read-zeroes", "on");
102 bs = bdrv_open("null-co://", NULL, opt, 0, &error_abort);
d185cf0e
KW
103 g_assert_nonnull(bs);
104
7f0317cf 105 snprintf(job_id, sizeof(job_id), "job%u", counter++);
75859b94 106 s = block_job_create(job_id, &test_block_job_driver, txn, bs,
bb02b65c 107 0, BLK_PERM_ALL, 0, JOB_DEFAULT,
c6cc12bf 108 test_block_job_cb, data, &error_abort);
6c6f312d
SH
109 s->iterations = iterations;
110 s->use_timer = use_timer;
111 s->rc = rc;
112 s->result = result;
6c6f312d
SH
113 data->job = s;
114 data->result = result;
6c6f312d
SH
115 return &s->common;
116}
117
118static void test_single_job(int expected)
119{
120 BlockJob *job;
62c9e416 121 JobTxn *txn;
6c6f312d
SH
122 int result = -EINPROGRESS;
123
7eaa8fb5 124 txn = job_txn_new();
75859b94 125 job = test_block_job_start(1, true, expected, &result, txn);
da01ff7f 126 job_start(&job->job);
6c6f312d
SH
127
128 if (expected == -ECANCELED) {
3d70ff53 129 job_cancel(&job->job, false);
6c6f312d
SH
130 }
131
132 while (result == -EINPROGRESS) {
133 aio_poll(qemu_get_aio_context(), true);
134 }
135 g_assert_cmpint(result, ==, expected);
136
7eaa8fb5 137 job_txn_unref(txn);
6c6f312d
SH
138}
139
140static void test_single_job_success(void)
141{
142 test_single_job(0);
143}
144
145static void test_single_job_failure(void)
146{
147 test_single_job(-EIO);
148}
149
150static void test_single_job_cancel(void)
151{
152 test_single_job(-ECANCELED);
153}
154
155static void test_pair_jobs(int expected1, int expected2)
156{
157 BlockJob *job1;
158 BlockJob *job2;
62c9e416 159 JobTxn *txn;
6c6f312d
SH
160 int result1 = -EINPROGRESS;
161 int result2 = -EINPROGRESS;
162
7eaa8fb5 163 txn = job_txn_new();
75859b94
JS
164 job1 = test_block_job_start(1, true, expected1, &result1, txn);
165 job2 = test_block_job_start(2, true, expected2, &result2, txn);
da01ff7f
KW
166 job_start(&job1->job);
167 job_start(&job2->job);
6c6f312d 168
7e74a734
PB
169 /* Release our reference now to trigger as many nice
170 * use-after-free bugs as possible.
171 */
7eaa8fb5 172 job_txn_unref(txn);
7e74a734 173
6c6f312d 174 if (expected1 == -ECANCELED) {
3d70ff53 175 job_cancel(&job1->job, false);
6c6f312d
SH
176 }
177 if (expected2 == -ECANCELED) {
3d70ff53 178 job_cancel(&job2->job, false);
6c6f312d
SH
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);
6c6f312d
SH
194}
195
196static void test_pair_jobs_success(void)
197{
198 test_pair_jobs(0, 0);
199}
200
201static 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
211static void test_pair_jobs_cancel(void)
212{
213 test_pair_jobs(-ECANCELED, 0);
214 test_pair_jobs(0, -ECANCELED);
215}
216
217static void test_pair_jobs_fail_cancel_race(void)
218{
219 BlockJob *job1;
220 BlockJob *job2;
62c9e416 221 JobTxn *txn;
6c6f312d
SH
222 int result1 = -EINPROGRESS;
223 int result2 = -EINPROGRESS;
224
7eaa8fb5 225 txn = job_txn_new();
75859b94
JS
226 job1 = test_block_job_start(1, true, -ECANCELED, &result1, txn);
227 job2 = test_block_job_start(2, false, 0, &result2, txn);
da01ff7f
KW
228 job_start(&job1->job);
229 job_start(&job2->job);
6c6f312d 230
3d70ff53 231 job_cancel(&job1->job, false);
6c6f312d
SH
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 */
3d70ff53
KW
236 job_enter(&job2->job);
237 job_enter(&job2->job);
6c6f312d
SH
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
7eaa8fb5 246 job_txn_unref(txn);
6c6f312d
SH
247}
248
249int main(int argc, char **argv)
250{
251 qemu_init_main_loop(&error_abort);
d185cf0e 252 bdrv_init();
6c6f312d
SH
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}