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