blob: 8bd13b9949ea7766d6d529d9d7706621b949a0f7 [file] [log] [blame]
Stefan Hajnoczi6c6f3122015-11-05 18:13:20 -05001/*
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
Peter Maydell681c28a2016-02-08 18:08:51 +000013#include "qemu/osdep.h"
Markus Armbrusterda34e652016-03-14 09:01:28 +010014#include "qapi/error.h"
Stefan Hajnoczi6c6f3122015-11-05 18:13:20 -050015#include "qemu/main-loop.h"
John Snowc87621e2016-10-27 12:07:00 -040016#include "block/blockjob_int.h"
Kevin Wolfb75536c2016-04-18 17:30:17 +020017#include "sysemu/block-backend.h"
Andrey Shinkevichca1ef1e2019-07-29 15:46:00 +030018#include "qapi/qmp/qdict.h"
Stefan Hajnoczi6c6f3122015-11-05 18:13:20 -050019
20typedef struct {
21 BlockJob common;
22 unsigned int iterations;
23 bool use_timer;
24 int rc;
25 int *result;
26} TestBlockJob;
27
John Snowe4dad422018-09-06 09:02:19 -040028static void test_block_job_clean(Job *job)
Stefan Hajnoczi6c6f3122015-11-05 18:13:20 -050029{
Kevin Wolf1908a552018-04-17 16:41:17 +020030 BlockJob *bjob = container_of(job, BlockJob, job);
31 BlockDriverState *bs = blk_bs(bjob->blk);
Stefan Hajnoczi6c6f3122015-11-05 18:13:20 -050032
Stefan Hajnoczi6c6f3122015-11-05 18:13:20 -050033 bdrv_unref(bs);
34}
35
John Snowf67432a2018-08-29 21:57:26 -040036static int coroutine_fn test_block_job_run(Job *job, Error **errp)
Stefan Hajnoczi6c6f3122015-11-05 18:13:20 -050037{
John Snowf67432a2018-08-29 21:57:26 -040038 TestBlockJob *s = container_of(job, TestBlockJob, common.job);
Stefan Hajnoczi6c6f3122015-11-05 18:13:20 -050039
40 while (s->iterations--) {
41 if (s->use_timer) {
John Snowf67432a2018-08-29 21:57:26 -040042 job_sleep_ns(job, 0);
Stefan Hajnoczi6c6f3122015-11-05 18:13:20 -050043 } else {
John Snowf67432a2018-08-29 21:57:26 -040044 job_yield(job);
Stefan Hajnoczi6c6f3122015-11-05 18:13:20 -050045 }
46
John Snowf67432a2018-08-29 21:57:26 -040047 if (job_is_cancelled(job)) {
Stefan Hajnoczi6c6f3122015-11-05 18:13:20 -050048 break;
49 }
50 }
51
John Snowf67432a2018-08-29 21:57:26 -040052 return s->rc;
Stefan Hajnoczi6c6f3122015-11-05 18:13:20 -050053}
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;
Kevin Wolfdaa7f2f2018-04-17 12:56:07 +020063 if (!ret && job_is_cancelled(&data->job->common.job)) {
Stefan Hajnoczi6c6f3122015-11-05 18:13:20 -050064 ret = -ECANCELED;
65 }
66 *data->result = ret;
67 g_free(data);
68}
69
John Snow5ccac6f2016-11-08 01:50:37 -050070static const BlockJobDriver test_block_job_driver = {
Kevin Wolf33e9e9b2018-04-12 17:29:59 +020071 .job_driver = {
72 .instance_size = sizeof(TestBlockJob),
Kevin Wolf80fa2c72018-04-13 18:50:05 +020073 .free = block_job_free,
Kevin Wolfb15de822018-04-18 17:10:26 +020074 .user_resume = block_job_user_resume,
John Snowf67432a2018-08-29 21:57:26 -040075 .run = test_block_job_run,
John Snowe4dad422018-09-06 09:02:19 -040076 .clean = test_block_job_clean,
Kevin Wolf33e9e9b2018-04-12 17:29:59 +020077 },
John Snow5ccac6f2016-11-08 01:50:37 -050078};
79
Stefan Hajnoczi6c6f3122015-11-05 18:13:20 -050080/* Create a block job that completes with a given return code after a given
81 * number of event loop iterations. The return code is stored in the given
82 * result pointer.
83 *
84 * The event loop iterations can either be handled automatically with a 0 delay
85 * timer, or they can be stepped manually by entering the coroutine.
86 */
87static BlockJob *test_block_job_start(unsigned int iterations,
88 bool use_timer,
Kevin Wolf62c9e412018-04-19 16:09:52 +020089 int rc, int *result, JobTxn *txn)
Stefan Hajnoczi6c6f3122015-11-05 18:13:20 -050090{
91 BlockDriverState *bs;
92 TestBlockJob *s;
93 TestBlockJobCBData *data;
Alberto Garcia7f0317c2016-07-05 17:28:56 +030094 static unsigned counter;
95 char job_id[24];
Stefan Hajnoczi6c6f3122015-11-05 18:13:20 -050096
97 data = g_new0(TestBlockJobCBData, 1);
Kevin Wolfd185cf02017-01-16 17:17:38 +010098
Andrey Shinkevichca1ef1e2019-07-29 15:46:00 +030099 QDict *opt = qdict_new();
100 qdict_put_str(opt, "file.read-zeroes", "on");
101 bs = bdrv_open("null-co://", NULL, opt, 0, &error_abort);
Kevin Wolfd185cf02017-01-16 17:17:38 +0100102 g_assert_nonnull(bs);
103
Alberto Garcia7f0317c2016-07-05 17:28:56 +0300104 snprintf(job_id, sizeof(job_id), "job%u", counter++);
John Snow75859b92018-03-10 03:27:27 -0500105 s = block_job_create(job_id, &test_block_job_driver, txn, bs,
Kevin Wolfbb02b652018-04-19 17:54:56 +0200106 0, BLK_PERM_ALL, 0, JOB_DEFAULT,
Kevin Wolfc6cc12b2017-01-16 17:18:09 +0100107 test_block_job_cb, data, &error_abort);
Stefan Hajnoczi6c6f3122015-11-05 18:13:20 -0500108 s->iterations = iterations;
109 s->use_timer = use_timer;
110 s->rc = rc;
111 s->result = result;
Stefan Hajnoczi6c6f3122015-11-05 18:13:20 -0500112 data->job = s;
113 data->result = result;
Stefan Hajnoczi6c6f3122015-11-05 18:13:20 -0500114 return &s->common;
115}
116
117static void test_single_job(int expected)
118{
119 BlockJob *job;
Kevin Wolf62c9e412018-04-19 16:09:52 +0200120 JobTxn *txn;
Stefan Hajnoczi6c6f3122015-11-05 18:13:20 -0500121 int result = -EINPROGRESS;
122
Kevin Wolf7eaa8fb2018-04-23 16:06:26 +0200123 txn = job_txn_new();
John Snow75859b92018-03-10 03:27:27 -0500124 job = test_block_job_start(1, true, expected, &result, txn);
Kevin Wolfda01ff72018-04-13 17:31:02 +0200125 job_start(&job->job);
Stefan Hajnoczi6c6f3122015-11-05 18:13:20 -0500126
127 if (expected == -ECANCELED) {
Kevin Wolf3d70ff52018-04-24 16:13:52 +0200128 job_cancel(&job->job, false);
Stefan Hajnoczi6c6f3122015-11-05 18:13:20 -0500129 }
130
131 while (result == -EINPROGRESS) {
132 aio_poll(qemu_get_aio_context(), true);
133 }
134 g_assert_cmpint(result, ==, expected);
135
Kevin Wolf7eaa8fb2018-04-23 16:06:26 +0200136 job_txn_unref(txn);
Stefan Hajnoczi6c6f3122015-11-05 18:13:20 -0500137}
138
139static void test_single_job_success(void)
140{
141 test_single_job(0);
142}
143
144static void test_single_job_failure(void)
145{
146 test_single_job(-EIO);
147}
148
149static void test_single_job_cancel(void)
150{
151 test_single_job(-ECANCELED);
152}
153
154static void test_pair_jobs(int expected1, int expected2)
155{
156 BlockJob *job1;
157 BlockJob *job2;
Kevin Wolf62c9e412018-04-19 16:09:52 +0200158 JobTxn *txn;
Stefan Hajnoczi6c6f3122015-11-05 18:13:20 -0500159 int result1 = -EINPROGRESS;
160 int result2 = -EINPROGRESS;
161
Kevin Wolf7eaa8fb2018-04-23 16:06:26 +0200162 txn = job_txn_new();
John Snow75859b92018-03-10 03:27:27 -0500163 job1 = test_block_job_start(1, true, expected1, &result1, txn);
164 job2 = test_block_job_start(2, true, expected2, &result2, txn);
Kevin Wolfda01ff72018-04-13 17:31:02 +0200165 job_start(&job1->job);
166 job_start(&job2->job);
Stefan Hajnoczi6c6f3122015-11-05 18:13:20 -0500167
Paolo Bonzini7e74a732017-05-08 16:13:08 +0200168 /* Release our reference now to trigger as many nice
169 * use-after-free bugs as possible.
170 */
Kevin Wolf7eaa8fb2018-04-23 16:06:26 +0200171 job_txn_unref(txn);
Paolo Bonzini7e74a732017-05-08 16:13:08 +0200172
Stefan Hajnoczi6c6f3122015-11-05 18:13:20 -0500173 if (expected1 == -ECANCELED) {
Kevin Wolf3d70ff52018-04-24 16:13:52 +0200174 job_cancel(&job1->job, false);
Stefan Hajnoczi6c6f3122015-11-05 18:13:20 -0500175 }
176 if (expected2 == -ECANCELED) {
Kevin Wolf3d70ff52018-04-24 16:13:52 +0200177 job_cancel(&job2->job, false);
Stefan Hajnoczi6c6f3122015-11-05 18:13:20 -0500178 }
179
180 while (result1 == -EINPROGRESS || result2 == -EINPROGRESS) {
181 aio_poll(qemu_get_aio_context(), true);
182 }
183
184 /* Failure or cancellation of one job cancels the other job */
185 if (expected1 != 0) {
186 expected2 = -ECANCELED;
187 } else if (expected2 != 0) {
188 expected1 = -ECANCELED;
189 }
190
191 g_assert_cmpint(result1, ==, expected1);
192 g_assert_cmpint(result2, ==, expected2);
Stefan Hajnoczi6c6f3122015-11-05 18:13:20 -0500193}
194
195static void test_pair_jobs_success(void)
196{
197 test_pair_jobs(0, 0);
198}
199
200static void test_pair_jobs_failure(void)
201{
202 /* Test both orderings. The two jobs run for a different number of
203 * iterations so the code path is different depending on which job fails
204 * first.
205 */
206 test_pair_jobs(-EIO, 0);
207 test_pair_jobs(0, -EIO);
208}
209
210static void test_pair_jobs_cancel(void)
211{
212 test_pair_jobs(-ECANCELED, 0);
213 test_pair_jobs(0, -ECANCELED);
214}
215
216static void test_pair_jobs_fail_cancel_race(void)
217{
218 BlockJob *job1;
219 BlockJob *job2;
Kevin Wolf62c9e412018-04-19 16:09:52 +0200220 JobTxn *txn;
Stefan Hajnoczi6c6f3122015-11-05 18:13:20 -0500221 int result1 = -EINPROGRESS;
222 int result2 = -EINPROGRESS;
223
Kevin Wolf7eaa8fb2018-04-23 16:06:26 +0200224 txn = job_txn_new();
John Snow75859b92018-03-10 03:27:27 -0500225 job1 = test_block_job_start(1, true, -ECANCELED, &result1, txn);
226 job2 = test_block_job_start(2, false, 0, &result2, txn);
Kevin Wolfda01ff72018-04-13 17:31:02 +0200227 job_start(&job1->job);
228 job_start(&job2->job);
Stefan Hajnoczi6c6f3122015-11-05 18:13:20 -0500229
Kevin Wolf3d70ff52018-04-24 16:13:52 +0200230 job_cancel(&job1->job, false);
Stefan Hajnoczi6c6f3122015-11-05 18:13:20 -0500231
232 /* Now make job2 finish before the main loop kicks jobs. This simulates
233 * the race between a pending kick and another job completing.
234 */
Kevin Wolf3d70ff52018-04-24 16:13:52 +0200235 job_enter(&job2->job);
236 job_enter(&job2->job);
Stefan Hajnoczi6c6f3122015-11-05 18:13:20 -0500237
238 while (result1 == -EINPROGRESS || result2 == -EINPROGRESS) {
239 aio_poll(qemu_get_aio_context(), true);
240 }
241
242 g_assert_cmpint(result1, ==, -ECANCELED);
243 g_assert_cmpint(result2, ==, -ECANCELED);
244
Kevin Wolf7eaa8fb2018-04-23 16:06:26 +0200245 job_txn_unref(txn);
Stefan Hajnoczi6c6f3122015-11-05 18:13:20 -0500246}
247
248int main(int argc, char **argv)
249{
250 qemu_init_main_loop(&error_abort);
Kevin Wolfd185cf02017-01-16 17:17:38 +0100251 bdrv_init();
Stefan Hajnoczi6c6f3122015-11-05 18:13:20 -0500252
253 g_test_init(&argc, &argv, NULL);
254 g_test_add_func("/single/success", test_single_job_success);
255 g_test_add_func("/single/failure", test_single_job_failure);
256 g_test_add_func("/single/cancel", test_single_job_cancel);
257 g_test_add_func("/pair/success", test_pair_jobs_success);
258 g_test_add_func("/pair/failure", test_pair_jobs_failure);
259 g_test_add_func("/pair/cancel", test_pair_jobs_cancel);
260 g_test_add_func("/pair/fail-cancel-race", test_pair_jobs_fail_cancel_race);
261 return g_test_run();
262}