]>
Commit | Line | Data |
---|---|---|
881cfd17 KW |
1 | /* |
2 | * Block node draining tests | |
3 | * | |
4 | * Copyright (c) 2017 Kevin Wolf <[email protected]> | |
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" | |
26 | #include "block/block.h" | |
7253220d | 27 | #include "block/blockjob_int.h" |
881cfd17 KW |
28 | #include "sysemu/block-backend.h" |
29 | #include "qapi/error.h" | |
bb675689 KW |
30 | #include "iothread.h" |
31 | ||
32 | static QemuEvent done_event; | |
881cfd17 KW |
33 | |
34 | typedef struct BDRVTestState { | |
35 | int drain_count; | |
bb675689 | 36 | AioContext *bh_indirection_ctx; |
57320ca9 | 37 | bool sleep_in_drain_begin; |
881cfd17 KW |
38 | } BDRVTestState; |
39 | ||
40 | static void coroutine_fn bdrv_test_co_drain_begin(BlockDriverState *bs) | |
41 | { | |
42 | BDRVTestState *s = bs->opaque; | |
43 | s->drain_count++; | |
57320ca9 KW |
44 | if (s->sleep_in_drain_begin) { |
45 | qemu_co_sleep_ns(QEMU_CLOCK_REALTIME, 100000); | |
46 | } | |
881cfd17 KW |
47 | } |
48 | ||
49 | static void coroutine_fn bdrv_test_co_drain_end(BlockDriverState *bs) | |
50 | { | |
51 | BDRVTestState *s = bs->opaque; | |
52 | s->drain_count--; | |
53 | } | |
54 | ||
55 | static void bdrv_test_close(BlockDriverState *bs) | |
56 | { | |
57 | BDRVTestState *s = bs->opaque; | |
58 | g_assert_cmpint(s->drain_count, >, 0); | |
59 | } | |
60 | ||
bb675689 KW |
61 | static void co_reenter_bh(void *opaque) |
62 | { | |
63 | aio_co_wake(opaque); | |
64 | } | |
65 | ||
881cfd17 KW |
66 | static int coroutine_fn bdrv_test_co_preadv(BlockDriverState *bs, |
67 | uint64_t offset, uint64_t bytes, | |
68 | QEMUIOVector *qiov, int flags) | |
69 | { | |
bb675689 KW |
70 | BDRVTestState *s = bs->opaque; |
71 | ||
881cfd17 KW |
72 | /* We want this request to stay until the polling loop in drain waits for |
73 | * it to complete. We need to sleep a while as bdrv_drain_invoke() comes | |
74 | * first and polls its result, too, but it shouldn't accidentally complete | |
75 | * this request yet. */ | |
76 | qemu_co_sleep_ns(QEMU_CLOCK_REALTIME, 100000); | |
77 | ||
bb675689 KW |
78 | if (s->bh_indirection_ctx) { |
79 | aio_bh_schedule_oneshot(s->bh_indirection_ctx, co_reenter_bh, | |
80 | qemu_coroutine_self()); | |
81 | qemu_coroutine_yield(); | |
82 | } | |
83 | ||
881cfd17 KW |
84 | return 0; |
85 | } | |
86 | ||
57320ca9 KW |
87 | static void bdrv_test_child_perm(BlockDriverState *bs, BdrvChild *c, |
88 | const BdrvChildRole *role, | |
89 | BlockReopenQueue *reopen_queue, | |
90 | uint64_t perm, uint64_t shared, | |
91 | uint64_t *nperm, uint64_t *nshared) | |
92 | { | |
93 | /* bdrv_format_default_perms() accepts only these two, so disguise | |
94 | * detach_by_driver_cb_role as one of them. */ | |
95 | if (role != &child_file && role != &child_backing) { | |
96 | role = &child_file; | |
97 | } | |
98 | ||
99 | bdrv_format_default_perms(bs, c, role, reopen_queue, perm, shared, | |
100 | nperm, nshared); | |
101 | } | |
102 | ||
881cfd17 KW |
103 | static BlockDriver bdrv_test = { |
104 | .format_name = "test", | |
105 | .instance_size = sizeof(BDRVTestState), | |
106 | ||
107 | .bdrv_close = bdrv_test_close, | |
108 | .bdrv_co_preadv = bdrv_test_co_preadv, | |
109 | ||
110 | .bdrv_co_drain_begin = bdrv_test_co_drain_begin, | |
111 | .bdrv_co_drain_end = bdrv_test_co_drain_end, | |
86e1c840 | 112 | |
57320ca9 | 113 | .bdrv_child_perm = bdrv_test_child_perm, |
881cfd17 KW |
114 | }; |
115 | ||
116 | static void aio_ret_cb(void *opaque, int ret) | |
117 | { | |
118 | int *aio_ret = opaque; | |
119 | *aio_ret = ret; | |
120 | } | |
121 | ||
0582eb10 KW |
122 | typedef struct CallInCoroutineData { |
123 | void (*entry)(void); | |
124 | bool done; | |
125 | } CallInCoroutineData; | |
126 | ||
127 | static coroutine_fn void call_in_coroutine_entry(void *opaque) | |
128 | { | |
129 | CallInCoroutineData *data = opaque; | |
130 | ||
131 | data->entry(); | |
132 | data->done = true; | |
133 | } | |
134 | ||
135 | static void call_in_coroutine(void (*entry)(void)) | |
136 | { | |
137 | Coroutine *co; | |
138 | CallInCoroutineData data = { | |
139 | .entry = entry, | |
140 | .done = false, | |
141 | }; | |
142 | ||
143 | co = qemu_coroutine_create(call_in_coroutine_entry, &data); | |
144 | qemu_coroutine_enter(co); | |
145 | while (!data.done) { | |
146 | aio_poll(qemu_get_aio_context(), true); | |
147 | } | |
148 | } | |
149 | ||
86e1c840 KW |
150 | enum drain_type { |
151 | BDRV_DRAIN_ALL, | |
152 | BDRV_DRAIN, | |
d2a85d0f | 153 | BDRV_SUBTREE_DRAIN, |
6c429a6a | 154 | DRAIN_TYPE_MAX, |
86e1c840 KW |
155 | }; |
156 | ||
157 | static void do_drain_begin(enum drain_type drain_type, BlockDriverState *bs) | |
158 | { | |
159 | switch (drain_type) { | |
160 | case BDRV_DRAIN_ALL: bdrv_drain_all_begin(); break; | |
161 | case BDRV_DRAIN: bdrv_drained_begin(bs); break; | |
d2a85d0f | 162 | case BDRV_SUBTREE_DRAIN: bdrv_subtree_drained_begin(bs); break; |
86e1c840 KW |
163 | default: g_assert_not_reached(); |
164 | } | |
165 | } | |
166 | ||
167 | static void do_drain_end(enum drain_type drain_type, BlockDriverState *bs) | |
168 | { | |
169 | switch (drain_type) { | |
170 | case BDRV_DRAIN_ALL: bdrv_drain_all_end(); break; | |
171 | case BDRV_DRAIN: bdrv_drained_end(bs); break; | |
d2a85d0f | 172 | case BDRV_SUBTREE_DRAIN: bdrv_subtree_drained_end(bs); break; |
86e1c840 KW |
173 | default: g_assert_not_reached(); |
174 | } | |
175 | } | |
176 | ||
f62c1729 KW |
177 | static void do_drain_begin_unlocked(enum drain_type drain_type, BlockDriverState *bs) |
178 | { | |
179 | if (drain_type != BDRV_DRAIN_ALL) { | |
180 | aio_context_acquire(bdrv_get_aio_context(bs)); | |
181 | } | |
182 | do_drain_begin(drain_type, bs); | |
183 | if (drain_type != BDRV_DRAIN_ALL) { | |
184 | aio_context_release(bdrv_get_aio_context(bs)); | |
185 | } | |
186 | } | |
187 | ||
188 | static void do_drain_end_unlocked(enum drain_type drain_type, BlockDriverState *bs) | |
189 | { | |
190 | if (drain_type != BDRV_DRAIN_ALL) { | |
191 | aio_context_acquire(bdrv_get_aio_context(bs)); | |
192 | } | |
193 | do_drain_end(drain_type, bs); | |
194 | if (drain_type != BDRV_DRAIN_ALL) { | |
195 | aio_context_release(bdrv_get_aio_context(bs)); | |
196 | } | |
197 | } | |
198 | ||
86e1c840 | 199 | static void test_drv_cb_common(enum drain_type drain_type, bool recursive) |
881cfd17 KW |
200 | { |
201 | BlockBackend *blk; | |
86e1c840 KW |
202 | BlockDriverState *bs, *backing; |
203 | BDRVTestState *s, *backing_s; | |
881cfd17 KW |
204 | BlockAIOCB *acb; |
205 | int aio_ret; | |
206 | ||
405d8fe0 | 207 | QEMUIOVector qiov = QEMU_IOVEC_INIT_BUF(qiov, NULL, 0); |
881cfd17 KW |
208 | |
209 | blk = blk_new(BLK_PERM_ALL, BLK_PERM_ALL); | |
210 | bs = bdrv_new_open_driver(&bdrv_test, "test-node", BDRV_O_RDWR, | |
211 | &error_abort); | |
212 | s = bs->opaque; | |
213 | blk_insert_bs(blk, bs, &error_abort); | |
214 | ||
86e1c840 KW |
215 | backing = bdrv_new_open_driver(&bdrv_test, "backing", 0, &error_abort); |
216 | backing_s = backing->opaque; | |
217 | bdrv_set_backing_hd(bs, backing, &error_abort); | |
218 | ||
881cfd17 KW |
219 | /* Simple bdrv_drain_all_begin/end pair, check that CBs are called */ |
220 | g_assert_cmpint(s->drain_count, ==, 0); | |
86e1c840 KW |
221 | g_assert_cmpint(backing_s->drain_count, ==, 0); |
222 | ||
223 | do_drain_begin(drain_type, bs); | |
224 | ||
881cfd17 | 225 | g_assert_cmpint(s->drain_count, ==, 1); |
86e1c840 KW |
226 | g_assert_cmpint(backing_s->drain_count, ==, !!recursive); |
227 | ||
228 | do_drain_end(drain_type, bs); | |
229 | ||
881cfd17 | 230 | g_assert_cmpint(s->drain_count, ==, 0); |
86e1c840 | 231 | g_assert_cmpint(backing_s->drain_count, ==, 0); |
881cfd17 KW |
232 | |
233 | /* Now do the same while a request is pending */ | |
234 | aio_ret = -EINPROGRESS; | |
235 | acb = blk_aio_preadv(blk, 0, &qiov, 0, aio_ret_cb, &aio_ret); | |
236 | g_assert(acb != NULL); | |
237 | g_assert_cmpint(aio_ret, ==, -EINPROGRESS); | |
238 | ||
239 | g_assert_cmpint(s->drain_count, ==, 0); | |
86e1c840 KW |
240 | g_assert_cmpint(backing_s->drain_count, ==, 0); |
241 | ||
242 | do_drain_begin(drain_type, bs); | |
243 | ||
881cfd17 KW |
244 | g_assert_cmpint(aio_ret, ==, 0); |
245 | g_assert_cmpint(s->drain_count, ==, 1); | |
86e1c840 KW |
246 | g_assert_cmpint(backing_s->drain_count, ==, !!recursive); |
247 | ||
248 | do_drain_end(drain_type, bs); | |
249 | ||
881cfd17 | 250 | g_assert_cmpint(s->drain_count, ==, 0); |
86e1c840 | 251 | g_assert_cmpint(backing_s->drain_count, ==, 0); |
881cfd17 | 252 | |
86e1c840 | 253 | bdrv_unref(backing); |
881cfd17 KW |
254 | bdrv_unref(bs); |
255 | blk_unref(blk); | |
256 | } | |
257 | ||
86e1c840 KW |
258 | static void test_drv_cb_drain_all(void) |
259 | { | |
260 | test_drv_cb_common(BDRV_DRAIN_ALL, true); | |
261 | } | |
262 | ||
263 | static void test_drv_cb_drain(void) | |
264 | { | |
265 | test_drv_cb_common(BDRV_DRAIN, false); | |
266 | } | |
267 | ||
d2a85d0f KW |
268 | static void test_drv_cb_drain_subtree(void) |
269 | { | |
270 | test_drv_cb_common(BDRV_SUBTREE_DRAIN, true); | |
271 | } | |
272 | ||
6d0252f2 KW |
273 | static void test_drv_cb_co_drain_all(void) |
274 | { | |
275 | call_in_coroutine(test_drv_cb_drain_all); | |
276 | } | |
277 | ||
0582eb10 KW |
278 | static void test_drv_cb_co_drain(void) |
279 | { | |
280 | call_in_coroutine(test_drv_cb_drain); | |
281 | } | |
282 | ||
283 | static void test_drv_cb_co_drain_subtree(void) | |
284 | { | |
285 | call_in_coroutine(test_drv_cb_drain_subtree); | |
286 | } | |
287 | ||
89a6ceab KW |
288 | static void test_quiesce_common(enum drain_type drain_type, bool recursive) |
289 | { | |
290 | BlockBackend *blk; | |
291 | BlockDriverState *bs, *backing; | |
292 | ||
293 | blk = blk_new(BLK_PERM_ALL, BLK_PERM_ALL); | |
294 | bs = bdrv_new_open_driver(&bdrv_test, "test-node", BDRV_O_RDWR, | |
295 | &error_abort); | |
296 | blk_insert_bs(blk, bs, &error_abort); | |
297 | ||
298 | backing = bdrv_new_open_driver(&bdrv_test, "backing", 0, &error_abort); | |
299 | bdrv_set_backing_hd(bs, backing, &error_abort); | |
300 | ||
301 | g_assert_cmpint(bs->quiesce_counter, ==, 0); | |
302 | g_assert_cmpint(backing->quiesce_counter, ==, 0); | |
303 | ||
304 | do_drain_begin(drain_type, bs); | |
305 | ||
306 | g_assert_cmpint(bs->quiesce_counter, ==, 1); | |
307 | g_assert_cmpint(backing->quiesce_counter, ==, !!recursive); | |
308 | ||
309 | do_drain_end(drain_type, bs); | |
310 | ||
311 | g_assert_cmpint(bs->quiesce_counter, ==, 0); | |
312 | g_assert_cmpint(backing->quiesce_counter, ==, 0); | |
313 | ||
314 | bdrv_unref(backing); | |
315 | bdrv_unref(bs); | |
316 | blk_unref(blk); | |
317 | } | |
318 | ||
319 | static void test_quiesce_drain_all(void) | |
320 | { | |
79ab8b21 | 321 | test_quiesce_common(BDRV_DRAIN_ALL, true); |
89a6ceab KW |
322 | } |
323 | ||
324 | static void test_quiesce_drain(void) | |
325 | { | |
326 | test_quiesce_common(BDRV_DRAIN, false); | |
327 | } | |
328 | ||
d2a85d0f KW |
329 | static void test_quiesce_drain_subtree(void) |
330 | { | |
331 | test_quiesce_common(BDRV_SUBTREE_DRAIN, true); | |
332 | } | |
333 | ||
6d0252f2 KW |
334 | static void test_quiesce_co_drain_all(void) |
335 | { | |
336 | call_in_coroutine(test_quiesce_drain_all); | |
337 | } | |
338 | ||
0582eb10 KW |
339 | static void test_quiesce_co_drain(void) |
340 | { | |
341 | call_in_coroutine(test_quiesce_drain); | |
342 | } | |
343 | ||
344 | static void test_quiesce_co_drain_subtree(void) | |
345 | { | |
346 | call_in_coroutine(test_quiesce_drain_subtree); | |
347 | } | |
348 | ||
6c429a6a KW |
349 | static void test_nested(void) |
350 | { | |
351 | BlockBackend *blk; | |
352 | BlockDriverState *bs, *backing; | |
353 | BDRVTestState *s, *backing_s; | |
354 | enum drain_type outer, inner; | |
355 | ||
356 | blk = blk_new(BLK_PERM_ALL, BLK_PERM_ALL); | |
357 | bs = bdrv_new_open_driver(&bdrv_test, "test-node", BDRV_O_RDWR, | |
358 | &error_abort); | |
359 | s = bs->opaque; | |
360 | blk_insert_bs(blk, bs, &error_abort); | |
361 | ||
362 | backing = bdrv_new_open_driver(&bdrv_test, "backing", 0, &error_abort); | |
363 | backing_s = backing->opaque; | |
364 | bdrv_set_backing_hd(bs, backing, &error_abort); | |
365 | ||
366 | for (outer = 0; outer < DRAIN_TYPE_MAX; outer++) { | |
367 | for (inner = 0; inner < DRAIN_TYPE_MAX; inner++) { | |
79ab8b21 | 368 | int backing_quiesce = (outer != BDRV_DRAIN) + |
6c429a6a KW |
369 | (inner != BDRV_DRAIN); |
370 | ||
371 | g_assert_cmpint(bs->quiesce_counter, ==, 0); | |
372 | g_assert_cmpint(backing->quiesce_counter, ==, 0); | |
373 | g_assert_cmpint(s->drain_count, ==, 0); | |
374 | g_assert_cmpint(backing_s->drain_count, ==, 0); | |
375 | ||
376 | do_drain_begin(outer, bs); | |
377 | do_drain_begin(inner, bs); | |
378 | ||
79ab8b21 | 379 | g_assert_cmpint(bs->quiesce_counter, ==, 2); |
6c429a6a KW |
380 | g_assert_cmpint(backing->quiesce_counter, ==, backing_quiesce); |
381 | g_assert_cmpint(s->drain_count, ==, 2); | |
79ab8b21 | 382 | g_assert_cmpint(backing_s->drain_count, ==, backing_quiesce); |
6c429a6a KW |
383 | |
384 | do_drain_end(inner, bs); | |
385 | do_drain_end(outer, bs); | |
386 | ||
387 | g_assert_cmpint(bs->quiesce_counter, ==, 0); | |
388 | g_assert_cmpint(backing->quiesce_counter, ==, 0); | |
389 | g_assert_cmpint(s->drain_count, ==, 0); | |
390 | g_assert_cmpint(backing_s->drain_count, ==, 0); | |
391 | } | |
392 | } | |
393 | ||
394 | bdrv_unref(backing); | |
395 | bdrv_unref(bs); | |
396 | blk_unref(blk); | |
397 | } | |
398 | ||
27e64474 KW |
399 | static void test_multiparent(void) |
400 | { | |
401 | BlockBackend *blk_a, *blk_b; | |
402 | BlockDriverState *bs_a, *bs_b, *backing; | |
403 | BDRVTestState *a_s, *b_s, *backing_s; | |
404 | ||
405 | blk_a = blk_new(BLK_PERM_ALL, BLK_PERM_ALL); | |
406 | bs_a = bdrv_new_open_driver(&bdrv_test, "test-node-a", BDRV_O_RDWR, | |
407 | &error_abort); | |
408 | a_s = bs_a->opaque; | |
409 | blk_insert_bs(blk_a, bs_a, &error_abort); | |
410 | ||
411 | blk_b = blk_new(BLK_PERM_ALL, BLK_PERM_ALL); | |
412 | bs_b = bdrv_new_open_driver(&bdrv_test, "test-node-b", BDRV_O_RDWR, | |
413 | &error_abort); | |
414 | b_s = bs_b->opaque; | |
415 | blk_insert_bs(blk_b, bs_b, &error_abort); | |
416 | ||
417 | backing = bdrv_new_open_driver(&bdrv_test, "backing", 0, &error_abort); | |
418 | backing_s = backing->opaque; | |
419 | bdrv_set_backing_hd(bs_a, backing, &error_abort); | |
420 | bdrv_set_backing_hd(bs_b, backing, &error_abort); | |
421 | ||
422 | g_assert_cmpint(bs_a->quiesce_counter, ==, 0); | |
423 | g_assert_cmpint(bs_b->quiesce_counter, ==, 0); | |
424 | g_assert_cmpint(backing->quiesce_counter, ==, 0); | |
425 | g_assert_cmpint(a_s->drain_count, ==, 0); | |
426 | g_assert_cmpint(b_s->drain_count, ==, 0); | |
427 | g_assert_cmpint(backing_s->drain_count, ==, 0); | |
428 | ||
429 | do_drain_begin(BDRV_SUBTREE_DRAIN, bs_a); | |
430 | ||
431 | g_assert_cmpint(bs_a->quiesce_counter, ==, 1); | |
432 | g_assert_cmpint(bs_b->quiesce_counter, ==, 1); | |
433 | g_assert_cmpint(backing->quiesce_counter, ==, 1); | |
434 | g_assert_cmpint(a_s->drain_count, ==, 1); | |
435 | g_assert_cmpint(b_s->drain_count, ==, 1); | |
436 | g_assert_cmpint(backing_s->drain_count, ==, 1); | |
437 | ||
438 | do_drain_begin(BDRV_SUBTREE_DRAIN, bs_b); | |
439 | ||
440 | g_assert_cmpint(bs_a->quiesce_counter, ==, 2); | |
441 | g_assert_cmpint(bs_b->quiesce_counter, ==, 2); | |
442 | g_assert_cmpint(backing->quiesce_counter, ==, 2); | |
443 | g_assert_cmpint(a_s->drain_count, ==, 2); | |
444 | g_assert_cmpint(b_s->drain_count, ==, 2); | |
445 | g_assert_cmpint(backing_s->drain_count, ==, 2); | |
446 | ||
447 | do_drain_end(BDRV_SUBTREE_DRAIN, bs_b); | |
448 | ||
449 | g_assert_cmpint(bs_a->quiesce_counter, ==, 1); | |
450 | g_assert_cmpint(bs_b->quiesce_counter, ==, 1); | |
451 | g_assert_cmpint(backing->quiesce_counter, ==, 1); | |
452 | g_assert_cmpint(a_s->drain_count, ==, 1); | |
453 | g_assert_cmpint(b_s->drain_count, ==, 1); | |
454 | g_assert_cmpint(backing_s->drain_count, ==, 1); | |
455 | ||
456 | do_drain_end(BDRV_SUBTREE_DRAIN, bs_a); | |
457 | ||
458 | g_assert_cmpint(bs_a->quiesce_counter, ==, 0); | |
459 | g_assert_cmpint(bs_b->quiesce_counter, ==, 0); | |
460 | g_assert_cmpint(backing->quiesce_counter, ==, 0); | |
461 | g_assert_cmpint(a_s->drain_count, ==, 0); | |
462 | g_assert_cmpint(b_s->drain_count, ==, 0); | |
463 | g_assert_cmpint(backing_s->drain_count, ==, 0); | |
464 | ||
465 | bdrv_unref(backing); | |
466 | bdrv_unref(bs_a); | |
467 | bdrv_unref(bs_b); | |
468 | blk_unref(blk_a); | |
469 | blk_unref(blk_b); | |
470 | } | |
471 | ||
19f7a7e5 | 472 | static void test_graph_change_drain_subtree(void) |
acebcf8d KW |
473 | { |
474 | BlockBackend *blk_a, *blk_b; | |
475 | BlockDriverState *bs_a, *bs_b, *backing; | |
476 | BDRVTestState *a_s, *b_s, *backing_s; | |
477 | ||
478 | blk_a = blk_new(BLK_PERM_ALL, BLK_PERM_ALL); | |
479 | bs_a = bdrv_new_open_driver(&bdrv_test, "test-node-a", BDRV_O_RDWR, | |
480 | &error_abort); | |
481 | a_s = bs_a->opaque; | |
482 | blk_insert_bs(blk_a, bs_a, &error_abort); | |
483 | ||
484 | blk_b = blk_new(BLK_PERM_ALL, BLK_PERM_ALL); | |
485 | bs_b = bdrv_new_open_driver(&bdrv_test, "test-node-b", BDRV_O_RDWR, | |
486 | &error_abort); | |
487 | b_s = bs_b->opaque; | |
488 | blk_insert_bs(blk_b, bs_b, &error_abort); | |
489 | ||
490 | backing = bdrv_new_open_driver(&bdrv_test, "backing", 0, &error_abort); | |
491 | backing_s = backing->opaque; | |
492 | bdrv_set_backing_hd(bs_a, backing, &error_abort); | |
493 | ||
494 | g_assert_cmpint(bs_a->quiesce_counter, ==, 0); | |
495 | g_assert_cmpint(bs_b->quiesce_counter, ==, 0); | |
496 | g_assert_cmpint(backing->quiesce_counter, ==, 0); | |
497 | g_assert_cmpint(a_s->drain_count, ==, 0); | |
498 | g_assert_cmpint(b_s->drain_count, ==, 0); | |
499 | g_assert_cmpint(backing_s->drain_count, ==, 0); | |
500 | ||
501 | do_drain_begin(BDRV_SUBTREE_DRAIN, bs_a); | |
502 | do_drain_begin(BDRV_SUBTREE_DRAIN, bs_a); | |
503 | do_drain_begin(BDRV_SUBTREE_DRAIN, bs_a); | |
504 | do_drain_begin(BDRV_SUBTREE_DRAIN, bs_b); | |
505 | do_drain_begin(BDRV_SUBTREE_DRAIN, bs_b); | |
506 | ||
507 | bdrv_set_backing_hd(bs_b, backing, &error_abort); | |
508 | g_assert_cmpint(bs_a->quiesce_counter, ==, 5); | |
509 | g_assert_cmpint(bs_b->quiesce_counter, ==, 5); | |
510 | g_assert_cmpint(backing->quiesce_counter, ==, 5); | |
511 | g_assert_cmpint(a_s->drain_count, ==, 5); | |
512 | g_assert_cmpint(b_s->drain_count, ==, 5); | |
513 | g_assert_cmpint(backing_s->drain_count, ==, 5); | |
514 | ||
515 | bdrv_set_backing_hd(bs_b, NULL, &error_abort); | |
516 | g_assert_cmpint(bs_a->quiesce_counter, ==, 3); | |
517 | g_assert_cmpint(bs_b->quiesce_counter, ==, 2); | |
518 | g_assert_cmpint(backing->quiesce_counter, ==, 3); | |
519 | g_assert_cmpint(a_s->drain_count, ==, 3); | |
520 | g_assert_cmpint(b_s->drain_count, ==, 2); | |
521 | g_assert_cmpint(backing_s->drain_count, ==, 3); | |
522 | ||
523 | bdrv_set_backing_hd(bs_b, backing, &error_abort); | |
524 | g_assert_cmpint(bs_a->quiesce_counter, ==, 5); | |
525 | g_assert_cmpint(bs_b->quiesce_counter, ==, 5); | |
526 | g_assert_cmpint(backing->quiesce_counter, ==, 5); | |
527 | g_assert_cmpint(a_s->drain_count, ==, 5); | |
528 | g_assert_cmpint(b_s->drain_count, ==, 5); | |
529 | g_assert_cmpint(backing_s->drain_count, ==, 5); | |
530 | ||
531 | do_drain_end(BDRV_SUBTREE_DRAIN, bs_b); | |
532 | do_drain_end(BDRV_SUBTREE_DRAIN, bs_b); | |
533 | do_drain_end(BDRV_SUBTREE_DRAIN, bs_a); | |
534 | do_drain_end(BDRV_SUBTREE_DRAIN, bs_a); | |
535 | do_drain_end(BDRV_SUBTREE_DRAIN, bs_a); | |
536 | ||
537 | g_assert_cmpint(bs_a->quiesce_counter, ==, 0); | |
538 | g_assert_cmpint(bs_b->quiesce_counter, ==, 0); | |
539 | g_assert_cmpint(backing->quiesce_counter, ==, 0); | |
540 | g_assert_cmpint(a_s->drain_count, ==, 0); | |
541 | g_assert_cmpint(b_s->drain_count, ==, 0); | |
542 | g_assert_cmpint(backing_s->drain_count, ==, 0); | |
543 | ||
544 | bdrv_unref(backing); | |
545 | bdrv_unref(bs_a); | |
546 | bdrv_unref(bs_b); | |
547 | blk_unref(blk_a); | |
548 | blk_unref(blk_b); | |
549 | } | |
550 | ||
19f7a7e5 KW |
551 | static void test_graph_change_drain_all(void) |
552 | { | |
553 | BlockBackend *blk_a, *blk_b; | |
554 | BlockDriverState *bs_a, *bs_b; | |
555 | BDRVTestState *a_s, *b_s; | |
556 | ||
557 | /* Create node A with a BlockBackend */ | |
558 | blk_a = blk_new(BLK_PERM_ALL, BLK_PERM_ALL); | |
559 | bs_a = bdrv_new_open_driver(&bdrv_test, "test-node-a", BDRV_O_RDWR, | |
560 | &error_abort); | |
561 | a_s = bs_a->opaque; | |
562 | blk_insert_bs(blk_a, bs_a, &error_abort); | |
563 | ||
564 | g_assert_cmpint(bs_a->quiesce_counter, ==, 0); | |
565 | g_assert_cmpint(a_s->drain_count, ==, 0); | |
566 | ||
567 | /* Call bdrv_drain_all_begin() */ | |
568 | bdrv_drain_all_begin(); | |
569 | ||
570 | g_assert_cmpint(bs_a->quiesce_counter, ==, 1); | |
571 | g_assert_cmpint(a_s->drain_count, ==, 1); | |
572 | ||
573 | /* Create node B with a BlockBackend */ | |
574 | blk_b = blk_new(BLK_PERM_ALL, BLK_PERM_ALL); | |
575 | bs_b = bdrv_new_open_driver(&bdrv_test, "test-node-b", BDRV_O_RDWR, | |
576 | &error_abort); | |
577 | b_s = bs_b->opaque; | |
578 | blk_insert_bs(blk_b, bs_b, &error_abort); | |
579 | ||
580 | g_assert_cmpint(bs_a->quiesce_counter, ==, 1); | |
581 | g_assert_cmpint(bs_b->quiesce_counter, ==, 1); | |
582 | g_assert_cmpint(a_s->drain_count, ==, 1); | |
583 | g_assert_cmpint(b_s->drain_count, ==, 1); | |
584 | ||
585 | /* Unref and finally delete node A */ | |
586 | blk_unref(blk_a); | |
587 | ||
588 | g_assert_cmpint(bs_a->quiesce_counter, ==, 1); | |
589 | g_assert_cmpint(bs_b->quiesce_counter, ==, 1); | |
590 | g_assert_cmpint(a_s->drain_count, ==, 1); | |
591 | g_assert_cmpint(b_s->drain_count, ==, 1); | |
592 | ||
593 | bdrv_unref(bs_a); | |
594 | ||
595 | g_assert_cmpint(bs_b->quiesce_counter, ==, 1); | |
596 | g_assert_cmpint(b_s->drain_count, ==, 1); | |
597 | ||
598 | /* End the drained section */ | |
599 | bdrv_drain_all_end(); | |
600 | ||
601 | g_assert_cmpint(bs_b->quiesce_counter, ==, 0); | |
602 | g_assert_cmpint(b_s->drain_count, ==, 0); | |
603 | ||
604 | bdrv_unref(bs_b); | |
605 | blk_unref(blk_b); | |
606 | } | |
607 | ||
bb675689 KW |
608 | struct test_iothread_data { |
609 | BlockDriverState *bs; | |
610 | enum drain_type drain_type; | |
611 | int *aio_ret; | |
612 | }; | |
613 | ||
614 | static void test_iothread_drain_entry(void *opaque) | |
615 | { | |
616 | struct test_iothread_data *data = opaque; | |
617 | ||
618 | aio_context_acquire(bdrv_get_aio_context(data->bs)); | |
619 | do_drain_begin(data->drain_type, data->bs); | |
620 | g_assert_cmpint(*data->aio_ret, ==, 0); | |
621 | do_drain_end(data->drain_type, data->bs); | |
622 | aio_context_release(bdrv_get_aio_context(data->bs)); | |
623 | ||
624 | qemu_event_set(&done_event); | |
625 | } | |
626 | ||
627 | static void test_iothread_aio_cb(void *opaque, int ret) | |
628 | { | |
629 | int *aio_ret = opaque; | |
630 | *aio_ret = ret; | |
631 | qemu_event_set(&done_event); | |
632 | } | |
633 | ||
ecc1a5c7 KW |
634 | static void test_iothread_main_thread_bh(void *opaque) |
635 | { | |
636 | struct test_iothread_data *data = opaque; | |
637 | ||
638 | /* Test that the AioContext is not yet locked in a random BH that is | |
639 | * executed during drain, otherwise this would deadlock. */ | |
640 | aio_context_acquire(bdrv_get_aio_context(data->bs)); | |
641 | bdrv_flush(data->bs); | |
642 | aio_context_release(bdrv_get_aio_context(data->bs)); | |
643 | } | |
644 | ||
bb675689 KW |
645 | /* |
646 | * Starts an AIO request on a BDS that runs in the AioContext of iothread 1. | |
647 | * The request involves a BH on iothread 2 before it can complete. | |
648 | * | |
649 | * @drain_thread = 0 means that do_drain_begin/end are called from the main | |
650 | * thread, @drain_thread = 1 means that they are called from iothread 1. Drain | |
651 | * for this BDS cannot be called from iothread 2 because only the main thread | |
652 | * may do cross-AioContext polling. | |
653 | */ | |
654 | static void test_iothread_common(enum drain_type drain_type, int drain_thread) | |
655 | { | |
656 | BlockBackend *blk; | |
657 | BlockDriverState *bs; | |
658 | BDRVTestState *s; | |
659 | BlockAIOCB *acb; | |
660 | int aio_ret; | |
661 | struct test_iothread_data data; | |
662 | ||
663 | IOThread *a = iothread_new(); | |
664 | IOThread *b = iothread_new(); | |
665 | AioContext *ctx_a = iothread_get_aio_context(a); | |
666 | AioContext *ctx_b = iothread_get_aio_context(b); | |
667 | ||
405d8fe0 | 668 | QEMUIOVector qiov = QEMU_IOVEC_INIT_BUF(qiov, NULL, 0); |
bb675689 KW |
669 | |
670 | /* bdrv_drain_all() may only be called from the main loop thread */ | |
671 | if (drain_type == BDRV_DRAIN_ALL && drain_thread != 0) { | |
672 | goto out; | |
673 | } | |
674 | ||
675 | blk = blk_new(BLK_PERM_ALL, BLK_PERM_ALL); | |
676 | bs = bdrv_new_open_driver(&bdrv_test, "test-node", BDRV_O_RDWR, | |
677 | &error_abort); | |
678 | s = bs->opaque; | |
679 | blk_insert_bs(blk, bs, &error_abort); | |
680 | ||
681 | blk_set_aio_context(blk, ctx_a); | |
682 | aio_context_acquire(ctx_a); | |
683 | ||
684 | s->bh_indirection_ctx = ctx_b; | |
685 | ||
686 | aio_ret = -EINPROGRESS; | |
dd353157 KW |
687 | qemu_event_reset(&done_event); |
688 | ||
bb675689 KW |
689 | if (drain_thread == 0) { |
690 | acb = blk_aio_preadv(blk, 0, &qiov, 0, test_iothread_aio_cb, &aio_ret); | |
691 | } else { | |
692 | acb = blk_aio_preadv(blk, 0, &qiov, 0, aio_ret_cb, &aio_ret); | |
693 | } | |
694 | g_assert(acb != NULL); | |
695 | g_assert_cmpint(aio_ret, ==, -EINPROGRESS); | |
696 | ||
697 | aio_context_release(ctx_a); | |
698 | ||
699 | data = (struct test_iothread_data) { | |
700 | .bs = bs, | |
701 | .drain_type = drain_type, | |
702 | .aio_ret = &aio_ret, | |
703 | }; | |
704 | ||
705 | switch (drain_thread) { | |
706 | case 0: | |
707 | if (drain_type != BDRV_DRAIN_ALL) { | |
708 | aio_context_acquire(ctx_a); | |
709 | } | |
710 | ||
ecc1a5c7 KW |
711 | aio_bh_schedule_oneshot(ctx_a, test_iothread_main_thread_bh, &data); |
712 | ||
bb675689 KW |
713 | /* The request is running on the IOThread a. Draining its block device |
714 | * will make sure that it has completed as far as the BDS is concerned, | |
715 | * but the drain in this thread can continue immediately after | |
716 | * bdrv_dec_in_flight() and aio_ret might be assigned only slightly | |
717 | * later. */ | |
bb675689 KW |
718 | do_drain_begin(drain_type, bs); |
719 | g_assert_cmpint(bs->in_flight, ==, 0); | |
720 | ||
721 | if (drain_type != BDRV_DRAIN_ALL) { | |
722 | aio_context_release(ctx_a); | |
723 | } | |
724 | qemu_event_wait(&done_event); | |
725 | if (drain_type != BDRV_DRAIN_ALL) { | |
726 | aio_context_acquire(ctx_a); | |
727 | } | |
728 | ||
729 | g_assert_cmpint(aio_ret, ==, 0); | |
730 | do_drain_end(drain_type, bs); | |
731 | ||
732 | if (drain_type != BDRV_DRAIN_ALL) { | |
733 | aio_context_release(ctx_a); | |
734 | } | |
735 | break; | |
736 | case 1: | |
bb675689 KW |
737 | aio_bh_schedule_oneshot(ctx_a, test_iothread_drain_entry, &data); |
738 | qemu_event_wait(&done_event); | |
739 | break; | |
740 | default: | |
741 | g_assert_not_reached(); | |
742 | } | |
743 | ||
744 | aio_context_acquire(ctx_a); | |
745 | blk_set_aio_context(blk, qemu_get_aio_context()); | |
746 | aio_context_release(ctx_a); | |
747 | ||
748 | bdrv_unref(bs); | |
749 | blk_unref(blk); | |
750 | ||
751 | out: | |
752 | iothread_join(a); | |
753 | iothread_join(b); | |
754 | } | |
755 | ||
756 | static void test_iothread_drain_all(void) | |
757 | { | |
758 | test_iothread_common(BDRV_DRAIN_ALL, 0); | |
759 | test_iothread_common(BDRV_DRAIN_ALL, 1); | |
760 | } | |
761 | ||
762 | static void test_iothread_drain(void) | |
763 | { | |
764 | test_iothread_common(BDRV_DRAIN, 0); | |
765 | test_iothread_common(BDRV_DRAIN, 1); | |
766 | } | |
767 | ||
768 | static void test_iothread_drain_subtree(void) | |
769 | { | |
770 | test_iothread_common(BDRV_SUBTREE_DRAIN, 0); | |
771 | test_iothread_common(BDRV_SUBTREE_DRAIN, 1); | |
772 | } | |
773 | ||
7253220d KW |
774 | |
775 | typedef struct TestBlockJob { | |
776 | BlockJob common; | |
d49725af KW |
777 | int run_ret; |
778 | int prepare_ret; | |
d8b3afd5 | 779 | bool running; |
7253220d KW |
780 | bool should_complete; |
781 | } TestBlockJob; | |
782 | ||
ae23dde9 KW |
783 | static int test_job_prepare(Job *job) |
784 | { | |
785 | TestBlockJob *s = container_of(job, TestBlockJob, common.job); | |
786 | ||
787 | /* Provoke an AIO_WAIT_WHILE() call to verify there is no deadlock */ | |
788 | blk_flush(s->common.blk); | |
d49725af KW |
789 | return s->prepare_ret; |
790 | } | |
791 | ||
792 | static void test_job_commit(Job *job) | |
793 | { | |
794 | TestBlockJob *s = container_of(job, TestBlockJob, common.job); | |
795 | ||
796 | /* Provoke an AIO_WAIT_WHILE() call to verify there is no deadlock */ | |
797 | blk_flush(s->common.blk); | |
798 | } | |
799 | ||
800 | static void test_job_abort(Job *job) | |
801 | { | |
802 | TestBlockJob *s = container_of(job, TestBlockJob, common.job); | |
803 | ||
804 | /* Provoke an AIO_WAIT_WHILE() call to verify there is no deadlock */ | |
805 | blk_flush(s->common.blk); | |
ae23dde9 KW |
806 | } |
807 | ||
f67432a2 | 808 | static int coroutine_fn test_job_run(Job *job, Error **errp) |
7253220d | 809 | { |
f67432a2 | 810 | TestBlockJob *s = container_of(job, TestBlockJob, common.job); |
7253220d | 811 | |
d8b3afd5 KW |
812 | /* We are running the actual job code past the pause point in |
813 | * job_co_entry(). */ | |
814 | s->running = true; | |
815 | ||
2e1795b5 | 816 | job_transition_to_ready(&s->common.job); |
7253220d | 817 | while (!s->should_complete) { |
5599c162 KW |
818 | /* Avoid job_sleep_ns() because it marks the job as !busy. We want to |
819 | * emulate some actual activity (probably some I/O) here so that drain | |
820 | * has to wait for this activity to stop. */ | |
d8b3afd5 KW |
821 | qemu_co_sleep_ns(QEMU_CLOCK_REALTIME, 1000000); |
822 | ||
89bd0305 | 823 | job_pause_point(&s->common.job); |
7253220d KW |
824 | } |
825 | ||
d49725af | 826 | return s->run_ret; |
7253220d KW |
827 | } |
828 | ||
3453d972 | 829 | static void test_job_complete(Job *job, Error **errp) |
7253220d | 830 | { |
3453d972 | 831 | TestBlockJob *s = container_of(job, TestBlockJob, common.job); |
7253220d KW |
832 | s->should_complete = true; |
833 | } | |
834 | ||
835 | BlockJobDriver test_job_driver = { | |
33e9e9bd KW |
836 | .job_driver = { |
837 | .instance_size = sizeof(TestBlockJob), | |
80fa2c75 | 838 | .free = block_job_free, |
b15de828 | 839 | .user_resume = block_job_user_resume, |
b69f777d | 840 | .drain = block_job_drain, |
f67432a2 | 841 | .run = test_job_run, |
3453d972 | 842 | .complete = test_job_complete, |
ae23dde9 | 843 | .prepare = test_job_prepare, |
d49725af KW |
844 | .commit = test_job_commit, |
845 | .abort = test_job_abort, | |
33e9e9bd | 846 | }, |
7253220d KW |
847 | }; |
848 | ||
d49725af KW |
849 | enum test_job_result { |
850 | TEST_JOB_SUCCESS, | |
851 | TEST_JOB_FAIL_RUN, | |
852 | TEST_JOB_FAIL_PREPARE, | |
853 | }; | |
854 | ||
d8b3afd5 KW |
855 | enum test_job_drain_node { |
856 | TEST_JOB_DRAIN_SRC, | |
857 | TEST_JOB_DRAIN_SRC_CHILD, | |
858 | TEST_JOB_DRAIN_SRC_PARENT, | |
859 | }; | |
860 | ||
861 | static void test_blockjob_common_drain_node(enum drain_type drain_type, | |
862 | bool use_iothread, | |
863 | enum test_job_result result, | |
864 | enum test_job_drain_node drain_node) | |
7253220d KW |
865 | { |
866 | BlockBackend *blk_src, *blk_target; | |
d8b3afd5 | 867 | BlockDriverState *src, *src_backing, *src_overlay, *target, *drain_bs; |
7253220d | 868 | BlockJob *job; |
d49725af | 869 | TestBlockJob *tjob; |
f62c1729 KW |
870 | IOThread *iothread = NULL; |
871 | AioContext *ctx; | |
7253220d KW |
872 | int ret; |
873 | ||
874 | src = bdrv_new_open_driver(&bdrv_test, "source", BDRV_O_RDWR, | |
875 | &error_abort); | |
d8b3afd5 KW |
876 | src_backing = bdrv_new_open_driver(&bdrv_test, "source-backing", |
877 | BDRV_O_RDWR, &error_abort); | |
878 | src_overlay = bdrv_new_open_driver(&bdrv_test, "source-overlay", | |
879 | BDRV_O_RDWR, &error_abort); | |
880 | ||
881 | bdrv_set_backing_hd(src_overlay, src, &error_abort); | |
882 | bdrv_unref(src); | |
883 | bdrv_set_backing_hd(src, src_backing, &error_abort); | |
884 | bdrv_unref(src_backing); | |
885 | ||
7253220d | 886 | blk_src = blk_new(BLK_PERM_ALL, BLK_PERM_ALL); |
d8b3afd5 KW |
887 | blk_insert_bs(blk_src, src_overlay, &error_abort); |
888 | ||
889 | switch (drain_node) { | |
890 | case TEST_JOB_DRAIN_SRC: | |
891 | drain_bs = src; | |
892 | break; | |
893 | case TEST_JOB_DRAIN_SRC_CHILD: | |
894 | drain_bs = src_backing; | |
895 | break; | |
896 | case TEST_JOB_DRAIN_SRC_PARENT: | |
897 | drain_bs = src_overlay; | |
898 | break; | |
899 | default: | |
900 | g_assert_not_reached(); | |
901 | } | |
7253220d | 902 | |
f62c1729 KW |
903 | if (use_iothread) { |
904 | iothread = iothread_new(); | |
905 | ctx = iothread_get_aio_context(iothread); | |
906 | blk_set_aio_context(blk_src, ctx); | |
907 | } else { | |
908 | ctx = qemu_get_aio_context(); | |
909 | } | |
910 | ||
7253220d KW |
911 | target = bdrv_new_open_driver(&bdrv_test, "target", BDRV_O_RDWR, |
912 | &error_abort); | |
913 | blk_target = blk_new(BLK_PERM_ALL, BLK_PERM_ALL); | |
914 | blk_insert_bs(blk_target, target, &error_abort); | |
915 | ||
f62c1729 | 916 | aio_context_acquire(ctx); |
d49725af KW |
917 | tjob = block_job_create("job0", &test_job_driver, NULL, src, |
918 | 0, BLK_PERM_ALL, | |
919 | 0, 0, NULL, NULL, &error_abort); | |
920 | job = &tjob->common; | |
7253220d | 921 | block_job_add_bdrv(job, "target", target, 0, BLK_PERM_ALL, &error_abort); |
d49725af KW |
922 | |
923 | switch (result) { | |
924 | case TEST_JOB_SUCCESS: | |
925 | break; | |
926 | case TEST_JOB_FAIL_RUN: | |
927 | tjob->run_ret = -EIO; | |
928 | break; | |
929 | case TEST_JOB_FAIL_PREPARE: | |
930 | tjob->prepare_ret = -EIO; | |
931 | break; | |
932 | } | |
933 | ||
da01ff7f | 934 | job_start(&job->job); |
f62c1729 | 935 | aio_context_release(ctx); |
7253220d | 936 | |
d8b3afd5 KW |
937 | if (use_iothread) { |
938 | /* job_co_entry() is run in the I/O thread, wait for the actual job | |
939 | * code to start (we don't want to catch the job in the pause point in | |
940 | * job_co_entry(). */ | |
941 | while (!tjob->running) { | |
942 | aio_poll(qemu_get_aio_context(), false); | |
943 | } | |
944 | } | |
945 | ||
da01ff7f KW |
946 | g_assert_cmpint(job->job.pause_count, ==, 0); |
947 | g_assert_false(job->job.paused); | |
d8b3afd5 | 948 | g_assert_true(tjob->running); |
5599c162 | 949 | g_assert_true(job->job.busy); /* We're in qemu_co_sleep_ns() */ |
7253220d | 950 | |
d8b3afd5 | 951 | do_drain_begin_unlocked(drain_type, drain_bs); |
7253220d KW |
952 | |
953 | if (drain_type == BDRV_DRAIN_ALL) { | |
81193349 | 954 | /* bdrv_drain_all() drains both src and target */ |
da01ff7f | 955 | g_assert_cmpint(job->job.pause_count, ==, 2); |
7253220d | 956 | } else { |
da01ff7f | 957 | g_assert_cmpint(job->job.pause_count, ==, 1); |
7253220d | 958 | } |
89bd0305 | 959 | g_assert_true(job->job.paused); |
da01ff7f | 960 | g_assert_false(job->job.busy); /* The job is paused */ |
7253220d | 961 | |
d8b3afd5 | 962 | do_drain_end_unlocked(drain_type, drain_bs); |
f62c1729 KW |
963 | |
964 | if (use_iothread) { | |
965 | /* paused is reset in the I/O thread, wait for it */ | |
966 | while (job->job.paused) { | |
967 | aio_poll(qemu_get_aio_context(), false); | |
968 | } | |
969 | } | |
7253220d | 970 | |
da01ff7f KW |
971 | g_assert_cmpint(job->job.pause_count, ==, 0); |
972 | g_assert_false(job->job.paused); | |
89bd0305 | 973 | g_assert_true(job->job.busy); /* We're in qemu_co_sleep_ns() */ |
7253220d KW |
974 | |
975 | do_drain_begin(drain_type, target); | |
976 | ||
977 | if (drain_type == BDRV_DRAIN_ALL) { | |
81193349 | 978 | /* bdrv_drain_all() drains both src and target */ |
da01ff7f | 979 | g_assert_cmpint(job->job.pause_count, ==, 2); |
7253220d | 980 | } else { |
da01ff7f | 981 | g_assert_cmpint(job->job.pause_count, ==, 1); |
7253220d | 982 | } |
89bd0305 | 983 | g_assert_true(job->job.paused); |
da01ff7f | 984 | g_assert_false(job->job.busy); /* The job is paused */ |
7253220d KW |
985 | |
986 | do_drain_end(drain_type, target); | |
987 | ||
f62c1729 KW |
988 | if (use_iothread) { |
989 | /* paused is reset in the I/O thread, wait for it */ | |
990 | while (job->job.paused) { | |
991 | aio_poll(qemu_get_aio_context(), false); | |
992 | } | |
993 | } | |
994 | ||
da01ff7f KW |
995 | g_assert_cmpint(job->job.pause_count, ==, 0); |
996 | g_assert_false(job->job.paused); | |
5599c162 | 997 | g_assert_true(job->job.busy); /* We're in qemu_co_sleep_ns() */ |
7253220d | 998 | |
f62c1729 | 999 | aio_context_acquire(ctx); |
3d70ff53 | 1000 | ret = job_complete_sync(&job->job, &error_abort); |
d49725af | 1001 | g_assert_cmpint(ret, ==, (result == TEST_JOB_SUCCESS ? 0 : -EIO)); |
7253220d | 1002 | |
f62c1729 KW |
1003 | if (use_iothread) { |
1004 | blk_set_aio_context(blk_src, qemu_get_aio_context()); | |
1005 | } | |
1006 | aio_context_release(ctx); | |
1007 | ||
7253220d KW |
1008 | blk_unref(blk_src); |
1009 | blk_unref(blk_target); | |
d8b3afd5 | 1010 | bdrv_unref(src_overlay); |
7253220d | 1011 | bdrv_unref(target); |
f62c1729 KW |
1012 | |
1013 | if (iothread) { | |
1014 | iothread_join(iothread); | |
1015 | } | |
7253220d KW |
1016 | } |
1017 | ||
d8b3afd5 KW |
1018 | static void test_blockjob_common(enum drain_type drain_type, bool use_iothread, |
1019 | enum test_job_result result) | |
1020 | { | |
1021 | test_blockjob_common_drain_node(drain_type, use_iothread, result, | |
1022 | TEST_JOB_DRAIN_SRC); | |
1023 | test_blockjob_common_drain_node(drain_type, use_iothread, result, | |
1024 | TEST_JOB_DRAIN_SRC_CHILD); | |
1025 | if (drain_type == BDRV_SUBTREE_DRAIN) { | |
1026 | test_blockjob_common_drain_node(drain_type, use_iothread, result, | |
1027 | TEST_JOB_DRAIN_SRC_PARENT); | |
1028 | } | |
1029 | } | |
1030 | ||
7253220d KW |
1031 | static void test_blockjob_drain_all(void) |
1032 | { | |
d49725af | 1033 | test_blockjob_common(BDRV_DRAIN_ALL, false, TEST_JOB_SUCCESS); |
7253220d KW |
1034 | } |
1035 | ||
1036 | static void test_blockjob_drain(void) | |
1037 | { | |
d49725af | 1038 | test_blockjob_common(BDRV_DRAIN, false, TEST_JOB_SUCCESS); |
7253220d KW |
1039 | } |
1040 | ||
d2a85d0f KW |
1041 | static void test_blockjob_drain_subtree(void) |
1042 | { | |
d49725af KW |
1043 | test_blockjob_common(BDRV_SUBTREE_DRAIN, false, TEST_JOB_SUCCESS); |
1044 | } | |
1045 | ||
1046 | static void test_blockjob_error_drain_all(void) | |
1047 | { | |
1048 | test_blockjob_common(BDRV_DRAIN_ALL, false, TEST_JOB_FAIL_RUN); | |
1049 | test_blockjob_common(BDRV_DRAIN_ALL, false, TEST_JOB_FAIL_PREPARE); | |
1050 | } | |
1051 | ||
1052 | static void test_blockjob_error_drain(void) | |
1053 | { | |
1054 | test_blockjob_common(BDRV_DRAIN, false, TEST_JOB_FAIL_RUN); | |
1055 | test_blockjob_common(BDRV_DRAIN, false, TEST_JOB_FAIL_PREPARE); | |
1056 | } | |
1057 | ||
1058 | static void test_blockjob_error_drain_subtree(void) | |
1059 | { | |
1060 | test_blockjob_common(BDRV_SUBTREE_DRAIN, false, TEST_JOB_FAIL_RUN); | |
1061 | test_blockjob_common(BDRV_SUBTREE_DRAIN, false, TEST_JOB_FAIL_PREPARE); | |
f62c1729 KW |
1062 | } |
1063 | ||
1064 | static void test_blockjob_iothread_drain_all(void) | |
1065 | { | |
d49725af | 1066 | test_blockjob_common(BDRV_DRAIN_ALL, true, TEST_JOB_SUCCESS); |
f62c1729 KW |
1067 | } |
1068 | ||
1069 | static void test_blockjob_iothread_drain(void) | |
1070 | { | |
d49725af | 1071 | test_blockjob_common(BDRV_DRAIN, true, TEST_JOB_SUCCESS); |
f62c1729 KW |
1072 | } |
1073 | ||
1074 | static void test_blockjob_iothread_drain_subtree(void) | |
1075 | { | |
d49725af KW |
1076 | test_blockjob_common(BDRV_SUBTREE_DRAIN, true, TEST_JOB_SUCCESS); |
1077 | } | |
1078 | ||
1079 | static void test_blockjob_iothread_error_drain_all(void) | |
1080 | { | |
1081 | test_blockjob_common(BDRV_DRAIN_ALL, true, TEST_JOB_FAIL_RUN); | |
1082 | test_blockjob_common(BDRV_DRAIN_ALL, true, TEST_JOB_FAIL_PREPARE); | |
1083 | } | |
1084 | ||
1085 | static void test_blockjob_iothread_error_drain(void) | |
1086 | { | |
1087 | test_blockjob_common(BDRV_DRAIN, true, TEST_JOB_FAIL_RUN); | |
1088 | test_blockjob_common(BDRV_DRAIN, true, TEST_JOB_FAIL_PREPARE); | |
1089 | } | |
1090 | ||
1091 | static void test_blockjob_iothread_error_drain_subtree(void) | |
1092 | { | |
1093 | test_blockjob_common(BDRV_SUBTREE_DRAIN, true, TEST_JOB_FAIL_RUN); | |
1094 | test_blockjob_common(BDRV_SUBTREE_DRAIN, true, TEST_JOB_FAIL_PREPARE); | |
d2a85d0f KW |
1095 | } |
1096 | ||
4c8158e3 HR |
1097 | |
1098 | typedef struct BDRVTestTopState { | |
1099 | BdrvChild *wait_child; | |
1100 | } BDRVTestTopState; | |
1101 | ||
1102 | static void bdrv_test_top_close(BlockDriverState *bs) | |
1103 | { | |
1104 | BdrvChild *c, *next_c; | |
1105 | QLIST_FOREACH_SAFE(c, &bs->children, next, next_c) { | |
1106 | bdrv_unref_child(bs, c); | |
1107 | } | |
1108 | } | |
1109 | ||
1110 | static int coroutine_fn bdrv_test_top_co_preadv(BlockDriverState *bs, | |
1111 | uint64_t offset, uint64_t bytes, | |
1112 | QEMUIOVector *qiov, int flags) | |
1113 | { | |
1114 | BDRVTestTopState *tts = bs->opaque; | |
1115 | return bdrv_co_preadv(tts->wait_child, offset, bytes, qiov, flags); | |
1116 | } | |
1117 | ||
1118 | static BlockDriver bdrv_test_top_driver = { | |
1119 | .format_name = "test_top_driver", | |
1120 | .instance_size = sizeof(BDRVTestTopState), | |
1121 | ||
1122 | .bdrv_close = bdrv_test_top_close, | |
1123 | .bdrv_co_preadv = bdrv_test_top_co_preadv, | |
1124 | ||
1125 | .bdrv_child_perm = bdrv_format_default_perms, | |
1126 | }; | |
1127 | ||
1128 | typedef struct TestCoDeleteByDrainData { | |
1129 | BlockBackend *blk; | |
1130 | bool detach_instead_of_delete; | |
1131 | bool done; | |
1132 | } TestCoDeleteByDrainData; | |
1133 | ||
1134 | static void coroutine_fn test_co_delete_by_drain(void *opaque) | |
1135 | { | |
1136 | TestCoDeleteByDrainData *dbdd = opaque; | |
1137 | BlockBackend *blk = dbdd->blk; | |
1138 | BlockDriverState *bs = blk_bs(blk); | |
1139 | BDRVTestTopState *tts = bs->opaque; | |
1140 | void *buffer = g_malloc(65536); | |
405d8fe0 | 1141 | QEMUIOVector qiov = QEMU_IOVEC_INIT_BUF(qiov, buffer, 65536); |
4c8158e3 HR |
1142 | |
1143 | /* Pretend some internal write operation from parent to child. | |
1144 | * Important: We have to read from the child, not from the parent! | |
1145 | * Draining works by first propagating it all up the tree to the | |
1146 | * root and then waiting for drainage from root to the leaves | |
1147 | * (protocol nodes). If we have a request waiting on the root, | |
1148 | * everything will be drained before we go back down the tree, but | |
1149 | * we do not want that. We want to be in the middle of draining | |
1150 | * when this following requests returns. */ | |
1151 | bdrv_co_preadv(tts->wait_child, 0, 65536, &qiov, 0); | |
1152 | ||
1153 | g_assert_cmpint(bs->refcnt, ==, 1); | |
1154 | ||
1155 | if (!dbdd->detach_instead_of_delete) { | |
1156 | blk_unref(blk); | |
1157 | } else { | |
1158 | BdrvChild *c, *next_c; | |
1159 | QLIST_FOREACH_SAFE(c, &bs->children, next, next_c) { | |
1160 | bdrv_unref_child(bs, c); | |
1161 | } | |
1162 | } | |
1163 | ||
1164 | dbdd->done = true; | |
7b43db3c | 1165 | g_free(buffer); |
4c8158e3 HR |
1166 | } |
1167 | ||
1168 | /** | |
1169 | * Test what happens when some BDS has some children, you drain one of | |
1170 | * them and this results in the BDS being deleted. | |
1171 | * | |
1172 | * If @detach_instead_of_delete is set, the BDS is not going to be | |
1173 | * deleted but will only detach all of its children. | |
1174 | */ | |
ebd31837 KW |
1175 | static void do_test_delete_by_drain(bool detach_instead_of_delete, |
1176 | enum drain_type drain_type) | |
4c8158e3 HR |
1177 | { |
1178 | BlockBackend *blk; | |
1179 | BlockDriverState *bs, *child_bs, *null_bs; | |
1180 | BDRVTestTopState *tts; | |
1181 | TestCoDeleteByDrainData dbdd; | |
1182 | Coroutine *co; | |
1183 | ||
1184 | bs = bdrv_new_open_driver(&bdrv_test_top_driver, "top", BDRV_O_RDWR, | |
1185 | &error_abort); | |
1186 | bs->total_sectors = 65536 >> BDRV_SECTOR_BITS; | |
1187 | tts = bs->opaque; | |
1188 | ||
1189 | null_bs = bdrv_open("null-co://", NULL, NULL, BDRV_O_RDWR | BDRV_O_PROTOCOL, | |
1190 | &error_abort); | |
1191 | bdrv_attach_child(bs, null_bs, "null-child", &child_file, &error_abort); | |
1192 | ||
1193 | /* This child will be the one to pass to requests through to, and | |
1194 | * it will stall until a drain occurs */ | |
1195 | child_bs = bdrv_new_open_driver(&bdrv_test, "child", BDRV_O_RDWR, | |
1196 | &error_abort); | |
1197 | child_bs->total_sectors = 65536 >> BDRV_SECTOR_BITS; | |
1198 | /* Takes our reference to child_bs */ | |
1199 | tts->wait_child = bdrv_attach_child(bs, child_bs, "wait-child", &child_file, | |
1200 | &error_abort); | |
1201 | ||
1202 | /* This child is just there to be deleted | |
1203 | * (for detach_instead_of_delete == true) */ | |
1204 | null_bs = bdrv_open("null-co://", NULL, NULL, BDRV_O_RDWR | BDRV_O_PROTOCOL, | |
1205 | &error_abort); | |
1206 | bdrv_attach_child(bs, null_bs, "null-child", &child_file, &error_abort); | |
1207 | ||
1208 | blk = blk_new(BLK_PERM_ALL, BLK_PERM_ALL); | |
1209 | blk_insert_bs(blk, bs, &error_abort); | |
1210 | ||
1211 | /* Referenced by blk now */ | |
1212 | bdrv_unref(bs); | |
1213 | ||
1214 | g_assert_cmpint(bs->refcnt, ==, 1); | |
1215 | g_assert_cmpint(child_bs->refcnt, ==, 1); | |
1216 | g_assert_cmpint(null_bs->refcnt, ==, 1); | |
1217 | ||
1218 | ||
1219 | dbdd = (TestCoDeleteByDrainData){ | |
1220 | .blk = blk, | |
1221 | .detach_instead_of_delete = detach_instead_of_delete, | |
1222 | .done = false, | |
1223 | }; | |
1224 | co = qemu_coroutine_create(test_co_delete_by_drain, &dbdd); | |
1225 | qemu_coroutine_enter(co); | |
1226 | ||
1227 | /* Drain the child while the read operation is still pending. | |
1228 | * This should result in the operation finishing and | |
1229 | * test_co_delete_by_drain() resuming. Thus, @bs will be deleted | |
1230 | * and the coroutine will exit while this drain operation is still | |
1231 | * in progress. */ | |
ebd31837 KW |
1232 | switch (drain_type) { |
1233 | case BDRV_DRAIN: | |
1234 | bdrv_ref(child_bs); | |
1235 | bdrv_drain(child_bs); | |
1236 | bdrv_unref(child_bs); | |
1237 | break; | |
1238 | case BDRV_SUBTREE_DRAIN: | |
1239 | /* Would have to ref/unref bs here for !detach_instead_of_delete, but | |
1240 | * then the whole test becomes pointless because the graph changes | |
1241 | * don't occur during the drain any more. */ | |
1242 | assert(detach_instead_of_delete); | |
1243 | bdrv_subtree_drained_begin(bs); | |
1244 | bdrv_subtree_drained_end(bs); | |
1245 | break; | |
19f7a7e5 KW |
1246 | case BDRV_DRAIN_ALL: |
1247 | bdrv_drain_all_begin(); | |
1248 | bdrv_drain_all_end(); | |
1249 | break; | |
ebd31837 KW |
1250 | default: |
1251 | g_assert_not_reached(); | |
1252 | } | |
4c8158e3 HR |
1253 | |
1254 | while (!dbdd.done) { | |
1255 | aio_poll(qemu_get_aio_context(), true); | |
1256 | } | |
1257 | ||
1258 | if (detach_instead_of_delete) { | |
1259 | /* Here, the reference has not passed over to the coroutine, | |
1260 | * so we have to delete the BB ourselves */ | |
1261 | blk_unref(blk); | |
1262 | } | |
1263 | } | |
1264 | ||
4c8158e3 HR |
1265 | static void test_delete_by_drain(void) |
1266 | { | |
ebd31837 | 1267 | do_test_delete_by_drain(false, BDRV_DRAIN); |
4c8158e3 HR |
1268 | } |
1269 | ||
19f7a7e5 KW |
1270 | static void test_detach_by_drain_all(void) |
1271 | { | |
1272 | do_test_delete_by_drain(true, BDRV_DRAIN_ALL); | |
1273 | } | |
1274 | ||
4c8158e3 HR |
1275 | static void test_detach_by_drain(void) |
1276 | { | |
ebd31837 KW |
1277 | do_test_delete_by_drain(true, BDRV_DRAIN); |
1278 | } | |
1279 | ||
1280 | static void test_detach_by_drain_subtree(void) | |
1281 | { | |
1282 | do_test_delete_by_drain(true, BDRV_SUBTREE_DRAIN); | |
4c8158e3 HR |
1283 | } |
1284 | ||
1285 | ||
231281ab KW |
1286 | struct detach_by_parent_data { |
1287 | BlockDriverState *parent_b; | |
1288 | BdrvChild *child_b; | |
1289 | BlockDriverState *c; | |
1290 | BdrvChild *child_c; | |
57320ca9 | 1291 | bool by_parent_cb; |
231281ab | 1292 | }; |
57320ca9 | 1293 | static struct detach_by_parent_data detach_by_parent_data; |
231281ab | 1294 | |
57320ca9 | 1295 | static void detach_indirect_bh(void *opaque) |
231281ab KW |
1296 | { |
1297 | struct detach_by_parent_data *data = opaque; | |
1298 | ||
231281ab KW |
1299 | bdrv_unref_child(data->parent_b, data->child_b); |
1300 | ||
1301 | bdrv_ref(data->c); | |
1302 | data->child_c = bdrv_attach_child(data->parent_b, data->c, "PB-C", | |
1303 | &child_file, &error_abort); | |
1304 | } | |
1305 | ||
57320ca9 KW |
1306 | static void detach_by_parent_aio_cb(void *opaque, int ret) |
1307 | { | |
1308 | struct detach_by_parent_data *data = &detach_by_parent_data; | |
1309 | ||
1310 | g_assert_cmpint(ret, ==, 0); | |
1311 | if (data->by_parent_cb) { | |
1312 | detach_indirect_bh(data); | |
1313 | } | |
1314 | } | |
1315 | ||
1316 | static void detach_by_driver_cb_drained_begin(BdrvChild *child) | |
1317 | { | |
1318 | aio_bh_schedule_oneshot(qemu_get_current_aio_context(), | |
1319 | detach_indirect_bh, &detach_by_parent_data); | |
1320 | child_file.drained_begin(child); | |
1321 | } | |
1322 | ||
1323 | static BdrvChildRole detach_by_driver_cb_role; | |
1324 | ||
231281ab KW |
1325 | /* |
1326 | * Initial graph: | |
1327 | * | |
1328 | * PA PB | |
1329 | * \ / \ | |
1330 | * A B C | |
1331 | * | |
57320ca9 KW |
1332 | * by_parent_cb == true: Test that parent callbacks don't poll |
1333 | * | |
1334 | * PA has a pending write request whose callback changes the child nodes of | |
1335 | * PB: It removes B and adds C instead. The subtree of PB is drained, which | |
1336 | * will indirectly drain the write request, too. | |
1337 | * | |
1338 | * by_parent_cb == false: Test that bdrv_drain_invoke() doesn't poll | |
1339 | * | |
1340 | * PA's BdrvChildRole has a .drained_begin callback that schedules a BH | |
1341 | * that does the same graph change. If bdrv_drain_invoke() calls it, the | |
1342 | * state is messed up, but if it is only polled in the single | |
1343 | * BDRV_POLL_WHILE() at the end of the drain, this should work fine. | |
231281ab | 1344 | */ |
57320ca9 | 1345 | static void test_detach_indirect(bool by_parent_cb) |
231281ab KW |
1346 | { |
1347 | BlockBackend *blk; | |
1348 | BlockDriverState *parent_a, *parent_b, *a, *b, *c; | |
1349 | BdrvChild *child_a, *child_b; | |
1350 | BlockAIOCB *acb; | |
231281ab | 1351 | |
405d8fe0 | 1352 | QEMUIOVector qiov = QEMU_IOVEC_INIT_BUF(qiov, NULL, 0); |
231281ab | 1353 | |
57320ca9 KW |
1354 | if (!by_parent_cb) { |
1355 | detach_by_driver_cb_role = child_file; | |
1356 | detach_by_driver_cb_role.drained_begin = | |
1357 | detach_by_driver_cb_drained_begin; | |
1358 | } | |
1359 | ||
231281ab KW |
1360 | /* Create all involved nodes */ |
1361 | parent_a = bdrv_new_open_driver(&bdrv_test, "parent-a", BDRV_O_RDWR, | |
1362 | &error_abort); | |
1363 | parent_b = bdrv_new_open_driver(&bdrv_test, "parent-b", 0, | |
1364 | &error_abort); | |
1365 | ||
1366 | a = bdrv_new_open_driver(&bdrv_test, "a", BDRV_O_RDWR, &error_abort); | |
1367 | b = bdrv_new_open_driver(&bdrv_test, "b", BDRV_O_RDWR, &error_abort); | |
1368 | c = bdrv_new_open_driver(&bdrv_test, "c", BDRV_O_RDWR, &error_abort); | |
1369 | ||
1370 | /* blk is a BB for parent-a */ | |
1371 | blk = blk_new(BLK_PERM_ALL, BLK_PERM_ALL); | |
1372 | blk_insert_bs(blk, parent_a, &error_abort); | |
1373 | bdrv_unref(parent_a); | |
1374 | ||
57320ca9 KW |
1375 | /* If we want to get bdrv_drain_invoke() to call aio_poll(), the driver |
1376 | * callback must not return immediately. */ | |
1377 | if (!by_parent_cb) { | |
1378 | BDRVTestState *s = parent_a->opaque; | |
1379 | s->sleep_in_drain_begin = true; | |
1380 | } | |
1381 | ||
231281ab KW |
1382 | /* Set child relationships */ |
1383 | bdrv_ref(b); | |
1384 | bdrv_ref(a); | |
1385 | child_b = bdrv_attach_child(parent_b, b, "PB-B", &child_file, &error_abort); | |
1386 | child_a = bdrv_attach_child(parent_b, a, "PB-A", &child_backing, &error_abort); | |
1387 | ||
1388 | bdrv_ref(a); | |
57320ca9 KW |
1389 | bdrv_attach_child(parent_a, a, "PA-A", |
1390 | by_parent_cb ? &child_file : &detach_by_driver_cb_role, | |
1391 | &error_abort); | |
231281ab KW |
1392 | |
1393 | g_assert_cmpint(parent_a->refcnt, ==, 1); | |
1394 | g_assert_cmpint(parent_b->refcnt, ==, 1); | |
1395 | g_assert_cmpint(a->refcnt, ==, 3); | |
1396 | g_assert_cmpint(b->refcnt, ==, 2); | |
1397 | g_assert_cmpint(c->refcnt, ==, 1); | |
1398 | ||
1399 | g_assert(QLIST_FIRST(&parent_b->children) == child_a); | |
1400 | g_assert(QLIST_NEXT(child_a, next) == child_b); | |
1401 | g_assert(QLIST_NEXT(child_b, next) == NULL); | |
1402 | ||
1403 | /* Start the evil write request */ | |
57320ca9 | 1404 | detach_by_parent_data = (struct detach_by_parent_data) { |
231281ab KW |
1405 | .parent_b = parent_b, |
1406 | .child_b = child_b, | |
1407 | .c = c, | |
57320ca9 | 1408 | .by_parent_cb = by_parent_cb, |
231281ab | 1409 | }; |
57320ca9 | 1410 | acb = blk_aio_preadv(blk, 0, &qiov, 0, detach_by_parent_aio_cb, NULL); |
231281ab KW |
1411 | g_assert(acb != NULL); |
1412 | ||
1413 | /* Drain and check the expected result */ | |
1414 | bdrv_subtree_drained_begin(parent_b); | |
1415 | ||
57320ca9 | 1416 | g_assert(detach_by_parent_data.child_c != NULL); |
231281ab KW |
1417 | |
1418 | g_assert_cmpint(parent_a->refcnt, ==, 1); | |
1419 | g_assert_cmpint(parent_b->refcnt, ==, 1); | |
1420 | g_assert_cmpint(a->refcnt, ==, 3); | |
1421 | g_assert_cmpint(b->refcnt, ==, 1); | |
1422 | g_assert_cmpint(c->refcnt, ==, 2); | |
1423 | ||
57320ca9 KW |
1424 | g_assert(QLIST_FIRST(&parent_b->children) == detach_by_parent_data.child_c); |
1425 | g_assert(QLIST_NEXT(detach_by_parent_data.child_c, next) == child_a); | |
231281ab KW |
1426 | g_assert(QLIST_NEXT(child_a, next) == NULL); |
1427 | ||
1428 | g_assert_cmpint(parent_a->quiesce_counter, ==, 1); | |
1429 | g_assert_cmpint(parent_b->quiesce_counter, ==, 1); | |
1430 | g_assert_cmpint(a->quiesce_counter, ==, 1); | |
1431 | g_assert_cmpint(b->quiesce_counter, ==, 0); | |
1432 | g_assert_cmpint(c->quiesce_counter, ==, 1); | |
1433 | ||
1434 | bdrv_subtree_drained_end(parent_b); | |
1435 | ||
1436 | bdrv_unref(parent_b); | |
1437 | blk_unref(blk); | |
1438 | ||
231281ab KW |
1439 | g_assert_cmpint(a->refcnt, ==, 1); |
1440 | g_assert_cmpint(b->refcnt, ==, 1); | |
1441 | g_assert_cmpint(c->refcnt, ==, 1); | |
1442 | bdrv_unref(a); | |
1443 | bdrv_unref(b); | |
1444 | bdrv_unref(c); | |
1445 | } | |
1446 | ||
57320ca9 KW |
1447 | static void test_detach_by_parent_cb(void) |
1448 | { | |
1449 | test_detach_indirect(true); | |
1450 | } | |
1451 | ||
1452 | static void test_detach_by_driver_cb(void) | |
1453 | { | |
1454 | test_detach_indirect(false); | |
1455 | } | |
231281ab | 1456 | |
b994c5bc KW |
1457 | static void test_append_to_drained(void) |
1458 | { | |
1459 | BlockBackend *blk; | |
1460 | BlockDriverState *base, *overlay; | |
1461 | BDRVTestState *base_s, *overlay_s; | |
1462 | ||
1463 | blk = blk_new(BLK_PERM_ALL, BLK_PERM_ALL); | |
1464 | base = bdrv_new_open_driver(&bdrv_test, "base", BDRV_O_RDWR, &error_abort); | |
1465 | base_s = base->opaque; | |
1466 | blk_insert_bs(blk, base, &error_abort); | |
1467 | ||
1468 | overlay = bdrv_new_open_driver(&bdrv_test, "overlay", BDRV_O_RDWR, | |
1469 | &error_abort); | |
1470 | overlay_s = overlay->opaque; | |
1471 | ||
1472 | do_drain_begin(BDRV_DRAIN, base); | |
1473 | g_assert_cmpint(base->quiesce_counter, ==, 1); | |
1474 | g_assert_cmpint(base_s->drain_count, ==, 1); | |
1475 | g_assert_cmpint(base->in_flight, ==, 0); | |
1476 | ||
1477 | /* Takes ownership of overlay, so we don't have to unref it later */ | |
1478 | bdrv_append(overlay, base, &error_abort); | |
1479 | g_assert_cmpint(base->in_flight, ==, 0); | |
1480 | g_assert_cmpint(overlay->in_flight, ==, 0); | |
1481 | ||
1482 | g_assert_cmpint(base->quiesce_counter, ==, 1); | |
1483 | g_assert_cmpint(base_s->drain_count, ==, 1); | |
1484 | g_assert_cmpint(overlay->quiesce_counter, ==, 1); | |
1485 | g_assert_cmpint(overlay_s->drain_count, ==, 1); | |
1486 | ||
1487 | do_drain_end(BDRV_DRAIN, base); | |
1488 | ||
1489 | g_assert_cmpint(base->quiesce_counter, ==, 0); | |
1490 | g_assert_cmpint(base_s->drain_count, ==, 0); | |
1491 | g_assert_cmpint(overlay->quiesce_counter, ==, 0); | |
1492 | g_assert_cmpint(overlay_s->drain_count, ==, 0); | |
1493 | ||
1494 | bdrv_unref(base); | |
1495 | blk_unref(blk); | |
1496 | } | |
1497 | ||
247d2737 KW |
1498 | static void test_set_aio_context(void) |
1499 | { | |
1500 | BlockDriverState *bs; | |
1501 | IOThread *a = iothread_new(); | |
1502 | IOThread *b = iothread_new(); | |
1503 | AioContext *ctx_a = iothread_get_aio_context(a); | |
1504 | AioContext *ctx_b = iothread_get_aio_context(b); | |
1505 | ||
1506 | bs = bdrv_new_open_driver(&bdrv_test, "test-node", BDRV_O_RDWR, | |
1507 | &error_abort); | |
1508 | ||
1509 | bdrv_drained_begin(bs); | |
1510 | bdrv_set_aio_context(bs, ctx_a); | |
1511 | ||
1512 | aio_context_acquire(ctx_a); | |
1513 | bdrv_drained_end(bs); | |
1514 | ||
1515 | bdrv_drained_begin(bs); | |
1516 | bdrv_set_aio_context(bs, ctx_b); | |
1517 | aio_context_release(ctx_a); | |
1518 | aio_context_acquire(ctx_b); | |
1519 | bdrv_set_aio_context(bs, qemu_get_aio_context()); | |
1520 | aio_context_release(ctx_b); | |
1521 | bdrv_drained_end(bs); | |
1522 | ||
1523 | bdrv_unref(bs); | |
1524 | iothread_join(a); | |
1525 | iothread_join(b); | |
1526 | } | |
1527 | ||
881cfd17 KW |
1528 | int main(int argc, char **argv) |
1529 | { | |
bb675689 KW |
1530 | int ret; |
1531 | ||
881cfd17 KW |
1532 | bdrv_init(); |
1533 | qemu_init_main_loop(&error_abort); | |
1534 | ||
1535 | g_test_init(&argc, &argv, NULL); | |
bb675689 | 1536 | qemu_event_init(&done_event, false); |
881cfd17 KW |
1537 | |
1538 | g_test_add_func("/bdrv-drain/driver-cb/drain_all", test_drv_cb_drain_all); | |
86e1c840 | 1539 | g_test_add_func("/bdrv-drain/driver-cb/drain", test_drv_cb_drain); |
d2a85d0f KW |
1540 | g_test_add_func("/bdrv-drain/driver-cb/drain_subtree", |
1541 | test_drv_cb_drain_subtree); | |
881cfd17 | 1542 | |
6d0252f2 KW |
1543 | g_test_add_func("/bdrv-drain/driver-cb/co/drain_all", |
1544 | test_drv_cb_co_drain_all); | |
0582eb10 KW |
1545 | g_test_add_func("/bdrv-drain/driver-cb/co/drain", test_drv_cb_co_drain); |
1546 | g_test_add_func("/bdrv-drain/driver-cb/co/drain_subtree", | |
1547 | test_drv_cb_co_drain_subtree); | |
1548 | ||
1549 | ||
89a6ceab KW |
1550 | g_test_add_func("/bdrv-drain/quiesce/drain_all", test_quiesce_drain_all); |
1551 | g_test_add_func("/bdrv-drain/quiesce/drain", test_quiesce_drain); | |
d2a85d0f KW |
1552 | g_test_add_func("/bdrv-drain/quiesce/drain_subtree", |
1553 | test_quiesce_drain_subtree); | |
89a6ceab | 1554 | |
6d0252f2 KW |
1555 | g_test_add_func("/bdrv-drain/quiesce/co/drain_all", |
1556 | test_quiesce_co_drain_all); | |
0582eb10 KW |
1557 | g_test_add_func("/bdrv-drain/quiesce/co/drain", test_quiesce_co_drain); |
1558 | g_test_add_func("/bdrv-drain/quiesce/co/drain_subtree", | |
1559 | test_quiesce_co_drain_subtree); | |
1560 | ||
6c429a6a | 1561 | g_test_add_func("/bdrv-drain/nested", test_nested); |
27e64474 | 1562 | g_test_add_func("/bdrv-drain/multiparent", test_multiparent); |
19f7a7e5 KW |
1563 | |
1564 | g_test_add_func("/bdrv-drain/graph-change/drain_subtree", | |
1565 | test_graph_change_drain_subtree); | |
1566 | g_test_add_func("/bdrv-drain/graph-change/drain_all", | |
1567 | test_graph_change_drain_all); | |
6c429a6a | 1568 | |
bb675689 KW |
1569 | g_test_add_func("/bdrv-drain/iothread/drain_all", test_iothread_drain_all); |
1570 | g_test_add_func("/bdrv-drain/iothread/drain", test_iothread_drain); | |
1571 | g_test_add_func("/bdrv-drain/iothread/drain_subtree", | |
1572 | test_iothread_drain_subtree); | |
1573 | ||
7253220d KW |
1574 | g_test_add_func("/bdrv-drain/blockjob/drain_all", test_blockjob_drain_all); |
1575 | g_test_add_func("/bdrv-drain/blockjob/drain", test_blockjob_drain); | |
d2a85d0f KW |
1576 | g_test_add_func("/bdrv-drain/blockjob/drain_subtree", |
1577 | test_blockjob_drain_subtree); | |
7253220d | 1578 | |
d49725af KW |
1579 | g_test_add_func("/bdrv-drain/blockjob/error/drain_all", |
1580 | test_blockjob_error_drain_all); | |
1581 | g_test_add_func("/bdrv-drain/blockjob/error/drain", | |
1582 | test_blockjob_error_drain); | |
1583 | g_test_add_func("/bdrv-drain/blockjob/error/drain_subtree", | |
1584 | test_blockjob_error_drain_subtree); | |
1585 | ||
f62c1729 KW |
1586 | g_test_add_func("/bdrv-drain/blockjob/iothread/drain_all", |
1587 | test_blockjob_iothread_drain_all); | |
1588 | g_test_add_func("/bdrv-drain/blockjob/iothread/drain", | |
1589 | test_blockjob_iothread_drain); | |
1590 | g_test_add_func("/bdrv-drain/blockjob/iothread/drain_subtree", | |
1591 | test_blockjob_iothread_drain_subtree); | |
1592 | ||
d49725af KW |
1593 | g_test_add_func("/bdrv-drain/blockjob/iothread/error/drain_all", |
1594 | test_blockjob_iothread_error_drain_all); | |
1595 | g_test_add_func("/bdrv-drain/blockjob/iothread/error/drain", | |
1596 | test_blockjob_iothread_error_drain); | |
1597 | g_test_add_func("/bdrv-drain/blockjob/iothread/error/drain_subtree", | |
1598 | test_blockjob_iothread_error_drain_subtree); | |
1599 | ||
ebd31837 | 1600 | g_test_add_func("/bdrv-drain/deletion/drain", test_delete_by_drain); |
19f7a7e5 | 1601 | g_test_add_func("/bdrv-drain/detach/drain_all", test_detach_by_drain_all); |
ebd31837 KW |
1602 | g_test_add_func("/bdrv-drain/detach/drain", test_detach_by_drain); |
1603 | g_test_add_func("/bdrv-drain/detach/drain_subtree", test_detach_by_drain_subtree); | |
231281ab | 1604 | g_test_add_func("/bdrv-drain/detach/parent_cb", test_detach_by_parent_cb); |
57320ca9 | 1605 | g_test_add_func("/bdrv-drain/detach/driver_cb", test_detach_by_driver_cb); |
4c8158e3 | 1606 | |
b994c5bc KW |
1607 | g_test_add_func("/bdrv-drain/attach/drain", test_append_to_drained); |
1608 | ||
247d2737 KW |
1609 | g_test_add_func("/bdrv-drain/set_aio_context", test_set_aio_context); |
1610 | ||
bb675689 KW |
1611 | ret = g_test_run(); |
1612 | qemu_event_destroy(&done_event); | |
1613 | return ret; | |
881cfd17 | 1614 | } |