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