]>
Commit | Line | Data |
---|---|---|
881cfd17 KW |
1 | /* |
2 | * Block node draining tests | |
3 | * | |
4 | * Copyright (c) 2017 Kevin Wolf <kwolf@redhat.com> | |
5 | * | |
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
7 | * of this software and associated documentation files (the "Software"), to deal | |
8 | * in the Software without restriction, including without limitation the rights | |
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
10 | * copies of the Software, and to permit persons to whom the Software is | |
11 | * furnished to do so, subject to the following conditions: | |
12 | * | |
13 | * The above copyright notice and this permission notice shall be included in | |
14 | * all copies or substantial portions of the Software. | |
15 | * | |
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
22 | * THE SOFTWARE. | |
23 | */ | |
24 | ||
25 | #include "qemu/osdep.h" | |
e2c1c34f | 26 | #include "block/block_int.h" |
7253220d | 27 | #include "block/blockjob_int.h" |
881cfd17 KW |
28 | #include "sysemu/block-backend.h" |
29 | #include "qapi/error.h" | |
db725815 | 30 | #include "qemu/main-loop.h" |
bb675689 KW |
31 | #include "iothread.h" |
32 | ||
33 | static QemuEvent done_event; | |
881cfd17 KW |
34 | |
35 | typedef struct BDRVTestState { | |
36 | int drain_count; | |
bb675689 | 37 | AioContext *bh_indirection_ctx; |
57320ca9 | 38 | bool sleep_in_drain_begin; |
881cfd17 KW |
39 | } BDRVTestState; |
40 | ||
7bce1c29 KW |
41 | static void coroutine_fn sleep_in_drain_begin(void *opaque) |
42 | { | |
43 | BlockDriverState *bs = opaque; | |
44 | ||
45 | qemu_co_sleep_ns(QEMU_CLOCK_REALTIME, 100000); | |
46 | bdrv_dec_in_flight(bs); | |
47 | } | |
48 | ||
5e8ac217 | 49 | static void bdrv_test_drain_begin(BlockDriverState *bs) |
881cfd17 KW |
50 | { |
51 | BDRVTestState *s = bs->opaque; | |
52 | s->drain_count++; | |
57320ca9 | 53 | if (s->sleep_in_drain_begin) { |
7bce1c29 KW |
54 | Coroutine *co = qemu_coroutine_create(sleep_in_drain_begin, bs); |
55 | bdrv_inc_in_flight(bs); | |
56 | aio_co_enter(bdrv_get_aio_context(bs), co); | |
57320ca9 | 57 | } |
881cfd17 KW |
58 | } |
59 | ||
5e8ac217 | 60 | static void bdrv_test_drain_end(BlockDriverState *bs) |
881cfd17 KW |
61 | { |
62 | BDRVTestState *s = bs->opaque; | |
63 | s->drain_count--; | |
64 | } | |
65 | ||
66 | static void bdrv_test_close(BlockDriverState *bs) | |
67 | { | |
68 | BDRVTestState *s = bs->opaque; | |
69 | g_assert_cmpint(s->drain_count, >, 0); | |
70 | } | |
71 | ||
bb675689 KW |
72 | static void co_reenter_bh(void *opaque) |
73 | { | |
74 | aio_co_wake(opaque); | |
75 | } | |
76 | ||
881cfd17 | 77 | static int coroutine_fn bdrv_test_co_preadv(BlockDriverState *bs, |
f7ef38dd VSO |
78 | int64_t offset, int64_t bytes, |
79 | QEMUIOVector *qiov, | |
80 | BdrvRequestFlags flags) | |
881cfd17 | 81 | { |
bb675689 KW |
82 | BDRVTestState *s = bs->opaque; |
83 | ||
881cfd17 KW |
84 | /* We want this request to stay until the polling loop in drain waits for |
85 | * it to complete. We need to sleep a while as bdrv_drain_invoke() comes | |
86 | * first and polls its result, too, but it shouldn't accidentally complete | |
87 | * this request yet. */ | |
88 | qemu_co_sleep_ns(QEMU_CLOCK_REALTIME, 100000); | |
89 | ||
bb675689 KW |
90 | if (s->bh_indirection_ctx) { |
91 | aio_bh_schedule_oneshot(s->bh_indirection_ctx, co_reenter_bh, | |
92 | qemu_coroutine_self()); | |
93 | qemu_coroutine_yield(); | |
94 | } | |
95 | ||
881cfd17 KW |
96 | return 0; |
97 | } | |
98 | ||
9746b35c HR |
99 | static int bdrv_test_change_backing_file(BlockDriverState *bs, |
100 | const char *backing_file, | |
101 | const char *backing_fmt) | |
102 | { | |
103 | return 0; | |
104 | } | |
105 | ||
881cfd17 KW |
106 | static BlockDriver bdrv_test = { |
107 | .format_name = "test", | |
108 | .instance_size = sizeof(BDRVTestState), | |
25f78d9e | 109 | .supports_backing = true, |
881cfd17 KW |
110 | |
111 | .bdrv_close = bdrv_test_close, | |
112 | .bdrv_co_preadv = bdrv_test_co_preadv, | |
113 | ||
5e8ac217 KW |
114 | .bdrv_drain_begin = bdrv_test_drain_begin, |
115 | .bdrv_drain_end = bdrv_test_drain_end, | |
86e1c840 | 116 | |
e5d8a406 | 117 | .bdrv_child_perm = bdrv_default_perms, |
9746b35c HR |
118 | |
119 | .bdrv_change_backing_file = bdrv_test_change_backing_file, | |
881cfd17 KW |
120 | }; |
121 | ||
122 | static void aio_ret_cb(void *opaque, int ret) | |
123 | { | |
124 | int *aio_ret = opaque; | |
125 | *aio_ret = ret; | |
126 | } | |
127 | ||
0582eb10 KW |
128 | typedef struct CallInCoroutineData { |
129 | void (*entry)(void); | |
130 | bool done; | |
131 | } CallInCoroutineData; | |
132 | ||
133 | static coroutine_fn void call_in_coroutine_entry(void *opaque) | |
134 | { | |
135 | CallInCoroutineData *data = opaque; | |
136 | ||
137 | data->entry(); | |
138 | data->done = true; | |
139 | } | |
140 | ||
141 | static void call_in_coroutine(void (*entry)(void)) | |
142 | { | |
143 | Coroutine *co; | |
144 | CallInCoroutineData data = { | |
145 | .entry = entry, | |
146 | .done = false, | |
147 | }; | |
148 | ||
149 | co = qemu_coroutine_create(call_in_coroutine_entry, &data); | |
150 | qemu_coroutine_enter(co); | |
151 | while (!data.done) { | |
152 | aio_poll(qemu_get_aio_context(), true); | |
153 | } | |
154 | } | |
155 | ||
86e1c840 KW |
156 | enum drain_type { |
157 | BDRV_DRAIN_ALL, | |
158 | BDRV_DRAIN, | |
6c429a6a | 159 | DRAIN_TYPE_MAX, |
86e1c840 KW |
160 | }; |
161 | ||
162 | static void do_drain_begin(enum drain_type drain_type, BlockDriverState *bs) | |
163 | { | |
164 | switch (drain_type) { | |
165 | case BDRV_DRAIN_ALL: bdrv_drain_all_begin(); break; | |
166 | case BDRV_DRAIN: bdrv_drained_begin(bs); break; | |
167 | default: g_assert_not_reached(); | |
168 | } | |
169 | } | |
170 | ||
171 | static void do_drain_end(enum drain_type drain_type, BlockDriverState *bs) | |
172 | { | |
173 | switch (drain_type) { | |
174 | case BDRV_DRAIN_ALL: bdrv_drain_all_end(); break; | |
175 | case BDRV_DRAIN: bdrv_drained_end(bs); break; | |
176 | default: g_assert_not_reached(); | |
177 | } | |
178 | } | |
179 | ||
f62c1729 KW |
180 | static void do_drain_begin_unlocked(enum drain_type drain_type, BlockDriverState *bs) |
181 | { | |
182 | if (drain_type != BDRV_DRAIN_ALL) { | |
183 | aio_context_acquire(bdrv_get_aio_context(bs)); | |
184 | } | |
185 | do_drain_begin(drain_type, bs); | |
186 | if (drain_type != BDRV_DRAIN_ALL) { | |
187 | aio_context_release(bdrv_get_aio_context(bs)); | |
188 | } | |
189 | } | |
190 | ||
57f3d07b KW |
191 | static BlockBackend * no_coroutine_fn test_setup(void) |
192 | { | |
193 | BlockBackend *blk; | |
194 | BlockDriverState *bs, *backing; | |
195 | ||
196 | blk = blk_new(qemu_get_aio_context(), BLK_PERM_ALL, BLK_PERM_ALL); | |
197 | bs = bdrv_new_open_driver(&bdrv_test, "test-node", BDRV_O_RDWR, | |
198 | &error_abort); | |
199 | blk_insert_bs(blk, bs, &error_abort); | |
200 | ||
201 | backing = bdrv_new_open_driver(&bdrv_test, "backing", 0, &error_abort); | |
202 | bdrv_set_backing_hd(bs, backing, &error_abort); | |
203 | ||
204 | bdrv_unref(backing); | |
205 | bdrv_unref(bs); | |
206 | ||
207 | return blk; | |
208 | } | |
209 | ||
f62c1729 KW |
210 | static void do_drain_end_unlocked(enum drain_type drain_type, BlockDriverState *bs) |
211 | { | |
212 | if (drain_type != BDRV_DRAIN_ALL) { | |
213 | aio_context_acquire(bdrv_get_aio_context(bs)); | |
214 | } | |
215 | do_drain_end(drain_type, bs); | |
216 | if (drain_type != BDRV_DRAIN_ALL) { | |
217 | aio_context_release(bdrv_get_aio_context(bs)); | |
218 | } | |
219 | } | |
220 | ||
004915a9 KW |
221 | /* |
222 | * Locking the block graph would be a bit cumbersome here because this function | |
223 | * is called both in coroutine and non-coroutine context. We know this is a test | |
224 | * and nothing else is running, so don't bother with TSA. | |
225 | */ | |
226 | static void coroutine_mixed_fn TSA_NO_TSA | |
227 | test_drv_cb_common(BlockBackend *blk, enum drain_type drain_type, | |
228 | bool recursive) | |
881cfd17 | 229 | { |
57f3d07b KW |
230 | BlockDriverState *bs = blk_bs(blk); |
231 | BlockDriverState *backing = bs->backing->bs; | |
86e1c840 | 232 | BDRVTestState *s, *backing_s; |
881cfd17 KW |
233 | BlockAIOCB *acb; |
234 | int aio_ret; | |
235 | ||
405d8fe0 | 236 | QEMUIOVector qiov = QEMU_IOVEC_INIT_BUF(qiov, NULL, 0); |
881cfd17 | 237 | |
881cfd17 | 238 | s = bs->opaque; |
86e1c840 | 239 | backing_s = backing->opaque; |
86e1c840 | 240 | |
881cfd17 KW |
241 | /* Simple bdrv_drain_all_begin/end pair, check that CBs are called */ |
242 | g_assert_cmpint(s->drain_count, ==, 0); | |
86e1c840 KW |
243 | g_assert_cmpint(backing_s->drain_count, ==, 0); |
244 | ||
245 | do_drain_begin(drain_type, bs); | |
246 | ||
881cfd17 | 247 | g_assert_cmpint(s->drain_count, ==, 1); |
86e1c840 KW |
248 | g_assert_cmpint(backing_s->drain_count, ==, !!recursive); |
249 | ||
250 | do_drain_end(drain_type, bs); | |
251 | ||
881cfd17 | 252 | g_assert_cmpint(s->drain_count, ==, 0); |
86e1c840 | 253 | g_assert_cmpint(backing_s->drain_count, ==, 0); |
881cfd17 KW |
254 | |
255 | /* Now do the same while a request is pending */ | |
256 | aio_ret = -EINPROGRESS; | |
257 | acb = blk_aio_preadv(blk, 0, &qiov, 0, aio_ret_cb, &aio_ret); | |
258 | g_assert(acb != NULL); | |
259 | g_assert_cmpint(aio_ret, ==, -EINPROGRESS); | |
260 | ||
261 | g_assert_cmpint(s->drain_count, ==, 0); | |
86e1c840 KW |
262 | g_assert_cmpint(backing_s->drain_count, ==, 0); |
263 | ||
264 | do_drain_begin(drain_type, bs); | |
265 | ||
881cfd17 KW |
266 | g_assert_cmpint(aio_ret, ==, 0); |
267 | g_assert_cmpint(s->drain_count, ==, 1); | |
86e1c840 KW |
268 | g_assert_cmpint(backing_s->drain_count, ==, !!recursive); |
269 | ||
270 | do_drain_end(drain_type, bs); | |
271 | ||
881cfd17 | 272 | g_assert_cmpint(s->drain_count, ==, 0); |
86e1c840 | 273 | g_assert_cmpint(backing_s->drain_count, ==, 0); |
881cfd17 KW |
274 | } |
275 | ||
86e1c840 KW |
276 | static void test_drv_cb_drain_all(void) |
277 | { | |
57f3d07b KW |
278 | BlockBackend *blk = test_setup(); |
279 | test_drv_cb_common(blk, BDRV_DRAIN_ALL, true); | |
280 | blk_unref(blk); | |
86e1c840 KW |
281 | } |
282 | ||
283 | static void test_drv_cb_drain(void) | |
284 | { | |
57f3d07b KW |
285 | BlockBackend *blk = test_setup(); |
286 | test_drv_cb_common(blk, BDRV_DRAIN, false); | |
287 | blk_unref(blk); | |
288 | } | |
289 | ||
290 | static void coroutine_fn test_drv_cb_co_drain_all_entry(void) | |
291 | { | |
292 | BlockBackend *blk = blk_all_next(NULL); | |
293 | test_drv_cb_common(blk, BDRV_DRAIN_ALL, true); | |
86e1c840 KW |
294 | } |
295 | ||
6d0252f2 KW |
296 | static void test_drv_cb_co_drain_all(void) |
297 | { | |
57f3d07b KW |
298 | BlockBackend *blk = test_setup(); |
299 | call_in_coroutine(test_drv_cb_co_drain_all_entry); | |
300 | blk_unref(blk); | |
6d0252f2 KW |
301 | } |
302 | ||
57f3d07b | 303 | static void coroutine_fn test_drv_cb_co_drain_entry(void) |
0582eb10 | 304 | { |
57f3d07b KW |
305 | BlockBackend *blk = blk_all_next(NULL); |
306 | test_drv_cb_common(blk, BDRV_DRAIN, false); | |
0582eb10 KW |
307 | } |
308 | ||
57f3d07b | 309 | static void test_drv_cb_co_drain(void) |
89a6ceab | 310 | { |
57f3d07b KW |
311 | BlockBackend *blk = test_setup(); |
312 | call_in_coroutine(test_drv_cb_co_drain_entry); | |
313 | blk_unref(blk); | |
314 | } | |
89a6ceab | 315 | |
004915a9 KW |
316 | /* |
317 | * Locking the block graph would be a bit cumbersome here because this function | |
318 | * is called both in coroutine and non-coroutine context. We know this is a test | |
319 | * and nothing else is running, so don't bother with TSA. | |
320 | */ | |
321 | static void coroutine_mixed_fn TSA_NO_TSA | |
322 | test_quiesce_common(BlockBackend *blk, enum drain_type drain_type, | |
323 | bool recursive) | |
57f3d07b KW |
324 | { |
325 | BlockDriverState *bs = blk_bs(blk); | |
326 | BlockDriverState *backing = bs->backing->bs; | |
89a6ceab KW |
327 | |
328 | g_assert_cmpint(bs->quiesce_counter, ==, 0); | |
329 | g_assert_cmpint(backing->quiesce_counter, ==, 0); | |
330 | ||
331 | do_drain_begin(drain_type, bs); | |
332 | ||
57e05be3 KW |
333 | if (drain_type == BDRV_DRAIN_ALL) { |
334 | g_assert_cmpint(bs->quiesce_counter, ==, 2); | |
335 | } else { | |
336 | g_assert_cmpint(bs->quiesce_counter, ==, 1); | |
337 | } | |
89a6ceab KW |
338 | g_assert_cmpint(backing->quiesce_counter, ==, !!recursive); |
339 | ||
340 | do_drain_end(drain_type, bs); | |
341 | ||
342 | g_assert_cmpint(bs->quiesce_counter, ==, 0); | |
343 | g_assert_cmpint(backing->quiesce_counter, ==, 0); | |
89a6ceab KW |
344 | } |
345 | ||
346 | static void test_quiesce_drain_all(void) | |
347 | { | |
57f3d07b KW |
348 | BlockBackend *blk = test_setup(); |
349 | test_quiesce_common(blk, BDRV_DRAIN_ALL, true); | |
350 | blk_unref(blk); | |
89a6ceab KW |
351 | } |
352 | ||
353 | static void test_quiesce_drain(void) | |
354 | { | |
57f3d07b KW |
355 | BlockBackend *blk = test_setup(); |
356 | test_quiesce_common(blk, BDRV_DRAIN, false); | |
357 | blk_unref(blk); | |
358 | } | |
359 | ||
360 | static void coroutine_fn test_quiesce_co_drain_all_entry(void) | |
361 | { | |
362 | BlockBackend *blk = blk_all_next(NULL); | |
363 | test_quiesce_common(blk, BDRV_DRAIN_ALL, true); | |
89a6ceab KW |
364 | } |
365 | ||
6d0252f2 KW |
366 | static void test_quiesce_co_drain_all(void) |
367 | { | |
57f3d07b KW |
368 | BlockBackend *blk = test_setup(); |
369 | call_in_coroutine(test_quiesce_co_drain_all_entry); | |
370 | blk_unref(blk); | |
371 | } | |
372 | ||
373 | static void coroutine_fn test_quiesce_co_drain_entry(void) | |
374 | { | |
375 | BlockBackend *blk = blk_all_next(NULL); | |
376 | test_quiesce_common(blk, BDRV_DRAIN, false); | |
6d0252f2 KW |
377 | } |
378 | ||
0582eb10 KW |
379 | static void test_quiesce_co_drain(void) |
380 | { | |
57f3d07b KW |
381 | BlockBackend *blk = test_setup(); |
382 | call_in_coroutine(test_quiesce_co_drain_entry); | |
383 | blk_unref(blk); | |
0582eb10 KW |
384 | } |
385 | ||
6c429a6a KW |
386 | static void test_nested(void) |
387 | { | |
388 | BlockBackend *blk; | |
389 | BlockDriverState *bs, *backing; | |
390 | BDRVTestState *s, *backing_s; | |
391 | enum drain_type outer, inner; | |
392 | ||
d861ab3a | 393 | blk = blk_new(qemu_get_aio_context(), BLK_PERM_ALL, BLK_PERM_ALL); |
6c429a6a KW |
394 | bs = bdrv_new_open_driver(&bdrv_test, "test-node", BDRV_O_RDWR, |
395 | &error_abort); | |
396 | s = bs->opaque; | |
397 | blk_insert_bs(blk, bs, &error_abort); | |
398 | ||
399 | backing = bdrv_new_open_driver(&bdrv_test, "backing", 0, &error_abort); | |
400 | backing_s = backing->opaque; | |
401 | bdrv_set_backing_hd(bs, backing, &error_abort); | |
402 | ||
403 | for (outer = 0; outer < DRAIN_TYPE_MAX; outer++) { | |
404 | for (inner = 0; inner < DRAIN_TYPE_MAX; inner++) { | |
57e05be3 KW |
405 | int backing_quiesce = (outer == BDRV_DRAIN_ALL) + |
406 | (inner == BDRV_DRAIN_ALL); | |
6c429a6a KW |
407 | |
408 | g_assert_cmpint(bs->quiesce_counter, ==, 0); | |
409 | g_assert_cmpint(backing->quiesce_counter, ==, 0); | |
410 | g_assert_cmpint(s->drain_count, ==, 0); | |
411 | g_assert_cmpint(backing_s->drain_count, ==, 0); | |
412 | ||
413 | do_drain_begin(outer, bs); | |
414 | do_drain_begin(inner, bs); | |
415 | ||
57e05be3 | 416 | g_assert_cmpint(bs->quiesce_counter, ==, 2 + !!backing_quiesce); |
6c429a6a | 417 | g_assert_cmpint(backing->quiesce_counter, ==, backing_quiesce); |
57e05be3 KW |
418 | g_assert_cmpint(s->drain_count, ==, 1); |
419 | g_assert_cmpint(backing_s->drain_count, ==, !!backing_quiesce); | |
6c429a6a KW |
420 | |
421 | do_drain_end(inner, bs); | |
422 | do_drain_end(outer, bs); | |
423 | ||
424 | g_assert_cmpint(bs->quiesce_counter, ==, 0); | |
425 | g_assert_cmpint(backing->quiesce_counter, ==, 0); | |
426 | g_assert_cmpint(s->drain_count, ==, 0); | |
427 | g_assert_cmpint(backing_s->drain_count, ==, 0); | |
428 | } | |
429 | } | |
430 | ||
431 | bdrv_unref(backing); | |
432 | bdrv_unref(bs); | |
433 | blk_unref(blk); | |
434 | } | |
435 | ||
19f7a7e5 KW |
436 | static void test_graph_change_drain_all(void) |
437 | { | |
438 | BlockBackend *blk_a, *blk_b; | |
439 | BlockDriverState *bs_a, *bs_b; | |
440 | BDRVTestState *a_s, *b_s; | |
441 | ||
442 | /* Create node A with a BlockBackend */ | |
d861ab3a | 443 | blk_a = blk_new(qemu_get_aio_context(), BLK_PERM_ALL, BLK_PERM_ALL); |
19f7a7e5 KW |
444 | bs_a = bdrv_new_open_driver(&bdrv_test, "test-node-a", BDRV_O_RDWR, |
445 | &error_abort); | |
446 | a_s = bs_a->opaque; | |
447 | blk_insert_bs(blk_a, bs_a, &error_abort); | |
448 | ||
449 | g_assert_cmpint(bs_a->quiesce_counter, ==, 0); | |
450 | g_assert_cmpint(a_s->drain_count, ==, 0); | |
451 | ||
452 | /* Call bdrv_drain_all_begin() */ | |
453 | bdrv_drain_all_begin(); | |
454 | ||
455 | g_assert_cmpint(bs_a->quiesce_counter, ==, 1); | |
456 | g_assert_cmpint(a_s->drain_count, ==, 1); | |
457 | ||
458 | /* Create node B with a BlockBackend */ | |
d861ab3a | 459 | blk_b = blk_new(qemu_get_aio_context(), BLK_PERM_ALL, BLK_PERM_ALL); |
19f7a7e5 KW |
460 | bs_b = bdrv_new_open_driver(&bdrv_test, "test-node-b", BDRV_O_RDWR, |
461 | &error_abort); | |
462 | b_s = bs_b->opaque; | |
463 | blk_insert_bs(blk_b, bs_b, &error_abort); | |
464 | ||
465 | g_assert_cmpint(bs_a->quiesce_counter, ==, 1); | |
466 | g_assert_cmpint(bs_b->quiesce_counter, ==, 1); | |
467 | g_assert_cmpint(a_s->drain_count, ==, 1); | |
468 | g_assert_cmpint(b_s->drain_count, ==, 1); | |
469 | ||
470 | /* Unref and finally delete node A */ | |
471 | blk_unref(blk_a); | |
472 | ||
473 | g_assert_cmpint(bs_a->quiesce_counter, ==, 1); | |
474 | g_assert_cmpint(bs_b->quiesce_counter, ==, 1); | |
475 | g_assert_cmpint(a_s->drain_count, ==, 1); | |
476 | g_assert_cmpint(b_s->drain_count, ==, 1); | |
477 | ||
478 | bdrv_unref(bs_a); | |
479 | ||
480 | g_assert_cmpint(bs_b->quiesce_counter, ==, 1); | |
481 | g_assert_cmpint(b_s->drain_count, ==, 1); | |
482 | ||
483 | /* End the drained section */ | |
484 | bdrv_drain_all_end(); | |
485 | ||
486 | g_assert_cmpint(bs_b->quiesce_counter, ==, 0); | |
487 | g_assert_cmpint(b_s->drain_count, ==, 0); | |
488 | ||
489 | bdrv_unref(bs_b); | |
490 | blk_unref(blk_b); | |
491 | } | |
492 | ||
bb675689 KW |
493 | struct test_iothread_data { |
494 | BlockDriverState *bs; | |
495 | enum drain_type drain_type; | |
496 | int *aio_ret; | |
ab613350 | 497 | bool co_done; |
bb675689 KW |
498 | }; |
499 | ||
ab613350 | 500 | static void coroutine_fn test_iothread_drain_co_entry(void *opaque) |
bb675689 KW |
501 | { |
502 | struct test_iothread_data *data = opaque; | |
503 | ||
bb675689 KW |
504 | do_drain_begin(data->drain_type, data->bs); |
505 | g_assert_cmpint(*data->aio_ret, ==, 0); | |
506 | do_drain_end(data->drain_type, data->bs); | |
bb675689 | 507 | |
ab613350 SH |
508 | data->co_done = true; |
509 | aio_wait_kick(); | |
bb675689 KW |
510 | } |
511 | ||
512 | static void test_iothread_aio_cb(void *opaque, int ret) | |
513 | { | |
514 | int *aio_ret = opaque; | |
515 | *aio_ret = ret; | |
516 | qemu_event_set(&done_event); | |
517 | } | |
518 | ||
ecc1a5c7 KW |
519 | static void test_iothread_main_thread_bh(void *opaque) |
520 | { | |
521 | struct test_iothread_data *data = opaque; | |
522 | ||
523 | /* Test that the AioContext is not yet locked in a random BH that is | |
524 | * executed during drain, otherwise this would deadlock. */ | |
525 | aio_context_acquire(bdrv_get_aio_context(data->bs)); | |
526 | bdrv_flush(data->bs); | |
c8bf923d | 527 | bdrv_dec_in_flight(data->bs); /* incremented by test_iothread_common() */ |
ecc1a5c7 KW |
528 | aio_context_release(bdrv_get_aio_context(data->bs)); |
529 | } | |
530 | ||
bb675689 KW |
531 | /* |
532 | * Starts an AIO request on a BDS that runs in the AioContext of iothread 1. | |
533 | * The request involves a BH on iothread 2 before it can complete. | |
534 | * | |
535 | * @drain_thread = 0 means that do_drain_begin/end are called from the main | |
536 | * thread, @drain_thread = 1 means that they are called from iothread 1. Drain | |
537 | * for this BDS cannot be called from iothread 2 because only the main thread | |
538 | * may do cross-AioContext polling. | |
539 | */ | |
540 | static void test_iothread_common(enum drain_type drain_type, int drain_thread) | |
541 | { | |
542 | BlockBackend *blk; | |
543 | BlockDriverState *bs; | |
544 | BDRVTestState *s; | |
545 | BlockAIOCB *acb; | |
ab613350 | 546 | Coroutine *co; |
bb675689 KW |
547 | int aio_ret; |
548 | struct test_iothread_data data; | |
549 | ||
550 | IOThread *a = iothread_new(); | |
551 | IOThread *b = iothread_new(); | |
552 | AioContext *ctx_a = iothread_get_aio_context(a); | |
553 | AioContext *ctx_b = iothread_get_aio_context(b); | |
554 | ||
405d8fe0 | 555 | QEMUIOVector qiov = QEMU_IOVEC_INIT_BUF(qiov, NULL, 0); |
bb675689 KW |
556 | |
557 | /* bdrv_drain_all() may only be called from the main loop thread */ | |
558 | if (drain_type == BDRV_DRAIN_ALL && drain_thread != 0) { | |
559 | goto out; | |
560 | } | |
561 | ||
d861ab3a | 562 | blk = blk_new(qemu_get_aio_context(), BLK_PERM_ALL, BLK_PERM_ALL); |
bb675689 KW |
563 | bs = bdrv_new_open_driver(&bdrv_test, "test-node", BDRV_O_RDWR, |
564 | &error_abort); | |
565 | s = bs->opaque; | |
566 | blk_insert_bs(blk, bs, &error_abort); | |
cf312932 | 567 | blk_set_disable_request_queuing(blk, true); |
bb675689 | 568 | |
97896a48 | 569 | blk_set_aio_context(blk, ctx_a, &error_abort); |
bb675689 KW |
570 | aio_context_acquire(ctx_a); |
571 | ||
572 | s->bh_indirection_ctx = ctx_b; | |
573 | ||
574 | aio_ret = -EINPROGRESS; | |
dd353157 KW |
575 | qemu_event_reset(&done_event); |
576 | ||
bb675689 KW |
577 | if (drain_thread == 0) { |
578 | acb = blk_aio_preadv(blk, 0, &qiov, 0, test_iothread_aio_cb, &aio_ret); | |
579 | } else { | |
580 | acb = blk_aio_preadv(blk, 0, &qiov, 0, aio_ret_cb, &aio_ret); | |
581 | } | |
582 | g_assert(acb != NULL); | |
583 | g_assert_cmpint(aio_ret, ==, -EINPROGRESS); | |
584 | ||
585 | aio_context_release(ctx_a); | |
586 | ||
587 | data = (struct test_iothread_data) { | |
588 | .bs = bs, | |
589 | .drain_type = drain_type, | |
590 | .aio_ret = &aio_ret, | |
591 | }; | |
592 | ||
593 | switch (drain_thread) { | |
594 | case 0: | |
595 | if (drain_type != BDRV_DRAIN_ALL) { | |
596 | aio_context_acquire(ctx_a); | |
597 | } | |
598 | ||
c8bf923d SH |
599 | /* |
600 | * Increment in_flight so that do_drain_begin() waits for | |
601 | * test_iothread_main_thread_bh(). This prevents the race between | |
602 | * test_iothread_main_thread_bh() in IOThread a and do_drain_begin() in | |
603 | * this thread. test_iothread_main_thread_bh() decrements in_flight. | |
604 | */ | |
605 | bdrv_inc_in_flight(bs); | |
ecc1a5c7 KW |
606 | aio_bh_schedule_oneshot(ctx_a, test_iothread_main_thread_bh, &data); |
607 | ||
bb675689 KW |
608 | /* The request is running on the IOThread a. Draining its block device |
609 | * will make sure that it has completed as far as the BDS is concerned, | |
610 | * but the drain in this thread can continue immediately after | |
611 | * bdrv_dec_in_flight() and aio_ret might be assigned only slightly | |
612 | * later. */ | |
bb675689 KW |
613 | do_drain_begin(drain_type, bs); |
614 | g_assert_cmpint(bs->in_flight, ==, 0); | |
615 | ||
616 | if (drain_type != BDRV_DRAIN_ALL) { | |
617 | aio_context_release(ctx_a); | |
618 | } | |
619 | qemu_event_wait(&done_event); | |
620 | if (drain_type != BDRV_DRAIN_ALL) { | |
621 | aio_context_acquire(ctx_a); | |
622 | } | |
623 | ||
624 | g_assert_cmpint(aio_ret, ==, 0); | |
625 | do_drain_end(drain_type, bs); | |
626 | ||
627 | if (drain_type != BDRV_DRAIN_ALL) { | |
628 | aio_context_release(ctx_a); | |
629 | } | |
630 | break; | |
631 | case 1: | |
ab613350 SH |
632 | co = qemu_coroutine_create(test_iothread_drain_co_entry, &data); |
633 | aio_co_enter(ctx_a, co); | |
634 | AIO_WAIT_WHILE_UNLOCKED(NULL, !data.co_done); | |
bb675689 KW |
635 | break; |
636 | default: | |
637 | g_assert_not_reached(); | |
638 | } | |
639 | ||
640 | aio_context_acquire(ctx_a); | |
97896a48 | 641 | blk_set_aio_context(blk, qemu_get_aio_context(), &error_abort); |
bb675689 KW |
642 | aio_context_release(ctx_a); |
643 | ||
644 | bdrv_unref(bs); | |
645 | blk_unref(blk); | |
646 | ||
647 | out: | |
648 | iothread_join(a); | |
649 | iothread_join(b); | |
650 | } | |
651 | ||
652 | static void test_iothread_drain_all(void) | |
653 | { | |
654 | test_iothread_common(BDRV_DRAIN_ALL, 0); | |
655 | test_iothread_common(BDRV_DRAIN_ALL, 1); | |
656 | } | |
657 | ||
658 | static void test_iothread_drain(void) | |
659 | { | |
660 | test_iothread_common(BDRV_DRAIN, 0); | |
661 | test_iothread_common(BDRV_DRAIN, 1); | |
662 | } | |
663 | ||
7253220d KW |
664 | |
665 | typedef struct TestBlockJob { | |
666 | BlockJob common; | |
1b177bbe | 667 | BlockDriverState *bs; |
d49725af KW |
668 | int run_ret; |
669 | int prepare_ret; | |
d8b3afd5 | 670 | bool running; |
7253220d KW |
671 | bool should_complete; |
672 | } TestBlockJob; | |
673 | ||
ae23dde9 KW |
674 | static int test_job_prepare(Job *job) |
675 | { | |
676 | TestBlockJob *s = container_of(job, TestBlockJob, common.job); | |
677 | ||
678 | /* Provoke an AIO_WAIT_WHILE() call to verify there is no deadlock */ | |
1b177bbe | 679 | bdrv_flush(s->bs); |
d49725af KW |
680 | return s->prepare_ret; |
681 | } | |
682 | ||
683 | static void test_job_commit(Job *job) | |
684 | { | |
685 | TestBlockJob *s = container_of(job, TestBlockJob, common.job); | |
686 | ||
687 | /* Provoke an AIO_WAIT_WHILE() call to verify there is no deadlock */ | |
1b177bbe | 688 | bdrv_flush(s->bs); |
d49725af KW |
689 | } |
690 | ||
691 | static void test_job_abort(Job *job) | |
692 | { | |
693 | TestBlockJob *s = container_of(job, TestBlockJob, common.job); | |
694 | ||
695 | /* Provoke an AIO_WAIT_WHILE() call to verify there is no deadlock */ | |
1b177bbe | 696 | bdrv_flush(s->bs); |
ae23dde9 KW |
697 | } |
698 | ||
f67432a2 | 699 | static int coroutine_fn test_job_run(Job *job, Error **errp) |
7253220d | 700 | { |
f67432a2 | 701 | TestBlockJob *s = container_of(job, TestBlockJob, common.job); |
7253220d | 702 | |
d8b3afd5 KW |
703 | /* We are running the actual job code past the pause point in |
704 | * job_co_entry(). */ | |
705 | s->running = true; | |
706 | ||
2e1795b5 | 707 | job_transition_to_ready(&s->common.job); |
7253220d | 708 | while (!s->should_complete) { |
5599c162 KW |
709 | /* Avoid job_sleep_ns() because it marks the job as !busy. We want to |
710 | * emulate some actual activity (probably some I/O) here so that drain | |
711 | * has to wait for this activity to stop. */ | |
d8b3afd5 KW |
712 | qemu_co_sleep_ns(QEMU_CLOCK_REALTIME, 1000000); |
713 | ||
89bd0305 | 714 | job_pause_point(&s->common.job); |
7253220d KW |
715 | } |
716 | ||
d49725af | 717 | return s->run_ret; |
7253220d KW |
718 | } |
719 | ||
3453d972 | 720 | static void test_job_complete(Job *job, Error **errp) |
7253220d | 721 | { |
3453d972 | 722 | TestBlockJob *s = container_of(job, TestBlockJob, common.job); |
7253220d KW |
723 | s->should_complete = true; |
724 | } | |
725 | ||
726 | BlockJobDriver test_job_driver = { | |
33e9e9bd KW |
727 | .job_driver = { |
728 | .instance_size = sizeof(TestBlockJob), | |
80fa2c75 | 729 | .free = block_job_free, |
b15de828 | 730 | .user_resume = block_job_user_resume, |
f67432a2 | 731 | .run = test_job_run, |
3453d972 | 732 | .complete = test_job_complete, |
ae23dde9 | 733 | .prepare = test_job_prepare, |
d49725af KW |
734 | .commit = test_job_commit, |
735 | .abort = test_job_abort, | |
33e9e9bd | 736 | }, |
7253220d KW |
737 | }; |
738 | ||
d49725af KW |
739 | enum test_job_result { |
740 | TEST_JOB_SUCCESS, | |
741 | TEST_JOB_FAIL_RUN, | |
742 | TEST_JOB_FAIL_PREPARE, | |
743 | }; | |
744 | ||
d8b3afd5 KW |
745 | enum test_job_drain_node { |
746 | TEST_JOB_DRAIN_SRC, | |
747 | TEST_JOB_DRAIN_SRC_CHILD, | |
d8b3afd5 KW |
748 | }; |
749 | ||
750 | static void test_blockjob_common_drain_node(enum drain_type drain_type, | |
751 | bool use_iothread, | |
752 | enum test_job_result result, | |
753 | enum test_job_drain_node drain_node) | |
7253220d KW |
754 | { |
755 | BlockBackend *blk_src, *blk_target; | |
d8b3afd5 | 756 | BlockDriverState *src, *src_backing, *src_overlay, *target, *drain_bs; |
7253220d | 757 | BlockJob *job; |
d49725af | 758 | TestBlockJob *tjob; |
f62c1729 KW |
759 | IOThread *iothread = NULL; |
760 | AioContext *ctx; | |
7253220d KW |
761 | int ret; |
762 | ||
763 | src = bdrv_new_open_driver(&bdrv_test, "source", BDRV_O_RDWR, | |
764 | &error_abort); | |
d8b3afd5 KW |
765 | src_backing = bdrv_new_open_driver(&bdrv_test, "source-backing", |
766 | BDRV_O_RDWR, &error_abort); | |
767 | src_overlay = bdrv_new_open_driver(&bdrv_test, "source-overlay", | |
768 | BDRV_O_RDWR, &error_abort); | |
769 | ||
770 | bdrv_set_backing_hd(src_overlay, src, &error_abort); | |
771 | bdrv_unref(src); | |
772 | bdrv_set_backing_hd(src, src_backing, &error_abort); | |
773 | bdrv_unref(src_backing); | |
774 | ||
d861ab3a | 775 | blk_src = blk_new(qemu_get_aio_context(), BLK_PERM_ALL, BLK_PERM_ALL); |
d8b3afd5 KW |
776 | blk_insert_bs(blk_src, src_overlay, &error_abort); |
777 | ||
778 | switch (drain_node) { | |
779 | case TEST_JOB_DRAIN_SRC: | |
780 | drain_bs = src; | |
781 | break; | |
782 | case TEST_JOB_DRAIN_SRC_CHILD: | |
783 | drain_bs = src_backing; | |
784 | break; | |
d8b3afd5 KW |
785 | default: |
786 | g_assert_not_reached(); | |
787 | } | |
7253220d | 788 | |
f62c1729 KW |
789 | if (use_iothread) { |
790 | iothread = iothread_new(); | |
791 | ctx = iothread_get_aio_context(iothread); | |
97896a48 | 792 | blk_set_aio_context(blk_src, ctx, &error_abort); |
f62c1729 KW |
793 | } else { |
794 | ctx = qemu_get_aio_context(); | |
795 | } | |
796 | ||
7253220d KW |
797 | target = bdrv_new_open_driver(&bdrv_test, "target", BDRV_O_RDWR, |
798 | &error_abort); | |
d861ab3a | 799 | blk_target = blk_new(qemu_get_aio_context(), BLK_PERM_ALL, BLK_PERM_ALL); |
7253220d | 800 | blk_insert_bs(blk_target, target, &error_abort); |
132ada80 | 801 | blk_set_allow_aio_context_change(blk_target, true); |
7253220d | 802 | |
f62c1729 | 803 | aio_context_acquire(ctx); |
d49725af KW |
804 | tjob = block_job_create("job0", &test_job_driver, NULL, src, |
805 | 0, BLK_PERM_ALL, | |
806 | 0, 0, NULL, NULL, &error_abort); | |
1b177bbe | 807 | tjob->bs = src; |
d49725af | 808 | job = &tjob->common; |
f3bbc53d KW |
809 | |
810 | bdrv_graph_wrlock(target); | |
7253220d | 811 | block_job_add_bdrv(job, "target", target, 0, BLK_PERM_ALL, &error_abort); |
f3bbc53d | 812 | bdrv_graph_wrunlock(); |
d49725af KW |
813 | |
814 | switch (result) { | |
815 | case TEST_JOB_SUCCESS: | |
816 | break; | |
817 | case TEST_JOB_FAIL_RUN: | |
818 | tjob->run_ret = -EIO; | |
819 | break; | |
820 | case TEST_JOB_FAIL_PREPARE: | |
821 | tjob->prepare_ret = -EIO; | |
822 | break; | |
823 | } | |
6f592e5a | 824 | aio_context_release(ctx); |
d49725af | 825 | |
da01ff7f | 826 | job_start(&job->job); |
7253220d | 827 | |
d8b3afd5 KW |
828 | if (use_iothread) { |
829 | /* job_co_entry() is run in the I/O thread, wait for the actual job | |
830 | * code to start (we don't want to catch the job in the pause point in | |
831 | * job_co_entry(). */ | |
832 | while (!tjob->running) { | |
833 | aio_poll(qemu_get_aio_context(), false); | |
834 | } | |
835 | } | |
836 | ||
191e7af3 EGE |
837 | WITH_JOB_LOCK_GUARD() { |
838 | g_assert_cmpint(job->job.pause_count, ==, 0); | |
839 | g_assert_false(job->job.paused); | |
840 | g_assert_true(tjob->running); | |
841 | g_assert_true(job->job.busy); /* We're in qemu_co_sleep_ns() */ | |
842 | } | |
7253220d | 843 | |
d8b3afd5 | 844 | do_drain_begin_unlocked(drain_type, drain_bs); |
7253220d | 845 | |
191e7af3 EGE |
846 | WITH_JOB_LOCK_GUARD() { |
847 | if (drain_type == BDRV_DRAIN_ALL) { | |
848 | /* bdrv_drain_all() drains both src and target */ | |
849 | g_assert_cmpint(job->job.pause_count, ==, 2); | |
850 | } else { | |
851 | g_assert_cmpint(job->job.pause_count, ==, 1); | |
852 | } | |
853 | g_assert_true(job->job.paused); | |
854 | g_assert_false(job->job.busy); /* The job is paused */ | |
7253220d | 855 | } |
7253220d | 856 | |
d8b3afd5 | 857 | do_drain_end_unlocked(drain_type, drain_bs); |
f62c1729 KW |
858 | |
859 | if (use_iothread) { | |
191e7af3 EGE |
860 | /* |
861 | * Here we are waiting for the paused status to change, | |
862 | * so don't bother protecting the read every time. | |
863 | * | |
864 | * paused is reset in the I/O thread, wait for it | |
865 | */ | |
f62c1729 KW |
866 | while (job->job.paused) { |
867 | aio_poll(qemu_get_aio_context(), false); | |
868 | } | |
869 | } | |
7253220d | 870 | |
191e7af3 EGE |
871 | WITH_JOB_LOCK_GUARD() { |
872 | g_assert_cmpint(job->job.pause_count, ==, 0); | |
873 | g_assert_false(job->job.paused); | |
874 | g_assert_true(job->job.busy); /* We're in qemu_co_sleep_ns() */ | |
875 | } | |
7253220d | 876 | |
132ada80 | 877 | do_drain_begin_unlocked(drain_type, target); |
7253220d | 878 | |
191e7af3 EGE |
879 | WITH_JOB_LOCK_GUARD() { |
880 | if (drain_type == BDRV_DRAIN_ALL) { | |
881 | /* bdrv_drain_all() drains both src and target */ | |
882 | g_assert_cmpint(job->job.pause_count, ==, 2); | |
883 | } else { | |
884 | g_assert_cmpint(job->job.pause_count, ==, 1); | |
885 | } | |
886 | g_assert_true(job->job.paused); | |
887 | g_assert_false(job->job.busy); /* The job is paused */ | |
7253220d | 888 | } |
7253220d | 889 | |
132ada80 | 890 | do_drain_end_unlocked(drain_type, target); |
7253220d | 891 | |
f62c1729 | 892 | if (use_iothread) { |
191e7af3 EGE |
893 | /* |
894 | * Here we are waiting for the paused status to change, | |
895 | * so don't bother protecting the read every time. | |
896 | * | |
897 | * paused is reset in the I/O thread, wait for it | |
898 | */ | |
f62c1729 KW |
899 | while (job->job.paused) { |
900 | aio_poll(qemu_get_aio_context(), false); | |
901 | } | |
902 | } | |
903 | ||
191e7af3 EGE |
904 | WITH_JOB_LOCK_GUARD() { |
905 | g_assert_cmpint(job->job.pause_count, ==, 0); | |
906 | g_assert_false(job->job.paused); | |
907 | g_assert_true(job->job.busy); /* We're in qemu_co_sleep_ns() */ | |
908 | } | |
7253220d | 909 | |
191e7af3 EGE |
910 | WITH_JOB_LOCK_GUARD() { |
911 | ret = job_complete_sync_locked(&job->job, &error_abort); | |
912 | } | |
d49725af | 913 | g_assert_cmpint(ret, ==, (result == TEST_JOB_SUCCESS ? 0 : -EIO)); |
7253220d | 914 | |
6f592e5a | 915 | aio_context_acquire(ctx); |
f62c1729 | 916 | if (use_iothread) { |
97896a48 | 917 | blk_set_aio_context(blk_src, qemu_get_aio_context(), &error_abort); |
ad943dcb | 918 | assert(blk_get_aio_context(blk_target) == qemu_get_aio_context()); |
f62c1729 KW |
919 | } |
920 | aio_context_release(ctx); | |
921 | ||
7253220d KW |
922 | blk_unref(blk_src); |
923 | blk_unref(blk_target); | |
d8b3afd5 | 924 | bdrv_unref(src_overlay); |
7253220d | 925 | bdrv_unref(target); |
f62c1729 KW |
926 | |
927 | if (iothread) { | |
928 | iothread_join(iothread); | |
929 | } | |
7253220d KW |
930 | } |
931 | ||
d8b3afd5 KW |
932 | static void test_blockjob_common(enum drain_type drain_type, bool use_iothread, |
933 | enum test_job_result result) | |
934 | { | |
935 | test_blockjob_common_drain_node(drain_type, use_iothread, result, | |
936 | TEST_JOB_DRAIN_SRC); | |
937 | test_blockjob_common_drain_node(drain_type, use_iothread, result, | |
938 | TEST_JOB_DRAIN_SRC_CHILD); | |
d8b3afd5 KW |
939 | } |
940 | ||
7253220d KW |
941 | static void test_blockjob_drain_all(void) |
942 | { | |
d49725af | 943 | test_blockjob_common(BDRV_DRAIN_ALL, false, TEST_JOB_SUCCESS); |
7253220d KW |
944 | } |
945 | ||
946 | static void test_blockjob_drain(void) | |
947 | { | |
d49725af | 948 | test_blockjob_common(BDRV_DRAIN, false, TEST_JOB_SUCCESS); |
7253220d KW |
949 | } |
950 | ||
d49725af KW |
951 | static void test_blockjob_error_drain_all(void) |
952 | { | |
953 | test_blockjob_common(BDRV_DRAIN_ALL, false, TEST_JOB_FAIL_RUN); | |
954 | test_blockjob_common(BDRV_DRAIN_ALL, false, TEST_JOB_FAIL_PREPARE); | |
955 | } | |
956 | ||
957 | static void test_blockjob_error_drain(void) | |
958 | { | |
959 | test_blockjob_common(BDRV_DRAIN, false, TEST_JOB_FAIL_RUN); | |
960 | test_blockjob_common(BDRV_DRAIN, false, TEST_JOB_FAIL_PREPARE); | |
961 | } | |
962 | ||
f62c1729 KW |
963 | static void test_blockjob_iothread_drain_all(void) |
964 | { | |
d49725af | 965 | test_blockjob_common(BDRV_DRAIN_ALL, true, TEST_JOB_SUCCESS); |
f62c1729 KW |
966 | } |
967 | ||
968 | static void test_blockjob_iothread_drain(void) | |
969 | { | |
d49725af | 970 | test_blockjob_common(BDRV_DRAIN, true, TEST_JOB_SUCCESS); |
f62c1729 KW |
971 | } |
972 | ||
d49725af KW |
973 | static void test_blockjob_iothread_error_drain_all(void) |
974 | { | |
975 | test_blockjob_common(BDRV_DRAIN_ALL, true, TEST_JOB_FAIL_RUN); | |
976 | test_blockjob_common(BDRV_DRAIN_ALL, true, TEST_JOB_FAIL_PREPARE); | |
977 | } | |
978 | ||
979 | static void test_blockjob_iothread_error_drain(void) | |
980 | { | |
981 | test_blockjob_common(BDRV_DRAIN, true, TEST_JOB_FAIL_RUN); | |
982 | test_blockjob_common(BDRV_DRAIN, true, TEST_JOB_FAIL_PREPARE); | |
983 | } | |
984 | ||
4c8158e3 HR |
985 | |
986 | typedef struct BDRVTestTopState { | |
987 | BdrvChild *wait_child; | |
988 | } BDRVTestTopState; | |
989 | ||
990 | static void bdrv_test_top_close(BlockDriverState *bs) | |
991 | { | |
992 | BdrvChild *c, *next_c; | |
32a8aba3 KW |
993 | |
994 | bdrv_graph_wrlock(NULL); | |
4c8158e3 HR |
995 | QLIST_FOREACH_SAFE(c, &bs->children, next, next_c) { |
996 | bdrv_unref_child(bs, c); | |
997 | } | |
32a8aba3 | 998 | bdrv_graph_wrunlock(); |
4c8158e3 HR |
999 | } |
1000 | ||
b9b10c35 KW |
1001 | static int coroutine_fn GRAPH_RDLOCK |
1002 | bdrv_test_top_co_preadv(BlockDriverState *bs, int64_t offset, int64_t bytes, | |
1003 | QEMUIOVector *qiov, BdrvRequestFlags flags) | |
4c8158e3 HR |
1004 | { |
1005 | BDRVTestTopState *tts = bs->opaque; | |
1006 | return bdrv_co_preadv(tts->wait_child, offset, bytes, qiov, flags); | |
1007 | } | |
1008 | ||
1009 | static BlockDriver bdrv_test_top_driver = { | |
1010 | .format_name = "test_top_driver", | |
1011 | .instance_size = sizeof(BDRVTestTopState), | |
1012 | ||
1013 | .bdrv_close = bdrv_test_top_close, | |
1014 | .bdrv_co_preadv = bdrv_test_top_co_preadv, | |
1015 | ||
69dca43d | 1016 | .bdrv_child_perm = bdrv_default_perms, |
4c8158e3 HR |
1017 | }; |
1018 | ||
1019 | typedef struct TestCoDeleteByDrainData { | |
1020 | BlockBackend *blk; | |
1021 | bool detach_instead_of_delete; | |
1022 | bool done; | |
1023 | } TestCoDeleteByDrainData; | |
1024 | ||
1025 | static void coroutine_fn test_co_delete_by_drain(void *opaque) | |
1026 | { | |
1027 | TestCoDeleteByDrainData *dbdd = opaque; | |
1028 | BlockBackend *blk = dbdd->blk; | |
1029 | BlockDriverState *bs = blk_bs(blk); | |
1030 | BDRVTestTopState *tts = bs->opaque; | |
1031 | void *buffer = g_malloc(65536); | |
405d8fe0 | 1032 | QEMUIOVector qiov = QEMU_IOVEC_INIT_BUF(qiov, buffer, 65536); |
4c8158e3 HR |
1033 | |
1034 | /* Pretend some internal write operation from parent to child. | |
1035 | * Important: We have to read from the child, not from the parent! | |
1036 | * Draining works by first propagating it all up the tree to the | |
1037 | * root and then waiting for drainage from root to the leaves | |
1038 | * (protocol nodes). If we have a request waiting on the root, | |
1039 | * everything will be drained before we go back down the tree, but | |
1040 | * we do not want that. We want to be in the middle of draining | |
1041 | * when this following requests returns. */ | |
87f130bd | 1042 | bdrv_graph_co_rdlock(); |
4c8158e3 | 1043 | bdrv_co_preadv(tts->wait_child, 0, 65536, &qiov, 0); |
87f130bd | 1044 | bdrv_graph_co_rdunlock(); |
4c8158e3 HR |
1045 | |
1046 | g_assert_cmpint(bs->refcnt, ==, 1); | |
1047 | ||
1048 | if (!dbdd->detach_instead_of_delete) { | |
01a10c24 | 1049 | blk_co_unref(blk); |
4c8158e3 HR |
1050 | } else { |
1051 | BdrvChild *c, *next_c; | |
680e0cc4 | 1052 | bdrv_graph_co_rdlock(); |
4c8158e3 | 1053 | QLIST_FOREACH_SAFE(c, &bs->children, next, next_c) { |
680e0cc4 | 1054 | bdrv_graph_co_rdunlock(); |
32a8aba3 | 1055 | bdrv_co_unref_child(bs, c); |
680e0cc4 | 1056 | bdrv_graph_co_rdlock(); |
4c8158e3 | 1057 | } |
680e0cc4 | 1058 | bdrv_graph_co_rdunlock(); |
4c8158e3 HR |
1059 | } |
1060 | ||
1061 | dbdd->done = true; | |
7b43db3c | 1062 | g_free(buffer); |
4c8158e3 HR |
1063 | } |
1064 | ||
1065 | /** | |
1066 | * Test what happens when some BDS has some children, you drain one of | |
1067 | * them and this results in the BDS being deleted. | |
1068 | * | |
1069 | * If @detach_instead_of_delete is set, the BDS is not going to be | |
1070 | * deleted but will only detach all of its children. | |
1071 | */ | |
ebd31837 KW |
1072 | static void do_test_delete_by_drain(bool detach_instead_of_delete, |
1073 | enum drain_type drain_type) | |
4c8158e3 HR |
1074 | { |
1075 | BlockBackend *blk; | |
1076 | BlockDriverState *bs, *child_bs, *null_bs; | |
1077 | BDRVTestTopState *tts; | |
1078 | TestCoDeleteByDrainData dbdd; | |
1079 | Coroutine *co; | |
1080 | ||
1081 | bs = bdrv_new_open_driver(&bdrv_test_top_driver, "top", BDRV_O_RDWR, | |
1082 | &error_abort); | |
1083 | bs->total_sectors = 65536 >> BDRV_SECTOR_BITS; | |
1084 | tts = bs->opaque; | |
1085 | ||
1086 | null_bs = bdrv_open("null-co://", NULL, NULL, BDRV_O_RDWR | BDRV_O_PROTOCOL, | |
1087 | &error_abort); | |
afdaeb9e | 1088 | bdrv_graph_wrlock(NULL); |
a16be3cd HR |
1089 | bdrv_attach_child(bs, null_bs, "null-child", &child_of_bds, |
1090 | BDRV_CHILD_DATA, &error_abort); | |
afdaeb9e | 1091 | bdrv_graph_wrunlock(); |
4c8158e3 HR |
1092 | |
1093 | /* This child will be the one to pass to requests through to, and | |
1094 | * it will stall until a drain occurs */ | |
1095 | child_bs = bdrv_new_open_driver(&bdrv_test, "child", BDRV_O_RDWR, | |
1096 | &error_abort); | |
1097 | child_bs->total_sectors = 65536 >> BDRV_SECTOR_BITS; | |
1098 | /* Takes our reference to child_bs */ | |
afdaeb9e | 1099 | bdrv_graph_wrlock(NULL); |
a16be3cd HR |
1100 | tts->wait_child = bdrv_attach_child(bs, child_bs, "wait-child", |
1101 | &child_of_bds, | |
1102 | BDRV_CHILD_DATA | BDRV_CHILD_PRIMARY, | |
1103 | &error_abort); | |
afdaeb9e | 1104 | bdrv_graph_wrunlock(); |
4c8158e3 HR |
1105 | |
1106 | /* This child is just there to be deleted | |
1107 | * (for detach_instead_of_delete == true) */ | |
1108 | null_bs = bdrv_open("null-co://", NULL, NULL, BDRV_O_RDWR | BDRV_O_PROTOCOL, | |
1109 | &error_abort); | |
afdaeb9e | 1110 | bdrv_graph_wrlock(NULL); |
a16be3cd HR |
1111 | bdrv_attach_child(bs, null_bs, "null-child", &child_of_bds, BDRV_CHILD_DATA, |
1112 | &error_abort); | |
afdaeb9e | 1113 | bdrv_graph_wrunlock(); |
4c8158e3 | 1114 | |
d861ab3a | 1115 | blk = blk_new(qemu_get_aio_context(), BLK_PERM_ALL, BLK_PERM_ALL); |
4c8158e3 HR |
1116 | blk_insert_bs(blk, bs, &error_abort); |
1117 | ||
1118 | /* Referenced by blk now */ | |
1119 | bdrv_unref(bs); | |
1120 | ||
1121 | g_assert_cmpint(bs->refcnt, ==, 1); | |
1122 | g_assert_cmpint(child_bs->refcnt, ==, 1); | |
1123 | g_assert_cmpint(null_bs->refcnt, ==, 1); | |
1124 | ||
1125 | ||
1126 | dbdd = (TestCoDeleteByDrainData){ | |
1127 | .blk = blk, | |
1128 | .detach_instead_of_delete = detach_instead_of_delete, | |
1129 | .done = false, | |
1130 | }; | |
1131 | co = qemu_coroutine_create(test_co_delete_by_drain, &dbdd); | |
1132 | qemu_coroutine_enter(co); | |
1133 | ||
1134 | /* Drain the child while the read operation is still pending. | |
1135 | * This should result in the operation finishing and | |
1136 | * test_co_delete_by_drain() resuming. Thus, @bs will be deleted | |
1137 | * and the coroutine will exit while this drain operation is still | |
1138 | * in progress. */ | |
ebd31837 KW |
1139 | switch (drain_type) { |
1140 | case BDRV_DRAIN: | |
1141 | bdrv_ref(child_bs); | |
1142 | bdrv_drain(child_bs); | |
1143 | bdrv_unref(child_bs); | |
1144 | break; | |
19f7a7e5 KW |
1145 | case BDRV_DRAIN_ALL: |
1146 | bdrv_drain_all_begin(); | |
1147 | bdrv_drain_all_end(); | |
1148 | break; | |
ebd31837 KW |
1149 | default: |
1150 | g_assert_not_reached(); | |
1151 | } | |
4c8158e3 HR |
1152 | |
1153 | while (!dbdd.done) { | |
1154 | aio_poll(qemu_get_aio_context(), true); | |
1155 | } | |
1156 | ||
1157 | if (detach_instead_of_delete) { | |
1158 | /* Here, the reference has not passed over to the coroutine, | |
1159 | * so we have to delete the BB ourselves */ | |
1160 | blk_unref(blk); | |
1161 | } | |
1162 | } | |
1163 | ||
4c8158e3 HR |
1164 | static void test_delete_by_drain(void) |
1165 | { | |
ebd31837 | 1166 | do_test_delete_by_drain(false, BDRV_DRAIN); |
4c8158e3 HR |
1167 | } |
1168 | ||
19f7a7e5 KW |
1169 | static void test_detach_by_drain_all(void) |
1170 | { | |
1171 | do_test_delete_by_drain(true, BDRV_DRAIN_ALL); | |
1172 | } | |
1173 | ||
4c8158e3 HR |
1174 | static void test_detach_by_drain(void) |
1175 | { | |
ebd31837 KW |
1176 | do_test_delete_by_drain(true, BDRV_DRAIN); |
1177 | } | |
1178 | ||
4c8158e3 | 1179 | |
231281ab KW |
1180 | struct detach_by_parent_data { |
1181 | BlockDriverState *parent_b; | |
1182 | BdrvChild *child_b; | |
1183 | BlockDriverState *c; | |
1184 | BdrvChild *child_c; | |
57320ca9 | 1185 | bool by_parent_cb; |
617f3a96 | 1186 | bool detach_on_drain; |
231281ab | 1187 | }; |
57320ca9 | 1188 | static struct detach_by_parent_data detach_by_parent_data; |
231281ab | 1189 | |
903df115 | 1190 | static void no_coroutine_fn detach_indirect_bh(void *opaque) |
231281ab KW |
1191 | { |
1192 | struct detach_by_parent_data *data = opaque; | |
1193 | ||
617f3a96 | 1194 | bdrv_dec_in_flight(data->child_b->bs); |
32a8aba3 KW |
1195 | |
1196 | bdrv_graph_wrlock(NULL); | |
231281ab KW |
1197 | bdrv_unref_child(data->parent_b, data->child_b); |
1198 | ||
1199 | bdrv_ref(data->c); | |
1200 | data->child_c = bdrv_attach_child(data->parent_b, data->c, "PB-C", | |
a16be3cd HR |
1201 | &child_of_bds, BDRV_CHILD_DATA, |
1202 | &error_abort); | |
afdaeb9e | 1203 | bdrv_graph_wrunlock(); |
231281ab KW |
1204 | } |
1205 | ||
903df115 | 1206 | static void coroutine_mixed_fn detach_by_parent_aio_cb(void *opaque, int ret) |
57320ca9 KW |
1207 | { |
1208 | struct detach_by_parent_data *data = &detach_by_parent_data; | |
1209 | ||
1210 | g_assert_cmpint(ret, ==, 0); | |
1211 | if (data->by_parent_cb) { | |
617f3a96 | 1212 | bdrv_inc_in_flight(data->child_b->bs); |
903df115 KW |
1213 | aio_bh_schedule_oneshot(qemu_get_current_aio_context(), |
1214 | detach_indirect_bh, &detach_by_parent_data); | |
57320ca9 KW |
1215 | } |
1216 | } | |
1217 | ||
d05ab380 | 1218 | static void GRAPH_RDLOCK detach_by_driver_cb_drained_begin(BdrvChild *child) |
57320ca9 | 1219 | { |
617f3a96 KW |
1220 | struct detach_by_parent_data *data = &detach_by_parent_data; |
1221 | ||
1222 | if (!data->detach_on_drain) { | |
1223 | return; | |
1224 | } | |
1225 | data->detach_on_drain = false; | |
1226 | ||
1227 | bdrv_inc_in_flight(data->child_b->bs); | |
57320ca9 KW |
1228 | aio_bh_schedule_oneshot(qemu_get_current_aio_context(), |
1229 | detach_indirect_bh, &detach_by_parent_data); | |
a16be3cd | 1230 | child_of_bds.drained_begin(child); |
57320ca9 KW |
1231 | } |
1232 | ||
bd86fb99 | 1233 | static BdrvChildClass detach_by_driver_cb_class; |
57320ca9 | 1234 | |
231281ab KW |
1235 | /* |
1236 | * Initial graph: | |
1237 | * | |
1238 | * PA PB | |
1239 | * \ / \ | |
1240 | * A B C | |
1241 | * | |
57320ca9 KW |
1242 | * by_parent_cb == true: Test that parent callbacks don't poll |
1243 | * | |
1244 | * PA has a pending write request whose callback changes the child nodes of | |
1245 | * PB: It removes B and adds C instead. The subtree of PB is drained, which | |
1246 | * will indirectly drain the write request, too. | |
1247 | * | |
1248 | * by_parent_cb == false: Test that bdrv_drain_invoke() doesn't poll | |
1249 | * | |
bd86fb99 | 1250 | * PA's BdrvChildClass has a .drained_begin callback that schedules a BH |
57320ca9 KW |
1251 | * that does the same graph change. If bdrv_drain_invoke() calls it, the |
1252 | * state is messed up, but if it is only polled in the single | |
1253 | * BDRV_POLL_WHILE() at the end of the drain, this should work fine. | |
231281ab | 1254 | */ |
d05ab380 | 1255 | static void TSA_NO_TSA test_detach_indirect(bool by_parent_cb) |
231281ab KW |
1256 | { |
1257 | BlockBackend *blk; | |
1258 | BlockDriverState *parent_a, *parent_b, *a, *b, *c; | |
1259 | BdrvChild *child_a, *child_b; | |
1260 | BlockAIOCB *acb; | |
231281ab | 1261 | |
405d8fe0 | 1262 | QEMUIOVector qiov = QEMU_IOVEC_INIT_BUF(qiov, NULL, 0); |
231281ab | 1263 | |
57320ca9 | 1264 | if (!by_parent_cb) { |
a16be3cd | 1265 | detach_by_driver_cb_class = child_of_bds; |
bd86fb99 | 1266 | detach_by_driver_cb_class.drained_begin = |
57320ca9 | 1267 | detach_by_driver_cb_drained_begin; |
617f3a96 KW |
1268 | detach_by_driver_cb_class.drained_end = NULL; |
1269 | detach_by_driver_cb_class.drained_poll = NULL; | |
57320ca9 KW |
1270 | } |
1271 | ||
617f3a96 KW |
1272 | detach_by_parent_data = (struct detach_by_parent_data) { |
1273 | .detach_on_drain = false, | |
1274 | }; | |
1275 | ||
231281ab KW |
1276 | /* Create all involved nodes */ |
1277 | parent_a = bdrv_new_open_driver(&bdrv_test, "parent-a", BDRV_O_RDWR, | |
1278 | &error_abort); | |
1279 | parent_b = bdrv_new_open_driver(&bdrv_test, "parent-b", 0, | |
1280 | &error_abort); | |
1281 | ||
1282 | a = bdrv_new_open_driver(&bdrv_test, "a", BDRV_O_RDWR, &error_abort); | |
1283 | b = bdrv_new_open_driver(&bdrv_test, "b", BDRV_O_RDWR, &error_abort); | |
1284 | c = bdrv_new_open_driver(&bdrv_test, "c", BDRV_O_RDWR, &error_abort); | |
1285 | ||
1286 | /* blk is a BB for parent-a */ | |
d861ab3a | 1287 | blk = blk_new(qemu_get_aio_context(), BLK_PERM_ALL, BLK_PERM_ALL); |
231281ab KW |
1288 | blk_insert_bs(blk, parent_a, &error_abort); |
1289 | bdrv_unref(parent_a); | |
1290 | ||
57320ca9 KW |
1291 | /* If we want to get bdrv_drain_invoke() to call aio_poll(), the driver |
1292 | * callback must not return immediately. */ | |
1293 | if (!by_parent_cb) { | |
1294 | BDRVTestState *s = parent_a->opaque; | |
1295 | s->sleep_in_drain_begin = true; | |
1296 | } | |
1297 | ||
231281ab KW |
1298 | /* Set child relationships */ |
1299 | bdrv_ref(b); | |
1300 | bdrv_ref(a); | |
afdaeb9e | 1301 | bdrv_graph_wrlock(NULL); |
a16be3cd HR |
1302 | child_b = bdrv_attach_child(parent_b, b, "PB-B", &child_of_bds, |
1303 | BDRV_CHILD_DATA, &error_abort); | |
25191e5f HR |
1304 | child_a = bdrv_attach_child(parent_b, a, "PB-A", &child_of_bds, |
1305 | BDRV_CHILD_COW, &error_abort); | |
231281ab KW |
1306 | |
1307 | bdrv_ref(a); | |
57320ca9 | 1308 | bdrv_attach_child(parent_a, a, "PA-A", |
a16be3cd HR |
1309 | by_parent_cb ? &child_of_bds : &detach_by_driver_cb_class, |
1310 | BDRV_CHILD_DATA, &error_abort); | |
afdaeb9e | 1311 | bdrv_graph_wrunlock(); |
231281ab KW |
1312 | |
1313 | g_assert_cmpint(parent_a->refcnt, ==, 1); | |
1314 | g_assert_cmpint(parent_b->refcnt, ==, 1); | |
1315 | g_assert_cmpint(a->refcnt, ==, 3); | |
1316 | g_assert_cmpint(b->refcnt, ==, 2); | |
1317 | g_assert_cmpint(c->refcnt, ==, 1); | |
1318 | ||
1319 | g_assert(QLIST_FIRST(&parent_b->children) == child_a); | |
1320 | g_assert(QLIST_NEXT(child_a, next) == child_b); | |
1321 | g_assert(QLIST_NEXT(child_b, next) == NULL); | |
1322 | ||
1323 | /* Start the evil write request */ | |
57320ca9 | 1324 | detach_by_parent_data = (struct detach_by_parent_data) { |
231281ab KW |
1325 | .parent_b = parent_b, |
1326 | .child_b = child_b, | |
1327 | .c = c, | |
57320ca9 | 1328 | .by_parent_cb = by_parent_cb, |
617f3a96 | 1329 | .detach_on_drain = true, |
231281ab | 1330 | }; |
57320ca9 | 1331 | acb = blk_aio_preadv(blk, 0, &qiov, 0, detach_by_parent_aio_cb, NULL); |
231281ab KW |
1332 | g_assert(acb != NULL); |
1333 | ||
1334 | /* Drain and check the expected result */ | |
299403ae KW |
1335 | bdrv_drained_begin(parent_b); |
1336 | bdrv_drained_begin(a); | |
1337 | bdrv_drained_begin(b); | |
1338 | bdrv_drained_begin(c); | |
231281ab | 1339 | |
57320ca9 | 1340 | g_assert(detach_by_parent_data.child_c != NULL); |
231281ab KW |
1341 | |
1342 | g_assert_cmpint(parent_a->refcnt, ==, 1); | |
1343 | g_assert_cmpint(parent_b->refcnt, ==, 1); | |
1344 | g_assert_cmpint(a->refcnt, ==, 3); | |
1345 | g_assert_cmpint(b->refcnt, ==, 1); | |
1346 | g_assert_cmpint(c->refcnt, ==, 2); | |
1347 | ||
57320ca9 KW |
1348 | g_assert(QLIST_FIRST(&parent_b->children) == detach_by_parent_data.child_c); |
1349 | g_assert(QLIST_NEXT(detach_by_parent_data.child_c, next) == child_a); | |
231281ab KW |
1350 | g_assert(QLIST_NEXT(child_a, next) == NULL); |
1351 | ||
1352 | g_assert_cmpint(parent_a->quiesce_counter, ==, 1); | |
299403ae | 1353 | g_assert_cmpint(parent_b->quiesce_counter, ==, 3); |
231281ab | 1354 | g_assert_cmpint(a->quiesce_counter, ==, 1); |
299403ae | 1355 | g_assert_cmpint(b->quiesce_counter, ==, 1); |
231281ab KW |
1356 | g_assert_cmpint(c->quiesce_counter, ==, 1); |
1357 | ||
299403ae KW |
1358 | bdrv_drained_end(parent_b); |
1359 | bdrv_drained_end(a); | |
1360 | bdrv_drained_end(b); | |
1361 | bdrv_drained_end(c); | |
231281ab KW |
1362 | |
1363 | bdrv_unref(parent_b); | |
1364 | blk_unref(blk); | |
1365 | ||
231281ab KW |
1366 | g_assert_cmpint(a->refcnt, ==, 1); |
1367 | g_assert_cmpint(b->refcnt, ==, 1); | |
1368 | g_assert_cmpint(c->refcnt, ==, 1); | |
1369 | bdrv_unref(a); | |
1370 | bdrv_unref(b); | |
1371 | bdrv_unref(c); | |
1372 | } | |
1373 | ||
57320ca9 KW |
1374 | static void test_detach_by_parent_cb(void) |
1375 | { | |
1376 | test_detach_indirect(true); | |
1377 | } | |
1378 | ||
1379 | static void test_detach_by_driver_cb(void) | |
1380 | { | |
1381 | test_detach_indirect(false); | |
1382 | } | |
231281ab | 1383 | |
b994c5bc KW |
1384 | static void test_append_to_drained(void) |
1385 | { | |
1386 | BlockBackend *blk; | |
1387 | BlockDriverState *base, *overlay; | |
1388 | BDRVTestState *base_s, *overlay_s; | |
1389 | ||
d861ab3a | 1390 | blk = blk_new(qemu_get_aio_context(), BLK_PERM_ALL, BLK_PERM_ALL); |
b994c5bc KW |
1391 | base = bdrv_new_open_driver(&bdrv_test, "base", BDRV_O_RDWR, &error_abort); |
1392 | base_s = base->opaque; | |
1393 | blk_insert_bs(blk, base, &error_abort); | |
1394 | ||
1395 | overlay = bdrv_new_open_driver(&bdrv_test, "overlay", BDRV_O_RDWR, | |
1396 | &error_abort); | |
1397 | overlay_s = overlay->opaque; | |
1398 | ||
1399 | do_drain_begin(BDRV_DRAIN, base); | |
1400 | g_assert_cmpint(base->quiesce_counter, ==, 1); | |
1401 | g_assert_cmpint(base_s->drain_count, ==, 1); | |
1402 | g_assert_cmpint(base->in_flight, ==, 0); | |
1403 | ||
487b9187 | 1404 | aio_context_acquire(qemu_get_aio_context()); |
b994c5bc | 1405 | bdrv_append(overlay, base, &error_abort); |
487b9187 KW |
1406 | aio_context_release(qemu_get_aio_context()); |
1407 | ||
b994c5bc KW |
1408 | g_assert_cmpint(base->in_flight, ==, 0); |
1409 | g_assert_cmpint(overlay->in_flight, ==, 0); | |
1410 | ||
1411 | g_assert_cmpint(base->quiesce_counter, ==, 1); | |
1412 | g_assert_cmpint(base_s->drain_count, ==, 1); | |
1413 | g_assert_cmpint(overlay->quiesce_counter, ==, 1); | |
1414 | g_assert_cmpint(overlay_s->drain_count, ==, 1); | |
1415 | ||
1416 | do_drain_end(BDRV_DRAIN, base); | |
1417 | ||
1418 | g_assert_cmpint(base->quiesce_counter, ==, 0); | |
1419 | g_assert_cmpint(base_s->drain_count, ==, 0); | |
1420 | g_assert_cmpint(overlay->quiesce_counter, ==, 0); | |
1421 | g_assert_cmpint(overlay_s->drain_count, ==, 0); | |
1422 | ||
ae9d4417 | 1423 | bdrv_unref(overlay); |
b994c5bc KW |
1424 | bdrv_unref(base); |
1425 | blk_unref(blk); | |
1426 | } | |
1427 | ||
247d2737 KW |
1428 | static void test_set_aio_context(void) |
1429 | { | |
1430 | BlockDriverState *bs; | |
1431 | IOThread *a = iothread_new(); | |
1432 | IOThread *b = iothread_new(); | |
1433 | AioContext *ctx_a = iothread_get_aio_context(a); | |
1434 | AioContext *ctx_b = iothread_get_aio_context(b); | |
1435 | ||
1436 | bs = bdrv_new_open_driver(&bdrv_test, "test-node", BDRV_O_RDWR, | |
1437 | &error_abort); | |
1438 | ||
1439 | bdrv_drained_begin(bs); | |
142e6907 | 1440 | bdrv_try_change_aio_context(bs, ctx_a, NULL, &error_abort); |
247d2737 KW |
1441 | |
1442 | aio_context_acquire(ctx_a); | |
1443 | bdrv_drained_end(bs); | |
1444 | ||
1445 | bdrv_drained_begin(bs); | |
142e6907 | 1446 | bdrv_try_change_aio_context(bs, ctx_b, NULL, &error_abort); |
247d2737 KW |
1447 | aio_context_release(ctx_a); |
1448 | aio_context_acquire(ctx_b); | |
142e6907 | 1449 | bdrv_try_change_aio_context(bs, qemu_get_aio_context(), NULL, &error_abort); |
247d2737 KW |
1450 | aio_context_release(ctx_b); |
1451 | bdrv_drained_end(bs); | |
1452 | ||
1453 | bdrv_unref(bs); | |
1454 | iothread_join(a); | |
1455 | iothread_join(b); | |
1456 | } | |
1457 | ||
8e442810 HR |
1458 | |
1459 | typedef struct TestDropBackingBlockJob { | |
1460 | BlockJob common; | |
1461 | bool should_complete; | |
1462 | bool *did_complete; | |
2afdc790 | 1463 | BlockDriverState *detach_also; |
1b177bbe | 1464 | BlockDriverState *bs; |
8e442810 HR |
1465 | } TestDropBackingBlockJob; |
1466 | ||
1467 | static int coroutine_fn test_drop_backing_job_run(Job *job, Error **errp) | |
1468 | { | |
1469 | TestDropBackingBlockJob *s = | |
1470 | container_of(job, TestDropBackingBlockJob, common.job); | |
1471 | ||
1472 | while (!s->should_complete) { | |
1473 | job_sleep_ns(job, 0); | |
1474 | } | |
1475 | ||
1476 | return 0; | |
1477 | } | |
1478 | ||
1479 | static void test_drop_backing_job_commit(Job *job) | |
1480 | { | |
1481 | TestDropBackingBlockJob *s = | |
1482 | container_of(job, TestDropBackingBlockJob, common.job); | |
1483 | ||
1b177bbe | 1484 | bdrv_set_backing_hd(s->bs, NULL, &error_abort); |
2afdc790 | 1485 | bdrv_set_backing_hd(s->detach_also, NULL, &error_abort); |
8e442810 HR |
1486 | |
1487 | *s->did_complete = true; | |
1488 | } | |
1489 | ||
1490 | static const BlockJobDriver test_drop_backing_job_driver = { | |
1491 | .job_driver = { | |
1492 | .instance_size = sizeof(TestDropBackingBlockJob), | |
1493 | .free = block_job_free, | |
1494 | .user_resume = block_job_user_resume, | |
8e442810 HR |
1495 | .run = test_drop_backing_job_run, |
1496 | .commit = test_drop_backing_job_commit, | |
1497 | } | |
1498 | }; | |
1499 | ||
1500 | /** | |
1501 | * Creates a child node with three parent nodes on it, and then runs a | |
1502 | * block job on the final one, parent-node-2. | |
1503 | * | |
8e442810 HR |
1504 | * The job is then asked to complete before a section where the child |
1505 | * is drained. | |
1506 | * | |
1507 | * Ending this section will undrain the child's parents, first | |
1508 | * parent-node-2, then parent-node-1, then parent-node-0 -- the parent | |
1509 | * list is in reverse order of how they were added. Ending the drain | |
1510 | * on parent-node-2 will resume the job, thus completing it and | |
1511 | * scheduling job_exit(). | |
1512 | * | |
1513 | * Ending the drain on parent-node-1 will poll the AioContext, which | |
1514 | * lets job_exit() and thus test_drop_backing_job_commit() run. That | |
2afdc790 | 1515 | * function first removes the child as parent-node-2's backing file. |
8e442810 HR |
1516 | * |
1517 | * In old (and buggy) implementations, there are two problems with | |
1518 | * that: | |
1519 | * (A) bdrv_drain_invoke() polls for every node that leaves the | |
1520 | * drained section. This means that job_exit() is scheduled | |
1521 | * before the child has left the drained section. Its | |
1522 | * quiesce_counter is therefore still 1 when it is removed from | |
1523 | * parent-node-2. | |
1524 | * | |
1525 | * (B) bdrv_replace_child_noperm() calls drained_end() on the old | |
1526 | * child's parents as many times as the child is quiesced. This | |
1527 | * means it will call drained_end() on parent-node-2 once. | |
1528 | * Because parent-node-2 is no longer quiesced at this point, this | |
1529 | * will fail. | |
1530 | * | |
1531 | * bdrv_replace_child_noperm() therefore must call drained_end() on | |
1532 | * the parent only if it really is still drained because the child is | |
1533 | * drained. | |
2afdc790 HR |
1534 | * |
1535 | * If removing child from parent-node-2 was successful (as it should | |
1536 | * be), test_drop_backing_job_commit() will then also remove the child | |
1537 | * from parent-node-0. | |
1538 | * | |
1539 | * With an old version of our drain infrastructure ((A) above), that | |
1540 | * resulted in the following flow: | |
1541 | * | |
1542 | * 1. child attempts to leave its drained section. The call recurses | |
1543 | * to its parents. | |
1544 | * | |
1545 | * 2. parent-node-2 leaves the drained section. Polling in | |
1546 | * bdrv_drain_invoke() will schedule job_exit(). | |
1547 | * | |
1548 | * 3. parent-node-1 leaves the drained section. Polling in | |
1549 | * bdrv_drain_invoke() will run job_exit(), thus disconnecting | |
1550 | * parent-node-0 from the child node. | |
1551 | * | |
1552 | * 4. bdrv_parent_drained_end() uses a QLIST_FOREACH_SAFE() loop to | |
1553 | * iterate over the parents. Thus, it now accesses the BdrvChild | |
1554 | * object that used to connect parent-node-0 and the child node. | |
1555 | * However, that object no longer exists, so it accesses a dangling | |
1556 | * pointer. | |
1557 | * | |
1558 | * The solution is to only poll once when running a bdrv_drained_end() | |
1559 | * operation, specifically at the end when all drained_end() | |
1560 | * operations for all involved nodes have been scheduled. | |
1561 | * Note that this also solves (A) above, thus hiding (B). | |
8e442810 HR |
1562 | */ |
1563 | static void test_blockjob_commit_by_drained_end(void) | |
1564 | { | |
1565 | BlockDriverState *bs_child, *bs_parents[3]; | |
1566 | TestDropBackingBlockJob *job; | |
1567 | bool job_has_completed = false; | |
1568 | int i; | |
1569 | ||
1570 | bs_child = bdrv_new_open_driver(&bdrv_test, "child-node", BDRV_O_RDWR, | |
1571 | &error_abort); | |
1572 | ||
1573 | for (i = 0; i < 3; i++) { | |
1574 | char name[32]; | |
1575 | snprintf(name, sizeof(name), "parent-node-%i", i); | |
1576 | bs_parents[i] = bdrv_new_open_driver(&bdrv_test, name, BDRV_O_RDWR, | |
1577 | &error_abort); | |
1578 | bdrv_set_backing_hd(bs_parents[i], bs_child, &error_abort); | |
1579 | } | |
1580 | ||
1581 | job = block_job_create("job", &test_drop_backing_job_driver, NULL, | |
1582 | bs_parents[2], 0, BLK_PERM_ALL, 0, 0, NULL, NULL, | |
1583 | &error_abort); | |
1b177bbe | 1584 | job->bs = bs_parents[2]; |
8e442810 | 1585 | |
2afdc790 | 1586 | job->detach_also = bs_parents[0]; |
8e442810 HR |
1587 | job->did_complete = &job_has_completed; |
1588 | ||
1589 | job_start(&job->common.job); | |
1590 | ||
1591 | job->should_complete = true; | |
1592 | bdrv_drained_begin(bs_child); | |
1593 | g_assert(!job_has_completed); | |
1594 | bdrv_drained_end(bs_child); | |
5e8ac217 | 1595 | aio_poll(qemu_get_aio_context(), false); |
8e442810 HR |
1596 | g_assert(job_has_completed); |
1597 | ||
1598 | bdrv_unref(bs_parents[0]); | |
1599 | bdrv_unref(bs_parents[1]); | |
1600 | bdrv_unref(bs_parents[2]); | |
1601 | bdrv_unref(bs_child); | |
1602 | } | |
1603 | ||
9746b35c HR |
1604 | |
1605 | typedef struct TestSimpleBlockJob { | |
1606 | BlockJob common; | |
1607 | bool should_complete; | |
1608 | bool *did_complete; | |
1609 | } TestSimpleBlockJob; | |
1610 | ||
1611 | static int coroutine_fn test_simple_job_run(Job *job, Error **errp) | |
1612 | { | |
1613 | TestSimpleBlockJob *s = container_of(job, TestSimpleBlockJob, common.job); | |
1614 | ||
1615 | while (!s->should_complete) { | |
1616 | job_sleep_ns(job, 0); | |
1617 | } | |
1618 | ||
1619 | return 0; | |
1620 | } | |
1621 | ||
1622 | static void test_simple_job_clean(Job *job) | |
1623 | { | |
1624 | TestSimpleBlockJob *s = container_of(job, TestSimpleBlockJob, common.job); | |
1625 | *s->did_complete = true; | |
1626 | } | |
1627 | ||
1628 | static const BlockJobDriver test_simple_job_driver = { | |
1629 | .job_driver = { | |
1630 | .instance_size = sizeof(TestSimpleBlockJob), | |
1631 | .free = block_job_free, | |
1632 | .user_resume = block_job_user_resume, | |
9746b35c HR |
1633 | .run = test_simple_job_run, |
1634 | .clean = test_simple_job_clean, | |
1635 | }, | |
1636 | }; | |
1637 | ||
1638 | static int drop_intermediate_poll_update_filename(BdrvChild *child, | |
1639 | BlockDriverState *new_base, | |
1640 | const char *filename, | |
1641 | Error **errp) | |
1642 | { | |
1643 | /* | |
1644 | * We are free to poll here, which may change the block graph, if | |
1645 | * it is not drained. | |
1646 | */ | |
1647 | ||
1648 | /* If the job is not drained: Complete it, schedule job_exit() */ | |
1649 | aio_poll(qemu_get_current_aio_context(), false); | |
1650 | /* If the job is not drained: Run job_exit(), finish the job */ | |
1651 | aio_poll(qemu_get_current_aio_context(), false); | |
1652 | ||
1653 | return 0; | |
1654 | } | |
1655 | ||
1656 | /** | |
1657 | * Test a poll in the midst of bdrv_drop_intermediate(). | |
1658 | * | |
bd86fb99 | 1659 | * bdrv_drop_intermediate() calls BdrvChildClass.update_filename(), |
9746b35c HR |
1660 | * which can yield or poll. This may lead to graph changes, unless |
1661 | * the whole subtree in question is drained. | |
1662 | * | |
1663 | * We test this on the following graph: | |
1664 | * | |
1665 | * Job | |
1666 | * | |
1667 | * | | |
1668 | * job-node | |
1669 | * | | |
1670 | * v | |
1671 | * | |
1672 | * job-node | |
1673 | * | |
1674 | * | | |
1675 | * backing | |
1676 | * | | |
1677 | * v | |
1678 | * | |
1679 | * node-2 --chain--> node-1 --chain--> node-0 | |
1680 | * | |
1681 | * We drop node-1 with bdrv_drop_intermediate(top=node-1, base=node-0). | |
1682 | * | |
1683 | * This first updates node-2's backing filename by invoking | |
1684 | * drop_intermediate_poll_update_filename(), which polls twice. This | |
1685 | * causes the job to finish, which in turns causes the job-node to be | |
1686 | * deleted. | |
1687 | * | |
1688 | * bdrv_drop_intermediate() uses a QLIST_FOREACH_SAFE() loop, so it | |
1689 | * already has a pointer to the BdrvChild edge between job-node and | |
1690 | * node-1. When it tries to handle that edge, we probably get a | |
1691 | * segmentation fault because the object no longer exists. | |
1692 | * | |
1693 | * | |
1694 | * The solution is for bdrv_drop_intermediate() to drain top's | |
1695 | * subtree. This prevents graph changes from happening just because | |
bd86fb99 | 1696 | * BdrvChildClass.update_filename() yields or polls. Thus, the block |
9746b35c HR |
1697 | * job is paused during that drained section and must finish before or |
1698 | * after. | |
1699 | * | |
1700 | * (In addition, bdrv_replace_child() must keep the job paused.) | |
1701 | */ | |
1702 | static void test_drop_intermediate_poll(void) | |
1703 | { | |
bd86fb99 | 1704 | static BdrvChildClass chain_child_class; |
9746b35c HR |
1705 | BlockDriverState *chain[3]; |
1706 | TestSimpleBlockJob *job; | |
1707 | BlockDriverState *job_node; | |
1708 | bool job_has_completed = false; | |
1709 | int i; | |
1710 | int ret; | |
1711 | ||
25191e5f | 1712 | chain_child_class = child_of_bds; |
bd86fb99 | 1713 | chain_child_class.update_filename = drop_intermediate_poll_update_filename; |
9746b35c HR |
1714 | |
1715 | for (i = 0; i < 3; i++) { | |
1716 | char name[32]; | |
1717 | snprintf(name, 32, "node-%i", i); | |
1718 | ||
1719 | chain[i] = bdrv_new_open_driver(&bdrv_test, name, 0, &error_abort); | |
1720 | } | |
1721 | ||
1722 | job_node = bdrv_new_open_driver(&bdrv_test, "job-node", BDRV_O_RDWR, | |
1723 | &error_abort); | |
1724 | bdrv_set_backing_hd(job_node, chain[1], &error_abort); | |
1725 | ||
1726 | /* | |
1727 | * Establish the chain last, so the chain links are the first | |
1728 | * elements in the BDS.parents lists | |
1729 | */ | |
afdaeb9e | 1730 | bdrv_graph_wrlock(NULL); |
9746b35c HR |
1731 | for (i = 0; i < 3; i++) { |
1732 | if (i) { | |
1733 | /* Takes the reference to chain[i - 1] */ | |
5bb04747 VSO |
1734 | bdrv_attach_child(chain[i], chain[i - 1], "chain", |
1735 | &chain_child_class, BDRV_CHILD_COW, &error_abort); | |
9746b35c HR |
1736 | } |
1737 | } | |
afdaeb9e | 1738 | bdrv_graph_wrunlock(); |
9746b35c HR |
1739 | |
1740 | job = block_job_create("job", &test_simple_job_driver, NULL, job_node, | |
1741 | 0, BLK_PERM_ALL, 0, 0, NULL, NULL, &error_abort); | |
1742 | ||
1743 | /* The job has a reference now */ | |
1744 | bdrv_unref(job_node); | |
1745 | ||
1746 | job->did_complete = &job_has_completed; | |
1747 | ||
1748 | job_start(&job->common.job); | |
1749 | job->should_complete = true; | |
1750 | ||
1751 | g_assert(!job_has_completed); | |
1752 | ret = bdrv_drop_intermediate(chain[1], chain[0], NULL); | |
5e8ac217 | 1753 | aio_poll(qemu_get_aio_context(), false); |
9746b35c HR |
1754 | g_assert(ret == 0); |
1755 | g_assert(job_has_completed); | |
1756 | ||
1757 | bdrv_unref(chain[2]); | |
1758 | } | |
1759 | ||
0513f984 HR |
1760 | |
1761 | typedef struct BDRVReplaceTestState { | |
23987471 | 1762 | bool setup_completed; |
0513f984 HR |
1763 | bool was_drained; |
1764 | bool was_undrained; | |
1765 | bool has_read; | |
1766 | ||
1767 | int drain_count; | |
1768 | ||
1769 | bool yield_before_read; | |
1770 | Coroutine *io_co; | |
1771 | Coroutine *drain_co; | |
1772 | } BDRVReplaceTestState; | |
1773 | ||
1774 | static void bdrv_replace_test_close(BlockDriverState *bs) | |
1775 | { | |
1776 | } | |
1777 | ||
1778 | /** | |
1779 | * If @bs has a backing file: | |
1780 | * Yield if .yield_before_read is true (and wait for drain_begin to | |
1781 | * wake us up). | |
1782 | * Forward the read to bs->backing. Set .has_read to true. | |
1783 | * If drain_begin has woken us, wake it in turn. | |
1784 | * | |
1785 | * Otherwise: | |
1786 | * Set .has_read to true and return success. | |
1787 | */ | |
b9b10c35 KW |
1788 | static int coroutine_fn GRAPH_RDLOCK |
1789 | bdrv_replace_test_co_preadv(BlockDriverState *bs, int64_t offset, int64_t bytes, | |
1790 | QEMUIOVector *qiov, BdrvRequestFlags flags) | |
0513f984 HR |
1791 | { |
1792 | BDRVReplaceTestState *s = bs->opaque; | |
1793 | ||
1794 | if (bs->backing) { | |
1795 | int ret; | |
1796 | ||
1797 | g_assert(!s->drain_count); | |
1798 | ||
1799 | s->io_co = qemu_coroutine_self(); | |
1800 | if (s->yield_before_read) { | |
1801 | s->yield_before_read = false; | |
1802 | qemu_coroutine_yield(); | |
1803 | } | |
1804 | s->io_co = NULL; | |
1805 | ||
fae2681a | 1806 | ret = bdrv_co_preadv(bs->backing, offset, bytes, qiov, 0); |
0513f984 HR |
1807 | s->has_read = true; |
1808 | ||
1809 | /* Wake up drain_co if it runs */ | |
1810 | if (s->drain_co) { | |
1811 | aio_co_wake(s->drain_co); | |
1812 | } | |
1813 | ||
1814 | return ret; | |
1815 | } | |
1816 | ||
1817 | s->has_read = true; | |
1818 | return 0; | |
1819 | } | |
1820 | ||
7bce1c29 KW |
1821 | static void coroutine_fn bdrv_replace_test_drain_co(void *opaque) |
1822 | { | |
1823 | BlockDriverState *bs = opaque; | |
1824 | BDRVReplaceTestState *s = bs->opaque; | |
1825 | ||
1826 | /* Keep waking io_co up until it is done */ | |
1827 | while (s->io_co) { | |
1828 | aio_co_wake(s->io_co); | |
1829 | s->io_co = NULL; | |
1830 | qemu_coroutine_yield(); | |
1831 | } | |
1832 | s->drain_co = NULL; | |
1833 | bdrv_dec_in_flight(bs); | |
1834 | } | |
1835 | ||
0513f984 HR |
1836 | /** |
1837 | * If .drain_count is 0, wake up .io_co if there is one; and set | |
1838 | * .was_drained. | |
1839 | * Increment .drain_count. | |
1840 | */ | |
5e8ac217 | 1841 | static void bdrv_replace_test_drain_begin(BlockDriverState *bs) |
0513f984 HR |
1842 | { |
1843 | BDRVReplaceTestState *s = bs->opaque; | |
1844 | ||
23987471 KW |
1845 | if (!s->setup_completed) { |
1846 | return; | |
1847 | } | |
1848 | ||
0513f984 | 1849 | if (!s->drain_count) { |
7bce1c29 KW |
1850 | s->drain_co = qemu_coroutine_create(bdrv_replace_test_drain_co, bs); |
1851 | bdrv_inc_in_flight(bs); | |
1852 | aio_co_enter(bdrv_get_aio_context(bs), s->drain_co); | |
0513f984 HR |
1853 | s->was_drained = true; |
1854 | } | |
1855 | s->drain_count++; | |
1856 | } | |
1857 | ||
7bce1c29 KW |
1858 | static void coroutine_fn bdrv_replace_test_read_entry(void *opaque) |
1859 | { | |
1860 | BlockDriverState *bs = opaque; | |
1861 | char data; | |
1862 | QEMUIOVector qiov = QEMU_IOVEC_INIT_BUF(qiov, &data, 1); | |
1863 | int ret; | |
1864 | ||
1865 | /* Queue a read request post-drain */ | |
b9b10c35 | 1866 | bdrv_graph_co_rdlock(); |
7bce1c29 | 1867 | ret = bdrv_replace_test_co_preadv(bs, 0, 1, &qiov, 0); |
b9b10c35 KW |
1868 | bdrv_graph_co_rdunlock(); |
1869 | ||
7bce1c29 KW |
1870 | g_assert(ret >= 0); |
1871 | bdrv_dec_in_flight(bs); | |
1872 | } | |
1873 | ||
0513f984 HR |
1874 | /** |
1875 | * Reduce .drain_count, set .was_undrained once it reaches 0. | |
1876 | * If .drain_count reaches 0 and the node has a backing file, issue a | |
1877 | * read request. | |
1878 | */ | |
5e8ac217 | 1879 | static void bdrv_replace_test_drain_end(BlockDriverState *bs) |
0513f984 HR |
1880 | { |
1881 | BDRVReplaceTestState *s = bs->opaque; | |
1882 | ||
004915a9 KW |
1883 | GRAPH_RDLOCK_GUARD_MAINLOOP(); |
1884 | ||
23987471 KW |
1885 | if (!s->setup_completed) { |
1886 | return; | |
1887 | } | |
1888 | ||
0513f984 HR |
1889 | g_assert(s->drain_count > 0); |
1890 | if (!--s->drain_count) { | |
0513f984 HR |
1891 | s->was_undrained = true; |
1892 | ||
1893 | if (bs->backing) { | |
7bce1c29 KW |
1894 | Coroutine *co = qemu_coroutine_create(bdrv_replace_test_read_entry, |
1895 | bs); | |
1896 | bdrv_inc_in_flight(bs); | |
1897 | aio_co_enter(bdrv_get_aio_context(bs), co); | |
0513f984 HR |
1898 | } |
1899 | } | |
1900 | } | |
1901 | ||
1902 | static BlockDriver bdrv_replace_test = { | |
1903 | .format_name = "replace_test", | |
1904 | .instance_size = sizeof(BDRVReplaceTestState), | |
9ebfc111 | 1905 | .supports_backing = true, |
0513f984 HR |
1906 | |
1907 | .bdrv_close = bdrv_replace_test_close, | |
1908 | .bdrv_co_preadv = bdrv_replace_test_co_preadv, | |
1909 | ||
5e8ac217 KW |
1910 | .bdrv_drain_begin = bdrv_replace_test_drain_begin, |
1911 | .bdrv_drain_end = bdrv_replace_test_drain_end, | |
0513f984 | 1912 | |
69dca43d | 1913 | .bdrv_child_perm = bdrv_default_perms, |
0513f984 HR |
1914 | }; |
1915 | ||
1916 | static void coroutine_fn test_replace_child_mid_drain_read_co(void *opaque) | |
1917 | { | |
1918 | int ret; | |
1919 | char data; | |
1920 | ||
1921 | ret = blk_co_pread(opaque, 0, 1, &data, 0); | |
1922 | g_assert(ret >= 0); | |
1923 | } | |
1924 | ||
1925 | /** | |
1926 | * We test two things: | |
1927 | * (1) bdrv_replace_child_noperm() must not undrain the parent if both | |
1928 | * children are drained. | |
1929 | * (2) bdrv_replace_child_noperm() must never flush I/O requests to a | |
1930 | * drained child. If the old child is drained, it must flush I/O | |
1931 | * requests after the new one has been attached. If the new child | |
1932 | * is drained, it must flush I/O requests before the old one is | |
1933 | * detached. | |
1934 | * | |
1935 | * To do so, we create one parent node and two child nodes; then | |
1936 | * attach one of the children (old_child_bs) to the parent, then | |
1937 | * drain both old_child_bs and new_child_bs according to | |
1938 | * old_drain_count and new_drain_count, respectively, and finally | |
1939 | * we invoke bdrv_replace_node() to replace old_child_bs by | |
1940 | * new_child_bs. | |
1941 | * | |
1942 | * The test block driver we use here (bdrv_replace_test) has a read | |
1943 | * function that: | |
1944 | * - For the parent node, can optionally yield, and then forwards the | |
1945 | * read to bdrv_preadv(), | |
1946 | * - For the child node, just returns immediately. | |
1947 | * | |
1948 | * If the read yields, the drain_begin function will wake it up. | |
1949 | * | |
1950 | * The drain_end function issues a read on the parent once it is fully | |
1951 | * undrained (which simulates requests starting to come in again). | |
1952 | */ | |
1953 | static void do_test_replace_child_mid_drain(int old_drain_count, | |
1954 | int new_drain_count) | |
1955 | { | |
1956 | BlockBackend *parent_blk; | |
1957 | BlockDriverState *parent_bs; | |
1958 | BlockDriverState *old_child_bs, *new_child_bs; | |
1959 | BDRVReplaceTestState *parent_s; | |
1960 | BDRVReplaceTestState *old_child_s, *new_child_s; | |
1961 | Coroutine *io_co; | |
1962 | int i; | |
1963 | ||
1964 | parent_bs = bdrv_new_open_driver(&bdrv_replace_test, "parent", 0, | |
1965 | &error_abort); | |
1966 | parent_s = parent_bs->opaque; | |
1967 | ||
1968 | parent_blk = blk_new(qemu_get_aio_context(), | |
1969 | BLK_PERM_CONSISTENT_READ, BLK_PERM_ALL); | |
1970 | blk_insert_bs(parent_blk, parent_bs, &error_abort); | |
1971 | ||
1972 | old_child_bs = bdrv_new_open_driver(&bdrv_replace_test, "old-child", 0, | |
1973 | &error_abort); | |
1974 | new_child_bs = bdrv_new_open_driver(&bdrv_replace_test, "new-child", 0, | |
1975 | &error_abort); | |
1976 | old_child_s = old_child_bs->opaque; | |
1977 | new_child_s = new_child_bs->opaque; | |
1978 | ||
1979 | /* So that we can read something */ | |
1980 | parent_bs->total_sectors = 1; | |
1981 | old_child_bs->total_sectors = 1; | |
1982 | new_child_bs->total_sectors = 1; | |
1983 | ||
1984 | bdrv_ref(old_child_bs); | |
afdaeb9e | 1985 | bdrv_graph_wrlock(NULL); |
5bb04747 VSO |
1986 | bdrv_attach_child(parent_bs, old_child_bs, "child", &child_of_bds, |
1987 | BDRV_CHILD_COW, &error_abort); | |
afdaeb9e | 1988 | bdrv_graph_wrunlock(); |
23987471 | 1989 | parent_s->setup_completed = true; |
0513f984 HR |
1990 | |
1991 | for (i = 0; i < old_drain_count; i++) { | |
1992 | bdrv_drained_begin(old_child_bs); | |
1993 | } | |
1994 | for (i = 0; i < new_drain_count; i++) { | |
1995 | bdrv_drained_begin(new_child_bs); | |
1996 | } | |
1997 | ||
1998 | if (!old_drain_count) { | |
1999 | /* | |
2000 | * Start a read operation that will yield, so it will not | |
2001 | * complete before the node is drained. | |
2002 | */ | |
2003 | parent_s->yield_before_read = true; | |
2004 | io_co = qemu_coroutine_create(test_replace_child_mid_drain_read_co, | |
2005 | parent_blk); | |
2006 | qemu_coroutine_enter(io_co); | |
2007 | } | |
2008 | ||
2009 | /* If we have started a read operation, it should have yielded */ | |
2010 | g_assert(!parent_s->has_read); | |
2011 | ||
2012 | /* Reset drained status so we can see what bdrv_replace_node() does */ | |
2013 | parent_s->was_drained = false; | |
2014 | parent_s->was_undrained = false; | |
2015 | ||
2016 | g_assert(parent_bs->quiesce_counter == old_drain_count); | |
ccd6a379 KW |
2017 | bdrv_drained_begin(old_child_bs); |
2018 | bdrv_drained_begin(new_child_bs); | |
2019 | bdrv_graph_wrlock(NULL); | |
0513f984 | 2020 | bdrv_replace_node(old_child_bs, new_child_bs, &error_abort); |
ccd6a379 KW |
2021 | bdrv_graph_wrunlock(); |
2022 | bdrv_drained_end(new_child_bs); | |
2023 | bdrv_drained_end(old_child_bs); | |
0513f984 HR |
2024 | g_assert(parent_bs->quiesce_counter == new_drain_count); |
2025 | ||
2026 | if (!old_drain_count && !new_drain_count) { | |
2027 | /* | |
2028 | * From undrained to undrained drains and undrains the parent, | |
2029 | * because bdrv_replace_node() contains a drained section for | |
2030 | * @old_child_bs. | |
2031 | */ | |
2032 | g_assert(parent_s->was_drained && parent_s->was_undrained); | |
2033 | } else if (!old_drain_count && new_drain_count) { | |
2034 | /* | |
2035 | * From undrained to drained should drain the parent and keep | |
2036 | * it that way. | |
2037 | */ | |
2038 | g_assert(parent_s->was_drained && !parent_s->was_undrained); | |
2039 | } else if (old_drain_count && !new_drain_count) { | |
2040 | /* | |
2041 | * From drained to undrained should undrain the parent and | |
2042 | * keep it that way. | |
2043 | */ | |
2044 | g_assert(!parent_s->was_drained && parent_s->was_undrained); | |
2045 | } else /* if (old_drain_count && new_drain_count) */ { | |
2046 | /* | |
2047 | * From drained to drained must not undrain the parent at any | |
2048 | * point | |
2049 | */ | |
2050 | g_assert(!parent_s->was_drained && !parent_s->was_undrained); | |
2051 | } | |
2052 | ||
2053 | if (!old_drain_count || !new_drain_count) { | |
2054 | /* | |
2055 | * If !old_drain_count, we have started a read request before | |
2056 | * bdrv_replace_node(). If !new_drain_count, the parent must | |
2057 | * have been undrained at some point, and | |
2058 | * bdrv_replace_test_co_drain_end() starts a read request | |
2059 | * then. | |
2060 | */ | |
2061 | g_assert(parent_s->has_read); | |
2062 | } else { | |
2063 | /* | |
2064 | * If the parent was never undrained, there is no way to start | |
2065 | * a read request. | |
2066 | */ | |
2067 | g_assert(!parent_s->has_read); | |
2068 | } | |
2069 | ||
2070 | /* A drained child must have not received any request */ | |
2071 | g_assert(!(old_drain_count && old_child_s->has_read)); | |
2072 | g_assert(!(new_drain_count && new_child_s->has_read)); | |
2073 | ||
2074 | for (i = 0; i < new_drain_count; i++) { | |
2075 | bdrv_drained_end(new_child_bs); | |
2076 | } | |
2077 | for (i = 0; i < old_drain_count; i++) { | |
2078 | bdrv_drained_end(old_child_bs); | |
2079 | } | |
2080 | ||
2081 | /* | |
2082 | * By now, bdrv_replace_test_co_drain_end() must have been called | |
2083 | * at some point while the new child was attached to the parent. | |
2084 | */ | |
2085 | g_assert(parent_s->has_read); | |
2086 | g_assert(new_child_s->has_read); | |
2087 | ||
2088 | blk_unref(parent_blk); | |
2089 | bdrv_unref(parent_bs); | |
2090 | bdrv_unref(old_child_bs); | |
2091 | bdrv_unref(new_child_bs); | |
2092 | } | |
2093 | ||
2094 | static void test_replace_child_mid_drain(void) | |
2095 | { | |
2096 | int old_drain_count, new_drain_count; | |
2097 | ||
2098 | for (old_drain_count = 0; old_drain_count < 2; old_drain_count++) { | |
2099 | for (new_drain_count = 0; new_drain_count < 2; new_drain_count++) { | |
2100 | do_test_replace_child_mid_drain(old_drain_count, new_drain_count); | |
2101 | } | |
2102 | } | |
2103 | } | |
2104 | ||
881cfd17 KW |
2105 | int main(int argc, char **argv) |
2106 | { | |
bb675689 KW |
2107 | int ret; |
2108 | ||
881cfd17 KW |
2109 | bdrv_init(); |
2110 | qemu_init_main_loop(&error_abort); | |
2111 | ||
2112 | g_test_init(&argc, &argv, NULL); | |
bb675689 | 2113 | qemu_event_init(&done_event, false); |
881cfd17 KW |
2114 | |
2115 | g_test_add_func("/bdrv-drain/driver-cb/drain_all", test_drv_cb_drain_all); | |
86e1c840 | 2116 | g_test_add_func("/bdrv-drain/driver-cb/drain", test_drv_cb_drain); |
881cfd17 | 2117 | |
6d0252f2 KW |
2118 | g_test_add_func("/bdrv-drain/driver-cb/co/drain_all", |
2119 | test_drv_cb_co_drain_all); | |
0582eb10 | 2120 | g_test_add_func("/bdrv-drain/driver-cb/co/drain", test_drv_cb_co_drain); |
0582eb10 | 2121 | |
89a6ceab KW |
2122 | g_test_add_func("/bdrv-drain/quiesce/drain_all", test_quiesce_drain_all); |
2123 | g_test_add_func("/bdrv-drain/quiesce/drain", test_quiesce_drain); | |
2124 | ||
6d0252f2 KW |
2125 | g_test_add_func("/bdrv-drain/quiesce/co/drain_all", |
2126 | test_quiesce_co_drain_all); | |
0582eb10 | 2127 | g_test_add_func("/bdrv-drain/quiesce/co/drain", test_quiesce_co_drain); |
0582eb10 | 2128 | |
6c429a6a | 2129 | g_test_add_func("/bdrv-drain/nested", test_nested); |
19f7a7e5 | 2130 | |
19f7a7e5 KW |
2131 | g_test_add_func("/bdrv-drain/graph-change/drain_all", |
2132 | test_graph_change_drain_all); | |
6c429a6a | 2133 | |
bb675689 KW |
2134 | g_test_add_func("/bdrv-drain/iothread/drain_all", test_iothread_drain_all); |
2135 | g_test_add_func("/bdrv-drain/iothread/drain", test_iothread_drain); | |
bb675689 | 2136 | |
7253220d KW |
2137 | g_test_add_func("/bdrv-drain/blockjob/drain_all", test_blockjob_drain_all); |
2138 | g_test_add_func("/bdrv-drain/blockjob/drain", test_blockjob_drain); | |
2139 | ||
d49725af KW |
2140 | g_test_add_func("/bdrv-drain/blockjob/error/drain_all", |
2141 | test_blockjob_error_drain_all); | |
2142 | g_test_add_func("/bdrv-drain/blockjob/error/drain", | |
2143 | test_blockjob_error_drain); | |
d49725af | 2144 | |
f62c1729 KW |
2145 | g_test_add_func("/bdrv-drain/blockjob/iothread/drain_all", |
2146 | test_blockjob_iothread_drain_all); | |
2147 | g_test_add_func("/bdrv-drain/blockjob/iothread/drain", | |
2148 | test_blockjob_iothread_drain); | |
f62c1729 | 2149 | |
d49725af KW |
2150 | g_test_add_func("/bdrv-drain/blockjob/iothread/error/drain_all", |
2151 | test_blockjob_iothread_error_drain_all); | |
2152 | g_test_add_func("/bdrv-drain/blockjob/iothread/error/drain", | |
2153 | test_blockjob_iothread_error_drain); | |
d49725af | 2154 | |
ebd31837 | 2155 | g_test_add_func("/bdrv-drain/deletion/drain", test_delete_by_drain); |
19f7a7e5 | 2156 | g_test_add_func("/bdrv-drain/detach/drain_all", test_detach_by_drain_all); |
ebd31837 | 2157 | g_test_add_func("/bdrv-drain/detach/drain", test_detach_by_drain); |
231281ab | 2158 | g_test_add_func("/bdrv-drain/detach/parent_cb", test_detach_by_parent_cb); |
57320ca9 | 2159 | g_test_add_func("/bdrv-drain/detach/driver_cb", test_detach_by_driver_cb); |
4c8158e3 | 2160 | |
b994c5bc KW |
2161 | g_test_add_func("/bdrv-drain/attach/drain", test_append_to_drained); |
2162 | ||
247d2737 KW |
2163 | g_test_add_func("/bdrv-drain/set_aio_context", test_set_aio_context); |
2164 | ||
8e442810 HR |
2165 | g_test_add_func("/bdrv-drain/blockjob/commit_by_drained_end", |
2166 | test_blockjob_commit_by_drained_end); | |
2167 | ||
9746b35c HR |
2168 | g_test_add_func("/bdrv-drain/bdrv_drop_intermediate/poll", |
2169 | test_drop_intermediate_poll); | |
2170 | ||
0513f984 HR |
2171 | g_test_add_func("/bdrv-drain/replace_child/mid-drain", |
2172 | test_replace_child_mid_drain); | |
2173 | ||
bb675689 KW |
2174 | ret = g_test_run(); |
2175 | qemu_event_destroy(&done_event); | |
2176 | return ret; | |
881cfd17 | 2177 | } |