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